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