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; |
| 1983 | return; |
| 1984 | } |
| 1985 | |
| 1986 | data_dir = rq_data_dir(req); |
| 1987 | |
| 1988 | if (data_dir == READ) { |
| 1989 | mq->num_of_potential_packed_wr_reqs = 0; |
| 1990 | mq->wr_packing_enabled = false; |
| 1991 | return; |
| 1992 | } else if (data_dir == WRITE) { |
| 1993 | mq->num_of_potential_packed_wr_reqs++; |
| 1994 | } |
| 1995 | |
| 1996 | if (mq->num_of_potential_packed_wr_reqs > |
| 1997 | mq->num_wr_reqs_to_start_packing) |
| 1998 | mq->wr_packing_enabled = true; |
| 1999 | } |
| 2000 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2001 | struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card) |
| 2002 | { |
| 2003 | if (!card) |
| 2004 | return NULL; |
| 2005 | |
| 2006 | return &card->wr_pack_stats; |
| 2007 | } |
| 2008 | EXPORT_SYMBOL(mmc_blk_get_packed_statistics); |
| 2009 | |
| 2010 | void mmc_blk_init_packed_statistics(struct mmc_card *card) |
| 2011 | { |
| 2012 | int max_num_of_packed_reqs = 0; |
| 2013 | |
| 2014 | if (!card || !card->wr_pack_stats.packing_events) |
| 2015 | return; |
| 2016 | |
| 2017 | max_num_of_packed_reqs = card->ext_csd.max_packed_writes; |
| 2018 | |
| 2019 | spin_lock(&card->wr_pack_stats.lock); |
| 2020 | memset(card->wr_pack_stats.packing_events, 0, |
| 2021 | (max_num_of_packed_reqs + 1) * |
| 2022 | sizeof(*card->wr_pack_stats.packing_events)); |
| 2023 | memset(&card->wr_pack_stats.pack_stop_reason, 0, |
| 2024 | sizeof(card->wr_pack_stats.pack_stop_reason)); |
| 2025 | card->wr_pack_stats.enabled = true; |
| 2026 | spin_unlock(&card->wr_pack_stats.lock); |
| 2027 | } |
| 2028 | EXPORT_SYMBOL(mmc_blk_init_packed_statistics); |
| 2029 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2030 | static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req) |
| 2031 | { |
| 2032 | struct request_queue *q = mq->queue; |
| 2033 | struct mmc_card *card = mq->card; |
| 2034 | struct request *cur = req, *next = NULL; |
| 2035 | struct mmc_blk_data *md = mq->data; |
| 2036 | struct mmc_queue_req *mqrq = mq->mqrq_cur; |
| 2037 | bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN; |
| 2038 | unsigned int req_sectors = 0, phys_segments = 0; |
| 2039 | unsigned int max_blk_count, max_phys_segs; |
| 2040 | bool put_back = true; |
| 2041 | u8 max_packed_rw = 0; |
| 2042 | u8 reqs = 0; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2043 | struct mmc_wr_pack_stats *stats = &card->wr_pack_stats; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2044 | |
Shawn Lin | 96e52da | 2016-08-26 08:49:55 +0800 | [diff] [blame] | 2045 | /* |
| 2046 | * We don't need to check packed for any further |
| 2047 | * operation of packed stuff as we set MMC_PACKED_NONE |
| 2048 | * and return zero for reqs if geting null packed. Also |
| 2049 | * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing |
| 2050 | * it again when removing blk req. |
| 2051 | */ |
| 2052 | if (!mqrq->packed) { |
| 2053 | md->flags &= (~MMC_BLK_PACKED_CMD); |
| 2054 | goto no_packed; |
| 2055 | } |
| 2056 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2057 | if (!(md->flags & MMC_BLK_PACKED_CMD)) |
| 2058 | goto no_packed; |
| 2059 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2060 | if (!mq->wr_packing_enabled) |
| 2061 | goto no_packed; |
| 2062 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2063 | if ((rq_data_dir(cur) == WRITE) && |
| 2064 | mmc_host_packed_wr(card->host)) |
| 2065 | max_packed_rw = card->ext_csd.max_packed_writes; |
| 2066 | |
| 2067 | if (max_packed_rw == 0) |
| 2068 | goto no_packed; |
| 2069 | |
| 2070 | if (mmc_req_rel_wr(cur) && |
| 2071 | (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) |
| 2072 | goto no_packed; |
| 2073 | |
| 2074 | if (mmc_large_sector(card) && |
| 2075 | !IS_ALIGNED(blk_rq_sectors(cur), 8)) |
| 2076 | goto no_packed; |
| 2077 | |
| 2078 | mmc_blk_clear_packed(mqrq); |
| 2079 | |
| 2080 | max_blk_count = min(card->host->max_blk_count, |
| 2081 | card->host->max_req_size >> 9); |
| 2082 | if (unlikely(max_blk_count > 0xffff)) |
| 2083 | max_blk_count = 0xffff; |
| 2084 | |
| 2085 | max_phys_segs = queue_max_segments(q); |
| 2086 | req_sectors += blk_rq_sectors(cur); |
| 2087 | phys_segments += cur->nr_phys_segments; |
| 2088 | |
| 2089 | if (rq_data_dir(cur) == WRITE) { |
| 2090 | req_sectors += mmc_large_sector(card) ? 8 : 1; |
| 2091 | phys_segments += mmc_calc_packed_hdr_segs(q, card); |
| 2092 | } |
| 2093 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2094 | spin_lock(&stats->lock); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2095 | do { |
| 2096 | if (reqs >= max_packed_rw - 1) { |
| 2097 | put_back = false; |
| 2098 | break; |
| 2099 | } |
| 2100 | |
| 2101 | spin_lock_irq(q->queue_lock); |
| 2102 | next = blk_fetch_request(q); |
| 2103 | spin_unlock_irq(q->queue_lock); |
| 2104 | if (!next) { |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2105 | MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2106 | put_back = false; |
| 2107 | break; |
| 2108 | } |
| 2109 | |
| 2110 | if (mmc_large_sector(card) && |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2111 | !IS_ALIGNED(blk_rq_sectors(next), 8)) { |
| 2112 | MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2113 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2114 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2115 | |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 2116 | if (req_op(next) == REQ_OP_DISCARD || |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 2117 | req_op(next) == REQ_OP_SECURE_ERASE || |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2118 | req_op(next) == REQ_OP_FLUSH) { |
| 2119 | if (req_op(next) != REQ_OP_SECURE_ERASE) |
| 2120 | MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2121 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2122 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2123 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2124 | if (rq_data_dir(cur) != rq_data_dir(next)) { |
| 2125 | MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2126 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2127 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2128 | |
| 2129 | if (mmc_req_rel_wr(next) && |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2130 | (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) { |
| 2131 | MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2132 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2133 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2134 | |
| 2135 | req_sectors += blk_rq_sectors(next); |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2136 | if (req_sectors > max_blk_count) { |
| 2137 | if (stats->enabled) |
| 2138 | stats->pack_stop_reason[EXCEEDS_SECTORS]++; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2139 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2140 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2141 | |
| 2142 | phys_segments += next->nr_phys_segments; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2143 | if (phys_segments > max_phys_segs) { |
| 2144 | MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2145 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2146 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2147 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2148 | if (rq_data_dir(next) == WRITE) |
| 2149 | mq->num_of_potential_packed_wr_reqs++; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2150 | list_add_tail(&next->queuelist, &mqrq->packed->list); |
| 2151 | cur = next; |
| 2152 | reqs++; |
| 2153 | } while (1); |
| 2154 | |
| 2155 | if (put_back) { |
| 2156 | spin_lock_irq(q->queue_lock); |
| 2157 | blk_requeue_request(q, next); |
| 2158 | spin_unlock_irq(q->queue_lock); |
| 2159 | } |
| 2160 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame^] | 2161 | if (stats->enabled) { |
| 2162 | if (reqs + 1 <= card->ext_csd.max_packed_writes) |
| 2163 | stats->packing_events[reqs + 1]++; |
| 2164 | if (reqs + 1 == max_packed_rw) |
| 2165 | MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD); |
| 2166 | } |
| 2167 | |
| 2168 | spin_unlock(&stats->lock); |
| 2169 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2170 | if (reqs > 0) { |
| 2171 | list_add(&req->queuelist, &mqrq->packed->list); |
| 2172 | mqrq->packed->nr_entries = ++reqs; |
| 2173 | mqrq->packed->retries = reqs; |
| 2174 | return reqs; |
| 2175 | } |
| 2176 | |
| 2177 | no_packed: |
| 2178 | mqrq->cmd_type = MMC_PACKED_NONE; |
| 2179 | return 0; |
| 2180 | } |
| 2181 | |
| 2182 | static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq, |
| 2183 | struct mmc_card *card, |
| 2184 | struct mmc_queue *mq) |
| 2185 | { |
| 2186 | struct mmc_blk_request *brq = &mqrq->brq; |
| 2187 | struct request *req = mqrq->req; |
| 2188 | struct request *prq; |
| 2189 | struct mmc_blk_data *md = mq->data; |
| 2190 | struct mmc_packed *packed = mqrq->packed; |
| 2191 | bool do_rel_wr, do_data_tag; |
Jiri Slaby | 3f2d266 | 2016-10-03 10:58:28 +0200 | [diff] [blame] | 2192 | __le32 *packed_cmd_hdr; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2193 | u8 hdr_blocks; |
| 2194 | u8 i = 1; |
| 2195 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2196 | mqrq->cmd_type = MMC_PACKED_WRITE; |
| 2197 | packed->blocks = 0; |
| 2198 | packed->idx_failure = MMC_PACKED_NR_IDX; |
| 2199 | |
| 2200 | packed_cmd_hdr = packed->cmd_hdr; |
| 2201 | memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr)); |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2202 | packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) | |
| 2203 | (PACKED_CMD_WR << 8) | PACKED_CMD_VER); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2204 | hdr_blocks = mmc_large_sector(card) ? 8 : 1; |
| 2205 | |
| 2206 | /* |
| 2207 | * Argument for each entry of packed group |
| 2208 | */ |
| 2209 | list_for_each_entry(prq, &packed->list, queuelist) { |
| 2210 | do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR); |
| 2211 | do_data_tag = (card->ext_csd.data_tag_unit_size) && |
| 2212 | (prq->cmd_flags & REQ_META) && |
| 2213 | (rq_data_dir(prq) == WRITE) && |
Adrian Hunter | d806b46 | 2016-06-10 16:22:16 +0300 | [diff] [blame] | 2214 | blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2215 | /* Argument of CMD23 */ |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2216 | packed_cmd_hdr[(i * 2)] = cpu_to_le32( |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2217 | (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) | |
| 2218 | (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) | |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2219 | blk_rq_sectors(prq)); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2220 | /* Argument of CMD18 or CMD25 */ |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2221 | packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32( |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2222 | mmc_card_blockaddr(card) ? |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2223 | blk_rq_pos(prq) : blk_rq_pos(prq) << 9); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2224 | packed->blocks += blk_rq_sectors(prq); |
| 2225 | i++; |
| 2226 | } |
| 2227 | |
| 2228 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
| 2229 | brq->mrq.cmd = &brq->cmd; |
| 2230 | brq->mrq.data = &brq->data; |
| 2231 | brq->mrq.sbc = &brq->sbc; |
| 2232 | brq->mrq.stop = &brq->stop; |
| 2233 | |
| 2234 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 2235 | brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks); |
| 2236 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 2237 | |
| 2238 | brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; |
| 2239 | brq->cmd.arg = blk_rq_pos(req); |
| 2240 | if (!mmc_card_blockaddr(card)) |
| 2241 | brq->cmd.arg <<= 9; |
| 2242 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 2243 | |
| 2244 | brq->data.blksz = 512; |
| 2245 | brq->data.blocks = packed->blocks + hdr_blocks; |
Jaehoon Chung | f53f110 | 2016-02-01 21:07:36 +0900 | [diff] [blame] | 2246 | brq->data.flags = MMC_DATA_WRITE; |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 2247 | brq->data.fault_injected = false; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2248 | |
| 2249 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 2250 | brq->stop.arg = 0; |
| 2251 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 2252 | |
| 2253 | mmc_set_data_timeout(&brq->data, card); |
| 2254 | |
| 2255 | brq->data.sg = mqrq->sg; |
| 2256 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); |
| 2257 | |
| 2258 | mqrq->mmc_active.mrq = &brq->mrq; |
| 2259 | mqrq->mmc_active.err_check = mmc_blk_packed_err_check; |
| 2260 | |
| 2261 | mmc_queue_bounce_pre(mqrq); |
| 2262 | } |
| 2263 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2264 | static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
| 2265 | struct mmc_blk_request *brq, struct request *req, |
| 2266 | int ret) |
| 2267 | { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2268 | struct mmc_queue_req *mq_rq; |
| 2269 | mq_rq = container_of(brq, struct mmc_queue_req, brq); |
| 2270 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2271 | /* |
| 2272 | * If this is an SD card and we're writing, we can first |
| 2273 | * mark the known good sectors as ok. |
| 2274 | * |
| 2275 | * If the card is not SD, we can still ok written sectors |
| 2276 | * as reported by the controller (which might be less than |
| 2277 | * the real number of written sectors, but never more). |
| 2278 | */ |
| 2279 | if (mmc_card_sd(card)) { |
| 2280 | u32 blocks; |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 2281 | if (!brq->data.fault_injected) { |
| 2282 | blocks = mmc_sd_num_wr_blocks(card); |
| 2283 | if (blocks != (u32)-1) |
| 2284 | ret = blk_end_request(req, 0, blocks << 9); |
| 2285 | } else |
| 2286 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2287 | } else { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2288 | if (!mmc_packed_cmd(mq_rq->cmd_type)) |
| 2289 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2290 | } |
| 2291 | return ret; |
| 2292 | } |
| 2293 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2294 | static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq) |
| 2295 | { |
| 2296 | struct request *prq; |
| 2297 | struct mmc_packed *packed = mq_rq->packed; |
| 2298 | int idx = packed->idx_failure, i = 0; |
| 2299 | int ret = 0; |
| 2300 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2301 | while (!list_empty(&packed->list)) { |
| 2302 | prq = list_entry_rq(packed->list.next); |
| 2303 | if (idx == i) { |
| 2304 | /* retry from error index */ |
| 2305 | packed->nr_entries -= idx; |
| 2306 | mq_rq->req = prq; |
| 2307 | ret = 1; |
| 2308 | |
| 2309 | if (packed->nr_entries == MMC_PACKED_NR_SINGLE) { |
| 2310 | list_del_init(&prq->queuelist); |
| 2311 | mmc_blk_clear_packed(mq_rq); |
| 2312 | } |
| 2313 | return ret; |
| 2314 | } |
| 2315 | list_del_init(&prq->queuelist); |
| 2316 | blk_end_request(prq, 0, blk_rq_bytes(prq)); |
| 2317 | i++; |
| 2318 | } |
| 2319 | |
| 2320 | mmc_blk_clear_packed(mq_rq); |
| 2321 | return ret; |
| 2322 | } |
| 2323 | |
| 2324 | static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq) |
| 2325 | { |
| 2326 | struct request *prq; |
| 2327 | struct mmc_packed *packed = mq_rq->packed; |
| 2328 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2329 | while (!list_empty(&packed->list)) { |
| 2330 | prq = list_entry_rq(packed->list.next); |
| 2331 | list_del_init(&prq->queuelist); |
| 2332 | blk_end_request(prq, -EIO, blk_rq_bytes(prq)); |
| 2333 | } |
| 2334 | |
| 2335 | mmc_blk_clear_packed(mq_rq); |
| 2336 | } |
| 2337 | |
| 2338 | static void mmc_blk_revert_packed_req(struct mmc_queue *mq, |
| 2339 | struct mmc_queue_req *mq_rq) |
| 2340 | { |
| 2341 | struct request *prq; |
| 2342 | struct request_queue *q = mq->queue; |
| 2343 | struct mmc_packed *packed = mq_rq->packed; |
| 2344 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2345 | while (!list_empty(&packed->list)) { |
| 2346 | prq = list_entry_rq(packed->list.prev); |
| 2347 | if (prq->queuelist.prev != &packed->list) { |
| 2348 | list_del_init(&prq->queuelist); |
| 2349 | spin_lock_irq(q->queue_lock); |
| 2350 | blk_requeue_request(mq->queue, prq); |
| 2351 | spin_unlock_irq(q->queue_lock); |
| 2352 | } else { |
| 2353 | list_del_init(&prq->queuelist); |
| 2354 | } |
| 2355 | } |
| 2356 | |
| 2357 | mmc_blk_clear_packed(mq_rq); |
| 2358 | } |
| 2359 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2360 | 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] | 2361 | { |
| 2362 | struct mmc_blk_data *md = mq->data; |
| 2363 | struct mmc_card *card = md->queue.card; |
| 2364 | struct mmc_blk_request *brq = &mq->mqrq_cur->brq; |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2365 | 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] | 2366 | enum mmc_blk_status status; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2367 | struct mmc_queue_req *mq_rq; |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2368 | struct request *req = rqc; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2369 | struct mmc_async_req *areq; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2370 | const u8 packed_nr = 2; |
| 2371 | u8 reqs = 0; |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2372 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 2373 | unsigned long waitfor = jiffies; |
| 2374 | #endif |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2375 | |
| 2376 | if (!rqc && !mq->mqrq_prev->req) |
| 2377 | return 0; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2378 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2379 | if (rqc) |
| 2380 | reqs = mmc_blk_prep_packed_list(mq, rqc); |
| 2381 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2382 | do { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2383 | if (rqc) { |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2384 | /* |
| 2385 | * When 4KB native sector is enabled, only 8 blocks |
| 2386 | * multiple read or write is allowed |
| 2387 | */ |
Yuan, Juntao | e87c856 | 2016-05-13 07:59:24 +0000 | [diff] [blame] | 2388 | if (mmc_large_sector(card) && |
| 2389 | !IS_ALIGNED(blk_rq_sectors(rqc), 8)) { |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2390 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", |
| 2391 | req->rq_disk->disk_name); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2392 | mq_rq = mq->mqrq_cur; |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2393 | goto cmd_abort; |
| 2394 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2395 | |
| 2396 | if (reqs >= packed_nr) |
| 2397 | mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur, |
| 2398 | card, mq); |
| 2399 | else |
| 2400 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2401 | areq = &mq->mqrq_cur->mmc_active; |
| 2402 | } else |
| 2403 | areq = NULL; |
| 2404 | areq = mmc_start_req(card->host, areq, (int *) &status); |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2405 | if (!areq) { |
| 2406 | if (status == MMC_BLK_NEW_REQUEST) |
| 2407 | mq->flags |= MMC_QUEUE_NEW_REQUEST; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2408 | return 0; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2409 | } |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 2410 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2411 | mq_rq = container_of(areq, struct mmc_queue_req, mmc_active); |
| 2412 | brq = &mq_rq->brq; |
| 2413 | req = mq_rq->req; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2414 | type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2415 | mmc_queue_bounce_post(mq_rq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 2416 | |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2417 | switch (status) { |
| 2418 | case MMC_BLK_SUCCESS: |
| 2419 | case MMC_BLK_PARTIAL: |
| 2420 | /* |
| 2421 | * A block was successfully transferred. |
| 2422 | */ |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2423 | mmc_blk_reset_success(md, type); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2424 | |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2425 | mmc_blk_simulate_delay(mq, rqc, waitfor); |
| 2426 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2427 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2428 | ret = mmc_blk_end_packed_req(mq_rq); |
| 2429 | break; |
| 2430 | } else { |
| 2431 | ret = blk_end_request(req, 0, |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2432 | brq->data.bytes_xfered); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2433 | } |
| 2434 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2435 | /* |
| 2436 | * If the blk_end_request function returns non-zero even |
| 2437 | * though all data has been transferred and no errors |
| 2438 | * were returned by the host controller, it's a bug. |
| 2439 | */ |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2440 | if (status == MMC_BLK_SUCCESS && ret) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2441 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2442 | __func__, blk_rq_bytes(req), |
| 2443 | brq->data.bytes_xfered); |
| 2444 | rqc = NULL; |
| 2445 | goto cmd_abort; |
| 2446 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2447 | break; |
| 2448 | case MMC_BLK_CMD_ERR: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2449 | ret = mmc_blk_cmd_err(md, card, brq, req, ret); |
Ding Wang | 29535f7 | 2015-05-18 20:14:15 +0800 | [diff] [blame] | 2450 | if (mmc_blk_reset(md, card->host, type)) |
| 2451 | goto cmd_abort; |
| 2452 | if (!ret) |
| 2453 | goto start_new_req; |
| 2454 | break; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2455 | case MMC_BLK_RETRY: |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2456 | retune_retry_done = brq->retune_retry_done; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2457 | if (retry++ < 5) |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 2458 | break; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2459 | /* Fall through */ |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2460 | case MMC_BLK_ABORT: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2461 | if (!mmc_blk_reset(md, card->host, type)) |
| 2462 | break; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 2463 | goto cmd_abort; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2464 | case MMC_BLK_DATA_ERR: { |
| 2465 | int err; |
| 2466 | |
| 2467 | err = mmc_blk_reset(md, card->host, type); |
| 2468 | if (!err) |
| 2469 | break; |
Sahitya Tummala | d0a1984 | 2014-10-31 09:46:20 +0530 | [diff] [blame] | 2470 | goto cmd_abort; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2471 | } |
| 2472 | case MMC_BLK_ECC_ERR: |
| 2473 | if (brq->data.blocks > 1) { |
| 2474 | /* Redo read one sector at a time */ |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2475 | pr_warn("%s: retrying using single block read\n", |
| 2476 | req->rq_disk->disk_name); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2477 | disable_multi = 1; |
| 2478 | break; |
| 2479 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2480 | /* |
| 2481 | * After an error, we redo I/O one sector at a |
| 2482 | * time, so we only reach here after trying to |
| 2483 | * read a single sector. |
| 2484 | */ |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 2485 | ret = blk_end_request(req, -EIO, |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2486 | brq->data.blksz); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2487 | if (!ret) |
| 2488 | goto start_new_req; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2489 | break; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 2490 | case MMC_BLK_NOMEDIUM: |
| 2491 | goto cmd_abort; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2492 | default: |
| 2493 | pr_err("%s: Unhandled return value (%d)", |
| 2494 | req->rq_disk->disk_name, status); |
| 2495 | goto cmd_abort; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 2496 | } |
| 2497 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2498 | if (ret) { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2499 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2500 | if (!mq_rq->packed->retries) |
| 2501 | goto cmd_abort; |
| 2502 | mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq); |
| 2503 | mmc_start_req(card->host, |
| 2504 | &mq_rq->mmc_active, NULL); |
| 2505 | } else { |
| 2506 | |
| 2507 | /* |
| 2508 | * In case of a incomplete request |
| 2509 | * prepare it again and resend. |
| 2510 | */ |
| 2511 | mmc_blk_rw_rq_prep(mq_rq, card, |
| 2512 | disable_multi, mq); |
| 2513 | mmc_start_req(card->host, |
| 2514 | &mq_rq->mmc_active, NULL); |
| 2515 | } |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2516 | mq_rq->brq.retune_retry_done = retune_retry_done; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2517 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2518 | } while (ret); |
| 2519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2520 | return 1; |
| 2521 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 2522 | cmd_abort: |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2523 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2524 | mmc_blk_abort_packed_req(mq_rq); |
| 2525 | } else { |
| 2526 | if (mmc_card_removed(card)) |
| 2527 | req->cmd_flags |= REQ_QUIET; |
| 2528 | while (ret) |
| 2529 | ret = blk_end_request(req, -EIO, |
| 2530 | blk_rq_cur_bytes(req)); |
| 2531 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2533 | start_new_req: |
| 2534 | if (rqc) { |
Seungwon Jeon | 7a81902 | 2013-01-22 19:48:07 +0900 | [diff] [blame] | 2535 | if (mmc_card_removed(card)) { |
| 2536 | rqc->cmd_flags |= REQ_QUIET; |
| 2537 | blk_end_request_all(rqc, -EIO); |
| 2538 | } else { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2539 | /* |
| 2540 | * If current request is packed, it needs to put back. |
| 2541 | */ |
| 2542 | if (mmc_packed_cmd(mq->mqrq_cur->cmd_type)) |
| 2543 | mmc_blk_revert_packed_req(mq, mq->mqrq_cur); |
| 2544 | |
Seungwon Jeon | 7a81902 | 2013-01-22 19:48:07 +0900 | [diff] [blame] | 2545 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
| 2546 | mmc_start_req(card->host, |
| 2547 | &mq->mqrq_cur->mmc_active, NULL); |
| 2548 | } |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2549 | } |
| 2550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | return 0; |
| 2552 | } |
| 2553 | |
Linus Walleij | 29eb7bd | 2016-09-20 11:34:38 +0200 | [diff] [blame] | 2554 | int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2555 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2556 | int ret; |
| 2557 | struct mmc_blk_data *md = mq->data; |
| 2558 | struct mmc_card *card = md->queue.card; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2559 | struct mmc_host *host = card->host; |
| 2560 | unsigned long flags; |
Adrian Hunter | 869c554 | 2016-08-25 14:11:43 -0600 | [diff] [blame] | 2561 | bool req_is_special = mmc_req_is_special(req); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2562 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2563 | if (req && !mq->mqrq_prev->req) |
| 2564 | /* claim host only for the first request */ |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2565 | mmc_get_card(card); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2566 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2567 | ret = mmc_blk_part_switch(card, md); |
| 2568 | if (ret) { |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2569 | if (req) { |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 2570 | blk_end_request_all(req, -EIO); |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2571 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2572 | ret = 0; |
| 2573 | goto out; |
| 2574 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2575 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2576 | mmc_blk_write_packing_control(mq, req); |
| 2577 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2578 | mq->flags &= ~MMC_QUEUE_NEW_REQUEST; |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 2579 | if (req && req_op(req) == REQ_OP_DISCARD) { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2580 | /* complete ongoing async transfer before issuing discard */ |
| 2581 | if (card->host->areq) |
| 2582 | mmc_blk_issue_rw_rq(mq, NULL); |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 2583 | ret = mmc_blk_issue_discard_rq(mq, req); |
| 2584 | } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) { |
| 2585 | /* complete ongoing async transfer before issuing secure erase*/ |
| 2586 | if (card->host->areq) |
| 2587 | mmc_blk_issue_rw_rq(mq, NULL); |
| 2588 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 2589 | } else if (req && req_op(req) == REQ_OP_FLUSH) { |
Jaehoon Chung | 393f9a0 | 2011-07-13 17:02:16 +0900 | [diff] [blame] | 2590 | /* complete ongoing async transfer before issuing flush */ |
| 2591 | if (card->host->areq) |
| 2592 | mmc_blk_issue_rw_rq(mq, NULL); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2593 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2594 | } else { |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2595 | if (!req && host->areq) { |
| 2596 | spin_lock_irqsave(&host->context_info.lock, flags); |
| 2597 | host->context_info.is_waiting_last_req = true; |
| 2598 | spin_unlock_irqrestore(&host->context_info.lock, flags); |
| 2599 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2600 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2601 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2602 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2603 | out: |
Adrian Hunter | 869c554 | 2016-08-25 14:11:43 -0600 | [diff] [blame] | 2604 | if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special) |
Seungwon Jeon | ef3a69c7 | 2013-03-14 15:17:13 +0900 | [diff] [blame] | 2605 | /* |
| 2606 | * Release host when there are no more requests |
| 2607 | * and after special request(discard, flush) is done. |
| 2608 | * In case sepecial request, there is no reentry to |
| 2609 | * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'. |
| 2610 | */ |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2611 | mmc_put_card(card); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2612 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2613 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2615 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 2616 | { |
| 2617 | return mmc_card_readonly(card) || |
| 2618 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 2619 | } |
| 2620 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2621 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 2622 | struct device *parent, |
| 2623 | sector_t size, |
| 2624 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2625 | const char *subname, |
| 2626 | int area_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | { |
| 2628 | struct mmc_blk_data *md; |
| 2629 | int devidx, ret; |
| 2630 | |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 2631 | again: |
| 2632 | if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL)) |
| 2633 | return ERR_PTR(-ENOMEM); |
| 2634 | |
| 2635 | spin_lock(&mmc_blk_lock); |
| 2636 | ret = ida_get_new(&mmc_blk_ida, &devidx); |
| 2637 | spin_unlock(&mmc_blk_lock); |
| 2638 | |
| 2639 | if (ret == -EAGAIN) |
| 2640 | goto again; |
| 2641 | else if (ret) |
| 2642 | return ERR_PTR(ret); |
| 2643 | |
| 2644 | if (devidx >= max_devices) { |
| 2645 | ret = -ENOSPC; |
| 2646 | goto out; |
| 2647 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 2649 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2650 | if (!md) { |
| 2651 | ret = -ENOMEM; |
| 2652 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2654 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2655 | md->area_type = area_type; |
| 2656 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2657 | /* |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2658 | * Set the read-only status based on the supported commands |
| 2659 | * and the write protect switch. |
| 2660 | */ |
| 2661 | md->read_only = mmc_blk_readonly(card); |
| 2662 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2663 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2664 | if (md->disk == NULL) { |
| 2665 | ret = -ENOMEM; |
| 2666 | goto err_kfree; |
| 2667 | } |
| 2668 | |
| 2669 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2670 | INIT_LIST_HEAD(&md->part); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2671 | md->usage = 1; |
| 2672 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 2673 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2674 | if (ret) |
| 2675 | goto err_putdisk; |
| 2676 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2677 | md->queue.data = md; |
| 2678 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2679 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2680 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2681 | md->disk->fops = &mmc_bdops; |
| 2682 | md->disk->private_data = md; |
| 2683 | md->disk->queue = md->queue.queue; |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 2684 | md->parent = parent; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2685 | set_disk_ro(md->disk, md->read_only || default_ro); |
Colin Cross | 382c55f | 2015-10-22 10:00:41 -0700 | [diff] [blame] | 2686 | md->disk->flags = GENHD_FL_EXT_DEVT; |
Ulf Hansson | f5b4d71 | 2014-09-03 11:02:23 +0200 | [diff] [blame] | 2687 | 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] | 2688 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2689 | |
| 2690 | /* |
| 2691 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 2692 | * |
| 2693 | * - be set for removable media with permanent block devices |
| 2694 | * - be unset for removable block devices with permanent media |
| 2695 | * |
| 2696 | * Since MMC block devices clearly fall under the second |
| 2697 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 2698 | * should use the block device creation/destruction hotplug |
| 2699 | * messages to tell when the card is present. |
| 2700 | */ |
| 2701 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2702 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
Ulf Hansson | 9aaf343 | 2016-04-06 16:12:08 +0200 | [diff] [blame] | 2703 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2704 | |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2705 | if (mmc_card_mmc(card)) |
| 2706 | blk_queue_logical_block_size(md->queue.queue, |
| 2707 | card->ext_csd.data_sector_size); |
| 2708 | else |
| 2709 | blk_queue_logical_block_size(md->queue.queue, 512); |
| 2710 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2711 | set_capacity(md->disk, size); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2712 | |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2713 | if (mmc_host_cmd23(card->host)) { |
Daniel Glöckner | 0ed50ab | 2016-08-30 14:17:30 +0200 | [diff] [blame] | 2714 | if ((mmc_card_mmc(card) && |
| 2715 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2716 | (mmc_card_sd(card) && |
| 2717 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) |
| 2718 | md->flags |= MMC_BLK_CMD23; |
| 2719 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2720 | |
| 2721 | if (mmc_card_mmc(card) && |
| 2722 | md->flags & MMC_BLK_CMD23 && |
| 2723 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
| 2724 | card->ext_csd.rel_sectors)) { |
| 2725 | md->flags |= MMC_BLK_REL_WR; |
Jens Axboe | e9d5c74 | 2016-03-30 10:17:20 -0600 | [diff] [blame] | 2726 | blk_queue_write_cache(md->queue.queue, true, true); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2727 | } |
| 2728 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2729 | if (mmc_card_mmc(card) && |
| 2730 | (area_type == MMC_BLK_DATA_AREA_MAIN) && |
| 2731 | (md->flags & MMC_BLK_CMD23) && |
| 2732 | card->ext_csd.packed_event_en) { |
| 2733 | if (!mmc_packed_init(&md->queue, card)) |
| 2734 | md->flags |= MMC_BLK_PACKED_CMD; |
| 2735 | } |
| 2736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2737 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2738 | |
| 2739 | err_putdisk: |
| 2740 | put_disk(md->disk); |
| 2741 | err_kfree: |
| 2742 | kfree(md); |
| 2743 | out: |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 2744 | spin_lock(&mmc_blk_lock); |
| 2745 | ida_remove(&mmc_blk_ida, devidx); |
| 2746 | spin_unlock(&mmc_blk_lock); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2747 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
| 2749 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2750 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 2751 | { |
| 2752 | sector_t size; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2753 | |
| 2754 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 2755 | /* |
| 2756 | * The EXT_CSD sector count is in number or 512 byte |
| 2757 | * sectors. |
| 2758 | */ |
| 2759 | size = card->ext_csd.sectors; |
| 2760 | } else { |
| 2761 | /* |
| 2762 | * The CSD capacity field is in units of read_blkbits. |
| 2763 | * set_capacity takes units of 512 bytes. |
| 2764 | */ |
Kuninori Morimoto | 087de9e | 2015-05-11 07:35:28 +0000 | [diff] [blame] | 2765 | size = (typeof(sector_t))card->csd.capacity |
| 2766 | << (card->csd.read_blkbits - 9); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2767 | } |
| 2768 | |
Tobias Klauser | 7a30f2a | 2015-01-21 15:56:44 +0100 | [diff] [blame] | 2769 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2770 | MMC_BLK_DATA_AREA_MAIN); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2771 | } |
| 2772 | |
| 2773 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 2774 | struct mmc_blk_data *md, |
| 2775 | unsigned int part_type, |
| 2776 | sector_t size, |
| 2777 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2778 | const char *subname, |
| 2779 | int area_type) |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2780 | { |
| 2781 | char cap_str[10]; |
| 2782 | struct mmc_blk_data *part_md; |
| 2783 | |
| 2784 | 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] | 2785 | subname, area_type); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2786 | if (IS_ERR(part_md)) |
| 2787 | return PTR_ERR(part_md); |
| 2788 | part_md->part_type = part_type; |
| 2789 | list_add(&part_md->part, &md->part); |
| 2790 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 2791 | 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] | 2792 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2793 | pr_info("%s: %s %s partition %u %s\n", |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2794 | part_md->disk->disk_name, mmc_card_id(card), |
| 2795 | mmc_card_name(card), part_md->part_type, cap_str); |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2799 | /* MMC Physical partitions consist of two boot partitions and |
| 2800 | * up to four general purpose partitions. |
| 2801 | * For each partition enabled in EXT_CSD a block device will be allocatedi |
| 2802 | * to provide access to the partition. |
| 2803 | */ |
| 2804 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2805 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 2806 | { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2807 | int idx, ret = 0; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2808 | |
| 2809 | if (!mmc_card_mmc(card)) |
| 2810 | return 0; |
| 2811 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2812 | for (idx = 0; idx < card->nr_parts; idx++) { |
| 2813 | if (card->part[idx].size) { |
| 2814 | ret = mmc_blk_alloc_part(card, md, |
| 2815 | card->part[idx].part_cfg, |
| 2816 | card->part[idx].size >> 9, |
| 2817 | card->part[idx].force_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2818 | card->part[idx].name, |
| 2819 | card->part[idx].area_type); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2820 | if (ret) |
| 2821 | return ret; |
| 2822 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2823 | } |
| 2824 | |
| 2825 | return ret; |
| 2826 | } |
| 2827 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2828 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 2829 | { |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2830 | struct mmc_card *card; |
| 2831 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2832 | if (md) { |
Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 2833 | /* |
| 2834 | * Flush remaining requests and free queues. It |
| 2835 | * is freeing the queue that stops new requests |
| 2836 | * from being accepted. |
| 2837 | */ |
Franck Jullien | 8efb83a | 2013-07-24 15:17:48 +0200 | [diff] [blame] | 2838 | card = md->queue.card; |
Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 2839 | mmc_cleanup_queue(&md->queue); |
| 2840 | if (md->flags & MMC_BLK_PACKED_CMD) |
| 2841 | mmc_packed_clean(&md->queue); |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2842 | device_remove_file(disk_to_dev(md->disk), |
| 2843 | &md->num_wr_reqs_to_start_packing); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2844 | if (md->disk->flags & GENHD_FL_UP) { |
| 2845 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2846 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 2847 | card->ext_csd.boot_ro_lockable) |
| 2848 | device_remove_file(disk_to_dev(md->disk), |
| 2849 | &md->power_ro_lock); |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2850 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 2851 | device_remove_file(disk_to_dev(md->disk), |
| 2852 | &dev_attr_max_write_speed); |
| 2853 | device_remove_file(disk_to_dev(md->disk), |
| 2854 | &dev_attr_max_read_speed); |
| 2855 | device_remove_file(disk_to_dev(md->disk), |
| 2856 | &dev_attr_cache_size); |
| 2857 | #endif |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2858 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2859 | del_gendisk(md->disk); |
| 2860 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2861 | mmc_blk_put(md); |
| 2862 | } |
| 2863 | } |
| 2864 | |
| 2865 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 2866 | struct mmc_blk_data *md) |
| 2867 | { |
| 2868 | struct list_head *pos, *q; |
| 2869 | struct mmc_blk_data *part_md; |
| 2870 | |
| 2871 | list_for_each_safe(pos, q, &md->part) { |
| 2872 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 2873 | list_del(pos); |
| 2874 | mmc_blk_remove_req(part_md); |
| 2875 | } |
| 2876 | } |
| 2877 | |
| 2878 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 2879 | { |
| 2880 | int ret; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2881 | struct mmc_card *card = md->queue.card; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2882 | |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 2883 | device_add_disk(md->parent, md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2884 | md->force_ro.show = force_ro_show; |
| 2885 | md->force_ro.store = force_ro_store; |
Rabin Vincent | 641c318 | 2011-04-23 20:52:58 +0530 | [diff] [blame] | 2886 | sysfs_attr_init(&md->force_ro.attr); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2887 | md->force_ro.attr.name = "force_ro"; |
| 2888 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 2889 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 2890 | if (ret) |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2891 | goto force_ro_fail; |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2892 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 2893 | atomic_set(&md->queue.max_write_speed, max_write_speed); |
| 2894 | ret = device_create_file(disk_to_dev(md->disk), |
| 2895 | &dev_attr_max_write_speed); |
| 2896 | if (ret) |
| 2897 | goto max_write_speed_fail; |
| 2898 | atomic_set(&md->queue.max_read_speed, max_read_speed); |
| 2899 | ret = device_create_file(disk_to_dev(md->disk), |
| 2900 | &dev_attr_max_read_speed); |
| 2901 | if (ret) |
| 2902 | goto max_read_speed_fail; |
| 2903 | atomic_set(&md->queue.cache_size, cache_size); |
| 2904 | atomic_long_set(&md->queue.cache_used, 0); |
| 2905 | md->queue.cache_jiffies = jiffies; |
| 2906 | ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size); |
| 2907 | if (ret) |
| 2908 | goto cache_size_fail; |
| 2909 | #endif |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2910 | |
| 2911 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 2912 | card->ext_csd.boot_ro_lockable) { |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 2913 | umode_t mode; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2914 | |
| 2915 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) |
| 2916 | mode = S_IRUGO; |
| 2917 | else |
| 2918 | mode = S_IRUGO | S_IWUSR; |
| 2919 | |
| 2920 | md->power_ro_lock.show = power_ro_lock_show; |
| 2921 | md->power_ro_lock.store = power_ro_lock_store; |
Rabin Vincent | 00d9ac0 | 2012-02-01 16:31:56 +0100 | [diff] [blame] | 2922 | sysfs_attr_init(&md->power_ro_lock.attr); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2923 | md->power_ro_lock.attr.mode = mode; |
| 2924 | md->power_ro_lock.attr.name = |
| 2925 | "ro_lock_until_next_power_on"; |
| 2926 | ret = device_create_file(disk_to_dev(md->disk), |
| 2927 | &md->power_ro_lock); |
| 2928 | if (ret) |
| 2929 | goto power_ro_lock_fail; |
| 2930 | } |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2931 | |
| 2932 | md->num_wr_reqs_to_start_packing.show = |
| 2933 | num_wr_reqs_to_start_packing_show; |
| 2934 | md->num_wr_reqs_to_start_packing.store = |
| 2935 | num_wr_reqs_to_start_packing_store; |
| 2936 | sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr); |
| 2937 | md->num_wr_reqs_to_start_packing.attr.name = |
| 2938 | "num_wr_reqs_to_start_packing"; |
| 2939 | md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR; |
| 2940 | ret = device_create_file(disk_to_dev(md->disk), |
| 2941 | &md->num_wr_reqs_to_start_packing); |
| 2942 | if (ret) |
| 2943 | goto power_ro_lock_fail; |
| 2944 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2945 | return ret; |
| 2946 | |
| 2947 | power_ro_lock_fail: |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2948 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 2949 | device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size); |
| 2950 | cache_size_fail: |
| 2951 | device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed); |
| 2952 | max_read_speed_fail: |
| 2953 | device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed); |
| 2954 | max_write_speed_fail: |
| 2955 | #endif |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2956 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 2957 | force_ro_fail: |
| 2958 | del_gendisk(md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2959 | |
| 2960 | return ret; |
| 2961 | } |
| 2962 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 2963 | static const struct mmc_fixup blk_fixups[] = |
| 2964 | { |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2965 | MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2966 | MMC_QUIRK_INAND_CMD38), |
| 2967 | MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2968 | MMC_QUIRK_INAND_CMD38), |
| 2969 | MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2970 | MMC_QUIRK_INAND_CMD38), |
| 2971 | MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2972 | MMC_QUIRK_INAND_CMD38), |
| 2973 | MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2974 | MMC_QUIRK_INAND_CMD38), |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2975 | |
| 2976 | /* |
| 2977 | * Some MMC cards experience performance degradation with CMD23 |
| 2978 | * instead of CMD12-bounded multiblock transfers. For now we'll |
| 2979 | * black list what's bad... |
| 2980 | * - Certain Toshiba cards. |
| 2981 | * |
| 2982 | * N.B. This doesn't affect SD cards. |
| 2983 | */ |
Yangbo Lu | 7d70d47 | 2015-07-10 11:44:03 +0800 | [diff] [blame] | 2984 | MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, |
| 2985 | MMC_QUIRK_BLK_NO_CMD23), |
| 2986 | MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, |
| 2987 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2988 | MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2989 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2990 | MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2991 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2992 | MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2993 | MMC_QUIRK_BLK_NO_CMD23), |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 2994 | |
| 2995 | /* |
Matt Gumbel | 32ecd32 | 2016-05-20 10:33:46 +0300 | [diff] [blame] | 2996 | * 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] | 2997 | */ |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2998 | 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] | 2999 | MMC_QUIRK_LONG_READ_TIME), |
Matt Gumbel | 32ecd32 | 2016-05-20 10:33:46 +0300 | [diff] [blame] | 3000 | MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
| 3001 | MMC_QUIRK_LONG_READ_TIME), |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 3002 | |
Ian Chen | 3550ccd | 2012-08-29 15:05:36 +0900 | [diff] [blame] | 3003 | /* |
Guoping Yu | 3c984a9 | 2014-08-06 12:44:55 +0800 | [diff] [blame] | 3004 | * Some Samsung MMC cards need longer data read timeout than |
| 3005 | * indicated in CSD. |
| 3006 | */ |
| 3007 | MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc, |
| 3008 | MMC_QUIRK_LONG_READ_TIME), |
| 3009 | |
| 3010 | /* |
Ian Chen | 3550ccd | 2012-08-29 15:05:36 +0900 | [diff] [blame] | 3011 | * On these Samsung MoviNAND parts, performing secure erase or |
| 3012 | * secure trim can result in unrecoverable corruption due to a |
| 3013 | * firmware bug. |
| 3014 | */ |
| 3015 | MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3016 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3017 | MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3018 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3019 | MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3020 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3021 | MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3022 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3023 | MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3024 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3025 | MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3026 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3027 | MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3028 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3029 | MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3030 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3031 | |
Shawn Lin | b5b4ff0 | 2015-08-12 13:08:32 +0800 | [diff] [blame] | 3032 | /* |
| 3033 | * On Some Kingston eMMCs, performing trim can result in |
| 3034 | * unrecoverable data conrruption occasionally due to a firmware bug. |
| 3035 | */ |
| 3036 | MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, |
| 3037 | MMC_QUIRK_TRIM_BROKEN), |
| 3038 | MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, |
| 3039 | MMC_QUIRK_TRIM_BROKEN), |
| 3040 | |
Pratibhasagar V | 8d664e3 | 2014-12-03 18:26:42 +0200 | [diff] [blame] | 3041 | /* Some INAND MCP devices advertise incorrect timeout values */ |
| 3042 | MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc, |
| 3043 | MMC_QUIRK_INAND_DATA_TIMEOUT), |
| 3044 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 3045 | END_FIXUP |
| 3046 | }; |
| 3047 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3048 | static int mmc_blk_probe(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3050 | struct mmc_blk_data *md, *part_md; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3051 | char cap_str[10]; |
| 3052 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 3053 | /* |
| 3054 | * Check that the card supports the command class(es) we need. |
| 3055 | */ |
| 3056 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | return -ENODEV; |
| 3058 | |
Lukas Czerner | 5204d00 | 2014-06-18 13:18:07 +0200 | [diff] [blame] | 3059 | mmc_fixup_device(card, blk_fixups); |
| 3060 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | md = mmc_blk_alloc(card); |
| 3062 | if (IS_ERR(md)) |
| 3063 | return PTR_ERR(md); |
| 3064 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 3065 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3066 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3067 | pr_info("%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3068 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3069 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3071 | if (mmc_blk_alloc_parts(card, md)) |
| 3072 | goto out; |
| 3073 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3074 | dev_set_drvdata(&card->dev, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 3075 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3076 | if (mmc_add_disk(md)) |
| 3077 | goto out; |
| 3078 | |
| 3079 | list_for_each_entry(part_md, &md->part, part) { |
| 3080 | if (mmc_add_disk(part_md)) |
| 3081 | goto out; |
| 3082 | } |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3083 | |
| 3084 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); |
| 3085 | pm_runtime_use_autosuspend(&card->dev); |
| 3086 | |
| 3087 | /* |
| 3088 | * Don't enable runtime PM for SD-combo cards here. Leave that |
| 3089 | * decision to be taken during the SDIO init sequence instead. |
| 3090 | */ |
| 3091 | if (card->type != MMC_TYPE_SD_COMBO) { |
| 3092 | pm_runtime_set_active(&card->dev); |
| 3093 | pm_runtime_enable(&card->dev); |
| 3094 | } |
| 3095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3096 | return 0; |
| 3097 | |
| 3098 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3099 | mmc_blk_remove_parts(card, md); |
| 3100 | mmc_blk_remove_req(md); |
Ulf Hansson | 5865f28 | 2012-03-22 11:47:26 +0100 | [diff] [blame] | 3101 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | } |
| 3103 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3104 | static void mmc_blk_remove(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3105 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3106 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3108 | mmc_blk_remove_parts(card, md); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3109 | pm_runtime_get_sync(&card->dev); |
Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 3110 | mmc_claim_host(card->host); |
| 3111 | mmc_blk_part_switch(card, md); |
| 3112 | mmc_release_host(card->host); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3113 | if (card->type != MMC_TYPE_SD_COMBO) |
| 3114 | pm_runtime_disable(&card->dev); |
| 3115 | pm_runtime_put_noidle(&card->dev); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3116 | mmc_blk_remove_req(md); |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3117 | dev_set_drvdata(&card->dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3120 | static int _mmc_blk_suspend(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3121 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3122 | struct mmc_blk_data *part_md; |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3123 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3124 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3125 | |
| 3126 | if (md) { |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3127 | rc = mmc_queue_suspend(&md->queue); |
| 3128 | if (rc) |
| 3129 | goto out; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3130 | list_for_each_entry(part_md, &md->part, part) { |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3131 | rc = mmc_queue_suspend(&part_md->queue); |
| 3132 | if (rc) |
| 3133 | goto out_resume; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3134 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3135 | } |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3136 | goto out; |
| 3137 | |
| 3138 | out_resume: |
| 3139 | mmc_queue_resume(&md->queue); |
| 3140 | list_for_each_entry(part_md, &md->part, part) { |
| 3141 | mmc_queue_resume(&part_md->queue); |
| 3142 | } |
| 3143 | out: |
| 3144 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | } |
| 3146 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3147 | static void mmc_blk_shutdown(struct mmc_card *card) |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3148 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3149 | _mmc_blk_suspend(card); |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3150 | } |
| 3151 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 3152 | #ifdef CONFIG_PM_SLEEP |
| 3153 | static int mmc_blk_suspend(struct device *dev) |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3154 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3155 | struct mmc_card *card = mmc_dev_to_card(dev); |
| 3156 | |
| 3157 | return _mmc_blk_suspend(card); |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3158 | } |
| 3159 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 3160 | static int mmc_blk_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3161 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3162 | struct mmc_blk_data *part_md; |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 3163 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3164 | |
| 3165 | if (md) { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3166 | /* |
| 3167 | * Resume involves the card going into idle state, |
| 3168 | * so current partition is always the main one. |
| 3169 | */ |
| 3170 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3172 | list_for_each_entry(part_md, &md->part, part) { |
| 3173 | mmc_queue_resume(&part_md->queue); |
| 3174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | } |
| 3176 | return 0; |
| 3177 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | #endif |
| 3179 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 3180 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
| 3181 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3182 | static struct mmc_driver mmc_driver = { |
| 3183 | .drv = { |
| 3184 | .name = "mmcblk", |
| 3185 | .pm = &mmc_blk_pm_ops, |
| 3186 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | .probe = mmc_blk_probe, |
| 3188 | .remove = mmc_blk_remove, |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3189 | .shutdown = mmc_blk_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | }; |
| 3191 | |
| 3192 | static int __init mmc_blk_init(void) |
| 3193 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3194 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3195 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 3196 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 3197 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 3198 | |
Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 3199 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 3200 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 3201 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 3202 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3203 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3204 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3205 | res = mmc_register_driver(&mmc_driver); |
| 3206 | if (res) |
| 3207 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3209 | return 0; |
| 3210 | out2: |
| 3211 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | out: |
| 3213 | return res; |
| 3214 | } |
| 3215 | |
| 3216 | static void __exit mmc_blk_exit(void) |
| 3217 | { |
| 3218 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 3219 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3220 | } |
| 3221 | |
| 3222 | module_init(mmc_blk_init); |
| 3223 | module_exit(mmc_blk_exit); |
| 3224 | |
| 3225 | MODULE_LICENSE("GPL"); |
| 3226 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 3227 | |