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 | |
| 1412 | /* |
| 1413 | * In case the packing control is not supported by the host, it should |
| 1414 | * not have an effect on the write packing. Therefore we have to enable |
| 1415 | * the write packing |
| 1416 | */ |
| 1417 | if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) { |
| 1418 | mq->wr_packing_enabled = true; |
| 1419 | return; |
| 1420 | } |
| 1421 | |
| 1422 | if (!req || (req && (req->cmd_flags & REQ_FLUSH))) { |
| 1423 | if (mq->num_of_potential_packed_wr_reqs > |
| 1424 | mq->num_wr_reqs_to_start_packing) |
| 1425 | mq->wr_packing_enabled = true; |
Tatyana Brokhman | 285ee17 | 2012-10-07 10:26:27 +0200 | [diff] [blame] | 1426 | mq->num_of_potential_packed_wr_reqs = 0; |
Tatyana Brokhman | 0cc7640 | 2012-10-07 09:52:16 +0200 | [diff] [blame] | 1427 | return; |
| 1428 | } |
| 1429 | |
| 1430 | data_dir = rq_data_dir(req); |
| 1431 | |
| 1432 | if (data_dir == READ) { |
| 1433 | mq->num_of_potential_packed_wr_reqs = 0; |
| 1434 | mq->wr_packing_enabled = false; |
| 1435 | return; |
| 1436 | } else if (data_dir == WRITE) { |
| 1437 | mq->num_of_potential_packed_wr_reqs++; |
| 1438 | } |
| 1439 | |
| 1440 | if (mq->num_of_potential_packed_wr_reqs > |
| 1441 | mq->num_wr_reqs_to_start_packing) |
| 1442 | mq->wr_packing_enabled = true; |
| 1443 | |
| 1444 | } |
| 1445 | |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1446 | struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card) |
| 1447 | { |
| 1448 | if (!card) |
| 1449 | return NULL; |
| 1450 | |
| 1451 | return &card->wr_pack_stats; |
| 1452 | } |
| 1453 | EXPORT_SYMBOL(mmc_blk_get_packed_statistics); |
| 1454 | |
| 1455 | void mmc_blk_init_packed_statistics(struct mmc_card *card) |
| 1456 | { |
| 1457 | int max_num_of_packed_reqs = 0; |
| 1458 | |
| 1459 | if (!card || !card->wr_pack_stats.packing_events) |
| 1460 | return; |
| 1461 | |
| 1462 | max_num_of_packed_reqs = card->ext_csd.max_packed_writes; |
| 1463 | |
| 1464 | spin_lock(&card->wr_pack_stats.lock); |
| 1465 | memset(card->wr_pack_stats.packing_events, 0, |
| 1466 | (max_num_of_packed_reqs + 1) * |
| 1467 | sizeof(*card->wr_pack_stats.packing_events)); |
| 1468 | memset(&card->wr_pack_stats.pack_stop_reason, 0, |
| 1469 | sizeof(card->wr_pack_stats.pack_stop_reason)); |
| 1470 | card->wr_pack_stats.enabled = true; |
| 1471 | spin_unlock(&card->wr_pack_stats.lock); |
| 1472 | } |
| 1473 | EXPORT_SYMBOL(mmc_blk_init_packed_statistics); |
| 1474 | |
Tatyana Brokhman | 09b010d | 2012-10-09 13:50:56 +0200 | [diff] [blame] | 1475 | void print_mmc_packing_stats(struct mmc_card *card) |
| 1476 | { |
| 1477 | int i; |
| 1478 | int max_num_of_packed_reqs = 0; |
| 1479 | |
| 1480 | if ((!card) || (!card->wr_pack_stats.packing_events)) |
| 1481 | return; |
| 1482 | |
| 1483 | max_num_of_packed_reqs = card->ext_csd.max_packed_writes; |
| 1484 | |
| 1485 | spin_lock(&card->wr_pack_stats.lock); |
| 1486 | |
| 1487 | pr_info("%s: write packing statistics:\n", |
| 1488 | mmc_hostname(card->host)); |
| 1489 | |
| 1490 | for (i = 1 ; i <= max_num_of_packed_reqs ; ++i) { |
| 1491 | if (card->wr_pack_stats.packing_events[i] != 0) |
| 1492 | pr_info("%s: Packed %d reqs - %d times\n", |
| 1493 | mmc_hostname(card->host), i, |
| 1494 | card->wr_pack_stats.packing_events[i]); |
| 1495 | } |
| 1496 | |
| 1497 | pr_info("%s: stopped packing due to the following reasons:\n", |
| 1498 | mmc_hostname(card->host)); |
| 1499 | |
| 1500 | if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS]) |
| 1501 | pr_info("%s: %d times: exceedmax num of segments\n", |
| 1502 | mmc_hostname(card->host), |
| 1503 | card->wr_pack_stats.pack_stop_reason[EXCEEDS_SEGMENTS]); |
| 1504 | if (card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS]) |
| 1505 | pr_info("%s: %d times: exceeding the max num of sectors\n", |
| 1506 | mmc_hostname(card->host), |
| 1507 | card->wr_pack_stats.pack_stop_reason[EXCEEDS_SECTORS]); |
| 1508 | if (card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR]) |
| 1509 | pr_info("%s: %d times: wrong data direction\n", |
| 1510 | mmc_hostname(card->host), |
| 1511 | card->wr_pack_stats.pack_stop_reason[WRONG_DATA_DIR]); |
| 1512 | if (card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD]) |
| 1513 | pr_info("%s: %d times: flush or discard\n", |
| 1514 | mmc_hostname(card->host), |
| 1515 | card->wr_pack_stats.pack_stop_reason[FLUSH_OR_DISCARD]); |
| 1516 | if (card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE]) |
| 1517 | pr_info("%s: %d times: empty queue\n", |
| 1518 | mmc_hostname(card->host), |
| 1519 | card->wr_pack_stats.pack_stop_reason[EMPTY_QUEUE]); |
| 1520 | if (card->wr_pack_stats.pack_stop_reason[REL_WRITE]) |
| 1521 | pr_info("%s: %d times: rel write\n", |
| 1522 | mmc_hostname(card->host), |
| 1523 | card->wr_pack_stats.pack_stop_reason[REL_WRITE]); |
| 1524 | if (card->wr_pack_stats.pack_stop_reason[THRESHOLD]) |
| 1525 | pr_info("%s: %d times: Threshold\n", |
| 1526 | mmc_hostname(card->host), |
| 1527 | card->wr_pack_stats.pack_stop_reason[THRESHOLD]); |
| 1528 | |
| 1529 | spin_unlock(&card->wr_pack_stats.lock); |
| 1530 | } |
| 1531 | EXPORT_SYMBOL(print_mmc_packing_stats); |
| 1532 | |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1533 | static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req) |
| 1534 | { |
| 1535 | struct request_queue *q = mq->queue; |
| 1536 | struct mmc_card *card = mq->card; |
| 1537 | struct request *cur = req, *next = NULL; |
| 1538 | struct mmc_blk_data *md = mq->data; |
| 1539 | bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN; |
| 1540 | unsigned int req_sectors = 0, phys_segments = 0; |
| 1541 | unsigned int max_blk_count, max_phys_segs; |
| 1542 | u8 put_back = 0; |
| 1543 | u8 max_packed_rw = 0; |
| 1544 | u8 reqs = 0; |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1545 | struct mmc_wr_pack_stats *stats = &card->wr_pack_stats; |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1546 | |
| 1547 | mmc_blk_clear_packed(mq->mqrq_cur); |
| 1548 | |
| 1549 | if (!(md->flags & MMC_BLK_CMD23) || |
| 1550 | !card->ext_csd.packed_event_en) |
| 1551 | goto no_packed; |
| 1552 | |
Tatyana Brokhman | 0cc7640 | 2012-10-07 09:52:16 +0200 | [diff] [blame] | 1553 | if (!mq->wr_packing_enabled) |
| 1554 | goto no_packed; |
| 1555 | |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1556 | if ((rq_data_dir(cur) == WRITE) && |
| 1557 | (card->host->caps2 & MMC_CAP2_PACKED_WR)) |
| 1558 | max_packed_rw = card->ext_csd.max_packed_writes; |
| 1559 | |
| 1560 | if (max_packed_rw == 0) |
| 1561 | goto no_packed; |
| 1562 | |
| 1563 | if (mmc_req_rel_wr(cur) && |
| 1564 | (md->flags & MMC_BLK_REL_WR) && |
| 1565 | !en_rel_wr) |
| 1566 | goto no_packed; |
| 1567 | |
| 1568 | if (mmc_large_sec(card) && |
| 1569 | !IS_ALIGNED(blk_rq_sectors(cur), 8)) |
| 1570 | goto no_packed; |
| 1571 | |
| 1572 | max_blk_count = min(card->host->max_blk_count, |
| 1573 | card->host->max_req_size >> 9); |
| 1574 | if (unlikely(max_blk_count > 0xffff)) |
| 1575 | max_blk_count = 0xffff; |
| 1576 | |
| 1577 | max_phys_segs = queue_max_segments(q); |
| 1578 | req_sectors += blk_rq_sectors(cur); |
| 1579 | phys_segments += cur->nr_phys_segments; |
| 1580 | |
| 1581 | if (rq_data_dir(cur) == WRITE) { |
| 1582 | req_sectors++; |
| 1583 | phys_segments++; |
| 1584 | } |
| 1585 | |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1586 | spin_lock(&stats->lock); |
| 1587 | |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1588 | while (reqs < max_packed_rw - 1) { |
| 1589 | spin_lock_irq(q->queue_lock); |
| 1590 | next = blk_fetch_request(q); |
| 1591 | spin_unlock_irq(q->queue_lock); |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1592 | if (!next) { |
| 1593 | MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1594 | break; |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1595 | } |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1596 | |
| 1597 | if (mmc_large_sec(card) && |
| 1598 | !IS_ALIGNED(blk_rq_sectors(next), 8)) { |
Tatyana Brokhman | 1718ef2 | 2012-10-04 11:11:08 +0200 | [diff] [blame] | 1599 | MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1600 | put_back = 1; |
| 1601 | break; |
| 1602 | } |
| 1603 | |
| 1604 | if (next->cmd_flags & REQ_DISCARD || |
| 1605 | next->cmd_flags & REQ_FLUSH) { |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1606 | MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1607 | put_back = 1; |
| 1608 | break; |
| 1609 | } |
| 1610 | |
| 1611 | if (rq_data_dir(cur) != rq_data_dir(next)) { |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1612 | MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1613 | put_back = 1; |
| 1614 | break; |
| 1615 | } |
| 1616 | |
| 1617 | if (mmc_req_rel_wr(next) && |
| 1618 | (md->flags & MMC_BLK_REL_WR) && |
| 1619 | !en_rel_wr) { |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1620 | MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1621 | put_back = 1; |
| 1622 | break; |
| 1623 | } |
| 1624 | |
| 1625 | req_sectors += blk_rq_sectors(next); |
| 1626 | if (req_sectors > max_blk_count) { |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1627 | if (stats->enabled) |
| 1628 | stats->pack_stop_reason[EXCEEDS_SECTORS]++; |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1629 | put_back = 1; |
| 1630 | break; |
| 1631 | } |
| 1632 | |
| 1633 | phys_segments += next->nr_phys_segments; |
| 1634 | if (phys_segments > max_phys_segs) { |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1635 | MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1636 | put_back = 1; |
| 1637 | break; |
| 1638 | } |
| 1639 | |
Maya Erez | 5f36069 | 2012-10-10 03:47:54 +0200 | [diff] [blame] | 1640 | if (rq_data_dir(next) == WRITE) { |
Tatyana Brokhman | 0cc7640 | 2012-10-07 09:52:16 +0200 | [diff] [blame] | 1641 | mq->num_of_potential_packed_wr_reqs++; |
Maya Erez | 5f36069 | 2012-10-10 03:47:54 +0200 | [diff] [blame] | 1642 | if (card->ext_csd.bkops_en) |
| 1643 | card->bkops_info.sectors_changed += |
| 1644 | blk_rq_sectors(next); |
| 1645 | } |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1646 | list_add_tail(&next->queuelist, &mq->mqrq_cur->packed_list); |
| 1647 | cur = next; |
| 1648 | reqs++; |
| 1649 | } |
| 1650 | |
| 1651 | if (put_back) { |
| 1652 | spin_lock_irq(q->queue_lock); |
| 1653 | blk_requeue_request(q, next); |
| 1654 | spin_unlock_irq(q->queue_lock); |
| 1655 | } |
| 1656 | |
Tatyana Brokhman | 464fbe1 | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 1657 | if (stats->enabled) { |
| 1658 | if (reqs + 1 <= card->ext_csd.max_packed_writes) |
| 1659 | stats->packing_events[reqs + 1]++; |
| 1660 | if (reqs + 1 == max_packed_rw) |
| 1661 | MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD); |
| 1662 | } |
| 1663 | |
| 1664 | spin_unlock(&stats->lock); |
| 1665 | |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1666 | if (reqs > 0) { |
| 1667 | list_add(&req->queuelist, &mq->mqrq_cur->packed_list); |
| 1668 | mq->mqrq_cur->packed_num = ++reqs; |
| 1669 | mq->mqrq_cur->packed_retries = reqs; |
| 1670 | return reqs; |
| 1671 | } |
| 1672 | |
| 1673 | no_packed: |
| 1674 | mmc_blk_clear_packed(mq->mqrq_cur); |
| 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq, |
| 1679 | struct mmc_card *card, |
| 1680 | struct mmc_queue *mq) |
| 1681 | { |
| 1682 | struct mmc_blk_request *brq = &mqrq->brq; |
| 1683 | struct request *req = mqrq->req; |
| 1684 | struct request *prq; |
| 1685 | struct mmc_blk_data *md = mq->data; |
| 1686 | bool do_rel_wr, do_data_tag; |
| 1687 | u32 *packed_cmd_hdr = mqrq->packed_cmd_hdr; |
| 1688 | u8 i = 1; |
| 1689 | |
| 1690 | mqrq->packed_cmd = MMC_PACKED_WRITE; |
| 1691 | mqrq->packed_blocks = 0; |
| 1692 | mqrq->packed_fail_idx = MMC_PACKED_N_IDX; |
| 1693 | |
| 1694 | memset(packed_cmd_hdr, 0, sizeof(mqrq->packed_cmd_hdr)); |
| 1695 | packed_cmd_hdr[0] = (mqrq->packed_num << 16) | |
| 1696 | (PACKED_CMD_WR << 8) | PACKED_CMD_VER; |
| 1697 | |
| 1698 | /* |
| 1699 | * Argument for each entry of packed group |
| 1700 | */ |
| 1701 | list_for_each_entry(prq, &mqrq->packed_list, queuelist) { |
| 1702 | do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR); |
| 1703 | do_data_tag = (card->ext_csd.data_tag_unit_size) && |
| 1704 | (prq->cmd_flags & REQ_META) && |
| 1705 | (rq_data_dir(prq) == WRITE) && |
| 1706 | ((brq->data.blocks * brq->data.blksz) >= |
| 1707 | card->ext_csd.data_tag_unit_size); |
| 1708 | /* Argument of CMD23 */ |
| 1709 | packed_cmd_hdr[(i * 2)] = |
| 1710 | (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) | |
| 1711 | (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) | |
| 1712 | blk_rq_sectors(prq); |
| 1713 | /* Argument of CMD18 or CMD25 */ |
| 1714 | packed_cmd_hdr[((i * 2)) + 1] = |
| 1715 | mmc_card_blockaddr(card) ? |
| 1716 | blk_rq_pos(prq) : blk_rq_pos(prq) << 9; |
| 1717 | mqrq->packed_blocks += blk_rq_sectors(prq); |
| 1718 | i++; |
| 1719 | } |
| 1720 | |
| 1721 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
| 1722 | brq->mrq.cmd = &brq->cmd; |
| 1723 | brq->mrq.data = &brq->data; |
| 1724 | brq->mrq.sbc = &brq->sbc; |
| 1725 | brq->mrq.stop = &brq->stop; |
| 1726 | |
| 1727 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 1728 | brq->sbc.arg = MMC_CMD23_ARG_PACKED | (mqrq->packed_blocks + 1); |
| 1729 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1730 | |
| 1731 | brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; |
| 1732 | brq->cmd.arg = blk_rq_pos(req); |
| 1733 | if (!mmc_card_blockaddr(card)) |
| 1734 | brq->cmd.arg <<= 9; |
| 1735 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 1736 | |
| 1737 | brq->data.blksz = 512; |
| 1738 | brq->data.blocks = mqrq->packed_blocks + 1; |
| 1739 | brq->data.flags |= MMC_DATA_WRITE; |
| 1740 | |
| 1741 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 1742 | brq->stop.arg = 0; |
| 1743 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 1744 | |
| 1745 | mmc_set_data_timeout(&brq->data, card); |
| 1746 | |
| 1747 | brq->data.sg = mqrq->sg; |
| 1748 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); |
| 1749 | |
| 1750 | mqrq->mmc_active.mrq = &brq->mrq; |
Tatyana Brokhman | 09b010d | 2012-10-09 13:50:56 +0200 | [diff] [blame] | 1751 | |
| 1752 | /* |
| 1753 | * This is intended for packed commands tests usage - in case these |
| 1754 | * functions are not in use the respective pointers are NULL |
| 1755 | */ |
| 1756 | if (mq->err_check_fn) |
| 1757 | mqrq->mmc_active.err_check = mq->err_check_fn; |
| 1758 | else |
| 1759 | mqrq->mmc_active.err_check = mmc_blk_packed_err_check; |
| 1760 | |
| 1761 | if (mq->packed_test_fn) |
| 1762 | mq->packed_test_fn(mq->queue, mqrq); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1763 | |
| 1764 | mmc_queue_bounce_pre(mqrq); |
| 1765 | } |
| 1766 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1767 | static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
| 1768 | struct mmc_blk_request *brq, struct request *req, |
| 1769 | int ret) |
| 1770 | { |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1771 | struct mmc_queue_req *mq_rq; |
| 1772 | mq_rq = container_of(brq, struct mmc_queue_req, brq); |
| 1773 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1774 | /* |
| 1775 | * If this is an SD card and we're writing, we can first |
| 1776 | * mark the known good sectors as ok. |
| 1777 | * |
| 1778 | * If the card is not SD, we can still ok written sectors |
| 1779 | * as reported by the controller (which might be less than |
| 1780 | * the real number of written sectors, but never more). |
| 1781 | */ |
| 1782 | if (mmc_card_sd(card)) { |
| 1783 | u32 blocks; |
Asutosh Das | 1a89714 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 1784 | if (!brq->data.fault_injected) { |
| 1785 | blocks = mmc_sd_num_wr_blocks(card); |
| 1786 | if (blocks != (u32)-1) |
| 1787 | ret = blk_end_request(req, 0, blocks << 9); |
| 1788 | } else |
| 1789 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1790 | } else { |
| 1791 | if (mq_rq->packed_cmd == MMC_PACKED_NONE) |
| 1792 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); |
| 1793 | } |
Seungwon Jeon | 968c774 | 2012-05-31 11:54:47 +0300 | [diff] [blame] | 1794 | return ret; |
| 1795 | } |
| 1796 | |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1797 | static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq) |
| 1798 | { |
| 1799 | struct request *prq; |
| 1800 | int idx = mq_rq->packed_fail_idx, i = 0; |
| 1801 | int ret = 0; |
| 1802 | |
| 1803 | while (!list_empty(&mq_rq->packed_list)) { |
| 1804 | prq = list_entry_rq(mq_rq->packed_list.next); |
| 1805 | if (idx == i) { |
| 1806 | /* retry from error index */ |
| 1807 | mq_rq->packed_num -= idx; |
| 1808 | mq_rq->req = prq; |
| 1809 | ret = 1; |
| 1810 | |
| 1811 | if (mq_rq->packed_num == MMC_PACKED_N_SINGLE) { |
| 1812 | list_del_init(&prq->queuelist); |
| 1813 | mmc_blk_clear_packed(mq_rq); |
| 1814 | } |
| 1815 | return ret; |
| 1816 | } |
| 1817 | list_del_init(&prq->queuelist); |
| 1818 | blk_end_request(prq, 0, blk_rq_bytes(prq)); |
| 1819 | i++; |
| 1820 | } |
| 1821 | |
| 1822 | mmc_blk_clear_packed(mq_rq); |
| 1823 | return ret; |
| 1824 | } |
| 1825 | static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq) |
| 1826 | { |
| 1827 | struct request *prq; |
| 1828 | |
| 1829 | while (!list_empty(&mq_rq->packed_list)) { |
| 1830 | prq = list_entry_rq(mq_rq->packed_list.next); |
| 1831 | list_del_init(&prq->queuelist); |
| 1832 | blk_end_request(prq, -EIO, blk_rq_bytes(prq)); |
| 1833 | } |
| 1834 | |
| 1835 | mmc_blk_clear_packed(mq_rq); |
| 1836 | } |
| 1837 | |
| 1838 | static void mmc_blk_revert_packed_req(struct mmc_queue *mq, |
| 1839 | struct mmc_queue_req *mq_rq) |
| 1840 | { |
| 1841 | struct request *prq; |
| 1842 | struct request_queue *q = mq->queue; |
| 1843 | |
| 1844 | while (!list_empty(&mq_rq->packed_list)) { |
| 1845 | prq = list_entry_rq(mq_rq->packed_list.prev); |
| 1846 | if (prq->queuelist.prev != &mq_rq->packed_list) { |
| 1847 | list_del_init(&prq->queuelist); |
| 1848 | spin_lock_irq(q->queue_lock); |
| 1849 | blk_requeue_request(mq->queue, prq); |
| 1850 | spin_unlock_irq(q->queue_lock); |
| 1851 | } else { |
| 1852 | list_del_init(&prq->queuelist); |
| 1853 | } |
| 1854 | } |
| 1855 | |
| 1856 | mmc_blk_clear_packed(mq_rq); |
| 1857 | } |
| 1858 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1859 | 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] | 1860 | { |
| 1861 | struct mmc_blk_data *md = mq->data; |
| 1862 | struct mmc_card *card = md->queue.card; |
| 1863 | struct mmc_blk_request *brq = &mq->mqrq_cur->brq; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1864 | int ret = 1, disable_multi = 0, retry = 0, type; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1865 | enum mmc_blk_status status; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1866 | struct mmc_queue_req *mq_rq; |
Tatyana Brokhman | f94cf3d | 2012-09-27 11:00:02 +0200 | [diff] [blame] | 1867 | struct request *req; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1868 | struct mmc_async_req *areq; |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1869 | const u8 packed_num = 2; |
| 1870 | u8 reqs = 0; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1871 | |
| 1872 | if (!rqc && !mq->mqrq_prev->req) |
| 1873 | return 0; |
Per Forlin | 54d49d77 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1874 | |
Maya Erez | 5f36069 | 2012-10-10 03:47:54 +0200 | [diff] [blame] | 1875 | if (rqc) { |
| 1876 | if ((card->ext_csd.bkops_en) && (rq_data_dir(rqc) == WRITE)) |
| 1877 | card->bkops_info.sectors_changed += blk_rq_sectors(rqc); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1878 | reqs = mmc_blk_prep_packed_list(mq, rqc); |
Maya Erez | 5f36069 | 2012-10-10 03:47:54 +0200 | [diff] [blame] | 1879 | } |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1880 | |
Per Forlin | 54d49d77 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1881 | do { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1882 | if (rqc) { |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1883 | if (reqs >= packed_num) |
| 1884 | mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur, |
| 1885 | card, mq); |
| 1886 | else |
| 1887 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1888 | areq = &mq->mqrq_cur->mmc_active; |
| 1889 | } else |
| 1890 | areq = NULL; |
| 1891 | areq = mmc_start_req(card->host, areq, (int *) &status); |
| 1892 | if (!areq) |
| 1893 | return 0; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 1894 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1895 | mq_rq = container_of(areq, struct mmc_queue_req, mmc_active); |
| 1896 | brq = &mq_rq->brq; |
| 1897 | req = mq_rq->req; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1898 | type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1899 | mmc_queue_bounce_post(mq_rq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 1900 | |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1901 | switch (status) { |
| 1902 | case MMC_BLK_SUCCESS: |
| 1903 | case MMC_BLK_PARTIAL: |
| 1904 | /* |
| 1905 | * A block was successfully transferred. |
| 1906 | */ |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1907 | mmc_blk_reset_success(md, type); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1908 | |
| 1909 | if (mq_rq->packed_cmd != MMC_PACKED_NONE) { |
| 1910 | ret = mmc_blk_end_packed_req(mq_rq); |
| 1911 | break; |
| 1912 | } else { |
| 1913 | ret = blk_end_request(req, 0, |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1914 | brq->data.bytes_xfered); |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1915 | } |
| 1916 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1917 | /* |
| 1918 | * If the blk_end_request function returns non-zero even |
| 1919 | * though all data has been transferred and no errors |
| 1920 | * were returned by the host controller, it's a bug. |
| 1921 | */ |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1922 | if (status == MMC_BLK_SUCCESS && ret) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1923 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1924 | __func__, blk_rq_bytes(req), |
| 1925 | brq->data.bytes_xfered); |
| 1926 | rqc = NULL; |
| 1927 | goto cmd_abort; |
| 1928 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1929 | break; |
| 1930 | case MMC_BLK_CMD_ERR: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1931 | ret = mmc_blk_cmd_err(md, card, brq, req, ret); |
| 1932 | if (!mmc_blk_reset(md, card->host, type)) |
| 1933 | break; |
| 1934 | goto cmd_abort; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1935 | case MMC_BLK_RETRY: |
| 1936 | if (retry++ < 5) |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1937 | break; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1938 | /* Fall through */ |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1939 | case MMC_BLK_ABORT: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1940 | if (!mmc_blk_reset(md, card->host, type)) |
| 1941 | break; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1942 | goto cmd_abort; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1943 | case MMC_BLK_DATA_ERR: { |
| 1944 | int err; |
| 1945 | |
| 1946 | err = mmc_blk_reset(md, card->host, type); |
| 1947 | if (!err) |
| 1948 | break; |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1949 | if (err == -ENODEV || |
| 1950 | mq_rq->packed_cmd != MMC_PACKED_NONE) |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1951 | goto cmd_abort; |
| 1952 | /* Fall through */ |
| 1953 | } |
| 1954 | case MMC_BLK_ECC_ERR: |
| 1955 | if (brq->data.blocks > 1) { |
| 1956 | /* Redo read one sector at a time */ |
| 1957 | pr_warning("%s: retrying using single block read\n", |
| 1958 | req->rq_disk->disk_name); |
| 1959 | disable_multi = 1; |
| 1960 | break; |
| 1961 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1962 | /* |
| 1963 | * After an error, we redo I/O one sector at a |
| 1964 | * time, so we only reach here after trying to |
| 1965 | * read a single sector. |
| 1966 | */ |
Subhash Jadavani | 4d63b1d | 2012-06-08 16:17:44 +0530 | [diff] [blame] | 1967 | ret = blk_end_request(req, -EIO, |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1968 | brq->data.blksz); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1969 | if (!ret) |
| 1970 | goto start_new_req; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1971 | break; |
Sujit Reddy Thumma | a8ad82c | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1972 | case MMC_BLK_NOMEDIUM: |
| 1973 | goto cmd_abort; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1974 | } |
| 1975 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1976 | if (ret) { |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1977 | if (mq_rq->packed_cmd == MMC_PACKED_NONE) { |
| 1978 | /* |
| 1979 | * In case of a incomplete request |
| 1980 | * prepare it again and resend. |
| 1981 | */ |
| 1982 | mmc_blk_rw_rq_prep(mq_rq, card, |
| 1983 | disable_multi, mq); |
| 1984 | mmc_start_req(card->host, |
| 1985 | &mq_rq->mmc_active, NULL); |
| 1986 | } else { |
| 1987 | if (!mq_rq->packed_retries) |
| 1988 | goto cmd_abort; |
| 1989 | mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq); |
| 1990 | mmc_start_req(card->host, |
| 1991 | &mq_rq->mmc_active, NULL); |
| 1992 | } |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1993 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | } while (ret); |
| 1995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | return 1; |
| 1997 | |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1998 | cmd_abort: |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 1999 | if (mq_rq->packed_cmd == MMC_PACKED_NONE) { |
| 2000 | if (mmc_card_removed(card)) |
| 2001 | req->cmd_flags |= REQ_QUIET; |
| 2002 | while (ret) |
| 2003 | ret = blk_end_request(req, -EIO, |
| 2004 | blk_rq_cur_bytes(req)); |
| 2005 | } else { |
| 2006 | mmc_blk_abort_packed_req(mq_rq); |
| 2007 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2009 | start_new_req: |
| 2010 | if (rqc) { |
Seungwon Jeon | 53f8f57 | 2012-09-27 15:00:26 +0200 | [diff] [blame] | 2011 | /* |
| 2012 | * If current request is packed, it needs to put back. |
| 2013 | */ |
| 2014 | if (mq->mqrq_cur->packed_cmd != MMC_PACKED_NONE) |
| 2015 | mmc_blk_revert_packed_req(mq, mq->mqrq_cur); |
| 2016 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2017 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
| 2018 | mmc_start_req(card->host, &mq->mqrq_cur->mmc_active, NULL); |
| 2019 | } |
| 2020 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2021 | return 0; |
| 2022 | } |
| 2023 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2024 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
| 2025 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2026 | int ret; |
| 2027 | struct mmc_blk_data *md = mq->data; |
| 2028 | struct mmc_card *card = md->queue.card; |
| 2029 | |
San Mehat | 148c57a | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 2030 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 2031 | if (mmc_bus_needs_resume(card->host)) { |
| 2032 | mmc_resume_bus(card->host); |
| 2033 | mmc_blk_set_blksize(md, card); |
| 2034 | } |
| 2035 | #endif |
| 2036 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2037 | if (req && !mq->mqrq_prev->req) |
| 2038 | /* claim host only for the first request */ |
| 2039 | mmc_claim_host(card->host); |
| 2040 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2041 | ret = mmc_blk_part_switch(card, md); |
| 2042 | if (ret) { |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2043 | if (req) { |
Subhash Jadavani | 4d63b1d | 2012-06-08 16:17:44 +0530 | [diff] [blame] | 2044 | blk_end_request_all(req, -EIO); |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2045 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2046 | ret = 0; |
| 2047 | goto out; |
| 2048 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2049 | |
Tatyana Brokhman | 0cc7640 | 2012-10-07 09:52:16 +0200 | [diff] [blame] | 2050 | mmc_blk_write_packing_control(mq, req); |
| 2051 | |
Maya Erez | 463bb95 | 2012-05-24 23:46:29 +0300 | [diff] [blame] | 2052 | if (req && req->cmd_flags & REQ_SANITIZE) { |
| 2053 | /* complete ongoing async transfer before issuing sanitize */ |
| 2054 | if (card->host && card->host->areq) |
| 2055 | mmc_blk_issue_rw_rq(mq, NULL); |
| 2056 | ret = mmc_blk_issue_sanitize_rq(mq, req); |
| 2057 | } else if (req && req->cmd_flags & REQ_DISCARD) { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2058 | /* complete ongoing async transfer before issuing discard */ |
| 2059 | if (card->host->areq) |
| 2060 | mmc_blk_issue_rw_rq(mq, NULL); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2061 | if (req->cmd_flags & REQ_SECURE) |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2062 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2063 | else |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2064 | ret = mmc_blk_issue_discard_rq(mq, req); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2065 | } else if (req && req->cmd_flags & REQ_FLUSH) { |
Jaehoon Chung | 393f9a0 | 2011-07-13 17:02:16 +0900 | [diff] [blame] | 2066 | /* complete ongoing async transfer before issuing flush */ |
| 2067 | if (card->host->areq) |
| 2068 | mmc_blk_issue_rw_rq(mq, NULL); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2069 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2070 | } else { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2071 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2072 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2073 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2074 | out: |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2075 | if (!req) |
| 2076 | /* release host only when there are no more requests */ |
| 2077 | mmc_release_host(card->host); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2078 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2079 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2081 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 2082 | { |
| 2083 | return mmc_card_readonly(card) || |
| 2084 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 2085 | } |
| 2086 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2087 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 2088 | struct device *parent, |
| 2089 | sector_t size, |
| 2090 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2091 | const char *subname, |
| 2092 | int area_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | { |
| 2094 | struct mmc_blk_data *md; |
| 2095 | int devidx, ret; |
| 2096 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2097 | devidx = find_first_zero_bit(dev_use, max_devices); |
| 2098 | if (devidx >= max_devices) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | return ERR_PTR(-ENOSPC); |
| 2100 | __set_bit(devidx, dev_use); |
| 2101 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 2102 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2103 | if (!md) { |
| 2104 | ret = -ENOMEM; |
| 2105 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2107 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2108 | /* |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2109 | * !subname implies we are creating main mmc_blk_data that will be |
| 2110 | * associated with mmc_card with mmc_set_drvdata. Due to device |
| 2111 | * partitions, devidx will not coincide with a per-physical card |
| 2112 | * index anymore so we keep track of a name index. |
| 2113 | */ |
| 2114 | if (!subname) { |
| 2115 | md->name_idx = find_first_zero_bit(name_use, max_devices); |
| 2116 | __set_bit(md->name_idx, name_use); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2117 | } else |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2118 | md->name_idx = ((struct mmc_blk_data *) |
| 2119 | dev_to_disk(parent)->private_data)->name_idx; |
| 2120 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2121 | md->area_type = area_type; |
| 2122 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2123 | /* |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2124 | * Set the read-only status based on the supported commands |
| 2125 | * and the write protect switch. |
| 2126 | */ |
| 2127 | md->read_only = mmc_blk_readonly(card); |
| 2128 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2129 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2130 | if (md->disk == NULL) { |
| 2131 | ret = -ENOMEM; |
| 2132 | goto err_kfree; |
| 2133 | } |
| 2134 | |
| 2135 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2136 | INIT_LIST_HEAD(&md->part); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2137 | md->usage = 1; |
| 2138 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 2139 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2140 | if (ret) |
| 2141 | goto err_putdisk; |
| 2142 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2143 | md->queue.issue_fn = mmc_blk_issue_rq; |
| 2144 | md->queue.data = md; |
| 2145 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2146 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2147 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2148 | md->disk->fops = &mmc_bdops; |
| 2149 | md->disk->private_data = md; |
| 2150 | md->disk->queue = md->queue.queue; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2151 | md->disk->driverfs_dev = parent; |
| 2152 | set_disk_ro(md->disk, md->read_only || default_ro); |
Colin Cross | 71b54d4 | 2010-09-03 12:41:21 -0700 | [diff] [blame] | 2153 | md->disk->flags = GENHD_FL_EXT_DEVT; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2154 | |
| 2155 | /* |
| 2156 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 2157 | * |
| 2158 | * - be set for removable media with permanent block devices |
| 2159 | * - be unset for removable block devices with permanent media |
| 2160 | * |
| 2161 | * Since MMC block devices clearly fall under the second |
| 2162 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 2163 | * should use the block device creation/destruction hotplug |
| 2164 | * messages to tell when the card is present. |
| 2165 | */ |
| 2166 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2167 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 2168 | "mmcblk%d%s", md->name_idx, subname ? subname : ""); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2169 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 2170 | blk_queue_logical_block_size(md->queue.queue, 512); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2171 | set_capacity(md->disk, size); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2172 | |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2173 | if (mmc_host_cmd23(card->host)) { |
| 2174 | if (mmc_card_mmc(card) || |
| 2175 | (mmc_card_sd(card) && |
| 2176 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) |
| 2177 | md->flags |= MMC_BLK_CMD23; |
| 2178 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2179 | |
| 2180 | if (mmc_card_mmc(card) && |
| 2181 | md->flags & MMC_BLK_CMD23 && |
| 2182 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
| 2183 | card->ext_csd.rel_sectors)) { |
| 2184 | md->flags |= MMC_BLK_REL_WR; |
| 2185 | blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA); |
| 2186 | } |
| 2187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2189 | |
| 2190 | err_putdisk: |
| 2191 | put_disk(md->disk); |
| 2192 | err_kfree: |
| 2193 | kfree(md); |
| 2194 | out: |
| 2195 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2198 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 2199 | { |
| 2200 | sector_t size; |
| 2201 | struct mmc_blk_data *md; |
| 2202 | |
| 2203 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 2204 | /* |
| 2205 | * The EXT_CSD sector count is in number or 512 byte |
| 2206 | * sectors. |
| 2207 | */ |
| 2208 | size = card->ext_csd.sectors; |
| 2209 | } else { |
| 2210 | /* |
| 2211 | * The CSD capacity field is in units of read_blkbits. |
| 2212 | * set_capacity takes units of 512 bytes. |
| 2213 | */ |
| 2214 | size = card->csd.capacity << (card->csd.read_blkbits - 9); |
| 2215 | } |
| 2216 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2217 | md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
| 2218 | MMC_BLK_DATA_AREA_MAIN); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2219 | return md; |
| 2220 | } |
| 2221 | |
| 2222 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 2223 | struct mmc_blk_data *md, |
| 2224 | unsigned int part_type, |
| 2225 | sector_t size, |
| 2226 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2227 | const char *subname, |
| 2228 | int area_type) |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2229 | { |
| 2230 | char cap_str[10]; |
| 2231 | struct mmc_blk_data *part_md; |
| 2232 | |
| 2233 | 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] | 2234 | subname, area_type); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2235 | if (IS_ERR(part_md)) |
| 2236 | return PTR_ERR(part_md); |
| 2237 | part_md->part_type = part_type; |
| 2238 | list_add(&part_md->part, &md->part); |
| 2239 | |
| 2240 | string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2, |
| 2241 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2242 | pr_info("%s: %s %s partition %u %s\n", |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2243 | part_md->disk->disk_name, mmc_card_id(card), |
| 2244 | mmc_card_name(card), part_md->part_type, cap_str); |
| 2245 | return 0; |
| 2246 | } |
| 2247 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2248 | /* MMC Physical partitions consist of two boot partitions and |
| 2249 | * up to four general purpose partitions. |
| 2250 | * For each partition enabled in EXT_CSD a block device will be allocatedi |
| 2251 | * to provide access to the partition. |
| 2252 | */ |
| 2253 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2254 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 2255 | { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2256 | int idx, ret = 0; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2257 | |
| 2258 | if (!mmc_card_mmc(card)) |
| 2259 | return 0; |
| 2260 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2261 | for (idx = 0; idx < card->nr_parts; idx++) { |
| 2262 | if (card->part[idx].size) { |
| 2263 | ret = mmc_blk_alloc_part(card, md, |
| 2264 | card->part[idx].part_cfg, |
| 2265 | card->part[idx].size >> 9, |
| 2266 | card->part[idx].force_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2267 | card->part[idx].name, |
| 2268 | card->part[idx].area_type); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2269 | if (ret) |
| 2270 | return ret; |
| 2271 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2272 | } |
| 2273 | |
| 2274 | return ret; |
| 2275 | } |
| 2276 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2277 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 2278 | { |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2279 | struct mmc_card *card; |
| 2280 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2281 | if (md) { |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2282 | card = md->queue.card; |
Tatyana Brokhman | 0cc7640 | 2012-10-07 09:52:16 +0200 | [diff] [blame] | 2283 | device_remove_file(disk_to_dev(md->disk), |
| 2284 | &md->num_wr_reqs_to_start_packing); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2285 | if (md->disk->flags & GENHD_FL_UP) { |
| 2286 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2287 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 2288 | card->ext_csd.boot_ro_lockable) |
| 2289 | device_remove_file(disk_to_dev(md->disk), |
| 2290 | &md->power_ro_lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2291 | |
| 2292 | /* Stop new requests from getting into the queue */ |
| 2293 | del_gendisk(md->disk); |
| 2294 | } |
| 2295 | |
| 2296 | /* Then flush out any already in there */ |
| 2297 | mmc_cleanup_queue(&md->queue); |
| 2298 | mmc_blk_put(md); |
| 2299 | } |
| 2300 | } |
| 2301 | |
| 2302 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 2303 | struct mmc_blk_data *md) |
| 2304 | { |
| 2305 | struct list_head *pos, *q; |
| 2306 | struct mmc_blk_data *part_md; |
| 2307 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2308 | __clear_bit(md->name_idx, name_use); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2309 | list_for_each_safe(pos, q, &md->part) { |
| 2310 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 2311 | list_del(pos); |
| 2312 | mmc_blk_remove_req(part_md); |
| 2313 | } |
| 2314 | } |
| 2315 | |
| 2316 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 2317 | { |
| 2318 | int ret; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2319 | struct mmc_card *card = md->queue.card; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2320 | |
| 2321 | add_disk(md->disk); |
| 2322 | md->force_ro.show = force_ro_show; |
| 2323 | md->force_ro.store = force_ro_store; |
Rabin Vincent | 641c318 | 2011-04-23 20:52:58 +0530 | [diff] [blame] | 2324 | sysfs_attr_init(&md->force_ro.attr); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2325 | md->force_ro.attr.name = "force_ro"; |
| 2326 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 2327 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 2328 | if (ret) |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2329 | goto force_ro_fail; |
| 2330 | |
| 2331 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 2332 | card->ext_csd.boot_ro_lockable) { |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 2333 | umode_t mode; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2334 | |
| 2335 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) |
| 2336 | mode = S_IRUGO; |
| 2337 | else |
| 2338 | mode = S_IRUGO | S_IWUSR; |
| 2339 | |
| 2340 | md->power_ro_lock.show = power_ro_lock_show; |
| 2341 | md->power_ro_lock.store = power_ro_lock_store; |
Rabin Vincent | 00d9ac0 | 2012-02-01 16:31:56 +0100 | [diff] [blame] | 2342 | sysfs_attr_init(&md->power_ro_lock.attr); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2343 | md->power_ro_lock.attr.mode = mode; |
| 2344 | md->power_ro_lock.attr.name = |
| 2345 | "ro_lock_until_next_power_on"; |
| 2346 | ret = device_create_file(disk_to_dev(md->disk), |
| 2347 | &md->power_ro_lock); |
| 2348 | if (ret) |
| 2349 | goto power_ro_lock_fail; |
| 2350 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2351 | |
Tatyana Brokhman | 0cc7640 | 2012-10-07 09:52:16 +0200 | [diff] [blame] | 2352 | md->num_wr_reqs_to_start_packing.show = |
| 2353 | num_wr_reqs_to_start_packing_show; |
| 2354 | md->num_wr_reqs_to_start_packing.store = |
| 2355 | num_wr_reqs_to_start_packing_store; |
| 2356 | sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr); |
| 2357 | md->num_wr_reqs_to_start_packing.attr.name = |
| 2358 | "num_wr_reqs_to_start_packing"; |
| 2359 | md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR; |
| 2360 | ret = device_create_file(disk_to_dev(md->disk), |
| 2361 | &md->num_wr_reqs_to_start_packing); |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 2362 | if (ret) |
Maya Erez | 8d006d3 | 2012-10-30 09:02:39 +0200 | [diff] [blame^] | 2363 | goto num_wr_reqs_to_start_packing_fail; |
Maya Erez | 63c61d6 | 2012-05-31 21:00:18 +0300 | [diff] [blame] | 2364 | |
Yaniv Gardi | e6c6b4b | 2012-10-11 11:36:24 +0200 | [diff] [blame] | 2365 | md->min_sectors_to_check_bkops_status.show = |
| 2366 | min_sectors_to_check_bkops_status_show; |
| 2367 | md->min_sectors_to_check_bkops_status.store = |
| 2368 | min_sectors_to_check_bkops_status_store; |
| 2369 | sysfs_attr_init(&md->min_sectors_to_check_bkops_status.attr); |
| 2370 | md->min_sectors_to_check_bkops_status.attr.name = |
| 2371 | "min_sectors_to_check_bkops_status"; |
| 2372 | md->min_sectors_to_check_bkops_status.attr.mode = S_IRUGO | S_IWUSR; |
| 2373 | ret = device_create_file(disk_to_dev(md->disk), |
| 2374 | &md->min_sectors_to_check_bkops_status); |
| 2375 | if (ret) |
Maya Erez | 8d006d3 | 2012-10-30 09:02:39 +0200 | [diff] [blame^] | 2376 | goto min_sectors_to_check_bkops_status_fails; |
Yaniv Gardi | e6c6b4b | 2012-10-11 11:36:24 +0200 | [diff] [blame] | 2377 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2378 | return ret; |
| 2379 | |
Maya Erez | 8d006d3 | 2012-10-30 09:02:39 +0200 | [diff] [blame^] | 2380 | min_sectors_to_check_bkops_status_fails: |
| 2381 | device_remove_file(disk_to_dev(md->disk), |
| 2382 | &md->num_wr_reqs_to_start_packing); |
| 2383 | num_wr_reqs_to_start_packing_fail: |
| 2384 | device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2385 | power_ro_lock_fail: |
Maya Erez | 8d006d3 | 2012-10-30 09:02:39 +0200 | [diff] [blame^] | 2386 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2387 | force_ro_fail: |
Maya Erez | 8d006d3 | 2012-10-30 09:02:39 +0200 | [diff] [blame^] | 2388 | del_gendisk(md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2389 | |
| 2390 | return ret; |
| 2391 | } |
| 2392 | |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2393 | #define CID_MANFID_SANDISK 0x2 |
| 2394 | #define CID_MANFID_TOSHIBA 0x11 |
| 2395 | #define CID_MANFID_MICRON 0x13 |
| 2396 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 2397 | static const struct mmc_fixup blk_fixups[] = |
| 2398 | { |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2399 | MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2400 | MMC_QUIRK_INAND_CMD38), |
| 2401 | MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2402 | MMC_QUIRK_INAND_CMD38), |
| 2403 | MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2404 | MMC_QUIRK_INAND_CMD38), |
| 2405 | MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2406 | MMC_QUIRK_INAND_CMD38), |
| 2407 | MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 2408 | MMC_QUIRK_INAND_CMD38), |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2409 | |
| 2410 | /* |
| 2411 | * Some MMC cards experience performance degradation with CMD23 |
| 2412 | * instead of CMD12-bounded multiblock transfers. For now we'll |
| 2413 | * black list what's bad... |
| 2414 | * - Certain Toshiba cards. |
| 2415 | * |
| 2416 | * N.B. This doesn't affect SD cards. |
| 2417 | */ |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2418 | MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2419 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2420 | MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2421 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2422 | MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2423 | MMC_QUIRK_BLK_NO_CMD23), |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 2424 | |
| 2425 | /* |
| 2426 | * Some Micron MMC cards needs longer data read timeout than |
| 2427 | * indicated in CSD. |
| 2428 | */ |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 2429 | 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] | 2430 | MMC_QUIRK_LONG_READ_TIME), |
| 2431 | |
Pratibhasagar V | 3a18bbd | 2012-04-17 14:41:19 +0530 | [diff] [blame] | 2432 | /* Some INAND MCP devices advertise incorrect timeout values */ |
| 2433 | MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc, |
| 2434 | MMC_QUIRK_INAND_DATA_TIMEOUT), |
| 2435 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 2436 | END_FIXUP |
| 2437 | }; |
| 2438 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | static int mmc_blk_probe(struct mmc_card *card) |
| 2440 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2441 | struct mmc_blk_data *md, *part_md; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2442 | char cap_str[10]; |
| 2443 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 2444 | /* |
| 2445 | * Check that the card supports the command class(es) we need. |
| 2446 | */ |
| 2447 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | return -ENODEV; |
| 2449 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | md = mmc_blk_alloc(card); |
| 2451 | if (IS_ERR(md)) |
| 2452 | return PTR_ERR(md); |
| 2453 | |
Yi Li | 444122f | 2009-02-05 15:31:57 +0800 | [diff] [blame] | 2454 | string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2455 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2456 | pr_info("%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2458 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2460 | if (mmc_blk_alloc_parts(card, md)) |
| 2461 | goto out; |
| 2462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2463 | mmc_set_drvdata(card, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 2464 | mmc_fixup_device(card, blk_fixups); |
| 2465 | |
San Mehat | 148c57a | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 2466 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 2467 | mmc_set_bus_resume_policy(card->host, 1); |
| 2468 | #endif |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2469 | if (mmc_add_disk(md)) |
| 2470 | goto out; |
| 2471 | |
| 2472 | list_for_each_entry(part_md, &md->part, part) { |
| 2473 | if (mmc_add_disk(part_md)) |
| 2474 | goto out; |
| 2475 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | return 0; |
| 2477 | |
| 2478 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2479 | mmc_blk_remove_parts(card, md); |
| 2480 | mmc_blk_remove_req(md); |
Ulf Hansson | 5865f28 | 2012-03-22 11:47:26 +0100 | [diff] [blame] | 2481 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2482 | } |
| 2483 | |
| 2484 | static void mmc_blk_remove(struct mmc_card *card) |
| 2485 | { |
| 2486 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 2487 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2488 | mmc_blk_remove_parts(card, md); |
Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 2489 | mmc_claim_host(card->host); |
| 2490 | mmc_blk_part_switch(card, md); |
| 2491 | mmc_release_host(card->host); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2492 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | mmc_set_drvdata(card, NULL); |
San Mehat | 148c57a | 2009-07-30 08:21:19 -0700 | [diff] [blame] | 2494 | #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME |
| 2495 | mmc_set_bus_resume_policy(card->host, 0); |
| 2496 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | } |
| 2498 | |
| 2499 | #ifdef CONFIG_PM |
Chuanxiao Dong | 32d317c | 2012-04-11 19:54:38 +0800 | [diff] [blame] | 2500 | static int mmc_blk_suspend(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2502 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 2504 | |
| 2505 | if (md) { |
| 2506 | mmc_queue_suspend(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2507 | list_for_each_entry(part_md, &md->part, part) { |
| 2508 | mmc_queue_suspend(&part_md->queue); |
| 2509 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | } |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
| 2514 | static int mmc_blk_resume(struct mmc_card *card) |
| 2515 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2516 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2517 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 2518 | |
| 2519 | if (md) { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2520 | /* |
| 2521 | * Resume involves the card going into idle state, |
| 2522 | * so current partition is always the main one. |
| 2523 | */ |
| 2524 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2525 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2526 | list_for_each_entry(part_md, &md->part, part) { |
| 2527 | mmc_queue_resume(&part_md->queue); |
| 2528 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2529 | } |
| 2530 | return 0; |
| 2531 | } |
| 2532 | #else |
| 2533 | #define mmc_blk_suspend NULL |
| 2534 | #define mmc_blk_resume NULL |
| 2535 | #endif |
| 2536 | |
| 2537 | static struct mmc_driver mmc_driver = { |
| 2538 | .drv = { |
| 2539 | .name = "mmcblk", |
| 2540 | }, |
| 2541 | .probe = mmc_blk_probe, |
| 2542 | .remove = mmc_blk_remove, |
| 2543 | .suspend = mmc_blk_suspend, |
| 2544 | .resume = mmc_blk_resume, |
| 2545 | }; |
| 2546 | |
| 2547 | static int __init mmc_blk_init(void) |
| 2548 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2549 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2551 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 2552 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 2553 | |
| 2554 | max_devices = 256 / perdev_minors; |
| 2555 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2556 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 2557 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2558 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2559 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2560 | res = mmc_register_driver(&mmc_driver); |
| 2561 | if (res) |
| 2562 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2563 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2564 | return 0; |
| 2565 | out2: |
| 2566 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | out: |
| 2568 | return res; |
| 2569 | } |
| 2570 | |
| 2571 | static void __exit mmc_blk_exit(void) |
| 2572 | { |
| 2573 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2574 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | module_init(mmc_blk_init); |
| 2578 | module_exit(mmc_blk_exit); |
| 2579 | |
| 2580 | MODULE_LICENSE("GPL"); |
| 2581 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 2582 | |