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