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