2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2008 Pierre Ossman
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
17 * Author: Andrew Christian
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
34 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
37 #include <linux/pm_runtime.h>
39 #include <linux/mmc/ioctl.h>
40 #include <linux/mmc/card.h>
41 #include <linux/mmc/host.h>
42 #include <linux/mmc/mmc.h>
43 #include <linux/mmc/sd.h>
45 #include <asm/uaccess.h>
49 MODULE_ALIAS("mmc:block");
50 #ifdef MODULE_PARAM_PREFIX
51 #undef MODULE_PARAM_PREFIX
53 #define MODULE_PARAM_PREFIX "mmcblk."
55 #define INAND_CMD38_ARG_EXT_CSD 113
56 #define INAND_CMD38_ARG_ERASE 0x00
57 #define INAND_CMD38_ARG_TRIM 0x01
58 #define INAND_CMD38_ARG_SECERASE 0x80
59 #define INAND_CMD38_ARG_SECTRIM1 0x81
60 #define INAND_CMD38_ARG_SECTRIM2 0x88
61 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
62 #define MMC_SANITIZE_REQ_TIMEOUT 240000
63 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
65 #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
66 (req->cmd_flags & REQ_META)) && \
67 (rq_data_dir(req) == WRITE))
68 #define PACKED_CMD_VER 0x01
69 #define PACKED_CMD_WR 0x02
71 static DEFINE_MUTEX(block_mutex);
74 * The defaults come from config options but can be overriden by module
77 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
80 * We've only got one major, so number of mmcblk devices is
81 * limited to 256 / number of minors per device.
83 static int max_devices;
85 /* 256 minors, so at most 256 separate devices */
86 static DECLARE_BITMAP(dev_use, 256);
87 static DECLARE_BITMAP(name_use, 256);
90 * There is one mmc_blk_data per slot.
95 struct mmc_queue queue;
96 struct list_head part;
99 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
100 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
101 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
104 unsigned int read_only;
105 unsigned int part_type;
106 unsigned int name_idx;
107 unsigned int reset_done;
108 #define MMC_BLK_READ BIT(0)
109 #define MMC_BLK_WRITE BIT(1)
110 #define MMC_BLK_DISCARD BIT(2)
111 #define MMC_BLK_SECDISCARD BIT(3)
114 * Only set in main mmc_blk_data associated
115 * with mmc_card with mmc_set_drvdata, and keeps
116 * track of the current selected device partition.
118 unsigned int part_curr;
119 struct device_attribute force_ro;
120 struct device_attribute power_ro_lock;
124 static DEFINE_MUTEX(open_lock);
127 MMC_PACKED_NR_IDX = -1,
129 MMC_PACKED_NR_SINGLE,
132 module_param(perdev_minors, int, 0444);
133 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
135 static inline int mmc_blk_part_switch(struct mmc_card *card,
136 struct mmc_blk_data *md);
137 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
139 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
141 struct mmc_packed *packed = mqrq->packed;
145 mqrq->cmd_type = MMC_PACKED_NONE;
146 packed->nr_entries = MMC_PACKED_NR_ZERO;
147 packed->idx_failure = MMC_PACKED_NR_IDX;
152 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
154 struct mmc_blk_data *md;
156 mutex_lock(&open_lock);
157 md = disk->private_data;
158 if (md && md->usage == 0)
162 mutex_unlock(&open_lock);
167 static inline int mmc_get_devidx(struct gendisk *disk)
169 int devmaj = MAJOR(disk_devt(disk));
170 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
173 devidx = disk->first_minor / perdev_minors;
177 static void mmc_blk_put(struct mmc_blk_data *md)
179 mutex_lock(&open_lock);
181 if (md->usage == 0) {
182 int devidx = mmc_get_devidx(md->disk);
183 blk_cleanup_queue(md->queue.queue);
185 __clear_bit(devidx, dev_use);
190 mutex_unlock(&open_lock);
193 static ssize_t power_ro_lock_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
197 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
198 struct mmc_card *card = md->queue.card;
201 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
203 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
206 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
211 static ssize_t power_ro_lock_store(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
215 struct mmc_blk_data *md, *part_md;
216 struct mmc_card *card;
219 if (kstrtoul(buf, 0, &set))
225 md = mmc_blk_get(dev_to_disk(dev));
226 card = md->queue.card;
230 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
231 card->ext_csd.boot_ro_lock |
232 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
233 card->ext_csd.part_time);
235 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
237 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md->disk->disk_name);
244 set_disk_ro(md->disk, 1);
246 list_for_each_entry(part_md, &md->part, part)
247 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
249 set_disk_ro(part_md->disk, 1);
257 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
263 ret = snprintf(buf, PAGE_SIZE, "%d",
264 get_disk_ro(dev_to_disk(dev)) ^
270 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
275 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
276 unsigned long set = simple_strtoul(buf, &end, 0);
282 set_disk_ro(dev_to_disk(dev), set || md->read_only);
289 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
291 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
294 mutex_lock(&block_mutex);
297 check_disk_change(bdev);
300 if ((mode & FMODE_WRITE) && md->read_only) {
305 mutex_unlock(&block_mutex);
310 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
312 struct mmc_blk_data *md = disk->private_data;
314 mutex_lock(&block_mutex);
316 mutex_unlock(&block_mutex);
320 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
322 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
328 struct mmc_blk_ioc_data {
329 struct mmc_ioc_cmd ic;
334 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
335 struct mmc_ioc_cmd __user *user)
337 struct mmc_blk_ioc_data *idata;
340 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
346 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
351 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
352 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
357 if (!idata->buf_bytes)
360 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
366 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
367 idata->ic.data_ptr, idata->buf_bytes)) {
382 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
388 if (!status || !retries_max)
392 err = get_card_status(card, status, 5);
396 if (!R1_STATUS(*status) &&
397 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
398 break; /* RPMB programming operation complete */
401 * Rechedule to give the MMC device a chance to continue
402 * processing the previous command without being polled too
405 usleep_range(1000, 5000);
406 } while (++retry_count < retries_max);
408 if (retry_count == retries_max)
414 static int ioctl_do_sanitize(struct mmc_card *card)
418 if (!(mmc_can_sanitize(card) &&
419 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
420 pr_warn("%s: %s - SANITIZE is not supported\n",
421 mmc_hostname(card->host), __func__);
426 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
427 mmc_hostname(card->host), __func__);
429 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
430 EXT_CSD_SANITIZE_START, 1,
431 MMC_SANITIZE_REQ_TIMEOUT);
434 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
435 mmc_hostname(card->host), __func__, err);
437 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
443 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
444 struct mmc_ioc_cmd __user *ic_ptr)
446 struct mmc_blk_ioc_data *idata;
447 struct mmc_blk_data *md;
448 struct mmc_card *card;
449 struct mmc_command cmd = {0};
450 struct mmc_data data = {0};
451 struct mmc_request mrq = {NULL};
452 struct scatterlist sg;
458 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
459 * whole block device, not on a partition. This prevents overspray
460 * between sibling partitions.
462 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
465 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
467 return PTR_ERR(idata);
469 md = mmc_blk_get(bdev->bd_disk);
475 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
478 card = md->queue.card;
484 cmd.opcode = idata->ic.opcode;
485 cmd.arg = idata->ic.arg;
486 cmd.flags = idata->ic.flags;
488 if (idata->buf_bytes) {
491 data.blksz = idata->ic.blksz;
492 data.blocks = idata->ic.blocks;
494 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
496 if (idata->ic.write_flag)
497 data.flags = MMC_DATA_WRITE;
499 data.flags = MMC_DATA_READ;
501 /* data.flags must already be set before doing this. */
502 mmc_set_data_timeout(&data, card);
504 /* Allow overriding the timeout_ns for empirical tuning. */
505 if (idata->ic.data_timeout_ns)
506 data.timeout_ns = idata->ic.data_timeout_ns;
508 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
510 * Pretend this is a data transfer and rely on the
511 * host driver to compute timeout. When all host
512 * drivers support cmd.cmd_timeout for R1B, this
516 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
518 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
528 err = mmc_blk_part_switch(card, md);
532 if (idata->ic.is_acmd) {
533 err = mmc_app_cmd(card->host, card);
539 err = mmc_set_blockcount(card, data.blocks,
540 idata->ic.write_flag & (1 << 31));
545 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
546 (cmd.opcode == MMC_SWITCH)) {
547 err = ioctl_do_sanitize(card);
550 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
556 mmc_wait_for_req(card->host, &mrq);
559 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
560 __func__, cmd.error);
565 dev_err(mmc_dev(card->host), "%s: data error %d\n",
566 __func__, data.error);
572 * According to the SD specs, some commands require a delay after
573 * issuing the command.
575 if (idata->ic.postsleep_min_us)
576 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
578 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
583 if (!idata->ic.write_flag) {
584 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
585 idata->buf, idata->buf_bytes)) {
593 * Ensure RPMB command has completed by polling CMD13
596 err = ioctl_rpmb_card_status_poll(card, &status, 5);
598 dev_err(mmc_dev(card->host),
599 "%s: Card Status=0x%08X, error %d\n",
600 __func__, status, err);
614 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
615 unsigned int cmd, unsigned long arg)
618 if (cmd == MMC_IOC_CMD)
619 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
624 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
625 unsigned int cmd, unsigned long arg)
627 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
631 static const struct block_device_operations mmc_bdops = {
632 .open = mmc_blk_open,
633 .release = mmc_blk_release,
634 .getgeo = mmc_blk_getgeo,
635 .owner = THIS_MODULE,
636 .ioctl = mmc_blk_ioctl,
638 .compat_ioctl = mmc_blk_compat_ioctl,
642 static inline int mmc_blk_part_switch(struct mmc_card *card,
643 struct mmc_blk_data *md)
646 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
648 if (main_md->part_curr == md->part_type)
651 if (mmc_card_mmc(card)) {
652 u8 part_config = card->ext_csd.part_config;
654 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
655 part_config |= md->part_type;
657 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
658 EXT_CSD_PART_CONFIG, part_config,
659 card->ext_csd.part_time);
663 card->ext_csd.part_config = part_config;
666 main_md->part_curr = md->part_type;
670 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
676 struct mmc_request mrq = {NULL};
677 struct mmc_command cmd = {0};
678 struct mmc_data data = {0};
680 struct scatterlist sg;
682 cmd.opcode = MMC_APP_CMD;
683 cmd.arg = card->rca << 16;
684 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
686 err = mmc_wait_for_cmd(card->host, &cmd, 0);
689 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
692 memset(&cmd, 0, sizeof(struct mmc_command));
694 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
696 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
700 data.flags = MMC_DATA_READ;
703 mmc_set_data_timeout(&data, card);
708 blocks = kmalloc(4, GFP_KERNEL);
712 sg_init_one(&sg, blocks, 4);
714 mmc_wait_for_req(card->host, &mrq);
716 result = ntohl(*blocks);
719 if (cmd.error || data.error)
725 static int send_stop(struct mmc_card *card, u32 *status)
727 struct mmc_command cmd = {0};
730 cmd.opcode = MMC_STOP_TRANSMISSION;
731 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
732 err = mmc_wait_for_cmd(card->host, &cmd, 5);
734 *status = cmd.resp[0];
738 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
740 struct mmc_command cmd = {0};
743 cmd.opcode = MMC_SEND_STATUS;
744 if (!mmc_host_is_spi(card->host))
745 cmd.arg = card->rca << 16;
746 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
747 err = mmc_wait_for_cmd(card->host, &cmd, retries);
749 *status = cmd.resp[0];
753 #define ERR_NOMEDIUM 3
756 #define ERR_CONTINUE 0
758 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
759 bool status_valid, u32 status)
763 /* response crc error, retry the r/w cmd */
764 pr_err("%s: %s sending %s command, card status %#x\n",
765 req->rq_disk->disk_name, "response CRC error",
770 pr_err("%s: %s sending %s command, card status %#x\n",
771 req->rq_disk->disk_name, "timed out", name, status);
773 /* If the status cmd initially failed, retry the r/w cmd */
778 * If it was a r/w cmd crc error, or illegal command
779 * (eg, issued in wrong state) then retry - we should
780 * have corrected the state problem above.
782 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
785 /* Otherwise abort the command */
789 /* We don't understand the error code the driver gave us */
790 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
791 req->rq_disk->disk_name, error, status);
797 * Initial r/w and stop cmd error recovery.
798 * We don't know whether the card received the r/w cmd or not, so try to
799 * restore things back to a sane state. Essentially, we do this as follows:
800 * - Obtain card status. If the first attempt to obtain card status fails,
801 * the status word will reflect the failed status cmd, not the failed
802 * r/w cmd. If we fail to obtain card status, it suggests we can no
803 * longer communicate with the card.
804 * - Check the card state. If the card received the cmd but there was a
805 * transient problem with the response, it might still be in a data transfer
806 * mode. Try to send it a stop command. If this fails, we can't recover.
807 * - If the r/w cmd failed due to a response CRC error, it was probably
808 * transient, so retry the cmd.
809 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
810 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
811 * illegal cmd, retry.
812 * Otherwise we don't understand what happened, so abort.
814 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
815 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
817 bool prev_cmd_status_valid = true;
818 u32 status, stop_status = 0;
821 if (mmc_card_removed(card))
825 * Try to get card status which indicates both the card state
826 * and why there was no response. If the first attempt fails,
827 * we can't be sure the returned status is for the r/w command.
829 for (retry = 2; retry >= 0; retry--) {
830 err = get_card_status(card, &status, 0);
834 prev_cmd_status_valid = false;
835 pr_err("%s: error %d sending status command, %sing\n",
836 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
839 /* We couldn't get a response from the card. Give up. */
841 /* Check if the card is removed */
842 if (mmc_detect_card_removed(card->host))
847 /* Flag ECC errors */
848 if ((status & R1_CARD_ECC_FAILED) ||
849 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
850 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
853 /* Flag General errors */
854 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
855 if ((status & R1_ERROR) ||
856 (brq->stop.resp[0] & R1_ERROR)) {
857 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
858 req->rq_disk->disk_name, __func__,
859 brq->stop.resp[0], status);
864 * Check the current card state. If it is in some data transfer
865 * mode, tell it to stop (and hopefully transition back to TRAN.)
867 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
868 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
869 err = send_stop(card, &stop_status);
871 pr_err("%s: error %d sending stop command\n",
872 req->rq_disk->disk_name, err);
875 * If the stop cmd also timed out, the card is probably
876 * not present, so abort. Other errors are bad news too.
880 if (stop_status & R1_CARD_ECC_FAILED)
882 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
883 if (stop_status & R1_ERROR) {
884 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
885 req->rq_disk->disk_name, __func__,
891 /* Check for set block count errors */
893 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
894 prev_cmd_status_valid, status);
896 /* Check for r/w command errors */
898 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
899 prev_cmd_status_valid, status);
902 if (!brq->stop.error)
905 /* Now for stop errors. These aren't fatal to the transfer. */
906 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
907 req->rq_disk->disk_name, brq->stop.error,
908 brq->cmd.resp[0], status);
911 * Subsitute in our own stop status as this will give the error
912 * state which happened during the execution of the r/w command.
915 brq->stop.resp[0] = stop_status;
921 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
926 if (md->reset_done & type)
929 md->reset_done |= type;
930 err = mmc_hw_reset(host);
931 /* Ensure we switch back to the correct partition */
932 if (err != -EOPNOTSUPP) {
933 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
936 main_md->part_curr = main_md->part_type;
937 part_err = mmc_blk_part_switch(host->card, md);
940 * We have failed to get back into the correct
941 * partition, so we need to abort the whole request.
949 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
951 md->reset_done &= ~type;
954 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
956 struct mmc_blk_data *md = mq->data;
957 struct mmc_card *card = md->queue.card;
958 unsigned int from, nr, arg;
959 int err = 0, type = MMC_BLK_DISCARD;
961 if (!mmc_can_erase(card)) {
966 from = blk_rq_pos(req);
967 nr = blk_rq_sectors(req);
969 if (mmc_can_discard(card))
970 arg = MMC_DISCARD_ARG;
971 else if (mmc_can_trim(card))
976 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
977 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
978 INAND_CMD38_ARG_EXT_CSD,
979 arg == MMC_TRIM_ARG ?
980 INAND_CMD38_ARG_TRIM :
981 INAND_CMD38_ARG_ERASE,
986 err = mmc_erase(card, from, nr, arg);
988 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
991 mmc_blk_reset_success(md, type);
992 blk_end_request(req, err, blk_rq_bytes(req));
997 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1000 struct mmc_blk_data *md = mq->data;
1001 struct mmc_card *card = md->queue.card;
1002 unsigned int from, nr, arg;
1003 int err = 0, type = MMC_BLK_SECDISCARD;
1005 if (!(mmc_can_secure_erase_trim(card))) {
1010 from = blk_rq_pos(req);
1011 nr = blk_rq_sectors(req);
1013 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1014 arg = MMC_SECURE_TRIM1_ARG;
1016 arg = MMC_SECURE_ERASE_ARG;
1019 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1020 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1021 INAND_CMD38_ARG_EXT_CSD,
1022 arg == MMC_SECURE_TRIM1_ARG ?
1023 INAND_CMD38_ARG_SECTRIM1 :
1024 INAND_CMD38_ARG_SECERASE,
1030 err = mmc_erase(card, from, nr, arg);
1036 if (arg == MMC_SECURE_TRIM1_ARG) {
1037 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1038 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1039 INAND_CMD38_ARG_EXT_CSD,
1040 INAND_CMD38_ARG_SECTRIM2,
1046 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1054 if (err && !mmc_blk_reset(md, card->host, type))
1057 mmc_blk_reset_success(md, type);
1059 blk_end_request(req, err, blk_rq_bytes(req));
1064 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1066 struct mmc_blk_data *md = mq->data;
1067 struct mmc_card *card = md->queue.card;
1070 ret = mmc_flush_cache(card);
1074 blk_end_request_all(req, ret);
1080 * Reformat current write as a reliable write, supporting
1081 * both legacy and the enhanced reliable write MMC cards.
1082 * In each transfer we'll handle only as much as a single
1083 * reliable write can handle, thus finish the request in
1084 * partial completions.
1086 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1087 struct mmc_card *card,
1088 struct request *req)
1090 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1091 /* Legacy mode imposes restrictions on transfers. */
1092 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1093 brq->data.blocks = 1;
1095 if (brq->data.blocks > card->ext_csd.rel_sectors)
1096 brq->data.blocks = card->ext_csd.rel_sectors;
1097 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1098 brq->data.blocks = 1;
1102 #define CMD_ERRORS \
1103 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1104 R1_ADDRESS_ERROR | /* Misaligned address */ \
1105 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1106 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1107 R1_CC_ERROR | /* Card controller error */ \
1108 R1_ERROR) /* General/unknown error */
1110 static int mmc_blk_err_check(struct mmc_card *card,
1111 struct mmc_async_req *areq)
1113 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1115 struct mmc_blk_request *brq = &mq_mrq->brq;
1116 struct request *req = mq_mrq->req;
1117 int ecc_err = 0, gen_err = 0;
1120 * sbc.error indicates a problem with the set block count
1121 * command. No data will have been transferred.
1123 * cmd.error indicates a problem with the r/w command. No
1124 * data will have been transferred.
1126 * stop.error indicates a problem with the stop command. Data
1127 * may have been transferred, or may still be transferring.
1129 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1131 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1133 return MMC_BLK_RETRY;
1135 return MMC_BLK_ABORT;
1137 return MMC_BLK_NOMEDIUM;
1144 * Check for errors relating to the execution of the
1145 * initial command - such as address errors. No data
1146 * has been transferred.
1148 if (brq->cmd.resp[0] & CMD_ERRORS) {
1149 pr_err("%s: r/w command failed, status = %#x\n",
1150 req->rq_disk->disk_name, brq->cmd.resp[0]);
1151 return MMC_BLK_ABORT;
1155 * Everything else is either success, or a data error of some
1156 * kind. If it was a write, we may have transitioned to
1157 * program mode, which we have to wait for it to complete.
1159 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1161 unsigned long timeout;
1163 /* Check stop command response */
1164 if (brq->stop.resp[0] & R1_ERROR) {
1165 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1166 req->rq_disk->disk_name, __func__,
1171 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1173 int err = get_card_status(card, &status, 5);
1175 pr_err("%s: error %d requesting status\n",
1176 req->rq_disk->disk_name, err);
1177 return MMC_BLK_CMD_ERR;
1180 if (status & R1_ERROR) {
1181 pr_err("%s: %s: general error sending status command, card status %#x\n",
1182 req->rq_disk->disk_name, __func__,
1187 /* Timeout if the device never becomes ready for data
1188 * and never leaves the program state.
1190 if (time_after(jiffies, timeout)) {
1191 pr_err("%s: Card stuck in programming state!"\
1192 " %s %s\n", mmc_hostname(card->host),
1193 req->rq_disk->disk_name, __func__);
1195 return MMC_BLK_CMD_ERR;
1198 * Some cards mishandle the status bits,
1199 * so make sure to check both the busy
1200 * indication and the card state.
1202 } while (!(status & R1_READY_FOR_DATA) ||
1203 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1206 /* if general error occurs, retry the write operation. */
1208 pr_warn("%s: retrying write for general error\n",
1209 req->rq_disk->disk_name);
1210 return MMC_BLK_RETRY;
1213 if (brq->data.error) {
1214 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1215 req->rq_disk->disk_name, brq->data.error,
1216 (unsigned)blk_rq_pos(req),
1217 (unsigned)blk_rq_sectors(req),
1218 brq->cmd.resp[0], brq->stop.resp[0]);
1220 if (rq_data_dir(req) == READ) {
1222 return MMC_BLK_ECC_ERR;
1223 return MMC_BLK_DATA_ERR;
1225 return MMC_BLK_CMD_ERR;
1229 if (!brq->data.bytes_xfered)
1230 return MMC_BLK_RETRY;
1232 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1233 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1234 return MMC_BLK_PARTIAL;
1236 return MMC_BLK_SUCCESS;
1239 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1240 return MMC_BLK_PARTIAL;
1242 return MMC_BLK_SUCCESS;
1245 static int mmc_blk_packed_err_check(struct mmc_card *card,
1246 struct mmc_async_req *areq)
1248 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1250 struct request *req = mq_rq->req;
1251 struct mmc_packed *packed = mq_rq->packed;
1252 int err, check, status;
1258 check = mmc_blk_err_check(card, areq);
1259 err = get_card_status(card, &status, 0);
1261 pr_err("%s: error %d sending status command\n",
1262 req->rq_disk->disk_name, err);
1263 return MMC_BLK_ABORT;
1266 if (status & R1_EXCEPTION_EVENT) {
1267 ext_csd = kzalloc(512, GFP_KERNEL);
1269 pr_err("%s: unable to allocate buffer for ext_csd\n",
1270 req->rq_disk->disk_name);
1274 err = mmc_send_ext_csd(card, ext_csd);
1276 pr_err("%s: error %d sending ext_csd\n",
1277 req->rq_disk->disk_name, err);
1278 check = MMC_BLK_ABORT;
1282 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1283 EXT_CSD_PACKED_FAILURE) &&
1284 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1285 EXT_CSD_PACKED_GENERIC_ERROR)) {
1286 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1287 EXT_CSD_PACKED_INDEXED_ERROR) {
1288 packed->idx_failure =
1289 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1290 check = MMC_BLK_PARTIAL;
1292 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1293 "failure index: %d\n",
1294 req->rq_disk->disk_name, packed->nr_entries,
1295 packed->blocks, packed->idx_failure);
1304 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1305 struct mmc_card *card,
1307 struct mmc_queue *mq)
1309 u32 readcmd, writecmd;
1310 struct mmc_blk_request *brq = &mqrq->brq;
1311 struct request *req = mqrq->req;
1312 struct mmc_blk_data *md = mq->data;
1316 * Reliable writes are used to implement Forced Unit Access and
1317 * REQ_META accesses, and are supported only on MMCs.
1319 * XXX: this really needs a good explanation of why REQ_META
1320 * is treated special.
1322 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1323 (req->cmd_flags & REQ_META)) &&
1324 (rq_data_dir(req) == WRITE) &&
1325 (md->flags & MMC_BLK_REL_WR);
1327 memset(brq, 0, sizeof(struct mmc_blk_request));
1328 brq->mrq.cmd = &brq->cmd;
1329 brq->mrq.data = &brq->data;
1331 brq->cmd.arg = blk_rq_pos(req);
1332 if (!mmc_card_blockaddr(card))
1334 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1335 brq->data.blksz = 512;
1336 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1338 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1339 brq->data.blocks = blk_rq_sectors(req);
1342 * The block layer doesn't support all sector count
1343 * restrictions, so we need to be prepared for too big
1346 if (brq->data.blocks > card->host->max_blk_count)
1347 brq->data.blocks = card->host->max_blk_count;
1349 if (brq->data.blocks > 1) {
1351 * After a read error, we redo the request one sector
1352 * at a time in order to accurately determine which
1353 * sectors can be read successfully.
1356 brq->data.blocks = 1;
1358 /* Some controllers can't do multiblock reads due to hw bugs */
1359 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1360 rq_data_dir(req) == READ)
1361 brq->data.blocks = 1;
1364 * Some controllers have HW issues while operating
1365 * in multiple I/O mode
1367 if (card->host->ops->multi_io_quirk)
1368 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1369 (rq_data_dir(req) == READ) ?
1370 MMC_DATA_READ : MMC_DATA_WRITE,
1374 if (brq->data.blocks > 1 || do_rel_wr) {
1375 /* SPI multiblock writes terminate using a special
1376 * token, not a STOP_TRANSMISSION request.
1378 if (!mmc_host_is_spi(card->host) ||
1379 rq_data_dir(req) == READ)
1380 brq->mrq.stop = &brq->stop;
1381 readcmd = MMC_READ_MULTIPLE_BLOCK;
1382 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1384 brq->mrq.stop = NULL;
1385 readcmd = MMC_READ_SINGLE_BLOCK;
1386 writecmd = MMC_WRITE_BLOCK;
1388 if (rq_data_dir(req) == READ) {
1389 brq->cmd.opcode = readcmd;
1390 brq->data.flags |= MMC_DATA_READ;
1392 brq->cmd.opcode = writecmd;
1393 brq->data.flags |= MMC_DATA_WRITE;
1397 mmc_apply_rel_rw(brq, card, req);
1400 * Data tag is used only during writing meta data to speed
1401 * up write and any subsequent read of this meta data
1403 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1404 (req->cmd_flags & REQ_META) &&
1405 (rq_data_dir(req) == WRITE) &&
1406 ((brq->data.blocks * brq->data.blksz) >=
1407 card->ext_csd.data_tag_unit_size);
1410 * Pre-defined multi-block transfers are preferable to
1411 * open ended-ones (and necessary for reliable writes).
1412 * However, it is not sufficient to just send CMD23,
1413 * and avoid the final CMD12, as on an error condition
1414 * CMD12 (stop) needs to be sent anyway. This, coupled
1415 * with Auto-CMD23 enhancements provided by some
1416 * hosts, means that the complexity of dealing
1417 * with this is best left to the host. If CMD23 is
1418 * supported by card and host, we'll fill sbc in and let
1419 * the host deal with handling it correctly. This means
1420 * that for hosts that don't expose MMC_CAP_CMD23, no
1421 * change of behavior will be observed.
1423 * N.B: Some MMC cards experience perf degradation.
1424 * We'll avoid using CMD23-bounded multiblock writes for
1425 * these, while retaining features like reliable writes.
1427 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1428 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1430 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1431 brq->sbc.arg = brq->data.blocks |
1432 (do_rel_wr ? (1 << 31) : 0) |
1433 (do_data_tag ? (1 << 29) : 0);
1434 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1435 brq->mrq.sbc = &brq->sbc;
1438 mmc_set_data_timeout(&brq->data, card);
1440 brq->data.sg = mqrq->sg;
1441 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1444 * Adjust the sg list so it is the same size as the
1447 if (brq->data.blocks != blk_rq_sectors(req)) {
1448 int i, data_size = brq->data.blocks << 9;
1449 struct scatterlist *sg;
1451 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1452 data_size -= sg->length;
1453 if (data_size <= 0) {
1454 sg->length += data_size;
1459 brq->data.sg_len = i;
1462 mqrq->mmc_active.mrq = &brq->mrq;
1463 mqrq->mmc_active.err_check = mmc_blk_err_check;
1465 mmc_queue_bounce_pre(mqrq);
1468 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1469 struct mmc_card *card)
1471 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1472 unsigned int max_seg_sz = queue_max_segment_size(q);
1473 unsigned int len, nr_segs = 0;
1476 len = min(hdr_sz, max_seg_sz);
1484 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1486 struct request_queue *q = mq->queue;
1487 struct mmc_card *card = mq->card;
1488 struct request *cur = req, *next = NULL;
1489 struct mmc_blk_data *md = mq->data;
1490 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1491 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1492 unsigned int req_sectors = 0, phys_segments = 0;
1493 unsigned int max_blk_count, max_phys_segs;
1494 bool put_back = true;
1495 u8 max_packed_rw = 0;
1498 if (!(md->flags & MMC_BLK_PACKED_CMD))
1501 if ((rq_data_dir(cur) == WRITE) &&
1502 mmc_host_packed_wr(card->host))
1503 max_packed_rw = card->ext_csd.max_packed_writes;
1505 if (max_packed_rw == 0)
1508 if (mmc_req_rel_wr(cur) &&
1509 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1512 if (mmc_large_sector(card) &&
1513 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1516 mmc_blk_clear_packed(mqrq);
1518 max_blk_count = min(card->host->max_blk_count,
1519 card->host->max_req_size >> 9);
1520 if (unlikely(max_blk_count > 0xffff))
1521 max_blk_count = 0xffff;
1523 max_phys_segs = queue_max_segments(q);
1524 req_sectors += blk_rq_sectors(cur);
1525 phys_segments += cur->nr_phys_segments;
1527 if (rq_data_dir(cur) == WRITE) {
1528 req_sectors += mmc_large_sector(card) ? 8 : 1;
1529 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1533 if (reqs >= max_packed_rw - 1) {
1538 spin_lock_irq(q->queue_lock);
1539 next = blk_fetch_request(q);
1540 spin_unlock_irq(q->queue_lock);
1546 if (mmc_large_sector(card) &&
1547 !IS_ALIGNED(blk_rq_sectors(next), 8))
1550 if (next->cmd_flags & REQ_DISCARD ||
1551 next->cmd_flags & REQ_FLUSH)
1554 if (rq_data_dir(cur) != rq_data_dir(next))
1557 if (mmc_req_rel_wr(next) &&
1558 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1561 req_sectors += blk_rq_sectors(next);
1562 if (req_sectors > max_blk_count)
1565 phys_segments += next->nr_phys_segments;
1566 if (phys_segments > max_phys_segs)
1569 list_add_tail(&next->queuelist, &mqrq->packed->list);
1575 spin_lock_irq(q->queue_lock);
1576 blk_requeue_request(q, next);
1577 spin_unlock_irq(q->queue_lock);
1581 list_add(&req->queuelist, &mqrq->packed->list);
1582 mqrq->packed->nr_entries = ++reqs;
1583 mqrq->packed->retries = reqs;
1588 mqrq->cmd_type = MMC_PACKED_NONE;
1592 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1593 struct mmc_card *card,
1594 struct mmc_queue *mq)
1596 struct mmc_blk_request *brq = &mqrq->brq;
1597 struct request *req = mqrq->req;
1598 struct request *prq;
1599 struct mmc_blk_data *md = mq->data;
1600 struct mmc_packed *packed = mqrq->packed;
1601 bool do_rel_wr, do_data_tag;
1602 u32 *packed_cmd_hdr;
1608 mqrq->cmd_type = MMC_PACKED_WRITE;
1610 packed->idx_failure = MMC_PACKED_NR_IDX;
1612 packed_cmd_hdr = packed->cmd_hdr;
1613 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1614 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1615 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1616 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1619 * Argument for each entry of packed group
1621 list_for_each_entry(prq, &packed->list, queuelist) {
1622 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1623 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1624 (prq->cmd_flags & REQ_META) &&
1625 (rq_data_dir(prq) == WRITE) &&
1626 ((brq->data.blocks * brq->data.blksz) >=
1627 card->ext_csd.data_tag_unit_size);
1628 /* Argument of CMD23 */
1629 packed_cmd_hdr[(i * 2)] =
1630 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1631 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1632 blk_rq_sectors(prq);
1633 /* Argument of CMD18 or CMD25 */
1634 packed_cmd_hdr[((i * 2)) + 1] =
1635 mmc_card_blockaddr(card) ?
1636 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1637 packed->blocks += blk_rq_sectors(prq);
1641 memset(brq, 0, sizeof(struct mmc_blk_request));
1642 brq->mrq.cmd = &brq->cmd;
1643 brq->mrq.data = &brq->data;
1644 brq->mrq.sbc = &brq->sbc;
1645 brq->mrq.stop = &brq->stop;
1647 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1648 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1649 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1651 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1652 brq->cmd.arg = blk_rq_pos(req);
1653 if (!mmc_card_blockaddr(card))
1655 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1657 brq->data.blksz = 512;
1658 brq->data.blocks = packed->blocks + hdr_blocks;
1659 brq->data.flags |= MMC_DATA_WRITE;
1661 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1663 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1665 mmc_set_data_timeout(&brq->data, card);
1667 brq->data.sg = mqrq->sg;
1668 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1670 mqrq->mmc_active.mrq = &brq->mrq;
1671 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1673 mmc_queue_bounce_pre(mqrq);
1676 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1677 struct mmc_blk_request *brq, struct request *req,
1680 struct mmc_queue_req *mq_rq;
1681 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1684 * If this is an SD card and we're writing, we can first
1685 * mark the known good sectors as ok.
1687 * If the card is not SD, we can still ok written sectors
1688 * as reported by the controller (which might be less than
1689 * the real number of written sectors, but never more).
1691 if (mmc_card_sd(card)) {
1694 blocks = mmc_sd_num_wr_blocks(card);
1695 if (blocks != (u32)-1) {
1696 ret = blk_end_request(req, 0, blocks << 9);
1699 if (!mmc_packed_cmd(mq_rq->cmd_type))
1700 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1705 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1707 struct request *prq;
1708 struct mmc_packed *packed = mq_rq->packed;
1709 int idx = packed->idx_failure, i = 0;
1714 while (!list_empty(&packed->list)) {
1715 prq = list_entry_rq(packed->list.next);
1717 /* retry from error index */
1718 packed->nr_entries -= idx;
1722 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1723 list_del_init(&prq->queuelist);
1724 mmc_blk_clear_packed(mq_rq);
1728 list_del_init(&prq->queuelist);
1729 blk_end_request(prq, 0, blk_rq_bytes(prq));
1733 mmc_blk_clear_packed(mq_rq);
1737 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1739 struct request *prq;
1740 struct mmc_packed *packed = mq_rq->packed;
1744 while (!list_empty(&packed->list)) {
1745 prq = list_entry_rq(packed->list.next);
1746 list_del_init(&prq->queuelist);
1747 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1750 mmc_blk_clear_packed(mq_rq);
1753 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1754 struct mmc_queue_req *mq_rq)
1756 struct request *prq;
1757 struct request_queue *q = mq->queue;
1758 struct mmc_packed *packed = mq_rq->packed;
1762 while (!list_empty(&packed->list)) {
1763 prq = list_entry_rq(packed->list.prev);
1764 if (prq->queuelist.prev != &packed->list) {
1765 list_del_init(&prq->queuelist);
1766 spin_lock_irq(q->queue_lock);
1767 blk_requeue_request(mq->queue, prq);
1768 spin_unlock_irq(q->queue_lock);
1770 list_del_init(&prq->queuelist);
1774 mmc_blk_clear_packed(mq_rq);
1777 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1779 struct mmc_blk_data *md = mq->data;
1780 struct mmc_card *card = md->queue.card;
1781 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1782 int ret = 1, disable_multi = 0, retry = 0, type;
1783 enum mmc_blk_status status;
1784 struct mmc_queue_req *mq_rq;
1785 struct request *req = rqc;
1786 struct mmc_async_req *areq;
1787 const u8 packed_nr = 2;
1790 if (!rqc && !mq->mqrq_prev->req)
1794 reqs = mmc_blk_prep_packed_list(mq, rqc);
1799 * When 4KB native sector is enabled, only 8 blocks
1800 * multiple read or write is allowed
1802 if ((brq->data.blocks & 0x07) &&
1803 (card->ext_csd.data_sector_size == 4096)) {
1804 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1805 req->rq_disk->disk_name);
1806 mq_rq = mq->mqrq_cur;
1810 if (reqs >= packed_nr)
1811 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1814 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1815 areq = &mq->mqrq_cur->mmc_active;
1818 areq = mmc_start_req(card->host, areq, (int *) &status);
1820 if (status == MMC_BLK_NEW_REQUEST)
1821 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1825 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1828 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1829 mmc_queue_bounce_post(mq_rq);
1832 case MMC_BLK_SUCCESS:
1833 case MMC_BLK_PARTIAL:
1835 * A block was successfully transferred.
1837 mmc_blk_reset_success(md, type);
1839 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1840 ret = mmc_blk_end_packed_req(mq_rq);
1843 ret = blk_end_request(req, 0,
1844 brq->data.bytes_xfered);
1848 * If the blk_end_request function returns non-zero even
1849 * though all data has been transferred and no errors
1850 * were returned by the host controller, it's a bug.
1852 if (status == MMC_BLK_SUCCESS && ret) {
1853 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1854 __func__, blk_rq_bytes(req),
1855 brq->data.bytes_xfered);
1860 case MMC_BLK_CMD_ERR:
1861 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1862 if (!mmc_blk_reset(md, card->host, type))
1870 if (!mmc_blk_reset(md, card->host, type))
1873 case MMC_BLK_DATA_ERR: {
1876 err = mmc_blk_reset(md, card->host, type);
1879 if (err == -ENODEV ||
1880 mmc_packed_cmd(mq_rq->cmd_type))
1884 case MMC_BLK_ECC_ERR:
1885 if (brq->data.blocks > 1) {
1886 /* Redo read one sector at a time */
1887 pr_warning("%s: retrying using single block read\n",
1888 req->rq_disk->disk_name);
1893 * After an error, we redo I/O one sector at a
1894 * time, so we only reach here after trying to
1895 * read a single sector.
1897 ret = blk_end_request(req, -EIO,
1902 case MMC_BLK_NOMEDIUM:
1905 pr_err("%s: Unhandled return value (%d)",
1906 req->rq_disk->disk_name, status);
1911 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1912 if (!mq_rq->packed->retries)
1914 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1915 mmc_start_req(card->host,
1916 &mq_rq->mmc_active, NULL);
1920 * In case of a incomplete request
1921 * prepare it again and resend.
1923 mmc_blk_rw_rq_prep(mq_rq, card,
1925 mmc_start_req(card->host,
1926 &mq_rq->mmc_active, NULL);
1934 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1935 mmc_blk_abort_packed_req(mq_rq);
1937 if (mmc_card_removed(card))
1938 req->cmd_flags |= REQ_QUIET;
1940 ret = blk_end_request(req, -EIO,
1941 blk_rq_cur_bytes(req));
1946 if (mmc_card_removed(card)) {
1947 rqc->cmd_flags |= REQ_QUIET;
1948 blk_end_request_all(rqc, -EIO);
1951 * If current request is packed, it needs to put back.
1953 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1954 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1956 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1957 mmc_start_req(card->host,
1958 &mq->mqrq_cur->mmc_active, NULL);
1965 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1968 struct mmc_blk_data *md = mq->data;
1969 struct mmc_card *card = md->queue.card;
1970 struct mmc_host *host = card->host;
1971 unsigned long flags;
1972 unsigned int cmd_flags = req ? req->cmd_flags : 0;
1974 if (req && !mq->mqrq_prev->req)
1975 /* claim host only for the first request */
1978 ret = mmc_blk_part_switch(card, md);
1981 blk_end_request_all(req, -EIO);
1987 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
1988 if (cmd_flags & REQ_DISCARD) {
1989 /* complete ongoing async transfer before issuing discard */
1990 if (card->host->areq)
1991 mmc_blk_issue_rw_rq(mq, NULL);
1992 if (req->cmd_flags & REQ_SECURE &&
1993 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1994 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1996 ret = mmc_blk_issue_discard_rq(mq, req);
1997 } else if (cmd_flags & REQ_FLUSH) {
1998 /* complete ongoing async transfer before issuing flush */
1999 if (card->host->areq)
2000 mmc_blk_issue_rw_rq(mq, NULL);
2001 ret = mmc_blk_issue_flush(mq, req);
2003 if (!req && host->areq) {
2004 spin_lock_irqsave(&host->context_info.lock, flags);
2005 host->context_info.is_waiting_last_req = true;
2006 spin_unlock_irqrestore(&host->context_info.lock, flags);
2008 ret = mmc_blk_issue_rw_rq(mq, req);
2012 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2013 (cmd_flags & MMC_REQ_SPECIAL_MASK))
2015 * Release host when there are no more requests
2016 * and after special request(discard, flush) is done.
2017 * In case sepecial request, there is no reentry to
2018 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2024 static inline int mmc_blk_readonly(struct mmc_card *card)
2026 return mmc_card_readonly(card) ||
2027 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2030 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2031 struct device *parent,
2034 const char *subname,
2037 struct mmc_blk_data *md;
2040 devidx = find_first_zero_bit(dev_use, max_devices);
2041 if (devidx >= max_devices)
2042 return ERR_PTR(-ENOSPC);
2043 __set_bit(devidx, dev_use);
2045 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2052 * !subname implies we are creating main mmc_blk_data that will be
2053 * associated with mmc_card with mmc_set_drvdata. Due to device
2054 * partitions, devidx will not coincide with a per-physical card
2055 * index anymore so we keep track of a name index.
2058 md->name_idx = find_first_zero_bit(name_use, max_devices);
2059 __set_bit(md->name_idx, name_use);
2061 md->name_idx = ((struct mmc_blk_data *)
2062 dev_to_disk(parent)->private_data)->name_idx;
2064 md->area_type = area_type;
2067 * Set the read-only status based on the supported commands
2068 * and the write protect switch.
2070 md->read_only = mmc_blk_readonly(card);
2072 md->disk = alloc_disk(perdev_minors);
2073 if (md->disk == NULL) {
2078 spin_lock_init(&md->lock);
2079 INIT_LIST_HEAD(&md->part);
2082 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2086 md->queue.issue_fn = mmc_blk_issue_rq;
2087 md->queue.data = md;
2089 md->disk->major = MMC_BLOCK_MAJOR;
2090 md->disk->first_minor = devidx * perdev_minors;
2091 md->disk->fops = &mmc_bdops;
2092 md->disk->private_data = md;
2093 md->disk->queue = md->queue.queue;
2094 md->disk->driverfs_dev = parent;
2095 set_disk_ro(md->disk, md->read_only || default_ro);
2096 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2097 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2100 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2102 * - be set for removable media with permanent block devices
2103 * - be unset for removable block devices with permanent media
2105 * Since MMC block devices clearly fall under the second
2106 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2107 * should use the block device creation/destruction hotplug
2108 * messages to tell when the card is present.
2111 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2112 "mmcblk%d%s", md->name_idx, subname ? subname : "");
2114 if (mmc_card_mmc(card))
2115 blk_queue_logical_block_size(md->queue.queue,
2116 card->ext_csd.data_sector_size);
2118 blk_queue_logical_block_size(md->queue.queue, 512);
2120 set_capacity(md->disk, size);
2122 if (mmc_host_cmd23(card->host)) {
2123 if (mmc_card_mmc(card) ||
2124 (mmc_card_sd(card) &&
2125 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2126 md->flags |= MMC_BLK_CMD23;
2129 if (mmc_card_mmc(card) &&
2130 md->flags & MMC_BLK_CMD23 &&
2131 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2132 card->ext_csd.rel_sectors)) {
2133 md->flags |= MMC_BLK_REL_WR;
2134 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2137 if (mmc_card_mmc(card) &&
2138 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2139 (md->flags & MMC_BLK_CMD23) &&
2140 card->ext_csd.packed_event_en) {
2141 if (!mmc_packed_init(&md->queue, card))
2142 md->flags |= MMC_BLK_PACKED_CMD;
2152 return ERR_PTR(ret);
2155 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2158 struct mmc_blk_data *md;
2160 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2162 * The EXT_CSD sector count is in number or 512 byte
2165 size = card->ext_csd.sectors;
2168 * The CSD capacity field is in units of read_blkbits.
2169 * set_capacity takes units of 512 bytes.
2171 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2174 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2175 MMC_BLK_DATA_AREA_MAIN);
2179 static int mmc_blk_alloc_part(struct mmc_card *card,
2180 struct mmc_blk_data *md,
2181 unsigned int part_type,
2184 const char *subname,
2188 struct mmc_blk_data *part_md;
2190 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2191 subname, area_type);
2192 if (IS_ERR(part_md))
2193 return PTR_ERR(part_md);
2194 part_md->part_type = part_type;
2195 list_add(&part_md->part, &md->part);
2197 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2198 cap_str, sizeof(cap_str));
2199 pr_info("%s: %s %s partition %u %s\n",
2200 part_md->disk->disk_name, mmc_card_id(card),
2201 mmc_card_name(card), part_md->part_type, cap_str);
2205 /* MMC Physical partitions consist of two boot partitions and
2206 * up to four general purpose partitions.
2207 * For each partition enabled in EXT_CSD a block device will be allocatedi
2208 * to provide access to the partition.
2211 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2215 if (!mmc_card_mmc(card))
2218 for (idx = 0; idx < card->nr_parts; idx++) {
2219 if (card->part[idx].size) {
2220 ret = mmc_blk_alloc_part(card, md,
2221 card->part[idx].part_cfg,
2222 card->part[idx].size >> 9,
2223 card->part[idx].force_ro,
2224 card->part[idx].name,
2225 card->part[idx].area_type);
2234 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2236 struct mmc_card *card;
2240 * Flush remaining requests and free queues. It
2241 * is freeing the queue that stops new requests
2242 * from being accepted.
2244 card = md->queue.card;
2245 mmc_cleanup_queue(&md->queue);
2246 if (md->flags & MMC_BLK_PACKED_CMD)
2247 mmc_packed_clean(&md->queue);
2248 if (md->disk->flags & GENHD_FL_UP) {
2249 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2250 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2251 card->ext_csd.boot_ro_lockable)
2252 device_remove_file(disk_to_dev(md->disk),
2253 &md->power_ro_lock);
2255 del_gendisk(md->disk);
2261 static void mmc_blk_remove_parts(struct mmc_card *card,
2262 struct mmc_blk_data *md)
2264 struct list_head *pos, *q;
2265 struct mmc_blk_data *part_md;
2267 __clear_bit(md->name_idx, name_use);
2268 list_for_each_safe(pos, q, &md->part) {
2269 part_md = list_entry(pos, struct mmc_blk_data, part);
2271 mmc_blk_remove_req(part_md);
2275 static int mmc_add_disk(struct mmc_blk_data *md)
2278 struct mmc_card *card = md->queue.card;
2281 md->force_ro.show = force_ro_show;
2282 md->force_ro.store = force_ro_store;
2283 sysfs_attr_init(&md->force_ro.attr);
2284 md->force_ro.attr.name = "force_ro";
2285 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2286 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2290 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2291 card->ext_csd.boot_ro_lockable) {
2294 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2297 mode = S_IRUGO | S_IWUSR;
2299 md->power_ro_lock.show = power_ro_lock_show;
2300 md->power_ro_lock.store = power_ro_lock_store;
2301 sysfs_attr_init(&md->power_ro_lock.attr);
2302 md->power_ro_lock.attr.mode = mode;
2303 md->power_ro_lock.attr.name =
2304 "ro_lock_until_next_power_on";
2305 ret = device_create_file(disk_to_dev(md->disk),
2306 &md->power_ro_lock);
2308 goto power_ro_lock_fail;
2313 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2315 del_gendisk(md->disk);
2320 #define CID_MANFID_SANDISK 0x2
2321 #define CID_MANFID_TOSHIBA 0x11
2322 #define CID_MANFID_MICRON 0x13
2323 #define CID_MANFID_SAMSUNG 0x15
2325 static const struct mmc_fixup blk_fixups[] =
2327 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2328 MMC_QUIRK_INAND_CMD38),
2329 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2330 MMC_QUIRK_INAND_CMD38),
2331 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2332 MMC_QUIRK_INAND_CMD38),
2333 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2334 MMC_QUIRK_INAND_CMD38),
2335 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2336 MMC_QUIRK_INAND_CMD38),
2339 * Some MMC cards experience performance degradation with CMD23
2340 * instead of CMD12-bounded multiblock transfers. For now we'll
2341 * black list what's bad...
2342 * - Certain Toshiba cards.
2344 * N.B. This doesn't affect SD cards.
2346 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2347 MMC_QUIRK_BLK_NO_CMD23),
2348 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2349 MMC_QUIRK_BLK_NO_CMD23),
2350 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2351 MMC_QUIRK_BLK_NO_CMD23),
2354 * Some Micron MMC cards needs longer data read timeout than
2357 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2358 MMC_QUIRK_LONG_READ_TIME),
2361 * On these Samsung MoviNAND parts, performing secure erase or
2362 * secure trim can result in unrecoverable corruption due to a
2365 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2366 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2367 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2368 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2369 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2370 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2371 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2372 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2373 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2374 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2375 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2376 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2377 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2378 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2379 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2380 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2385 static int mmc_blk_probe(struct mmc_card *card)
2387 struct mmc_blk_data *md, *part_md;
2391 * Check that the card supports the command class(es) we need.
2393 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2396 md = mmc_blk_alloc(card);
2400 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
2401 cap_str, sizeof(cap_str));
2402 pr_info("%s: %s %s %s %s\n",
2403 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2404 cap_str, md->read_only ? "(ro)" : "");
2406 if (mmc_blk_alloc_parts(card, md))
2409 mmc_set_drvdata(card, md);
2410 mmc_fixup_device(card, blk_fixups);
2412 if (mmc_add_disk(md))
2415 list_for_each_entry(part_md, &md->part, part) {
2416 if (mmc_add_disk(part_md))
2420 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2421 pm_runtime_use_autosuspend(&card->dev);
2424 * Don't enable runtime PM for SD-combo cards here. Leave that
2425 * decision to be taken during the SDIO init sequence instead.
2427 if (card->type != MMC_TYPE_SD_COMBO) {
2428 pm_runtime_set_active(&card->dev);
2429 pm_runtime_enable(&card->dev);
2435 mmc_blk_remove_parts(card, md);
2436 mmc_blk_remove_req(md);
2440 static void mmc_blk_remove(struct mmc_card *card)
2442 struct mmc_blk_data *md = mmc_get_drvdata(card);
2444 mmc_blk_remove_parts(card, md);
2445 pm_runtime_get_sync(&card->dev);
2446 mmc_claim_host(card->host);
2447 mmc_blk_part_switch(card, md);
2448 mmc_release_host(card->host);
2449 if (card->type != MMC_TYPE_SD_COMBO)
2450 pm_runtime_disable(&card->dev);
2451 pm_runtime_put_noidle(&card->dev);
2452 mmc_blk_remove_req(md);
2453 mmc_set_drvdata(card, NULL);
2456 static int _mmc_blk_suspend(struct mmc_card *card)
2458 struct mmc_blk_data *part_md;
2459 struct mmc_blk_data *md = mmc_get_drvdata(card);
2462 mmc_queue_suspend(&md->queue);
2463 list_for_each_entry(part_md, &md->part, part) {
2464 mmc_queue_suspend(&part_md->queue);
2470 static void mmc_blk_shutdown(struct mmc_card *card)
2472 _mmc_blk_suspend(card);
2476 static int mmc_blk_suspend(struct mmc_card *card)
2478 return _mmc_blk_suspend(card);
2481 static int mmc_blk_resume(struct mmc_card *card)
2483 struct mmc_blk_data *part_md;
2484 struct mmc_blk_data *md = mmc_get_drvdata(card);
2488 * Resume involves the card going into idle state,
2489 * so current partition is always the main one.
2491 md->part_curr = md->part_type;
2492 mmc_queue_resume(&md->queue);
2493 list_for_each_entry(part_md, &md->part, part) {
2494 mmc_queue_resume(&part_md->queue);
2500 #define mmc_blk_suspend NULL
2501 #define mmc_blk_resume NULL
2504 static struct mmc_driver mmc_driver = {
2508 .probe = mmc_blk_probe,
2509 .remove = mmc_blk_remove,
2510 .suspend = mmc_blk_suspend,
2511 .resume = mmc_blk_resume,
2512 .shutdown = mmc_blk_shutdown,
2515 static int __init mmc_blk_init(void)
2519 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2520 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2522 max_devices = 256 / perdev_minors;
2524 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2528 res = mmc_register_driver(&mmc_driver);
2534 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2539 static void __exit mmc_blk_exit(void)
2541 mmc_unregister_driver(&mmc_driver);
2542 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2545 module_init(mmc_blk_init);
2546 module_exit(mmc_blk_exit);
2548 MODULE_LICENSE("GPL");
2549 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");