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 {
124 module_param(perdev_minors, int, 0444);
125 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
127 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
129 struct mmc_blk_data *md;
131 mutex_lock(&open_lock);
132 md = disk->private_data;
133 if (md && md->usage == 0)
137 mutex_unlock(&open_lock);
142 static inline int mmc_get_devidx(struct gendisk *disk)
144 int devmaj = MAJOR(disk_devt(disk));
145 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
148 devidx = disk->first_minor / perdev_minors;
152 static void mmc_blk_put(struct mmc_blk_data *md)
154 mutex_lock(&open_lock);
156 if (md->usage == 0) {
157 int devidx = mmc_get_devidx(md->disk);
158 blk_cleanup_queue(md->queue.queue);
160 __clear_bit(devidx, dev_use);
165 mutex_unlock(&open_lock);
168 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
172 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
174 ret = snprintf(buf, PAGE_SIZE, "%d",
175 get_disk_ro(dev_to_disk(dev)) ^
181 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
182 const char *buf, size_t count)
186 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
187 unsigned long set = simple_strtoul(buf, &end, 0);
193 set_disk_ro(dev_to_disk(dev), set || md->read_only);
200 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
202 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
205 mutex_lock(&block_mutex);
208 check_disk_change(bdev);
211 if ((mode & FMODE_WRITE) && md->read_only) {
216 mutex_unlock(&block_mutex);
221 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
223 struct mmc_blk_data *md = disk->private_data;
225 mutex_lock(&block_mutex);
227 mutex_unlock(&block_mutex);
232 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
234 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
240 struct mmc_blk_ioc_data {
241 struct mmc_ioc_cmd ic;
246 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
247 struct mmc_ioc_cmd __user *user)
249 struct mmc_blk_ioc_data *idata;
252 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
258 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
263 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
264 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
269 if (!idata->buf_bytes)
272 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
278 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
279 idata->ic.data_ptr, idata->buf_bytes)) {
294 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
295 struct mmc_ioc_cmd __user *ic_ptr)
297 struct mmc_blk_ioc_data *idata;
298 struct mmc_blk_data *md;
299 struct mmc_card *card;
300 struct mmc_command cmd = {0};
301 struct mmc_data data = {0};
302 struct mmc_request mrq = {NULL};
303 struct scatterlist sg;
307 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
308 * whole block device, not on a partition. This prevents overspray
309 * between sibling partitions.
311 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
314 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
316 return PTR_ERR(idata);
318 md = mmc_blk_get(bdev->bd_disk);
324 card = md->queue.card;
330 cmd.opcode = idata->ic.opcode;
331 cmd.arg = idata->ic.arg;
332 cmd.flags = idata->ic.flags;
334 if (idata->buf_bytes) {
337 data.blksz = idata->ic.blksz;
338 data.blocks = idata->ic.blocks;
340 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
342 if (idata->ic.write_flag)
343 data.flags = MMC_DATA_WRITE;
345 data.flags = MMC_DATA_READ;
347 /* data.flags must already be set before doing this. */
348 mmc_set_data_timeout(&data, card);
350 /* Allow overriding the timeout_ns for empirical tuning. */
351 if (idata->ic.data_timeout_ns)
352 data.timeout_ns = idata->ic.data_timeout_ns;
354 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
356 * Pretend this is a data transfer and rely on the
357 * host driver to compute timeout. When all host
358 * drivers support cmd.cmd_timeout for R1B, this
362 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
364 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
372 mmc_claim_host(card->host);
374 if (idata->ic.is_acmd) {
375 err = mmc_app_cmd(card->host, card);
380 mmc_wait_for_req(card->host, &mrq);
383 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
384 __func__, cmd.error);
389 dev_err(mmc_dev(card->host), "%s: data error %d\n",
390 __func__, data.error);
396 * According to the SD specs, some commands require a delay after
397 * issuing the command.
399 if (idata->ic.postsleep_min_us)
400 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
402 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
407 if (!idata->ic.write_flag) {
408 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
409 idata->buf, idata->buf_bytes)) {
416 mmc_release_host(card->host);
425 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
426 unsigned int cmd, unsigned long arg)
429 if (cmd == MMC_IOC_CMD)
430 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
435 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
436 unsigned int cmd, unsigned long arg)
438 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
442 static const struct block_device_operations mmc_bdops = {
443 .open = mmc_blk_open,
444 .release = mmc_blk_release,
445 .getgeo = mmc_blk_getgeo,
446 .owner = THIS_MODULE,
447 .ioctl = mmc_blk_ioctl,
449 .compat_ioctl = mmc_blk_compat_ioctl,
453 static inline int mmc_blk_part_switch(struct mmc_card *card,
454 struct mmc_blk_data *md)
457 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
459 if (main_md->part_curr == md->part_type)
462 if (mmc_card_mmc(card)) {
463 u8 part_config = card->ext_csd.part_config;
465 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
466 part_config |= md->part_type;
468 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
469 EXT_CSD_PART_CONFIG, part_config,
470 card->ext_csd.part_time);
474 card->ext_csd.part_config = part_config;
477 main_md->part_curr = md->part_type;
481 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
487 struct mmc_request mrq = {NULL};
488 struct mmc_command cmd = {0};
489 struct mmc_data data = {0};
490 unsigned int timeout_us;
492 struct scatterlist sg;
494 cmd.opcode = MMC_APP_CMD;
495 cmd.arg = card->rca << 16;
496 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
498 err = mmc_wait_for_cmd(card->host, &cmd, 0);
501 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
504 memset(&cmd, 0, sizeof(struct mmc_command));
506 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
508 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
510 data.timeout_ns = card->csd.tacc_ns * 100;
511 data.timeout_clks = card->csd.tacc_clks * 100;
513 timeout_us = data.timeout_ns / 1000;
514 timeout_us += data.timeout_clks * 1000 /
515 (card->host->ios.clock / 1000);
517 if (timeout_us > 100000) {
518 data.timeout_ns = 100000000;
519 data.timeout_clks = 0;
524 data.flags = MMC_DATA_READ;
531 blocks = kmalloc(4, GFP_KERNEL);
535 sg_init_one(&sg, blocks, 4);
537 mmc_wait_for_req(card->host, &mrq);
539 result = ntohl(*blocks);
542 if (cmd.error || data.error)
548 static int send_stop(struct mmc_card *card, u32 *status)
550 struct mmc_command cmd = {0};
553 cmd.opcode = MMC_STOP_TRANSMISSION;
554 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
555 err = mmc_wait_for_cmd(card->host, &cmd, 5);
557 *status = cmd.resp[0];
561 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
563 struct mmc_command cmd = {0};
566 cmd.opcode = MMC_SEND_STATUS;
567 if (!mmc_host_is_spi(card->host))
568 cmd.arg = card->rca << 16;
569 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
570 err = mmc_wait_for_cmd(card->host, &cmd, retries);
572 *status = cmd.resp[0];
578 #define ERR_CONTINUE 0
580 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
581 bool status_valid, u32 status)
585 /* response crc error, retry the r/w cmd */
586 pr_err("%s: %s sending %s command, card status %#x\n",
587 req->rq_disk->disk_name, "response CRC error",
592 pr_err("%s: %s sending %s command, card status %#x\n",
593 req->rq_disk->disk_name, "timed out", name, status);
595 /* If the status cmd initially failed, retry the r/w cmd */
600 * If it was a r/w cmd crc error, or illegal command
601 * (eg, issued in wrong state) then retry - we should
602 * have corrected the state problem above.
604 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
607 /* Otherwise abort the command */
611 /* We don't understand the error code the driver gave us */
612 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
613 req->rq_disk->disk_name, error, status);
619 * Initial r/w and stop cmd error recovery.
620 * We don't know whether the card received the r/w cmd or not, so try to
621 * restore things back to a sane state. Essentially, we do this as follows:
622 * - Obtain card status. If the first attempt to obtain card status fails,
623 * the status word will reflect the failed status cmd, not the failed
624 * r/w cmd. If we fail to obtain card status, it suggests we can no
625 * longer communicate with the card.
626 * - Check the card state. If the card received the cmd but there was a
627 * transient problem with the response, it might still be in a data transfer
628 * mode. Try to send it a stop command. If this fails, we can't recover.
629 * - If the r/w cmd failed due to a response CRC error, it was probably
630 * transient, so retry the cmd.
631 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
632 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
633 * illegal cmd, retry.
634 * Otherwise we don't understand what happened, so abort.
636 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
637 struct mmc_blk_request *brq, int *ecc_err)
639 bool prev_cmd_status_valid = true;
640 u32 status, stop_status = 0;
644 * Try to get card status which indicates both the card state
645 * and why there was no response. If the first attempt fails,
646 * we can't be sure the returned status is for the r/w command.
648 for (retry = 2; retry >= 0; retry--) {
649 err = get_card_status(card, &status, 0);
653 prev_cmd_status_valid = false;
654 pr_err("%s: error %d sending status command, %sing\n",
655 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
658 /* We couldn't get a response from the card. Give up. */
662 /* Flag ECC errors */
663 if ((status & R1_CARD_ECC_FAILED) ||
664 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
665 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
669 * Check the current card state. If it is in some data transfer
670 * mode, tell it to stop (and hopefully transition back to TRAN.)
672 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
673 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
674 err = send_stop(card, &stop_status);
676 pr_err("%s: error %d sending stop command\n",
677 req->rq_disk->disk_name, err);
680 * If the stop cmd also timed out, the card is probably
681 * not present, so abort. Other errors are bad news too.
685 if (stop_status & R1_CARD_ECC_FAILED)
689 /* Check for set block count errors */
691 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
692 prev_cmd_status_valid, status);
694 /* Check for r/w command errors */
696 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
697 prev_cmd_status_valid, status);
700 if (!brq->stop.error)
703 /* Now for stop errors. These aren't fatal to the transfer. */
704 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
705 req->rq_disk->disk_name, brq->stop.error,
706 brq->cmd.resp[0], status);
709 * Subsitute in our own stop status as this will give the error
710 * state which happened during the execution of the r/w command.
713 brq->stop.resp[0] = stop_status;
719 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
724 if (md->reset_done & type)
727 md->reset_done |= type;
728 err = mmc_hw_reset(host);
729 /* Ensure we switch back to the correct partition */
730 if (err != -EOPNOTSUPP) {
731 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
734 main_md->part_curr = main_md->part_type;
735 part_err = mmc_blk_part_switch(host->card, md);
738 * We have failed to get back into the correct
739 * partition, so we need to abort the whole request.
747 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
749 md->reset_done &= ~type;
752 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
754 struct mmc_blk_data *md = mq->data;
755 struct mmc_card *card = md->queue.card;
756 unsigned int from, nr, arg;
757 int err = 0, type = MMC_BLK_DISCARD;
759 if (!mmc_can_erase(card)) {
764 from = blk_rq_pos(req);
765 nr = blk_rq_sectors(req);
767 if (mmc_can_discard(card))
768 arg = MMC_DISCARD_ARG;
769 else if (mmc_can_trim(card))
774 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
775 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
776 INAND_CMD38_ARG_EXT_CSD,
777 arg == MMC_TRIM_ARG ?
778 INAND_CMD38_ARG_TRIM :
779 INAND_CMD38_ARG_ERASE,
784 err = mmc_erase(card, from, nr, arg);
786 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
789 mmc_blk_reset_success(md, type);
790 spin_lock_irq(&md->lock);
791 __blk_end_request(req, err, blk_rq_bytes(req));
792 spin_unlock_irq(&md->lock);
797 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
800 struct mmc_blk_data *md = mq->data;
801 struct mmc_card *card = md->queue.card;
802 unsigned int from, nr, arg, trim_arg, erase_arg;
803 int err = 0, type = MMC_BLK_SECDISCARD;
805 if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
810 from = blk_rq_pos(req);
811 nr = blk_rq_sectors(req);
813 /* The sanitize operation is supported at v4.5 only */
814 if (mmc_can_sanitize(card)) {
815 erase_arg = MMC_ERASE_ARG;
816 trim_arg = MMC_TRIM_ARG;
818 erase_arg = MMC_SECURE_ERASE_ARG;
819 trim_arg = MMC_SECURE_TRIM1_ARG;
822 if (mmc_erase_group_aligned(card, from, nr))
824 else if (mmc_can_trim(card))
831 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
832 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
833 INAND_CMD38_ARG_EXT_CSD,
834 arg == MMC_SECURE_TRIM1_ARG ?
835 INAND_CMD38_ARG_SECTRIM1 :
836 INAND_CMD38_ARG_SECERASE,
842 err = mmc_erase(card, from, nr, arg);
848 if (arg == MMC_SECURE_TRIM1_ARG) {
849 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
850 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
851 INAND_CMD38_ARG_EXT_CSD,
852 INAND_CMD38_ARG_SECTRIM2,
858 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
865 if (mmc_can_sanitize(card))
866 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
867 EXT_CSD_SANITIZE_START, 1, 0);
869 if (err && !mmc_blk_reset(md, card->host, type))
872 mmc_blk_reset_success(md, type);
874 spin_lock_irq(&md->lock);
875 __blk_end_request(req, err, blk_rq_bytes(req));
876 spin_unlock_irq(&md->lock);
881 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
883 struct mmc_blk_data *md = mq->data;
884 struct mmc_card *card = md->queue.card;
887 ret = mmc_flush_cache(card);
891 spin_lock_irq(&md->lock);
892 __blk_end_request_all(req, ret);
893 spin_unlock_irq(&md->lock);
899 * Reformat current write as a reliable write, supporting
900 * both legacy and the enhanced reliable write MMC cards.
901 * In each transfer we'll handle only as much as a single
902 * reliable write can handle, thus finish the request in
903 * partial completions.
905 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
906 struct mmc_card *card,
909 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
910 /* Legacy mode imposes restrictions on transfers. */
911 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
912 brq->data.blocks = 1;
914 if (brq->data.blocks > card->ext_csd.rel_sectors)
915 brq->data.blocks = card->ext_csd.rel_sectors;
916 else if (brq->data.blocks < card->ext_csd.rel_sectors)
917 brq->data.blocks = 1;
922 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
923 R1_ADDRESS_ERROR | /* Misaligned address */ \
924 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
925 R1_WP_VIOLATION | /* Tried to write to protected block */ \
926 R1_CC_ERROR | /* Card controller error */ \
927 R1_ERROR) /* General/unknown error */
929 static int mmc_blk_err_check(struct mmc_card *card,
930 struct mmc_async_req *areq)
932 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
934 struct mmc_blk_request *brq = &mq_mrq->brq;
935 struct request *req = mq_mrq->req;
939 * sbc.error indicates a problem with the set block count
940 * command. No data will have been transferred.
942 * cmd.error indicates a problem with the r/w command. No
943 * data will have been transferred.
945 * stop.error indicates a problem with the stop command. Data
946 * may have been transferred, or may still be transferring.
948 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
950 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
952 return MMC_BLK_RETRY;
954 return MMC_BLK_ABORT;
961 * Check for errors relating to the execution of the
962 * initial command - such as address errors. No data
963 * has been transferred.
965 if (brq->cmd.resp[0] & CMD_ERRORS) {
966 pr_err("%s: r/w command failed, status = %#x\n",
967 req->rq_disk->disk_name, brq->cmd.resp[0]);
968 return MMC_BLK_ABORT;
972 * Everything else is either success, or a data error of some
973 * kind. If it was a write, we may have transitioned to
974 * program mode, which we have to wait for it to complete.
976 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
979 int err = get_card_status(card, &status, 5);
981 pr_err("%s: error %d requesting status\n",
982 req->rq_disk->disk_name, err);
983 return MMC_BLK_CMD_ERR;
986 * Some cards mishandle the status bits,
987 * so make sure to check both the busy
988 * indication and the card state.
990 } while (!(status & R1_READY_FOR_DATA) ||
991 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
994 if (brq->data.error) {
995 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
996 req->rq_disk->disk_name, brq->data.error,
997 (unsigned)blk_rq_pos(req),
998 (unsigned)blk_rq_sectors(req),
999 brq->cmd.resp[0], brq->stop.resp[0]);
1001 if (rq_data_dir(req) == READ) {
1003 return MMC_BLK_ECC_ERR;
1004 return MMC_BLK_DATA_ERR;
1006 return MMC_BLK_CMD_ERR;
1010 if (!brq->data.bytes_xfered)
1011 return MMC_BLK_RETRY;
1013 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1014 return MMC_BLK_PARTIAL;
1016 return MMC_BLK_SUCCESS;
1019 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1020 struct mmc_card *card,
1022 struct mmc_queue *mq)
1024 u32 readcmd, writecmd;
1025 struct mmc_blk_request *brq = &mqrq->brq;
1026 struct request *req = mqrq->req;
1027 struct mmc_blk_data *md = mq->data;
1030 * Reliable writes are used to implement Forced Unit Access and
1031 * REQ_META accesses, and are supported only on MMCs.
1033 * XXX: this really needs a good explanation of why REQ_META
1034 * is treated special.
1036 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1037 (req->cmd_flags & REQ_META)) &&
1038 (rq_data_dir(req) == WRITE) &&
1039 (md->flags & MMC_BLK_REL_WR);
1041 memset(brq, 0, sizeof(struct mmc_blk_request));
1042 brq->mrq.cmd = &brq->cmd;
1043 brq->mrq.data = &brq->data;
1045 brq->cmd.arg = blk_rq_pos(req);
1046 if (!mmc_card_blockaddr(card))
1048 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1049 brq->data.blksz = 512;
1050 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1052 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1053 brq->data.blocks = blk_rq_sectors(req);
1056 * The block layer doesn't support all sector count
1057 * restrictions, so we need to be prepared for too big
1060 if (brq->data.blocks > card->host->max_blk_count)
1061 brq->data.blocks = card->host->max_blk_count;
1063 if (brq->data.blocks > 1) {
1065 * After a read error, we redo the request one sector
1066 * at a time in order to accurately determine which
1067 * sectors can be read successfully.
1070 brq->data.blocks = 1;
1072 /* Some controllers can't do multiblock reads due to hw bugs */
1073 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1074 rq_data_dir(req) == READ)
1075 brq->data.blocks = 1;
1078 if (brq->data.blocks > 1 || do_rel_wr) {
1079 /* SPI multiblock writes terminate using a special
1080 * token, not a STOP_TRANSMISSION request.
1082 if (!mmc_host_is_spi(card->host) ||
1083 rq_data_dir(req) == READ)
1084 brq->mrq.stop = &brq->stop;
1085 readcmd = MMC_READ_MULTIPLE_BLOCK;
1086 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1088 brq->mrq.stop = NULL;
1089 readcmd = MMC_READ_SINGLE_BLOCK;
1090 writecmd = MMC_WRITE_BLOCK;
1092 if (rq_data_dir(req) == READ) {
1093 brq->cmd.opcode = readcmd;
1094 brq->data.flags |= MMC_DATA_READ;
1096 brq->cmd.opcode = writecmd;
1097 brq->data.flags |= MMC_DATA_WRITE;
1101 mmc_apply_rel_rw(brq, card, req);
1104 * Pre-defined multi-block transfers are preferable to
1105 * open ended-ones (and necessary for reliable writes).
1106 * However, it is not sufficient to just send CMD23,
1107 * and avoid the final CMD12, as on an error condition
1108 * CMD12 (stop) needs to be sent anyway. This, coupled
1109 * with Auto-CMD23 enhancements provided by some
1110 * hosts, means that the complexity of dealing
1111 * with this is best left to the host. If CMD23 is
1112 * supported by card and host, we'll fill sbc in and let
1113 * the host deal with handling it correctly. This means
1114 * that for hosts that don't expose MMC_CAP_CMD23, no
1115 * change of behavior will be observed.
1117 * N.B: Some MMC cards experience perf degradation.
1118 * We'll avoid using CMD23-bounded multiblock writes for
1119 * these, while retaining features like reliable writes.
1122 if ((md->flags & MMC_BLK_CMD23) &&
1123 mmc_op_multi(brq->cmd.opcode) &&
1124 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
1125 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1126 brq->sbc.arg = brq->data.blocks |
1127 (do_rel_wr ? (1 << 31) : 0);
1128 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1129 brq->mrq.sbc = &brq->sbc;
1132 mmc_set_data_timeout(&brq->data, card);
1134 brq->data.sg = mqrq->sg;
1135 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1138 * Adjust the sg list so it is the same size as the
1141 if (brq->data.blocks != blk_rq_sectors(req)) {
1142 int i, data_size = brq->data.blocks << 9;
1143 struct scatterlist *sg;
1145 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1146 data_size -= sg->length;
1147 if (data_size <= 0) {
1148 sg->length += data_size;
1153 brq->data.sg_len = i;
1156 mqrq->mmc_active.mrq = &brq->mrq;
1157 mqrq->mmc_active.err_check = mmc_blk_err_check;
1159 mmc_queue_bounce_pre(mqrq);
1162 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1163 struct mmc_blk_request *brq, struct request *req,
1167 * If this is an SD card and we're writing, we can first
1168 * mark the known good sectors as ok.
1170 * If the card is not SD, we can still ok written sectors
1171 * as reported by the controller (which might be less than
1172 * the real number of written sectors, but never more).
1174 if (mmc_card_sd(card)) {
1177 blocks = mmc_sd_num_wr_blocks(card);
1178 if (blocks != (u32)-1) {
1179 spin_lock_irq(&md->lock);
1180 ret = __blk_end_request(req, 0, blocks << 9);
1181 spin_unlock_irq(&md->lock);
1184 spin_lock_irq(&md->lock);
1185 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
1186 spin_unlock_irq(&md->lock);
1191 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1193 struct mmc_blk_data *md = mq->data;
1194 struct mmc_card *card = md->queue.card;
1195 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1196 int ret = 1, disable_multi = 0, retry = 0, type;
1197 enum mmc_blk_status status;
1198 struct mmc_queue_req *mq_rq;
1199 struct request *req;
1200 struct mmc_async_req *areq;
1202 if (!rqc && !mq->mqrq_prev->req)
1207 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1208 areq = &mq->mqrq_cur->mmc_active;
1211 areq = mmc_start_req(card->host, areq, (int *) &status);
1215 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1218 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1219 mmc_queue_bounce_post(mq_rq);
1222 case MMC_BLK_SUCCESS:
1223 case MMC_BLK_PARTIAL:
1225 * A block was successfully transferred.
1227 mmc_blk_reset_success(md, type);
1228 spin_lock_irq(&md->lock);
1229 ret = __blk_end_request(req, 0,
1230 brq->data.bytes_xfered);
1231 spin_unlock_irq(&md->lock);
1233 * If the blk_end_request function returns non-zero even
1234 * though all data has been transferred and no errors
1235 * were returned by the host controller, it's a bug.
1237 if (status == MMC_BLK_SUCCESS && ret) {
1238 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1239 __func__, blk_rq_bytes(req),
1240 brq->data.bytes_xfered);
1245 case MMC_BLK_CMD_ERR:
1246 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1247 if (!mmc_blk_reset(md, card->host, type))
1255 if (!mmc_blk_reset(md, card->host, type))
1258 case MMC_BLK_DATA_ERR: {
1261 err = mmc_blk_reset(md, card->host, type);
1268 case MMC_BLK_ECC_ERR:
1269 if (brq->data.blocks > 1) {
1270 /* Redo read one sector at a time */
1271 pr_warning("%s: retrying using single block read\n",
1272 req->rq_disk->disk_name);
1277 * After an error, we redo I/O one sector at a
1278 * time, so we only reach here after trying to
1279 * read a single sector.
1281 spin_lock_irq(&md->lock);
1282 ret = __blk_end_request(req, -EIO,
1284 spin_unlock_irq(&md->lock);
1292 * In case of a incomplete request
1293 * prepare it again and resend.
1295 mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1296 mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
1303 spin_lock_irq(&md->lock);
1305 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
1306 spin_unlock_irq(&md->lock);
1310 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1311 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1317 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1320 struct mmc_blk_data *md = mq->data;
1321 struct mmc_card *card = md->queue.card;
1323 if (req && !mq->mqrq_prev->req)
1324 /* claim host only for the first request */
1325 mmc_claim_host(card->host);
1327 ret = mmc_blk_part_switch(card, md);
1330 spin_lock_irq(&md->lock);
1331 __blk_end_request_all(req, -EIO);
1332 spin_unlock_irq(&md->lock);
1338 if (req && req->cmd_flags & REQ_DISCARD) {
1339 /* complete ongoing async transfer before issuing discard */
1340 if (card->host->areq)
1341 mmc_blk_issue_rw_rq(mq, NULL);
1342 if (req->cmd_flags & REQ_SECURE &&
1343 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1344 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1346 ret = mmc_blk_issue_discard_rq(mq, req);
1347 } else if (req && req->cmd_flags & REQ_FLUSH) {
1348 /* complete ongoing async transfer before issuing flush */
1349 if (card->host->areq)
1350 mmc_blk_issue_rw_rq(mq, NULL);
1351 ret = mmc_blk_issue_flush(mq, req);
1353 ret = mmc_blk_issue_rw_rq(mq, req);
1358 /* release host only when there are no more requests */
1359 mmc_release_host(card->host);
1363 static inline int mmc_blk_readonly(struct mmc_card *card)
1365 return mmc_card_readonly(card) ||
1366 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1369 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1370 struct device *parent,
1373 const char *subname)
1375 struct mmc_blk_data *md;
1378 devidx = find_first_zero_bit(dev_use, max_devices);
1379 if (devidx >= max_devices)
1380 return ERR_PTR(-ENOSPC);
1381 __set_bit(devidx, dev_use);
1383 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1390 * !subname implies we are creating main mmc_blk_data that will be
1391 * associated with mmc_card with mmc_set_drvdata. Due to device
1392 * partitions, devidx will not coincide with a per-physical card
1393 * index anymore so we keep track of a name index.
1396 md->name_idx = find_first_zero_bit(name_use, max_devices);
1397 __set_bit(md->name_idx, name_use);
1400 md->name_idx = ((struct mmc_blk_data *)
1401 dev_to_disk(parent)->private_data)->name_idx;
1404 * Set the read-only status based on the supported commands
1405 * and the write protect switch.
1407 md->read_only = mmc_blk_readonly(card);
1409 md->disk = alloc_disk(perdev_minors);
1410 if (md->disk == NULL) {
1415 spin_lock_init(&md->lock);
1416 INIT_LIST_HEAD(&md->part);
1419 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1423 md->queue.issue_fn = mmc_blk_issue_rq;
1424 md->queue.data = md;
1426 md->disk->major = MMC_BLOCK_MAJOR;
1427 md->disk->first_minor = devidx * perdev_minors;
1428 md->disk->fops = &mmc_bdops;
1429 md->disk->private_data = md;
1430 md->disk->queue = md->queue.queue;
1431 md->disk->driverfs_dev = parent;
1432 set_disk_ro(md->disk, md->read_only || default_ro);
1435 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1437 * - be set for removable media with permanent block devices
1438 * - be unset for removable block devices with permanent media
1440 * Since MMC block devices clearly fall under the second
1441 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1442 * should use the block device creation/destruction hotplug
1443 * messages to tell when the card is present.
1446 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1447 "mmcblk%d%s", md->name_idx, subname ? subname : "");
1449 blk_queue_logical_block_size(md->queue.queue, 512);
1450 set_capacity(md->disk, size);
1452 if (mmc_host_cmd23(card->host)) {
1453 if (mmc_card_mmc(card) ||
1454 (mmc_card_sd(card) &&
1455 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1456 md->flags |= MMC_BLK_CMD23;
1459 if (mmc_card_mmc(card) &&
1460 md->flags & MMC_BLK_CMD23 &&
1461 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1462 card->ext_csd.rel_sectors)) {
1463 md->flags |= MMC_BLK_REL_WR;
1464 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1474 return ERR_PTR(ret);
1477 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1480 struct mmc_blk_data *md;
1482 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1484 * The EXT_CSD sector count is in number or 512 byte
1487 size = card->ext_csd.sectors;
1490 * The CSD capacity field is in units of read_blkbits.
1491 * set_capacity takes units of 512 bytes.
1493 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1496 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1500 static int mmc_blk_alloc_part(struct mmc_card *card,
1501 struct mmc_blk_data *md,
1502 unsigned int part_type,
1505 const char *subname)
1508 struct mmc_blk_data *part_md;
1510 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1512 if (IS_ERR(part_md))
1513 return PTR_ERR(part_md);
1514 part_md->part_type = part_type;
1515 list_add(&part_md->part, &md->part);
1517 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1518 cap_str, sizeof(cap_str));
1519 pr_info("%s: %s %s partition %u %s\n",
1520 part_md->disk->disk_name, mmc_card_id(card),
1521 mmc_card_name(card), part_md->part_type, cap_str);
1525 /* MMC Physical partitions consist of two boot partitions and
1526 * up to four general purpose partitions.
1527 * For each partition enabled in EXT_CSD a block device will be allocatedi
1528 * to provide access to the partition.
1531 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1535 if (!mmc_card_mmc(card))
1538 for (idx = 0; idx < card->nr_parts; idx++) {
1539 if (card->part[idx].size) {
1540 ret = mmc_blk_alloc_part(card, md,
1541 card->part[idx].part_cfg,
1542 card->part[idx].size >> 9,
1543 card->part[idx].force_ro,
1544 card->part[idx].name);
1554 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1558 mmc_claim_host(card->host);
1559 err = mmc_set_blocklen(card, 512);
1560 mmc_release_host(card->host);
1563 pr_err("%s: unable to set block size to 512: %d\n",
1564 md->disk->disk_name, err);
1571 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1574 if (md->disk->flags & GENHD_FL_UP) {
1575 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1577 /* Stop new requests from getting into the queue */
1578 del_gendisk(md->disk);
1581 /* Then flush out any already in there */
1582 mmc_cleanup_queue(&md->queue);
1587 static void mmc_blk_remove_parts(struct mmc_card *card,
1588 struct mmc_blk_data *md)
1590 struct list_head *pos, *q;
1591 struct mmc_blk_data *part_md;
1593 __clear_bit(md->name_idx, name_use);
1594 list_for_each_safe(pos, q, &md->part) {
1595 part_md = list_entry(pos, struct mmc_blk_data, part);
1597 mmc_blk_remove_req(part_md);
1601 static int mmc_add_disk(struct mmc_blk_data *md)
1606 md->force_ro.show = force_ro_show;
1607 md->force_ro.store = force_ro_store;
1608 sysfs_attr_init(&md->force_ro.attr);
1609 md->force_ro.attr.name = "force_ro";
1610 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1611 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1613 del_gendisk(md->disk);
1618 #define CID_MANFID_SAMSUNG 0x15
1620 static const struct mmc_fixup blk_fixups[] =
1622 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1623 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1624 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1625 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1626 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1629 * Some MMC cards experience performance degradation with CMD23
1630 * instead of CMD12-bounded multiblock transfers. For now we'll
1631 * black list what's bad...
1632 * - Certain Toshiba cards.
1634 * N.B. This doesn't affect SD cards.
1636 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1637 MMC_QUIRK_BLK_NO_CMD23),
1638 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1639 MMC_QUIRK_BLK_NO_CMD23),
1640 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1641 MMC_QUIRK_BLK_NO_CMD23),
1644 * Some Micron MMC cards needs longer data read timeout than
1647 MMC_FIXUP(CID_NAME_ANY, 0x13, 0x200, add_quirk_mmc,
1648 MMC_QUIRK_LONG_READ_TIME),
1651 * On these Samsung MoviNAND parts, performing secure erase or
1652 * secure trim can result in unrecoverable corruption due to a
1655 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1656 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1657 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1658 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1659 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1660 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1661 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1662 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1663 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1664 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1665 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1666 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1667 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1668 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1669 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1670 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1675 static int mmc_blk_probe(struct mmc_card *card)
1677 struct mmc_blk_data *md, *part_md;
1682 * Check that the card supports the command class(es) we need.
1684 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1687 md = mmc_blk_alloc(card);
1691 err = mmc_blk_set_blksize(md, card);
1695 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1696 cap_str, sizeof(cap_str));
1697 pr_info("%s: %s %s %s %s\n",
1698 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1699 cap_str, md->read_only ? "(ro)" : "");
1701 if (mmc_blk_alloc_parts(card, md))
1704 mmc_set_drvdata(card, md);
1705 mmc_fixup_device(card, blk_fixups);
1707 if (mmc_add_disk(md))
1710 list_for_each_entry(part_md, &md->part, part) {
1711 if (mmc_add_disk(part_md))
1717 mmc_blk_remove_parts(card, md);
1718 mmc_blk_remove_req(md);
1722 static void mmc_blk_remove(struct mmc_card *card)
1724 struct mmc_blk_data *md = mmc_get_drvdata(card);
1726 mmc_blk_remove_parts(card, md);
1727 mmc_claim_host(card->host);
1728 mmc_blk_part_switch(card, md);
1729 mmc_release_host(card->host);
1730 mmc_blk_remove_req(md);
1731 mmc_set_drvdata(card, NULL);
1735 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1737 struct mmc_blk_data *part_md;
1738 struct mmc_blk_data *md = mmc_get_drvdata(card);
1741 mmc_queue_suspend(&md->queue);
1742 list_for_each_entry(part_md, &md->part, part) {
1743 mmc_queue_suspend(&part_md->queue);
1749 static int mmc_blk_resume(struct mmc_card *card)
1751 struct mmc_blk_data *part_md;
1752 struct mmc_blk_data *md = mmc_get_drvdata(card);
1755 mmc_blk_set_blksize(md, card);
1758 * Resume involves the card going into idle state,
1759 * so current partition is always the main one.
1761 md->part_curr = md->part_type;
1762 mmc_queue_resume(&md->queue);
1763 list_for_each_entry(part_md, &md->part, part) {
1764 mmc_queue_resume(&part_md->queue);
1770 #define mmc_blk_suspend NULL
1771 #define mmc_blk_resume NULL
1774 static struct mmc_driver mmc_driver = {
1778 .probe = mmc_blk_probe,
1779 .remove = mmc_blk_remove,
1780 .suspend = mmc_blk_suspend,
1781 .resume = mmc_blk_resume,
1784 static int __init mmc_blk_init(void)
1788 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1789 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1791 max_devices = 256 / perdev_minors;
1793 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1797 res = mmc_register_driver(&mmc_driver);
1803 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1808 static void __exit mmc_blk_exit(void)
1810 mmc_unregister_driver(&mmc_driver);
1811 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1814 module_init(mmc_blk_init);
1815 module_exit(mmc_blk_exit);
1817 MODULE_LICENSE("GPL");
1818 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");