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>
38 #include <linux/mmc/ioctl.h>
39 #include <linux/mmc/card.h>
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/mmc.h>
42 #include <linux/mmc/sd.h>
44 #include <asm/system.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
62 static DEFINE_MUTEX(block_mutex);
65 * The defaults come from config options but can be overriden by module
68 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
71 * We've only got one major, so number of mmcblk devices is
72 * limited to 256 / number of minors per device.
74 static int max_devices;
76 /* 256 minors, so at most 256 separate devices */
77 static DECLARE_BITMAP(dev_use, 256);
78 static DECLARE_BITMAP(name_use, 256);
81 * There is one mmc_blk_data per slot.
86 struct mmc_queue queue;
87 struct list_head part;
90 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
91 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
94 unsigned int read_only;
95 unsigned int part_type;
96 unsigned int name_idx;
97 unsigned int reset_done;
98 #define MMC_BLK_READ BIT(0)
99 #define MMC_BLK_WRITE BIT(1)
100 #define MMC_BLK_DISCARD BIT(2)
101 #define MMC_BLK_SECDISCARD BIT(3)
104 * Only set in main mmc_blk_data associated
105 * with mmc_card with mmc_set_drvdata, and keeps
106 * track of the current selected device partition.
108 unsigned int part_curr;
109 struct device_attribute force_ro;
112 static DEFINE_MUTEX(open_lock);
114 enum mmc_blk_status {
125 module_param(perdev_minors, int, 0444);
126 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
128 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
130 struct mmc_blk_data *md;
132 mutex_lock(&open_lock);
133 md = disk->private_data;
134 if (md && md->usage == 0)
138 mutex_unlock(&open_lock);
143 static inline int mmc_get_devidx(struct gendisk *disk)
145 int devmaj = MAJOR(disk_devt(disk));
146 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
149 devidx = disk->first_minor / perdev_minors;
153 static void mmc_blk_put(struct mmc_blk_data *md)
155 mutex_lock(&open_lock);
157 if (md->usage == 0) {
158 int devidx = mmc_get_devidx(md->disk);
159 blk_cleanup_queue(md->queue.queue);
161 __clear_bit(devidx, dev_use);
166 mutex_unlock(&open_lock);
169 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
173 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
175 ret = snprintf(buf, PAGE_SIZE, "%d",
176 get_disk_ro(dev_to_disk(dev)) ^
182 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
183 const char *buf, size_t count)
187 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
188 unsigned long set = simple_strtoul(buf, &end, 0);
194 set_disk_ro(dev_to_disk(dev), set || md->read_only);
201 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
203 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
206 mutex_lock(&block_mutex);
209 check_disk_change(bdev);
212 if ((mode & FMODE_WRITE) && md->read_only) {
217 mutex_unlock(&block_mutex);
222 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
224 struct mmc_blk_data *md = disk->private_data;
226 mutex_lock(&block_mutex);
228 mutex_unlock(&block_mutex);
233 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
235 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
241 struct mmc_blk_ioc_data {
242 struct mmc_ioc_cmd ic;
247 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
248 struct mmc_ioc_cmd __user *user)
250 struct mmc_blk_ioc_data *idata;
253 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
259 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
264 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
265 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
270 if (!idata->buf_bytes)
273 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
279 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
280 idata->ic.data_ptr, idata->buf_bytes)) {
295 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
296 struct mmc_ioc_cmd __user *ic_ptr)
298 struct mmc_blk_ioc_data *idata;
299 struct mmc_blk_data *md;
300 struct mmc_card *card;
301 struct mmc_command cmd = {0};
302 struct mmc_data data = {0};
303 struct mmc_request mrq = {NULL};
304 struct scatterlist sg;
308 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
309 * whole block device, not on a partition. This prevents overspray
310 * between sibling partitions.
312 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
315 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
317 return PTR_ERR(idata);
319 md = mmc_blk_get(bdev->bd_disk);
325 card = md->queue.card;
331 cmd.opcode = idata->ic.opcode;
332 cmd.arg = idata->ic.arg;
333 cmd.flags = idata->ic.flags;
335 if (idata->buf_bytes) {
338 data.blksz = idata->ic.blksz;
339 data.blocks = idata->ic.blocks;
341 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
343 if (idata->ic.write_flag)
344 data.flags = MMC_DATA_WRITE;
346 data.flags = MMC_DATA_READ;
348 /* data.flags must already be set before doing this. */
349 mmc_set_data_timeout(&data, card);
351 /* Allow overriding the timeout_ns for empirical tuning. */
352 if (idata->ic.data_timeout_ns)
353 data.timeout_ns = idata->ic.data_timeout_ns;
355 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
357 * Pretend this is a data transfer and rely on the
358 * host driver to compute timeout. When all host
359 * drivers support cmd.cmd_timeout for R1B, this
363 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
365 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
373 mmc_claim_host(card->host);
375 if (idata->ic.is_acmd) {
376 err = mmc_app_cmd(card->host, card);
381 mmc_wait_for_req(card->host, &mrq);
384 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
385 __func__, cmd.error);
390 dev_err(mmc_dev(card->host), "%s: data error %d\n",
391 __func__, data.error);
397 * According to the SD specs, some commands require a delay after
398 * issuing the command.
400 if (idata->ic.postsleep_min_us)
401 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
403 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
408 if (!idata->ic.write_flag) {
409 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
410 idata->buf, idata->buf_bytes)) {
417 mmc_release_host(card->host);
427 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
428 unsigned int cmd, unsigned long arg)
431 if (cmd == MMC_IOC_CMD)
432 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
437 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
438 unsigned int cmd, unsigned long arg)
440 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
444 static const struct block_device_operations mmc_bdops = {
445 .open = mmc_blk_open,
446 .release = mmc_blk_release,
447 .getgeo = mmc_blk_getgeo,
448 .owner = THIS_MODULE,
449 .ioctl = mmc_blk_ioctl,
451 .compat_ioctl = mmc_blk_compat_ioctl,
455 static inline int mmc_blk_part_switch(struct mmc_card *card,
456 struct mmc_blk_data *md)
459 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
461 if (main_md->part_curr == md->part_type)
464 if (mmc_card_mmc(card)) {
465 u8 part_config = card->ext_csd.part_config;
467 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
468 part_config |= md->part_type;
470 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
471 EXT_CSD_PART_CONFIG, part_config,
472 card->ext_csd.part_time);
476 card->ext_csd.part_config = part_config;
479 main_md->part_curr = md->part_type;
483 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
489 struct mmc_request mrq = {NULL};
490 struct mmc_command cmd = {0};
491 struct mmc_data data = {0};
493 struct scatterlist sg;
495 cmd.opcode = MMC_APP_CMD;
496 cmd.arg = card->rca << 16;
497 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
499 err = mmc_wait_for_cmd(card->host, &cmd, 0);
502 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
505 memset(&cmd, 0, sizeof(struct mmc_command));
507 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
509 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
513 data.flags = MMC_DATA_READ;
516 mmc_set_data_timeout(&data, card);
521 blocks = kmalloc(4, GFP_KERNEL);
525 sg_init_one(&sg, blocks, 4);
527 mmc_wait_for_req(card->host, &mrq);
529 result = ntohl(*blocks);
532 if (cmd.error || data.error)
538 static int send_stop(struct mmc_card *card, u32 *status)
540 struct mmc_command cmd = {0};
543 cmd.opcode = MMC_STOP_TRANSMISSION;
544 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
545 err = mmc_wait_for_cmd(card->host, &cmd, 5);
547 *status = cmd.resp[0];
551 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
553 struct mmc_command cmd = {0};
556 cmd.opcode = MMC_SEND_STATUS;
557 if (!mmc_host_is_spi(card->host))
558 cmd.arg = card->rca << 16;
559 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
560 err = mmc_wait_for_cmd(card->host, &cmd, retries);
562 *status = cmd.resp[0];
566 #define ERR_NOMEDIUM 3
569 #define ERR_CONTINUE 0
571 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
572 bool status_valid, u32 status)
576 /* response crc error, retry the r/w cmd */
577 pr_err("%s: %s sending %s command, card status %#x\n",
578 req->rq_disk->disk_name, "response CRC error",
583 pr_err("%s: %s sending %s command, card status %#x\n",
584 req->rq_disk->disk_name, "timed out", name, status);
586 /* If the status cmd initially failed, retry the r/w cmd */
591 * If it was a r/w cmd crc error, or illegal command
592 * (eg, issued in wrong state) then retry - we should
593 * have corrected the state problem above.
595 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
598 /* Otherwise abort the command */
602 /* We don't understand the error code the driver gave us */
603 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
604 req->rq_disk->disk_name, error, status);
610 * Initial r/w and stop cmd error recovery.
611 * We don't know whether the card received the r/w cmd or not, so try to
612 * restore things back to a sane state. Essentially, we do this as follows:
613 * - Obtain card status. If the first attempt to obtain card status fails,
614 * the status word will reflect the failed status cmd, not the failed
615 * r/w cmd. If we fail to obtain card status, it suggests we can no
616 * longer communicate with the card.
617 * - Check the card state. If the card received the cmd but there was a
618 * transient problem with the response, it might still be in a data transfer
619 * mode. Try to send it a stop command. If this fails, we can't recover.
620 * - If the r/w cmd failed due to a response CRC error, it was probably
621 * transient, so retry the cmd.
622 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
623 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
624 * illegal cmd, retry.
625 * Otherwise we don't understand what happened, so abort.
627 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
628 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
630 bool prev_cmd_status_valid = true;
631 u32 status, stop_status = 0;
634 if (mmc_card_removed(card))
638 * Try to get card status which indicates both the card state
639 * and why there was no response. If the first attempt fails,
640 * we can't be sure the returned status is for the r/w command.
642 for (retry = 2; retry >= 0; retry--) {
643 err = get_card_status(card, &status, 0);
647 prev_cmd_status_valid = false;
648 pr_err("%s: error %d sending status command, %sing\n",
649 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
652 /* We couldn't get a response from the card. Give up. */
654 /* Check if the card is removed */
655 if (mmc_detect_card_removed(card->host))
660 /* Flag ECC errors */
661 if ((status & R1_CARD_ECC_FAILED) ||
662 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
663 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
666 /* Flag General errors */
667 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
668 if ((status & R1_ERROR) ||
669 (brq->stop.resp[0] & R1_ERROR)) {
670 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
671 req->rq_disk->disk_name, __func__,
672 brq->stop.resp[0], status);
677 * Check the current card state. If it is in some data transfer
678 * mode, tell it to stop (and hopefully transition back to TRAN.)
680 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
681 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
682 err = send_stop(card, &stop_status);
684 pr_err("%s: error %d sending stop command\n",
685 req->rq_disk->disk_name, err);
688 * If the stop cmd also timed out, the card is probably
689 * not present, so abort. Other errors are bad news too.
693 if (stop_status & R1_CARD_ECC_FAILED)
695 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
696 if (stop_status & R1_ERROR) {
697 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
698 req->rq_disk->disk_name, __func__,
704 /* Check for set block count errors */
706 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
707 prev_cmd_status_valid, status);
709 /* Check for r/w command errors */
711 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
712 prev_cmd_status_valid, status);
715 if (!brq->stop.error)
718 /* Now for stop errors. These aren't fatal to the transfer. */
719 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
720 req->rq_disk->disk_name, brq->stop.error,
721 brq->cmd.resp[0], status);
724 * Subsitute in our own stop status as this will give the error
725 * state which happened during the execution of the r/w command.
728 brq->stop.resp[0] = stop_status;
734 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
739 if (md->reset_done & type)
742 md->reset_done |= type;
743 err = mmc_hw_reset(host);
744 /* Ensure we switch back to the correct partition */
745 if (err != -EOPNOTSUPP) {
746 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
749 main_md->part_curr = main_md->part_type;
750 part_err = mmc_blk_part_switch(host->card, md);
753 * We have failed to get back into the correct
754 * partition, so we need to abort the whole request.
762 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
764 md->reset_done &= ~type;
767 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
769 struct mmc_blk_data *md = mq->data;
770 struct mmc_card *card = md->queue.card;
771 unsigned int from, nr, arg;
772 int err = 0, type = MMC_BLK_DISCARD;
774 if (!mmc_can_erase(card)) {
779 from = blk_rq_pos(req);
780 nr = blk_rq_sectors(req);
782 if (mmc_can_discard(card))
783 arg = MMC_DISCARD_ARG;
784 else if (mmc_can_trim(card))
789 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
790 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
791 INAND_CMD38_ARG_EXT_CSD,
792 arg == MMC_TRIM_ARG ?
793 INAND_CMD38_ARG_TRIM :
794 INAND_CMD38_ARG_ERASE,
799 err = mmc_erase(card, from, nr, arg);
801 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
804 mmc_blk_reset_success(md, type);
805 blk_end_request(req, err, blk_rq_bytes(req));
810 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
813 struct mmc_blk_data *md = mq->data;
814 struct mmc_card *card = md->queue.card;
815 unsigned int from, nr, arg, trim_arg, erase_arg;
816 int err = 0, type = MMC_BLK_SECDISCARD;
818 if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
823 from = blk_rq_pos(req);
824 nr = blk_rq_sectors(req);
826 /* The sanitize operation is supported at v4.5 only */
827 if (mmc_can_sanitize(card)) {
828 erase_arg = MMC_ERASE_ARG;
829 trim_arg = MMC_TRIM_ARG;
831 erase_arg = MMC_SECURE_ERASE_ARG;
832 trim_arg = MMC_SECURE_TRIM1_ARG;
835 if (mmc_erase_group_aligned(card, from, nr))
837 else if (mmc_can_trim(card))
844 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
845 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
846 INAND_CMD38_ARG_EXT_CSD,
847 arg == MMC_SECURE_TRIM1_ARG ?
848 INAND_CMD38_ARG_SECTRIM1 :
849 INAND_CMD38_ARG_SECERASE,
855 err = mmc_erase(card, from, nr, arg);
861 if (arg == MMC_SECURE_TRIM1_ARG) {
862 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
863 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
864 INAND_CMD38_ARG_EXT_CSD,
865 INAND_CMD38_ARG_SECTRIM2,
871 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
878 if (mmc_can_sanitize(card))
879 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
880 EXT_CSD_SANITIZE_START, 1, 0);
882 if (err && !mmc_blk_reset(md, card->host, type))
885 mmc_blk_reset_success(md, type);
887 blk_end_request(req, err, blk_rq_bytes(req));
892 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
894 struct mmc_blk_data *md = mq->data;
895 struct mmc_card *card = md->queue.card;
898 ret = mmc_flush_cache(card);
902 blk_end_request_all(req, ret);
908 * Reformat current write as a reliable write, supporting
909 * both legacy and the enhanced reliable write MMC cards.
910 * In each transfer we'll handle only as much as a single
911 * reliable write can handle, thus finish the request in
912 * partial completions.
914 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
915 struct mmc_card *card,
918 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
919 /* Legacy mode imposes restrictions on transfers. */
920 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
921 brq->data.blocks = 1;
923 if (brq->data.blocks > card->ext_csd.rel_sectors)
924 brq->data.blocks = card->ext_csd.rel_sectors;
925 else if (brq->data.blocks < card->ext_csd.rel_sectors)
926 brq->data.blocks = 1;
931 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
932 R1_ADDRESS_ERROR | /* Misaligned address */ \
933 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
934 R1_WP_VIOLATION | /* Tried to write to protected block */ \
935 R1_CC_ERROR | /* Card controller error */ \
936 R1_ERROR) /* General/unknown error */
938 static int mmc_blk_err_check(struct mmc_card *card,
939 struct mmc_async_req *areq)
941 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
943 struct mmc_blk_request *brq = &mq_mrq->brq;
944 struct request *req = mq_mrq->req;
945 int ecc_err = 0, gen_err = 0;
948 * sbc.error indicates a problem with the set block count
949 * command. No data will have been transferred.
951 * cmd.error indicates a problem with the r/w command. No
952 * data will have been transferred.
954 * stop.error indicates a problem with the stop command. Data
955 * may have been transferred, or may still be transferring.
957 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
959 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
961 return MMC_BLK_RETRY;
963 return MMC_BLK_ABORT;
965 return MMC_BLK_NOMEDIUM;
972 * Check for errors relating to the execution of the
973 * initial command - such as address errors. No data
974 * has been transferred.
976 if (brq->cmd.resp[0] & CMD_ERRORS) {
977 pr_err("%s: r/w command failed, status = %#x\n",
978 req->rq_disk->disk_name, brq->cmd.resp[0]);
979 return MMC_BLK_ABORT;
983 * Everything else is either success, or a data error of some
984 * kind. If it was a write, we may have transitioned to
985 * program mode, which we have to wait for it to complete.
987 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
990 /* Check stop command response */
991 if (brq->stop.resp[0] & R1_ERROR) {
992 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
993 req->rq_disk->disk_name, __func__,
999 int err = get_card_status(card, &status, 5);
1001 pr_err("%s: error %d requesting status\n",
1002 req->rq_disk->disk_name, err);
1003 return MMC_BLK_CMD_ERR;
1006 if (status & R1_ERROR) {
1007 pr_err("%s: %s: general error sending status command, card status %#x\n",
1008 req->rq_disk->disk_name, __func__,
1014 * Some cards mishandle the status bits,
1015 * so make sure to check both the busy
1016 * indication and the card state.
1018 } while (!(status & R1_READY_FOR_DATA) ||
1019 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1022 /* if general error occurs, retry the write operation. */
1024 pr_warning("%s: retrying write for general error\n",
1025 req->rq_disk->disk_name);
1026 return MMC_BLK_RETRY;
1029 if (brq->data.error) {
1030 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1031 req->rq_disk->disk_name, brq->data.error,
1032 (unsigned)blk_rq_pos(req),
1033 (unsigned)blk_rq_sectors(req),
1034 brq->cmd.resp[0], brq->stop.resp[0]);
1036 if (rq_data_dir(req) == READ) {
1038 return MMC_BLK_ECC_ERR;
1039 return MMC_BLK_DATA_ERR;
1041 return MMC_BLK_CMD_ERR;
1045 if (!brq->data.bytes_xfered)
1046 return MMC_BLK_RETRY;
1048 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1049 return MMC_BLK_PARTIAL;
1051 return MMC_BLK_SUCCESS;
1054 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1055 struct mmc_card *card,
1057 struct mmc_queue *mq)
1059 u32 readcmd, writecmd;
1060 struct mmc_blk_request *brq = &mqrq->brq;
1061 struct request *req = mqrq->req;
1062 struct mmc_blk_data *md = mq->data;
1065 * Reliable writes are used to implement Forced Unit Access and
1066 * REQ_META accesses, and are supported only on MMCs.
1068 * XXX: this really needs a good explanation of why REQ_META
1069 * is treated special.
1071 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1072 (req->cmd_flags & REQ_META)) &&
1073 (rq_data_dir(req) == WRITE) &&
1074 (md->flags & MMC_BLK_REL_WR);
1076 memset(brq, 0, sizeof(struct mmc_blk_request));
1077 brq->mrq.cmd = &brq->cmd;
1078 brq->mrq.data = &brq->data;
1080 brq->cmd.arg = blk_rq_pos(req);
1081 if (!mmc_card_blockaddr(card))
1083 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1084 brq->data.blksz = 512;
1085 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1087 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1088 brq->data.blocks = blk_rq_sectors(req);
1091 * The block layer doesn't support all sector count
1092 * restrictions, so we need to be prepared for too big
1095 if (brq->data.blocks > card->host->max_blk_count)
1096 brq->data.blocks = card->host->max_blk_count;
1098 if (brq->data.blocks > 1) {
1100 * After a read error, we redo the request one sector
1101 * at a time in order to accurately determine which
1102 * sectors can be read successfully.
1105 brq->data.blocks = 1;
1107 /* Some controllers can't do multiblock reads due to hw bugs */
1108 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1109 rq_data_dir(req) == READ)
1110 brq->data.blocks = 1;
1113 if (brq->data.blocks > 1 || do_rel_wr) {
1114 /* SPI multiblock writes terminate using a special
1115 * token, not a STOP_TRANSMISSION request.
1117 if (!mmc_host_is_spi(card->host) ||
1118 rq_data_dir(req) == READ)
1119 brq->mrq.stop = &brq->stop;
1120 readcmd = MMC_READ_MULTIPLE_BLOCK;
1121 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1123 brq->mrq.stop = NULL;
1124 readcmd = MMC_READ_SINGLE_BLOCK;
1125 writecmd = MMC_WRITE_BLOCK;
1127 if (rq_data_dir(req) == READ) {
1128 brq->cmd.opcode = readcmd;
1129 brq->data.flags |= MMC_DATA_READ;
1131 brq->cmd.opcode = writecmd;
1132 brq->data.flags |= MMC_DATA_WRITE;
1136 mmc_apply_rel_rw(brq, card, req);
1139 * Pre-defined multi-block transfers are preferable to
1140 * open ended-ones (and necessary for reliable writes).
1141 * However, it is not sufficient to just send CMD23,
1142 * and avoid the final CMD12, as on an error condition
1143 * CMD12 (stop) needs to be sent anyway. This, coupled
1144 * with Auto-CMD23 enhancements provided by some
1145 * hosts, means that the complexity of dealing
1146 * with this is best left to the host. If CMD23 is
1147 * supported by card and host, we'll fill sbc in and let
1148 * the host deal with handling it correctly. This means
1149 * that for hosts that don't expose MMC_CAP_CMD23, no
1150 * change of behavior will be observed.
1152 * N.B: Some MMC cards experience perf degradation.
1153 * We'll avoid using CMD23-bounded multiblock writes for
1154 * these, while retaining features like reliable writes.
1157 if ((md->flags & MMC_BLK_CMD23) &&
1158 mmc_op_multi(brq->cmd.opcode) &&
1159 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
1160 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1161 brq->sbc.arg = brq->data.blocks |
1162 (do_rel_wr ? (1 << 31) : 0);
1163 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1164 brq->mrq.sbc = &brq->sbc;
1167 mmc_set_data_timeout(&brq->data, card);
1169 brq->data.sg = mqrq->sg;
1170 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1173 * Adjust the sg list so it is the same size as the
1176 if (brq->data.blocks != blk_rq_sectors(req)) {
1177 int i, data_size = brq->data.blocks << 9;
1178 struct scatterlist *sg;
1180 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1181 data_size -= sg->length;
1182 if (data_size <= 0) {
1183 sg->length += data_size;
1188 brq->data.sg_len = i;
1191 mqrq->mmc_active.mrq = &brq->mrq;
1192 mqrq->mmc_active.err_check = mmc_blk_err_check;
1194 mmc_queue_bounce_pre(mqrq);
1197 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1198 struct mmc_blk_request *brq, struct request *req,
1202 * If this is an SD card and we're writing, we can first
1203 * mark the known good sectors as ok.
1205 * If the card is not SD, we can still ok written sectors
1206 * as reported by the controller (which might be less than
1207 * the real number of written sectors, but never more).
1209 if (mmc_card_sd(card)) {
1212 blocks = mmc_sd_num_wr_blocks(card);
1213 if (blocks != (u32)-1) {
1214 ret = blk_end_request(req, 0, blocks << 9);
1217 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1222 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1224 struct mmc_blk_data *md = mq->data;
1225 struct mmc_card *card = md->queue.card;
1226 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1227 int ret = 1, disable_multi = 0, retry = 0, type;
1228 enum mmc_blk_status status;
1229 struct mmc_queue_req *mq_rq;
1230 struct request *req;
1231 struct mmc_async_req *areq;
1233 if (!rqc && !mq->mqrq_prev->req)
1238 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1239 areq = &mq->mqrq_cur->mmc_active;
1242 areq = mmc_start_req(card->host, areq, (int *) &status);
1246 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1249 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1250 mmc_queue_bounce_post(mq_rq);
1253 case MMC_BLK_SUCCESS:
1254 case MMC_BLK_PARTIAL:
1256 * A block was successfully transferred.
1258 mmc_blk_reset_success(md, type);
1259 ret = blk_end_request(req, 0,
1260 brq->data.bytes_xfered);
1262 * If the blk_end_request function returns non-zero even
1263 * though all data has been transferred and no errors
1264 * were returned by the host controller, it's a bug.
1266 if (status == MMC_BLK_SUCCESS && ret) {
1267 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1268 __func__, blk_rq_bytes(req),
1269 brq->data.bytes_xfered);
1274 case MMC_BLK_CMD_ERR:
1275 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1276 if (!mmc_blk_reset(md, card->host, type))
1284 if (!mmc_blk_reset(md, card->host, type))
1287 case MMC_BLK_DATA_ERR: {
1290 err = mmc_blk_reset(md, card->host, type);
1297 case MMC_BLK_ECC_ERR:
1298 if (brq->data.blocks > 1) {
1299 /* Redo read one sector at a time */
1300 pr_warning("%s: retrying using single block read\n",
1301 req->rq_disk->disk_name);
1306 * After an error, we redo I/O one sector at a
1307 * time, so we only reach here after trying to
1308 * read a single sector.
1310 ret = blk_end_request(req, -EIO,
1315 case MMC_BLK_NOMEDIUM:
1321 * In case of a incomplete request
1322 * prepare it again and resend.
1324 mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1325 mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
1332 if (mmc_card_removed(card))
1333 req->cmd_flags |= REQ_QUIET;
1335 ret = blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
1339 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1340 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1346 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1349 struct mmc_blk_data *md = mq->data;
1350 struct mmc_card *card = md->queue.card;
1352 if (req && !mq->mqrq_prev->req)
1353 /* claim host only for the first request */
1354 mmc_claim_host(card->host);
1356 ret = mmc_blk_part_switch(card, md);
1359 blk_end_request_all(req, -EIO);
1365 if (req && req->cmd_flags & REQ_DISCARD) {
1366 /* complete ongoing async transfer before issuing discard */
1367 if (card->host->areq)
1368 mmc_blk_issue_rw_rq(mq, NULL);
1369 if (req->cmd_flags & REQ_SECURE &&
1370 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1371 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1373 ret = mmc_blk_issue_discard_rq(mq, req);
1374 } else if (req && req->cmd_flags & REQ_FLUSH) {
1375 /* complete ongoing async transfer before issuing flush */
1376 if (card->host->areq)
1377 mmc_blk_issue_rw_rq(mq, NULL);
1378 ret = mmc_blk_issue_flush(mq, req);
1380 ret = mmc_blk_issue_rw_rq(mq, req);
1385 /* release host only when there are no more requests */
1386 mmc_release_host(card->host);
1390 static inline int mmc_blk_readonly(struct mmc_card *card)
1392 return mmc_card_readonly(card) ||
1393 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1396 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1397 struct device *parent,
1400 const char *subname)
1402 struct mmc_blk_data *md;
1405 devidx = find_first_zero_bit(dev_use, max_devices);
1406 if (devidx >= max_devices)
1407 return ERR_PTR(-ENOSPC);
1408 __set_bit(devidx, dev_use);
1410 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1417 * !subname implies we are creating main mmc_blk_data that will be
1418 * associated with mmc_card with mmc_set_drvdata. Due to device
1419 * partitions, devidx will not coincide with a per-physical card
1420 * index anymore so we keep track of a name index.
1423 md->name_idx = find_first_zero_bit(name_use, max_devices);
1424 __set_bit(md->name_idx, name_use);
1427 md->name_idx = ((struct mmc_blk_data *)
1428 dev_to_disk(parent)->private_data)->name_idx;
1431 * Set the read-only status based on the supported commands
1432 * and the write protect switch.
1434 md->read_only = mmc_blk_readonly(card);
1436 md->disk = alloc_disk(perdev_minors);
1437 if (md->disk == NULL) {
1442 spin_lock_init(&md->lock);
1443 INIT_LIST_HEAD(&md->part);
1446 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1450 md->queue.issue_fn = mmc_blk_issue_rq;
1451 md->queue.data = md;
1453 md->disk->major = MMC_BLOCK_MAJOR;
1454 md->disk->first_minor = devidx * perdev_minors;
1455 md->disk->fops = &mmc_bdops;
1456 md->disk->private_data = md;
1457 md->disk->queue = md->queue.queue;
1458 md->disk->driverfs_dev = parent;
1459 set_disk_ro(md->disk, md->read_only || default_ro);
1462 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1464 * - be set for removable media with permanent block devices
1465 * - be unset for removable block devices with permanent media
1467 * Since MMC block devices clearly fall under the second
1468 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1469 * should use the block device creation/destruction hotplug
1470 * messages to tell when the card is present.
1473 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1474 "mmcblk%d%s", md->name_idx, subname ? subname : "");
1476 blk_queue_logical_block_size(md->queue.queue, 512);
1477 set_capacity(md->disk, size);
1479 if (mmc_host_cmd23(card->host)) {
1480 if (mmc_card_mmc(card) ||
1481 (mmc_card_sd(card) &&
1482 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1483 md->flags |= MMC_BLK_CMD23;
1486 if (mmc_card_mmc(card) &&
1487 md->flags & MMC_BLK_CMD23 &&
1488 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1489 card->ext_csd.rel_sectors)) {
1490 md->flags |= MMC_BLK_REL_WR;
1491 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1501 return ERR_PTR(ret);
1504 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1507 struct mmc_blk_data *md;
1509 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1511 * The EXT_CSD sector count is in number or 512 byte
1514 size = card->ext_csd.sectors;
1517 * The CSD capacity field is in units of read_blkbits.
1518 * set_capacity takes units of 512 bytes.
1520 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1523 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1527 static int mmc_blk_alloc_part(struct mmc_card *card,
1528 struct mmc_blk_data *md,
1529 unsigned int part_type,
1532 const char *subname)
1535 struct mmc_blk_data *part_md;
1537 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1539 if (IS_ERR(part_md))
1540 return PTR_ERR(part_md);
1541 part_md->part_type = part_type;
1542 list_add(&part_md->part, &md->part);
1544 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1545 cap_str, sizeof(cap_str));
1546 pr_info("%s: %s %s partition %u %s\n",
1547 part_md->disk->disk_name, mmc_card_id(card),
1548 mmc_card_name(card), part_md->part_type, cap_str);
1552 /* MMC Physical partitions consist of two boot partitions and
1553 * up to four general purpose partitions.
1554 * For each partition enabled in EXT_CSD a block device will be allocatedi
1555 * to provide access to the partition.
1558 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1562 if (!mmc_card_mmc(card))
1565 for (idx = 0; idx < card->nr_parts; idx++) {
1566 if (card->part[idx].size) {
1567 ret = mmc_blk_alloc_part(card, md,
1568 card->part[idx].part_cfg,
1569 card->part[idx].size >> 9,
1570 card->part[idx].force_ro,
1571 card->part[idx].name);
1581 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1585 mmc_claim_host(card->host);
1586 err = mmc_set_blocklen(card, 512);
1587 mmc_release_host(card->host);
1590 pr_err("%s: unable to set block size to 512: %d\n",
1591 md->disk->disk_name, err);
1598 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1601 if (md->disk->flags & GENHD_FL_UP) {
1602 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1604 /* Stop new requests from getting into the queue */
1605 del_gendisk(md->disk);
1608 /* Then flush out any already in there */
1609 mmc_cleanup_queue(&md->queue);
1614 static void mmc_blk_remove_parts(struct mmc_card *card,
1615 struct mmc_blk_data *md)
1617 struct list_head *pos, *q;
1618 struct mmc_blk_data *part_md;
1620 __clear_bit(md->name_idx, name_use);
1621 list_for_each_safe(pos, q, &md->part) {
1622 part_md = list_entry(pos, struct mmc_blk_data, part);
1624 mmc_blk_remove_req(part_md);
1628 static int mmc_add_disk(struct mmc_blk_data *md)
1633 md->force_ro.show = force_ro_show;
1634 md->force_ro.store = force_ro_store;
1635 sysfs_attr_init(&md->force_ro.attr);
1636 md->force_ro.attr.name = "force_ro";
1637 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1638 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1640 del_gendisk(md->disk);
1645 #define CID_MANFID_SAMSUNG 0x15
1647 static const struct mmc_fixup blk_fixups[] =
1649 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1650 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1651 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1652 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1653 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1656 * Some MMC cards experience performance degradation with CMD23
1657 * instead of CMD12-bounded multiblock transfers. For now we'll
1658 * black list what's bad...
1659 * - Certain Toshiba cards.
1661 * N.B. This doesn't affect SD cards.
1663 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1664 MMC_QUIRK_BLK_NO_CMD23),
1665 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1666 MMC_QUIRK_BLK_NO_CMD23),
1667 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1668 MMC_QUIRK_BLK_NO_CMD23),
1671 * Some Micron MMC cards needs longer data read timeout than
1674 MMC_FIXUP(CID_NAME_ANY, 0x13, 0x200, add_quirk_mmc,
1675 MMC_QUIRK_LONG_READ_TIME),
1678 * On these Samsung MoviNAND parts, performing secure erase or
1679 * secure trim can result in unrecoverable corruption due to a
1682 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1683 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1684 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1685 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1686 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1687 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1688 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1689 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1690 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1691 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1692 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1693 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1694 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1695 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1696 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1697 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1702 static int mmc_blk_probe(struct mmc_card *card)
1704 struct mmc_blk_data *md, *part_md;
1709 * Check that the card supports the command class(es) we need.
1711 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1714 md = mmc_blk_alloc(card);
1718 err = mmc_blk_set_blksize(md, card);
1722 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1723 cap_str, sizeof(cap_str));
1724 pr_info("%s: %s %s %s %s\n",
1725 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1726 cap_str, md->read_only ? "(ro)" : "");
1728 if (mmc_blk_alloc_parts(card, md))
1731 mmc_set_drvdata(card, md);
1732 mmc_fixup_device(card, blk_fixups);
1734 if (mmc_add_disk(md))
1737 list_for_each_entry(part_md, &md->part, part) {
1738 if (mmc_add_disk(part_md))
1744 mmc_blk_remove_parts(card, md);
1745 mmc_blk_remove_req(md);
1749 static void mmc_blk_remove(struct mmc_card *card)
1751 struct mmc_blk_data *md = mmc_get_drvdata(card);
1753 mmc_blk_remove_parts(card, md);
1754 mmc_claim_host(card->host);
1755 mmc_blk_part_switch(card, md);
1756 mmc_release_host(card->host);
1757 mmc_blk_remove_req(md);
1758 mmc_set_drvdata(card, NULL);
1762 static int mmc_blk_suspend(struct mmc_card *card)
1764 struct mmc_blk_data *part_md;
1765 struct mmc_blk_data *md = mmc_get_drvdata(card);
1768 mmc_queue_suspend(&md->queue);
1769 list_for_each_entry(part_md, &md->part, part) {
1770 mmc_queue_suspend(&part_md->queue);
1776 static int mmc_blk_resume(struct mmc_card *card)
1778 struct mmc_blk_data *part_md;
1779 struct mmc_blk_data *md = mmc_get_drvdata(card);
1782 mmc_blk_set_blksize(md, card);
1785 * Resume involves the card going into idle state,
1786 * so current partition is always the main one.
1788 md->part_curr = md->part_type;
1789 mmc_queue_resume(&md->queue);
1790 list_for_each_entry(part_md, &md->part, part) {
1791 mmc_queue_resume(&part_md->queue);
1797 #define mmc_blk_suspend NULL
1798 #define mmc_blk_resume NULL
1801 static struct mmc_driver mmc_driver = {
1805 .probe = mmc_blk_probe,
1806 .remove = mmc_blk_remove,
1807 .suspend = mmc_blk_suspend,
1808 .resume = mmc_blk_resume,
1811 static int __init mmc_blk_init(void)
1815 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1816 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1818 max_devices = 256 / perdev_minors;
1820 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1824 res = mmc_register_driver(&mmc_driver);
1830 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1835 static void __exit mmc_blk_exit(void)
1837 mmc_unregister_driver(&mmc_driver);
1838 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1841 module_init(mmc_blk_init);
1842 module_exit(mmc_blk_exit);
1844 MODULE_LICENSE("GPL");
1845 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");