Merge branch 'for-rmk/virt/psci' into for-arm-soc/virt/mach
[linux/fpc-iii.git] / drivers / mmc / card / block.c
blob21056b9ef0a0f97bf9f1e71de1e78a3253cd5284
1 /*
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
18 * 28 May 2002
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/fs.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/uaccess.h>
46 #include "queue.h"
48 MODULE_ALIAS("mmc:block");
49 #ifdef MODULE_PARAM_PREFIX
50 #undef MODULE_PARAM_PREFIX
51 #endif
52 #define MODULE_PARAM_PREFIX "mmcblk."
54 #define INAND_CMD38_ARG_EXT_CSD 113
55 #define INAND_CMD38_ARG_ERASE 0x00
56 #define INAND_CMD38_ARG_TRIM 0x01
57 #define INAND_CMD38_ARG_SECERASE 0x80
58 #define INAND_CMD38_ARG_SECTRIM1 0x81
59 #define INAND_CMD38_ARG_SECTRIM2 0x88
60 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
62 static DEFINE_MUTEX(block_mutex);
65 * The defaults come from config options but can be overriden by module
66 * or bootarg options.
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.
83 struct mmc_blk_data {
84 spinlock_t lock;
85 struct gendisk *disk;
86 struct mmc_queue queue;
87 struct list_head part;
89 unsigned int flags;
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 */
93 unsigned int usage;
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;
110 struct device_attribute power_ro_lock;
111 int area_type;
114 static DEFINE_MUTEX(open_lock);
116 enum mmc_blk_status {
117 MMC_BLK_SUCCESS = 0,
118 MMC_BLK_PARTIAL,
119 MMC_BLK_CMD_ERR,
120 MMC_BLK_RETRY,
121 MMC_BLK_ABORT,
122 MMC_BLK_DATA_ERR,
123 MMC_BLK_ECC_ERR,
124 MMC_BLK_NOMEDIUM,
127 module_param(perdev_minors, int, 0444);
128 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
130 static inline int mmc_blk_part_switch(struct mmc_card *card,
131 struct mmc_blk_data *md);
132 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
134 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
136 struct mmc_blk_data *md;
138 mutex_lock(&open_lock);
139 md = disk->private_data;
140 if (md && md->usage == 0)
141 md = NULL;
142 if (md)
143 md->usage++;
144 mutex_unlock(&open_lock);
146 return md;
149 static inline int mmc_get_devidx(struct gendisk *disk)
151 int devmaj = MAJOR(disk_devt(disk));
152 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
154 if (!devmaj)
155 devidx = disk->first_minor / perdev_minors;
156 return devidx;
159 static void mmc_blk_put(struct mmc_blk_data *md)
161 mutex_lock(&open_lock);
162 md->usage--;
163 if (md->usage == 0) {
164 int devidx = mmc_get_devidx(md->disk);
165 blk_cleanup_queue(md->queue.queue);
167 __clear_bit(devidx, dev_use);
169 put_disk(md->disk);
170 kfree(md);
172 mutex_unlock(&open_lock);
175 static ssize_t power_ro_lock_show(struct device *dev,
176 struct device_attribute *attr, char *buf)
178 int ret;
179 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
180 struct mmc_card *card = md->queue.card;
181 int locked = 0;
183 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
184 locked = 2;
185 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
186 locked = 1;
188 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
190 return ret;
193 static ssize_t power_ro_lock_store(struct device *dev,
194 struct device_attribute *attr, const char *buf, size_t count)
196 int ret;
197 struct mmc_blk_data *md, *part_md;
198 struct mmc_card *card;
199 unsigned long set;
201 if (kstrtoul(buf, 0, &set))
202 return -EINVAL;
204 if (set != 1)
205 return count;
207 md = mmc_blk_get(dev_to_disk(dev));
208 card = md->queue.card;
210 mmc_claim_host(card->host);
212 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
213 card->ext_csd.boot_ro_lock |
214 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
215 card->ext_csd.part_time);
216 if (ret)
217 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
218 else
219 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
221 mmc_release_host(card->host);
223 if (!ret) {
224 pr_info("%s: Locking boot partition ro until next power on\n",
225 md->disk->disk_name);
226 set_disk_ro(md->disk, 1);
228 list_for_each_entry(part_md, &md->part, part)
229 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
230 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
231 set_disk_ro(part_md->disk, 1);
235 mmc_blk_put(md);
236 return count;
239 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
240 char *buf)
242 int ret;
243 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
245 ret = snprintf(buf, PAGE_SIZE, "%d",
246 get_disk_ro(dev_to_disk(dev)) ^
247 md->read_only);
248 mmc_blk_put(md);
249 return ret;
252 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
253 const char *buf, size_t count)
255 int ret;
256 char *end;
257 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
258 unsigned long set = simple_strtoul(buf, &end, 0);
259 if (end == buf) {
260 ret = -EINVAL;
261 goto out;
264 set_disk_ro(dev_to_disk(dev), set || md->read_only);
265 ret = count;
266 out:
267 mmc_blk_put(md);
268 return ret;
271 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
273 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
274 int ret = -ENXIO;
276 mutex_lock(&block_mutex);
277 if (md) {
278 if (md->usage == 2)
279 check_disk_change(bdev);
280 ret = 0;
282 if ((mode & FMODE_WRITE) && md->read_only) {
283 mmc_blk_put(md);
284 ret = -EROFS;
287 mutex_unlock(&block_mutex);
289 return ret;
292 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
294 struct mmc_blk_data *md = disk->private_data;
296 mutex_lock(&block_mutex);
297 mmc_blk_put(md);
298 mutex_unlock(&block_mutex);
299 return 0;
302 static int
303 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
305 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
306 geo->heads = 4;
307 geo->sectors = 16;
308 return 0;
311 struct mmc_blk_ioc_data {
312 struct mmc_ioc_cmd ic;
313 unsigned char *buf;
314 u64 buf_bytes;
317 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
318 struct mmc_ioc_cmd __user *user)
320 struct mmc_blk_ioc_data *idata;
321 int err;
323 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
324 if (!idata) {
325 err = -ENOMEM;
326 goto out;
329 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
330 err = -EFAULT;
331 goto idata_err;
334 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
335 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
336 err = -EOVERFLOW;
337 goto idata_err;
340 if (!idata->buf_bytes)
341 return idata;
343 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
344 if (!idata->buf) {
345 err = -ENOMEM;
346 goto idata_err;
349 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
350 idata->ic.data_ptr, idata->buf_bytes)) {
351 err = -EFAULT;
352 goto copy_err;
355 return idata;
357 copy_err:
358 kfree(idata->buf);
359 idata_err:
360 kfree(idata);
361 out:
362 return ERR_PTR(err);
365 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
366 u32 retries_max)
368 int err;
369 u32 retry_count = 0;
371 if (!status || !retries_max)
372 return -EINVAL;
374 do {
375 err = get_card_status(card, status, 5);
376 if (err)
377 break;
379 if (!R1_STATUS(*status) &&
380 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
381 break; /* RPMB programming operation complete */
384 * Rechedule to give the MMC device a chance to continue
385 * processing the previous command without being polled too
386 * frequently.
388 usleep_range(1000, 5000);
389 } while (++retry_count < retries_max);
391 if (retry_count == retries_max)
392 err = -EPERM;
394 return err;
397 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
398 struct mmc_ioc_cmd __user *ic_ptr)
400 struct mmc_blk_ioc_data *idata;
401 struct mmc_blk_data *md;
402 struct mmc_card *card;
403 struct mmc_command cmd = {0};
404 struct mmc_data data = {0};
405 struct mmc_request mrq = {NULL};
406 struct scatterlist sg;
407 int err;
408 int is_rpmb = false;
409 u32 status = 0;
412 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
413 * whole block device, not on a partition. This prevents overspray
414 * between sibling partitions.
416 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
417 return -EPERM;
419 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
420 if (IS_ERR(idata))
421 return PTR_ERR(idata);
423 md = mmc_blk_get(bdev->bd_disk);
424 if (!md) {
425 err = -EINVAL;
426 goto cmd_err;
429 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
430 is_rpmb = true;
432 card = md->queue.card;
433 if (IS_ERR(card)) {
434 err = PTR_ERR(card);
435 goto cmd_done;
438 cmd.opcode = idata->ic.opcode;
439 cmd.arg = idata->ic.arg;
440 cmd.flags = idata->ic.flags;
442 if (idata->buf_bytes) {
443 data.sg = &sg;
444 data.sg_len = 1;
445 data.blksz = idata->ic.blksz;
446 data.blocks = idata->ic.blocks;
448 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
450 if (idata->ic.write_flag)
451 data.flags = MMC_DATA_WRITE;
452 else
453 data.flags = MMC_DATA_READ;
455 /* data.flags must already be set before doing this. */
456 mmc_set_data_timeout(&data, card);
458 /* Allow overriding the timeout_ns for empirical tuning. */
459 if (idata->ic.data_timeout_ns)
460 data.timeout_ns = idata->ic.data_timeout_ns;
462 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
464 * Pretend this is a data transfer and rely on the
465 * host driver to compute timeout. When all host
466 * drivers support cmd.cmd_timeout for R1B, this
467 * can be changed to:
469 * mrq.data = NULL;
470 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
472 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
475 mrq.data = &data;
478 mrq.cmd = &cmd;
480 mmc_claim_host(card->host);
482 err = mmc_blk_part_switch(card, md);
483 if (err)
484 goto cmd_rel_host;
486 if (idata->ic.is_acmd) {
487 err = mmc_app_cmd(card->host, card);
488 if (err)
489 goto cmd_rel_host;
492 if (is_rpmb) {
493 err = mmc_set_blockcount(card, data.blocks,
494 idata->ic.write_flag & (1 << 31));
495 if (err)
496 goto cmd_rel_host;
499 mmc_wait_for_req(card->host, &mrq);
501 if (cmd.error) {
502 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
503 __func__, cmd.error);
504 err = cmd.error;
505 goto cmd_rel_host;
507 if (data.error) {
508 dev_err(mmc_dev(card->host), "%s: data error %d\n",
509 __func__, data.error);
510 err = data.error;
511 goto cmd_rel_host;
515 * According to the SD specs, some commands require a delay after
516 * issuing the command.
518 if (idata->ic.postsleep_min_us)
519 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
521 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
522 err = -EFAULT;
523 goto cmd_rel_host;
526 if (!idata->ic.write_flag) {
527 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
528 idata->buf, idata->buf_bytes)) {
529 err = -EFAULT;
530 goto cmd_rel_host;
534 if (is_rpmb) {
536 * Ensure RPMB command has completed by polling CMD13
537 * "Send Status".
539 err = ioctl_rpmb_card_status_poll(card, &status, 5);
540 if (err)
541 dev_err(mmc_dev(card->host),
542 "%s: Card Status=0x%08X, error %d\n",
543 __func__, status, err);
546 cmd_rel_host:
547 mmc_release_host(card->host);
549 cmd_done:
550 mmc_blk_put(md);
551 cmd_err:
552 kfree(idata->buf);
553 kfree(idata);
554 return err;
557 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
558 unsigned int cmd, unsigned long arg)
560 int ret = -EINVAL;
561 if (cmd == MMC_IOC_CMD)
562 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
563 return ret;
566 #ifdef CONFIG_COMPAT
567 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
568 unsigned int cmd, unsigned long arg)
570 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
572 #endif
574 static const struct block_device_operations mmc_bdops = {
575 .open = mmc_blk_open,
576 .release = mmc_blk_release,
577 .getgeo = mmc_blk_getgeo,
578 .owner = THIS_MODULE,
579 .ioctl = mmc_blk_ioctl,
580 #ifdef CONFIG_COMPAT
581 .compat_ioctl = mmc_blk_compat_ioctl,
582 #endif
585 static inline int mmc_blk_part_switch(struct mmc_card *card,
586 struct mmc_blk_data *md)
588 int ret;
589 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
591 if (main_md->part_curr == md->part_type)
592 return 0;
594 if (mmc_card_mmc(card)) {
595 u8 part_config = card->ext_csd.part_config;
597 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
598 part_config |= md->part_type;
600 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
601 EXT_CSD_PART_CONFIG, part_config,
602 card->ext_csd.part_time);
603 if (ret)
604 return ret;
606 card->ext_csd.part_config = part_config;
609 main_md->part_curr = md->part_type;
610 return 0;
613 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
615 int err;
616 u32 result;
617 __be32 *blocks;
619 struct mmc_request mrq = {NULL};
620 struct mmc_command cmd = {0};
621 struct mmc_data data = {0};
623 struct scatterlist sg;
625 cmd.opcode = MMC_APP_CMD;
626 cmd.arg = card->rca << 16;
627 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
629 err = mmc_wait_for_cmd(card->host, &cmd, 0);
630 if (err)
631 return (u32)-1;
632 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
633 return (u32)-1;
635 memset(&cmd, 0, sizeof(struct mmc_command));
637 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
638 cmd.arg = 0;
639 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
641 data.blksz = 4;
642 data.blocks = 1;
643 data.flags = MMC_DATA_READ;
644 data.sg = &sg;
645 data.sg_len = 1;
646 mmc_set_data_timeout(&data, card);
648 mrq.cmd = &cmd;
649 mrq.data = &data;
651 blocks = kmalloc(4, GFP_KERNEL);
652 if (!blocks)
653 return (u32)-1;
655 sg_init_one(&sg, blocks, 4);
657 mmc_wait_for_req(card->host, &mrq);
659 result = ntohl(*blocks);
660 kfree(blocks);
662 if (cmd.error || data.error)
663 result = (u32)-1;
665 return result;
668 static int send_stop(struct mmc_card *card, u32 *status)
670 struct mmc_command cmd = {0};
671 int err;
673 cmd.opcode = MMC_STOP_TRANSMISSION;
674 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
675 err = mmc_wait_for_cmd(card->host, &cmd, 5);
676 if (err == 0)
677 *status = cmd.resp[0];
678 return err;
681 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
683 struct mmc_command cmd = {0};
684 int err;
686 cmd.opcode = MMC_SEND_STATUS;
687 if (!mmc_host_is_spi(card->host))
688 cmd.arg = card->rca << 16;
689 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
690 err = mmc_wait_for_cmd(card->host, &cmd, retries);
691 if (err == 0)
692 *status = cmd.resp[0];
693 return err;
696 #define ERR_NOMEDIUM 3
697 #define ERR_RETRY 2
698 #define ERR_ABORT 1
699 #define ERR_CONTINUE 0
701 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
702 bool status_valid, u32 status)
704 switch (error) {
705 case -EILSEQ:
706 /* response crc error, retry the r/w cmd */
707 pr_err("%s: %s sending %s command, card status %#x\n",
708 req->rq_disk->disk_name, "response CRC error",
709 name, status);
710 return ERR_RETRY;
712 case -ETIMEDOUT:
713 pr_err("%s: %s sending %s command, card status %#x\n",
714 req->rq_disk->disk_name, "timed out", name, status);
716 /* If the status cmd initially failed, retry the r/w cmd */
717 if (!status_valid)
718 return ERR_RETRY;
721 * If it was a r/w cmd crc error, or illegal command
722 * (eg, issued in wrong state) then retry - we should
723 * have corrected the state problem above.
725 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
726 return ERR_RETRY;
728 /* Otherwise abort the command */
729 return ERR_ABORT;
731 default:
732 /* We don't understand the error code the driver gave us */
733 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
734 req->rq_disk->disk_name, error, status);
735 return ERR_ABORT;
740 * Initial r/w and stop cmd error recovery.
741 * We don't know whether the card received the r/w cmd or not, so try to
742 * restore things back to a sane state. Essentially, we do this as follows:
743 * - Obtain card status. If the first attempt to obtain card status fails,
744 * the status word will reflect the failed status cmd, not the failed
745 * r/w cmd. If we fail to obtain card status, it suggests we can no
746 * longer communicate with the card.
747 * - Check the card state. If the card received the cmd but there was a
748 * transient problem with the response, it might still be in a data transfer
749 * mode. Try to send it a stop command. If this fails, we can't recover.
750 * - If the r/w cmd failed due to a response CRC error, it was probably
751 * transient, so retry the cmd.
752 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
753 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
754 * illegal cmd, retry.
755 * Otherwise we don't understand what happened, so abort.
757 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
758 struct mmc_blk_request *brq, int *ecc_err)
760 bool prev_cmd_status_valid = true;
761 u32 status, stop_status = 0;
762 int err, retry;
764 if (mmc_card_removed(card))
765 return ERR_NOMEDIUM;
768 * Try to get card status which indicates both the card state
769 * and why there was no response. If the first attempt fails,
770 * we can't be sure the returned status is for the r/w command.
772 for (retry = 2; retry >= 0; retry--) {
773 err = get_card_status(card, &status, 0);
774 if (!err)
775 break;
777 prev_cmd_status_valid = false;
778 pr_err("%s: error %d sending status command, %sing\n",
779 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
782 /* We couldn't get a response from the card. Give up. */
783 if (err) {
784 /* Check if the card is removed */
785 if (mmc_detect_card_removed(card->host))
786 return ERR_NOMEDIUM;
787 return ERR_ABORT;
790 /* Flag ECC errors */
791 if ((status & R1_CARD_ECC_FAILED) ||
792 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
793 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
794 *ecc_err = 1;
797 * Check the current card state. If it is in some data transfer
798 * mode, tell it to stop (and hopefully transition back to TRAN.)
800 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
801 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
802 err = send_stop(card, &stop_status);
803 if (err)
804 pr_err("%s: error %d sending stop command\n",
805 req->rq_disk->disk_name, err);
808 * If the stop cmd also timed out, the card is probably
809 * not present, so abort. Other errors are bad news too.
811 if (err)
812 return ERR_ABORT;
813 if (stop_status & R1_CARD_ECC_FAILED)
814 *ecc_err = 1;
817 /* Check for set block count errors */
818 if (brq->sbc.error)
819 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
820 prev_cmd_status_valid, status);
822 /* Check for r/w command errors */
823 if (brq->cmd.error)
824 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
825 prev_cmd_status_valid, status);
827 /* Data errors */
828 if (!brq->stop.error)
829 return ERR_CONTINUE;
831 /* Now for stop errors. These aren't fatal to the transfer. */
832 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
833 req->rq_disk->disk_name, brq->stop.error,
834 brq->cmd.resp[0], status);
837 * Subsitute in our own stop status as this will give the error
838 * state which happened during the execution of the r/w command.
840 if (stop_status) {
841 brq->stop.resp[0] = stop_status;
842 brq->stop.error = 0;
844 return ERR_CONTINUE;
847 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
848 int type)
850 int err;
852 if (md->reset_done & type)
853 return -EEXIST;
855 md->reset_done |= type;
856 err = mmc_hw_reset(host);
857 /* Ensure we switch back to the correct partition */
858 if (err != -EOPNOTSUPP) {
859 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
860 int part_err;
862 main_md->part_curr = main_md->part_type;
863 part_err = mmc_blk_part_switch(host->card, md);
864 if (part_err) {
866 * We have failed to get back into the correct
867 * partition, so we need to abort the whole request.
869 return -ENODEV;
872 return err;
875 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
877 md->reset_done &= ~type;
880 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
882 struct mmc_blk_data *md = mq->data;
883 struct mmc_card *card = md->queue.card;
884 unsigned int from, nr, arg;
885 int err = 0, type = MMC_BLK_DISCARD;
887 if (!mmc_can_erase(card)) {
888 err = -EOPNOTSUPP;
889 goto out;
892 from = blk_rq_pos(req);
893 nr = blk_rq_sectors(req);
895 if (mmc_can_discard(card))
896 arg = MMC_DISCARD_ARG;
897 else if (mmc_can_trim(card))
898 arg = MMC_TRIM_ARG;
899 else
900 arg = MMC_ERASE_ARG;
901 retry:
902 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
903 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
904 INAND_CMD38_ARG_EXT_CSD,
905 arg == MMC_TRIM_ARG ?
906 INAND_CMD38_ARG_TRIM :
907 INAND_CMD38_ARG_ERASE,
909 if (err)
910 goto out;
912 err = mmc_erase(card, from, nr, arg);
913 out:
914 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
915 goto retry;
916 if (!err)
917 mmc_blk_reset_success(md, type);
918 blk_end_request(req, err, blk_rq_bytes(req));
920 return err ? 0 : 1;
923 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
924 struct request *req)
926 struct mmc_blk_data *md = mq->data;
927 struct mmc_card *card = md->queue.card;
928 unsigned int from, nr, arg, trim_arg, erase_arg;
929 int err = 0, type = MMC_BLK_SECDISCARD;
931 if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
932 err = -EOPNOTSUPP;
933 goto out;
936 from = blk_rq_pos(req);
937 nr = blk_rq_sectors(req);
939 /* The sanitize operation is supported at v4.5 only */
940 if (mmc_can_sanitize(card)) {
941 erase_arg = MMC_ERASE_ARG;
942 trim_arg = MMC_TRIM_ARG;
943 } else {
944 erase_arg = MMC_SECURE_ERASE_ARG;
945 trim_arg = MMC_SECURE_TRIM1_ARG;
948 if (mmc_erase_group_aligned(card, from, nr))
949 arg = erase_arg;
950 else if (mmc_can_trim(card))
951 arg = trim_arg;
952 else {
953 err = -EINVAL;
954 goto out;
956 retry:
957 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
958 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
959 INAND_CMD38_ARG_EXT_CSD,
960 arg == MMC_SECURE_TRIM1_ARG ?
961 INAND_CMD38_ARG_SECTRIM1 :
962 INAND_CMD38_ARG_SECERASE,
964 if (err)
965 goto out_retry;
968 err = mmc_erase(card, from, nr, arg);
969 if (err == -EIO)
970 goto out_retry;
971 if (err)
972 goto out;
974 if (arg == MMC_SECURE_TRIM1_ARG) {
975 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
976 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
977 INAND_CMD38_ARG_EXT_CSD,
978 INAND_CMD38_ARG_SECTRIM2,
980 if (err)
981 goto out_retry;
984 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
985 if (err == -EIO)
986 goto out_retry;
987 if (err)
988 goto out;
991 if (mmc_can_sanitize(card))
992 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
993 EXT_CSD_SANITIZE_START, 1, 0);
994 out_retry:
995 if (err && !mmc_blk_reset(md, card->host, type))
996 goto retry;
997 if (!err)
998 mmc_blk_reset_success(md, type);
999 out:
1000 blk_end_request(req, err, blk_rq_bytes(req));
1002 return err ? 0 : 1;
1005 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1007 struct mmc_blk_data *md = mq->data;
1008 struct mmc_card *card = md->queue.card;
1009 int ret = 0;
1011 ret = mmc_flush_cache(card);
1012 if (ret)
1013 ret = -EIO;
1015 blk_end_request_all(req, ret);
1017 return ret ? 0 : 1;
1021 * Reformat current write as a reliable write, supporting
1022 * both legacy and the enhanced reliable write MMC cards.
1023 * In each transfer we'll handle only as much as a single
1024 * reliable write can handle, thus finish the request in
1025 * partial completions.
1027 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1028 struct mmc_card *card,
1029 struct request *req)
1031 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1032 /* Legacy mode imposes restrictions on transfers. */
1033 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1034 brq->data.blocks = 1;
1036 if (brq->data.blocks > card->ext_csd.rel_sectors)
1037 brq->data.blocks = card->ext_csd.rel_sectors;
1038 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1039 brq->data.blocks = 1;
1043 #define CMD_ERRORS \
1044 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1045 R1_ADDRESS_ERROR | /* Misaligned address */ \
1046 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1047 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1048 R1_CC_ERROR | /* Card controller error */ \
1049 R1_ERROR) /* General/unknown error */
1051 static int mmc_blk_err_check(struct mmc_card *card,
1052 struct mmc_async_req *areq)
1054 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1055 mmc_active);
1056 struct mmc_blk_request *brq = &mq_mrq->brq;
1057 struct request *req = mq_mrq->req;
1058 int ecc_err = 0;
1061 * sbc.error indicates a problem with the set block count
1062 * command. No data will have been transferred.
1064 * cmd.error indicates a problem with the r/w command. No
1065 * data will have been transferred.
1067 * stop.error indicates a problem with the stop command. Data
1068 * may have been transferred, or may still be transferring.
1070 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1071 brq->data.error) {
1072 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
1073 case ERR_RETRY:
1074 return MMC_BLK_RETRY;
1075 case ERR_ABORT:
1076 return MMC_BLK_ABORT;
1077 case ERR_NOMEDIUM:
1078 return MMC_BLK_NOMEDIUM;
1079 case ERR_CONTINUE:
1080 break;
1085 * Check for errors relating to the execution of the
1086 * initial command - such as address errors. No data
1087 * has been transferred.
1089 if (brq->cmd.resp[0] & CMD_ERRORS) {
1090 pr_err("%s: r/w command failed, status = %#x\n",
1091 req->rq_disk->disk_name, brq->cmd.resp[0]);
1092 return MMC_BLK_ABORT;
1096 * Everything else is either success, or a data error of some
1097 * kind. If it was a write, we may have transitioned to
1098 * program mode, which we have to wait for it to complete.
1100 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1101 u32 status;
1102 unsigned long timeout;
1104 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1105 do {
1106 int err = get_card_status(card, &status, 5);
1107 if (err) {
1108 pr_err("%s: error %d requesting status\n",
1109 req->rq_disk->disk_name, err);
1110 return MMC_BLK_CMD_ERR;
1113 /* Timeout if the device never becomes ready for data
1114 * and never leaves the program state.
1116 if (time_after(jiffies, timeout)) {
1117 pr_err("%s: Card stuck in programming state!"\
1118 " %s %s\n", mmc_hostname(card->host),
1119 req->rq_disk->disk_name, __func__);
1121 return MMC_BLK_CMD_ERR;
1124 * Some cards mishandle the status bits,
1125 * so make sure to check both the busy
1126 * indication and the card state.
1128 } while (!(status & R1_READY_FOR_DATA) ||
1129 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1132 if (brq->data.error) {
1133 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1134 req->rq_disk->disk_name, brq->data.error,
1135 (unsigned)blk_rq_pos(req),
1136 (unsigned)blk_rq_sectors(req),
1137 brq->cmd.resp[0], brq->stop.resp[0]);
1139 if (rq_data_dir(req) == READ) {
1140 if (ecc_err)
1141 return MMC_BLK_ECC_ERR;
1142 return MMC_BLK_DATA_ERR;
1143 } else {
1144 return MMC_BLK_CMD_ERR;
1148 if (!brq->data.bytes_xfered)
1149 return MMC_BLK_RETRY;
1151 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1152 return MMC_BLK_PARTIAL;
1154 return MMC_BLK_SUCCESS;
1157 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1158 struct mmc_card *card,
1159 int disable_multi,
1160 struct mmc_queue *mq)
1162 u32 readcmd, writecmd;
1163 struct mmc_blk_request *brq = &mqrq->brq;
1164 struct request *req = mqrq->req;
1165 struct mmc_blk_data *md = mq->data;
1166 bool do_data_tag;
1169 * Reliable writes are used to implement Forced Unit Access and
1170 * REQ_META accesses, and are supported only on MMCs.
1172 * XXX: this really needs a good explanation of why REQ_META
1173 * is treated special.
1175 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1176 (req->cmd_flags & REQ_META)) &&
1177 (rq_data_dir(req) == WRITE) &&
1178 (md->flags & MMC_BLK_REL_WR);
1180 memset(brq, 0, sizeof(struct mmc_blk_request));
1181 brq->mrq.cmd = &brq->cmd;
1182 brq->mrq.data = &brq->data;
1184 brq->cmd.arg = blk_rq_pos(req);
1185 if (!mmc_card_blockaddr(card))
1186 brq->cmd.arg <<= 9;
1187 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1188 brq->data.blksz = 512;
1189 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1190 brq->stop.arg = 0;
1191 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1192 brq->data.blocks = blk_rq_sectors(req);
1195 * The block layer doesn't support all sector count
1196 * restrictions, so we need to be prepared for too big
1197 * requests.
1199 if (brq->data.blocks > card->host->max_blk_count)
1200 brq->data.blocks = card->host->max_blk_count;
1202 if (brq->data.blocks > 1) {
1204 * After a read error, we redo the request one sector
1205 * at a time in order to accurately determine which
1206 * sectors can be read successfully.
1208 if (disable_multi)
1209 brq->data.blocks = 1;
1211 /* Some controllers can't do multiblock reads due to hw bugs */
1212 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1213 rq_data_dir(req) == READ)
1214 brq->data.blocks = 1;
1217 if (brq->data.blocks > 1 || do_rel_wr) {
1218 /* SPI multiblock writes terminate using a special
1219 * token, not a STOP_TRANSMISSION request.
1221 if (!mmc_host_is_spi(card->host) ||
1222 rq_data_dir(req) == READ)
1223 brq->mrq.stop = &brq->stop;
1224 readcmd = MMC_READ_MULTIPLE_BLOCK;
1225 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1226 } else {
1227 brq->mrq.stop = NULL;
1228 readcmd = MMC_READ_SINGLE_BLOCK;
1229 writecmd = MMC_WRITE_BLOCK;
1231 if (rq_data_dir(req) == READ) {
1232 brq->cmd.opcode = readcmd;
1233 brq->data.flags |= MMC_DATA_READ;
1234 } else {
1235 brq->cmd.opcode = writecmd;
1236 brq->data.flags |= MMC_DATA_WRITE;
1239 if (do_rel_wr)
1240 mmc_apply_rel_rw(brq, card, req);
1243 * Data tag is used only during writing meta data to speed
1244 * up write and any subsequent read of this meta data
1246 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1247 (req->cmd_flags & REQ_META) &&
1248 (rq_data_dir(req) == WRITE) &&
1249 ((brq->data.blocks * brq->data.blksz) >=
1250 card->ext_csd.data_tag_unit_size);
1253 * Pre-defined multi-block transfers are preferable to
1254 * open ended-ones (and necessary for reliable writes).
1255 * However, it is not sufficient to just send CMD23,
1256 * and avoid the final CMD12, as on an error condition
1257 * CMD12 (stop) needs to be sent anyway. This, coupled
1258 * with Auto-CMD23 enhancements provided by some
1259 * hosts, means that the complexity of dealing
1260 * with this is best left to the host. If CMD23 is
1261 * supported by card and host, we'll fill sbc in and let
1262 * the host deal with handling it correctly. This means
1263 * that for hosts that don't expose MMC_CAP_CMD23, no
1264 * change of behavior will be observed.
1266 * N.B: Some MMC cards experience perf degradation.
1267 * We'll avoid using CMD23-bounded multiblock writes for
1268 * these, while retaining features like reliable writes.
1270 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1271 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1272 do_data_tag)) {
1273 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1274 brq->sbc.arg = brq->data.blocks |
1275 (do_rel_wr ? (1 << 31) : 0) |
1276 (do_data_tag ? (1 << 29) : 0);
1277 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1278 brq->mrq.sbc = &brq->sbc;
1281 mmc_set_data_timeout(&brq->data, card);
1283 brq->data.sg = mqrq->sg;
1284 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1287 * Adjust the sg list so it is the same size as the
1288 * request.
1290 if (brq->data.blocks != blk_rq_sectors(req)) {
1291 int i, data_size = brq->data.blocks << 9;
1292 struct scatterlist *sg;
1294 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1295 data_size -= sg->length;
1296 if (data_size <= 0) {
1297 sg->length += data_size;
1298 i++;
1299 break;
1302 brq->data.sg_len = i;
1305 mqrq->mmc_active.mrq = &brq->mrq;
1306 mqrq->mmc_active.err_check = mmc_blk_err_check;
1308 mmc_queue_bounce_pre(mqrq);
1311 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1312 struct mmc_blk_request *brq, struct request *req,
1313 int ret)
1316 * If this is an SD card and we're writing, we can first
1317 * mark the known good sectors as ok.
1319 * If the card is not SD, we can still ok written sectors
1320 * as reported by the controller (which might be less than
1321 * the real number of written sectors, but never more).
1323 if (mmc_card_sd(card)) {
1324 u32 blocks;
1326 blocks = mmc_sd_num_wr_blocks(card);
1327 if (blocks != (u32)-1) {
1328 ret = blk_end_request(req, 0, blocks << 9);
1330 } else {
1331 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1333 return ret;
1336 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1338 struct mmc_blk_data *md = mq->data;
1339 struct mmc_card *card = md->queue.card;
1340 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1341 int ret = 1, disable_multi = 0, retry = 0, type;
1342 enum mmc_blk_status status;
1343 struct mmc_queue_req *mq_rq;
1344 struct request *req = rqc;
1345 struct mmc_async_req *areq;
1347 if (!rqc && !mq->mqrq_prev->req)
1348 return 0;
1350 do {
1351 if (rqc) {
1353 * When 4KB native sector is enabled, only 8 blocks
1354 * multiple read or write is allowed
1356 if ((brq->data.blocks & 0x07) &&
1357 (card->ext_csd.data_sector_size == 4096)) {
1358 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1359 req->rq_disk->disk_name);
1360 goto cmd_abort;
1362 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1363 areq = &mq->mqrq_cur->mmc_active;
1364 } else
1365 areq = NULL;
1366 areq = mmc_start_req(card->host, areq, (int *) &status);
1367 if (!areq)
1368 return 0;
1370 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1371 brq = &mq_rq->brq;
1372 req = mq_rq->req;
1373 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1374 mmc_queue_bounce_post(mq_rq);
1376 switch (status) {
1377 case MMC_BLK_SUCCESS:
1378 case MMC_BLK_PARTIAL:
1380 * A block was successfully transferred.
1382 mmc_blk_reset_success(md, type);
1383 ret = blk_end_request(req, 0,
1384 brq->data.bytes_xfered);
1386 * If the blk_end_request function returns non-zero even
1387 * though all data has been transferred and no errors
1388 * were returned by the host controller, it's a bug.
1390 if (status == MMC_BLK_SUCCESS && ret) {
1391 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1392 __func__, blk_rq_bytes(req),
1393 brq->data.bytes_xfered);
1394 rqc = NULL;
1395 goto cmd_abort;
1397 break;
1398 case MMC_BLK_CMD_ERR:
1399 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1400 if (!mmc_blk_reset(md, card->host, type))
1401 break;
1402 goto cmd_abort;
1403 case MMC_BLK_RETRY:
1404 if (retry++ < 5)
1405 break;
1406 /* Fall through */
1407 case MMC_BLK_ABORT:
1408 if (!mmc_blk_reset(md, card->host, type))
1409 break;
1410 goto cmd_abort;
1411 case MMC_BLK_DATA_ERR: {
1412 int err;
1414 err = mmc_blk_reset(md, card->host, type);
1415 if (!err)
1416 break;
1417 if (err == -ENODEV)
1418 goto cmd_abort;
1419 /* Fall through */
1421 case MMC_BLK_ECC_ERR:
1422 if (brq->data.blocks > 1) {
1423 /* Redo read one sector at a time */
1424 pr_warning("%s: retrying using single block read\n",
1425 req->rq_disk->disk_name);
1426 disable_multi = 1;
1427 break;
1430 * After an error, we redo I/O one sector at a
1431 * time, so we only reach here after trying to
1432 * read a single sector.
1434 ret = blk_end_request(req, -EIO,
1435 brq->data.blksz);
1436 if (!ret)
1437 goto start_new_req;
1438 break;
1439 case MMC_BLK_NOMEDIUM:
1440 goto cmd_abort;
1443 if (ret) {
1445 * In case of a incomplete request
1446 * prepare it again and resend.
1448 mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1449 mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
1451 } while (ret);
1453 return 1;
1455 cmd_abort:
1456 if (mmc_card_removed(card))
1457 req->cmd_flags |= REQ_QUIET;
1458 while (ret)
1459 ret = blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
1461 start_new_req:
1462 if (rqc) {
1463 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1464 mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL);
1467 return 0;
1470 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1472 int ret;
1473 struct mmc_blk_data *md = mq->data;
1474 struct mmc_card *card = md->queue.card;
1476 if (req && !mq->mqrq_prev->req)
1477 /* claim host only for the first request */
1478 mmc_claim_host(card->host);
1480 ret = mmc_blk_part_switch(card, md);
1481 if (ret) {
1482 if (req) {
1483 blk_end_request_all(req, -EIO);
1485 ret = 0;
1486 goto out;
1489 if (req && req->cmd_flags & REQ_DISCARD) {
1490 /* complete ongoing async transfer before issuing discard */
1491 if (card->host->areq)
1492 mmc_blk_issue_rw_rq(mq, NULL);
1493 if (req->cmd_flags & REQ_SECURE &&
1494 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1495 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1496 else
1497 ret = mmc_blk_issue_discard_rq(mq, req);
1498 } else if (req && req->cmd_flags & REQ_FLUSH) {
1499 /* complete ongoing async transfer before issuing flush */
1500 if (card->host->areq)
1501 mmc_blk_issue_rw_rq(mq, NULL);
1502 ret = mmc_blk_issue_flush(mq, req);
1503 } else {
1504 ret = mmc_blk_issue_rw_rq(mq, req);
1507 out:
1508 if (!req)
1509 /* release host only when there are no more requests */
1510 mmc_release_host(card->host);
1511 return ret;
1514 static inline int mmc_blk_readonly(struct mmc_card *card)
1516 return mmc_card_readonly(card) ||
1517 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1520 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1521 struct device *parent,
1522 sector_t size,
1523 bool default_ro,
1524 const char *subname,
1525 int area_type)
1527 struct mmc_blk_data *md;
1528 int devidx, ret;
1530 devidx = find_first_zero_bit(dev_use, max_devices);
1531 if (devidx >= max_devices)
1532 return ERR_PTR(-ENOSPC);
1533 __set_bit(devidx, dev_use);
1535 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1536 if (!md) {
1537 ret = -ENOMEM;
1538 goto out;
1542 * !subname implies we are creating main mmc_blk_data that will be
1543 * associated with mmc_card with mmc_set_drvdata. Due to device
1544 * partitions, devidx will not coincide with a per-physical card
1545 * index anymore so we keep track of a name index.
1547 if (!subname) {
1548 md->name_idx = find_first_zero_bit(name_use, max_devices);
1549 __set_bit(md->name_idx, name_use);
1550 } else
1551 md->name_idx = ((struct mmc_blk_data *)
1552 dev_to_disk(parent)->private_data)->name_idx;
1554 md->area_type = area_type;
1557 * Set the read-only status based on the supported commands
1558 * and the write protect switch.
1560 md->read_only = mmc_blk_readonly(card);
1562 md->disk = alloc_disk(perdev_minors);
1563 if (md->disk == NULL) {
1564 ret = -ENOMEM;
1565 goto err_kfree;
1568 spin_lock_init(&md->lock);
1569 INIT_LIST_HEAD(&md->part);
1570 md->usage = 1;
1572 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1573 if (ret)
1574 goto err_putdisk;
1576 md->queue.issue_fn = mmc_blk_issue_rq;
1577 md->queue.data = md;
1579 md->disk->major = MMC_BLOCK_MAJOR;
1580 md->disk->first_minor = devidx * perdev_minors;
1581 md->disk->fops = &mmc_bdops;
1582 md->disk->private_data = md;
1583 md->disk->queue = md->queue.queue;
1584 md->disk->driverfs_dev = parent;
1585 set_disk_ro(md->disk, md->read_only || default_ro);
1586 if (area_type & MMC_BLK_DATA_AREA_RPMB)
1587 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
1590 * As discussed on lkml, GENHD_FL_REMOVABLE should:
1592 * - be set for removable media with permanent block devices
1593 * - be unset for removable block devices with permanent media
1595 * Since MMC block devices clearly fall under the second
1596 * case, we do not set GENHD_FL_REMOVABLE. Userspace
1597 * should use the block device creation/destruction hotplug
1598 * messages to tell when the card is present.
1601 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1602 "mmcblk%d%s", md->name_idx, subname ? subname : "");
1604 if (mmc_card_mmc(card))
1605 blk_queue_logical_block_size(md->queue.queue,
1606 card->ext_csd.data_sector_size);
1607 else
1608 blk_queue_logical_block_size(md->queue.queue, 512);
1610 set_capacity(md->disk, size);
1612 if (mmc_host_cmd23(card->host)) {
1613 if (mmc_card_mmc(card) ||
1614 (mmc_card_sd(card) &&
1615 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1616 md->flags |= MMC_BLK_CMD23;
1619 if (mmc_card_mmc(card) &&
1620 md->flags & MMC_BLK_CMD23 &&
1621 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1622 card->ext_csd.rel_sectors)) {
1623 md->flags |= MMC_BLK_REL_WR;
1624 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1627 return md;
1629 err_putdisk:
1630 put_disk(md->disk);
1631 err_kfree:
1632 kfree(md);
1633 out:
1634 return ERR_PTR(ret);
1637 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1639 sector_t size;
1640 struct mmc_blk_data *md;
1642 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1644 * The EXT_CSD sector count is in number or 512 byte
1645 * sectors.
1647 size = card->ext_csd.sectors;
1648 } else {
1650 * The CSD capacity field is in units of read_blkbits.
1651 * set_capacity takes units of 512 bytes.
1653 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1656 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1657 MMC_BLK_DATA_AREA_MAIN);
1658 return md;
1661 static int mmc_blk_alloc_part(struct mmc_card *card,
1662 struct mmc_blk_data *md,
1663 unsigned int part_type,
1664 sector_t size,
1665 bool default_ro,
1666 const char *subname,
1667 int area_type)
1669 char cap_str[10];
1670 struct mmc_blk_data *part_md;
1672 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1673 subname, area_type);
1674 if (IS_ERR(part_md))
1675 return PTR_ERR(part_md);
1676 part_md->part_type = part_type;
1677 list_add(&part_md->part, &md->part);
1679 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1680 cap_str, sizeof(cap_str));
1681 pr_info("%s: %s %s partition %u %s\n",
1682 part_md->disk->disk_name, mmc_card_id(card),
1683 mmc_card_name(card), part_md->part_type, cap_str);
1684 return 0;
1687 /* MMC Physical partitions consist of two boot partitions and
1688 * up to four general purpose partitions.
1689 * For each partition enabled in EXT_CSD a block device will be allocatedi
1690 * to provide access to the partition.
1693 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1695 int idx, ret = 0;
1697 if (!mmc_card_mmc(card))
1698 return 0;
1700 for (idx = 0; idx < card->nr_parts; idx++) {
1701 if (card->part[idx].size) {
1702 ret = mmc_blk_alloc_part(card, md,
1703 card->part[idx].part_cfg,
1704 card->part[idx].size >> 9,
1705 card->part[idx].force_ro,
1706 card->part[idx].name,
1707 card->part[idx].area_type);
1708 if (ret)
1709 return ret;
1713 return ret;
1716 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1718 struct mmc_card *card;
1720 if (md) {
1721 card = md->queue.card;
1722 if (md->disk->flags & GENHD_FL_UP) {
1723 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1724 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1725 card->ext_csd.boot_ro_lockable)
1726 device_remove_file(disk_to_dev(md->disk),
1727 &md->power_ro_lock);
1729 /* Stop new requests from getting into the queue */
1730 del_gendisk(md->disk);
1733 /* Then flush out any already in there */
1734 mmc_cleanup_queue(&md->queue);
1735 mmc_blk_put(md);
1739 static void mmc_blk_remove_parts(struct mmc_card *card,
1740 struct mmc_blk_data *md)
1742 struct list_head *pos, *q;
1743 struct mmc_blk_data *part_md;
1745 __clear_bit(md->name_idx, name_use);
1746 list_for_each_safe(pos, q, &md->part) {
1747 part_md = list_entry(pos, struct mmc_blk_data, part);
1748 list_del(pos);
1749 mmc_blk_remove_req(part_md);
1753 static int mmc_add_disk(struct mmc_blk_data *md)
1755 int ret;
1756 struct mmc_card *card = md->queue.card;
1758 add_disk(md->disk);
1759 md->force_ro.show = force_ro_show;
1760 md->force_ro.store = force_ro_store;
1761 sysfs_attr_init(&md->force_ro.attr);
1762 md->force_ro.attr.name = "force_ro";
1763 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1764 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1765 if (ret)
1766 goto force_ro_fail;
1768 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1769 card->ext_csd.boot_ro_lockable) {
1770 umode_t mode;
1772 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
1773 mode = S_IRUGO;
1774 else
1775 mode = S_IRUGO | S_IWUSR;
1777 md->power_ro_lock.show = power_ro_lock_show;
1778 md->power_ro_lock.store = power_ro_lock_store;
1779 sysfs_attr_init(&md->power_ro_lock.attr);
1780 md->power_ro_lock.attr.mode = mode;
1781 md->power_ro_lock.attr.name =
1782 "ro_lock_until_next_power_on";
1783 ret = device_create_file(disk_to_dev(md->disk),
1784 &md->power_ro_lock);
1785 if (ret)
1786 goto power_ro_lock_fail;
1788 return ret;
1790 power_ro_lock_fail:
1791 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1792 force_ro_fail:
1793 del_gendisk(md->disk);
1795 return ret;
1798 #define CID_MANFID_SANDISK 0x2
1799 #define CID_MANFID_TOSHIBA 0x11
1800 #define CID_MANFID_MICRON 0x13
1801 #define CID_MANFID_SAMSUNG 0x15
1803 static const struct mmc_fixup blk_fixups[] =
1805 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
1806 MMC_QUIRK_INAND_CMD38),
1807 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
1808 MMC_QUIRK_INAND_CMD38),
1809 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
1810 MMC_QUIRK_INAND_CMD38),
1811 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
1812 MMC_QUIRK_INAND_CMD38),
1813 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
1814 MMC_QUIRK_INAND_CMD38),
1817 * Some MMC cards experience performance degradation with CMD23
1818 * instead of CMD12-bounded multiblock transfers. For now we'll
1819 * black list what's bad...
1820 * - Certain Toshiba cards.
1822 * N.B. This doesn't affect SD cards.
1824 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1825 MMC_QUIRK_BLK_NO_CMD23),
1826 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1827 MMC_QUIRK_BLK_NO_CMD23),
1828 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1829 MMC_QUIRK_BLK_NO_CMD23),
1832 * Some Micron MMC cards needs longer data read timeout than
1833 * indicated in CSD.
1835 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
1836 MMC_QUIRK_LONG_READ_TIME),
1839 * On these Samsung MoviNAND parts, performing secure erase or
1840 * secure trim can result in unrecoverable corruption due to a
1841 * firmware bug.
1843 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1844 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1845 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1846 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1847 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1848 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1849 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1850 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1851 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1852 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1853 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1854 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1855 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1856 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1857 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1858 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1860 END_FIXUP
1863 static int mmc_blk_probe(struct mmc_card *card)
1865 struct mmc_blk_data *md, *part_md;
1866 char cap_str[10];
1869 * Check that the card supports the command class(es) we need.
1871 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1872 return -ENODEV;
1874 md = mmc_blk_alloc(card);
1875 if (IS_ERR(md))
1876 return PTR_ERR(md);
1878 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1879 cap_str, sizeof(cap_str));
1880 pr_info("%s: %s %s %s %s\n",
1881 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1882 cap_str, md->read_only ? "(ro)" : "");
1884 if (mmc_blk_alloc_parts(card, md))
1885 goto out;
1887 mmc_set_drvdata(card, md);
1888 mmc_fixup_device(card, blk_fixups);
1890 if (mmc_add_disk(md))
1891 goto out;
1893 list_for_each_entry(part_md, &md->part, part) {
1894 if (mmc_add_disk(part_md))
1895 goto out;
1897 return 0;
1899 out:
1900 mmc_blk_remove_parts(card, md);
1901 mmc_blk_remove_req(md);
1902 return 0;
1905 static void mmc_blk_remove(struct mmc_card *card)
1907 struct mmc_blk_data *md = mmc_get_drvdata(card);
1909 mmc_blk_remove_parts(card, md);
1910 mmc_claim_host(card->host);
1911 mmc_blk_part_switch(card, md);
1912 mmc_release_host(card->host);
1913 mmc_blk_remove_req(md);
1914 mmc_set_drvdata(card, NULL);
1917 #ifdef CONFIG_PM
1918 static int mmc_blk_suspend(struct mmc_card *card)
1920 struct mmc_blk_data *part_md;
1921 struct mmc_blk_data *md = mmc_get_drvdata(card);
1923 if (md) {
1924 mmc_queue_suspend(&md->queue);
1925 list_for_each_entry(part_md, &md->part, part) {
1926 mmc_queue_suspend(&part_md->queue);
1929 return 0;
1932 static int mmc_blk_resume(struct mmc_card *card)
1934 struct mmc_blk_data *part_md;
1935 struct mmc_blk_data *md = mmc_get_drvdata(card);
1937 if (md) {
1939 * Resume involves the card going into idle state,
1940 * so current partition is always the main one.
1942 md->part_curr = md->part_type;
1943 mmc_queue_resume(&md->queue);
1944 list_for_each_entry(part_md, &md->part, part) {
1945 mmc_queue_resume(&part_md->queue);
1948 return 0;
1950 #else
1951 #define mmc_blk_suspend NULL
1952 #define mmc_blk_resume NULL
1953 #endif
1955 static struct mmc_driver mmc_driver = {
1956 .drv = {
1957 .name = "mmcblk",
1959 .probe = mmc_blk_probe,
1960 .remove = mmc_blk_remove,
1961 .suspend = mmc_blk_suspend,
1962 .resume = mmc_blk_resume,
1965 static int __init mmc_blk_init(void)
1967 int res;
1969 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1970 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1972 max_devices = 256 / perdev_minors;
1974 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1975 if (res)
1976 goto out;
1978 res = mmc_register_driver(&mmc_driver);
1979 if (res)
1980 goto out2;
1982 return 0;
1983 out2:
1984 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1985 out:
1986 return res;
1989 static void __exit mmc_blk_exit(void)
1991 mmc_unregister_driver(&mmc_driver);
1992 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1995 module_init(mmc_blk_init);
1996 module_exit(mmc_blk_exit);
1998 MODULE_LICENSE("GPL");
1999 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");