irqchip/s3c24xx: Mark init_eint as __maybe_unused
[linux/fpc-iii.git] / drivers / mmc / card / block.c
blobd8486168415ae1123f6a31d7126cfc9192fe884b
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");
51 #ifdef KERNEL
52 #ifdef MODULE_PARAM_PREFIX
53 #undef MODULE_PARAM_PREFIX
54 #endif
55 #define MODULE_PARAM_PREFIX "mmcblk."
56 #endif
58 #define INAND_CMD38_ARG_EXT_CSD 113
59 #define INAND_CMD38_ARG_ERASE 0x00
60 #define INAND_CMD38_ARG_TRIM 0x01
61 #define INAND_CMD38_ARG_SECERASE 0x80
62 #define INAND_CMD38_ARG_SECTRIM1 0x81
63 #define INAND_CMD38_ARG_SECTRIM2 0x88
64 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
65 #define MMC_SANITIZE_REQ_TIMEOUT 240000
66 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
68 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
69 (rq_data_dir(req) == WRITE))
70 #define PACKED_CMD_VER 0x01
71 #define PACKED_CMD_WR 0x02
73 static DEFINE_MUTEX(block_mutex);
76 * The defaults come from config options but can be overriden by module
77 * or bootarg options.
79 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
82 * We've only got one major, so number of mmcblk devices is
83 * limited to (1 << 20) / number of minors per device. It is also
84 * currently limited by the size of the static bitmaps below.
86 static int max_devices;
88 #define MAX_DEVICES 256
90 /* TODO: Replace these with struct ida */
91 static DECLARE_BITMAP(dev_use, MAX_DEVICES);
92 static DECLARE_BITMAP(name_use, MAX_DEVICES);
95 * There is one mmc_blk_data per slot.
97 struct mmc_blk_data {
98 spinlock_t lock;
99 struct gendisk *disk;
100 struct mmc_queue queue;
101 struct list_head part;
103 unsigned int flags;
104 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
105 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
106 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
108 unsigned int usage;
109 unsigned int read_only;
110 unsigned int part_type;
111 unsigned int name_idx;
112 unsigned int reset_done;
113 #define MMC_BLK_READ BIT(0)
114 #define MMC_BLK_WRITE BIT(1)
115 #define MMC_BLK_DISCARD BIT(2)
116 #define MMC_BLK_SECDISCARD BIT(3)
119 * Only set in main mmc_blk_data associated
120 * with mmc_card with dev_set_drvdata, and keeps
121 * track of the current selected device partition.
123 unsigned int part_curr;
124 struct device_attribute force_ro;
125 struct device_attribute power_ro_lock;
126 int area_type;
129 static DEFINE_MUTEX(open_lock);
131 enum {
132 MMC_PACKED_NR_IDX = -1,
133 MMC_PACKED_NR_ZERO,
134 MMC_PACKED_NR_SINGLE,
137 module_param(perdev_minors, int, 0444);
138 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
140 static inline int mmc_blk_part_switch(struct mmc_card *card,
141 struct mmc_blk_data *md);
142 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
144 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
146 struct mmc_packed *packed = mqrq->packed;
148 BUG_ON(!packed);
150 mqrq->cmd_type = MMC_PACKED_NONE;
151 packed->nr_entries = MMC_PACKED_NR_ZERO;
152 packed->idx_failure = MMC_PACKED_NR_IDX;
153 packed->retries = 0;
154 packed->blocks = 0;
157 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
159 struct mmc_blk_data *md;
161 mutex_lock(&open_lock);
162 md = disk->private_data;
163 if (md && md->usage == 0)
164 md = NULL;
165 if (md)
166 md->usage++;
167 mutex_unlock(&open_lock);
169 return md;
172 static inline int mmc_get_devidx(struct gendisk *disk)
174 int devmaj = MAJOR(disk_devt(disk));
175 int devidx = MINOR(disk_devt(disk)) / perdev_minors;
177 if (!devmaj)
178 devidx = disk->first_minor / perdev_minors;
179 return devidx;
182 static void mmc_blk_put(struct mmc_blk_data *md)
184 mutex_lock(&open_lock);
185 md->usage--;
186 if (md->usage == 0) {
187 int devidx = mmc_get_devidx(md->disk);
188 blk_cleanup_queue(md->queue.queue);
190 __clear_bit(devidx, dev_use);
192 put_disk(md->disk);
193 kfree(md);
195 mutex_unlock(&open_lock);
198 static ssize_t power_ro_lock_show(struct device *dev,
199 struct device_attribute *attr, char *buf)
201 int ret;
202 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
203 struct mmc_card *card = md->queue.card;
204 int locked = 0;
206 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
207 locked = 2;
208 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
209 locked = 1;
211 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
213 mmc_blk_put(md);
215 return ret;
218 static ssize_t power_ro_lock_store(struct device *dev,
219 struct device_attribute *attr, const char *buf, size_t count)
221 int ret;
222 struct mmc_blk_data *md, *part_md;
223 struct mmc_card *card;
224 unsigned long set;
226 if (kstrtoul(buf, 0, &set))
227 return -EINVAL;
229 if (set != 1)
230 return count;
232 md = mmc_blk_get(dev_to_disk(dev));
233 card = md->queue.card;
235 mmc_get_card(card);
237 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
238 card->ext_csd.boot_ro_lock |
239 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
240 card->ext_csd.part_time);
241 if (ret)
242 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
243 else
244 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
246 mmc_put_card(card);
248 if (!ret) {
249 pr_info("%s: Locking boot partition ro until next power on\n",
250 md->disk->disk_name);
251 set_disk_ro(md->disk, 1);
253 list_for_each_entry(part_md, &md->part, part)
254 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
255 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
256 set_disk_ro(part_md->disk, 1);
260 mmc_blk_put(md);
261 return count;
264 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
265 char *buf)
267 int ret;
268 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
270 ret = snprintf(buf, PAGE_SIZE, "%d\n",
271 get_disk_ro(dev_to_disk(dev)) ^
272 md->read_only);
273 mmc_blk_put(md);
274 return ret;
277 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
278 const char *buf, size_t count)
280 int ret;
281 char *end;
282 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
283 unsigned long set = simple_strtoul(buf, &end, 0);
284 if (end == buf) {
285 ret = -EINVAL;
286 goto out;
289 set_disk_ro(dev_to_disk(dev), set || md->read_only);
290 ret = count;
291 out:
292 mmc_blk_put(md);
293 return ret;
296 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
298 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
299 int ret = -ENXIO;
301 mutex_lock(&block_mutex);
302 if (md) {
303 if (md->usage == 2)
304 check_disk_change(bdev);
305 ret = 0;
307 if ((mode & FMODE_WRITE) && md->read_only) {
308 mmc_blk_put(md);
309 ret = -EROFS;
312 mutex_unlock(&block_mutex);
314 return ret;
317 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
319 struct mmc_blk_data *md = disk->private_data;
321 mutex_lock(&block_mutex);
322 mmc_blk_put(md);
323 mutex_unlock(&block_mutex);
326 static int
327 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
329 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
330 geo->heads = 4;
331 geo->sectors = 16;
332 return 0;
335 struct mmc_blk_ioc_data {
336 struct mmc_ioc_cmd ic;
337 unsigned char *buf;
338 u64 buf_bytes;
341 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
342 struct mmc_ioc_cmd __user *user)
344 struct mmc_blk_ioc_data *idata;
345 int err;
347 idata = kzalloc(sizeof(*idata), GFP_KERNEL);
348 if (!idata) {
349 err = -ENOMEM;
350 goto out;
353 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
354 err = -EFAULT;
355 goto idata_err;
358 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
359 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
360 err = -EOVERFLOW;
361 goto idata_err;
364 if (!idata->buf_bytes)
365 return idata;
367 idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
368 if (!idata->buf) {
369 err = -ENOMEM;
370 goto idata_err;
373 if (copy_from_user(idata->buf, (void __user *)(unsigned long)
374 idata->ic.data_ptr, idata->buf_bytes)) {
375 err = -EFAULT;
376 goto copy_err;
379 return idata;
381 copy_err:
382 kfree(idata->buf);
383 idata_err:
384 kfree(idata);
385 out:
386 return ERR_PTR(err);
389 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
390 struct mmc_blk_ioc_data *idata)
392 struct mmc_ioc_cmd *ic = &idata->ic;
394 if (copy_to_user(&(ic_ptr->response), ic->response,
395 sizeof(ic->response)))
396 return -EFAULT;
398 if (!idata->ic.write_flag) {
399 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
400 idata->buf, idata->buf_bytes))
401 return -EFAULT;
404 return 0;
407 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
408 u32 retries_max)
410 int err;
411 u32 retry_count = 0;
413 if (!status || !retries_max)
414 return -EINVAL;
416 do {
417 err = get_card_status(card, status, 5);
418 if (err)
419 break;
421 if (!R1_STATUS(*status) &&
422 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
423 break; /* RPMB programming operation complete */
426 * Rechedule to give the MMC device a chance to continue
427 * processing the previous command without being polled too
428 * frequently.
430 usleep_range(1000, 5000);
431 } while (++retry_count < retries_max);
433 if (retry_count == retries_max)
434 err = -EPERM;
436 return err;
439 static int ioctl_do_sanitize(struct mmc_card *card)
441 int err;
443 if (!mmc_can_sanitize(card)) {
444 pr_warn("%s: %s - SANITIZE is not supported\n",
445 mmc_hostname(card->host), __func__);
446 err = -EOPNOTSUPP;
447 goto out;
450 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
451 mmc_hostname(card->host), __func__);
453 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
454 EXT_CSD_SANITIZE_START, 1,
455 MMC_SANITIZE_REQ_TIMEOUT);
457 if (err)
458 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
459 mmc_hostname(card->host), __func__, err);
461 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
462 __func__);
463 out:
464 return err;
467 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
468 struct mmc_blk_ioc_data *idata)
470 struct mmc_command cmd = {0};
471 struct mmc_data data = {0};
472 struct mmc_request mrq = {NULL};
473 struct scatterlist sg;
474 int err;
475 int is_rpmb = false;
476 u32 status = 0;
478 if (!card || !md || !idata)
479 return -EINVAL;
481 if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
482 is_rpmb = true;
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 err = mmc_blk_part_switch(card, md);
527 if (err)
528 return err;
530 if (idata->ic.is_acmd) {
531 err = mmc_app_cmd(card->host, card);
532 if (err)
533 return err;
536 if (is_rpmb) {
537 err = mmc_set_blockcount(card, data.blocks,
538 idata->ic.write_flag & (1 << 31));
539 if (err)
540 return err;
543 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
544 (cmd.opcode == MMC_SWITCH)) {
545 err = ioctl_do_sanitize(card);
547 if (err)
548 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
549 __func__, err);
551 return err;
554 mmc_wait_for_req(card->host, &mrq);
556 if (cmd.error) {
557 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
558 __func__, cmd.error);
559 return cmd.error;
561 if (data.error) {
562 dev_err(mmc_dev(card->host), "%s: data error %d\n",
563 __func__, data.error);
564 return data.error;
568 * According to the SD specs, some commands require a delay after
569 * issuing the command.
571 if (idata->ic.postsleep_min_us)
572 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
574 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
576 if (is_rpmb) {
578 * Ensure RPMB command has completed by polling CMD13
579 * "Send Status".
581 err = ioctl_rpmb_card_status_poll(card, &status, 5);
582 if (err)
583 dev_err(mmc_dev(card->host),
584 "%s: Card Status=0x%08X, error %d\n",
585 __func__, status, err);
588 return err;
591 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
592 struct mmc_ioc_cmd __user *ic_ptr)
594 struct mmc_blk_ioc_data *idata;
595 struct mmc_blk_data *md;
596 struct mmc_card *card;
597 int err = 0, ioc_err = 0;
599 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
600 if (IS_ERR(idata))
601 return PTR_ERR(idata);
603 md = mmc_blk_get(bdev->bd_disk);
604 if (!md) {
605 err = -EINVAL;
606 goto cmd_err;
609 card = md->queue.card;
610 if (IS_ERR(card)) {
611 err = PTR_ERR(card);
612 goto cmd_done;
615 mmc_get_card(card);
617 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata);
619 mmc_put_card(card);
621 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
623 cmd_done:
624 mmc_blk_put(md);
625 cmd_err:
626 kfree(idata->buf);
627 kfree(idata);
628 return ioc_err ? ioc_err : err;
631 static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev,
632 struct mmc_ioc_multi_cmd __user *user)
634 struct mmc_blk_ioc_data **idata = NULL;
635 struct mmc_ioc_cmd __user *cmds = user->cmds;
636 struct mmc_card *card;
637 struct mmc_blk_data *md;
638 int i, err = 0, ioc_err = 0;
639 __u64 num_of_cmds;
641 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
642 sizeof(num_of_cmds)))
643 return -EFAULT;
645 if (num_of_cmds > MMC_IOC_MAX_CMDS)
646 return -EINVAL;
648 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
649 if (!idata)
650 return -ENOMEM;
652 for (i = 0; i < num_of_cmds; i++) {
653 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
654 if (IS_ERR(idata[i])) {
655 err = PTR_ERR(idata[i]);
656 num_of_cmds = i;
657 goto cmd_err;
661 md = mmc_blk_get(bdev->bd_disk);
662 if (!md)
663 goto cmd_err;
665 card = md->queue.card;
666 if (IS_ERR(card)) {
667 err = PTR_ERR(card);
668 goto cmd_done;
671 mmc_get_card(card);
673 for (i = 0; i < num_of_cmds && !ioc_err; i++)
674 ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]);
676 mmc_put_card(card);
678 /* copy to user if data and response */
679 for (i = 0; i < num_of_cmds && !err; i++)
680 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
682 cmd_done:
683 mmc_blk_put(md);
684 cmd_err:
685 for (i = 0; i < num_of_cmds; i++) {
686 kfree(idata[i]->buf);
687 kfree(idata[i]);
689 kfree(idata);
690 return ioc_err ? ioc_err : err;
693 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
694 unsigned int cmd, unsigned long arg)
697 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
698 * whole block device, not on a partition. This prevents overspray
699 * between sibling partitions.
701 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
702 return -EPERM;
704 switch (cmd) {
705 case MMC_IOC_CMD:
706 return mmc_blk_ioctl_cmd(bdev,
707 (struct mmc_ioc_cmd __user *)arg);
708 case MMC_IOC_MULTI_CMD:
709 return mmc_blk_ioctl_multi_cmd(bdev,
710 (struct mmc_ioc_multi_cmd __user *)arg);
711 default:
712 return -EINVAL;
716 #ifdef CONFIG_COMPAT
717 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
718 unsigned int cmd, unsigned long arg)
720 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
722 #endif
724 static const struct block_device_operations mmc_bdops = {
725 .open = mmc_blk_open,
726 .release = mmc_blk_release,
727 .getgeo = mmc_blk_getgeo,
728 .owner = THIS_MODULE,
729 .ioctl = mmc_blk_ioctl,
730 #ifdef CONFIG_COMPAT
731 .compat_ioctl = mmc_blk_compat_ioctl,
732 #endif
735 static inline int mmc_blk_part_switch(struct mmc_card *card,
736 struct mmc_blk_data *md)
738 int ret;
739 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
741 if (main_md->part_curr == md->part_type)
742 return 0;
744 if (mmc_card_mmc(card)) {
745 u8 part_config = card->ext_csd.part_config;
747 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
748 part_config |= md->part_type;
750 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
751 EXT_CSD_PART_CONFIG, part_config,
752 card->ext_csd.part_time);
753 if (ret)
754 return ret;
756 card->ext_csd.part_config = part_config;
759 main_md->part_curr = md->part_type;
760 return 0;
763 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
765 int err;
766 u32 result;
767 __be32 *blocks;
769 struct mmc_request mrq = {NULL};
770 struct mmc_command cmd = {0};
771 struct mmc_data data = {0};
773 struct scatterlist sg;
775 cmd.opcode = MMC_APP_CMD;
776 cmd.arg = card->rca << 16;
777 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
779 err = mmc_wait_for_cmd(card->host, &cmd, 0);
780 if (err)
781 return (u32)-1;
782 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
783 return (u32)-1;
785 memset(&cmd, 0, sizeof(struct mmc_command));
787 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
788 cmd.arg = 0;
789 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
791 data.blksz = 4;
792 data.blocks = 1;
793 data.flags = MMC_DATA_READ;
794 data.sg = &sg;
795 data.sg_len = 1;
796 mmc_set_data_timeout(&data, card);
798 mrq.cmd = &cmd;
799 mrq.data = &data;
801 blocks = kmalloc(4, GFP_KERNEL);
802 if (!blocks)
803 return (u32)-1;
805 sg_init_one(&sg, blocks, 4);
807 mmc_wait_for_req(card->host, &mrq);
809 result = ntohl(*blocks);
810 kfree(blocks);
812 if (cmd.error || data.error)
813 result = (u32)-1;
815 return result;
818 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
820 struct mmc_command cmd = {0};
821 int err;
823 cmd.opcode = MMC_SEND_STATUS;
824 if (!mmc_host_is_spi(card->host))
825 cmd.arg = card->rca << 16;
826 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
827 err = mmc_wait_for_cmd(card->host, &cmd, retries);
828 if (err == 0)
829 *status = cmd.resp[0];
830 return err;
833 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
834 bool hw_busy_detect, struct request *req, int *gen_err)
836 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
837 int err = 0;
838 u32 status;
840 do {
841 err = get_card_status(card, &status, 5);
842 if (err) {
843 pr_err("%s: error %d requesting status\n",
844 req->rq_disk->disk_name, err);
845 return err;
848 if (status & R1_ERROR) {
849 pr_err("%s: %s: error sending status cmd, status %#x\n",
850 req->rq_disk->disk_name, __func__, status);
851 *gen_err = 1;
854 /* We may rely on the host hw to handle busy detection.*/
855 if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
856 hw_busy_detect)
857 break;
860 * Timeout if the device never becomes ready for data and never
861 * leaves the program state.
863 if (time_after(jiffies, timeout)) {
864 pr_err("%s: Card stuck in programming state! %s %s\n",
865 mmc_hostname(card->host),
866 req->rq_disk->disk_name, __func__);
867 return -ETIMEDOUT;
871 * Some cards mishandle the status bits,
872 * so make sure to check both the busy
873 * indication and the card state.
875 } while (!(status & R1_READY_FOR_DATA) ||
876 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
878 return err;
881 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
882 struct request *req, int *gen_err, u32 *stop_status)
884 struct mmc_host *host = card->host;
885 struct mmc_command cmd = {0};
886 int err;
887 bool use_r1b_resp = rq_data_dir(req) == WRITE;
890 * Normally we use R1B responses for WRITE, but in cases where the host
891 * has specified a max_busy_timeout we need to validate it. A failure
892 * means we need to prevent the host from doing hw busy detection, which
893 * is done by converting to a R1 response instead.
895 if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
896 use_r1b_resp = false;
898 cmd.opcode = MMC_STOP_TRANSMISSION;
899 if (use_r1b_resp) {
900 cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
901 cmd.busy_timeout = timeout_ms;
902 } else {
903 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
906 err = mmc_wait_for_cmd(host, &cmd, 5);
907 if (err)
908 return err;
910 *stop_status = cmd.resp[0];
912 /* No need to check card status in case of READ. */
913 if (rq_data_dir(req) == READ)
914 return 0;
916 if (!mmc_host_is_spi(host) &&
917 (*stop_status & R1_ERROR)) {
918 pr_err("%s: %s: general error sending stop command, resp %#x\n",
919 req->rq_disk->disk_name, __func__, *stop_status);
920 *gen_err = 1;
923 return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
926 #define ERR_NOMEDIUM 3
927 #define ERR_RETRY 2
928 #define ERR_ABORT 1
929 #define ERR_CONTINUE 0
931 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
932 bool status_valid, u32 status)
934 switch (error) {
935 case -EILSEQ:
936 /* response crc error, retry the r/w cmd */
937 pr_err("%s: %s sending %s command, card status %#x\n",
938 req->rq_disk->disk_name, "response CRC error",
939 name, status);
940 return ERR_RETRY;
942 case -ETIMEDOUT:
943 pr_err("%s: %s sending %s command, card status %#x\n",
944 req->rq_disk->disk_name, "timed out", name, status);
946 /* If the status cmd initially failed, retry the r/w cmd */
947 if (!status_valid)
948 return ERR_RETRY;
951 * If it was a r/w cmd crc error, or illegal command
952 * (eg, issued in wrong state) then retry - we should
953 * have corrected the state problem above.
955 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
956 return ERR_RETRY;
958 /* Otherwise abort the command */
959 return ERR_ABORT;
961 default:
962 /* We don't understand the error code the driver gave us */
963 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
964 req->rq_disk->disk_name, error, status);
965 return ERR_ABORT;
970 * Initial r/w and stop cmd error recovery.
971 * We don't know whether the card received the r/w cmd or not, so try to
972 * restore things back to a sane state. Essentially, we do this as follows:
973 * - Obtain card status. If the first attempt to obtain card status fails,
974 * the status word will reflect the failed status cmd, not the failed
975 * r/w cmd. If we fail to obtain card status, it suggests we can no
976 * longer communicate with the card.
977 * - Check the card state. If the card received the cmd but there was a
978 * transient problem with the response, it might still be in a data transfer
979 * mode. Try to send it a stop command. If this fails, we can't recover.
980 * - If the r/w cmd failed due to a response CRC error, it was probably
981 * transient, so retry the cmd.
982 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
983 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
984 * illegal cmd, retry.
985 * Otherwise we don't understand what happened, so abort.
987 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
988 struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
990 bool prev_cmd_status_valid = true;
991 u32 status, stop_status = 0;
992 int err, retry;
994 if (mmc_card_removed(card))
995 return ERR_NOMEDIUM;
998 * Try to get card status which indicates both the card state
999 * and why there was no response. If the first attempt fails,
1000 * we can't be sure the returned status is for the r/w command.
1002 for (retry = 2; retry >= 0; retry--) {
1003 err = get_card_status(card, &status, 0);
1004 if (!err)
1005 break;
1007 /* Re-tune if needed */
1008 mmc_retune_recheck(card->host);
1010 prev_cmd_status_valid = false;
1011 pr_err("%s: error %d sending status command, %sing\n",
1012 req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1015 /* We couldn't get a response from the card. Give up. */
1016 if (err) {
1017 /* Check if the card is removed */
1018 if (mmc_detect_card_removed(card->host))
1019 return ERR_NOMEDIUM;
1020 return ERR_ABORT;
1023 /* Flag ECC errors */
1024 if ((status & R1_CARD_ECC_FAILED) ||
1025 (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1026 (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1027 *ecc_err = 1;
1029 /* Flag General errors */
1030 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1031 if ((status & R1_ERROR) ||
1032 (brq->stop.resp[0] & R1_ERROR)) {
1033 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1034 req->rq_disk->disk_name, __func__,
1035 brq->stop.resp[0], status);
1036 *gen_err = 1;
1040 * Check the current card state. If it is in some data transfer
1041 * mode, tell it to stop (and hopefully transition back to TRAN.)
1043 if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1044 R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1045 err = send_stop(card,
1046 DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1047 req, gen_err, &stop_status);
1048 if (err) {
1049 pr_err("%s: error %d sending stop command\n",
1050 req->rq_disk->disk_name, err);
1052 * If the stop cmd also timed out, the card is probably
1053 * not present, so abort. Other errors are bad news too.
1055 return ERR_ABORT;
1058 if (stop_status & R1_CARD_ECC_FAILED)
1059 *ecc_err = 1;
1062 /* Check for set block count errors */
1063 if (brq->sbc.error)
1064 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1065 prev_cmd_status_valid, status);
1067 /* Check for r/w command errors */
1068 if (brq->cmd.error)
1069 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1070 prev_cmd_status_valid, status);
1072 /* Data errors */
1073 if (!brq->stop.error)
1074 return ERR_CONTINUE;
1076 /* Now for stop errors. These aren't fatal to the transfer. */
1077 pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1078 req->rq_disk->disk_name, brq->stop.error,
1079 brq->cmd.resp[0], status);
1082 * Subsitute in our own stop status as this will give the error
1083 * state which happened during the execution of the r/w command.
1085 if (stop_status) {
1086 brq->stop.resp[0] = stop_status;
1087 brq->stop.error = 0;
1089 return ERR_CONTINUE;
1092 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1093 int type)
1095 int err;
1097 if (md->reset_done & type)
1098 return -EEXIST;
1100 md->reset_done |= type;
1101 err = mmc_hw_reset(host);
1102 /* Ensure we switch back to the correct partition */
1103 if (err != -EOPNOTSUPP) {
1104 struct mmc_blk_data *main_md =
1105 dev_get_drvdata(&host->card->dev);
1106 int part_err;
1108 main_md->part_curr = main_md->part_type;
1109 part_err = mmc_blk_part_switch(host->card, md);
1110 if (part_err) {
1112 * We have failed to get back into the correct
1113 * partition, so we need to abort the whole request.
1115 return -ENODEV;
1118 return err;
1121 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1123 md->reset_done &= ~type;
1126 int mmc_access_rpmb(struct mmc_queue *mq)
1128 struct mmc_blk_data *md = mq->data;
1130 * If this is a RPMB partition access, return ture
1132 if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1133 return true;
1135 return false;
1138 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1140 struct mmc_blk_data *md = mq->data;
1141 struct mmc_card *card = md->queue.card;
1142 unsigned int from, nr, arg;
1143 int err = 0, type = MMC_BLK_DISCARD;
1145 if (!mmc_can_erase(card)) {
1146 err = -EOPNOTSUPP;
1147 goto out;
1150 from = blk_rq_pos(req);
1151 nr = blk_rq_sectors(req);
1153 if (mmc_can_discard(card))
1154 arg = MMC_DISCARD_ARG;
1155 else if (mmc_can_trim(card))
1156 arg = MMC_TRIM_ARG;
1157 else
1158 arg = MMC_ERASE_ARG;
1159 retry:
1160 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1161 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1162 INAND_CMD38_ARG_EXT_CSD,
1163 arg == MMC_TRIM_ARG ?
1164 INAND_CMD38_ARG_TRIM :
1165 INAND_CMD38_ARG_ERASE,
1167 if (err)
1168 goto out;
1170 err = mmc_erase(card, from, nr, arg);
1171 out:
1172 if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1173 goto retry;
1174 if (!err)
1175 mmc_blk_reset_success(md, type);
1176 blk_end_request(req, err, blk_rq_bytes(req));
1178 return err ? 0 : 1;
1181 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1182 struct request *req)
1184 struct mmc_blk_data *md = mq->data;
1185 struct mmc_card *card = md->queue.card;
1186 unsigned int from, nr, arg;
1187 int err = 0, type = MMC_BLK_SECDISCARD;
1189 if (!(mmc_can_secure_erase_trim(card))) {
1190 err = -EOPNOTSUPP;
1191 goto out;
1194 from = blk_rq_pos(req);
1195 nr = blk_rq_sectors(req);
1197 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1198 arg = MMC_SECURE_TRIM1_ARG;
1199 else
1200 arg = MMC_SECURE_ERASE_ARG;
1202 retry:
1203 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1204 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1205 INAND_CMD38_ARG_EXT_CSD,
1206 arg == MMC_SECURE_TRIM1_ARG ?
1207 INAND_CMD38_ARG_SECTRIM1 :
1208 INAND_CMD38_ARG_SECERASE,
1210 if (err)
1211 goto out_retry;
1214 err = mmc_erase(card, from, nr, arg);
1215 if (err == -EIO)
1216 goto out_retry;
1217 if (err)
1218 goto out;
1220 if (arg == MMC_SECURE_TRIM1_ARG) {
1221 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1222 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1223 INAND_CMD38_ARG_EXT_CSD,
1224 INAND_CMD38_ARG_SECTRIM2,
1226 if (err)
1227 goto out_retry;
1230 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1231 if (err == -EIO)
1232 goto out_retry;
1233 if (err)
1234 goto out;
1237 out_retry:
1238 if (err && !mmc_blk_reset(md, card->host, type))
1239 goto retry;
1240 if (!err)
1241 mmc_blk_reset_success(md, type);
1242 out:
1243 blk_end_request(req, err, blk_rq_bytes(req));
1245 return err ? 0 : 1;
1248 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1250 struct mmc_blk_data *md = mq->data;
1251 struct mmc_card *card = md->queue.card;
1252 int ret = 0;
1254 ret = mmc_flush_cache(card);
1255 if (ret)
1256 ret = -EIO;
1258 blk_end_request_all(req, ret);
1260 return ret ? 0 : 1;
1264 * Reformat current write as a reliable write, supporting
1265 * both legacy and the enhanced reliable write MMC cards.
1266 * In each transfer we'll handle only as much as a single
1267 * reliable write can handle, thus finish the request in
1268 * partial completions.
1270 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1271 struct mmc_card *card,
1272 struct request *req)
1274 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1275 /* Legacy mode imposes restrictions on transfers. */
1276 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1277 brq->data.blocks = 1;
1279 if (brq->data.blocks > card->ext_csd.rel_sectors)
1280 brq->data.blocks = card->ext_csd.rel_sectors;
1281 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1282 brq->data.blocks = 1;
1286 #define CMD_ERRORS \
1287 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1288 R1_ADDRESS_ERROR | /* Misaligned address */ \
1289 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1290 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1291 R1_CC_ERROR | /* Card controller error */ \
1292 R1_ERROR) /* General/unknown error */
1294 static int mmc_blk_err_check(struct mmc_card *card,
1295 struct mmc_async_req *areq)
1297 struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1298 mmc_active);
1299 struct mmc_blk_request *brq = &mq_mrq->brq;
1300 struct request *req = mq_mrq->req;
1301 int need_retune = card->host->need_retune;
1302 int ecc_err = 0, gen_err = 0;
1305 * sbc.error indicates a problem with the set block count
1306 * command. No data will have been transferred.
1308 * cmd.error indicates a problem with the r/w command. No
1309 * data will have been transferred.
1311 * stop.error indicates a problem with the stop command. Data
1312 * may have been transferred, or may still be transferring.
1314 if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1315 brq->data.error) {
1316 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1317 case ERR_RETRY:
1318 return MMC_BLK_RETRY;
1319 case ERR_ABORT:
1320 return MMC_BLK_ABORT;
1321 case ERR_NOMEDIUM:
1322 return MMC_BLK_NOMEDIUM;
1323 case ERR_CONTINUE:
1324 break;
1329 * Check for errors relating to the execution of the
1330 * initial command - such as address errors. No data
1331 * has been transferred.
1333 if (brq->cmd.resp[0] & CMD_ERRORS) {
1334 pr_err("%s: r/w command failed, status = %#x\n",
1335 req->rq_disk->disk_name, brq->cmd.resp[0]);
1336 return MMC_BLK_ABORT;
1340 * Everything else is either success, or a data error of some
1341 * kind. If it was a write, we may have transitioned to
1342 * program mode, which we have to wait for it to complete.
1344 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1345 int err;
1347 /* Check stop command response */
1348 if (brq->stop.resp[0] & R1_ERROR) {
1349 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1350 req->rq_disk->disk_name, __func__,
1351 brq->stop.resp[0]);
1352 gen_err = 1;
1355 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1356 &gen_err);
1357 if (err)
1358 return MMC_BLK_CMD_ERR;
1361 /* if general error occurs, retry the write operation. */
1362 if (gen_err) {
1363 pr_warn("%s: retrying write for general error\n",
1364 req->rq_disk->disk_name);
1365 return MMC_BLK_RETRY;
1368 if (brq->data.error) {
1369 if (need_retune && !brq->retune_retry_done) {
1370 pr_info("%s: retrying because a re-tune was needed\n",
1371 req->rq_disk->disk_name);
1372 brq->retune_retry_done = 1;
1373 return MMC_BLK_RETRY;
1375 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1376 req->rq_disk->disk_name, brq->data.error,
1377 (unsigned)blk_rq_pos(req),
1378 (unsigned)blk_rq_sectors(req),
1379 brq->cmd.resp[0], brq->stop.resp[0]);
1381 if (rq_data_dir(req) == READ) {
1382 if (ecc_err)
1383 return MMC_BLK_ECC_ERR;
1384 return MMC_BLK_DATA_ERR;
1385 } else {
1386 return MMC_BLK_CMD_ERR;
1390 if (!brq->data.bytes_xfered)
1391 return MMC_BLK_RETRY;
1393 if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1394 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1395 return MMC_BLK_PARTIAL;
1396 else
1397 return MMC_BLK_SUCCESS;
1400 if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1401 return MMC_BLK_PARTIAL;
1403 return MMC_BLK_SUCCESS;
1406 static int mmc_blk_packed_err_check(struct mmc_card *card,
1407 struct mmc_async_req *areq)
1409 struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1410 mmc_active);
1411 struct request *req = mq_rq->req;
1412 struct mmc_packed *packed = mq_rq->packed;
1413 int err, check, status;
1414 u8 *ext_csd;
1416 BUG_ON(!packed);
1418 packed->retries--;
1419 check = mmc_blk_err_check(card, areq);
1420 err = get_card_status(card, &status, 0);
1421 if (err) {
1422 pr_err("%s: error %d sending status command\n",
1423 req->rq_disk->disk_name, err);
1424 return MMC_BLK_ABORT;
1427 if (status & R1_EXCEPTION_EVENT) {
1428 err = mmc_get_ext_csd(card, &ext_csd);
1429 if (err) {
1430 pr_err("%s: error %d sending ext_csd\n",
1431 req->rq_disk->disk_name, err);
1432 return MMC_BLK_ABORT;
1435 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1436 EXT_CSD_PACKED_FAILURE) &&
1437 (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1438 EXT_CSD_PACKED_GENERIC_ERROR)) {
1439 if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1440 EXT_CSD_PACKED_INDEXED_ERROR) {
1441 packed->idx_failure =
1442 ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1443 check = MMC_BLK_PARTIAL;
1445 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1446 "failure index: %d\n",
1447 req->rq_disk->disk_name, packed->nr_entries,
1448 packed->blocks, packed->idx_failure);
1450 kfree(ext_csd);
1453 return check;
1456 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1457 struct mmc_card *card,
1458 int disable_multi,
1459 struct mmc_queue *mq)
1461 u32 readcmd, writecmd;
1462 struct mmc_blk_request *brq = &mqrq->brq;
1463 struct request *req = mqrq->req;
1464 struct mmc_blk_data *md = mq->data;
1465 bool do_data_tag;
1468 * Reliable writes are used to implement Forced Unit Access and
1469 * are supported only on MMCs.
1471 bool do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1472 (rq_data_dir(req) == WRITE) &&
1473 (md->flags & MMC_BLK_REL_WR);
1475 memset(brq, 0, sizeof(struct mmc_blk_request));
1476 brq->mrq.cmd = &brq->cmd;
1477 brq->mrq.data = &brq->data;
1479 brq->cmd.arg = blk_rq_pos(req);
1480 if (!mmc_card_blockaddr(card))
1481 brq->cmd.arg <<= 9;
1482 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1483 brq->data.blksz = 512;
1484 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1485 brq->stop.arg = 0;
1486 brq->data.blocks = blk_rq_sectors(req);
1489 * The block layer doesn't support all sector count
1490 * restrictions, so we need to be prepared for too big
1491 * requests.
1493 if (brq->data.blocks > card->host->max_blk_count)
1494 brq->data.blocks = card->host->max_blk_count;
1496 if (brq->data.blocks > 1) {
1498 * After a read error, we redo the request one sector
1499 * at a time in order to accurately determine which
1500 * sectors can be read successfully.
1502 if (disable_multi)
1503 brq->data.blocks = 1;
1506 * Some controllers have HW issues while operating
1507 * in multiple I/O mode
1509 if (card->host->ops->multi_io_quirk)
1510 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1511 (rq_data_dir(req) == READ) ?
1512 MMC_DATA_READ : MMC_DATA_WRITE,
1513 brq->data.blocks);
1516 if (brq->data.blocks > 1 || do_rel_wr) {
1517 /* SPI multiblock writes terminate using a special
1518 * token, not a STOP_TRANSMISSION request.
1520 if (!mmc_host_is_spi(card->host) ||
1521 rq_data_dir(req) == READ)
1522 brq->mrq.stop = &brq->stop;
1523 readcmd = MMC_READ_MULTIPLE_BLOCK;
1524 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1525 } else {
1526 brq->mrq.stop = NULL;
1527 readcmd = MMC_READ_SINGLE_BLOCK;
1528 writecmd = MMC_WRITE_BLOCK;
1530 if (rq_data_dir(req) == READ) {
1531 brq->cmd.opcode = readcmd;
1532 brq->data.flags |= MMC_DATA_READ;
1533 if (brq->mrq.stop)
1534 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 |
1535 MMC_CMD_AC;
1536 } else {
1537 brq->cmd.opcode = writecmd;
1538 brq->data.flags |= MMC_DATA_WRITE;
1539 if (brq->mrq.stop)
1540 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B |
1541 MMC_CMD_AC;
1544 if (do_rel_wr)
1545 mmc_apply_rel_rw(brq, card, req);
1548 * Data tag is used only during writing meta data to speed
1549 * up write and any subsequent read of this meta data
1551 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1552 (req->cmd_flags & REQ_META) &&
1553 (rq_data_dir(req) == WRITE) &&
1554 ((brq->data.blocks * brq->data.blksz) >=
1555 card->ext_csd.data_tag_unit_size);
1558 * Pre-defined multi-block transfers are preferable to
1559 * open ended-ones (and necessary for reliable writes).
1560 * However, it is not sufficient to just send CMD23,
1561 * and avoid the final CMD12, as on an error condition
1562 * CMD12 (stop) needs to be sent anyway. This, coupled
1563 * with Auto-CMD23 enhancements provided by some
1564 * hosts, means that the complexity of dealing
1565 * with this is best left to the host. If CMD23 is
1566 * supported by card and host, we'll fill sbc in and let
1567 * the host deal with handling it correctly. This means
1568 * that for hosts that don't expose MMC_CAP_CMD23, no
1569 * change of behavior will be observed.
1571 * N.B: Some MMC cards experience perf degradation.
1572 * We'll avoid using CMD23-bounded multiblock writes for
1573 * these, while retaining features like reliable writes.
1575 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1576 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1577 do_data_tag)) {
1578 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1579 brq->sbc.arg = brq->data.blocks |
1580 (do_rel_wr ? (1 << 31) : 0) |
1581 (do_data_tag ? (1 << 29) : 0);
1582 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1583 brq->mrq.sbc = &brq->sbc;
1586 mmc_set_data_timeout(&brq->data, card);
1588 brq->data.sg = mqrq->sg;
1589 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1592 * Adjust the sg list so it is the same size as the
1593 * request.
1595 if (brq->data.blocks != blk_rq_sectors(req)) {
1596 int i, data_size = brq->data.blocks << 9;
1597 struct scatterlist *sg;
1599 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1600 data_size -= sg->length;
1601 if (data_size <= 0) {
1602 sg->length += data_size;
1603 i++;
1604 break;
1607 brq->data.sg_len = i;
1610 mqrq->mmc_active.mrq = &brq->mrq;
1611 mqrq->mmc_active.err_check = mmc_blk_err_check;
1613 mmc_queue_bounce_pre(mqrq);
1616 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1617 struct mmc_card *card)
1619 unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1620 unsigned int max_seg_sz = queue_max_segment_size(q);
1621 unsigned int len, nr_segs = 0;
1623 do {
1624 len = min(hdr_sz, max_seg_sz);
1625 hdr_sz -= len;
1626 nr_segs++;
1627 } while (hdr_sz);
1629 return nr_segs;
1632 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1634 struct request_queue *q = mq->queue;
1635 struct mmc_card *card = mq->card;
1636 struct request *cur = req, *next = NULL;
1637 struct mmc_blk_data *md = mq->data;
1638 struct mmc_queue_req *mqrq = mq->mqrq_cur;
1639 bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1640 unsigned int req_sectors = 0, phys_segments = 0;
1641 unsigned int max_blk_count, max_phys_segs;
1642 bool put_back = true;
1643 u8 max_packed_rw = 0;
1644 u8 reqs = 0;
1646 if (!(md->flags & MMC_BLK_PACKED_CMD))
1647 goto no_packed;
1649 if ((rq_data_dir(cur) == WRITE) &&
1650 mmc_host_packed_wr(card->host))
1651 max_packed_rw = card->ext_csd.max_packed_writes;
1653 if (max_packed_rw == 0)
1654 goto no_packed;
1656 if (mmc_req_rel_wr(cur) &&
1657 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1658 goto no_packed;
1660 if (mmc_large_sector(card) &&
1661 !IS_ALIGNED(blk_rq_sectors(cur), 8))
1662 goto no_packed;
1664 mmc_blk_clear_packed(mqrq);
1666 max_blk_count = min(card->host->max_blk_count,
1667 card->host->max_req_size >> 9);
1668 if (unlikely(max_blk_count > 0xffff))
1669 max_blk_count = 0xffff;
1671 max_phys_segs = queue_max_segments(q);
1672 req_sectors += blk_rq_sectors(cur);
1673 phys_segments += cur->nr_phys_segments;
1675 if (rq_data_dir(cur) == WRITE) {
1676 req_sectors += mmc_large_sector(card) ? 8 : 1;
1677 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1680 do {
1681 if (reqs >= max_packed_rw - 1) {
1682 put_back = false;
1683 break;
1686 spin_lock_irq(q->queue_lock);
1687 next = blk_fetch_request(q);
1688 spin_unlock_irq(q->queue_lock);
1689 if (!next) {
1690 put_back = false;
1691 break;
1694 if (mmc_large_sector(card) &&
1695 !IS_ALIGNED(blk_rq_sectors(next), 8))
1696 break;
1698 if (next->cmd_flags & REQ_DISCARD ||
1699 next->cmd_flags & REQ_FLUSH)
1700 break;
1702 if (rq_data_dir(cur) != rq_data_dir(next))
1703 break;
1705 if (mmc_req_rel_wr(next) &&
1706 (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1707 break;
1709 req_sectors += blk_rq_sectors(next);
1710 if (req_sectors > max_blk_count)
1711 break;
1713 phys_segments += next->nr_phys_segments;
1714 if (phys_segments > max_phys_segs)
1715 break;
1717 list_add_tail(&next->queuelist, &mqrq->packed->list);
1718 cur = next;
1719 reqs++;
1720 } while (1);
1722 if (put_back) {
1723 spin_lock_irq(q->queue_lock);
1724 blk_requeue_request(q, next);
1725 spin_unlock_irq(q->queue_lock);
1728 if (reqs > 0) {
1729 list_add(&req->queuelist, &mqrq->packed->list);
1730 mqrq->packed->nr_entries = ++reqs;
1731 mqrq->packed->retries = reqs;
1732 return reqs;
1735 no_packed:
1736 mqrq->cmd_type = MMC_PACKED_NONE;
1737 return 0;
1740 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1741 struct mmc_card *card,
1742 struct mmc_queue *mq)
1744 struct mmc_blk_request *brq = &mqrq->brq;
1745 struct request *req = mqrq->req;
1746 struct request *prq;
1747 struct mmc_blk_data *md = mq->data;
1748 struct mmc_packed *packed = mqrq->packed;
1749 bool do_rel_wr, do_data_tag;
1750 u32 *packed_cmd_hdr;
1751 u8 hdr_blocks;
1752 u8 i = 1;
1754 BUG_ON(!packed);
1756 mqrq->cmd_type = MMC_PACKED_WRITE;
1757 packed->blocks = 0;
1758 packed->idx_failure = MMC_PACKED_NR_IDX;
1760 packed_cmd_hdr = packed->cmd_hdr;
1761 memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1762 packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1763 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1764 hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1767 * Argument for each entry of packed group
1769 list_for_each_entry(prq, &packed->list, queuelist) {
1770 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1771 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1772 (prq->cmd_flags & REQ_META) &&
1773 (rq_data_dir(prq) == WRITE) &&
1774 ((brq->data.blocks * brq->data.blksz) >=
1775 card->ext_csd.data_tag_unit_size);
1776 /* Argument of CMD23 */
1777 packed_cmd_hdr[(i * 2)] =
1778 (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1779 (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1780 blk_rq_sectors(prq);
1781 /* Argument of CMD18 or CMD25 */
1782 packed_cmd_hdr[((i * 2)) + 1] =
1783 mmc_card_blockaddr(card) ?
1784 blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1785 packed->blocks += blk_rq_sectors(prq);
1786 i++;
1789 memset(brq, 0, sizeof(struct mmc_blk_request));
1790 brq->mrq.cmd = &brq->cmd;
1791 brq->mrq.data = &brq->data;
1792 brq->mrq.sbc = &brq->sbc;
1793 brq->mrq.stop = &brq->stop;
1795 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1796 brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1797 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1799 brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1800 brq->cmd.arg = blk_rq_pos(req);
1801 if (!mmc_card_blockaddr(card))
1802 brq->cmd.arg <<= 9;
1803 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1805 brq->data.blksz = 512;
1806 brq->data.blocks = packed->blocks + hdr_blocks;
1807 brq->data.flags |= MMC_DATA_WRITE;
1809 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1810 brq->stop.arg = 0;
1811 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1813 mmc_set_data_timeout(&brq->data, card);
1815 brq->data.sg = mqrq->sg;
1816 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1818 mqrq->mmc_active.mrq = &brq->mrq;
1819 mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1821 mmc_queue_bounce_pre(mqrq);
1824 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1825 struct mmc_blk_request *brq, struct request *req,
1826 int ret)
1828 struct mmc_queue_req *mq_rq;
1829 mq_rq = container_of(brq, struct mmc_queue_req, brq);
1832 * If this is an SD card and we're writing, we can first
1833 * mark the known good sectors as ok.
1835 * If the card is not SD, we can still ok written sectors
1836 * as reported by the controller (which might be less than
1837 * the real number of written sectors, but never more).
1839 if (mmc_card_sd(card)) {
1840 u32 blocks;
1842 blocks = mmc_sd_num_wr_blocks(card);
1843 if (blocks != (u32)-1) {
1844 ret = blk_end_request(req, 0, blocks << 9);
1846 } else {
1847 if (!mmc_packed_cmd(mq_rq->cmd_type))
1848 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1850 return ret;
1853 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1855 struct request *prq;
1856 struct mmc_packed *packed = mq_rq->packed;
1857 int idx = packed->idx_failure, i = 0;
1858 int ret = 0;
1860 BUG_ON(!packed);
1862 while (!list_empty(&packed->list)) {
1863 prq = list_entry_rq(packed->list.next);
1864 if (idx == i) {
1865 /* retry from error index */
1866 packed->nr_entries -= idx;
1867 mq_rq->req = prq;
1868 ret = 1;
1870 if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1871 list_del_init(&prq->queuelist);
1872 mmc_blk_clear_packed(mq_rq);
1874 return ret;
1876 list_del_init(&prq->queuelist);
1877 blk_end_request(prq, 0, blk_rq_bytes(prq));
1878 i++;
1881 mmc_blk_clear_packed(mq_rq);
1882 return ret;
1885 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1887 struct request *prq;
1888 struct mmc_packed *packed = mq_rq->packed;
1890 BUG_ON(!packed);
1892 while (!list_empty(&packed->list)) {
1893 prq = list_entry_rq(packed->list.next);
1894 list_del_init(&prq->queuelist);
1895 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1898 mmc_blk_clear_packed(mq_rq);
1901 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1902 struct mmc_queue_req *mq_rq)
1904 struct request *prq;
1905 struct request_queue *q = mq->queue;
1906 struct mmc_packed *packed = mq_rq->packed;
1908 BUG_ON(!packed);
1910 while (!list_empty(&packed->list)) {
1911 prq = list_entry_rq(packed->list.prev);
1912 if (prq->queuelist.prev != &packed->list) {
1913 list_del_init(&prq->queuelist);
1914 spin_lock_irq(q->queue_lock);
1915 blk_requeue_request(mq->queue, prq);
1916 spin_unlock_irq(q->queue_lock);
1917 } else {
1918 list_del_init(&prq->queuelist);
1922 mmc_blk_clear_packed(mq_rq);
1925 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1927 struct mmc_blk_data *md = mq->data;
1928 struct mmc_card *card = md->queue.card;
1929 struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1930 int ret = 1, disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1931 enum mmc_blk_status status;
1932 struct mmc_queue_req *mq_rq;
1933 struct request *req = rqc;
1934 struct mmc_async_req *areq;
1935 const u8 packed_nr = 2;
1936 u8 reqs = 0;
1938 if (!rqc && !mq->mqrq_prev->req)
1939 return 0;
1941 if (rqc)
1942 reqs = mmc_blk_prep_packed_list(mq, rqc);
1944 do {
1945 if (rqc) {
1947 * When 4KB native sector is enabled, only 8 blocks
1948 * multiple read or write is allowed
1950 if ((brq->data.blocks & 0x07) &&
1951 (card->ext_csd.data_sector_size == 4096)) {
1952 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1953 req->rq_disk->disk_name);
1954 mq_rq = mq->mqrq_cur;
1955 goto cmd_abort;
1958 if (reqs >= packed_nr)
1959 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1960 card, mq);
1961 else
1962 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1963 areq = &mq->mqrq_cur->mmc_active;
1964 } else
1965 areq = NULL;
1966 areq = mmc_start_req(card->host, areq, (int *) &status);
1967 if (!areq) {
1968 if (status == MMC_BLK_NEW_REQUEST)
1969 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1970 return 0;
1973 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1974 brq = &mq_rq->brq;
1975 req = mq_rq->req;
1976 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1977 mmc_queue_bounce_post(mq_rq);
1979 switch (status) {
1980 case MMC_BLK_SUCCESS:
1981 case MMC_BLK_PARTIAL:
1983 * A block was successfully transferred.
1985 mmc_blk_reset_success(md, type);
1987 if (mmc_packed_cmd(mq_rq->cmd_type)) {
1988 ret = mmc_blk_end_packed_req(mq_rq);
1989 break;
1990 } else {
1991 ret = blk_end_request(req, 0,
1992 brq->data.bytes_xfered);
1996 * If the blk_end_request function returns non-zero even
1997 * though all data has been transferred and no errors
1998 * were returned by the host controller, it's a bug.
2000 if (status == MMC_BLK_SUCCESS && ret) {
2001 pr_err("%s BUG rq_tot %d d_xfer %d\n",
2002 __func__, blk_rq_bytes(req),
2003 brq->data.bytes_xfered);
2004 rqc = NULL;
2005 goto cmd_abort;
2007 break;
2008 case MMC_BLK_CMD_ERR:
2009 ret = mmc_blk_cmd_err(md, card, brq, req, ret);
2010 if (mmc_blk_reset(md, card->host, type))
2011 goto cmd_abort;
2012 if (!ret)
2013 goto start_new_req;
2014 break;
2015 case MMC_BLK_RETRY:
2016 retune_retry_done = brq->retune_retry_done;
2017 if (retry++ < 5)
2018 break;
2019 /* Fall through */
2020 case MMC_BLK_ABORT:
2021 if (!mmc_blk_reset(md, card->host, type))
2022 break;
2023 goto cmd_abort;
2024 case MMC_BLK_DATA_ERR: {
2025 int err;
2027 err = mmc_blk_reset(md, card->host, type);
2028 if (!err)
2029 break;
2030 if (err == -ENODEV ||
2031 mmc_packed_cmd(mq_rq->cmd_type))
2032 goto cmd_abort;
2033 /* Fall through */
2035 case MMC_BLK_ECC_ERR:
2036 if (brq->data.blocks > 1) {
2037 /* Redo read one sector at a time */
2038 pr_warn("%s: retrying using single block read\n",
2039 req->rq_disk->disk_name);
2040 disable_multi = 1;
2041 break;
2044 * After an error, we redo I/O one sector at a
2045 * time, so we only reach here after trying to
2046 * read a single sector.
2048 ret = blk_end_request(req, -EIO,
2049 brq->data.blksz);
2050 if (!ret)
2051 goto start_new_req;
2052 break;
2053 case MMC_BLK_NOMEDIUM:
2054 goto cmd_abort;
2055 default:
2056 pr_err("%s: Unhandled return value (%d)",
2057 req->rq_disk->disk_name, status);
2058 goto cmd_abort;
2061 if (ret) {
2062 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2063 if (!mq_rq->packed->retries)
2064 goto cmd_abort;
2065 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
2066 mmc_start_req(card->host,
2067 &mq_rq->mmc_active, NULL);
2068 } else {
2071 * In case of a incomplete request
2072 * prepare it again and resend.
2074 mmc_blk_rw_rq_prep(mq_rq, card,
2075 disable_multi, mq);
2076 mmc_start_req(card->host,
2077 &mq_rq->mmc_active, NULL);
2079 mq_rq->brq.retune_retry_done = retune_retry_done;
2081 } while (ret);
2083 return 1;
2085 cmd_abort:
2086 if (mmc_packed_cmd(mq_rq->cmd_type)) {
2087 mmc_blk_abort_packed_req(mq_rq);
2088 } else {
2089 if (mmc_card_removed(card))
2090 req->cmd_flags |= REQ_QUIET;
2091 while (ret)
2092 ret = blk_end_request(req, -EIO,
2093 blk_rq_cur_bytes(req));
2096 start_new_req:
2097 if (rqc) {
2098 if (mmc_card_removed(card)) {
2099 rqc->cmd_flags |= REQ_QUIET;
2100 blk_end_request_all(rqc, -EIO);
2101 } else {
2103 * If current request is packed, it needs to put back.
2105 if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
2106 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
2108 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
2109 mmc_start_req(card->host,
2110 &mq->mqrq_cur->mmc_active, NULL);
2114 return 0;
2117 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
2119 int ret;
2120 struct mmc_blk_data *md = mq->data;
2121 struct mmc_card *card = md->queue.card;
2122 struct mmc_host *host = card->host;
2123 unsigned long flags;
2124 unsigned int cmd_flags = req ? req->cmd_flags : 0;
2126 if (req && !mq->mqrq_prev->req)
2127 /* claim host only for the first request */
2128 mmc_get_card(card);
2130 ret = mmc_blk_part_switch(card, md);
2131 if (ret) {
2132 if (req) {
2133 blk_end_request_all(req, -EIO);
2135 ret = 0;
2136 goto out;
2139 mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
2140 if (cmd_flags & REQ_DISCARD) {
2141 /* complete ongoing async transfer before issuing discard */
2142 if (card->host->areq)
2143 mmc_blk_issue_rw_rq(mq, NULL);
2144 if (req->cmd_flags & REQ_SECURE)
2145 ret = mmc_blk_issue_secdiscard_rq(mq, req);
2146 else
2147 ret = mmc_blk_issue_discard_rq(mq, req);
2148 } else if (cmd_flags & REQ_FLUSH) {
2149 /* complete ongoing async transfer before issuing flush */
2150 if (card->host->areq)
2151 mmc_blk_issue_rw_rq(mq, NULL);
2152 ret = mmc_blk_issue_flush(mq, req);
2153 } else {
2154 if (!req && host->areq) {
2155 spin_lock_irqsave(&host->context_info.lock, flags);
2156 host->context_info.is_waiting_last_req = true;
2157 spin_unlock_irqrestore(&host->context_info.lock, flags);
2159 ret = mmc_blk_issue_rw_rq(mq, req);
2162 out:
2163 if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2164 (cmd_flags & MMC_REQ_SPECIAL_MASK))
2166 * Release host when there are no more requests
2167 * and after special request(discard, flush) is done.
2168 * In case sepecial request, there is no reentry to
2169 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2171 mmc_put_card(card);
2172 return ret;
2175 static inline int mmc_blk_readonly(struct mmc_card *card)
2177 return mmc_card_readonly(card) ||
2178 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2181 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2182 struct device *parent,
2183 sector_t size,
2184 bool default_ro,
2185 const char *subname,
2186 int area_type)
2188 struct mmc_blk_data *md;
2189 int devidx, ret;
2191 devidx = find_first_zero_bit(dev_use, max_devices);
2192 if (devidx >= max_devices)
2193 return ERR_PTR(-ENOSPC);
2194 __set_bit(devidx, dev_use);
2196 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2197 if (!md) {
2198 ret = -ENOMEM;
2199 goto out;
2203 * !subname implies we are creating main mmc_blk_data that will be
2204 * associated with mmc_card with dev_set_drvdata. Due to device
2205 * partitions, devidx will not coincide with a per-physical card
2206 * index anymore so we keep track of a name index.
2208 if (!subname) {
2209 md->name_idx = find_first_zero_bit(name_use, max_devices);
2210 __set_bit(md->name_idx, name_use);
2211 } else
2212 md->name_idx = ((struct mmc_blk_data *)
2213 dev_to_disk(parent)->private_data)->name_idx;
2215 md->area_type = area_type;
2218 * Set the read-only status based on the supported commands
2219 * and the write protect switch.
2221 md->read_only = mmc_blk_readonly(card);
2223 md->disk = alloc_disk(perdev_minors);
2224 if (md->disk == NULL) {
2225 ret = -ENOMEM;
2226 goto err_kfree;
2229 spin_lock_init(&md->lock);
2230 INIT_LIST_HEAD(&md->part);
2231 md->usage = 1;
2233 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2234 if (ret)
2235 goto err_putdisk;
2237 md->queue.issue_fn = mmc_blk_issue_rq;
2238 md->queue.data = md;
2240 md->disk->major = MMC_BLOCK_MAJOR;
2241 md->disk->first_minor = devidx * perdev_minors;
2242 md->disk->fops = &mmc_bdops;
2243 md->disk->private_data = md;
2244 md->disk->queue = md->queue.queue;
2245 md->disk->driverfs_dev = parent;
2246 set_disk_ro(md->disk, md->read_only || default_ro);
2247 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2248 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2251 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2253 * - be set for removable media with permanent block devices
2254 * - be unset for removable block devices with permanent media
2256 * Since MMC block devices clearly fall under the second
2257 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2258 * should use the block device creation/destruction hotplug
2259 * messages to tell when the card is present.
2262 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2263 "mmcblk%u%s", md->name_idx, subname ? subname : "");
2265 if (mmc_card_mmc(card))
2266 blk_queue_logical_block_size(md->queue.queue,
2267 card->ext_csd.data_sector_size);
2268 else
2269 blk_queue_logical_block_size(md->queue.queue, 512);
2271 set_capacity(md->disk, size);
2273 if (mmc_host_cmd23(card->host)) {
2274 if (mmc_card_mmc(card) ||
2275 (mmc_card_sd(card) &&
2276 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2277 md->flags |= MMC_BLK_CMD23;
2280 if (mmc_card_mmc(card) &&
2281 md->flags & MMC_BLK_CMD23 &&
2282 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2283 card->ext_csd.rel_sectors)) {
2284 md->flags |= MMC_BLK_REL_WR;
2285 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2288 if (mmc_card_mmc(card) &&
2289 (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2290 (md->flags & MMC_BLK_CMD23) &&
2291 card->ext_csd.packed_event_en) {
2292 if (!mmc_packed_init(&md->queue, card))
2293 md->flags |= MMC_BLK_PACKED_CMD;
2296 return md;
2298 err_putdisk:
2299 put_disk(md->disk);
2300 err_kfree:
2301 kfree(md);
2302 out:
2303 return ERR_PTR(ret);
2306 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2308 sector_t size;
2310 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2312 * The EXT_CSD sector count is in number or 512 byte
2313 * sectors.
2315 size = card->ext_csd.sectors;
2316 } else {
2318 * The CSD capacity field is in units of read_blkbits.
2319 * set_capacity takes units of 512 bytes.
2321 size = (typeof(sector_t))card->csd.capacity
2322 << (card->csd.read_blkbits - 9);
2325 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2326 MMC_BLK_DATA_AREA_MAIN);
2329 static int mmc_blk_alloc_part(struct mmc_card *card,
2330 struct mmc_blk_data *md,
2331 unsigned int part_type,
2332 sector_t size,
2333 bool default_ro,
2334 const char *subname,
2335 int area_type)
2337 char cap_str[10];
2338 struct mmc_blk_data *part_md;
2340 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2341 subname, area_type);
2342 if (IS_ERR(part_md))
2343 return PTR_ERR(part_md);
2344 part_md->part_type = part_type;
2345 list_add(&part_md->part, &md->part);
2347 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2348 cap_str, sizeof(cap_str));
2349 pr_info("%s: %s %s partition %u %s\n",
2350 part_md->disk->disk_name, mmc_card_id(card),
2351 mmc_card_name(card), part_md->part_type, cap_str);
2352 return 0;
2355 /* MMC Physical partitions consist of two boot partitions and
2356 * up to four general purpose partitions.
2357 * For each partition enabled in EXT_CSD a block device will be allocatedi
2358 * to provide access to the partition.
2361 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2363 int idx, ret = 0;
2365 if (!mmc_card_mmc(card))
2366 return 0;
2368 for (idx = 0; idx < card->nr_parts; idx++) {
2369 if (card->part[idx].size) {
2370 ret = mmc_blk_alloc_part(card, md,
2371 card->part[idx].part_cfg,
2372 card->part[idx].size >> 9,
2373 card->part[idx].force_ro,
2374 card->part[idx].name,
2375 card->part[idx].area_type);
2376 if (ret)
2377 return ret;
2381 return ret;
2384 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2386 struct mmc_card *card;
2388 if (md) {
2390 * Flush remaining requests and free queues. It
2391 * is freeing the queue that stops new requests
2392 * from being accepted.
2394 card = md->queue.card;
2395 mmc_cleanup_queue(&md->queue);
2396 if (md->flags & MMC_BLK_PACKED_CMD)
2397 mmc_packed_clean(&md->queue);
2398 if (md->disk->flags & GENHD_FL_UP) {
2399 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2400 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2401 card->ext_csd.boot_ro_lockable)
2402 device_remove_file(disk_to_dev(md->disk),
2403 &md->power_ro_lock);
2405 del_gendisk(md->disk);
2407 mmc_blk_put(md);
2411 static void mmc_blk_remove_parts(struct mmc_card *card,
2412 struct mmc_blk_data *md)
2414 struct list_head *pos, *q;
2415 struct mmc_blk_data *part_md;
2417 __clear_bit(md->name_idx, name_use);
2418 list_for_each_safe(pos, q, &md->part) {
2419 part_md = list_entry(pos, struct mmc_blk_data, part);
2420 list_del(pos);
2421 mmc_blk_remove_req(part_md);
2425 static int mmc_add_disk(struct mmc_blk_data *md)
2427 int ret;
2428 struct mmc_card *card = md->queue.card;
2430 add_disk(md->disk);
2431 md->force_ro.show = force_ro_show;
2432 md->force_ro.store = force_ro_store;
2433 sysfs_attr_init(&md->force_ro.attr);
2434 md->force_ro.attr.name = "force_ro";
2435 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2436 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2437 if (ret)
2438 goto force_ro_fail;
2440 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2441 card->ext_csd.boot_ro_lockable) {
2442 umode_t mode;
2444 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2445 mode = S_IRUGO;
2446 else
2447 mode = S_IRUGO | S_IWUSR;
2449 md->power_ro_lock.show = power_ro_lock_show;
2450 md->power_ro_lock.store = power_ro_lock_store;
2451 sysfs_attr_init(&md->power_ro_lock.attr);
2452 md->power_ro_lock.attr.mode = mode;
2453 md->power_ro_lock.attr.name =
2454 "ro_lock_until_next_power_on";
2455 ret = device_create_file(disk_to_dev(md->disk),
2456 &md->power_ro_lock);
2457 if (ret)
2458 goto power_ro_lock_fail;
2460 return ret;
2462 power_ro_lock_fail:
2463 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2464 force_ro_fail:
2465 del_gendisk(md->disk);
2467 return ret;
2470 #define CID_MANFID_SANDISK 0x2
2471 #define CID_MANFID_TOSHIBA 0x11
2472 #define CID_MANFID_MICRON 0x13
2473 #define CID_MANFID_SAMSUNG 0x15
2474 #define CID_MANFID_KINGSTON 0x70
2476 static const struct mmc_fixup blk_fixups[] =
2478 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2479 MMC_QUIRK_INAND_CMD38),
2480 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2481 MMC_QUIRK_INAND_CMD38),
2482 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2483 MMC_QUIRK_INAND_CMD38),
2484 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2485 MMC_QUIRK_INAND_CMD38),
2486 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2487 MMC_QUIRK_INAND_CMD38),
2490 * Some MMC cards experience performance degradation with CMD23
2491 * instead of CMD12-bounded multiblock transfers. For now we'll
2492 * black list what's bad...
2493 * - Certain Toshiba cards.
2495 * N.B. This doesn't affect SD cards.
2497 MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2498 MMC_QUIRK_BLK_NO_CMD23),
2499 MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc,
2500 MMC_QUIRK_BLK_NO_CMD23),
2501 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2502 MMC_QUIRK_BLK_NO_CMD23),
2503 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2504 MMC_QUIRK_BLK_NO_CMD23),
2505 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2506 MMC_QUIRK_BLK_NO_CMD23),
2509 * Some Micron MMC cards needs longer data read timeout than
2510 * indicated in CSD.
2512 MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2513 MMC_QUIRK_LONG_READ_TIME),
2516 * On these Samsung MoviNAND parts, performing secure erase or
2517 * secure trim can result in unrecoverable corruption due to a
2518 * firmware bug.
2520 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2521 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2522 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2523 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2524 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2525 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2526 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2527 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2528 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2529 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2530 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2531 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2532 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2533 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2534 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2535 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2538 * On Some Kingston eMMCs, performing trim can result in
2539 * unrecoverable data conrruption occasionally due to a firmware bug.
2541 MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2542 MMC_QUIRK_TRIM_BROKEN),
2543 MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc,
2544 MMC_QUIRK_TRIM_BROKEN),
2546 END_FIXUP
2549 static int mmc_blk_probe(struct mmc_card *card)
2551 struct mmc_blk_data *md, *part_md;
2552 char cap_str[10];
2555 * Check that the card supports the command class(es) we need.
2557 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2558 return -ENODEV;
2560 mmc_fixup_device(card, blk_fixups);
2562 md = mmc_blk_alloc(card);
2563 if (IS_ERR(md))
2564 return PTR_ERR(md);
2566 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2567 cap_str, sizeof(cap_str));
2568 pr_info("%s: %s %s %s %s\n",
2569 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2570 cap_str, md->read_only ? "(ro)" : "");
2572 if (mmc_blk_alloc_parts(card, md))
2573 goto out;
2575 dev_set_drvdata(&card->dev, md);
2577 if (mmc_add_disk(md))
2578 goto out;
2580 list_for_each_entry(part_md, &md->part, part) {
2581 if (mmc_add_disk(part_md))
2582 goto out;
2585 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2586 pm_runtime_use_autosuspend(&card->dev);
2589 * Don't enable runtime PM for SD-combo cards here. Leave that
2590 * decision to be taken during the SDIO init sequence instead.
2592 if (card->type != MMC_TYPE_SD_COMBO) {
2593 pm_runtime_set_active(&card->dev);
2594 pm_runtime_enable(&card->dev);
2597 return 0;
2599 out:
2600 mmc_blk_remove_parts(card, md);
2601 mmc_blk_remove_req(md);
2602 return 0;
2605 static void mmc_blk_remove(struct mmc_card *card)
2607 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2609 mmc_blk_remove_parts(card, md);
2610 pm_runtime_get_sync(&card->dev);
2611 mmc_claim_host(card->host);
2612 mmc_blk_part_switch(card, md);
2613 mmc_release_host(card->host);
2614 if (card->type != MMC_TYPE_SD_COMBO)
2615 pm_runtime_disable(&card->dev);
2616 pm_runtime_put_noidle(&card->dev);
2617 mmc_blk_remove_req(md);
2618 dev_set_drvdata(&card->dev, NULL);
2621 static int _mmc_blk_suspend(struct mmc_card *card)
2623 struct mmc_blk_data *part_md;
2624 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2626 if (md) {
2627 mmc_queue_suspend(&md->queue);
2628 list_for_each_entry(part_md, &md->part, part) {
2629 mmc_queue_suspend(&part_md->queue);
2632 return 0;
2635 static void mmc_blk_shutdown(struct mmc_card *card)
2637 _mmc_blk_suspend(card);
2640 #ifdef CONFIG_PM_SLEEP
2641 static int mmc_blk_suspend(struct device *dev)
2643 struct mmc_card *card = mmc_dev_to_card(dev);
2645 return _mmc_blk_suspend(card);
2648 static int mmc_blk_resume(struct device *dev)
2650 struct mmc_blk_data *part_md;
2651 struct mmc_blk_data *md = dev_get_drvdata(dev);
2653 if (md) {
2655 * Resume involves the card going into idle state,
2656 * so current partition is always the main one.
2658 md->part_curr = md->part_type;
2659 mmc_queue_resume(&md->queue);
2660 list_for_each_entry(part_md, &md->part, part) {
2661 mmc_queue_resume(&part_md->queue);
2664 return 0;
2666 #endif
2668 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2670 static struct mmc_driver mmc_driver = {
2671 .drv = {
2672 .name = "mmcblk",
2673 .pm = &mmc_blk_pm_ops,
2675 .probe = mmc_blk_probe,
2676 .remove = mmc_blk_remove,
2677 .shutdown = mmc_blk_shutdown,
2680 static int __init mmc_blk_init(void)
2682 int res;
2684 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2685 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2687 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2689 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2690 if (res)
2691 goto out;
2693 res = mmc_register_driver(&mmc_driver);
2694 if (res)
2695 goto out2;
2697 return 0;
2698 out2:
2699 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2700 out:
2701 return res;
2704 static void __exit mmc_blk_exit(void)
2706 mmc_unregister_driver(&mmc_driver);
2707 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2710 module_init(mmc_blk_init);
2711 module_exit(mmc_blk_exit);
2713 MODULE_LICENSE("GPL");
2714 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");