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