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