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