| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Block driver for media (i.e., flash cards) | 
|  | 3 | * | 
|  | 4 | * Copyright 2002 Hewlett-Packard Company | 
| Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 5 | * Copyright 2005-2008 Pierre Ossman | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * | 
|  | 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. | 
|  | 10 | * | 
|  | 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. | 
|  | 14 | * | 
|  | 15 | * Many thanks to Alessandro Rubini and Jonathan Corbet! | 
|  | 16 | * | 
|  | 17 | * Author:  Andrew Christian | 
|  | 18 | *          28 May 2002 | 
|  | 19 | */ | 
|  | 20 | #include <linux/moduleparam.h> | 
|  | 21 | #include <linux/module.h> | 
|  | 22 | #include <linux/init.h> | 
|  | 23 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/kernel.h> | 
|  | 25 | #include <linux/fs.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/errno.h> | 
|  | 28 | #include <linux/hdreg.h> | 
|  | 29 | #include <linux/kdev_t.h> | 
|  | 30 | #include <linux/blkdev.h> | 
| Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 31 | #include <linux/mutex.h> | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 32 | #include <linux/scatterlist.h> | 
| Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 33 | #include <linux/string_helpers.h> | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 34 | #include <linux/delay.h> | 
|  | 35 | #include <linux/capability.h> | 
|  | 36 | #include <linux/compat.h> | 
| Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 37 | #include <linux/pm_runtime.h> | 
| Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 38 | #include <linux/idr.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 |  | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 40 | #include <linux/mmc/ioctl.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | #include <linux/mmc/card.h> | 
| Pierre Ossman | 385e3227 | 2006-06-18 14:34:37 +0200 | [diff] [blame] | 42 | #include <linux/mmc/host.h> | 
| Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 43 | #include <linux/mmc/mmc.h> | 
|  | 44 | #include <linux/mmc/sd.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 |  | 
| Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 46 | #include <linux/uaccess.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 |  | 
| Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 48 | #include "queue.h" | 
| Baoyou Xie | 48ab086 | 2016-09-30 09:37:38 +0800 | [diff] [blame] | 49 | #include "block.h" | 
| Ulf Hansson | 55244c5 | 2017-01-13 14:14:08 +0100 | [diff] [blame] | 50 | #include "core.h" | 
| Ulf Hansson | 4facdde | 2017-01-13 14:14:14 +0100 | [diff] [blame] | 51 | #include "card.h" | 
| Ulf Hansson | 5857b29 | 2017-01-13 14:14:15 +0100 | [diff] [blame] | 52 | #include "host.h" | 
| Ulf Hansson | 4facdde | 2017-01-13 14:14:14 +0100 | [diff] [blame] | 53 | #include "bus.h" | 
| Ulf Hansson | 55244c5 | 2017-01-13 14:14:08 +0100 | [diff] [blame] | 54 | #include "mmc_ops.h" | 
| Shawn Lin | 28fc64a | 2017-02-15 16:35:28 +0800 | [diff] [blame] | 55 | #include "quirks.h" | 
| Ulf Hansson | 55244c5 | 2017-01-13 14:14:08 +0100 | [diff] [blame] | 56 | #include "sd_ops.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 |  | 
| Andy Whitcroft | 6b0b628 | 2009-02-23 12:38:41 +0000 | [diff] [blame] | 58 | MODULE_ALIAS("mmc:block"); | 
| Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 59 | #ifdef MODULE_PARAM_PREFIX | 
|  | 60 | #undef MODULE_PARAM_PREFIX | 
|  | 61 | #endif | 
|  | 62 | #define MODULE_PARAM_PREFIX "mmcblk." | 
| David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 63 |  | 
| Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 64 | #define MMC_BLK_TIMEOUT_MS  (10 * 60 * 1000)        /* 10 minute timeout */ | 
| Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 65 | #define MMC_SANITIZE_REQ_TIMEOUT 240000 | 
|  | 66 | #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16) | 
| Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 67 |  | 
| Luca Porzio | d3df046 | 2015-11-06 15:12:26 +0000 | [diff] [blame] | 68 | #define mmc_req_rel_wr(req)	((req->cmd_flags & REQ_FUA) && \ | 
| Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 69 | (rq_data_dir(req) == WRITE)) | 
| Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 70 | static DEFINE_MUTEX(block_mutex); | 
| Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 71 |  | 
|  | 72 | /* | 
|  | 73 | * The defaults come from config options but can be overriden by module | 
|  | 74 | * or bootarg options. | 
|  | 75 | */ | 
|  | 76 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; | 
|  | 77 |  | 
|  | 78 | /* | 
|  | 79 | * We've only got one major, so number of mmcblk devices is | 
| Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 80 | * limited to (1 << 20) / number of minors per device.  It is also | 
| Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 81 | * limited by the MAX_DEVICES below. | 
| Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 82 | */ | 
|  | 83 | static int max_devices; | 
|  | 84 |  | 
| Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 85 | #define MAX_DEVICES 256 | 
|  | 86 |  | 
| Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 87 | static DEFINE_IDA(mmc_blk_ida); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | /* | 
|  | 90 | * There is one mmc_blk_data per slot. | 
|  | 91 | */ | 
|  | 92 | struct mmc_blk_data { | 
|  | 93 | spinlock_t	lock; | 
| Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 94 | struct device	*parent; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | struct gendisk	*disk; | 
|  | 96 | struct mmc_queue queue; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 97 | struct list_head part; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 |  | 
| Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 99 | unsigned int	flags; | 
|  | 100 | #define MMC_BLK_CMD23	(1 << 0)	/* Can do SET_BLOCK_COUNT for multiblock */ | 
|  | 101 | #define MMC_BLK_REL_WR	(1 << 1)	/* MMC Reliable write support */ | 
|  | 102 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | unsigned int	usage; | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 104 | unsigned int	read_only; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 105 | unsigned int	part_type; | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 106 | unsigned int	reset_done; | 
|  | 107 | #define MMC_BLK_READ		BIT(0) | 
|  | 108 | #define MMC_BLK_WRITE		BIT(1) | 
|  | 109 | #define MMC_BLK_DISCARD		BIT(2) | 
|  | 110 | #define MMC_BLK_SECDISCARD	BIT(3) | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 111 |  | 
|  | 112 | /* | 
|  | 113 | * Only set in main mmc_blk_data associated | 
| Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 114 | * with mmc_card with dev_set_drvdata, and keeps | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 115 | * track of the current selected device partition. | 
|  | 116 | */ | 
|  | 117 | unsigned int	part_curr; | 
|  | 118 | struct device_attribute force_ro; | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 119 | struct device_attribute power_ro_lock; | 
|  | 120 | int	area_type; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | }; | 
|  | 122 |  | 
| Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 123 | static DEFINE_MUTEX(open_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 |  | 
| Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 125 | module_param(perdev_minors, int, 0444); | 
|  | 126 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); | 
|  | 127 |  | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 128 | static inline int mmc_blk_part_switch(struct mmc_card *card, | 
|  | 129 | struct mmc_blk_data *md); | 
|  | 130 | static int get_card_status(struct mmc_card *card, u32 *status, int retries); | 
|  | 131 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) | 
|  | 133 | { | 
|  | 134 | struct mmc_blk_data *md; | 
|  | 135 |  | 
| Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 136 | mutex_lock(&open_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | md = disk->private_data; | 
|  | 138 | if (md && md->usage == 0) | 
|  | 139 | md = NULL; | 
|  | 140 | if (md) | 
|  | 141 | md->usage++; | 
| Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 142 | mutex_unlock(&open_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 |  | 
|  | 144 | return md; | 
|  | 145 | } | 
|  | 146 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 147 | static inline int mmc_get_devidx(struct gendisk *disk) | 
|  | 148 | { | 
| Colin Cross | 382c55f | 2015-10-22 10:00:41 -0700 | [diff] [blame] | 149 | int devidx = disk->first_minor / perdev_minors; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 150 | return devidx; | 
|  | 151 | } | 
|  | 152 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | static void mmc_blk_put(struct mmc_blk_data *md) | 
|  | 154 | { | 
| Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 155 | mutex_lock(&open_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | md->usage--; | 
|  | 157 | if (md->usage == 0) { | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 158 | int devidx = mmc_get_devidx(md->disk); | 
| Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 159 | blk_cleanup_queue(md->queue.queue); | 
| Heiner Kallweit | a04848c | 2017-02-01 19:44:22 +0100 | [diff] [blame] | 160 | ida_simple_remove(&mmc_blk_ida, devidx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | put_disk(md->disk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | kfree(md); | 
|  | 163 | } | 
| Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 164 | mutex_unlock(&open_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } | 
|  | 166 |  | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 167 | static ssize_t power_ro_lock_show(struct device *dev, | 
|  | 168 | struct device_attribute *attr, char *buf) | 
|  | 169 | { | 
|  | 170 | int ret; | 
|  | 171 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | 
|  | 172 | struct mmc_card *card = md->queue.card; | 
|  | 173 | int locked = 0; | 
|  | 174 |  | 
|  | 175 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN) | 
|  | 176 | locked = 2; | 
|  | 177 | else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN) | 
|  | 178 | locked = 1; | 
|  | 179 |  | 
|  | 180 | ret = snprintf(buf, PAGE_SIZE, "%d\n", locked); | 
|  | 181 |  | 
| Tomas Winkler | 9098f84 | 2015-07-16 15:50:45 +0200 | [diff] [blame] | 182 | mmc_blk_put(md); | 
|  | 183 |  | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 184 | return ret; | 
|  | 185 | } | 
|  | 186 |  | 
|  | 187 | static ssize_t power_ro_lock_store(struct device *dev, | 
|  | 188 | struct device_attribute *attr, const char *buf, size_t count) | 
|  | 189 | { | 
|  | 190 | int ret; | 
|  | 191 | struct mmc_blk_data *md, *part_md; | 
|  | 192 | struct mmc_card *card; | 
|  | 193 | unsigned long set; | 
|  | 194 |  | 
|  | 195 | if (kstrtoul(buf, 0, &set)) | 
|  | 196 | return -EINVAL; | 
|  | 197 |  | 
|  | 198 | if (set != 1) | 
|  | 199 | return count; | 
|  | 200 |  | 
|  | 201 | md = mmc_blk_get(dev_to_disk(dev)); | 
|  | 202 | card = md->queue.card; | 
|  | 203 |  | 
| Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 204 | mmc_get_card(card); | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 205 |  | 
|  | 206 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, | 
|  | 207 | card->ext_csd.boot_ro_lock | | 
|  | 208 | EXT_CSD_BOOT_WP_B_PWR_WP_EN, | 
|  | 209 | card->ext_csd.part_time); | 
|  | 210 | if (ret) | 
|  | 211 | pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret); | 
|  | 212 | else | 
|  | 213 | card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN; | 
|  | 214 |  | 
| Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 215 | mmc_put_card(card); | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 216 |  | 
|  | 217 | if (!ret) { | 
|  | 218 | pr_info("%s: Locking boot partition ro until next power on\n", | 
|  | 219 | md->disk->disk_name); | 
|  | 220 | set_disk_ro(md->disk, 1); | 
|  | 221 |  | 
|  | 222 | list_for_each_entry(part_md, &md->part, part) | 
|  | 223 | if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) { | 
|  | 224 | pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name); | 
|  | 225 | set_disk_ro(part_md->disk, 1); | 
|  | 226 | } | 
|  | 227 | } | 
|  | 228 |  | 
|  | 229 | mmc_blk_put(md); | 
|  | 230 | return count; | 
|  | 231 | } | 
|  | 232 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 233 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, | 
|  | 234 | char *buf) | 
|  | 235 | { | 
|  | 236 | int ret; | 
|  | 237 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | 
|  | 238 |  | 
| Baruch Siach | 0031a98 | 2014-09-22 10:12:51 +0300 | [diff] [blame] | 239 | ret = snprintf(buf, PAGE_SIZE, "%d\n", | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 240 | get_disk_ro(dev_to_disk(dev)) ^ | 
|  | 241 | md->read_only); | 
|  | 242 | mmc_blk_put(md); | 
|  | 243 | return ret; | 
|  | 244 | } | 
|  | 245 |  | 
|  | 246 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, | 
|  | 247 | const char *buf, size_t count) | 
|  | 248 | { | 
|  | 249 | int ret; | 
|  | 250 | char *end; | 
|  | 251 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); | 
|  | 252 | unsigned long set = simple_strtoul(buf, &end, 0); | 
|  | 253 | if (end == buf) { | 
|  | 254 | ret = -EINVAL; | 
|  | 255 | goto out; | 
|  | 256 | } | 
|  | 257 |  | 
|  | 258 | set_disk_ro(dev_to_disk(dev), set || md->read_only); | 
|  | 259 | ret = count; | 
|  | 260 | out: | 
|  | 261 | mmc_blk_put(md); | 
|  | 262 | return ret; | 
|  | 263 | } | 
|  | 264 |  | 
| Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 265 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | { | 
| Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 267 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | int ret = -ENXIO; | 
|  | 269 |  | 
| Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 270 | mutex_lock(&block_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | if (md) { | 
|  | 272 | if (md->usage == 2) | 
| Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 273 | check_disk_change(bdev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | ret = 0; | 
| Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 275 |  | 
| Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 276 | if ((mode & FMODE_WRITE) && md->read_only) { | 
| Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 277 | mmc_blk_put(md); | 
| Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 278 | ret = -EROFS; | 
| Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 279 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | } | 
| Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 281 | mutex_unlock(&block_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 |  | 
|  | 283 | return ret; | 
|  | 284 | } | 
|  | 285 |  | 
| Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 286 | static void mmc_blk_release(struct gendisk *disk, fmode_t mode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { | 
| Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 288 | struct mmc_blk_data *md = disk->private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 |  | 
| Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 290 | mutex_lock(&block_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | mmc_blk_put(md); | 
| Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 292 | mutex_unlock(&block_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } | 
|  | 294 |  | 
|  | 295 | static int | 
| Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 296 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | { | 
| Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 298 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); | 
|  | 299 | geo->heads = 4; | 
|  | 300 | geo->sectors = 16; | 
|  | 301 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | } | 
|  | 303 |  | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 304 | struct mmc_blk_ioc_data { | 
|  | 305 | struct mmc_ioc_cmd ic; | 
|  | 306 | unsigned char *buf; | 
|  | 307 | u64 buf_bytes; | 
|  | 308 | }; | 
|  | 309 |  | 
|  | 310 | static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( | 
|  | 311 | struct mmc_ioc_cmd __user *user) | 
|  | 312 | { | 
|  | 313 | struct mmc_blk_ioc_data *idata; | 
|  | 314 | int err; | 
|  | 315 |  | 
| yalin wang | 1ff8950 | 2015-11-12 19:27:11 +0800 | [diff] [blame] | 316 | idata = kmalloc(sizeof(*idata), GFP_KERNEL); | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 317 | if (!idata) { | 
|  | 318 | err = -ENOMEM; | 
| Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 319 | goto out; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
|  | 322 | if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { | 
|  | 323 | err = -EFAULT; | 
| Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 324 | goto idata_err; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 325 | } | 
|  | 326 |  | 
|  | 327 | idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; | 
|  | 328 | if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { | 
|  | 329 | err = -EOVERFLOW; | 
| Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 330 | goto idata_err; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 331 | } | 
|  | 332 |  | 
| Ville Viinikka | bfe5b1b | 2016-07-08 18:27:02 +0300 | [diff] [blame] | 333 | if (!idata->buf_bytes) { | 
|  | 334 | idata->buf = NULL; | 
| Johan Rudholm | 4d6144d | 2011-11-23 09:05:58 +0100 | [diff] [blame] | 335 | return idata; | 
| Ville Viinikka | bfe5b1b | 2016-07-08 18:27:02 +0300 | [diff] [blame] | 336 | } | 
| Johan Rudholm | 4d6144d | 2011-11-23 09:05:58 +0100 | [diff] [blame] | 337 |  | 
| yalin wang | 1ff8950 | 2015-11-12 19:27:11 +0800 | [diff] [blame] | 338 | idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL); | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 339 | if (!idata->buf) { | 
|  | 340 | err = -ENOMEM; | 
| Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 341 | goto idata_err; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 342 | } | 
|  | 343 |  | 
|  | 344 | if (copy_from_user(idata->buf, (void __user *)(unsigned long) | 
|  | 345 | idata->ic.data_ptr, idata->buf_bytes)) { | 
|  | 346 | err = -EFAULT; | 
|  | 347 | goto copy_err; | 
|  | 348 | } | 
|  | 349 |  | 
|  | 350 | return idata; | 
|  | 351 |  | 
|  | 352 | copy_err: | 
|  | 353 | kfree(idata->buf); | 
| Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 354 | idata_err: | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 355 | kfree(idata); | 
| Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 356 | out: | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 357 | return ERR_PTR(err); | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 358 | } | 
|  | 359 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 360 | static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr, | 
|  | 361 | struct mmc_blk_ioc_data *idata) | 
|  | 362 | { | 
|  | 363 | struct mmc_ioc_cmd *ic = &idata->ic; | 
|  | 364 |  | 
|  | 365 | if (copy_to_user(&(ic_ptr->response), ic->response, | 
|  | 366 | sizeof(ic->response))) | 
|  | 367 | return -EFAULT; | 
|  | 368 |  | 
|  | 369 | if (!idata->ic.write_flag) { | 
|  | 370 | if (copy_to_user((void __user *)(unsigned long)ic->data_ptr, | 
|  | 371 | idata->buf, idata->buf_bytes)) | 
|  | 372 | return -EFAULT; | 
|  | 373 | } | 
|  | 374 |  | 
|  | 375 | return 0; | 
|  | 376 | } | 
|  | 377 |  | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 378 | static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status, | 
|  | 379 | u32 retries_max) | 
|  | 380 | { | 
|  | 381 | int err; | 
|  | 382 | u32 retry_count = 0; | 
|  | 383 |  | 
|  | 384 | if (!status || !retries_max) | 
|  | 385 | return -EINVAL; | 
|  | 386 |  | 
|  | 387 | do { | 
|  | 388 | err = get_card_status(card, status, 5); | 
|  | 389 | if (err) | 
|  | 390 | break; | 
|  | 391 |  | 
|  | 392 | if (!R1_STATUS(*status) && | 
|  | 393 | (R1_CURRENT_STATE(*status) != R1_STATE_PRG)) | 
|  | 394 | break; /* RPMB programming operation complete */ | 
|  | 395 |  | 
|  | 396 | /* | 
|  | 397 | * Rechedule to give the MMC device a chance to continue | 
|  | 398 | * processing the previous command without being polled too | 
|  | 399 | * frequently. | 
|  | 400 | */ | 
|  | 401 | usleep_range(1000, 5000); | 
|  | 402 | } while (++retry_count < retries_max); | 
|  | 403 |  | 
|  | 404 | if (retry_count == retries_max) | 
|  | 405 | err = -EPERM; | 
|  | 406 |  | 
|  | 407 | return err; | 
|  | 408 | } | 
|  | 409 |  | 
| Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 410 | static int ioctl_do_sanitize(struct mmc_card *card) | 
|  | 411 | { | 
|  | 412 | int err; | 
|  | 413 |  | 
| Ulf Hansson | a2d1086 | 2013-12-16 14:37:26 +0100 | [diff] [blame] | 414 | if (!mmc_can_sanitize(card)) { | 
| Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 415 | pr_warn("%s: %s - SANITIZE is not supported\n", | 
|  | 416 | mmc_hostname(card->host), __func__); | 
|  | 417 | err = -EOPNOTSUPP; | 
|  | 418 | goto out; | 
|  | 419 | } | 
|  | 420 |  | 
|  | 421 | pr_debug("%s: %s - SANITIZE IN PROGRESS...\n", | 
|  | 422 | mmc_hostname(card->host), __func__); | 
|  | 423 |  | 
|  | 424 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | 
|  | 425 | EXT_CSD_SANITIZE_START, 1, | 
|  | 426 | MMC_SANITIZE_REQ_TIMEOUT); | 
|  | 427 |  | 
|  | 428 | if (err) | 
|  | 429 | pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n", | 
|  | 430 | mmc_hostname(card->host), __func__, err); | 
|  | 431 |  | 
|  | 432 | pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host), | 
|  | 433 | __func__); | 
|  | 434 | out: | 
|  | 435 | return err; | 
|  | 436 | } | 
|  | 437 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 438 | static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md, | 
|  | 439 | struct mmc_blk_ioc_data *idata) | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 440 | { | 
| Masahiro Yamada | c7836d1 | 2016-12-19 20:51:18 +0900 | [diff] [blame] | 441 | struct mmc_command cmd = {}; | 
|  | 442 | struct mmc_data data = {}; | 
|  | 443 | struct mmc_request mrq = {}; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 444 | struct scatterlist sg; | 
|  | 445 | int err; | 
| Linus Walleij | 829043c | 2017-05-18 11:29:33 +0200 | [diff] [blame] | 446 | bool is_rpmb = false; | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 447 | u32 status = 0; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 448 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 449 | if (!card || !md || !idata) | 
|  | 450 | return -EINVAL; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 451 |  | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 452 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) | 
|  | 453 | is_rpmb = true; | 
|  | 454 |  | 
| Johan Rudholm | 4d6144d | 2011-11-23 09:05:58 +0100 | [diff] [blame] | 455 | cmd.opcode = idata->ic.opcode; | 
|  | 456 | cmd.arg = idata->ic.arg; | 
|  | 457 | cmd.flags = idata->ic.flags; | 
|  | 458 |  | 
|  | 459 | if (idata->buf_bytes) { | 
|  | 460 | data.sg = &sg; | 
|  | 461 | data.sg_len = 1; | 
|  | 462 | data.blksz = idata->ic.blksz; | 
|  | 463 | data.blocks = idata->ic.blocks; | 
|  | 464 |  | 
|  | 465 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); | 
|  | 466 |  | 
|  | 467 | if (idata->ic.write_flag) | 
|  | 468 | data.flags = MMC_DATA_WRITE; | 
|  | 469 | else | 
|  | 470 | data.flags = MMC_DATA_READ; | 
|  | 471 |  | 
|  | 472 | /* data.flags must already be set before doing this. */ | 
|  | 473 | mmc_set_data_timeout(&data, card); | 
|  | 474 |  | 
|  | 475 | /* Allow overriding the timeout_ns for empirical tuning. */ | 
|  | 476 | if (idata->ic.data_timeout_ns) | 
|  | 477 | data.timeout_ns = idata->ic.data_timeout_ns; | 
|  | 478 |  | 
|  | 479 | if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { | 
|  | 480 | /* | 
|  | 481 | * Pretend this is a data transfer and rely on the | 
|  | 482 | * host driver to compute timeout.  When all host | 
|  | 483 | * drivers support cmd.cmd_timeout for R1B, this | 
|  | 484 | * can be changed to: | 
|  | 485 | * | 
|  | 486 | *     mrq.data = NULL; | 
|  | 487 | *     cmd.cmd_timeout = idata->ic.cmd_timeout_ms; | 
|  | 488 | */ | 
|  | 489 | data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; | 
|  | 490 | } | 
|  | 491 |  | 
|  | 492 | mrq.data = &data; | 
|  | 493 | } | 
|  | 494 |  | 
|  | 495 | mrq.cmd = &cmd; | 
|  | 496 |  | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 497 | err = mmc_blk_part_switch(card, md); | 
|  | 498 | if (err) | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 499 | return err; | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 500 |  | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 501 | if (idata->ic.is_acmd) { | 
|  | 502 | err = mmc_app_cmd(card->host, card); | 
|  | 503 | if (err) | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 504 | return err; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 505 | } | 
|  | 506 |  | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 507 | if (is_rpmb) { | 
|  | 508 | err = mmc_set_blockcount(card, data.blocks, | 
|  | 509 | idata->ic.write_flag & (1 << 31)); | 
|  | 510 | if (err) | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 511 | return err; | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 512 | } | 
|  | 513 |  | 
| Yaniv Gardi | a82e484 | 2013-06-05 14:13:08 +0300 | [diff] [blame] | 514 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) && | 
|  | 515 | (cmd.opcode == MMC_SWITCH)) { | 
| Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 516 | err = ioctl_do_sanitize(card); | 
|  | 517 |  | 
|  | 518 | if (err) | 
|  | 519 | pr_err("%s: ioctl_do_sanitize() failed. err = %d", | 
|  | 520 | __func__, err); | 
|  | 521 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 522 | return err; | 
| Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 523 | } | 
|  | 524 |  | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 525 | mmc_wait_for_req(card->host, &mrq); | 
|  | 526 |  | 
|  | 527 | if (cmd.error) { | 
|  | 528 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", | 
|  | 529 | __func__, cmd.error); | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 530 | return cmd.error; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 531 | } | 
|  | 532 | if (data.error) { | 
|  | 533 | dev_err(mmc_dev(card->host), "%s: data error %d\n", | 
|  | 534 | __func__, data.error); | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 535 | return data.error; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 536 | } | 
|  | 537 |  | 
|  | 538 | /* | 
|  | 539 | * According to the SD specs, some commands require a delay after | 
|  | 540 | * issuing the command. | 
|  | 541 | */ | 
|  | 542 | if (idata->ic.postsleep_min_us) | 
|  | 543 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); | 
|  | 544 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 545 | memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp)); | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 546 |  | 
| Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 547 | if (is_rpmb) { | 
|  | 548 | /* | 
|  | 549 | * Ensure RPMB command has completed by polling CMD13 | 
|  | 550 | * "Send Status". | 
|  | 551 | */ | 
|  | 552 | err = ioctl_rpmb_card_status_poll(card, &status, 5); | 
|  | 553 | if (err) | 
|  | 554 | dev_err(mmc_dev(card->host), | 
|  | 555 | "%s: Card Status=0x%08X, error %d\n", | 
|  | 556 | __func__, status, err); | 
|  | 557 | } | 
|  | 558 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 559 | return err; | 
|  | 560 | } | 
|  | 561 |  | 
|  | 562 | static int mmc_blk_ioctl_cmd(struct block_device *bdev, | 
|  | 563 | struct mmc_ioc_cmd __user *ic_ptr) | 
|  | 564 | { | 
|  | 565 | struct mmc_blk_ioc_data *idata; | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 566 | struct mmc_blk_ioc_data *idatas[1]; | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 567 | struct mmc_blk_data *md; | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 568 | struct mmc_queue *mq; | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 569 | struct mmc_card *card; | 
| Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 570 | int err = 0, ioc_err = 0; | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 571 | struct request *req; | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 572 |  | 
| Shawn Lin | 83c742c | 2016-03-16 18:15:47 +0800 | [diff] [blame] | 573 | /* | 
|  | 574 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the | 
|  | 575 | * whole block device, not on a partition.  This prevents overspray | 
|  | 576 | * between sibling partitions. | 
|  | 577 | */ | 
|  | 578 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) | 
|  | 579 | return -EPERM; | 
|  | 580 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 581 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); | 
|  | 582 | if (IS_ERR(idata)) | 
|  | 583 | return PTR_ERR(idata); | 
|  | 584 |  | 
|  | 585 | md = mmc_blk_get(bdev->bd_disk); | 
|  | 586 | if (!md) { | 
|  | 587 | err = -EINVAL; | 
|  | 588 | goto cmd_err; | 
|  | 589 | } | 
|  | 590 |  | 
|  | 591 | card = md->queue.card; | 
|  | 592 | if (IS_ERR(card)) { | 
|  | 593 | err = PTR_ERR(card); | 
|  | 594 | goto cmd_done; | 
|  | 595 | } | 
|  | 596 |  | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 597 | /* | 
|  | 598 | * Dispatch the ioctl() into the block request queue. | 
|  | 599 | */ | 
|  | 600 | mq = &md->queue; | 
|  | 601 | req = blk_get_request(mq->queue, | 
|  | 602 | idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, | 
|  | 603 | __GFP_RECLAIM); | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 604 | idatas[0] = idata; | 
| Linus Walleij | 02166a0 | 2017-05-19 15:37:28 +0200 | [diff] [blame^] | 605 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL; | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 606 | req_to_mmc_queue_req(req)->idata = idatas; | 
|  | 607 | req_to_mmc_queue_req(req)->ioc_count = 1; | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 608 | blk_execute_rq(mq->queue, NULL, req, 0); | 
|  | 609 | ioc_err = req_to_mmc_queue_req(req)->ioc_result; | 
| Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 610 | err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata); | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 611 | blk_put_request(req); | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 612 |  | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 613 | cmd_done: | 
|  | 614 | mmc_blk_put(md); | 
| Philippe De Swert | 1c02f00 | 2012-04-11 23:31:45 +0300 | [diff] [blame] | 615 | cmd_err: | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 616 | kfree(idata->buf); | 
|  | 617 | kfree(idata); | 
| Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 618 | return ioc_err ? ioc_err : err; | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 619 | } | 
|  | 620 |  | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 621 | /* | 
| Linus Walleij | 02166a0 | 2017-05-19 15:37:28 +0200 | [diff] [blame^] | 622 | * The non-block commands come back from the block layer after it queued it and | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 623 | * processed it with all other requests and then they get issued in this | 
|  | 624 | * function. | 
|  | 625 | */ | 
| Linus Walleij | 02166a0 | 2017-05-19 15:37:28 +0200 | [diff] [blame^] | 626 | static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req) | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 627 | { | 
|  | 628 | struct mmc_queue_req *mq_rq; | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 629 | struct mmc_card *card = mq->card; | 
|  | 630 | struct mmc_blk_data *md = mq->blkdata; | 
|  | 631 | int ioc_err; | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 632 | int i; | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 633 |  | 
|  | 634 | mq_rq = req_to_mmc_queue_req(req); | 
| Linus Walleij | 02166a0 | 2017-05-19 15:37:28 +0200 | [diff] [blame^] | 635 |  | 
|  | 636 | switch (mq_rq->drv_op) { | 
|  | 637 | case MMC_DRV_OP_IOCTL: | 
|  | 638 | for (i = 0; i < mq_rq->ioc_count; i++) { | 
|  | 639 | ioc_err = | 
|  | 640 | __mmc_blk_ioctl_cmd(card, md, mq_rq->idata[i]); | 
|  | 641 | if (ioc_err) | 
|  | 642 | break; | 
|  | 643 | } | 
|  | 644 | mq_rq->ioc_result = ioc_err; | 
|  | 645 |  | 
|  | 646 | /* Always switch back to main area after RPMB access */ | 
|  | 647 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) | 
|  | 648 | mmc_blk_part_switch(card, dev_get_drvdata(&card->dev)); | 
|  | 649 |  | 
|  | 650 | blk_end_request_all(req, ioc_err); | 
|  | 651 | break; | 
|  | 652 | default: | 
|  | 653 | /* Unknown operation */ | 
|  | 654 | break; | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 655 | } | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 656 | } | 
|  | 657 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 658 | static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev, | 
|  | 659 | struct mmc_ioc_multi_cmd __user *user) | 
|  | 660 | { | 
|  | 661 | struct mmc_blk_ioc_data **idata = NULL; | 
|  | 662 | struct mmc_ioc_cmd __user *cmds = user->cmds; | 
|  | 663 | struct mmc_card *card; | 
|  | 664 | struct mmc_blk_data *md; | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 665 | struct mmc_queue *mq; | 
| Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 666 | int i, err = 0, ioc_err = 0; | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 667 | __u64 num_of_cmds; | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 668 | struct request *req; | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 669 |  | 
| Shawn Lin | 83c742c | 2016-03-16 18:15:47 +0800 | [diff] [blame] | 670 | /* | 
|  | 671 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the | 
|  | 672 | * whole block device, not on a partition.  This prevents overspray | 
|  | 673 | * between sibling partitions. | 
|  | 674 | */ | 
|  | 675 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) | 
|  | 676 | return -EPERM; | 
|  | 677 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 678 | if (copy_from_user(&num_of_cmds, &user->num_of_cmds, | 
|  | 679 | sizeof(num_of_cmds))) | 
|  | 680 | return -EFAULT; | 
|  | 681 |  | 
|  | 682 | if (num_of_cmds > MMC_IOC_MAX_CMDS) | 
|  | 683 | return -EINVAL; | 
|  | 684 |  | 
|  | 685 | idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL); | 
|  | 686 | if (!idata) | 
|  | 687 | return -ENOMEM; | 
|  | 688 |  | 
|  | 689 | for (i = 0; i < num_of_cmds; i++) { | 
|  | 690 | idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]); | 
|  | 691 | if (IS_ERR(idata[i])) { | 
|  | 692 | err = PTR_ERR(idata[i]); | 
|  | 693 | num_of_cmds = i; | 
|  | 694 | goto cmd_err; | 
|  | 695 | } | 
|  | 696 | } | 
|  | 697 |  | 
|  | 698 | md = mmc_blk_get(bdev->bd_disk); | 
| Olof Johansson | f00ab14 | 2016-02-09 09:34:30 -0800 | [diff] [blame] | 699 | if (!md) { | 
|  | 700 | err = -EINVAL; | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 701 | goto cmd_err; | 
| Olof Johansson | f00ab14 | 2016-02-09 09:34:30 -0800 | [diff] [blame] | 702 | } | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 703 |  | 
|  | 704 | card = md->queue.card; | 
|  | 705 | if (IS_ERR(card)) { | 
|  | 706 | err = PTR_ERR(card); | 
|  | 707 | goto cmd_done; | 
|  | 708 | } | 
|  | 709 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 710 |  | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 711 | /* | 
|  | 712 | * Dispatch the ioctl()s into the block request queue. | 
|  | 713 | */ | 
|  | 714 | mq = &md->queue; | 
|  | 715 | req = blk_get_request(mq->queue, | 
|  | 716 | idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, | 
|  | 717 | __GFP_RECLAIM); | 
| Linus Walleij | 02166a0 | 2017-05-19 15:37:28 +0200 | [diff] [blame^] | 718 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL; | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 719 | req_to_mmc_queue_req(req)->idata = idata; | 
|  | 720 | req_to_mmc_queue_req(req)->ioc_count = num_of_cmds; | 
|  | 721 | blk_execute_rq(mq->queue, NULL, req, 0); | 
|  | 722 | ioc_err = req_to_mmc_queue_req(req)->ioc_result; | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 723 |  | 
|  | 724 | /* copy to user if data and response */ | 
| Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 725 | for (i = 0; i < num_of_cmds && !err; i++) | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 726 | err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]); | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 727 |  | 
| Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 728 | blk_put_request(req); | 
|  | 729 |  | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 730 | cmd_done: | 
|  | 731 | mmc_blk_put(md); | 
|  | 732 | cmd_err: | 
|  | 733 | for (i = 0; i < num_of_cmds; i++) { | 
|  | 734 | kfree(idata[i]->buf); | 
|  | 735 | kfree(idata[i]); | 
|  | 736 | } | 
|  | 737 | kfree(idata); | 
| Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 738 | return ioc_err ? ioc_err : err; | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 739 | } | 
|  | 740 |  | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 741 | static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, | 
|  | 742 | unsigned int cmd, unsigned long arg) | 
|  | 743 | { | 
| Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 744 | switch (cmd) { | 
|  | 745 | case MMC_IOC_CMD: | 
|  | 746 | return mmc_blk_ioctl_cmd(bdev, | 
|  | 747 | (struct mmc_ioc_cmd __user *)arg); | 
|  | 748 | case MMC_IOC_MULTI_CMD: | 
|  | 749 | return mmc_blk_ioctl_multi_cmd(bdev, | 
|  | 750 | (struct mmc_ioc_multi_cmd __user *)arg); | 
|  | 751 | default: | 
|  | 752 | return -EINVAL; | 
|  | 753 | } | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 754 | } | 
|  | 755 |  | 
|  | 756 | #ifdef CONFIG_COMPAT | 
|  | 757 | static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, | 
|  | 758 | unsigned int cmd, unsigned long arg) | 
|  | 759 | { | 
|  | 760 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); | 
|  | 761 | } | 
|  | 762 | #endif | 
|  | 763 |  | 
| Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 764 | static const struct block_device_operations mmc_bdops = { | 
| Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 765 | .open			= mmc_blk_open, | 
|  | 766 | .release		= mmc_blk_release, | 
| Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 767 | .getgeo			= mmc_blk_getgeo, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | .owner			= THIS_MODULE, | 
| John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 769 | .ioctl			= mmc_blk_ioctl, | 
|  | 770 | #ifdef CONFIG_COMPAT | 
|  | 771 | .compat_ioctl		= mmc_blk_compat_ioctl, | 
|  | 772 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | }; | 
|  | 774 |  | 
| Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 775 | static int mmc_blk_part_switch_pre(struct mmc_card *card, | 
|  | 776 | unsigned int part_type) | 
|  | 777 | { | 
|  | 778 | int ret = 0; | 
|  | 779 |  | 
|  | 780 | if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { | 
|  | 781 | if (card->ext_csd.cmdq_en) { | 
|  | 782 | ret = mmc_cmdq_disable(card); | 
|  | 783 | if (ret) | 
|  | 784 | return ret; | 
|  | 785 | } | 
|  | 786 | mmc_retune_pause(card->host); | 
|  | 787 | } | 
|  | 788 |  | 
|  | 789 | return ret; | 
|  | 790 | } | 
|  | 791 |  | 
|  | 792 | static int mmc_blk_part_switch_post(struct mmc_card *card, | 
|  | 793 | unsigned int part_type) | 
|  | 794 | { | 
|  | 795 | int ret = 0; | 
|  | 796 |  | 
|  | 797 | if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { | 
|  | 798 | mmc_retune_unpause(card->host); | 
|  | 799 | if (card->reenable_cmdq && !card->ext_csd.cmdq_en) | 
|  | 800 | ret = mmc_cmdq_enable(card); | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | return ret; | 
|  | 804 | } | 
|  | 805 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 806 | static inline int mmc_blk_part_switch(struct mmc_card *card, | 
|  | 807 | struct mmc_blk_data *md) | 
|  | 808 | { | 
| Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 809 | int ret = 0; | 
| Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 810 | struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); | 
| Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 811 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 812 | if (main_md->part_curr == md->part_type) | 
|  | 813 | return 0; | 
|  | 814 |  | 
|  | 815 | if (mmc_card_mmc(card)) { | 
| Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 816 | u8 part_config = card->ext_csd.part_config; | 
|  | 817 |  | 
| Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 818 | ret = mmc_blk_part_switch_pre(card, md->part_type); | 
|  | 819 | if (ret) | 
|  | 820 | return ret; | 
| Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 821 |  | 
| Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 822 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; | 
|  | 823 | part_config |= md->part_type; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 824 |  | 
|  | 825 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | 
| Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 826 | EXT_CSD_PART_CONFIG, part_config, | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 827 | card->ext_csd.part_time); | 
| Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 828 | if (ret) { | 
| Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 829 | mmc_blk_part_switch_post(card, md->part_type); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 830 | return ret; | 
| Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 831 | } | 
| Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 832 |  | 
|  | 833 | card->ext_csd.part_config = part_config; | 
| Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 834 |  | 
| Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 835 | ret = mmc_blk_part_switch_post(card, main_md->part_curr); | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 836 | } | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 837 |  | 
|  | 838 | main_md->part_curr = md->part_type; | 
| Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 839 | return ret; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 840 | } | 
|  | 841 |  | 
| Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 842 | static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks) | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 843 | { | 
|  | 844 | int err; | 
| Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 845 | u32 result; | 
|  | 846 | __be32 *blocks; | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 847 |  | 
| Masahiro Yamada | c7836d1 | 2016-12-19 20:51:18 +0900 | [diff] [blame] | 848 | struct mmc_request mrq = {}; | 
|  | 849 | struct mmc_command cmd = {}; | 
|  | 850 | struct mmc_data data = {}; | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 851 |  | 
|  | 852 | struct scatterlist sg; | 
|  | 853 |  | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 854 | cmd.opcode = MMC_APP_CMD; | 
|  | 855 | cmd.arg = card->rca << 16; | 
| David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 856 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 857 |  | 
|  | 858 | err = mmc_wait_for_cmd(card->host, &cmd, 0); | 
| David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 859 | if (err) | 
| Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 860 | return err; | 
| David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 861 | if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) | 
| Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 862 | return -EIO; | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 863 |  | 
|  | 864 | memset(&cmd, 0, sizeof(struct mmc_command)); | 
|  | 865 |  | 
|  | 866 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; | 
|  | 867 | cmd.arg = 0; | 
| David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 868 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 869 |  | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 870 | data.blksz = 4; | 
|  | 871 | data.blocks = 1; | 
|  | 872 | data.flags = MMC_DATA_READ; | 
|  | 873 | data.sg = &sg; | 
|  | 874 | data.sg_len = 1; | 
| Subhash Jadavani | d380443 | 2012-06-13 17:10:43 +0530 | [diff] [blame] | 875 | mmc_set_data_timeout(&data, card); | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 876 |  | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 877 | mrq.cmd = &cmd; | 
|  | 878 | mrq.data = &data; | 
|  | 879 |  | 
| Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 880 | blocks = kmalloc(4, GFP_KERNEL); | 
|  | 881 | if (!blocks) | 
| Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 882 | return -ENOMEM; | 
| Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 883 |  | 
|  | 884 | sg_init_one(&sg, blocks, 4); | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 885 |  | 
|  | 886 | mmc_wait_for_req(card->host, &mrq); | 
|  | 887 |  | 
| Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 888 | result = ntohl(*blocks); | 
|  | 889 | kfree(blocks); | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 890 |  | 
| Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 891 | if (cmd.error || data.error) | 
| Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 892 | return -EIO; | 
| Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 893 |  | 
| Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 894 | *written_blocks = result; | 
|  | 895 |  | 
|  | 896 | return 0; | 
| Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 897 | } | 
|  | 898 |  | 
| Russell King - ARM Linux | 0a2d404 | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 899 | static int get_card_status(struct mmc_card *card, u32 *status, int retries) | 
| Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 900 | { | 
| Masahiro Yamada | c7836d1 | 2016-12-19 20:51:18 +0900 | [diff] [blame] | 901 | struct mmc_command cmd = {}; | 
| Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 902 | int err; | 
|  | 903 |  | 
| Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 904 | cmd.opcode = MMC_SEND_STATUS; | 
|  | 905 | if (!mmc_host_is_spi(card->host)) | 
|  | 906 | cmd.arg = card->rca << 16; | 
|  | 907 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; | 
| Russell King - ARM Linux | 0a2d404 | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 908 | err = mmc_wait_for_cmd(card->host, &cmd, retries); | 
|  | 909 | if (err == 0) | 
|  | 910 | *status = cmd.resp[0]; | 
|  | 911 | return err; | 
| Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 912 | } | 
|  | 913 |  | 
| Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 914 | static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, | 
| Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 915 | bool hw_busy_detect, struct request *req, bool *gen_err) | 
| Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 916 | { | 
|  | 917 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); | 
|  | 918 | int err = 0; | 
|  | 919 | u32 status; | 
|  | 920 |  | 
|  | 921 | do { | 
|  | 922 | err = get_card_status(card, &status, 5); | 
|  | 923 | if (err) { | 
|  | 924 | pr_err("%s: error %d requesting status\n", | 
|  | 925 | req->rq_disk->disk_name, err); | 
|  | 926 | return err; | 
|  | 927 | } | 
|  | 928 |  | 
|  | 929 | if (status & R1_ERROR) { | 
|  | 930 | pr_err("%s: %s: error sending status cmd, status %#x\n", | 
|  | 931 | req->rq_disk->disk_name, __func__, status); | 
| Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 932 | *gen_err = true; | 
| Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 933 | } | 
|  | 934 |  | 
| Ulf Hansson | 95a9129 | 2014-01-29 13:11:27 +0100 | [diff] [blame] | 935 | /* We may rely on the host hw to handle busy detection.*/ | 
|  | 936 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && | 
|  | 937 | hw_busy_detect) | 
|  | 938 | break; | 
|  | 939 |  | 
| Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 940 | /* | 
|  | 941 | * Timeout if the device never becomes ready for data and never | 
|  | 942 | * leaves the program state. | 
|  | 943 | */ | 
|  | 944 | if (time_after(jiffies, timeout)) { | 
|  | 945 | pr_err("%s: Card stuck in programming state! %s %s\n", | 
|  | 946 | mmc_hostname(card->host), | 
|  | 947 | req->rq_disk->disk_name, __func__); | 
|  | 948 | return -ETIMEDOUT; | 
|  | 949 | } | 
|  | 950 |  | 
|  | 951 | /* | 
|  | 952 | * Some cards mishandle the status bits, | 
|  | 953 | * so make sure to check both the busy | 
|  | 954 | * indication and the card state. | 
|  | 955 | */ | 
|  | 956 | } while (!(status & R1_READY_FOR_DATA) || | 
|  | 957 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); | 
|  | 958 |  | 
|  | 959 | return err; | 
|  | 960 | } | 
|  | 961 |  | 
| Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 962 | static int send_stop(struct mmc_card *card, unsigned int timeout_ms, | 
| Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 963 | struct request *req, bool *gen_err, u32 *stop_status) | 
| Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 964 | { | 
|  | 965 | struct mmc_host *host = card->host; | 
| Masahiro Yamada | c7836d1 | 2016-12-19 20:51:18 +0900 | [diff] [blame] | 966 | struct mmc_command cmd = {}; | 
| Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 967 | int err; | 
|  | 968 | bool use_r1b_resp = rq_data_dir(req) == WRITE; | 
|  | 969 |  | 
|  | 970 | /* | 
|  | 971 | * Normally we use R1B responses for WRITE, but in cases where the host | 
|  | 972 | * has specified a max_busy_timeout we need to validate it. A failure | 
|  | 973 | * means we need to prevent the host from doing hw busy detection, which | 
|  | 974 | * is done by converting to a R1 response instead. | 
|  | 975 | */ | 
|  | 976 | if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) | 
|  | 977 | use_r1b_resp = false; | 
|  | 978 |  | 
|  | 979 | cmd.opcode = MMC_STOP_TRANSMISSION; | 
|  | 980 | if (use_r1b_resp) { | 
|  | 981 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | 
|  | 982 | cmd.busy_timeout = timeout_ms; | 
|  | 983 | } else { | 
|  | 984 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | 
|  | 985 | } | 
|  | 986 |  | 
|  | 987 | err = mmc_wait_for_cmd(host, &cmd, 5); | 
|  | 988 | if (err) | 
|  | 989 | return err; | 
|  | 990 |  | 
|  | 991 | *stop_status = cmd.resp[0]; | 
|  | 992 |  | 
|  | 993 | /* No need to check card status in case of READ. */ | 
|  | 994 | if (rq_data_dir(req) == READ) | 
|  | 995 | return 0; | 
|  | 996 |  | 
|  | 997 | if (!mmc_host_is_spi(host) && | 
|  | 998 | (*stop_status & R1_ERROR)) { | 
|  | 999 | pr_err("%s: %s: general error sending stop command, resp %#x\n", | 
|  | 1000 | req->rq_disk->disk_name, __func__, *stop_status); | 
| Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 1001 | *gen_err = true; | 
| Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1002 | } | 
|  | 1003 |  | 
|  | 1004 | return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err); | 
|  | 1005 | } | 
|  | 1006 |  | 
| Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1007 | #define ERR_NOMEDIUM	3 | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1008 | #define ERR_RETRY	2 | 
|  | 1009 | #define ERR_ABORT	1 | 
|  | 1010 | #define ERR_CONTINUE	0 | 
|  | 1011 |  | 
|  | 1012 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, | 
|  | 1013 | bool status_valid, u32 status) | 
|  | 1014 | { | 
|  | 1015 | switch (error) { | 
|  | 1016 | case -EILSEQ: | 
|  | 1017 | /* response crc error, retry the r/w cmd */ | 
|  | 1018 | pr_err("%s: %s sending %s command, card status %#x\n", | 
|  | 1019 | req->rq_disk->disk_name, "response CRC error", | 
|  | 1020 | name, status); | 
|  | 1021 | return ERR_RETRY; | 
|  | 1022 |  | 
|  | 1023 | case -ETIMEDOUT: | 
|  | 1024 | pr_err("%s: %s sending %s command, card status %#x\n", | 
|  | 1025 | req->rq_disk->disk_name, "timed out", name, status); | 
|  | 1026 |  | 
|  | 1027 | /* If the status cmd initially failed, retry the r/w cmd */ | 
| Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1028 | if (!status_valid) { | 
|  | 1029 | pr_err("%s: status not valid, retrying timeout\n", | 
|  | 1030 | req->rq_disk->disk_name); | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1031 | return ERR_RETRY; | 
| Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1032 | } | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1033 |  | 
|  | 1034 | /* | 
|  | 1035 | * If it was a r/w cmd crc error, or illegal command | 
|  | 1036 | * (eg, issued in wrong state) then retry - we should | 
|  | 1037 | * have corrected the state problem above. | 
|  | 1038 | */ | 
| Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1039 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { | 
|  | 1040 | pr_err("%s: command error, retrying timeout\n", | 
|  | 1041 | req->rq_disk->disk_name); | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1042 | return ERR_RETRY; | 
| Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1043 | } | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1044 |  | 
|  | 1045 | /* Otherwise abort the command */ | 
|  | 1046 | return ERR_ABORT; | 
|  | 1047 |  | 
|  | 1048 | default: | 
|  | 1049 | /* We don't understand the error code the driver gave us */ | 
|  | 1050 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", | 
|  | 1051 | req->rq_disk->disk_name, error, status); | 
|  | 1052 | return ERR_ABORT; | 
|  | 1053 | } | 
|  | 1054 | } | 
|  | 1055 |  | 
|  | 1056 | /* | 
|  | 1057 | * Initial r/w and stop cmd error recovery. | 
|  | 1058 | * We don't know whether the card received the r/w cmd or not, so try to | 
|  | 1059 | * restore things back to a sane state.  Essentially, we do this as follows: | 
|  | 1060 | * - Obtain card status.  If the first attempt to obtain card status fails, | 
|  | 1061 | *   the status word will reflect the failed status cmd, not the failed | 
|  | 1062 | *   r/w cmd.  If we fail to obtain card status, it suggests we can no | 
|  | 1063 | *   longer communicate with the card. | 
|  | 1064 | * - Check the card state.  If the card received the cmd but there was a | 
|  | 1065 | *   transient problem with the response, it might still be in a data transfer | 
|  | 1066 | *   mode.  Try to send it a stop command.  If this fails, we can't recover. | 
|  | 1067 | * - If the r/w cmd failed due to a response CRC error, it was probably | 
|  | 1068 | *   transient, so retry the cmd. | 
|  | 1069 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. | 
|  | 1070 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or | 
|  | 1071 | *   illegal cmd, retry. | 
|  | 1072 | * Otherwise we don't understand what happened, so abort. | 
|  | 1073 | */ | 
|  | 1074 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, | 
| Linus Walleij | 2cc6458 | 2016-11-04 11:05:18 +0100 | [diff] [blame] | 1075 | struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err) | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1076 | { | 
|  | 1077 | bool prev_cmd_status_valid = true; | 
|  | 1078 | u32 status, stop_status = 0; | 
|  | 1079 | int err, retry; | 
|  | 1080 |  | 
| Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1081 | if (mmc_card_removed(card)) | 
|  | 1082 | return ERR_NOMEDIUM; | 
|  | 1083 |  | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1084 | /* | 
|  | 1085 | * Try to get card status which indicates both the card state | 
|  | 1086 | * and why there was no response.  If the first attempt fails, | 
|  | 1087 | * we can't be sure the returned status is for the r/w command. | 
|  | 1088 | */ | 
|  | 1089 | for (retry = 2; retry >= 0; retry--) { | 
|  | 1090 | err = get_card_status(card, &status, 0); | 
|  | 1091 | if (!err) | 
|  | 1092 | break; | 
|  | 1093 |  | 
| Adrian Hunter | 6f398ad | 2015-05-07 13:10:23 +0300 | [diff] [blame] | 1094 | /* Re-tune if needed */ | 
|  | 1095 | mmc_retune_recheck(card->host); | 
|  | 1096 |  | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1097 | prev_cmd_status_valid = false; | 
|  | 1098 | pr_err("%s: error %d sending status command, %sing\n", | 
|  | 1099 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); | 
|  | 1100 | } | 
|  | 1101 |  | 
|  | 1102 | /* We couldn't get a response from the card.  Give up. */ | 
| Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1103 | if (err) { | 
|  | 1104 | /* Check if the card is removed */ | 
|  | 1105 | if (mmc_detect_card_removed(card->host)) | 
|  | 1106 | return ERR_NOMEDIUM; | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1107 | return ERR_ABORT; | 
| Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1108 | } | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1109 |  | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1110 | /* Flag ECC errors */ | 
|  | 1111 | if ((status & R1_CARD_ECC_FAILED) || | 
|  | 1112 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || | 
|  | 1113 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) | 
| Linus Walleij | 2cc6458 | 2016-11-04 11:05:18 +0100 | [diff] [blame] | 1114 | *ecc_err = true; | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1115 |  | 
| KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1116 | /* Flag General errors */ | 
|  | 1117 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) | 
|  | 1118 | if ((status & R1_ERROR) || | 
|  | 1119 | (brq->stop.resp[0] & R1_ERROR)) { | 
|  | 1120 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", | 
|  | 1121 | req->rq_disk->disk_name, __func__, | 
|  | 1122 | brq->stop.resp[0], status); | 
| Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 1123 | *gen_err = true; | 
| KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1124 | } | 
|  | 1125 |  | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1126 | /* | 
|  | 1127 | * Check the current card state.  If it is in some data transfer | 
|  | 1128 | * mode, tell it to stop (and hopefully transition back to TRAN.) | 
|  | 1129 | */ | 
|  | 1130 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || | 
|  | 1131 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { | 
| Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1132 | err = send_stop(card, | 
|  | 1133 | DIV_ROUND_UP(brq->data.timeout_ns, 1000000), | 
|  | 1134 | req, gen_err, &stop_status); | 
|  | 1135 | if (err) { | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1136 | pr_err("%s: error %d sending stop command\n", | 
|  | 1137 | req->rq_disk->disk_name, err); | 
| Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1138 | /* | 
|  | 1139 | * If the stop cmd also timed out, the card is probably | 
|  | 1140 | * not present, so abort. Other errors are bad news too. | 
|  | 1141 | */ | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1142 | return ERR_ABORT; | 
| Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1143 | } | 
|  | 1144 |  | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1145 | if (stop_status & R1_CARD_ECC_FAILED) | 
| Linus Walleij | 2cc6458 | 2016-11-04 11:05:18 +0100 | [diff] [blame] | 1146 | *ecc_err = true; | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1147 | } | 
|  | 1148 |  | 
|  | 1149 | /* Check for set block count errors */ | 
|  | 1150 | if (brq->sbc.error) | 
|  | 1151 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, | 
|  | 1152 | prev_cmd_status_valid, status); | 
|  | 1153 |  | 
|  | 1154 | /* Check for r/w command errors */ | 
|  | 1155 | if (brq->cmd.error) | 
|  | 1156 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, | 
|  | 1157 | prev_cmd_status_valid, status); | 
|  | 1158 |  | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1159 | /* Data errors */ | 
|  | 1160 | if (!brq->stop.error) | 
|  | 1161 | return ERR_CONTINUE; | 
|  | 1162 |  | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1163 | /* Now for stop errors.  These aren't fatal to the transfer. */ | 
| Johan Rudholm | 5e1344e | 2014-09-17 09:50:42 +0200 | [diff] [blame] | 1164 | pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1165 | req->rq_disk->disk_name, brq->stop.error, | 
|  | 1166 | brq->cmd.resp[0], status); | 
|  | 1167 |  | 
|  | 1168 | /* | 
|  | 1169 | * Subsitute in our own stop status as this will give the error | 
|  | 1170 | * state which happened during the execution of the r/w command. | 
|  | 1171 | */ | 
|  | 1172 | if (stop_status) { | 
|  | 1173 | brq->stop.resp[0] = stop_status; | 
|  | 1174 | brq->stop.error = 0; | 
|  | 1175 | } | 
|  | 1176 | return ERR_CONTINUE; | 
|  | 1177 | } | 
|  | 1178 |  | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1179 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, | 
|  | 1180 | int type) | 
|  | 1181 | { | 
|  | 1182 | int err; | 
|  | 1183 |  | 
|  | 1184 | if (md->reset_done & type) | 
|  | 1185 | return -EEXIST; | 
|  | 1186 |  | 
|  | 1187 | md->reset_done |= type; | 
|  | 1188 | err = mmc_hw_reset(host); | 
|  | 1189 | /* Ensure we switch back to the correct partition */ | 
|  | 1190 | if (err != -EOPNOTSUPP) { | 
| Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 1191 | struct mmc_blk_data *main_md = | 
|  | 1192 | dev_get_drvdata(&host->card->dev); | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1193 | int part_err; | 
|  | 1194 |  | 
|  | 1195 | main_md->part_curr = main_md->part_type; | 
|  | 1196 | part_err = mmc_blk_part_switch(host->card, md); | 
|  | 1197 | if (part_err) { | 
|  | 1198 | /* | 
|  | 1199 | * We have failed to get back into the correct | 
|  | 1200 | * partition, so we need to abort the whole request. | 
|  | 1201 | */ | 
|  | 1202 | return -ENODEV; | 
|  | 1203 | } | 
|  | 1204 | } | 
|  | 1205 | return err; | 
|  | 1206 | } | 
|  | 1207 |  | 
|  | 1208 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) | 
|  | 1209 | { | 
|  | 1210 | md->reset_done &= ~type; | 
|  | 1211 | } | 
|  | 1212 |  | 
| Chuanxiao Dong | 4e93b9a | 2014-08-12 12:01:30 +0800 | [diff] [blame] | 1213 | int mmc_access_rpmb(struct mmc_queue *mq) | 
|  | 1214 | { | 
| Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1215 | struct mmc_blk_data *md = mq->blkdata; | 
| Chuanxiao Dong | 4e93b9a | 2014-08-12 12:01:30 +0800 | [diff] [blame] | 1216 | /* | 
|  | 1217 | * If this is a RPMB partition access, return ture | 
|  | 1218 | */ | 
|  | 1219 | if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) | 
|  | 1220 | return true; | 
|  | 1221 |  | 
|  | 1222 | return false; | 
|  | 1223 | } | 
|  | 1224 |  | 
| Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1225 | static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1226 | { | 
| Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1227 | struct mmc_blk_data *md = mq->blkdata; | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1228 | struct mmc_card *card = md->queue.card; | 
|  | 1229 | unsigned int from, nr, arg; | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1230 | int err = 0, type = MMC_BLK_DISCARD; | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1231 |  | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1232 | if (!mmc_can_erase(card)) { | 
|  | 1233 | err = -EOPNOTSUPP; | 
| Geert Uytterhoeven | 8cb6ed1 | 2016-12-19 15:03:44 +0100 | [diff] [blame] | 1234 | goto fail; | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1235 | } | 
|  | 1236 |  | 
|  | 1237 | from = blk_rq_pos(req); | 
|  | 1238 | nr = blk_rq_sectors(req); | 
|  | 1239 |  | 
| Kyungmin Park | b3bf915 | 2011-10-18 09:34:04 +0900 | [diff] [blame] | 1240 | if (mmc_can_discard(card)) | 
|  | 1241 | arg = MMC_DISCARD_ARG; | 
|  | 1242 | else if (mmc_can_trim(card)) | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1243 | arg = MMC_TRIM_ARG; | 
|  | 1244 | else | 
|  | 1245 | arg = MMC_ERASE_ARG; | 
| Geert Uytterhoeven | 164b50b | 2016-12-19 15:03:45 +0100 | [diff] [blame] | 1246 | do { | 
|  | 1247 | err = 0; | 
|  | 1248 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { | 
|  | 1249 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | 
|  | 1250 | INAND_CMD38_ARG_EXT_CSD, | 
|  | 1251 | arg == MMC_TRIM_ARG ? | 
|  | 1252 | INAND_CMD38_ARG_TRIM : | 
|  | 1253 | INAND_CMD38_ARG_ERASE, | 
|  | 1254 | 0); | 
|  | 1255 | } | 
|  | 1256 | if (!err) | 
|  | 1257 | err = mmc_erase(card, from, nr, arg); | 
|  | 1258 | } while (err == -EIO && !mmc_blk_reset(md, card->host, type)); | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1259 | if (!err) | 
|  | 1260 | mmc_blk_reset_success(md, type); | 
| Geert Uytterhoeven | 8cb6ed1 | 2016-12-19 15:03:44 +0100 | [diff] [blame] | 1261 | fail: | 
| Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1262 | blk_end_request(req, err, blk_rq_bytes(req)); | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1263 | } | 
|  | 1264 |  | 
| Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1265 | static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1266 | struct request *req) | 
|  | 1267 | { | 
| Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1268 | struct mmc_blk_data *md = mq->blkdata; | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1269 | struct mmc_card *card = md->queue.card; | 
| Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1270 | unsigned int from, nr, arg; | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1271 | int err = 0, type = MMC_BLK_SECDISCARD; | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1272 |  | 
| Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1273 | if (!(mmc_can_secure_erase_trim(card))) { | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1274 | err = -EOPNOTSUPP; | 
|  | 1275 | goto out; | 
|  | 1276 | } | 
|  | 1277 |  | 
|  | 1278 | from = blk_rq_pos(req); | 
|  | 1279 | nr = blk_rq_sectors(req); | 
|  | 1280 |  | 
| Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1281 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) | 
|  | 1282 | arg = MMC_SECURE_TRIM1_ARG; | 
|  | 1283 | else | 
|  | 1284 | arg = MMC_SECURE_ERASE_ARG; | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1285 |  | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1286 | retry: | 
| Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1287 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { | 
|  | 1288 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | 
|  | 1289 | INAND_CMD38_ARG_EXT_CSD, | 
|  | 1290 | arg == MMC_SECURE_TRIM1_ARG ? | 
|  | 1291 | INAND_CMD38_ARG_SECTRIM1 : | 
|  | 1292 | INAND_CMD38_ARG_SECERASE, | 
|  | 1293 | 0); | 
|  | 1294 | if (err) | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1295 | goto out_retry; | 
| Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1296 | } | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1297 |  | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1298 | err = mmc_erase(card, from, nr, arg); | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1299 | if (err == -EIO) | 
|  | 1300 | goto out_retry; | 
|  | 1301 | if (err) | 
|  | 1302 | goto out; | 
|  | 1303 |  | 
|  | 1304 | if (arg == MMC_SECURE_TRIM1_ARG) { | 
| Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1305 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { | 
|  | 1306 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, | 
|  | 1307 | INAND_CMD38_ARG_EXT_CSD, | 
|  | 1308 | INAND_CMD38_ARG_SECTRIM2, | 
|  | 1309 | 0); | 
|  | 1310 | if (err) | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1311 | goto out_retry; | 
| Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1312 | } | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1313 |  | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1314 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1315 | if (err == -EIO) | 
|  | 1316 | goto out_retry; | 
|  | 1317 | if (err) | 
|  | 1318 | goto out; | 
| Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1319 | } | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1320 |  | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1321 | out_retry: | 
|  | 1322 | if (err && !mmc_blk_reset(md, card->host, type)) | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1323 | goto retry; | 
|  | 1324 | if (!err) | 
|  | 1325 | mmc_blk_reset_success(md, type); | 
| Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1326 | out: | 
| Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1327 | blk_end_request(req, err, blk_rq_bytes(req)); | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1328 | } | 
|  | 1329 |  | 
| Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1330 | static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1331 | { | 
| Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1332 | struct mmc_blk_data *md = mq->blkdata; | 
| Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1333 | struct mmc_card *card = md->queue.card; | 
|  | 1334 | int ret = 0; | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1335 |  | 
| Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1336 | ret = mmc_flush_cache(card); | 
|  | 1337 | if (ret) | 
|  | 1338 | ret = -EIO; | 
|  | 1339 |  | 
| Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1340 | blk_end_request_all(req, ret); | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1341 | } | 
|  | 1342 |  | 
|  | 1343 | /* | 
|  | 1344 | * Reformat current write as a reliable write, supporting | 
|  | 1345 | * both legacy and the enhanced reliable write MMC cards. | 
|  | 1346 | * In each transfer we'll handle only as much as a single | 
|  | 1347 | * reliable write can handle, thus finish the request in | 
|  | 1348 | * partial completions. | 
|  | 1349 | */ | 
| Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1350 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, | 
|  | 1351 | struct mmc_card *card, | 
|  | 1352 | struct request *req) | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1353 | { | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1354 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { | 
|  | 1355 | /* Legacy mode imposes restrictions on transfers. */ | 
| Adrian Hunter | 9cb38f7 | 2017-03-13 14:36:40 +0200 | [diff] [blame] | 1356 | if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors)) | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1357 | brq->data.blocks = 1; | 
|  | 1358 |  | 
|  | 1359 | if (brq->data.blocks > card->ext_csd.rel_sectors) | 
|  | 1360 | brq->data.blocks = card->ext_csd.rel_sectors; | 
|  | 1361 | else if (brq->data.blocks < card->ext_csd.rel_sectors) | 
|  | 1362 | brq->data.blocks = 1; | 
|  | 1363 | } | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1364 | } | 
|  | 1365 |  | 
| Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1366 | #define CMD_ERRORS							\ | 
|  | 1367 | (R1_OUT_OF_RANGE |	/* Command argument out of range */	\ | 
|  | 1368 | R1_ADDRESS_ERROR |	/* Misaligned address */		\ | 
|  | 1369 | R1_BLOCK_LEN_ERROR |	/* Transferred block length incorrect */\ | 
|  | 1370 | R1_WP_VIOLATION |	/* Tried to write to protected block */	\ | 
|  | 1371 | R1_CC_ERROR |		/* Card controller error */		\ | 
|  | 1372 | R1_ERROR)		/* General/unknown error */ | 
|  | 1373 |  | 
| Linus Walleij | 8e8b3f5 | 2016-11-04 11:05:19 +0100 | [diff] [blame] | 1374 | static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card, | 
|  | 1375 | struct mmc_async_req *areq) | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1376 | { | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1377 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, | 
| Linus Walleij | 74f5ba3 | 2017-02-01 13:47:55 +0100 | [diff] [blame] | 1378 | areq); | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1379 | struct mmc_blk_request *brq = &mq_mrq->brq; | 
| Linus Walleij | 67e69d5 | 2017-05-19 15:37:27 +0200 | [diff] [blame] | 1380 | struct request *req = mmc_queue_req_to_req(mq_mrq); | 
| Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1381 | int need_retune = card->host->need_retune; | 
| Linus Walleij | 2cc6458 | 2016-11-04 11:05:18 +0100 | [diff] [blame] | 1382 | bool ecc_err = false; | 
| Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 1383 | bool gen_err = false; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1384 |  | 
|  | 1385 | /* | 
|  | 1386 | * sbc.error indicates a problem with the set block count | 
|  | 1387 | * command.  No data will have been transferred. | 
|  | 1388 | * | 
|  | 1389 | * cmd.error indicates a problem with the r/w command.  No | 
|  | 1390 | * data will have been transferred. | 
|  | 1391 | * | 
|  | 1392 | * stop.error indicates a problem with the stop command.  Data | 
|  | 1393 | * may have been transferred, or may still be transferring. | 
|  | 1394 | */ | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1395 | if (brq->sbc.error || brq->cmd.error || brq->stop.error || | 
|  | 1396 | brq->data.error) { | 
| KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1397 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1398 | case ERR_RETRY: | 
|  | 1399 | return MMC_BLK_RETRY; | 
|  | 1400 | case ERR_ABORT: | 
|  | 1401 | return MMC_BLK_ABORT; | 
| Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1402 | case ERR_NOMEDIUM: | 
|  | 1403 | return MMC_BLK_NOMEDIUM; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1404 | case ERR_CONTINUE: | 
|  | 1405 | break; | 
|  | 1406 | } | 
|  | 1407 | } | 
|  | 1408 |  | 
|  | 1409 | /* | 
|  | 1410 | * Check for errors relating to the execution of the | 
|  | 1411 | * initial command - such as address errors.  No data | 
|  | 1412 | * has been transferred. | 
|  | 1413 | */ | 
|  | 1414 | if (brq->cmd.resp[0] & CMD_ERRORS) { | 
|  | 1415 | pr_err("%s: r/w command failed, status = %#x\n", | 
|  | 1416 | req->rq_disk->disk_name, brq->cmd.resp[0]); | 
|  | 1417 | return MMC_BLK_ABORT; | 
|  | 1418 | } | 
|  | 1419 |  | 
|  | 1420 | /* | 
|  | 1421 | * Everything else is either success, or a data error of some | 
|  | 1422 | * kind.  If it was a write, we may have transitioned to | 
|  | 1423 | * program mode, which we have to wait for it to complete. | 
|  | 1424 | */ | 
|  | 1425 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { | 
| Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1426 | int err; | 
| Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 1427 |  | 
| KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1428 | /* Check stop command response */ | 
|  | 1429 | if (brq->stop.resp[0] & R1_ERROR) { | 
|  | 1430 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", | 
|  | 1431 | req->rq_disk->disk_name, __func__, | 
|  | 1432 | brq->stop.resp[0]); | 
| Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 1433 | gen_err = true; | 
| KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1434 | } | 
|  | 1435 |  | 
| Ulf Hansson | 95a9129 | 2014-01-29 13:11:27 +0100 | [diff] [blame] | 1436 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, | 
|  | 1437 | &gen_err); | 
| Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1438 | if (err) | 
|  | 1439 | return MMC_BLK_CMD_ERR; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1440 | } | 
|  | 1441 |  | 
| KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1442 | /* if general error occurs, retry the write operation. */ | 
|  | 1443 | if (gen_err) { | 
|  | 1444 | pr_warn("%s: retrying write for general error\n", | 
|  | 1445 | req->rq_disk->disk_name); | 
|  | 1446 | return MMC_BLK_RETRY; | 
|  | 1447 | } | 
|  | 1448 |  | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1449 | if (brq->data.error) { | 
| Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1450 | if (need_retune && !brq->retune_retry_done) { | 
| Russell King | 09faf61 | 2016-01-29 09:44:00 +0000 | [diff] [blame] | 1451 | pr_debug("%s: retrying because a re-tune was needed\n", | 
|  | 1452 | req->rq_disk->disk_name); | 
| Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1453 | brq->retune_retry_done = 1; | 
|  | 1454 | return MMC_BLK_RETRY; | 
|  | 1455 | } | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1456 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", | 
|  | 1457 | req->rq_disk->disk_name, brq->data.error, | 
|  | 1458 | (unsigned)blk_rq_pos(req), | 
|  | 1459 | (unsigned)blk_rq_sectors(req), | 
|  | 1460 | brq->cmd.resp[0], brq->stop.resp[0]); | 
|  | 1461 |  | 
|  | 1462 | if (rq_data_dir(req) == READ) { | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1463 | if (ecc_err) | 
|  | 1464 | return MMC_BLK_ECC_ERR; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1465 | return MMC_BLK_DATA_ERR; | 
|  | 1466 | } else { | 
|  | 1467 | return MMC_BLK_CMD_ERR; | 
|  | 1468 | } | 
|  | 1469 | } | 
|  | 1470 |  | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1471 | if (!brq->data.bytes_xfered) | 
|  | 1472 | return MMC_BLK_RETRY; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1473 |  | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1474 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) | 
|  | 1475 | return MMC_BLK_PARTIAL; | 
|  | 1476 |  | 
|  | 1477 | return MMC_BLK_SUCCESS; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1478 | } | 
|  | 1479 |  | 
| Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1480 | static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq, | 
|  | 1481 | int disable_multi, bool *do_rel_wr, | 
|  | 1482 | bool *do_data_tag) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1483 | { | 
| Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1484 | struct mmc_blk_data *md = mq->blkdata; | 
|  | 1485 | struct mmc_card *card = md->queue.card; | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1486 | struct mmc_blk_request *brq = &mqrq->brq; | 
| Linus Walleij | 67e69d5 | 2017-05-19 15:37:27 +0200 | [diff] [blame] | 1487 | struct request *req = mmc_queue_req_to_req(mqrq); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 |  | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1489 | /* | 
|  | 1490 | * Reliable writes are used to implement Forced Unit Access and | 
| Luca Porzio | d3df046 | 2015-11-06 15:12:26 +0000 | [diff] [blame] | 1491 | * are supported only on MMCs. | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1492 | */ | 
| Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1493 | *do_rel_wr = (req->cmd_flags & REQ_FUA) && | 
|  | 1494 | rq_data_dir(req) == WRITE && | 
|  | 1495 | (md->flags & MMC_BLK_REL_WR); | 
| Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1496 |  | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1497 | memset(brq, 0, sizeof(struct mmc_blk_request)); | 
| Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1498 |  | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1499 | brq->mrq.data = &brq->data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1500 |  | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1501 | brq->stop.opcode = MMC_STOP_TRANSMISSION; | 
|  | 1502 | brq->stop.arg = 0; | 
| Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1503 |  | 
|  | 1504 | if (rq_data_dir(req) == READ) { | 
|  | 1505 | brq->data.flags = MMC_DATA_READ; | 
|  | 1506 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; | 
|  | 1507 | } else { | 
|  | 1508 | brq->data.flags = MMC_DATA_WRITE; | 
|  | 1509 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; | 
|  | 1510 | } | 
|  | 1511 |  | 
|  | 1512 | brq->data.blksz = 512; | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1513 | brq->data.blocks = blk_rq_sectors(req); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 |  | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1515 | /* | 
|  | 1516 | * The block layer doesn't support all sector count | 
|  | 1517 | * restrictions, so we need to be prepared for too big | 
|  | 1518 | * requests. | 
|  | 1519 | */ | 
|  | 1520 | if (brq->data.blocks > card->host->max_blk_count) | 
|  | 1521 | brq->data.blocks = card->host->max_blk_count; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 |  | 
| Paul Walmsley | 2bf22b3 | 2011-10-06 14:50:33 -0600 | [diff] [blame] | 1523 | if (brq->data.blocks > 1) { | 
|  | 1524 | /* | 
|  | 1525 | * After a read error, we redo the request one sector | 
|  | 1526 | * at a time in order to accurately determine which | 
|  | 1527 | * sectors can be read successfully. | 
|  | 1528 | */ | 
|  | 1529 | if (disable_multi) | 
|  | 1530 | brq->data.blocks = 1; | 
|  | 1531 |  | 
| Kuninori Morimoto | 2e47e84 | 2014-09-02 19:08:53 -0700 | [diff] [blame] | 1532 | /* | 
|  | 1533 | * Some controllers have HW issues while operating | 
|  | 1534 | * in multiple I/O mode | 
|  | 1535 | */ | 
|  | 1536 | if (card->host->ops->multi_io_quirk) | 
|  | 1537 | brq->data.blocks = card->host->ops->multi_io_quirk(card, | 
|  | 1538 | (rq_data_dir(req) == READ) ? | 
|  | 1539 | MMC_DATA_READ : MMC_DATA_WRITE, | 
|  | 1540 | brq->data.blocks); | 
| Paul Walmsley | 2bf22b3 | 2011-10-06 14:50:33 -0600 | [diff] [blame] | 1541 | } | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1542 |  | 
| Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1543 | if (*do_rel_wr) | 
|  | 1544 | mmc_apply_rel_rw(brq, card, req); | 
|  | 1545 |  | 
|  | 1546 | /* | 
|  | 1547 | * Data tag is used only during writing meta data to speed | 
|  | 1548 | * up write and any subsequent read of this meta data | 
|  | 1549 | */ | 
|  | 1550 | *do_data_tag = card->ext_csd.data_tag_unit_size && | 
|  | 1551 | (req->cmd_flags & REQ_META) && | 
|  | 1552 | (rq_data_dir(req) == WRITE) && | 
|  | 1553 | ((brq->data.blocks * brq->data.blksz) >= | 
|  | 1554 | card->ext_csd.data_tag_unit_size); | 
|  | 1555 |  | 
|  | 1556 | mmc_set_data_timeout(&brq->data, card); | 
|  | 1557 |  | 
|  | 1558 | brq->data.sg = mqrq->sg; | 
|  | 1559 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); | 
|  | 1560 |  | 
|  | 1561 | /* | 
|  | 1562 | * Adjust the sg list so it is the same size as the | 
|  | 1563 | * request. | 
|  | 1564 | */ | 
|  | 1565 | if (brq->data.blocks != blk_rq_sectors(req)) { | 
|  | 1566 | int i, data_size = brq->data.blocks << 9; | 
|  | 1567 | struct scatterlist *sg; | 
|  | 1568 |  | 
|  | 1569 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { | 
|  | 1570 | data_size -= sg->length; | 
|  | 1571 | if (data_size <= 0) { | 
|  | 1572 | sg->length += data_size; | 
|  | 1573 | i++; | 
|  | 1574 | break; | 
|  | 1575 | } | 
|  | 1576 | } | 
|  | 1577 | brq->data.sg_len = i; | 
|  | 1578 | } | 
|  | 1579 |  | 
|  | 1580 | mqrq->areq.mrq = &brq->mrq; | 
|  | 1581 |  | 
|  | 1582 | mmc_queue_bounce_pre(mqrq); | 
|  | 1583 | } | 
|  | 1584 |  | 
|  | 1585 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, | 
|  | 1586 | struct mmc_card *card, | 
|  | 1587 | int disable_multi, | 
|  | 1588 | struct mmc_queue *mq) | 
|  | 1589 | { | 
|  | 1590 | u32 readcmd, writecmd; | 
|  | 1591 | struct mmc_blk_request *brq = &mqrq->brq; | 
| Linus Walleij | 67e69d5 | 2017-05-19 15:37:27 +0200 | [diff] [blame] | 1592 | struct request *req = mmc_queue_req_to_req(mqrq); | 
| Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1593 | struct mmc_blk_data *md = mq->blkdata; | 
|  | 1594 | bool do_rel_wr, do_data_tag; | 
|  | 1595 |  | 
|  | 1596 | mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag); | 
|  | 1597 |  | 
|  | 1598 | brq->mrq.cmd = &brq->cmd; | 
|  | 1599 |  | 
|  | 1600 | brq->cmd.arg = blk_rq_pos(req); | 
|  | 1601 | if (!mmc_card_blockaddr(card)) | 
|  | 1602 | brq->cmd.arg <<= 9; | 
|  | 1603 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; | 
|  | 1604 |  | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1605 | if (brq->data.blocks > 1 || do_rel_wr) { | 
|  | 1606 | /* SPI multiblock writes terminate using a special | 
|  | 1607 | * token, not a STOP_TRANSMISSION request. | 
| Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 1608 | */ | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1609 | if (!mmc_host_is_spi(card->host) || | 
|  | 1610 | rq_data_dir(req) == READ) | 
|  | 1611 | brq->mrq.stop = &brq->stop; | 
|  | 1612 | readcmd = MMC_READ_MULTIPLE_BLOCK; | 
|  | 1613 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; | 
|  | 1614 | } else { | 
|  | 1615 | brq->mrq.stop = NULL; | 
|  | 1616 | readcmd = MMC_READ_SINGLE_BLOCK; | 
|  | 1617 | writecmd = MMC_WRITE_BLOCK; | 
|  | 1618 | } | 
| Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1619 | brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd; | 
| Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1620 |  | 
|  | 1621 | /* | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1622 | * Pre-defined multi-block transfers are preferable to | 
|  | 1623 | * open ended-ones (and necessary for reliable writes). | 
|  | 1624 | * However, it is not sufficient to just send CMD23, | 
|  | 1625 | * and avoid the final CMD12, as on an error condition | 
|  | 1626 | * CMD12 (stop) needs to be sent anyway. This, coupled | 
|  | 1627 | * with Auto-CMD23 enhancements provided by some | 
|  | 1628 | * hosts, means that the complexity of dealing | 
|  | 1629 | * with this is best left to the host. If CMD23 is | 
|  | 1630 | * supported by card and host, we'll fill sbc in and let | 
|  | 1631 | * the host deal with handling it correctly. This means | 
|  | 1632 | * that for hosts that don't expose MMC_CAP_CMD23, no | 
|  | 1633 | * change of behavior will be observed. | 
|  | 1634 | * | 
|  | 1635 | * N.B: Some MMC cards experience perf degradation. | 
|  | 1636 | * We'll avoid using CMD23-bounded multiblock writes for | 
|  | 1637 | * these, while retaining features like reliable writes. | 
|  | 1638 | */ | 
| Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1639 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && | 
|  | 1640 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || | 
|  | 1641 | do_data_tag)) { | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1642 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; | 
|  | 1643 | brq->sbc.arg = brq->data.blocks | | 
| Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1644 | (do_rel_wr ? (1 << 31) : 0) | | 
|  | 1645 | (do_data_tag ? (1 << 29) : 0); | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1646 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; | 
|  | 1647 | brq->mrq.sbc = &brq->sbc; | 
|  | 1648 | } | 
| Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1649 |  | 
| Linus Walleij | 74f5ba3 | 2017-02-01 13:47:55 +0100 | [diff] [blame] | 1650 | mqrq->areq.err_check = mmc_blk_err_check; | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1651 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 |  | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1653 | static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, | 
|  | 1654 | struct mmc_blk_request *brq, struct request *req, | 
|  | 1655 | bool old_req_pending) | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1656 | { | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1657 | bool req_pending; | 
|  | 1658 |  | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1659 | /* | 
|  | 1660 | * If this is an SD card and we're writing, we can first | 
|  | 1661 | * mark the known good sectors as ok. | 
|  | 1662 | * | 
|  | 1663 | * If the card is not SD, we can still ok written sectors | 
|  | 1664 | * as reported by the controller (which might be less than | 
|  | 1665 | * the real number of written sectors, but never more). | 
|  | 1666 | */ | 
|  | 1667 | if (mmc_card_sd(card)) { | 
|  | 1668 | u32 blocks; | 
| Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 1669 | int err; | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1670 |  | 
| Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 1671 | err = mmc_sd_num_wr_blocks(card, &blocks); | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1672 | if (err) | 
|  | 1673 | req_pending = old_req_pending; | 
|  | 1674 | else | 
|  | 1675 | req_pending = blk_end_request(req, 0, blocks << 9); | 
| Adrian Hunter | 5dd784d2 | 2016-11-29 12:09:08 +0200 | [diff] [blame] | 1676 | } else { | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1677 | req_pending = blk_end_request(req, 0, brq->data.bytes_xfered); | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1678 | } | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1679 | return req_pending; | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1680 | } | 
|  | 1681 |  | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1682 | static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card, | 
|  | 1683 | struct request *req, | 
|  | 1684 | struct mmc_queue_req *mqrq) | 
| Linus Walleij | 4e1f780 | 2017-01-24 11:17:52 +0100 | [diff] [blame] | 1685 | { | 
| Linus Walleij | 4e1f780 | 2017-01-24 11:17:52 +0100 | [diff] [blame] | 1686 | if (mmc_card_removed(card)) | 
|  | 1687 | req->rq_flags |= RQF_QUIET; | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1688 | while (blk_end_request(req, -EIO, blk_rq_cur_bytes(req))); | 
| Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1689 | mq->qcnt--; | 
| Linus Walleij | 4e1f780 | 2017-01-24 11:17:52 +0100 | [diff] [blame] | 1690 | } | 
|  | 1691 |  | 
| Linus Walleij | b2928e1 | 2017-02-01 13:47:54 +0100 | [diff] [blame] | 1692 | /** | 
|  | 1693 | * mmc_blk_rw_try_restart() - tries to restart the current async request | 
|  | 1694 | * @mq: the queue with the card and host to restart | 
|  | 1695 | * @req: a new request that want to be started after the current one | 
|  | 1696 | */ | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1697 | static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req, | 
|  | 1698 | struct mmc_queue_req *mqrq) | 
| Linus Walleij | efb5a05 | 2017-01-24 11:17:53 +0100 | [diff] [blame] | 1699 | { | 
|  | 1700 | if (!req) | 
|  | 1701 | return; | 
|  | 1702 |  | 
| Linus Walleij | b2928e1 | 2017-02-01 13:47:54 +0100 | [diff] [blame] | 1703 | /* | 
|  | 1704 | * If the card was removed, just cancel everything and return. | 
|  | 1705 | */ | 
|  | 1706 | if (mmc_card_removed(mq->card)) { | 
| Linus Walleij | efb5a05 | 2017-01-24 11:17:53 +0100 | [diff] [blame] | 1707 | req->rq_flags |= RQF_QUIET; | 
|  | 1708 | blk_end_request_all(req, -EIO); | 
| Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1709 | mq->qcnt--; /* FIXME: just set to 0? */ | 
| Linus Walleij | b2928e1 | 2017-02-01 13:47:54 +0100 | [diff] [blame] | 1710 | return; | 
| Linus Walleij | efb5a05 | 2017-01-24 11:17:53 +0100 | [diff] [blame] | 1711 | } | 
| Linus Walleij | b2928e1 | 2017-02-01 13:47:54 +0100 | [diff] [blame] | 1712 | /* Else proceed and try to restart the current async request */ | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1713 | mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); | 
|  | 1714 | mmc_start_areq(mq->card->host, &mqrq->areq, NULL); | 
| Linus Walleij | efb5a05 | 2017-01-24 11:17:53 +0100 | [diff] [blame] | 1715 | } | 
|  | 1716 |  | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1717 | static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req) | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1718 | { | 
| Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1719 | struct mmc_blk_data *md = mq->blkdata; | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1720 | struct mmc_card *card = md->queue.card; | 
| Adrian Hunter | 5be8037 | 2016-11-29 12:09:09 +0200 | [diff] [blame] | 1721 | struct mmc_blk_request *brq; | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1722 | int disable_multi = 0, retry = 0, type, retune_retry_done = 0; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1723 | enum mmc_blk_status status; | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1724 | struct mmc_queue_req *mqrq_cur = NULL; | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1725 | struct mmc_queue_req *mq_rq; | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1726 | struct request *old_req; | 
| Linus Walleij | 7d552a4 | 2017-01-24 11:17:56 +0100 | [diff] [blame] | 1727 | struct mmc_async_req *new_areq; | 
|  | 1728 | struct mmc_async_req *old_areq; | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1729 | bool req_pending = true; | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1730 |  | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1731 | if (new_req) { | 
| Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1732 | mqrq_cur = req_to_mmc_queue_req(new_req); | 
|  | 1733 | mq->qcnt++; | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1734 | } | 
|  | 1735 |  | 
|  | 1736 | if (!mq->qcnt) | 
| Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1737 | return; | 
| Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1738 |  | 
|  | 1739 | do { | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1740 | if (new_req) { | 
| Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 1741 | /* | 
|  | 1742 | * When 4KB native sector is enabled, only 8 blocks | 
|  | 1743 | * multiple read or write is allowed | 
|  | 1744 | */ | 
| Yuan, Juntao | e87c856 | 2016-05-13 07:59:24 +0000 | [diff] [blame] | 1745 | if (mmc_large_sector(card) && | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1746 | !IS_ALIGNED(blk_rq_sectors(new_req), 8)) { | 
| Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 1747 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1748 | new_req->rq_disk->disk_name); | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1749 | mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur); | 
| Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1750 | return; | 
| Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 1751 | } | 
| Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1752 |  | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1753 | mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq); | 
|  | 1754 | new_areq = &mqrq_cur->areq; | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1755 | } else | 
| Linus Walleij | 7d552a4 | 2017-01-24 11:17:56 +0100 | [diff] [blame] | 1756 | new_areq = NULL; | 
|  | 1757 |  | 
| Linus Walleij | c3399ef | 2017-02-01 13:47:53 +0100 | [diff] [blame] | 1758 | old_areq = mmc_start_areq(card->host, new_areq, &status); | 
| Linus Walleij | 7d552a4 | 2017-01-24 11:17:56 +0100 | [diff] [blame] | 1759 | if (!old_areq) { | 
| Linus Walleij | da0dbaf | 2017-01-24 11:17:55 +0100 | [diff] [blame] | 1760 | /* | 
|  | 1761 | * We have just put the first request into the pipeline | 
|  | 1762 | * and there is nothing more to do until it is | 
|  | 1763 | * complete. | 
|  | 1764 | */ | 
| Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1765 | return; | 
| Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1766 | } | 
| Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 1767 |  | 
| Linus Walleij | da0dbaf | 2017-01-24 11:17:55 +0100 | [diff] [blame] | 1768 | /* | 
|  | 1769 | * An asynchronous request has been completed and we proceed | 
|  | 1770 | * to handle the result of it. | 
|  | 1771 | */ | 
| Linus Walleij | 74f5ba3 | 2017-02-01 13:47:55 +0100 | [diff] [blame] | 1772 | mq_rq =	container_of(old_areq, struct mmc_queue_req, areq); | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1773 | brq = &mq_rq->brq; | 
| Linus Walleij | 67e69d5 | 2017-05-19 15:37:27 +0200 | [diff] [blame] | 1774 | old_req = mmc_queue_req_to_req(mq_rq); | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1775 | type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1776 | mmc_queue_bounce_post(mq_rq); | 
| Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 1777 |  | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1778 | switch (status) { | 
|  | 1779 | case MMC_BLK_SUCCESS: | 
|  | 1780 | case MMC_BLK_PARTIAL: | 
|  | 1781 | /* | 
|  | 1782 | * A block was successfully transferred. | 
|  | 1783 | */ | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1784 | mmc_blk_reset_success(md, type); | 
| Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1785 |  | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1786 | req_pending = blk_end_request(old_req, 0, | 
|  | 1787 | brq->data.bytes_xfered); | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1788 | /* | 
|  | 1789 | * If the blk_end_request function returns non-zero even | 
|  | 1790 | * though all data has been transferred and no errors | 
|  | 1791 | * were returned by the host controller, it's a bug. | 
|  | 1792 | */ | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1793 | if (status == MMC_BLK_SUCCESS && req_pending) { | 
| Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1794 | pr_err("%s BUG rq_tot %d d_xfer %d\n", | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1795 | __func__, blk_rq_bytes(old_req), | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1796 | brq->data.bytes_xfered); | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1797 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); | 
| Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1798 | return; | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1799 | } | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1800 | break; | 
|  | 1801 | case MMC_BLK_CMD_ERR: | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1802 | req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending); | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1803 | if (mmc_blk_reset(md, card->host, type)) { | 
| Adrian Hunter | 8ecc344 | 2017-03-13 14:36:33 +0200 | [diff] [blame] | 1804 | if (req_pending) | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1805 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); | 
|  | 1806 | else | 
| Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1807 | mq->qcnt--; | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1808 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1809 | return; | 
|  | 1810 | } | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1811 | if (!req_pending) { | 
| Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1812 | mq->qcnt--; | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1813 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1814 | return; | 
|  | 1815 | } | 
| Ding Wang | 29535f7 | 2015-05-18 20:14:15 +0800 | [diff] [blame] | 1816 | break; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1817 | case MMC_BLK_RETRY: | 
| Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1818 | retune_retry_done = brq->retune_retry_done; | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1819 | if (retry++ < 5) | 
| Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1820 | break; | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1821 | /* Fall through */ | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1822 | case MMC_BLK_ABORT: | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1823 | if (!mmc_blk_reset(md, card->host, type)) | 
|  | 1824 | break; | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1825 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1826 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1827 | return; | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1828 | case MMC_BLK_DATA_ERR: { | 
|  | 1829 | int err; | 
|  | 1830 |  | 
|  | 1831 | err = mmc_blk_reset(md, card->host, type); | 
|  | 1832 | if (!err) | 
|  | 1833 | break; | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1834 | if (err == -ENODEV) { | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1835 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1836 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1837 | return; | 
|  | 1838 | } | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1839 | /* Fall through */ | 
|  | 1840 | } | 
|  | 1841 | case MMC_BLK_ECC_ERR: | 
|  | 1842 | if (brq->data.blocks > 1) { | 
|  | 1843 | /* Redo read one sector at a time */ | 
| Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 1844 | pr_warn("%s: retrying using single block read\n", | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1845 | old_req->rq_disk->disk_name); | 
| Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1846 | disable_multi = 1; | 
|  | 1847 | break; | 
|  | 1848 | } | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1849 | /* | 
|  | 1850 | * After an error, we redo I/O one sector at a | 
|  | 1851 | * time, so we only reach here after trying to | 
|  | 1852 | * read a single sector. | 
|  | 1853 | */ | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1854 | req_pending = blk_end_request(old_req, -EIO, | 
|  | 1855 | brq->data.blksz); | 
|  | 1856 | if (!req_pending) { | 
| Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1857 | mq->qcnt--; | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1858 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1859 | return; | 
|  | 1860 | } | 
| Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1861 | break; | 
| Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1862 | case MMC_BLK_NOMEDIUM: | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1863 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1864 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1865 | return; | 
| Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1866 | default: | 
|  | 1867 | pr_err("%s: Unhandled return value (%d)", | 
| Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1868 | old_req->rq_disk->disk_name, status); | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1869 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); | 
| Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1870 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); | 
| Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1871 | return; | 
| Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1872 | } | 
|  | 1873 |  | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1874 | if (req_pending) { | 
| Linus Walleij | 03d640a | 2016-11-25 10:35:00 +0100 | [diff] [blame] | 1875 | /* | 
|  | 1876 | * In case of a incomplete request | 
|  | 1877 | * prepare it again and resend. | 
|  | 1878 | */ | 
|  | 1879 | mmc_blk_rw_rq_prep(mq_rq, card, | 
|  | 1880 | disable_multi, mq); | 
| Linus Walleij | c3399ef | 2017-02-01 13:47:53 +0100 | [diff] [blame] | 1881 | mmc_start_areq(card->host, | 
| Linus Walleij | 74f5ba3 | 2017-02-01 13:47:55 +0100 | [diff] [blame] | 1882 | &mq_rq->areq, NULL); | 
| Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1883 | mq_rq->brq.retune_retry_done = retune_retry_done; | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1884 | } | 
| Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1885 | } while (req_pending); | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1886 |  | 
| Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1887 | mq->qcnt--; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1888 | } | 
|  | 1889 |  | 
| Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1890 | void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1891 | { | 
| Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1892 | int ret; | 
| Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1893 | struct mmc_blk_data *md = mq->blkdata; | 
| Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1894 | struct mmc_card *card = md->queue.card; | 
|  | 1895 |  | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1896 | if (req && !mq->qcnt) | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1897 | /* claim host only for the first request */ | 
| Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 1898 | mmc_get_card(card); | 
| Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1899 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1900 | ret = mmc_blk_part_switch(card, md); | 
|  | 1901 | if (ret) { | 
| Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 1902 | if (req) { | 
| Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1903 | blk_end_request_all(req, -EIO); | 
| Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 1904 | } | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1905 | goto out; | 
|  | 1906 | } | 
| Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1907 |  | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 1908 | if (req) { | 
|  | 1909 | switch (req_op(req)) { | 
|  | 1910 | case REQ_OP_DRV_IN: | 
|  | 1911 | case REQ_OP_DRV_OUT: | 
|  | 1912 | /* | 
|  | 1913 | * Complete ongoing async transfer before issuing | 
|  | 1914 | * ioctl()s | 
|  | 1915 | */ | 
|  | 1916 | if (mq->qcnt) | 
|  | 1917 | mmc_blk_issue_rw_rq(mq, NULL); | 
| Linus Walleij | 02166a0 | 2017-05-19 15:37:28 +0200 | [diff] [blame^] | 1918 | mmc_blk_issue_drv_op(mq, req); | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 1919 | break; | 
|  | 1920 | case REQ_OP_DISCARD: | 
|  | 1921 | /* | 
|  | 1922 | * Complete ongoing async transfer before issuing | 
|  | 1923 | * discard. | 
|  | 1924 | */ | 
|  | 1925 | if (mq->qcnt) | 
|  | 1926 | mmc_blk_issue_rw_rq(mq, NULL); | 
|  | 1927 | mmc_blk_issue_discard_rq(mq, req); | 
|  | 1928 | break; | 
|  | 1929 | case REQ_OP_SECURE_ERASE: | 
|  | 1930 | /* | 
|  | 1931 | * Complete ongoing async transfer before issuing | 
|  | 1932 | * secure erase. | 
|  | 1933 | */ | 
|  | 1934 | if (mq->qcnt) | 
|  | 1935 | mmc_blk_issue_rw_rq(mq, NULL); | 
|  | 1936 | mmc_blk_issue_secdiscard_rq(mq, req); | 
|  | 1937 | break; | 
|  | 1938 | case REQ_OP_FLUSH: | 
|  | 1939 | /* | 
|  | 1940 | * Complete ongoing async transfer before issuing | 
|  | 1941 | * flush. | 
|  | 1942 | */ | 
|  | 1943 | if (mq->qcnt) | 
|  | 1944 | mmc_blk_issue_rw_rq(mq, NULL); | 
|  | 1945 | mmc_blk_issue_flush(mq, req); | 
|  | 1946 | break; | 
|  | 1947 | default: | 
|  | 1948 | /* Normal request, just issue it */ | 
|  | 1949 | mmc_blk_issue_rw_rq(mq, req); | 
|  | 1950 | card->host->context_info.is_waiting_last_req = false; | 
|  | 1951 | break; | 
|  | 1952 | }; | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1953 | } else { | 
| Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 1954 | /* No request, flushing the pipeline with NULL */ | 
|  | 1955 | mmc_blk_issue_rw_rq(mq, NULL); | 
| Adrian Hunter | 2602b74 | 2017-03-13 14:36:32 +0200 | [diff] [blame] | 1956 | card->host->context_info.is_waiting_last_req = false; | 
| Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1957 | } | 
| Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 1958 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1959 | out: | 
| Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1960 | if (!mq->qcnt) | 
| Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 1961 | mmc_put_card(card); | 
| Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1962 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 |  | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1964 | static inline int mmc_blk_readonly(struct mmc_card *card) | 
|  | 1965 | { | 
|  | 1966 | return mmc_card_readonly(card) || | 
|  | 1967 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); | 
|  | 1968 | } | 
|  | 1969 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1970 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, | 
|  | 1971 | struct device *parent, | 
|  | 1972 | sector_t size, | 
|  | 1973 | bool default_ro, | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 1974 | const char *subname, | 
|  | 1975 | int area_type) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | { | 
|  | 1977 | struct mmc_blk_data *md; | 
|  | 1978 | int devidx, ret; | 
|  | 1979 |  | 
| Heiner Kallweit | a04848c | 2017-02-01 19:44:22 +0100 | [diff] [blame] | 1980 | devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL); | 
|  | 1981 | if (devidx < 0) | 
|  | 1982 | return ERR_PTR(devidx); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 |  | 
| Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 1984 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1985 | if (!md) { | 
|  | 1986 | ret = -ENOMEM; | 
|  | 1987 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1988 | } | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1989 |  | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 1990 | md->area_type = area_type; | 
|  | 1991 |  | 
| Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 1992 | /* | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1993 | * Set the read-only status based on the supported commands | 
|  | 1994 | * and the write protect switch. | 
|  | 1995 | */ | 
|  | 1996 | md->read_only = mmc_blk_readonly(card); | 
|  | 1997 |  | 
| Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1998 | md->disk = alloc_disk(perdev_minors); | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 1999 | if (md->disk == NULL) { | 
|  | 2000 | ret = -ENOMEM; | 
|  | 2001 | goto err_kfree; | 
|  | 2002 | } | 
|  | 2003 |  | 
|  | 2004 | spin_lock_init(&md->lock); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2005 | INIT_LIST_HEAD(&md->part); | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2006 | md->usage = 1; | 
|  | 2007 |  | 
| Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 2008 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2009 | if (ret) | 
|  | 2010 | goto err_putdisk; | 
|  | 2011 |  | 
| Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 2012 | md->queue.blkdata = md; | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2013 |  | 
| Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2014 | md->disk->major	= MMC_BLOCK_MAJOR; | 
| Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2015 | md->disk->first_minor = devidx * perdev_minors; | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2016 | md->disk->fops = &mmc_bdops; | 
|  | 2017 | md->disk->private_data = md; | 
|  | 2018 | md->disk->queue = md->queue.queue; | 
| Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 2019 | md->parent = parent; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2020 | set_disk_ro(md->disk, md->read_only || default_ro); | 
| Colin Cross | 382c55f | 2015-10-22 10:00:41 -0700 | [diff] [blame] | 2021 | md->disk->flags = GENHD_FL_EXT_DEVT; | 
| Ulf Hansson | f5b4d71 | 2014-09-03 11:02:23 +0200 | [diff] [blame] | 2022 | if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT)) | 
| Loic Pallardy | 53d8f97 | 2012-08-06 17:12:28 +0200 | [diff] [blame] | 2023 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2024 |  | 
|  | 2025 | /* | 
|  | 2026 | * As discussed on lkml, GENHD_FL_REMOVABLE should: | 
|  | 2027 | * | 
|  | 2028 | * - be set for removable media with permanent block devices | 
|  | 2029 | * - be unset for removable block devices with permanent media | 
|  | 2030 | * | 
|  | 2031 | * Since MMC block devices clearly fall under the second | 
|  | 2032 | * case, we do not set GENHD_FL_REMOVABLE.  Userspace | 
|  | 2033 | * should use the block device creation/destruction hotplug | 
|  | 2034 | * messages to tell when the card is present. | 
|  | 2035 | */ | 
|  | 2036 |  | 
| Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2037 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), | 
| Ulf Hansson | 9aaf343 | 2016-04-06 16:12:08 +0200 | [diff] [blame] | 2038 | "mmcblk%u%s", card->host->index, subname ? subname : ""); | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2039 |  | 
| Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2040 | if (mmc_card_mmc(card)) | 
|  | 2041 | blk_queue_logical_block_size(md->queue.queue, | 
|  | 2042 | card->ext_csd.data_sector_size); | 
|  | 2043 | else | 
|  | 2044 | blk_queue_logical_block_size(md->queue.queue, 512); | 
|  | 2045 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2046 | set_capacity(md->disk, size); | 
| Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2047 |  | 
| Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2048 | if (mmc_host_cmd23(card->host)) { | 
| Daniel Glöckner | 0ed50ab | 2016-08-30 14:17:30 +0200 | [diff] [blame] | 2049 | if ((mmc_card_mmc(card) && | 
|  | 2050 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || | 
| Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2051 | (mmc_card_sd(card) && | 
|  | 2052 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) | 
|  | 2053 | md->flags |= MMC_BLK_CMD23; | 
|  | 2054 | } | 
| Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2055 |  | 
|  | 2056 | if (mmc_card_mmc(card) && | 
|  | 2057 | md->flags & MMC_BLK_CMD23 && | 
|  | 2058 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || | 
|  | 2059 | card->ext_csd.rel_sectors)) { | 
|  | 2060 | md->flags |= MMC_BLK_REL_WR; | 
| Jens Axboe | e9d5c74 | 2016-03-30 10:17:20 -0600 | [diff] [blame] | 2061 | blk_queue_write_cache(md->queue.queue, true, true); | 
| Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2062 | } | 
|  | 2063 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | return md; | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2065 |  | 
|  | 2066 | err_putdisk: | 
|  | 2067 | put_disk(md->disk); | 
|  | 2068 | err_kfree: | 
|  | 2069 | kfree(md); | 
|  | 2070 | out: | 
| Heiner Kallweit | a04848c | 2017-02-01 19:44:22 +0100 | [diff] [blame] | 2071 | ida_simple_remove(&mmc_blk_ida, devidx); | 
| Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2072 | return ERR_PTR(ret); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | } | 
|  | 2074 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2075 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) | 
|  | 2076 | { | 
|  | 2077 | sector_t size; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2078 |  | 
|  | 2079 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { | 
|  | 2080 | /* | 
|  | 2081 | * The EXT_CSD sector count is in number or 512 byte | 
|  | 2082 | * sectors. | 
|  | 2083 | */ | 
|  | 2084 | size = card->ext_csd.sectors; | 
|  | 2085 | } else { | 
|  | 2086 | /* | 
|  | 2087 | * The CSD capacity field is in units of read_blkbits. | 
|  | 2088 | * set_capacity takes units of 512 bytes. | 
|  | 2089 | */ | 
| Kuninori Morimoto | 087de9e | 2015-05-11 07:35:28 +0000 | [diff] [blame] | 2090 | size = (typeof(sector_t))card->csd.capacity | 
|  | 2091 | << (card->csd.read_blkbits - 9); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2092 | } | 
|  | 2093 |  | 
| Tobias Klauser | 7a30f2a | 2015-01-21 15:56:44 +0100 | [diff] [blame] | 2094 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2095 | MMC_BLK_DATA_AREA_MAIN); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2096 | } | 
|  | 2097 |  | 
|  | 2098 | static int mmc_blk_alloc_part(struct mmc_card *card, | 
|  | 2099 | struct mmc_blk_data *md, | 
|  | 2100 | unsigned int part_type, | 
|  | 2101 | sector_t size, | 
|  | 2102 | bool default_ro, | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2103 | const char *subname, | 
|  | 2104 | int area_type) | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2105 | { | 
|  | 2106 | char cap_str[10]; | 
|  | 2107 | struct mmc_blk_data *part_md; | 
|  | 2108 |  | 
|  | 2109 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2110 | subname, area_type); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2111 | if (IS_ERR(part_md)) | 
|  | 2112 | return PTR_ERR(part_md); | 
|  | 2113 | part_md->part_type = part_type; | 
|  | 2114 | list_add(&part_md->part, &md->part); | 
|  | 2115 |  | 
| James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 2116 | string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2, | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2117 | cap_str, sizeof(cap_str)); | 
| Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2118 | pr_info("%s: %s %s partition %u %s\n", | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2119 | part_md->disk->disk_name, mmc_card_id(card), | 
|  | 2120 | mmc_card_name(card), part_md->part_type, cap_str); | 
|  | 2121 | return 0; | 
|  | 2122 | } | 
|  | 2123 |  | 
| Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2124 | /* MMC Physical partitions consist of two boot partitions and | 
|  | 2125 | * up to four general purpose partitions. | 
|  | 2126 | * For each partition enabled in EXT_CSD a block device will be allocatedi | 
|  | 2127 | * to provide access to the partition. | 
|  | 2128 | */ | 
|  | 2129 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2130 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) | 
|  | 2131 | { | 
| Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2132 | int idx, ret = 0; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2133 |  | 
|  | 2134 | if (!mmc_card_mmc(card)) | 
|  | 2135 | return 0; | 
|  | 2136 |  | 
| Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2137 | for (idx = 0; idx < card->nr_parts; idx++) { | 
|  | 2138 | if (card->part[idx].size) { | 
|  | 2139 | ret = mmc_blk_alloc_part(card, md, | 
|  | 2140 | card->part[idx].part_cfg, | 
|  | 2141 | card->part[idx].size >> 9, | 
|  | 2142 | card->part[idx].force_ro, | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2143 | card->part[idx].name, | 
|  | 2144 | card->part[idx].area_type); | 
| Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2145 | if (ret) | 
|  | 2146 | return ret; | 
|  | 2147 | } | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2148 | } | 
|  | 2149 |  | 
|  | 2150 | return ret; | 
|  | 2151 | } | 
|  | 2152 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2153 | static void mmc_blk_remove_req(struct mmc_blk_data *md) | 
|  | 2154 | { | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2155 | struct mmc_card *card; | 
|  | 2156 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2157 | if (md) { | 
| Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 2158 | /* | 
|  | 2159 | * Flush remaining requests and free queues. It | 
|  | 2160 | * is freeing the queue that stops new requests | 
|  | 2161 | * from being accepted. | 
|  | 2162 | */ | 
| Franck Jullien | 8efb83a | 2013-07-24 15:17:48 +0200 | [diff] [blame] | 2163 | card = md->queue.card; | 
| Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 2164 | mmc_cleanup_queue(&md->queue); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2165 | if (md->disk->flags & GENHD_FL_UP) { | 
|  | 2166 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2167 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && | 
|  | 2168 | card->ext_csd.boot_ro_lockable) | 
|  | 2169 | device_remove_file(disk_to_dev(md->disk), | 
|  | 2170 | &md->power_ro_lock); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2171 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2172 | del_gendisk(md->disk); | 
|  | 2173 | } | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2174 | mmc_blk_put(md); | 
|  | 2175 | } | 
|  | 2176 | } | 
|  | 2177 |  | 
|  | 2178 | static void mmc_blk_remove_parts(struct mmc_card *card, | 
|  | 2179 | struct mmc_blk_data *md) | 
|  | 2180 | { | 
|  | 2181 | struct list_head *pos, *q; | 
|  | 2182 | struct mmc_blk_data *part_md; | 
|  | 2183 |  | 
|  | 2184 | list_for_each_safe(pos, q, &md->part) { | 
|  | 2185 | part_md = list_entry(pos, struct mmc_blk_data, part); | 
|  | 2186 | list_del(pos); | 
|  | 2187 | mmc_blk_remove_req(part_md); | 
|  | 2188 | } | 
|  | 2189 | } | 
|  | 2190 |  | 
|  | 2191 | static int mmc_add_disk(struct mmc_blk_data *md) | 
|  | 2192 | { | 
|  | 2193 | int ret; | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2194 | struct mmc_card *card = md->queue.card; | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2195 |  | 
| Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 2196 | device_add_disk(md->parent, md->disk); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2197 | md->force_ro.show = force_ro_show; | 
|  | 2198 | md->force_ro.store = force_ro_store; | 
| Rabin Vincent | 641c318 | 2011-04-23 20:52:58 +0530 | [diff] [blame] | 2199 | sysfs_attr_init(&md->force_ro.attr); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2200 | md->force_ro.attr.name = "force_ro"; | 
|  | 2201 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; | 
|  | 2202 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); | 
|  | 2203 | if (ret) | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2204 | goto force_ro_fail; | 
|  | 2205 |  | 
|  | 2206 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && | 
|  | 2207 | card->ext_csd.boot_ro_lockable) { | 
| Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 2208 | umode_t mode; | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2209 |  | 
|  | 2210 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) | 
|  | 2211 | mode = S_IRUGO; | 
|  | 2212 | else | 
|  | 2213 | mode = S_IRUGO | S_IWUSR; | 
|  | 2214 |  | 
|  | 2215 | md->power_ro_lock.show = power_ro_lock_show; | 
|  | 2216 | md->power_ro_lock.store = power_ro_lock_store; | 
| Rabin Vincent | 00d9ac0 | 2012-02-01 16:31:56 +0100 | [diff] [blame] | 2217 | sysfs_attr_init(&md->power_ro_lock.attr); | 
| Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2218 | md->power_ro_lock.attr.mode = mode; | 
|  | 2219 | md->power_ro_lock.attr.name = | 
|  | 2220 | "ro_lock_until_next_power_on"; | 
|  | 2221 | ret = device_create_file(disk_to_dev(md->disk), | 
|  | 2222 | &md->power_ro_lock); | 
|  | 2223 | if (ret) | 
|  | 2224 | goto power_ro_lock_fail; | 
|  | 2225 | } | 
|  | 2226 | return ret; | 
|  | 2227 |  | 
|  | 2228 | power_ro_lock_fail: | 
|  | 2229 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); | 
|  | 2230 | force_ro_fail: | 
|  | 2231 | del_gendisk(md->disk); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2232 |  | 
|  | 2233 | return ret; | 
|  | 2234 | } | 
|  | 2235 |  | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2236 | static int mmc_blk_probe(struct mmc_card *card) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | { | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2238 | struct mmc_blk_data *md, *part_md; | 
| Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2239 | char cap_str[10]; | 
|  | 2240 |  | 
| Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 2241 | /* | 
|  | 2242 | * Check that the card supports the command class(es) we need. | 
|  | 2243 | */ | 
|  | 2244 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | return -ENODEV; | 
|  | 2246 |  | 
| Shawn Lin | 8c7cdbf | 2017-02-15 16:36:47 +0800 | [diff] [blame] | 2247 | mmc_fixup_device(card, mmc_blk_fixups); | 
| Lukas Czerner | 5204d00 | 2014-06-18 13:18:07 +0200 | [diff] [blame] | 2248 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2249 | md = mmc_blk_alloc(card); | 
| Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 2250 | if (IS_ERR(md)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | return PTR_ERR(md); | 
|  | 2252 |  | 
| James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 2253 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, | 
| Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2254 | cap_str, sizeof(cap_str)); | 
| Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2255 | pr_info("%s: %s %s %s %s\n", | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), | 
| Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2257 | cap_str, md->read_only ? "(ro)" : ""); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2259 | if (mmc_blk_alloc_parts(card, md)) | 
|  | 2260 | goto out; | 
|  | 2261 |  | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2262 | dev_set_drvdata(&card->dev, md); | 
| Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 2263 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2264 | if (mmc_add_disk(md)) | 
|  | 2265 | goto out; | 
|  | 2266 |  | 
|  | 2267 | list_for_each_entry(part_md, &md->part, part) { | 
|  | 2268 | if (mmc_add_disk(part_md)) | 
|  | 2269 | goto out; | 
|  | 2270 | } | 
| Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2271 |  | 
|  | 2272 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); | 
|  | 2273 | pm_runtime_use_autosuspend(&card->dev); | 
|  | 2274 |  | 
|  | 2275 | /* | 
|  | 2276 | * Don't enable runtime PM for SD-combo cards here. Leave that | 
|  | 2277 | * decision to be taken during the SDIO init sequence instead. | 
|  | 2278 | */ | 
|  | 2279 | if (card->type != MMC_TYPE_SD_COMBO) { | 
|  | 2280 | pm_runtime_set_active(&card->dev); | 
|  | 2281 | pm_runtime_enable(&card->dev); | 
|  | 2282 | } | 
|  | 2283 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2284 | return 0; | 
|  | 2285 |  | 
|  | 2286 | out: | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2287 | mmc_blk_remove_parts(card, md); | 
|  | 2288 | mmc_blk_remove_req(md); | 
| Ulf Hansson | 5865f28 | 2012-03-22 11:47:26 +0100 | [diff] [blame] | 2289 | return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | } | 
|  | 2291 |  | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2292 | static void mmc_blk_remove(struct mmc_card *card) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | { | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2294 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 |  | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2296 | mmc_blk_remove_parts(card, md); | 
| Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2297 | pm_runtime_get_sync(&card->dev); | 
| Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 2298 | mmc_claim_host(card->host); | 
|  | 2299 | mmc_blk_part_switch(card, md); | 
|  | 2300 | mmc_release_host(card->host); | 
| Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2301 | if (card->type != MMC_TYPE_SD_COMBO) | 
|  | 2302 | pm_runtime_disable(&card->dev); | 
|  | 2303 | pm_runtime_put_noidle(&card->dev); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2304 | mmc_blk_remove_req(md); | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2305 | dev_set_drvdata(&card->dev, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2306 | } | 
|  | 2307 |  | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2308 | static int _mmc_blk_suspend(struct mmc_card *card) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | { | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2310 | struct mmc_blk_data *part_md; | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2311 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2312 |  | 
|  | 2313 | if (md) { | 
|  | 2314 | mmc_queue_suspend(&md->queue); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2315 | list_for_each_entry(part_md, &md->part, part) { | 
|  | 2316 | mmc_queue_suspend(&part_md->queue); | 
|  | 2317 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | } | 
|  | 2319 | return 0; | 
|  | 2320 | } | 
|  | 2321 |  | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2322 | static void mmc_blk_shutdown(struct mmc_card *card) | 
| Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2323 | { | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2324 | _mmc_blk_suspend(card); | 
| Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2325 | } | 
|  | 2326 |  | 
| Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 2327 | #ifdef CONFIG_PM_SLEEP | 
|  | 2328 | static int mmc_blk_suspend(struct device *dev) | 
| Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2329 | { | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2330 | struct mmc_card *card = mmc_dev_to_card(dev); | 
|  | 2331 |  | 
|  | 2332 | return _mmc_blk_suspend(card); | 
| Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2333 | } | 
|  | 2334 |  | 
| Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 2335 | static int mmc_blk_resume(struct device *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | { | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2337 | struct mmc_blk_data *part_md; | 
| Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 2338 | struct mmc_blk_data *md = dev_get_drvdata(dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2339 |  | 
|  | 2340 | if (md) { | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2341 | /* | 
|  | 2342 | * Resume involves the card going into idle state, | 
|  | 2343 | * so current partition is always the main one. | 
|  | 2344 | */ | 
|  | 2345 | md->part_curr = md->part_type; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | mmc_queue_resume(&md->queue); | 
| Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2347 | list_for_each_entry(part_md, &md->part, part) { | 
|  | 2348 | mmc_queue_resume(&part_md->queue); | 
|  | 2349 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | } | 
|  | 2351 | return 0; | 
|  | 2352 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | #endif | 
|  | 2354 |  | 
| Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 2355 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); | 
|  | 2356 |  | 
| Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2357 | static struct mmc_driver mmc_driver = { | 
|  | 2358 | .drv		= { | 
|  | 2359 | .name	= "mmcblk", | 
|  | 2360 | .pm	= &mmc_blk_pm_ops, | 
|  | 2361 | }, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2362 | .probe		= mmc_blk_probe, | 
|  | 2363 | .remove		= mmc_blk_remove, | 
| Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2364 | .shutdown	= mmc_blk_shutdown, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | }; | 
|  | 2366 |  | 
|  | 2367 | static int __init mmc_blk_init(void) | 
|  | 2368 | { | 
| Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2369 | int res; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 |  | 
| Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2371 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) | 
|  | 2372 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); | 
|  | 2373 |  | 
| Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 2374 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); | 
| Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2375 |  | 
| Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2376 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); | 
|  | 2377 | if (res) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 |  | 
| Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2380 | res = mmc_register_driver(&mmc_driver); | 
|  | 2381 | if (res) | 
|  | 2382 | goto out2; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 |  | 
| Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2384 | return 0; | 
|  | 2385 | out2: | 
|  | 2386 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2387 | out: | 
|  | 2388 | return res; | 
|  | 2389 | } | 
|  | 2390 |  | 
|  | 2391 | static void __exit mmc_blk_exit(void) | 
|  | 2392 | { | 
|  | 2393 | mmc_unregister_driver(&mmc_driver); | 
| Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2394 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | } | 
|  | 2396 |  | 
|  | 2397 | module_init(mmc_blk_init); | 
|  | 2398 | module_exit(mmc_blk_exit); | 
|  | 2399 |  | 
|  | 2400 | MODULE_LICENSE("GPL"); | 
|  | 2401 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); | 
|  | 2402 |  |