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;
99 * Only set in main mmc_blk_data associated
100 * with mmc_card with mmc_set_drvdata, and keeps
101 * track of the current selected device partition.
103 unsigned int part_curr;
104 struct device_attribute force_ro;
107 static DEFINE_MUTEX(open_lock);
109 module_param(perdev_minors, int, 0444);
110 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
112 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
114 struct mmc_blk_data *md;
116 mutex_lock(&open_lock);
117 md = disk->private_data;
118 if (md && md->usage == 0)
122 mutex_unlock(&open_lock);
127 static inline int mmc_get_devidx(struct gendisk *disk)
129 int devmaj = MAJOR(disk_devt(disk));
130 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
133 devidx = disk->first_minor / perdev_minors;
137 static void mmc_blk_put(struct mmc_blk_data *md)
139 mutex_lock(&open_lock);
141 if (md->usage == 0) {
142 int devidx = mmc_get_devidx(md->disk);
143 blk_cleanup_queue(md->queue.queue);
145 __clear_bit(devidx, dev_use);
150 mutex_unlock(&open_lock);
153 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
157 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
159 ret = snprintf(buf, PAGE_SIZE, "%d",
160 get_disk_ro(dev_to_disk(dev)) ^
166 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
167 const char *buf, size_t count)
171 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172 unsigned long set = simple_strtoul(buf, &end, 0);
178 set_disk_ro(dev_to_disk(dev), set || md->read_only);
185 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
187 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
190 mutex_lock(&block_mutex);
193 check_disk_change(bdev);
196 if ((mode & FMODE_WRITE) && md->read_only) {
201 mutex_unlock(&block_mutex);
206 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
208 struct mmc_blk_data *md = disk->private_data;
210 mutex_lock(&block_mutex);
212 mutex_unlock(&block_mutex);
217 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
219 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
225 struct mmc_blk_ioc_data {
226 struct mmc_ioc_cmd ic;
231 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
232 struct mmc_ioc_cmd __user *user)
234 struct mmc_blk_ioc_data *idata;
237 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
243 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
248 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
249 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
254 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
260 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
261 idata->ic.data_ptr, idata->buf_bytes)) {
276 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
277 struct mmc_ioc_cmd __user *ic_ptr)
279 struct mmc_blk_ioc_data *idata;
280 struct mmc_blk_data *md;
281 struct mmc_card *card;
282 struct mmc_command cmd = {0};
283 struct mmc_data data = {0};
284 struct mmc_request mrq = {0};
285 struct scatterlist sg;
289 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
290 * whole block device, not on a partition. This prevents overspray
291 * between sibling partitions.
293 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
296 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
298 return PTR_ERR(idata);
300 cmd.opcode = idata->ic.opcode;
301 cmd.arg = idata->ic.arg;
302 cmd.flags = idata->ic.flags;
306 data.blksz = idata->ic.blksz;
307 data.blocks = idata->ic.blocks;
309 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
311 if (idata->ic.write_flag)
312 data.flags = MMC_DATA_WRITE;
314 data.flags = MMC_DATA_READ;
319 md = mmc_blk_get(bdev->bd_disk);
325 card = md->queue.card;
331 mmc_claim_host(card->host);
333 if (idata->ic.is_acmd) {
334 err = mmc_app_cmd(card->host, card);
339 /* data.flags must already be set before doing this. */
340 mmc_set_data_timeout(&data, card);
341 /* Allow overriding the timeout_ns for empirical tuning. */
342 if (idata->ic.data_timeout_ns)
343 data.timeout_ns = idata->ic.data_timeout_ns;
345 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
347 * Pretend this is a data transfer and rely on the host driver
348 * to compute timeout. When all host drivers support
349 * cmd.cmd_timeout for R1B, this can be changed to:
352 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
354 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
357 mmc_wait_for_req(card->host, &mrq);
360 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
361 __func__, cmd.error);
366 dev_err(mmc_dev(card->host), "%s: data error %d\n",
367 __func__, data.error);
373 * According to the SD specs, some commands require a delay after
374 * issuing the command.
376 if (idata->ic.postsleep_min_us)
377 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
379 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
384 if (!idata->ic.write_flag) {
385 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
386 idata->buf, idata->buf_bytes)) {
393 mmc_release_host(card->host);
402 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
403 unsigned int cmd, unsigned long arg)
406 if (cmd == MMC_IOC_CMD)
407 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
412 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
413 unsigned int cmd, unsigned long arg)
415 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
419 static const struct block_device_operations mmc_bdops = {
420 .open = mmc_blk_open,
421 .release = mmc_blk_release,
422 .getgeo = mmc_blk_getgeo,
423 .owner = THIS_MODULE,
424 .ioctl = mmc_blk_ioctl,
426 .compat_ioctl = mmc_blk_compat_ioctl,
430 static inline int mmc_blk_part_switch(struct mmc_card *card,
431 struct mmc_blk_data *md)
434 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
435 if (main_md->part_curr == md->part_type)
438 if (mmc_card_mmc(card)) {
439 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
440 card->ext_csd.part_config |= md->part_type;
442 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
443 EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
444 card->ext_csd.part_time);
449 main_md->part_curr = md->part_type;
453 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
459 struct mmc_request mrq = {0};
460 struct mmc_command cmd = {0};
461 struct mmc_data data = {0};
462 unsigned int timeout_us;
464 struct scatterlist sg;
466 cmd.opcode = MMC_APP_CMD;
467 cmd.arg = card->rca << 16;
468 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
470 err = mmc_wait_for_cmd(card->host, &cmd, 0);
473 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
476 memset(&cmd, 0, sizeof(struct mmc_command));
478 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
480 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
482 data.timeout_ns = card->csd.tacc_ns * 100;
483 data.timeout_clks = card->csd.tacc_clks * 100;
485 timeout_us = data.timeout_ns / 1000;
486 timeout_us += data.timeout_clks * 1000 /
487 (card->host->ios.clock / 1000);
489 if (timeout_us > 100000) {
490 data.timeout_ns = 100000000;
491 data.timeout_clks = 0;
496 data.flags = MMC_DATA_READ;
503 blocks = kmalloc(4, GFP_KERNEL);
507 sg_init_one(&sg, blocks, 4);
509 mmc_wait_for_req(card->host, &mrq);
511 result = ntohl(*blocks);
514 if (cmd.error || data.error)
520 static int send_stop(struct mmc_card *card, u32 *status)
522 struct mmc_command cmd = {0};
525 cmd.opcode = MMC_STOP_TRANSMISSION;
526 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
527 err = mmc_wait_for_cmd(card->host, &cmd, 5);
529 *status = cmd.resp[0];
533 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
535 struct mmc_command cmd = {0};
538 cmd.opcode = MMC_SEND_STATUS;
539 if (!mmc_host_is_spi(card->host))
540 cmd.arg = card->rca << 16;
541 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
542 err = mmc_wait_for_cmd(card->host, &cmd, retries);
544 *status = cmd.resp[0];
550 #define ERR_CONTINUE 0
552 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
553 bool status_valid, u32 status)
557 /* response crc error, retry the r/w cmd */
558 pr_err("%s: %s sending %s command, card status %#x\n",
559 req->rq_disk->disk_name, "response CRC error",
564 pr_err("%s: %s sending %s command, card status %#x\n",
565 req->rq_disk->disk_name, "timed out", name, status);
567 /* If the status cmd initially failed, retry the r/w cmd */
572 * If it was a r/w cmd crc error, or illegal command
573 * (eg, issued in wrong state) then retry - we should
574 * have corrected the state problem above.
576 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
579 /* Otherwise abort the command */
583 /* We don't understand the error code the driver gave us */
584 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
585 req->rq_disk->disk_name, error, status);
591 * Initial r/w and stop cmd error recovery.
592 * We don't know whether the card received the r/w cmd or not, so try to
593 * restore things back to a sane state. Essentially, we do this as follows:
594 * - Obtain card status. If the first attempt to obtain card status fails,
595 * the status word will reflect the failed status cmd, not the failed
596 * r/w cmd. If we fail to obtain card status, it suggests we can no
597 * longer communicate with the card.
598 * - Check the card state. If the card received the cmd but there was a
599 * transient problem with the response, it might still be in a data transfer
600 * mode. Try to send it a stop command. If this fails, we can't recover.
601 * - If the r/w cmd failed due to a response CRC error, it was probably
602 * transient, so retry the cmd.
603 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
604 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
605 * illegal cmd, retry.
606 * Otherwise we don't understand what happened, so abort.
608 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
609 struct mmc_blk_request *brq)
611 bool prev_cmd_status_valid = true;
612 u32 status, stop_status = 0;
616 * Try to get card status which indicates both the card state
617 * and why there was no response. If the first attempt fails,
618 * we can't be sure the returned status is for the r/w command.
620 for (retry = 2; retry >= 0; retry--) {
621 err = get_card_status(card, &status, 0);
625 prev_cmd_status_valid = false;
626 pr_err("%s: error %d sending status command, %sing\n",
627 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
630 /* We couldn't get a response from the card. Give up. */
635 * Check the current card state. If it is in some data transfer
636 * mode, tell it to stop (and hopefully transition back to TRAN.)
638 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
639 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
640 err = send_stop(card, &stop_status);
642 pr_err("%s: error %d sending stop command\n",
643 req->rq_disk->disk_name, err);
646 * If the stop cmd also timed out, the card is probably
647 * not present, so abort. Other errors are bad news too.
653 /* Check for set block count errors */
655 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
656 prev_cmd_status_valid, status);
658 /* Check for r/w command errors */
660 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
661 prev_cmd_status_valid, status);
663 /* Now for stop errors. These aren't fatal to the transfer. */
664 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
665 req->rq_disk->disk_name, brq->stop.error,
666 brq->cmd.resp[0], status);
669 * Subsitute in our own stop status as this will give the error
670 * state which happened during the execution of the r/w command.
673 brq->stop.resp[0] = stop_status;
679 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
681 struct mmc_blk_data *md = mq->data;
682 struct mmc_card *card = md->queue.card;
683 unsigned int from, nr, arg;
686 if (!mmc_can_erase(card)) {
691 from = blk_rq_pos(req);
692 nr = blk_rq_sectors(req);
694 if (mmc_can_trim(card))
699 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
700 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
701 INAND_CMD38_ARG_EXT_CSD,
702 arg == MMC_TRIM_ARG ?
703 INAND_CMD38_ARG_TRIM :
704 INAND_CMD38_ARG_ERASE,
709 err = mmc_erase(card, from, nr, arg);
711 spin_lock_irq(&md->lock);
712 __blk_end_request(req, err, blk_rq_bytes(req));
713 spin_unlock_irq(&md->lock);
718 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
721 struct mmc_blk_data *md = mq->data;
722 struct mmc_card *card = md->queue.card;
723 unsigned int from, nr, arg;
726 if (!mmc_can_secure_erase_trim(card)) {
731 from = blk_rq_pos(req);
732 nr = blk_rq_sectors(req);
734 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
735 arg = MMC_SECURE_TRIM1_ARG;
737 arg = MMC_SECURE_ERASE_ARG;
739 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
740 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
741 INAND_CMD38_ARG_EXT_CSD,
742 arg == MMC_SECURE_TRIM1_ARG ?
743 INAND_CMD38_ARG_SECTRIM1 :
744 INAND_CMD38_ARG_SECERASE,
749 err = mmc_erase(card, from, nr, arg);
750 if (!err && arg == MMC_SECURE_TRIM1_ARG) {
751 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
752 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
753 INAND_CMD38_ARG_EXT_CSD,
754 INAND_CMD38_ARG_SECTRIM2,
759 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
762 spin_lock_irq(&md->lock);
763 __blk_end_request(req, err, blk_rq_bytes(req));
764 spin_unlock_irq(&md->lock);
769 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
771 struct mmc_blk_data *md = mq->data;
774 * No-op, only service this because we need REQ_FUA for reliable
777 spin_lock_irq(&md->lock);
778 __blk_end_request_all(req, 0);
779 spin_unlock_irq(&md->lock);
785 * Reformat current write as a reliable write, supporting
786 * both legacy and the enhanced reliable write MMC cards.
787 * In each transfer we'll handle only as much as a single
788 * reliable write can handle, thus finish the request in
789 * partial completions.
791 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
792 struct mmc_card *card,
795 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
796 /* Legacy mode imposes restrictions on transfers. */
797 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
798 brq->data.blocks = 1;
800 if (brq->data.blocks > card->ext_csd.rel_sectors)
801 brq->data.blocks = card->ext_csd.rel_sectors;
802 else if (brq->data.blocks < card->ext_csd.rel_sectors)
803 brq->data.blocks = 1;
808 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
809 R1_ADDRESS_ERROR | /* Misaligned address */ \
810 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
811 R1_WP_VIOLATION | /* Tried to write to protected block */ \
812 R1_CC_ERROR | /* Card controller error */ \
813 R1_ERROR) /* General/unknown error */
815 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
816 struct mmc_card *card,
818 struct mmc_queue *mq)
820 u32 readcmd, writecmd;
821 struct mmc_blk_request *brq = &mqrq->brq;
822 struct request *req = mqrq->req;
823 struct mmc_blk_data *md = mq->data;
826 * Reliable writes are used to implement Forced Unit Access and
827 * REQ_META accesses, and are supported only on MMCs.
829 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
830 (req->cmd_flags & REQ_META)) &&
831 (rq_data_dir(req) == WRITE) &&
832 (md->flags & MMC_BLK_REL_WR);
834 memset(brq, 0, sizeof(struct mmc_blk_request));
835 brq->mrq.cmd = &brq->cmd;
836 brq->mrq.data = &brq->data;
838 brq->cmd.arg = blk_rq_pos(req);
839 if (!mmc_card_blockaddr(card))
841 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
842 brq->data.blksz = 512;
843 brq->stop.opcode = MMC_STOP_TRANSMISSION;
845 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
846 brq->data.blocks = blk_rq_sectors(req);
849 * The block layer doesn't support all sector count
850 * restrictions, so we need to be prepared for too big
853 if (brq->data.blocks > card->host->max_blk_count)
854 brq->data.blocks = card->host->max_blk_count;
857 * After a read error, we redo the request one sector at a time
858 * in order to accurately determine which sectors can be read
861 if (disable_multi && brq->data.blocks > 1)
862 brq->data.blocks = 1;
864 if (brq->data.blocks > 1 || do_rel_wr) {
865 /* SPI multiblock writes terminate using a special
866 * token, not a STOP_TRANSMISSION request.
868 if (!mmc_host_is_spi(card->host) ||
869 rq_data_dir(req) == READ)
870 brq->mrq.stop = &brq->stop;
871 readcmd = MMC_READ_MULTIPLE_BLOCK;
872 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
874 brq->mrq.stop = NULL;
875 readcmd = MMC_READ_SINGLE_BLOCK;
876 writecmd = MMC_WRITE_BLOCK;
878 if (rq_data_dir(req) == READ) {
879 brq->cmd.opcode = readcmd;
880 brq->data.flags |= MMC_DATA_READ;
882 brq->cmd.opcode = writecmd;
883 brq->data.flags |= MMC_DATA_WRITE;
887 mmc_apply_rel_rw(brq, card, req);
890 * Pre-defined multi-block transfers are preferable to
891 * open ended-ones (and necessary for reliable writes).
892 * However, it is not sufficient to just send CMD23,
893 * and avoid the final CMD12, as on an error condition
894 * CMD12 (stop) needs to be sent anyway. This, coupled
895 * with Auto-CMD23 enhancements provided by some
896 * hosts, means that the complexity of dealing
897 * with this is best left to the host. If CMD23 is
898 * supported by card and host, we'll fill sbc in and let
899 * the host deal with handling it correctly. This means
900 * that for hosts that don't expose MMC_CAP_CMD23, no
901 * change of behavior will be observed.
903 * N.B: Some MMC cards experience perf degradation.
904 * We'll avoid using CMD23-bounded multiblock writes for
905 * these, while retaining features like reliable writes.
908 if ((md->flags & MMC_BLK_CMD23) &&
909 mmc_op_multi(brq->cmd.opcode) &&
910 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
911 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
912 brq->sbc.arg = brq->data.blocks |
913 (do_rel_wr ? (1 << 31) : 0);
914 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
915 brq->mrq.sbc = &brq->sbc;
918 mmc_set_data_timeout(&brq->data, card);
920 brq->data.sg = mqrq->sg;
921 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
924 * Adjust the sg list so it is the same size as the
927 if (brq->data.blocks != blk_rq_sectors(req)) {
928 int i, data_size = brq->data.blocks << 9;
929 struct scatterlist *sg;
931 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
932 data_size -= sg->length;
933 if (data_size <= 0) {
934 sg->length += data_size;
939 brq->data.sg_len = i;
942 mmc_queue_bounce_pre(mqrq);
945 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
947 struct mmc_blk_data *md = mq->data;
948 struct mmc_card *card = md->queue.card;
949 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
950 int ret = 1, disable_multi = 0, retry = 0;
953 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, disable_multi, mq);
954 mmc_wait_for_req(card->host, &brq->mrq);
956 mmc_queue_bounce_post(mq->mqrq_cur);
959 * sbc.error indicates a problem with the set block count
960 * command. No data will have been transferred.
962 * cmd.error indicates a problem with the r/w command. No
963 * data will have been transferred.
965 * stop.error indicates a problem with the stop command. Data
966 * may have been transferred, or may still be transferring.
968 if (brq->sbc.error || brq->cmd.error || brq->stop.error) {
969 switch (mmc_blk_cmd_recovery(card, req, brq)) {
981 * Check for errors relating to the execution of the
982 * initial command - such as address errors. No data
983 * has been transferred.
985 if (brq->cmd.resp[0] & CMD_ERRORS) {
986 pr_err("%s: r/w command failed, status = %#x\n",
987 req->rq_disk->disk_name, brq->cmd.resp[0]);
992 * Everything else is either success, or a data error of some
993 * kind. If it was a write, we may have transitioned to
994 * program mode, which we have to wait for it to complete.
996 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
999 int err = get_card_status(card, &status, 5);
1001 printk(KERN_ERR "%s: error %d requesting status\n",
1002 req->rq_disk->disk_name, err);
1006 * Some cards mishandle the status bits,
1007 * so make sure to check both the busy
1008 * indication and the card state.
1010 } while (!(status & R1_READY_FOR_DATA) ||
1011 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1014 if (brq->data.error) {
1015 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1016 req->rq_disk->disk_name, brq->data.error,
1017 (unsigned)blk_rq_pos(req),
1018 (unsigned)blk_rq_sectors(req),
1019 brq->cmd.resp[0], brq->stop.resp[0]);
1021 if (rq_data_dir(req) == READ) {
1022 if (brq->data.blocks > 1) {
1023 /* Redo read one sector at a time */
1024 pr_warning("%s: retrying using single block read\n",
1025 req->rq_disk->disk_name);
1031 * After an error, we redo I/O one sector at a
1032 * time, so we only reach here after trying to
1033 * read a single sector.
1035 spin_lock_irq(&md->lock);
1036 ret = __blk_end_request(req, -EIO,
1038 spin_unlock_irq(&md->lock);
1046 * A block was successfully transferred.
1048 spin_lock_irq(&md->lock);
1049 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
1050 spin_unlock_irq(&md->lock);
1057 * If this is an SD card and we're writing, we can first
1058 * mark the known good sectors as ok.
1060 * If the card is not SD, we can still ok written sectors
1061 * as reported by the controller (which might be less than
1062 * the real number of written sectors, but never more).
1064 if (mmc_card_sd(card)) {
1067 blocks = mmc_sd_num_wr_blocks(card);
1068 if (blocks != (u32)-1) {
1069 spin_lock_irq(&md->lock);
1070 ret = __blk_end_request(req, 0, blocks << 9);
1071 spin_unlock_irq(&md->lock);
1074 spin_lock_irq(&md->lock);
1075 ret = __blk_end_request(req, 0, brq->data.bytes_xfered);
1076 spin_unlock_irq(&md->lock);
1080 spin_lock_irq(&md->lock);
1082 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
1083 spin_unlock_irq(&md->lock);
1088 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1091 struct mmc_blk_data *md = mq->data;
1092 struct mmc_card *card = md->queue.card;
1094 mmc_claim_host(card->host);
1095 ret = mmc_blk_part_switch(card, md);
1101 if (req->cmd_flags & REQ_DISCARD) {
1102 if (req->cmd_flags & REQ_SECURE)
1103 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1105 ret = mmc_blk_issue_discard_rq(mq, req);
1106 } else if (req->cmd_flags & REQ_FLUSH) {
1107 ret = mmc_blk_issue_flush(mq, req);
1109 ret = mmc_blk_issue_rw_rq(mq, req);
1113 mmc_release_host(card->host);
1117 static inline int mmc_blk_readonly(struct mmc_card *card)
1119 return mmc_card_readonly(card) ||
1120 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1123 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1124 struct device *parent,
1127 const char *subname)
1129 struct mmc_blk_data *md;
1132 devidx = find_first_zero_bit(dev_use, max_devices);
1133 if (devidx >= max_devices)
1134 return ERR_PTR(-ENOSPC);
1135 __set_bit(devidx, dev_use);
1137 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1144 * !subname implies we are creating main mmc_blk_data that will be
1145 * associated with mmc_card with mmc_set_drvdata. Due to device
1146 * partitions, devidx will not coincide with a per-physical card
1147 * index anymore so we keep track of a name index.
1150 md->name_idx = find_first_zero_bit(name_use, max_devices);
1151 __set_bit(md->name_idx, name_use);
1154 md->name_idx = ((struct mmc_blk_data *)
1155 dev_to_disk(parent)->private_data)->name_idx;
1158 * Set the read-only status based on the supported commands
1159 * and the write protect switch.
1161 md->read_only = mmc_blk_readonly(card);
1163 md->disk = alloc_disk(perdev_minors);
1164 if (md->disk == NULL) {
1169 spin_lock_init(&md->lock);
1170 INIT_LIST_HEAD(&md->part);
1173 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1177 md->queue.issue_fn = mmc_blk_issue_rq;
1178 md->queue.data = md;
1180 md->disk->major = MMC_BLOCK_MAJOR;
1181 md->disk->first_minor = devidx * perdev_minors;
1182 md->disk->fops = &mmc_bdops;
1183 md->disk->private_data = md;
1184 md->disk->queue = md->queue.queue;
1185 md->disk->driverfs_dev = parent;
1186 set_disk_ro(md->disk, md->read_only || default_ro);
1189 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1191 * - be set for removable media with permanent block devices
1192 * - be unset for removable block devices with permanent media
1194 * Since MMC block devices clearly fall under the second
1195 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1196 * should use the block device creation/destruction hotplug
1197 * messages to tell when the card is present.
1200 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1201 "mmcblk%d%s", md->name_idx, subname ? subname : "");
1203 blk_queue_logical_block_size(md->queue.queue, 512);
1204 set_capacity(md->disk, size);
1206 if (mmc_host_cmd23(card->host)) {
1207 if (mmc_card_mmc(card) ||
1208 (mmc_card_sd(card) &&
1209 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1210 md->flags |= MMC_BLK_CMD23;
1213 if (mmc_card_mmc(card) &&
1214 md->flags & MMC_BLK_CMD23 &&
1215 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1216 card->ext_csd.rel_sectors)) {
1217 md->flags |= MMC_BLK_REL_WR;
1218 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1228 return ERR_PTR(ret);
1231 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1234 struct mmc_blk_data *md;
1236 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1238 * The EXT_CSD sector count is in number or 512 byte
1241 size = card->ext_csd.sectors;
1244 * The CSD capacity field is in units of read_blkbits.
1245 * set_capacity takes units of 512 bytes.
1247 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1250 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1254 static int mmc_blk_alloc_part(struct mmc_card *card,
1255 struct mmc_blk_data *md,
1256 unsigned int part_type,
1259 const char *subname)
1262 struct mmc_blk_data *part_md;
1264 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1266 if (IS_ERR(part_md))
1267 return PTR_ERR(part_md);
1268 part_md->part_type = part_type;
1269 list_add(&part_md->part, &md->part);
1271 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1272 cap_str, sizeof(cap_str));
1273 printk(KERN_INFO "%s: %s %s partition %u %s\n",
1274 part_md->disk->disk_name, mmc_card_id(card),
1275 mmc_card_name(card), part_md->part_type, cap_str);
1279 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1283 if (!mmc_card_mmc(card))
1286 if (card->ext_csd.boot_size) {
1287 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1288 card->ext_csd.boot_size >> 9,
1293 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1294 card->ext_csd.boot_size >> 9,
1305 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1309 mmc_claim_host(card->host);
1310 err = mmc_set_blocklen(card, 512);
1311 mmc_release_host(card->host);
1314 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1315 md->disk->disk_name, err);
1322 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1325 if (md->disk->flags & GENHD_FL_UP) {
1326 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1328 /* Stop new requests from getting into the queue */
1329 del_gendisk(md->disk);
1332 /* Then flush out any already in there */
1333 mmc_cleanup_queue(&md->queue);
1338 static void mmc_blk_remove_parts(struct mmc_card *card,
1339 struct mmc_blk_data *md)
1341 struct list_head *pos, *q;
1342 struct mmc_blk_data *part_md;
1344 __clear_bit(md->name_idx, name_use);
1345 list_for_each_safe(pos, q, &md->part) {
1346 part_md = list_entry(pos, struct mmc_blk_data, part);
1348 mmc_blk_remove_req(part_md);
1352 static int mmc_add_disk(struct mmc_blk_data *md)
1357 md->force_ro.show = force_ro_show;
1358 md->force_ro.store = force_ro_store;
1359 sysfs_attr_init(&md->force_ro.attr);
1360 md->force_ro.attr.name = "force_ro";
1361 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1362 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1364 del_gendisk(md->disk);
1369 static const struct mmc_fixup blk_fixups[] =
1371 MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1372 MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1373 MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1374 MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1375 MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1378 * Some MMC cards experience performance degradation with CMD23
1379 * instead of CMD12-bounded multiblock transfers. For now we'll
1380 * black list what's bad...
1381 * - Certain Toshiba cards.
1383 * N.B. This doesn't affect SD cards.
1385 MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1386 MMC_QUIRK_BLK_NO_CMD23),
1387 MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1388 MMC_QUIRK_BLK_NO_CMD23),
1389 MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1390 MMC_QUIRK_BLK_NO_CMD23),
1394 static int mmc_blk_probe(struct mmc_card *card)
1396 struct mmc_blk_data *md, *part_md;
1401 * Check that the card supports the command class(es) we need.
1403 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1406 md = mmc_blk_alloc(card);
1410 err = mmc_blk_set_blksize(md, card);
1414 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1415 cap_str, sizeof(cap_str));
1416 printk(KERN_INFO "%s: %s %s %s %s\n",
1417 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1418 cap_str, md->read_only ? "(ro)" : "");
1420 if (mmc_blk_alloc_parts(card, md))
1423 mmc_set_drvdata(card, md);
1424 mmc_fixup_device(card, blk_fixups);
1426 if (mmc_add_disk(md))
1429 list_for_each_entry(part_md, &md->part, part) {
1430 if (mmc_add_disk(part_md))
1436 mmc_blk_remove_parts(card, md);
1437 mmc_blk_remove_req(md);
1441 static void mmc_blk_remove(struct mmc_card *card)
1443 struct mmc_blk_data *md = mmc_get_drvdata(card);
1445 mmc_blk_remove_parts(card, md);
1446 mmc_claim_host(card->host);
1447 mmc_blk_part_switch(card, md);
1448 mmc_release_host(card->host);
1449 mmc_blk_remove_req(md);
1450 mmc_set_drvdata(card, NULL);
1454 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1456 struct mmc_blk_data *part_md;
1457 struct mmc_blk_data *md = mmc_get_drvdata(card);
1460 mmc_queue_suspend(&md->queue);
1461 list_for_each_entry(part_md, &md->part, part) {
1462 mmc_queue_suspend(&part_md->queue);
1468 static int mmc_blk_resume(struct mmc_card *card)
1470 struct mmc_blk_data *part_md;
1471 struct mmc_blk_data *md = mmc_get_drvdata(card);
1474 mmc_blk_set_blksize(md, card);
1477 * Resume involves the card going into idle state,
1478 * so current partition is always the main one.
1480 md->part_curr = md->part_type;
1481 mmc_queue_resume(&md->queue);
1482 list_for_each_entry(part_md, &md->part, part) {
1483 mmc_queue_resume(&part_md->queue);
1489 #define mmc_blk_suspend NULL
1490 #define mmc_blk_resume NULL
1493 static struct mmc_driver mmc_driver = {
1497 .probe = mmc_blk_probe,
1498 .remove = mmc_blk_remove,
1499 .suspend = mmc_blk_suspend,
1500 .resume = mmc_blk_resume,
1503 static int __init mmc_blk_init(void)
1507 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1508 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1510 max_devices = 256 / perdev_minors;
1512 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1516 res = mmc_register_driver(&mmc_driver);
1522 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1527 static void __exit mmc_blk_exit(void)
1529 mmc_unregister_driver(&mmc_driver);
1530 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1533 module_init(mmc_blk_init);
1534 module_exit(mmc_blk_exit);
1536 MODULE_LICENSE("GPL");
1537 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");