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