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