Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/net
[linux-2.6.git] / drivers / mmc / card / block.c
blobcd0b7f4a1ff2dbd58419d92dcb077379c68e3a20
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>
37 #include <linux/pm_runtime.h>
39 #include <linux/mmc/ioctl.h>
40 #include <linux/mmc/card.h>
41 #include <linux/mmc/host.h>
42 #include <linux/mmc/mmc.h>
43 #include <linux/mmc/sd.h>
45 #include <asm/uaccess.h>
47 #include "queue.h"
49 MODULE_ALIAS("mmc:block");
50 #ifdef MODULE_PARAM_PREFIX
51 #undef MODULE_PARAM_PREFIX
52 #endif
53 #define MODULE_PARAM_PREFIX "mmcblk."
55 #define INAND_CMD38_ARG_EXT_CSD 113
56 #define INAND_CMD38_ARG_ERASE 0x00
57 #define INAND_CMD38_ARG_TRIM 0x01
58 #define INAND_CMD38_ARG_SECERASE 0x80
59 #define INAND_CMD38_ARG_SECTRIM1 0x81
60 #define INAND_CMD38_ARG_SECTRIM2 0x88
61 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
62 #define MMC_SANITIZE_REQ_TIMEOUT 240000
63 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
65 #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
66 (req->cmd_flags & REQ_META)) && \
67 (rq_data_dir(req) == WRITE))
68 #define PACKED_CMD_VER 0x01
69 #define PACKED_CMD_WR 0x02
71 static DEFINE_MUTEX(block_mutex);
74 * The defaults come from config options but can be overriden by module
75 * or bootarg options.
77 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
80 * We've only got one major, so number of mmcblk devices is
81 * limited to 256 / number of minors per device.
83 static int max_devices;
85 /* 256 minors, so at most 256 separate devices */
86 static DECLARE_BITMAP(dev_use, 256);
87 static DECLARE_BITMAP(name_use, 256);
90 * There is one mmc_blk_data per slot.
92 struct mmc_blk_data {
93 spinlock_t lock;
94 struct gendisk *disk;
95 struct mmc_queue queue;
96 struct list_head part;
98 unsigned int flags;
99 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
100 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
101 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
103 unsigned int usage;
104 unsigned int read_only;
105 unsigned int part_type;
106 unsigned int name_idx;
107 unsigned int reset_done;
108 #define MMC_BLK_READ BIT(0)
109 #define MMC_BLK_WRITE BIT(1)
110 #define MMC_BLK_DISCARD BIT(2)
111 #define MMC_BLK_SECDISCARD BIT(3)
114 * Only set in main mmc_blk_data associated
115 * with mmc_card with mmc_set_drvdata, and keeps
116 * track of the current selected device partition.
118 unsigned int part_curr;
119 struct device_attribute force_ro;
120 struct device_attribute power_ro_lock;
121 int area_type;
124 static DEFINE_MUTEX(open_lock);
126 enum {
127 MMC_PACKED_NR_IDX = -1,
128 MMC_PACKED_NR_ZERO,
129 MMC_PACKED_NR_SINGLE,
132 module_param(perdev_minors, int, 0444);
133 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
135 static inline int mmc_blk_part_switch(struct mmc_card *card,
136 struct mmc_blk_data *md);
137 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
139 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
141 struct mmc_packed *packed = mqrq->packed;
143 BUG_ON(!packed);
145 mqrq->cmd_type = MMC_PACKED_NONE;
146 packed->nr_entries = MMC_PACKED_NR_ZERO;
147 packed->idx_failure = MMC_PACKED_NR_IDX;
148 packed->retries = 0;
149 packed->blocks = 0;
152 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
154 struct mmc_blk_data *md;
156 mutex_lock(&open_lock);
157 md = disk->private_data;
158 if (md && md->usage == 0)
159 md = NULL;
160 if (md)
161 md->usage++;
162 mutex_unlock(&open_lock);
164 return md;
167 static inline int mmc_get_devidx(struct gendisk *disk)
169 int devmaj = MAJOR(disk_devt(disk));
170 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
172 if (!devmaj)
173 devidx = disk->first_minor / perdev_minors;
174 return devidx;
177 static void mmc_blk_put(struct mmc_blk_data *md)
179 mutex_lock(&open_lock);
180 md->usage--;
181 if (md->usage == 0) {
182 int devidx = mmc_get_devidx(md->disk);
183 blk_cleanup_queue(md->queue.queue);
185 __clear_bit(devidx, dev_use);
187 put_disk(md->disk);
188 kfree(md);
190 mutex_unlock(&open_lock);
193 static ssize_t power_ro_lock_show(struct device *dev,
194 struct device_attribute *attr, char *buf)
196 int ret;
197 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
198 struct mmc_card *card = md->queue.card;
199 int locked = 0;
201 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
202 locked = 2;
203 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
204 locked = 1;
206 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
208 return ret;
211 static ssize_t power_ro_lock_store(struct device *dev,
212 struct device_attribute *attr, const char *buf, size_t count)
214 int ret;
215 struct mmc_blk_data *md, *part_md;
216 struct mmc_card *card;
217 unsigned long set;
219 if (kstrtoul(buf, 0, &set))
220 return -EINVAL;
222 if (set != 1)
223 return count;
225 md = mmc_blk_get(dev_to_disk(dev));
226 card = md->queue.card;
228 mmc_get_card(card);
230 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
231 card->ext_csd.boot_ro_lock |
232 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
233 card->ext_csd.part_time);
234 if (ret)
235 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
236 else
237 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
239 mmc_put_card(card);
241 if (!ret) {
242 pr_info("%s: Locking boot partition ro until next power on\n",
243 md->disk->disk_name);
244 set_disk_ro(md->disk, 1);
246 list_for_each_entry(part_md, &md->part, part)
247 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
248 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
249 set_disk_ro(part_md->disk, 1);
253 mmc_blk_put(md);
254 return count;
257 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
258 char *buf)
260 int ret;
261 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
263 ret = snprintf(buf, PAGE_SIZE, "%d",
264 get_disk_ro(dev_to_disk(dev)) ^
265 md->read_only);
266 mmc_blk_put(md);
267 return ret;
270 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
271 const char *buf, size_t count)
273 int ret;
274 char *end;
275 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
276 unsigned long set = simple_strtoul(buf, &end, 0);
277 if (end == buf) {
278 ret = -EINVAL;
279 goto out;
282 set_disk_ro(dev_to_disk(dev), set || md->read_only);
283 ret = count;
284 out:
285 mmc_blk_put(md);
286 return ret;
289 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
291 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
292 int ret = -ENXIO;
294 mutex_lock(&block_mutex);
295 if (md) {
296 if (md->usage == 2)
297 check_disk_change(bdev);
298 ret = 0;
300 if ((mode & FMODE_WRITE) && md->read_only) {
301 mmc_blk_put(md);
302 ret = -EROFS;
305 mutex_unlock(&block_mutex);
307 return ret;
310 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
312 struct mmc_blk_data *md = disk->private_data;
314 mutex_lock(&block_mutex);
315 mmc_blk_put(md);
316 mutex_unlock(&block_mutex);
319 static int
320 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
322 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
323 geo->heads = 4;
324 geo->sectors = 16;
325 return 0;
328 struct mmc_blk_ioc_data {
329 struct mmc_ioc_cmd ic;
330 unsigned char *buf;
331 u64 buf_bytes;
334 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
335 struct mmc_ioc_cmd __user *user)
337 struct mmc_blk_ioc_data *idata;
338 int err;
340 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
341 if (!idata) {
342 err = -ENOMEM;
343 goto out;
346 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
347 err = -EFAULT;
348 goto idata_err;
351 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
352 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
353 err = -EOVERFLOW;
354 goto idata_err;
357 if (!idata->buf_bytes)
358 return idata;
360 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
361 if (!idata->buf) {
362 err = -ENOMEM;
363 goto idata_err;
366 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
367 idata->ic.data_ptr, idata->buf_bytes)) {
368 err = -EFAULT;
369 goto copy_err;
372 return idata;
374 copy_err:
375 kfree(idata->buf);
376 idata_err:
377 kfree(idata);
378 out:
379 return ERR_PTR(err);
382 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
383 u32 retries_max)
385 int err;
386 u32 retry_count = 0;
388 if (!status || !retries_max)
389 return -EINVAL;
391 do {
392 err = get_card_status(card, status, 5);
393 if (err)
394 break;
396 if (!R1_STATUS(*status) &&
397 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
398 break; /* RPMB programming operation complete */
401 * Rechedule to give the MMC device a chance to continue
402 * processing the previous command without being polled too
403 * frequently.
405 usleep_range(1000, 5000);
406 } while (++retry_count < retries_max);
408 if (retry_count == retries_max)
409 err = -EPERM;
411 return err;
414 static int ioctl_do_sanitize(struct mmc_card *card)
416 int err;
418 if (!(mmc_can_sanitize(card) &&
419 (card->host->caps2 & MMC_CAP2_SANITIZE))) {
420 pr_warn("%s: %s - SANITIZE is not supported\n",
421 mmc_hostname(card->host), __func__);
422 err = -EOPNOTSUPP;
423 goto out;
426 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
427 mmc_hostname(card->host), __func__);
429 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
430 EXT_CSD_SANITIZE_START, 1,
431 MMC_SANITIZE_REQ_TIMEOUT);
433 if (err)
434 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
435 mmc_hostname(card->host), __func__, err);
437 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
438 __func__);
439 out:
440 return err;
443 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
444 struct mmc_ioc_cmd __user *ic_ptr)
446 struct mmc_blk_ioc_data *idata;
447 struct mmc_blk_data *md;
448 struct mmc_card *card;
449 struct mmc_command cmd = {0};
450 struct mmc_data data = {0};
451 struct mmc_request mrq = {NULL};
452 struct scatterlist sg;
453 int err;
454 int is_rpmb = false;
455 u32 status = 0;
458 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
459 * whole block device, not on a partition. This prevents overspray
460 * between sibling partitions.
462 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
463 return -EPERM;
465 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
466 if (IS_ERR(idata))
467 return PTR_ERR(idata);
469 md = mmc_blk_get(bdev->bd_disk);
470 if (!md) {
471 err = -EINVAL;
472 goto cmd_err;
475 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
476 is_rpmb = true;
478 card = md->queue.card;
479 if (IS_ERR(card)) {
480 err = PTR_ERR(card);
481 goto cmd_done;
484 cmd.opcode = idata->ic.opcode;
485 cmd.arg = idata->ic.arg;
486 cmd.flags = idata->ic.flags;
488 if (idata->buf_bytes) {
489 data.sg = &sg;
490 data.sg_len = 1;
491 data.blksz = idata->ic.blksz;
492 data.blocks = idata->ic.blocks;
494 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
496 if (idata->ic.write_flag)
497 data.flags = MMC_DATA_WRITE;
498 else
499 data.flags = MMC_DATA_READ;
501 /* data.flags must already be set before doing this. */
502 mmc_set_data_timeout(&data, card);
504 /* Allow overriding the timeout_ns for empirical tuning. */
505 if (idata->ic.data_timeout_ns)
506 data.timeout_ns = idata->ic.data_timeout_ns;
508 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
510 * Pretend this is a data transfer and rely on the
511 * host driver to compute timeout. When all host
512 * drivers support cmd.cmd_timeout for R1B, this
513 * can be changed to:
515 * mrq.data = NULL;
516 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
518 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
521 mrq.data = &data;
524 mrq.cmd = &cmd;
526 mmc_get_card(card);
528 err = mmc_blk_part_switch(card, md);
529 if (err)
530 goto cmd_rel_host;
532 if (idata->ic.is_acmd) {
533 err = mmc_app_cmd(card->host, card);
534 if (err)
535 goto cmd_rel_host;
538 if (is_rpmb) {
539 err = mmc_set_blockcount(card, data.blocks,
540 idata->ic.write_flag & (1 << 31));
541 if (err)
542 goto cmd_rel_host;
545 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
546 (cmd.opcode == MMC_SWITCH)) {
547 err = ioctl_do_sanitize(card);
549 if (err)
550 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
551 __func__, err);
553 goto cmd_rel_host;
556 mmc_wait_for_req(card->host, &mrq);
558 if (cmd.error) {
559 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
560 __func__, cmd.error);
561 err = cmd.error;
562 goto cmd_rel_host;
564 if (data.error) {
565 dev_err(mmc_dev(card->host), "%s: data error %d\n",
566 __func__, data.error);
567 err = data.error;
568 goto cmd_rel_host;
572 * According to the SD specs, some commands require a delay after
573 * issuing the command.
575 if (idata->ic.postsleep_min_us)
576 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
578 if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
579 err = -EFAULT;
580 goto cmd_rel_host;
583 if (!idata->ic.write_flag) {
584 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
585 idata->buf, idata->buf_bytes)) {
586 err = -EFAULT;
587 goto cmd_rel_host;
591 if (is_rpmb) {
593 * Ensure RPMB command has completed by polling CMD13
594 * "Send Status".
596 err = ioctl_rpmb_card_status_poll(card, &status, 5);
597 if (err)
598 dev_err(mmc_dev(card->host),
599 "%s: Card Status=0x%08X, error %d\n",
600 __func__, status, err);
603 cmd_rel_host:
604 mmc_put_card(card);
606 cmd_done:
607 mmc_blk_put(md);
608 cmd_err:
609 kfree(idata->buf);
610 kfree(idata);
611 return err;
614 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
615 unsigned int cmd, unsigned long arg)
617 int ret = -EINVAL;
618 if (cmd == MMC_IOC_CMD)
619 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
620 return ret;
623 #ifdef CONFIG_COMPAT
624 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
625 unsigned int cmd, unsigned long arg)
627 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
629 #endif
631 static const struct block_device_operations mmc_bdops = {
632 .open = mmc_blk_open,
633 .release = mmc_blk_release,
634 .getgeo = mmc_blk_getgeo,
635 .owner = THIS_MODULE,
636 .ioctl = mmc_blk_ioctl,
637 #ifdef CONFIG_COMPAT
638 .compat_ioctl = mmc_blk_compat_ioctl,
639 #endif
642 static inline int mmc_blk_part_switch(struct mmc_card *card,
643 struct mmc_blk_data *md)
645 int ret;
646 struct mmc_blk_data *main_md = mmc_get_drvdata(card);
648 if (main_md->part_curr == md->part_type)
649 return 0;
651 if (mmc_card_mmc(card)) {
652 u8 part_config = card->ext_csd.part_config;
654 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
655 part_config |= md->part_type;
657 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
658 EXT_CSD_PART_CONFIG, part_config,
659 card->ext_csd.part_time);
660 if (ret)
661 return ret;
663 card->ext_csd.part_config = part_config;
666 main_md->part_curr = md->part_type;
667 return 0;
670 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
672 int err;
673 u32 result;
674 __be32 *blocks;
676 struct mmc_request mrq = {NULL};
677 struct mmc_command cmd = {0};
678 struct mmc_data data = {0};
680 struct scatterlist sg;
682 cmd.opcode = MMC_APP_CMD;
683 cmd.arg = card->rca << 16;
684 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
686 err = mmc_wait_for_cmd(card->host, &cmd, 0);
687 if (err)
688 return (u32)-1;
689 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
690 return (u32)-1;
692 memset(&cmd, 0, sizeof(struct mmc_command));
694 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
695 cmd.arg = 0;
696 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
698 data.blksz = 4;
699 data.blocks = 1;
700 data.flags = MMC_DATA_READ;
701 data.sg = &sg;
702 data.sg_len = 1;
703 mmc_set_data_timeout(&data, card);
705 mrq.cmd = &cmd;
706 mrq.data = &data;
708 blocks = kmalloc(4, GFP_KERNEL);
709 if (!blocks)
710 return (u32)-1;
712 sg_init_one(&sg, blocks, 4);
714 mmc_wait_for_req(card->host, &mrq);
716 result = ntohl(*blocks);
717 kfree(blocks);
719 if (cmd.error || data.error)
720 result = (u32)-1;
722 return result;
725 static int send_stop(struct mmc_card *card, u32 *status)
727 struct mmc_command cmd = {0};
728 int err;
730 cmd.opcode = MMC_STOP_TRANSMISSION;
731 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
732 err = mmc_wait_for_cmd(card->host, &cmd, 5);
733 if (err == 0)
734 *status = cmd.resp[0];
735 return err;
738 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
740 struct mmc_command cmd = {0};
741 int err;
743 cmd.opcode = MMC_SEND_STATUS;
744 if (!mmc_host_is_spi(card->host))
745 cmd.arg = card->rca << 16;
746 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
747 err = mmc_wait_for_cmd(card->host, &cmd, retries);
748 if (err == 0)
749 *status = cmd.resp[0];
750 return err;
753 #define ERR_NOMEDIUM 3
754 #define ERR_RETRY 2
755 #define ERR_ABORT 1
756 #define ERR_CONTINUE 0
758 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
759 bool status_valid, u32 status)
761 switch (error) {
762 case -EILSEQ:
763 /* response crc error, retry the r/w cmd */
764 pr_err("%s: %s sending %s command, card status %#x\n",
765 req->rq_disk->disk_name, "response CRC error",
766 name, status);
767 return ERR_RETRY;
769 case -ETIMEDOUT:
770 pr_err("%s: %s sending %s command, card status %#x\n",
771 req->rq_disk->disk_name, "timed out", name, status);
773 /* If the status cmd initially failed, retry the r/w cmd */
774 if (!status_valid)
775 return ERR_RETRY;
778 * If it was a r/w cmd crc error, or illegal command
779 * (eg, issued in wrong state) then retry - we should
780 * have corrected the state problem above.
782 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
783 return ERR_RETRY;
785 /* Otherwise abort the command */
786 return ERR_ABORT;
788 default:
789 /* We don't understand the error code the driver gave us */
790 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
791 req->rq_disk->disk_name, error, status);
792 return ERR_ABORT;
797 * Initial r/w and stop cmd error recovery.
798 * We don't know whether the card received the r/w cmd or not, so try to
799 * restore things back to a sane state. Essentially, we do this as follows:
800 * - Obtain card status. If the first attempt to obtain card status fails,
801 * the status word will reflect the failed status cmd, not the failed
802 * r/w cmd. If we fail to obtain card status, it suggests we can no
803 * longer communicate with the card.
804 * - Check the card state. If the card received the cmd but there was a
805 * transient problem with the response, it might still be in a data transfer
806 * mode. Try to send it a stop command. If this fails, we can't recover.
807 * - If the r/w cmd failed due to a response CRC error, it was probably
808 * transient, so retry the cmd.
809 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
810 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
811 * illegal cmd, retry.
812 * Otherwise we don't understand what happened, so abort.
814 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
815 struct mmc_blk_request *brq, int *ecc_err)
817 bool prev_cmd_status_valid = true;
818 u32 status, stop_status = 0;
819 int err, retry;
821 if (mmc_card_removed(card))
822 return ERR_NOMEDIUM;
825 * Try to get card status which indicates both the card state
826 * and why there was no response. If the first attempt fails,
827 * we can't be sure the returned status is for the r/w command.
829 for (retry = 2; retry >= 0; retry--) {
830 err = get_card_status(card, &status, 0);
831 if (!err)
832 break;
834 prev_cmd_status_valid = false;
835 pr_err("%s: error %d sending status command, %sing\n",
836 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
839 /* We couldn't get a response from the card. Give up. */
840 if (err) {
841 /* Check if the card is removed */
842 if (mmc_detect_card_removed(card->host))
843 return ERR_NOMEDIUM;
844 return ERR_ABORT;
847 /* Flag ECC errors */
848 if ((status & R1_CARD_ECC_FAILED) ||
849 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
850 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
851 *ecc_err = 1;
854 * Check the current card state. If it is in some data transfer
855 * mode, tell it to stop (and hopefully transition back to TRAN.)
857 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
858 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
859 err = send_stop(card, &stop_status);
860 if (err)
861 pr_err("%s: error %d sending stop command\n",
862 req->rq_disk->disk_name, err);
865 * If the stop cmd also timed out, the card is probably
866 * not present, so abort. Other errors are bad news too.
868 if (err)
869 return ERR_ABORT;
870 if (stop_status & R1_CARD_ECC_FAILED)
871 *ecc_err = 1;
874 /* Check for set block count errors */
875 if (brq->sbc.error)
876 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
877 prev_cmd_status_valid, status);
879 /* Check for r/w command errors */
880 if (brq->cmd.error)
881 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
882 prev_cmd_status_valid, status);
884 /* Data errors */
885 if (!brq->stop.error)
886 return ERR_CONTINUE;
888 /* Now for stop errors. These aren't fatal to the transfer. */
889 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
890 req->rq_disk->disk_name, brq->stop.error,
891 brq->cmd.resp[0], status);
894 * Subsitute in our own stop status as this will give the error
895 * state which happened during the execution of the r/w command.
897 if (stop_status) {
898 brq->stop.resp[0] = stop_status;
899 brq->stop.error = 0;
901 return ERR_CONTINUE;
904 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
905 int type)
907 int err;
909 if (md->reset_done & type)
910 return -EEXIST;
912 md->reset_done |= type;
913 err = mmc_hw_reset(host);
914 /* Ensure we switch back to the correct partition */
915 if (err != -EOPNOTSUPP) {
916 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
917 int part_err;
919 main_md->part_curr = main_md->part_type;
920 part_err = mmc_blk_part_switch(host->card, md);
921 if (part_err) {
923 * We have failed to get back into the correct
924 * partition, so we need to abort the whole request.
926 return -ENODEV;
929 return err;
932 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
934 md->reset_done &= ~type;
937 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
939 struct mmc_blk_data *md = mq->data;
940 struct mmc_card *card = md->queue.card;
941 unsigned int from, nr, arg;
942 int err = 0, type = MMC_BLK_DISCARD;
944 if (!mmc_can_erase(card)) {
945 err = -EOPNOTSUPP;
946 goto out;
949 from = blk_rq_pos(req);
950 nr = blk_rq_sectors(req);
952 if (mmc_can_discard(card))
953 arg = MMC_DISCARD_ARG;
954 else if (mmc_can_trim(card))
955 arg = MMC_TRIM_ARG;
956 else
957 arg = MMC_ERASE_ARG;
958 retry:
959 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
960 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
961 INAND_CMD38_ARG_EXT_CSD,
962 arg == MMC_TRIM_ARG ?
963 INAND_CMD38_ARG_TRIM :
964 INAND_CMD38_ARG_ERASE,
966 if (err)
967 goto out;
969 err = mmc_erase(card, from, nr, arg);
970 out:
971 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
972 goto retry;
973 if (!err)
974 mmc_blk_reset_success(md, type);
975 blk_end_request(req, err, blk_rq_bytes(req));
977 return err ? 0 : 1;
980 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
981 struct request *req)
983 struct mmc_blk_data *md = mq->data;
984 struct mmc_card *card = md->queue.card;
985 unsigned int from, nr, arg;
986 int err = 0, type = MMC_BLK_SECDISCARD;
988 if (!(mmc_can_secure_erase_trim(card))) {
989 err = -EOPNOTSUPP;
990 goto out;
993 from = blk_rq_pos(req);
994 nr = blk_rq_sectors(req);
996 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
997 arg = MMC_SECURE_TRIM1_ARG;
998 else
999 arg = MMC_SECURE_ERASE_ARG;
1001 retry:
1002 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1003 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1004 INAND_CMD38_ARG_EXT_CSD,
1005 arg == MMC_SECURE_TRIM1_ARG ?
1006 INAND_CMD38_ARG_SECTRIM1 :
1007 INAND_CMD38_ARG_SECERASE,
1009 if (err)
1010 goto out_retry;
1013 err = mmc_erase(card, from, nr, arg);
1014 if (err == -EIO)
1015 goto out_retry;
1016 if (err)
1017 goto out;
1019 if (arg == MMC_SECURE_TRIM1_ARG) {
1020 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1021 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1022 INAND_CMD38_ARG_EXT_CSD,
1023 INAND_CMD38_ARG_SECTRIM2,
1025 if (err)
1026 goto out_retry;
1029 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1030 if (err == -EIO)
1031 goto out_retry;
1032 if (err)
1033 goto out;
1036 out_retry:
1037 if (err && !mmc_blk_reset(md, card->host, type))
1038 goto retry;
1039 if (!err)
1040 mmc_blk_reset_success(md, type);
1041 out:
1042 blk_end_request(req, err, blk_rq_bytes(req));
1044 return err ? 0 : 1;
1047 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1049 struct mmc_blk_data *md = mq->data;
1050 struct mmc_card *card = md->queue.card;
1051 int ret = 0;
1053 ret = mmc_flush_cache(card);
1054 if (ret)
1055 ret = -EIO;
1057 blk_end_request_all(req, ret);
1059 return ret ? 0 : 1;
1063 * Reformat current write as a reliable write, supporting
1064 * both legacy and the enhanced reliable write MMC cards.
1065 * In each transfer we'll handle only as much as a single
1066 * reliable write can handle, thus finish the request in
1067 * partial completions.
1069 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1070 struct mmc_card *card,
1071 struct request *req)
1073 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1074 /* Legacy mode imposes restrictions on transfers. */
1075 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1076 brq->data.blocks = 1;
1078 if (brq->data.blocks > card->ext_csd.rel_sectors)
1079 brq->data.blocks = card->ext_csd.rel_sectors;
1080 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1081 brq->data.blocks = 1;
1085 #define CMD_ERRORS \
1086 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1087 R1_ADDRESS_ERROR | /* Misaligned address */ \
1088 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1089 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1090 R1_CC_ERROR | /* Card controller error */ \
1091 R1_ERROR) /* General/unknown error */
1093 static int mmc_blk_err_check(struct mmc_card *card,
1094 struct mmc_async_req *areq)
1096 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1097 mmc_active);
1098 struct mmc_blk_request *brq = &mq_mrq->brq;
1099 struct request *req = mq_mrq->req;
1100 int ecc_err = 0;
1103 * sbc.error indicates a problem with the set block count
1104 * command. No data will have been transferred.
1106 * cmd.error indicates a problem with the r/w command. No
1107 * data will have been transferred.
1109 * stop.error indicates a problem with the stop command. Data
1110 * may have been transferred, or may still be transferring.
1112 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1113 brq->data.error) {
1114 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
1115 case ERR_RETRY:
1116 return MMC_BLK_RETRY;
1117 case ERR_ABORT:
1118 return MMC_BLK_ABORT;
1119 case ERR_NOMEDIUM:
1120 return MMC_BLK_NOMEDIUM;
1121 case ERR_CONTINUE:
1122 break;
1127 * Check for errors relating to the execution of the
1128 * initial command - such as address errors. No data
1129 * has been transferred.
1131 if (brq->cmd.resp[0] & CMD_ERRORS) {
1132 pr_err("%s: r/w command failed, status = %#x\n",
1133 req->rq_disk->disk_name, brq->cmd.resp[0]);
1134 return MMC_BLK_ABORT;
1138 * Everything else is either success, or a data error of some
1139 * kind. If it was a write, we may have transitioned to
1140 * program mode, which we have to wait for it to complete.
1142 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1143 u32 status;
1144 unsigned long timeout;
1146 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1147 do {
1148 int err = get_card_status(card, &status, 5);
1149 if (err) {
1150 pr_err("%s: error %d requesting status\n",
1151 req->rq_disk->disk_name, err);
1152 return MMC_BLK_CMD_ERR;
1155 /* Timeout if the device never becomes ready for data
1156 * and never leaves the program state.
1158 if (time_after(jiffies, timeout)) {
1159 pr_err("%s: Card stuck in programming state!"\
1160 " %s %s\n", mmc_hostname(card->host),
1161 req->rq_disk->disk_name, __func__);
1163 return MMC_BLK_CMD_ERR;
1166 * Some cards mishandle the status bits,
1167 * so make sure to check both the busy
1168 * indication and the card state.
1170 } while (!(status & R1_READY_FOR_DATA) ||
1171 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1174 if (brq->data.error) {
1175 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1176 req->rq_disk->disk_name, brq->data.error,
1177 (unsigned)blk_rq_pos(req),
1178 (unsigned)blk_rq_sectors(req),
1179 brq->cmd.resp[0], brq->stop.resp[0]);
1181 if (rq_data_dir(req) == READ) {
1182 if (ecc_err)
1183 return MMC_BLK_ECC_ERR;
1184 return MMC_BLK_DATA_ERR;
1185 } else {
1186 return MMC_BLK_CMD_ERR;
1190 if (!brq->data.bytes_xfered)
1191 return MMC_BLK_RETRY;
1193 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1194 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1195 return MMC_BLK_PARTIAL;
1196 else
1197 return MMC_BLK_SUCCESS;
1200 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1201 return MMC_BLK_PARTIAL;
1203 return MMC_BLK_SUCCESS;
1206 static int mmc_blk_packed_err_check(struct mmc_card *card,
1207 struct mmc_async_req *areq)
1209 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1210 mmc_active);
1211 struct request *req = mq_rq->req;
1212 struct mmc_packed *packed = mq_rq->packed;
1213 int err, check, status;
1214 u8 *ext_csd;
1216 BUG_ON(!packed);
1218 packed->retries--;
1219 check = mmc_blk_err_check(card, areq);
1220 err = get_card_status(card, &status, 0);
1221 if (err) {
1222 pr_err("%s: error %d sending status command\n",
1223 req->rq_disk->disk_name, err);
1224 return MMC_BLK_ABORT;
1227 if (status & R1_EXCEPTION_EVENT) {
1228 ext_csd = kzalloc(512, GFP_KERNEL);
1229 if (!ext_csd) {
1230 pr_err("%s: unable to allocate buffer for ext_csd\n",
1231 req->rq_disk->disk_name);
1232 return -ENOMEM;
1235 err = mmc_send_ext_csd(card, ext_csd);
1236 if (err) {
1237 pr_err("%s: error %d sending ext_csd\n",
1238 req->rq_disk->disk_name, err);
1239 check = MMC_BLK_ABORT;
1240 goto free;
1243 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1244 EXT_CSD_PACKED_FAILURE) &&
1245 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1246 EXT_CSD_PACKED_GENERIC_ERROR)) {
1247 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1248 EXT_CSD_PACKED_INDEXED_ERROR) {
1249 packed->idx_failure =
1250 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1251 check = MMC_BLK_PARTIAL;
1253 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1254 "failure index: %d\n",
1255 req->rq_disk->disk_name, packed->nr_entries,
1256 packed->blocks, packed->idx_failure);
1258 free:
1259 kfree(ext_csd);
1262 return check;
1265 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1266 struct mmc_card *card,
1267 int disable_multi,
1268 struct mmc_queue *mq)
1270 u32 readcmd, writecmd;
1271 struct mmc_blk_request *brq = &mqrq->brq;
1272 struct request *req = mqrq->req;
1273 struct mmc_blk_data *md = mq->data;
1274 bool do_data_tag;
1277 * Reliable writes are used to implement Forced Unit Access and
1278 * REQ_META accesses, and are supported only on MMCs.
1280 * XXX: this really needs a good explanation of why REQ_META
1281 * is treated special.
1283 bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1284 (req->cmd_flags & REQ_META)) &&
1285 (rq_data_dir(req) == WRITE) &&
1286 (md->flags & MMC_BLK_REL_WR);
1288 memset(brq, 0, sizeof(struct mmc_blk_request));
1289 brq->mrq.cmd = &brq->cmd;
1290 brq->mrq.data = &brq->data;
1292 brq->cmd.arg = blk_rq_pos(req);
1293 if (!mmc_card_blockaddr(card))
1294 brq->cmd.arg <<= 9;
1295 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1296 brq->data.blksz = 512;
1297 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1298 brq->stop.arg = 0;
1299 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1300 brq->data.blocks = blk_rq_sectors(req);
1303 * The block layer doesn't support all sector count
1304 * restrictions, so we need to be prepared for too big
1305 * requests.
1307 if (brq->data.blocks > card->host->max_blk_count)
1308 brq->data.blocks = card->host->max_blk_count;
1310 if (brq->data.blocks > 1) {
1312 * After a read error, we redo the request one sector
1313 * at a time in order to accurately determine which
1314 * sectors can be read successfully.
1316 if (disable_multi)
1317 brq->data.blocks = 1;
1319 /* Some controllers can't do multiblock reads due to hw bugs */
1320 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1321 rq_data_dir(req) == READ)
1322 brq->data.blocks = 1;
1325 if (brq->data.blocks > 1 || do_rel_wr) {
1326 /* SPI multiblock writes terminate using a special
1327 * token, not a STOP_TRANSMISSION request.
1329 if (!mmc_host_is_spi(card->host) ||
1330 rq_data_dir(req) == READ)
1331 brq->mrq.stop = &brq->stop;
1332 readcmd = MMC_READ_MULTIPLE_BLOCK;
1333 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1334 } else {
1335 brq->mrq.stop = NULL;
1336 readcmd = MMC_READ_SINGLE_BLOCK;
1337 writecmd = MMC_WRITE_BLOCK;
1339 if (rq_data_dir(req) == READ) {
1340 brq->cmd.opcode = readcmd;
1341 brq->data.flags |= MMC_DATA_READ;
1342 } else {
1343 brq->cmd.opcode = writecmd;
1344 brq->data.flags |= MMC_DATA_WRITE;
1347 if (do_rel_wr)
1348 mmc_apply_rel_rw(brq, card, req);
1351 * Data tag is used only during writing meta data to speed
1352 * up write and any subsequent read of this meta data
1354 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1355 (req->cmd_flags & REQ_META) &&
1356 (rq_data_dir(req) == WRITE) &&
1357 ((brq->data.blocks * brq->data.blksz) >=
1358 card->ext_csd.data_tag_unit_size);
1361 * Pre-defined multi-block transfers are preferable to
1362 * open ended-ones (and necessary for reliable writes).
1363 * However, it is not sufficient to just send CMD23,
1364 * and avoid the final CMD12, as on an error condition
1365 * CMD12 (stop) needs to be sent anyway. This, coupled
1366 * with Auto-CMD23 enhancements provided by some
1367 * hosts, means that the complexity of dealing
1368 * with this is best left to the host. If CMD23 is
1369 * supported by card and host, we'll fill sbc in and let
1370 * the host deal with handling it correctly. This means
1371 * that for hosts that don't expose MMC_CAP_CMD23, no
1372 * change of behavior will be observed.
1374 * N.B: Some MMC cards experience perf degradation.
1375 * We'll avoid using CMD23-bounded multiblock writes for
1376 * these, while retaining features like reliable writes.
1378 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1379 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1380 do_data_tag)) {
1381 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1382 brq->sbc.arg = brq->data.blocks |
1383 (do_rel_wr ? (1 << 31) : 0) |
1384 (do_data_tag ? (1 << 29) : 0);
1385 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1386 brq->mrq.sbc = &brq->sbc;
1389 mmc_set_data_timeout(&brq->data, card);
1391 brq->data.sg = mqrq->sg;
1392 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1395 * Adjust the sg list so it is the same size as the
1396 * request.
1398 if (brq->data.blocks != blk_rq_sectors(req)) {
1399 int i, data_size = brq->data.blocks << 9;
1400 struct scatterlist *sg;
1402 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1403 data_size -= sg->length;
1404 if (data_size <= 0) {
1405 sg->length += data_size;
1406 i++;
1407 break;
1410 brq->data.sg_len = i;
1413 mqrq->mmc_active.mrq = &brq->mrq;
1414 mqrq->mmc_active.err_check = mmc_blk_err_check;
1416 mmc_queue_bounce_pre(mqrq);
1419 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1420 struct mmc_card *card)
1422 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1423 unsigned int max_seg_sz = queue_max_segment_size(q);
1424 unsigned int len, nr_segs = 0;
1426 do {
1427 len = min(hdr_sz, max_seg_sz);
1428 hdr_sz -= len;
1429 nr_segs++;
1430 } while (hdr_sz);
1432 return nr_segs;
1435 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1437 struct request_queue *q = mq->queue;
1438 struct mmc_card *card = mq->card;
1439 struct request *cur = req, *next = NULL;
1440 struct mmc_blk_data *md = mq->data;
1441 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1442 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1443 unsigned int req_sectors = 0, phys_segments = 0;
1444 unsigned int max_blk_count, max_phys_segs;
1445 bool put_back = true;
1446 u8 max_packed_rw = 0;
1447 u8 reqs = 0;
1449 if (!(md->flags & MMC_BLK_PACKED_CMD))
1450 goto no_packed;
1452 if ((rq_data_dir(cur) == WRITE) &&
1453 mmc_host_packed_wr(card->host))
1454 max_packed_rw = card->ext_csd.max_packed_writes;
1456 if (max_packed_rw == 0)
1457 goto no_packed;
1459 if (mmc_req_rel_wr(cur) &&
1460 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1461 goto no_packed;
1463 if (mmc_large_sector(card) &&
1464 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1465 goto no_packed;
1467 mmc_blk_clear_packed(mqrq);
1469 max_blk_count = min(card->host->max_blk_count,
1470 card->host->max_req_size >> 9);
1471 if (unlikely(max_blk_count > 0xffff))
1472 max_blk_count = 0xffff;
1474 max_phys_segs = queue_max_segments(q);
1475 req_sectors += blk_rq_sectors(cur);
1476 phys_segments += cur->nr_phys_segments;
1478 if (rq_data_dir(cur) == WRITE) {
1479 req_sectors += mmc_large_sector(card) ? 8 : 1;
1480 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1483 do {
1484 if (reqs >= max_packed_rw - 1) {
1485 put_back = false;
1486 break;
1489 spin_lock_irq(q->queue_lock);
1490 next = blk_fetch_request(q);
1491 spin_unlock_irq(q->queue_lock);
1492 if (!next) {
1493 put_back = false;
1494 break;
1497 if (mmc_large_sector(card) &&
1498 !IS_ALIGNED(blk_rq_sectors(next), 8))
1499 break;
1501 if (next->cmd_flags & REQ_DISCARD ||
1502 next->cmd_flags & REQ_FLUSH)
1503 break;
1505 if (rq_data_dir(cur) != rq_data_dir(next))
1506 break;
1508 if (mmc_req_rel_wr(next) &&
1509 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1510 break;
1512 req_sectors += blk_rq_sectors(next);
1513 if (req_sectors > max_blk_count)
1514 break;
1516 phys_segments += next->nr_phys_segments;
1517 if (phys_segments > max_phys_segs)
1518 break;
1520 list_add_tail(&next->queuelist, &mqrq->packed->list);
1521 cur = next;
1522 reqs++;
1523 } while (1);
1525 if (put_back) {
1526 spin_lock_irq(q->queue_lock);
1527 blk_requeue_request(q, next);
1528 spin_unlock_irq(q->queue_lock);
1531 if (reqs > 0) {
1532 list_add(&req->queuelist, &mqrq->packed->list);
1533 mqrq->packed->nr_entries = ++reqs;
1534 mqrq->packed->retries = reqs;
1535 return reqs;
1538 no_packed:
1539 mqrq->cmd_type = MMC_PACKED_NONE;
1540 return 0;
1543 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1544 struct mmc_card *card,
1545 struct mmc_queue *mq)
1547 struct mmc_blk_request *brq = &mqrq->brq;
1548 struct request *req = mqrq->req;
1549 struct request *prq;
1550 struct mmc_blk_data *md = mq->data;
1551 struct mmc_packed *packed = mqrq->packed;
1552 bool do_rel_wr, do_data_tag;
1553 u32 *packed_cmd_hdr;
1554 u8 hdr_blocks;
1555 u8 i = 1;
1557 BUG_ON(!packed);
1559 mqrq->cmd_type = MMC_PACKED_WRITE;
1560 packed->blocks = 0;
1561 packed->idx_failure = MMC_PACKED_NR_IDX;
1563 packed_cmd_hdr = packed->cmd_hdr;
1564 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1565 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1566 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1567 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1570 * Argument for each entry of packed group
1572 list_for_each_entry(prq, &packed->list, queuelist) {
1573 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1574 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1575 (prq->cmd_flags & REQ_META) &&
1576 (rq_data_dir(prq) == WRITE) &&
1577 ((brq->data.blocks * brq->data.blksz) >=
1578 card->ext_csd.data_tag_unit_size);
1579 /* Argument of CMD23 */
1580 packed_cmd_hdr[(i * 2)] =
1581 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1582 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1583 blk_rq_sectors(prq);
1584 /* Argument of CMD18 or CMD25 */
1585 packed_cmd_hdr[((i * 2)) + 1] =
1586 mmc_card_blockaddr(card) ?
1587 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1588 packed->blocks += blk_rq_sectors(prq);
1589 i++;
1592 memset(brq, 0, sizeof(struct mmc_blk_request));
1593 brq->mrq.cmd = &brq->cmd;
1594 brq->mrq.data = &brq->data;
1595 brq->mrq.sbc = &brq->sbc;
1596 brq->mrq.stop = &brq->stop;
1598 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1599 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1600 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1602 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1603 brq->cmd.arg = blk_rq_pos(req);
1604 if (!mmc_card_blockaddr(card))
1605 brq->cmd.arg <<= 9;
1606 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1608 brq->data.blksz = 512;
1609 brq->data.blocks = packed->blocks + hdr_blocks;
1610 brq->data.flags |= MMC_DATA_WRITE;
1612 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1613 brq->stop.arg = 0;
1614 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1616 mmc_set_data_timeout(&brq->data, card);
1618 brq->data.sg = mqrq->sg;
1619 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1621 mqrq->mmc_active.mrq = &brq->mrq;
1622 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1624 mmc_queue_bounce_pre(mqrq);
1627 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1628 struct mmc_blk_request *brq, struct request *req,
1629 int ret)
1631 struct mmc_queue_req *mq_rq;
1632 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1635 * If this is an SD card and we're writing, we can first
1636 * mark the known good sectors as ok.
1638 * If the card is not SD, we can still ok written sectors
1639 * as reported by the controller (which might be less than
1640 * the real number of written sectors, but never more).
1642 if (mmc_card_sd(card)) {
1643 u32 blocks;
1645 blocks = mmc_sd_num_wr_blocks(card);
1646 if (blocks != (u32)-1) {
1647 ret = blk_end_request(req, 0, blocks << 9);
1649 } else {
1650 if (!mmc_packed_cmd(mq_rq->cmd_type))
1651 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1653 return ret;
1656 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1658 struct request *prq;
1659 struct mmc_packed *packed = mq_rq->packed;
1660 int idx = packed->idx_failure, i = 0;
1661 int ret = 0;
1663 BUG_ON(!packed);
1665 while (!list_empty(&packed->list)) {
1666 prq = list_entry_rq(packed->list.next);
1667 if (idx == i) {
1668 /* retry from error index */
1669 packed->nr_entries -= idx;
1670 mq_rq->req = prq;
1671 ret = 1;
1673 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1674 list_del_init(&prq->queuelist);
1675 mmc_blk_clear_packed(mq_rq);
1677 return ret;
1679 list_del_init(&prq->queuelist);
1680 blk_end_request(prq, 0, blk_rq_bytes(prq));
1681 i++;
1684 mmc_blk_clear_packed(mq_rq);
1685 return ret;
1688 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1690 struct request *prq;
1691 struct mmc_packed *packed = mq_rq->packed;
1693 BUG_ON(!packed);
1695 while (!list_empty(&packed->list)) {
1696 prq = list_entry_rq(packed->list.next);
1697 list_del_init(&prq->queuelist);
1698 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1701 mmc_blk_clear_packed(mq_rq);
1704 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1705 struct mmc_queue_req *mq_rq)
1707 struct request *prq;
1708 struct request_queue *q = mq->queue;
1709 struct mmc_packed *packed = mq_rq->packed;
1711 BUG_ON(!packed);
1713 while (!list_empty(&packed->list)) {
1714 prq = list_entry_rq(packed->list.prev);
1715 if (prq->queuelist.prev != &packed->list) {
1716 list_del_init(&prq->queuelist);
1717 spin_lock_irq(q->queue_lock);
1718 blk_requeue_request(mq->queue, prq);
1719 spin_unlock_irq(q->queue_lock);
1720 } else {
1721 list_del_init(&prq->queuelist);
1725 mmc_blk_clear_packed(mq_rq);
1728 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1730 struct mmc_blk_data *md = mq->data;
1731 struct mmc_card *card = md->queue.card;
1732 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1733 int ret = 1, disable_multi = 0, retry = 0, type;
1734 enum mmc_blk_status status;
1735 struct mmc_queue_req *mq_rq;
1736 struct request *req = rqc;
1737 struct mmc_async_req *areq;
1738 const u8 packed_nr = 2;
1739 u8 reqs = 0;
1741 if (!rqc && !mq->mqrq_prev->req)
1742 return 0;
1744 if (rqc)
1745 reqs = mmc_blk_prep_packed_list(mq, rqc);
1747 do {
1748 if (rqc) {
1750 * When 4KB native sector is enabled, only 8 blocks
1751 * multiple read or write is allowed
1753 if ((brq->data.blocks & 0x07) &&
1754 (card->ext_csd.data_sector_size == 4096)) {
1755 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1756 req->rq_disk->disk_name);
1757 mq_rq = mq->mqrq_cur;
1758 goto cmd_abort;
1761 if (reqs >= packed_nr)
1762 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1763 card, mq);
1764 else
1765 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1766 areq = &mq->mqrq_cur->mmc_active;
1767 } else
1768 areq = NULL;
1769 areq = mmc_start_req(card->host, areq, (int *) &status);
1770 if (!areq) {
1771 if (status == MMC_BLK_NEW_REQUEST)
1772 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1773 return 0;
1776 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1777 brq = &mq_rq->brq;
1778 req = mq_rq->req;
1779 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1780 mmc_queue_bounce_post(mq_rq);
1782 switch (status) {
1783 case MMC_BLK_SUCCESS:
1784 case MMC_BLK_PARTIAL:
1786 * A block was successfully transferred.
1788 mmc_blk_reset_success(md, type);
1790 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1791 ret = mmc_blk_end_packed_req(mq_rq);
1792 break;
1793 } else {
1794 ret = blk_end_request(req, 0,
1795 brq->data.bytes_xfered);
1799 * If the blk_end_request function returns non-zero even
1800 * though all data has been transferred and no errors
1801 * were returned by the host controller, it's a bug.
1803 if (status == MMC_BLK_SUCCESS && ret) {
1804 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1805 __func__, blk_rq_bytes(req),
1806 brq->data.bytes_xfered);
1807 rqc = NULL;
1808 goto cmd_abort;
1810 break;
1811 case MMC_BLK_CMD_ERR:
1812 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1813 if (!mmc_blk_reset(md, card->host, type))
1814 break;
1815 goto cmd_abort;
1816 case MMC_BLK_RETRY:
1817 if (retry++ < 5)
1818 break;
1819 /* Fall through */
1820 case MMC_BLK_ABORT:
1821 if (!mmc_blk_reset(md, card->host, type))
1822 break;
1823 goto cmd_abort;
1824 case MMC_BLK_DATA_ERR: {
1825 int err;
1827 err = mmc_blk_reset(md, card->host, type);
1828 if (!err)
1829 break;
1830 if (err == -ENODEV ||
1831 mmc_packed_cmd(mq_rq->cmd_type))
1832 goto cmd_abort;
1833 /* Fall through */
1835 case MMC_BLK_ECC_ERR:
1836 if (brq->data.blocks > 1) {
1837 /* Redo read one sector at a time */
1838 pr_warning("%s: retrying using single block read\n",
1839 req->rq_disk->disk_name);
1840 disable_multi = 1;
1841 break;
1844 * After an error, we redo I/O one sector at a
1845 * time, so we only reach here after trying to
1846 * read a single sector.
1848 ret = blk_end_request(req, -EIO,
1849 brq->data.blksz);
1850 if (!ret)
1851 goto start_new_req;
1852 break;
1853 case MMC_BLK_NOMEDIUM:
1854 goto cmd_abort;
1855 default:
1856 pr_err("%s: Unhandled return value (%d)",
1857 req->rq_disk->disk_name, status);
1858 goto cmd_abort;
1861 if (ret) {
1862 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1863 if (!mq_rq->packed->retries)
1864 goto cmd_abort;
1865 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1866 mmc_start_req(card->host,
1867 &mq_rq->mmc_active, NULL);
1868 } else {
1871 * In case of a incomplete request
1872 * prepare it again and resend.
1874 mmc_blk_rw_rq_prep(mq_rq, card,
1875 disable_multi, mq);
1876 mmc_start_req(card->host,
1877 &mq_rq->mmc_active, NULL);
1880 } while (ret);
1882 return 1;
1884 cmd_abort:
1885 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1886 mmc_blk_abort_packed_req(mq_rq);
1887 } else {
1888 if (mmc_card_removed(card))
1889 req->cmd_flags |= REQ_QUIET;
1890 while (ret)
1891 ret = blk_end_request(req, -EIO,
1892 blk_rq_cur_bytes(req));
1895 start_new_req:
1896 if (rqc) {
1897 if (mmc_card_removed(card)) {
1898 rqc->cmd_flags |= REQ_QUIET;
1899 blk_end_request_all(rqc, -EIO);
1900 } else {
1902 * If current request is packed, it needs to put back.
1904 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1905 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1907 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1908 mmc_start_req(card->host,
1909 &mq->mqrq_cur->mmc_active, NULL);
1913 return 0;
1916 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1918 int ret;
1919 struct mmc_blk_data *md = mq->data;
1920 struct mmc_card *card = md->queue.card;
1921 struct mmc_host *host = card->host;
1922 unsigned long flags;
1924 if (req && !mq->mqrq_prev->req)
1925 /* claim host only for the first request */
1926 mmc_get_card(card);
1928 ret = mmc_blk_part_switch(card, md);
1929 if (ret) {
1930 if (req) {
1931 blk_end_request_all(req, -EIO);
1933 ret = 0;
1934 goto out;
1937 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
1938 if (req && req->cmd_flags & REQ_DISCARD) {
1939 /* complete ongoing async transfer before issuing discard */
1940 if (card->host->areq)
1941 mmc_blk_issue_rw_rq(mq, NULL);
1942 if (req->cmd_flags & REQ_SECURE &&
1943 !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1944 ret = mmc_blk_issue_secdiscard_rq(mq, req);
1945 else
1946 ret = mmc_blk_issue_discard_rq(mq, req);
1947 } else if (req && req->cmd_flags & REQ_FLUSH) {
1948 /* complete ongoing async transfer before issuing flush */
1949 if (card->host->areq)
1950 mmc_blk_issue_rw_rq(mq, NULL);
1951 ret = mmc_blk_issue_flush(mq, req);
1952 } else {
1953 if (!req && host->areq) {
1954 spin_lock_irqsave(&host->context_info.lock, flags);
1955 host->context_info.is_waiting_last_req = true;
1956 spin_unlock_irqrestore(&host->context_info.lock, flags);
1958 ret = mmc_blk_issue_rw_rq(mq, req);
1961 out:
1962 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
1963 (req && (req->cmd_flags & MMC_REQ_SPECIAL_MASK)))
1965 * Release host when there are no more requests
1966 * and after special request(discard, flush) is done.
1967 * In case sepecial request, there is no reentry to
1968 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
1970 mmc_put_card(card);
1971 return ret;
1974 static inline int mmc_blk_readonly(struct mmc_card *card)
1976 return mmc_card_readonly(card) ||
1977 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1980 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1981 struct device *parent,
1982 sector_t size,
1983 bool default_ro,
1984 const char *subname,
1985 int area_type)
1987 struct mmc_blk_data *md;
1988 int devidx, ret;
1990 devidx = find_first_zero_bit(dev_use, max_devices);
1991 if (devidx >= max_devices)
1992 return ERR_PTR(-ENOSPC);
1993 __set_bit(devidx, dev_use);
1995 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1996 if (!md) {
1997 ret = -ENOMEM;
1998 goto out;
2002 * !subname implies we are creating main mmc_blk_data that will be
2003 * associated with mmc_card with mmc_set_drvdata. Due to device
2004 * partitions, devidx will not coincide with a per-physical card
2005 * index anymore so we keep track of a name index.
2007 if (!subname) {
2008 md->name_idx = find_first_zero_bit(name_use, max_devices);
2009 __set_bit(md->name_idx, name_use);
2010 } else
2011 md->name_idx = ((struct mmc_blk_data *)
2012 dev_to_disk(parent)->private_data)->name_idx;
2014 md->area_type = area_type;
2017 * Set the read-only status based on the supported commands
2018 * and the write protect switch.
2020 md->read_only = mmc_blk_readonly(card);
2022 md->disk = alloc_disk(perdev_minors);
2023 if (md->disk == NULL) {
2024 ret = -ENOMEM;
2025 goto err_kfree;
2028 spin_lock_init(&md->lock);
2029 INIT_LIST_HEAD(&md->part);
2030 md->usage = 1;
2032 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2033 if (ret)
2034 goto err_putdisk;
2036 md->queue.issue_fn = mmc_blk_issue_rq;
2037 md->queue.data = md;
2039 md->disk->major = MMC_BLOCK_MAJOR;
2040 md->disk->first_minor = devidx * perdev_minors;
2041 md->disk->fops = &mmc_bdops;
2042 md->disk->private_data = md;
2043 md->disk->queue = md->queue.queue;
2044 md->disk->driverfs_dev = parent;
2045 set_disk_ro(md->disk, md->read_only || default_ro);
2046 if (area_type & MMC_BLK_DATA_AREA_RPMB)
2047 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2050 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2052 * - be set for removable media with permanent block devices
2053 * - be unset for removable block devices with permanent media
2055 * Since MMC block devices clearly fall under the second
2056 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2057 * should use the block device creation/destruction hotplug
2058 * messages to tell when the card is present.
2061 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2062 "mmcblk%d%s", md->name_idx, subname ? subname : "");
2064 if (mmc_card_mmc(card))
2065 blk_queue_logical_block_size(md->queue.queue,
2066 card->ext_csd.data_sector_size);
2067 else
2068 blk_queue_logical_block_size(md->queue.queue, 512);
2070 set_capacity(md->disk, size);
2072 if (mmc_host_cmd23(card->host)) {
2073 if (mmc_card_mmc(card) ||
2074 (mmc_card_sd(card) &&
2075 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2076 md->flags |= MMC_BLK_CMD23;
2079 if (mmc_card_mmc(card) &&
2080 md->flags & MMC_BLK_CMD23 &&
2081 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2082 card->ext_csd.rel_sectors)) {
2083 md->flags |= MMC_BLK_REL_WR;
2084 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2087 if (mmc_card_mmc(card) &&
2088 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2089 (md->flags & MMC_BLK_CMD23) &&
2090 card->ext_csd.packed_event_en) {
2091 if (!mmc_packed_init(&md->queue, card))
2092 md->flags |= MMC_BLK_PACKED_CMD;
2095 return md;
2097 err_putdisk:
2098 put_disk(md->disk);
2099 err_kfree:
2100 kfree(md);
2101 out:
2102 return ERR_PTR(ret);
2105 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2107 sector_t size;
2108 struct mmc_blk_data *md;
2110 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2112 * The EXT_CSD sector count is in number or 512 byte
2113 * sectors.
2115 size = card->ext_csd.sectors;
2116 } else {
2118 * The CSD capacity field is in units of read_blkbits.
2119 * set_capacity takes units of 512 bytes.
2121 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2124 md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2125 MMC_BLK_DATA_AREA_MAIN);
2126 return md;
2129 static int mmc_blk_alloc_part(struct mmc_card *card,
2130 struct mmc_blk_data *md,
2131 unsigned int part_type,
2132 sector_t size,
2133 bool default_ro,
2134 const char *subname,
2135 int area_type)
2137 char cap_str[10];
2138 struct mmc_blk_data *part_md;
2140 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2141 subname, area_type);
2142 if (IS_ERR(part_md))
2143 return PTR_ERR(part_md);
2144 part_md->part_type = part_type;
2145 list_add(&part_md->part, &md->part);
2147 string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2148 cap_str, sizeof(cap_str));
2149 pr_info("%s: %s %s partition %u %s\n",
2150 part_md->disk->disk_name, mmc_card_id(card),
2151 mmc_card_name(card), part_md->part_type, cap_str);
2152 return 0;
2155 /* MMC Physical partitions consist of two boot partitions and
2156 * up to four general purpose partitions.
2157 * For each partition enabled in EXT_CSD a block device will be allocatedi
2158 * to provide access to the partition.
2161 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2163 int idx, ret = 0;
2165 if (!mmc_card_mmc(card))
2166 return 0;
2168 for (idx = 0; idx < card->nr_parts; idx++) {
2169 if (card->part[idx].size) {
2170 ret = mmc_blk_alloc_part(card, md,
2171 card->part[idx].part_cfg,
2172 card->part[idx].size >> 9,
2173 card->part[idx].force_ro,
2174 card->part[idx].name,
2175 card->part[idx].area_type);
2176 if (ret)
2177 return ret;
2181 return ret;
2184 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2186 struct mmc_card *card;
2188 if (md) {
2190 * Flush remaining requests and free queues. It
2191 * is freeing the queue that stops new requests
2192 * from being accepted.
2194 mmc_cleanup_queue(&md->queue);
2195 if (md->flags & MMC_BLK_PACKED_CMD)
2196 mmc_packed_clean(&md->queue);
2197 card = md->queue.card;
2198 if (md->disk->flags & GENHD_FL_UP) {
2199 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2200 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2201 card->ext_csd.boot_ro_lockable)
2202 device_remove_file(disk_to_dev(md->disk),
2203 &md->power_ro_lock);
2205 del_gendisk(md->disk);
2207 mmc_blk_put(md);
2211 static void mmc_blk_remove_parts(struct mmc_card *card,
2212 struct mmc_blk_data *md)
2214 struct list_head *pos, *q;
2215 struct mmc_blk_data *part_md;
2217 __clear_bit(md->name_idx, name_use);
2218 list_for_each_safe(pos, q, &md->part) {
2219 part_md = list_entry(pos, struct mmc_blk_data, part);
2220 list_del(pos);
2221 mmc_blk_remove_req(part_md);
2225 static int mmc_add_disk(struct mmc_blk_data *md)
2227 int ret;
2228 struct mmc_card *card = md->queue.card;
2230 add_disk(md->disk);
2231 md->force_ro.show = force_ro_show;
2232 md->force_ro.store = force_ro_store;
2233 sysfs_attr_init(&md->force_ro.attr);
2234 md->force_ro.attr.name = "force_ro";
2235 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2236 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2237 if (ret)
2238 goto force_ro_fail;
2240 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2241 card->ext_csd.boot_ro_lockable) {
2242 umode_t mode;
2244 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2245 mode = S_IRUGO;
2246 else
2247 mode = S_IRUGO | S_IWUSR;
2249 md->power_ro_lock.show = power_ro_lock_show;
2250 md->power_ro_lock.store = power_ro_lock_store;
2251 sysfs_attr_init(&md->power_ro_lock.attr);
2252 md->power_ro_lock.attr.mode = mode;
2253 md->power_ro_lock.attr.name =
2254 "ro_lock_until_next_power_on";
2255 ret = device_create_file(disk_to_dev(md->disk),
2256 &md->power_ro_lock);
2257 if (ret)
2258 goto power_ro_lock_fail;
2260 return ret;
2262 power_ro_lock_fail:
2263 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2264 force_ro_fail:
2265 del_gendisk(md->disk);
2267 return ret;
2270 #define CID_MANFID_SANDISK 0x2
2271 #define CID_MANFID_TOSHIBA 0x11
2272 #define CID_MANFID_MICRON 0x13
2273 #define CID_MANFID_SAMSUNG 0x15
2275 static const struct mmc_fixup blk_fixups[] =
2277 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2278 MMC_QUIRK_INAND_CMD38),
2279 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2280 MMC_QUIRK_INAND_CMD38),
2281 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2282 MMC_QUIRK_INAND_CMD38),
2283 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2284 MMC_QUIRK_INAND_CMD38),
2285 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2286 MMC_QUIRK_INAND_CMD38),
2289 * Some MMC cards experience performance degradation with CMD23
2290 * instead of CMD12-bounded multiblock transfers. For now we'll
2291 * black list what's bad...
2292 * - Certain Toshiba cards.
2294 * N.B. This doesn't affect SD cards.
2296 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2297 MMC_QUIRK_BLK_NO_CMD23),
2298 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2299 MMC_QUIRK_BLK_NO_CMD23),
2300 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2301 MMC_QUIRK_BLK_NO_CMD23),
2304 * Some Micron MMC cards needs longer data read timeout than
2305 * indicated in CSD.
2307 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2308 MMC_QUIRK_LONG_READ_TIME),
2311 * On these Samsung MoviNAND parts, performing secure erase or
2312 * secure trim can result in unrecoverable corruption due to a
2313 * firmware bug.
2315 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2316 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2317 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2318 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2319 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2320 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2321 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2322 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2323 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2324 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2325 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2326 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2327 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2328 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2329 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2330 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2332 END_FIXUP
2335 static int mmc_blk_probe(struct mmc_card *card)
2337 struct mmc_blk_data *md, *part_md;
2338 char cap_str[10];
2341 * Check that the card supports the command class(es) we need.
2343 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2344 return -ENODEV;
2346 md = mmc_blk_alloc(card);
2347 if (IS_ERR(md))
2348 return PTR_ERR(md);
2350 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
2351 cap_str, sizeof(cap_str));
2352 pr_info("%s: %s %s %s %s\n",
2353 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2354 cap_str, md->read_only ? "(ro)" : "");
2356 if (mmc_blk_alloc_parts(card, md))
2357 goto out;
2359 mmc_set_drvdata(card, md);
2360 mmc_fixup_device(card, blk_fixups);
2362 if (mmc_add_disk(md))
2363 goto out;
2365 list_for_each_entry(part_md, &md->part, part) {
2366 if (mmc_add_disk(part_md))
2367 goto out;
2370 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2371 pm_runtime_use_autosuspend(&card->dev);
2374 * Don't enable runtime PM for SD-combo cards here. Leave that
2375 * decision to be taken during the SDIO init sequence instead.
2377 if (card->type != MMC_TYPE_SD_COMBO) {
2378 pm_runtime_set_active(&card->dev);
2379 pm_runtime_enable(&card->dev);
2382 return 0;
2384 out:
2385 mmc_blk_remove_parts(card, md);
2386 mmc_blk_remove_req(md);
2387 return 0;
2390 static void mmc_blk_remove(struct mmc_card *card)
2392 struct mmc_blk_data *md = mmc_get_drvdata(card);
2394 mmc_blk_remove_parts(card, md);
2395 pm_runtime_get_sync(&card->dev);
2396 mmc_claim_host(card->host);
2397 mmc_blk_part_switch(card, md);
2398 mmc_release_host(card->host);
2399 if (card->type != MMC_TYPE_SD_COMBO)
2400 pm_runtime_disable(&card->dev);
2401 pm_runtime_put_noidle(&card->dev);
2402 mmc_blk_remove_req(md);
2403 mmc_set_drvdata(card, NULL);
2406 static int _mmc_blk_suspend(struct mmc_card *card)
2408 struct mmc_blk_data *part_md;
2409 struct mmc_blk_data *md = mmc_get_drvdata(card);
2411 if (md) {
2412 pm_runtime_get_sync(&card->dev);
2413 mmc_queue_suspend(&md->queue);
2414 list_for_each_entry(part_md, &md->part, part) {
2415 mmc_queue_suspend(&part_md->queue);
2418 return 0;
2421 static void mmc_blk_shutdown(struct mmc_card *card)
2423 _mmc_blk_suspend(card);
2426 #ifdef CONFIG_PM
2427 static int mmc_blk_suspend(struct mmc_card *card)
2429 return _mmc_blk_suspend(card);
2432 static int mmc_blk_resume(struct mmc_card *card)
2434 struct mmc_blk_data *part_md;
2435 struct mmc_blk_data *md = mmc_get_drvdata(card);
2437 if (md) {
2439 * Resume involves the card going into idle state,
2440 * so current partition is always the main one.
2442 md->part_curr = md->part_type;
2443 mmc_queue_resume(&md->queue);
2444 list_for_each_entry(part_md, &md->part, part) {
2445 mmc_queue_resume(&part_md->queue);
2447 pm_runtime_put(&card->dev);
2449 return 0;
2451 #else
2452 #define mmc_blk_suspend NULL
2453 #define mmc_blk_resume NULL
2454 #endif
2456 static struct mmc_driver mmc_driver = {
2457 .drv = {
2458 .name = "mmcblk",
2460 .probe = mmc_blk_probe,
2461 .remove = mmc_blk_remove,
2462 .suspend = mmc_blk_suspend,
2463 .resume = mmc_blk_resume,
2464 .shutdown = mmc_blk_shutdown,
2467 static int __init mmc_blk_init(void)
2469 int res;
2471 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2472 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2474 max_devices = 256 / perdev_minors;
2476 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2477 if (res)
2478 goto out;
2480 res = mmc_register_driver(&mmc_driver);
2481 if (res)
2482 goto out2;
2484 return 0;
2485 out2:
2486 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2487 out:
2488 return res;
2491 static void __exit mmc_blk_exit(void)
2493 mmc_unregister_driver(&mmc_driver);
2494 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2497 module_init(mmc_blk_init);
2498 module_exit(mmc_blk_exit);
2500 MODULE_LICENSE("GPL");
2501 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");