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