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