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 |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 63 | #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute 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; |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 824 | int is_rpmb = false; |
| 825 | u32 status = 0; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 826 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 827 | if (!card || !md || !idata) |
| 828 | return -EINVAL; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 829 | |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 830 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) |
| 831 | is_rpmb = true; |
| 832 | |
Johan Rudholm | 4d6144d | 2011-11-23 09:05:58 +0100 | [diff] [blame] | 833 | cmd.opcode = idata->ic.opcode; |
| 834 | cmd.arg = idata->ic.arg; |
| 835 | cmd.flags = idata->ic.flags; |
| 836 | |
| 837 | if (idata->buf_bytes) { |
| 838 | data.sg = &sg; |
| 839 | data.sg_len = 1; |
| 840 | data.blksz = idata->ic.blksz; |
| 841 | data.blocks = idata->ic.blocks; |
| 842 | |
| 843 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); |
| 844 | |
| 845 | if (idata->ic.write_flag) |
| 846 | data.flags = MMC_DATA_WRITE; |
| 847 | else |
| 848 | data.flags = MMC_DATA_READ; |
| 849 | |
| 850 | /* data.flags must already be set before doing this. */ |
| 851 | mmc_set_data_timeout(&data, card); |
| 852 | |
| 853 | /* Allow overriding the timeout_ns for empirical tuning. */ |
| 854 | if (idata->ic.data_timeout_ns) |
| 855 | data.timeout_ns = idata->ic.data_timeout_ns; |
| 856 | |
| 857 | if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { |
| 858 | /* |
| 859 | * Pretend this is a data transfer and rely on the |
| 860 | * host driver to compute timeout. When all host |
| 861 | * drivers support cmd.cmd_timeout for R1B, this |
| 862 | * can be changed to: |
| 863 | * |
| 864 | * mrq.data = NULL; |
| 865 | * cmd.cmd_timeout = idata->ic.cmd_timeout_ms; |
| 866 | */ |
| 867 | data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; |
| 868 | } |
| 869 | |
| 870 | mrq.data = &data; |
| 871 | } |
| 872 | |
| 873 | mrq.cmd = &cmd; |
| 874 | |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 875 | err = mmc_blk_part_switch(card, md); |
| 876 | if (err) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 877 | return err; |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 878 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 879 | if (idata->ic.is_acmd) { |
| 880 | err = mmc_app_cmd(card->host, card); |
| 881 | if (err) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 882 | return err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 883 | } |
| 884 | |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 885 | if (is_rpmb) { |
| 886 | err = mmc_set_blockcount(card, data.blocks, |
| 887 | idata->ic.write_flag & (1 << 31)); |
| 888 | if (err) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 889 | return err; |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 890 | } |
| 891 | |
Yaniv Gardi | a82e484 | 2013-06-05 14:13:08 +0300 | [diff] [blame] | 892 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) && |
| 893 | (cmd.opcode == MMC_SWITCH)) { |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 894 | err = ioctl_do_sanitize(card); |
| 895 | |
| 896 | if (err) |
| 897 | pr_err("%s: ioctl_do_sanitize() failed. err = %d", |
| 898 | __func__, err); |
| 899 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 900 | return err; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 901 | } |
| 902 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 903 | mmc_wait_for_req(card->host, &mrq); |
| 904 | |
| 905 | if (cmd.error) { |
| 906 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", |
| 907 | __func__, cmd.error); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 908 | return cmd.error; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 909 | } |
| 910 | if (data.error) { |
| 911 | dev_err(mmc_dev(card->host), "%s: data error %d\n", |
| 912 | __func__, data.error); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 913 | return data.error; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /* |
| 917 | * According to the SD specs, some commands require a delay after |
| 918 | * issuing the command. |
| 919 | */ |
| 920 | if (idata->ic.postsleep_min_us) |
| 921 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); |
| 922 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 923 | memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp)); |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 924 | |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 925 | if (is_rpmb) { |
| 926 | /* |
| 927 | * Ensure RPMB command has completed by polling CMD13 |
| 928 | * "Send Status". |
| 929 | */ |
| 930 | err = ioctl_rpmb_card_status_poll(card, &status, 5); |
| 931 | if (err) |
| 932 | dev_err(mmc_dev(card->host), |
| 933 | "%s: Card Status=0x%08X, error %d\n", |
| 934 | __func__, status, err); |
| 935 | } |
| 936 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 937 | return err; |
| 938 | } |
| 939 | |
| 940 | static int mmc_blk_ioctl_cmd(struct block_device *bdev, |
| 941 | struct mmc_ioc_cmd __user *ic_ptr) |
| 942 | { |
| 943 | struct mmc_blk_ioc_data *idata; |
| 944 | struct mmc_blk_data *md; |
| 945 | struct mmc_card *card; |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 946 | int err = 0, ioc_err = 0; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 947 | |
Shawn Lin | 83c742c | 2016-03-16 18:15:47 +0800 | [diff] [blame] | 948 | /* |
| 949 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the |
| 950 | * whole block device, not on a partition. This prevents overspray |
| 951 | * between sibling partitions. |
| 952 | */ |
| 953 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) |
| 954 | return -EPERM; |
| 955 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 956 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); |
| 957 | if (IS_ERR(idata)) |
| 958 | return PTR_ERR(idata); |
| 959 | |
| 960 | md = mmc_blk_get(bdev->bd_disk); |
| 961 | if (!md) { |
| 962 | err = -EINVAL; |
| 963 | goto cmd_err; |
| 964 | } |
| 965 | |
| 966 | card = md->queue.card; |
| 967 | if (IS_ERR(card)) { |
| 968 | err = PTR_ERR(card); |
| 969 | goto cmd_done; |
| 970 | } |
| 971 | |
| 972 | mmc_get_card(card); |
| 973 | |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 974 | ioc_err = __mmc_blk_ioctl_cmd(card, md, idata); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 975 | |
Adrian Hunter | 3c86656 | 2016-05-04 14:38:12 +0300 | [diff] [blame] | 976 | /* Always switch back to main area after RPMB access */ |
| 977 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) |
| 978 | mmc_blk_part_switch(card, dev_get_drvdata(&card->dev)); |
| 979 | |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 980 | mmc_put_card(card); |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 981 | |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 982 | err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 983 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 984 | cmd_done: |
| 985 | mmc_blk_put(md); |
Philippe De Swert | 1c02f00 | 2012-04-11 23:31:45 +0300 | [diff] [blame] | 986 | cmd_err: |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 987 | kfree(idata->buf); |
| 988 | kfree(idata); |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 989 | return ioc_err ? ioc_err : err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 990 | } |
| 991 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 992 | static int mmc_blk_ioctl_multi_cmd(struct block_device *bdev, |
| 993 | struct mmc_ioc_multi_cmd __user *user) |
| 994 | { |
| 995 | struct mmc_blk_ioc_data **idata = NULL; |
| 996 | struct mmc_ioc_cmd __user *cmds = user->cmds; |
| 997 | struct mmc_card *card; |
| 998 | struct mmc_blk_data *md; |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 999 | int i, err = 0, ioc_err = 0; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1000 | __u64 num_of_cmds; |
| 1001 | |
Shawn Lin | 83c742c | 2016-03-16 18:15:47 +0800 | [diff] [blame] | 1002 | /* |
| 1003 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the |
| 1004 | * whole block device, not on a partition. This prevents overspray |
| 1005 | * between sibling partitions. |
| 1006 | */ |
| 1007 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) |
| 1008 | return -EPERM; |
| 1009 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1010 | if (copy_from_user(&num_of_cmds, &user->num_of_cmds, |
| 1011 | sizeof(num_of_cmds))) |
| 1012 | return -EFAULT; |
| 1013 | |
| 1014 | if (num_of_cmds > MMC_IOC_MAX_CMDS) |
| 1015 | return -EINVAL; |
| 1016 | |
| 1017 | idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL); |
| 1018 | if (!idata) |
| 1019 | return -ENOMEM; |
| 1020 | |
| 1021 | for (i = 0; i < num_of_cmds; i++) { |
| 1022 | idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]); |
| 1023 | if (IS_ERR(idata[i])) { |
| 1024 | err = PTR_ERR(idata[i]); |
| 1025 | num_of_cmds = i; |
| 1026 | goto cmd_err; |
| 1027 | } |
| 1028 | } |
| 1029 | |
| 1030 | md = mmc_blk_get(bdev->bd_disk); |
Olof Johansson | f00ab14 | 2016-02-09 09:34:30 -0800 | [diff] [blame] | 1031 | if (!md) { |
| 1032 | err = -EINVAL; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1033 | goto cmd_err; |
Olof Johansson | f00ab14 | 2016-02-09 09:34:30 -0800 | [diff] [blame] | 1034 | } |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1035 | |
| 1036 | card = md->queue.card; |
| 1037 | if (IS_ERR(card)) { |
| 1038 | err = PTR_ERR(card); |
| 1039 | goto cmd_done; |
| 1040 | } |
| 1041 | |
| 1042 | mmc_get_card(card); |
| 1043 | |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 1044 | for (i = 0; i < num_of_cmds && !ioc_err; i++) |
| 1045 | ioc_err = __mmc_blk_ioctl_cmd(card, md, idata[i]); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1046 | |
Adrian Hunter | 3c86656 | 2016-05-04 14:38:12 +0300 | [diff] [blame] | 1047 | /* Always switch back to main area after RPMB access */ |
| 1048 | if (md->area_type & MMC_BLK_DATA_AREA_RPMB) |
| 1049 | mmc_blk_part_switch(card, dev_get_drvdata(&card->dev)); |
| 1050 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1051 | mmc_put_card(card); |
| 1052 | |
| 1053 | /* copy to user if data and response */ |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 1054 | for (i = 0; i < num_of_cmds && !err; i++) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1055 | err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1056 | |
| 1057 | cmd_done: |
| 1058 | mmc_blk_put(md); |
| 1059 | cmd_err: |
| 1060 | for (i = 0; i < num_of_cmds; i++) { |
| 1061 | kfree(idata[i]->buf); |
| 1062 | kfree(idata[i]); |
| 1063 | } |
| 1064 | kfree(idata); |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 1065 | return ioc_err ? ioc_err : err; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1066 | } |
| 1067 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 1068 | static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, |
| 1069 | unsigned int cmd, unsigned long arg) |
| 1070 | { |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 1071 | switch (cmd) { |
| 1072 | case MMC_IOC_CMD: |
| 1073 | return mmc_blk_ioctl_cmd(bdev, |
| 1074 | (struct mmc_ioc_cmd __user *)arg); |
| 1075 | case MMC_IOC_MULTI_CMD: |
| 1076 | return mmc_blk_ioctl_multi_cmd(bdev, |
| 1077 | (struct mmc_ioc_multi_cmd __user *)arg); |
| 1078 | default: |
| 1079 | return -EINVAL; |
| 1080 | } |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | #ifdef CONFIG_COMPAT |
| 1084 | static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 1085 | unsigned int cmd, unsigned long arg) |
| 1086 | { |
| 1087 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); |
| 1088 | } |
| 1089 | #endif |
| 1090 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1091 | static const struct block_device_operations mmc_bdops = { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 1092 | .open = mmc_blk_open, |
| 1093 | .release = mmc_blk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1094 | .getgeo = mmc_blk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | .owner = THIS_MODULE, |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 1096 | .ioctl = mmc_blk_ioctl, |
| 1097 | #ifdef CONFIG_COMPAT |
| 1098 | .compat_ioctl = mmc_blk_compat_ioctl, |
| 1099 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | }; |
| 1101 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1102 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
| 1103 | struct mmc_blk_data *md) |
| 1104 | { |
| 1105 | int ret; |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 1106 | struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 1107 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1108 | if (main_md->part_curr == md->part_type) |
| 1109 | return 0; |
| 1110 | |
| 1111 | if (mmc_card_mmc(card)) { |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 1112 | u8 part_config = card->ext_csd.part_config; |
| 1113 | |
Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 1114 | if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) |
| 1115 | mmc_retune_pause(card->host); |
| 1116 | |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 1117 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 1118 | part_config |= md->part_type; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1119 | |
| 1120 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 1121 | EXT_CSD_PART_CONFIG, part_config, |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1122 | card->ext_csd.part_time); |
Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 1123 | if (ret) { |
| 1124 | if (md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) |
| 1125 | mmc_retune_unpause(card->host); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1126 | return ret; |
Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 1127 | } |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 1128 | |
| 1129 | card->ext_csd.part_config = part_config; |
Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 1130 | |
| 1131 | if (main_md->part_curr == EXT_CSD_PART_CONFIG_ACC_RPMB) |
| 1132 | mmc_retune_unpause(card->host); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1133 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1134 | |
| 1135 | main_md->part_curr = md->part_type; |
| 1136 | return 0; |
| 1137 | } |
| 1138 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1139 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
| 1140 | { |
| 1141 | int err; |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 1142 | u32 result; |
| 1143 | __be32 *blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1144 | |
Venkatraman S | ad5fd97 | 2011-08-25 00:30:50 +0530 | [diff] [blame] | 1145 | struct mmc_request mrq = {NULL}; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 1146 | struct mmc_command cmd = {0}; |
Chris Ball | a61ad2b | 2011-04-13 23:46:05 -0400 | [diff] [blame] | 1147 | struct mmc_data data = {0}; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1148 | |
| 1149 | struct scatterlist sg; |
| 1150 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1151 | cmd.opcode = MMC_APP_CMD; |
| 1152 | cmd.arg = card->rca << 16; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 1153 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1154 | |
| 1155 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 1156 | if (err) |
| 1157 | return (u32)-1; |
| 1158 | 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] | 1159 | return (u32)-1; |
| 1160 | |
| 1161 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 1162 | |
| 1163 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 1164 | cmd.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 1165 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1166 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1167 | data.blksz = 4; |
| 1168 | data.blocks = 1; |
| 1169 | data.flags = MMC_DATA_READ; |
| 1170 | data.sg = &sg; |
| 1171 | data.sg_len = 1; |
Subhash Jadavani | d380443 | 2012-06-13 17:10:43 +0530 | [diff] [blame] | 1172 | mmc_set_data_timeout(&data, card); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1173 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1174 | mrq.cmd = &cmd; |
| 1175 | mrq.data = &data; |
| 1176 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 1177 | blocks = kmalloc(4, GFP_KERNEL); |
| 1178 | if (!blocks) |
| 1179 | return (u32)-1; |
| 1180 | |
| 1181 | sg_init_one(&sg, blocks, 4); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1182 | |
| 1183 | mmc_wait_for_req(card->host, &mrq); |
| 1184 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 1185 | result = ntohl(*blocks); |
| 1186 | kfree(blocks); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1187 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 1188 | if (cmd.error || data.error) |
| 1189 | result = (u32)-1; |
| 1190 | |
| 1191 | return result; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Russell King - ARM Linux | 0a2d404 | 2011-06-20 20:10:08 +0100 | [diff] [blame] | 1194 | 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] | 1195 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 1196 | struct mmc_command cmd = {0}; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 1197 | int err; |
| 1198 | |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 1199 | cmd.opcode = MMC_SEND_STATUS; |
| 1200 | if (!mmc_host_is_spi(card->host)) |
| 1201 | cmd.arg = card->rca << 16; |
| 1202 | 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] | 1203 | err = mmc_wait_for_cmd(card->host, &cmd, retries); |
| 1204 | if (err == 0) |
| 1205 | *status = cmd.resp[0]; |
| 1206 | return err; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 1207 | } |
| 1208 | |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1209 | 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] | 1210 | bool hw_busy_detect, struct request *req, int *gen_err) |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1211 | { |
| 1212 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 1213 | int err = 0; |
| 1214 | u32 status; |
| 1215 | |
| 1216 | do { |
| 1217 | err = get_card_status(card, &status, 5); |
| 1218 | if (err) { |
| 1219 | pr_err("%s: error %d requesting status\n", |
| 1220 | req->rq_disk->disk_name, err); |
| 1221 | return err; |
| 1222 | } |
| 1223 | |
| 1224 | if (status & R1_ERROR) { |
| 1225 | pr_err("%s: %s: error sending status cmd, status %#x\n", |
| 1226 | req->rq_disk->disk_name, __func__, status); |
| 1227 | *gen_err = 1; |
| 1228 | } |
| 1229 | |
Ulf Hansson | 95a9129 | 2014-01-29 13:11:27 +0100 | [diff] [blame] | 1230 | /* We may rely on the host hw to handle busy detection.*/ |
| 1231 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && |
| 1232 | hw_busy_detect) |
| 1233 | break; |
| 1234 | |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1235 | /* |
| 1236 | * Timeout if the device never becomes ready for data and never |
| 1237 | * leaves the program state. |
| 1238 | */ |
| 1239 | if (time_after(jiffies, timeout)) { |
| 1240 | pr_err("%s: Card stuck in programming state! %s %s\n", |
| 1241 | mmc_hostname(card->host), |
| 1242 | req->rq_disk->disk_name, __func__); |
| 1243 | return -ETIMEDOUT; |
| 1244 | } |
| 1245 | |
| 1246 | /* |
| 1247 | * Some cards mishandle the status bits, |
| 1248 | * so make sure to check both the busy |
| 1249 | * indication and the card state. |
| 1250 | */ |
| 1251 | } while (!(status & R1_READY_FOR_DATA) || |
| 1252 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); |
| 1253 | |
| 1254 | return err; |
| 1255 | } |
| 1256 | |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1257 | static int send_stop(struct mmc_card *card, unsigned int timeout_ms, |
| 1258 | struct request *req, int *gen_err, u32 *stop_status) |
| 1259 | { |
| 1260 | struct mmc_host *host = card->host; |
| 1261 | struct mmc_command cmd = {0}; |
| 1262 | int err; |
| 1263 | bool use_r1b_resp = rq_data_dir(req) == WRITE; |
| 1264 | |
| 1265 | /* |
| 1266 | * Normally we use R1B responses for WRITE, but in cases where the host |
| 1267 | * has specified a max_busy_timeout we need to validate it. A failure |
| 1268 | * means we need to prevent the host from doing hw busy detection, which |
| 1269 | * is done by converting to a R1 response instead. |
| 1270 | */ |
| 1271 | if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) |
| 1272 | use_r1b_resp = false; |
| 1273 | |
| 1274 | cmd.opcode = MMC_STOP_TRANSMISSION; |
| 1275 | if (use_r1b_resp) { |
| 1276 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 1277 | cmd.busy_timeout = timeout_ms; |
| 1278 | } else { |
| 1279 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1280 | } |
| 1281 | |
| 1282 | err = mmc_wait_for_cmd(host, &cmd, 5); |
| 1283 | if (err) |
| 1284 | return err; |
| 1285 | |
| 1286 | *stop_status = cmd.resp[0]; |
| 1287 | |
| 1288 | /* No need to check card status in case of READ. */ |
| 1289 | if (rq_data_dir(req) == READ) |
| 1290 | return 0; |
| 1291 | |
| 1292 | if (!mmc_host_is_spi(host) && |
| 1293 | (*stop_status & R1_ERROR)) { |
| 1294 | pr_err("%s: %s: general error sending stop command, resp %#x\n", |
| 1295 | req->rq_disk->disk_name, __func__, *stop_status); |
| 1296 | *gen_err = 1; |
| 1297 | } |
| 1298 | |
| 1299 | return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err); |
| 1300 | } |
| 1301 | |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1302 | #define ERR_NOMEDIUM 3 |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1303 | #define ERR_RETRY 2 |
| 1304 | #define ERR_ABORT 1 |
| 1305 | #define ERR_CONTINUE 0 |
| 1306 | |
| 1307 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, |
| 1308 | bool status_valid, u32 status) |
| 1309 | { |
| 1310 | switch (error) { |
| 1311 | case -EILSEQ: |
| 1312 | /* response crc error, retry the r/w cmd */ |
| 1313 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 1314 | req->rq_disk->disk_name, "response CRC error", |
| 1315 | name, status); |
| 1316 | return ERR_RETRY; |
| 1317 | |
| 1318 | case -ETIMEDOUT: |
| 1319 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 1320 | req->rq_disk->disk_name, "timed out", name, status); |
| 1321 | |
| 1322 | /* If the status cmd initially failed, retry the r/w cmd */ |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1323 | if (!status_valid) { |
| 1324 | pr_err("%s: status not valid, retrying timeout\n", |
| 1325 | req->rq_disk->disk_name); |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1326 | return ERR_RETRY; |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1327 | } |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1328 | |
| 1329 | /* |
| 1330 | * If it was a r/w cmd crc error, or illegal command |
| 1331 | * (eg, issued in wrong state) then retry - we should |
| 1332 | * have corrected the state problem above. |
| 1333 | */ |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1334 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { |
| 1335 | pr_err("%s: command error, retrying timeout\n", |
| 1336 | req->rq_disk->disk_name); |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1337 | return ERR_RETRY; |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1338 | } |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1339 | |
| 1340 | /* Otherwise abort the command */ |
| 1341 | return ERR_ABORT; |
| 1342 | |
| 1343 | default: |
| 1344 | /* We don't understand the error code the driver gave us */ |
| 1345 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", |
| 1346 | req->rq_disk->disk_name, error, status); |
| 1347 | return ERR_ABORT; |
| 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | /* |
| 1352 | * Initial r/w and stop cmd error recovery. |
| 1353 | * We don't know whether the card received the r/w cmd or not, so try to |
| 1354 | * restore things back to a sane state. Essentially, we do this as follows: |
| 1355 | * - Obtain card status. If the first attempt to obtain card status fails, |
| 1356 | * the status word will reflect the failed status cmd, not the failed |
| 1357 | * r/w cmd. If we fail to obtain card status, it suggests we can no |
| 1358 | * longer communicate with the card. |
| 1359 | * - Check the card state. If the card received the cmd but there was a |
| 1360 | * transient problem with the response, it might still be in a data transfer |
| 1361 | * mode. Try to send it a stop command. If this fails, we can't recover. |
| 1362 | * - If the r/w cmd failed due to a response CRC error, it was probably |
| 1363 | * transient, so retry the cmd. |
| 1364 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. |
| 1365 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or |
| 1366 | * illegal cmd, retry. |
| 1367 | * Otherwise we don't understand what happened, so abort. |
| 1368 | */ |
| 1369 | 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] | 1370 | 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] | 1371 | { |
| 1372 | bool prev_cmd_status_valid = true; |
| 1373 | u32 status, stop_status = 0; |
| 1374 | int err, retry; |
| 1375 | |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1376 | if (mmc_card_removed(card)) |
| 1377 | return ERR_NOMEDIUM; |
| 1378 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1379 | /* |
| 1380 | * Try to get card status which indicates both the card state |
| 1381 | * and why there was no response. If the first attempt fails, |
| 1382 | * we can't be sure the returned status is for the r/w command. |
| 1383 | */ |
| 1384 | for (retry = 2; retry >= 0; retry--) { |
| 1385 | err = get_card_status(card, &status, 0); |
| 1386 | if (!err) |
| 1387 | break; |
| 1388 | |
Adrian Hunter | 6f398ad | 2015-05-07 13:10:23 +0300 | [diff] [blame] | 1389 | /* Re-tune if needed */ |
| 1390 | mmc_retune_recheck(card->host); |
| 1391 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1392 | prev_cmd_status_valid = false; |
| 1393 | pr_err("%s: error %d sending status command, %sing\n", |
| 1394 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); |
| 1395 | } |
| 1396 | |
| 1397 | /* We couldn't get a response from the card. Give up. */ |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1398 | if (err) { |
| 1399 | /* Check if the card is removed */ |
| 1400 | if (mmc_detect_card_removed(card->host)) |
| 1401 | return ERR_NOMEDIUM; |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1402 | return ERR_ABORT; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1403 | } |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1404 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1405 | /* Flag ECC errors */ |
| 1406 | if ((status & R1_CARD_ECC_FAILED) || |
| 1407 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || |
| 1408 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) |
| 1409 | *ecc_err = 1; |
| 1410 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1411 | /* Flag General errors */ |
| 1412 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) |
| 1413 | if ((status & R1_ERROR) || |
| 1414 | (brq->stop.resp[0] & R1_ERROR)) { |
| 1415 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", |
| 1416 | req->rq_disk->disk_name, __func__, |
| 1417 | brq->stop.resp[0], status); |
| 1418 | *gen_err = 1; |
| 1419 | } |
| 1420 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1421 | /* |
| 1422 | * Check the current card state. If it is in some data transfer |
| 1423 | * mode, tell it to stop (and hopefully transition back to TRAN.) |
| 1424 | */ |
| 1425 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || |
| 1426 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1427 | err = send_stop(card, |
| 1428 | DIV_ROUND_UP(brq->data.timeout_ns, 1000000), |
| 1429 | req, gen_err, &stop_status); |
| 1430 | if (err) { |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1431 | pr_err("%s: error %d sending stop command\n", |
| 1432 | req->rq_disk->disk_name, err); |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1433 | /* |
| 1434 | * If the stop cmd also timed out, the card is probably |
| 1435 | * not present, so abort. Other errors are bad news too. |
| 1436 | */ |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1437 | return ERR_ABORT; |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1438 | } |
| 1439 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1440 | if (stop_status & R1_CARD_ECC_FAILED) |
| 1441 | *ecc_err = 1; |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | /* Check for set block count errors */ |
| 1445 | if (brq->sbc.error) |
| 1446 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, |
| 1447 | prev_cmd_status_valid, status); |
| 1448 | |
| 1449 | /* Check for r/w command errors */ |
| 1450 | if (brq->cmd.error) |
| 1451 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, |
| 1452 | prev_cmd_status_valid, status); |
| 1453 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1454 | /* Data errors */ |
| 1455 | if (!brq->stop.error) |
| 1456 | return ERR_CONTINUE; |
| 1457 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1458 | /* Now for stop errors. These aren't fatal to the transfer. */ |
Johan Rudholm | 5e1344e | 2014-09-17 09:50:42 +0200 | [diff] [blame] | 1459 | 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] | 1460 | req->rq_disk->disk_name, brq->stop.error, |
| 1461 | brq->cmd.resp[0], status); |
| 1462 | |
| 1463 | /* |
| 1464 | * Subsitute in our own stop status as this will give the error |
| 1465 | * state which happened during the execution of the r/w command. |
| 1466 | */ |
| 1467 | if (stop_status) { |
| 1468 | brq->stop.resp[0] = stop_status; |
| 1469 | brq->stop.error = 0; |
| 1470 | } |
| 1471 | return ERR_CONTINUE; |
| 1472 | } |
| 1473 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1474 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
| 1475 | int type) |
| 1476 | { |
| 1477 | int err; |
| 1478 | |
| 1479 | if (md->reset_done & type) |
| 1480 | return -EEXIST; |
| 1481 | |
| 1482 | md->reset_done |= type; |
| 1483 | err = mmc_hw_reset(host); |
| 1484 | /* Ensure we switch back to the correct partition */ |
| 1485 | if (err != -EOPNOTSUPP) { |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 1486 | struct mmc_blk_data *main_md = |
| 1487 | dev_get_drvdata(&host->card->dev); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1488 | int part_err; |
| 1489 | |
| 1490 | main_md->part_curr = main_md->part_type; |
| 1491 | part_err = mmc_blk_part_switch(host->card, md); |
| 1492 | if (part_err) { |
| 1493 | /* |
| 1494 | * We have failed to get back into the correct |
| 1495 | * partition, so we need to abort the whole request. |
| 1496 | */ |
| 1497 | return -ENODEV; |
| 1498 | } |
| 1499 | } |
| 1500 | return err; |
| 1501 | } |
| 1502 | |
| 1503 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) |
| 1504 | { |
| 1505 | md->reset_done &= ~type; |
| 1506 | } |
| 1507 | |
Chuanxiao Dong | 4e93b9a | 2014-08-12 12:01:30 +0800 | [diff] [blame] | 1508 | int mmc_access_rpmb(struct mmc_queue *mq) |
| 1509 | { |
| 1510 | struct mmc_blk_data *md = mq->data; |
| 1511 | /* |
| 1512 | * If this is a RPMB partition access, return ture |
| 1513 | */ |
| 1514 | if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) |
| 1515 | return true; |
| 1516 | |
| 1517 | return false; |
| 1518 | } |
| 1519 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1520 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
| 1521 | { |
| 1522 | struct mmc_blk_data *md = mq->data; |
| 1523 | struct mmc_card *card = md->queue.card; |
| 1524 | unsigned int from, nr, arg; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1525 | int err = 0, type = MMC_BLK_DISCARD; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1526 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1527 | if (!mmc_can_erase(card)) { |
| 1528 | err = -EOPNOTSUPP; |
| 1529 | goto out; |
| 1530 | } |
| 1531 | |
| 1532 | from = blk_rq_pos(req); |
| 1533 | nr = blk_rq_sectors(req); |
| 1534 | |
Kyungmin Park | b3bf915 | 2011-10-18 09:34:04 +0900 | [diff] [blame] | 1535 | if (mmc_can_discard(card)) |
| 1536 | arg = MMC_DISCARD_ARG; |
| 1537 | else if (mmc_can_trim(card)) |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1538 | arg = MMC_TRIM_ARG; |
| 1539 | else |
| 1540 | arg = MMC_ERASE_ARG; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1541 | retry: |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1542 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1543 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1544 | INAND_CMD38_ARG_EXT_CSD, |
| 1545 | arg == MMC_TRIM_ARG ? |
| 1546 | INAND_CMD38_ARG_TRIM : |
| 1547 | INAND_CMD38_ARG_ERASE, |
| 1548 | 0); |
| 1549 | if (err) |
| 1550 | goto out; |
| 1551 | } |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1552 | err = mmc_erase(card, from, nr, arg); |
| 1553 | out: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1554 | if (err == -EIO && !mmc_blk_reset(md, card->host, type)) |
| 1555 | goto retry; |
| 1556 | if (!err) |
| 1557 | mmc_blk_reset_success(md, type); |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1558 | blk_end_request(req, err, blk_rq_bytes(req)); |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1559 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1560 | return err ? 0 : 1; |
| 1561 | } |
| 1562 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1563 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
| 1564 | struct request *req) |
| 1565 | { |
| 1566 | struct mmc_blk_data *md = mq->data; |
| 1567 | struct mmc_card *card = md->queue.card; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1568 | unsigned int from, nr, arg; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1569 | int err = 0, type = MMC_BLK_SECDISCARD; |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1570 | |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1571 | if (!(mmc_can_secure_erase_trim(card))) { |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1572 | err = -EOPNOTSUPP; |
| 1573 | goto out; |
| 1574 | } |
| 1575 | |
| 1576 | from = blk_rq_pos(req); |
| 1577 | nr = blk_rq_sectors(req); |
| 1578 | |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1579 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
| 1580 | arg = MMC_SECURE_TRIM1_ARG; |
| 1581 | else |
| 1582 | arg = MMC_SECURE_ERASE_ARG; |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1583 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1584 | retry: |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1585 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1586 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1587 | INAND_CMD38_ARG_EXT_CSD, |
| 1588 | arg == MMC_SECURE_TRIM1_ARG ? |
| 1589 | INAND_CMD38_ARG_SECTRIM1 : |
| 1590 | INAND_CMD38_ARG_SECERASE, |
| 1591 | 0); |
| 1592 | if (err) |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1593 | goto out_retry; |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1594 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1595 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1596 | err = mmc_erase(card, from, nr, arg); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1597 | if (err == -EIO) |
| 1598 | goto out_retry; |
| 1599 | if (err) |
| 1600 | goto out; |
| 1601 | |
| 1602 | if (arg == MMC_SECURE_TRIM1_ARG) { |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1603 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1604 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1605 | INAND_CMD38_ARG_EXT_CSD, |
| 1606 | INAND_CMD38_ARG_SECTRIM2, |
| 1607 | 0); |
| 1608 | if (err) |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1609 | goto out_retry; |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1610 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1611 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1612 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1613 | if (err == -EIO) |
| 1614 | goto out_retry; |
| 1615 | if (err) |
| 1616 | goto out; |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1617 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1618 | |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1619 | out_retry: |
| 1620 | if (err && !mmc_blk_reset(md, card->host, type)) |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1621 | goto retry; |
| 1622 | if (!err) |
| 1623 | mmc_blk_reset_success(md, type); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1624 | out: |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1625 | blk_end_request(req, err, blk_rq_bytes(req)); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1626 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1627 | return err ? 0 : 1; |
| 1628 | } |
| 1629 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1630 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
| 1631 | { |
| 1632 | struct mmc_blk_data *md = mq->data; |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1633 | struct mmc_card *card = md->queue.card; |
| 1634 | int ret = 0; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1635 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1636 | ret = mmc_flush_cache(card); |
| 1637 | if (ret) |
| 1638 | ret = -EIO; |
| 1639 | |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 1640 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 1641 | else if (atomic_read(&mq->cache_size)) { |
| 1642 | long used = mmc_blk_cache_used(mq, jiffies); |
| 1643 | |
| 1644 | if (used) { |
| 1645 | int speed = atomic_read(&mq->max_write_speed); |
| 1646 | |
| 1647 | if (speed_valid(speed)) { |
| 1648 | unsigned long msecs = jiffies_to_msecs( |
| 1649 | size_and_speed_to_jiffies( |
| 1650 | used, speed)); |
| 1651 | if (msecs) |
| 1652 | msleep(msecs); |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | #endif |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1657 | blk_end_request_all(req, ret); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1658 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1659 | return ret ? 0 : 1; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | /* |
| 1663 | * Reformat current write as a reliable write, supporting |
| 1664 | * both legacy and the enhanced reliable write MMC cards. |
| 1665 | * In each transfer we'll handle only as much as a single |
| 1666 | * reliable write can handle, thus finish the request in |
| 1667 | * partial completions. |
| 1668 | */ |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1669 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 1670 | struct mmc_card *card, |
| 1671 | struct request *req) |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1672 | { |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1673 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 1674 | /* Legacy mode imposes restrictions on transfers. */ |
| 1675 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) |
| 1676 | brq->data.blocks = 1; |
| 1677 | |
| 1678 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 1679 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 1680 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 1681 | brq->data.blocks = 1; |
| 1682 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1683 | } |
| 1684 | |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1685 | #define CMD_ERRORS \ |
| 1686 | (R1_OUT_OF_RANGE | /* Command argument out of range */ \ |
| 1687 | R1_ADDRESS_ERROR | /* Misaligned address */ \ |
| 1688 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ |
| 1689 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ |
| 1690 | R1_CC_ERROR | /* Card controller error */ \ |
| 1691 | R1_ERROR) /* General/unknown error */ |
| 1692 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1693 | static int mmc_blk_err_check(struct mmc_card *card, |
| 1694 | struct mmc_async_req *areq) |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1695 | { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1696 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, |
| 1697 | mmc_active); |
| 1698 | struct mmc_blk_request *brq = &mq_mrq->brq; |
| 1699 | struct request *req = mq_mrq->req; |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1700 | int need_retune = card->host->need_retune; |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1701 | int ecc_err = 0, gen_err = 0; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1702 | |
| 1703 | /* |
| 1704 | * sbc.error indicates a problem with the set block count |
| 1705 | * command. No data will have been transferred. |
| 1706 | * |
| 1707 | * cmd.error indicates a problem with the r/w command. No |
| 1708 | * data will have been transferred. |
| 1709 | * |
| 1710 | * stop.error indicates a problem with the stop command. Data |
| 1711 | * may have been transferred, or may still be transferring. |
| 1712 | */ |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1713 | if (brq->sbc.error || brq->cmd.error || brq->stop.error || |
| 1714 | brq->data.error) { |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1715 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1716 | case ERR_RETRY: |
| 1717 | return MMC_BLK_RETRY; |
| 1718 | case ERR_ABORT: |
| 1719 | return MMC_BLK_ABORT; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1720 | case ERR_NOMEDIUM: |
| 1721 | return MMC_BLK_NOMEDIUM; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1722 | case ERR_CONTINUE: |
| 1723 | break; |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | /* |
| 1728 | * Check for errors relating to the execution of the |
| 1729 | * initial command - such as address errors. No data |
| 1730 | * has been transferred. |
| 1731 | */ |
| 1732 | if (brq->cmd.resp[0] & CMD_ERRORS) { |
| 1733 | pr_err("%s: r/w command failed, status = %#x\n", |
| 1734 | req->rq_disk->disk_name, brq->cmd.resp[0]); |
| 1735 | return MMC_BLK_ABORT; |
| 1736 | } |
| 1737 | |
| 1738 | /* |
| 1739 | * Everything else is either success, or a data error of some |
| 1740 | * kind. If it was a write, we may have transitioned to |
| 1741 | * program mode, which we have to wait for it to complete. |
| 1742 | */ |
| 1743 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1744 | int err; |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 1745 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1746 | /* Check stop command response */ |
| 1747 | if (brq->stop.resp[0] & R1_ERROR) { |
| 1748 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", |
| 1749 | req->rq_disk->disk_name, __func__, |
| 1750 | brq->stop.resp[0]); |
| 1751 | gen_err = 1; |
| 1752 | } |
| 1753 | |
Ulf Hansson | 95a9129 | 2014-01-29 13:11:27 +0100 | [diff] [blame] | 1754 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, |
| 1755 | &gen_err); |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1756 | if (err) |
| 1757 | return MMC_BLK_CMD_ERR; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1758 | } |
| 1759 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1760 | /* if general error occurs, retry the write operation. */ |
| 1761 | if (gen_err) { |
| 1762 | pr_warn("%s: retrying write for general error\n", |
| 1763 | req->rq_disk->disk_name); |
| 1764 | return MMC_BLK_RETRY; |
| 1765 | } |
| 1766 | |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1767 | if (brq->data.error) { |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1768 | if (need_retune && !brq->retune_retry_done) { |
Russell King | 09faf61 | 2016-01-29 09:44:00 +0000 | [diff] [blame] | 1769 | pr_debug("%s: retrying because a re-tune was needed\n", |
| 1770 | req->rq_disk->disk_name); |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1771 | brq->retune_retry_done = 1; |
| 1772 | return MMC_BLK_RETRY; |
| 1773 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1774 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
| 1775 | req->rq_disk->disk_name, brq->data.error, |
| 1776 | (unsigned)blk_rq_pos(req), |
| 1777 | (unsigned)blk_rq_sectors(req), |
| 1778 | brq->cmd.resp[0], brq->stop.resp[0]); |
| 1779 | |
| 1780 | if (rq_data_dir(req) == READ) { |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1781 | if (ecc_err) |
| 1782 | return MMC_BLK_ECC_ERR; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1783 | return MMC_BLK_DATA_ERR; |
| 1784 | } else { |
| 1785 | return MMC_BLK_CMD_ERR; |
| 1786 | } |
| 1787 | } |
| 1788 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1789 | if (!brq->data.bytes_xfered) |
| 1790 | return MMC_BLK_RETRY; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1791 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1792 | if (mmc_packed_cmd(mq_mrq->cmd_type)) { |
| 1793 | if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered)) |
| 1794 | return MMC_BLK_PARTIAL; |
| 1795 | else |
| 1796 | return MMC_BLK_SUCCESS; |
| 1797 | } |
| 1798 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1799 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) |
| 1800 | return MMC_BLK_PARTIAL; |
| 1801 | |
| 1802 | return MMC_BLK_SUCCESS; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1803 | } |
| 1804 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1805 | static int mmc_blk_packed_err_check(struct mmc_card *card, |
| 1806 | struct mmc_async_req *areq) |
| 1807 | { |
| 1808 | struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req, |
| 1809 | mmc_active); |
| 1810 | struct request *req = mq_rq->req; |
| 1811 | struct mmc_packed *packed = mq_rq->packed; |
| 1812 | int err, check, status; |
| 1813 | u8 *ext_csd; |
| 1814 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1815 | packed->retries--; |
| 1816 | check = mmc_blk_err_check(card, areq); |
| 1817 | err = get_card_status(card, &status, 0); |
| 1818 | if (err) { |
| 1819 | pr_err("%s: error %d sending status command\n", |
| 1820 | req->rq_disk->disk_name, err); |
| 1821 | return MMC_BLK_ABORT; |
| 1822 | } |
| 1823 | |
| 1824 | if (status & R1_EXCEPTION_EVENT) { |
Ulf Hansson | 86817ff | 2014-10-17 11:39:05 +0200 | [diff] [blame] | 1825 | err = mmc_get_ext_csd(card, &ext_csd); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1826 | if (err) { |
| 1827 | pr_err("%s: error %d sending ext_csd\n", |
| 1828 | req->rq_disk->disk_name, err); |
Ulf Hansson | 86817ff | 2014-10-17 11:39:05 +0200 | [diff] [blame] | 1829 | return MMC_BLK_ABORT; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1830 | } |
| 1831 | |
| 1832 | if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] & |
| 1833 | EXT_CSD_PACKED_FAILURE) && |
| 1834 | (ext_csd[EXT_CSD_PACKED_CMD_STATUS] & |
| 1835 | EXT_CSD_PACKED_GENERIC_ERROR)) { |
| 1836 | if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] & |
| 1837 | EXT_CSD_PACKED_INDEXED_ERROR) { |
| 1838 | packed->idx_failure = |
| 1839 | ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1; |
| 1840 | check = MMC_BLK_PARTIAL; |
| 1841 | } |
| 1842 | pr_err("%s: packed cmd failed, nr %u, sectors %u, " |
| 1843 | "failure index: %d\n", |
| 1844 | req->rq_disk->disk_name, packed->nr_entries, |
| 1845 | packed->blocks, packed->idx_failure); |
| 1846 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1847 | kfree(ext_csd); |
| 1848 | } |
| 1849 | |
| 1850 | return check; |
| 1851 | } |
| 1852 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1853 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
| 1854 | struct mmc_card *card, |
| 1855 | int disable_multi, |
| 1856 | struct mmc_queue *mq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | { |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1858 | u32 readcmd, writecmd; |
| 1859 | struct mmc_blk_request *brq = &mqrq->brq; |
| 1860 | struct request *req = mqrq->req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1861 | struct mmc_blk_data *md = mq->data; |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1862 | bool do_data_tag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1864 | /* |
| 1865 | * Reliable writes are used to implement Forced Unit Access and |
Luca Porzio | d3df046 | 2015-11-06 15:12:26 +0000 | [diff] [blame] | 1866 | * are supported only on MMCs. |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1867 | */ |
Luca Porzio | d3df046 | 2015-11-06 15:12:26 +0000 | [diff] [blame] | 1868 | bool do_rel_wr = (req->cmd_flags & REQ_FUA) && |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1869 | (rq_data_dir(req) == WRITE) && |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1870 | (md->flags & MMC_BLK_REL_WR); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1871 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1872 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
| 1873 | brq->mrq.cmd = &brq->cmd; |
| 1874 | brq->mrq.data = &brq->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1875 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1876 | brq->cmd.arg = blk_rq_pos(req); |
| 1877 | if (!mmc_card_blockaddr(card)) |
| 1878 | brq->cmd.arg <<= 9; |
| 1879 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 1880 | brq->data.blksz = 512; |
| 1881 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 1882 | brq->stop.arg = 0; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1883 | brq->data.blocks = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 1885 | brq->data.fault_injected = false; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1886 | /* |
| 1887 | * The block layer doesn't support all sector count |
| 1888 | * restrictions, so we need to be prepared for too big |
| 1889 | * requests. |
| 1890 | */ |
| 1891 | if (brq->data.blocks > card->host->max_blk_count) |
| 1892 | brq->data.blocks = card->host->max_blk_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1893 | |
Paul Walmsley | 2bf22b3 | 2011-10-06 14:50:33 -0600 | [diff] [blame] | 1894 | if (brq->data.blocks > 1) { |
| 1895 | /* |
| 1896 | * After a read error, we redo the request one sector |
| 1897 | * at a time in order to accurately determine which |
| 1898 | * sectors can be read successfully. |
| 1899 | */ |
| 1900 | if (disable_multi) |
| 1901 | brq->data.blocks = 1; |
| 1902 | |
Kuninori Morimoto | 2e47e84 | 2014-09-02 19:08:53 -0700 | [diff] [blame] | 1903 | /* |
| 1904 | * Some controllers have HW issues while operating |
| 1905 | * in multiple I/O mode |
| 1906 | */ |
| 1907 | if (card->host->ops->multi_io_quirk) |
| 1908 | brq->data.blocks = card->host->ops->multi_io_quirk(card, |
| 1909 | (rq_data_dir(req) == READ) ? |
| 1910 | MMC_DATA_READ : MMC_DATA_WRITE, |
| 1911 | brq->data.blocks); |
Paul Walmsley | 2bf22b3 | 2011-10-06 14:50:33 -0600 | [diff] [blame] | 1912 | } |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1913 | |
| 1914 | if (brq->data.blocks > 1 || do_rel_wr) { |
| 1915 | /* SPI multiblock writes terminate using a special |
| 1916 | * token, not a STOP_TRANSMISSION request. |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 1917 | */ |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1918 | if (!mmc_host_is_spi(card->host) || |
| 1919 | rq_data_dir(req) == READ) |
| 1920 | brq->mrq.stop = &brq->stop; |
| 1921 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 1922 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
| 1923 | } else { |
| 1924 | brq->mrq.stop = NULL; |
| 1925 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 1926 | writecmd = MMC_WRITE_BLOCK; |
| 1927 | } |
| 1928 | if (rq_data_dir(req) == READ) { |
| 1929 | brq->cmd.opcode = readcmd; |
Jaehoon Chung | f53f110 | 2016-02-01 21:07:36 +0900 | [diff] [blame] | 1930 | brq->data.flags = MMC_DATA_READ; |
Ulf Hansson | bcc3e17 | 2014-01-14 21:24:21 +0100 | [diff] [blame] | 1931 | if (brq->mrq.stop) |
| 1932 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | |
| 1933 | MMC_CMD_AC; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1934 | } else { |
| 1935 | brq->cmd.opcode = writecmd; |
Jaehoon Chung | f53f110 | 2016-02-01 21:07:36 +0900 | [diff] [blame] | 1936 | brq->data.flags = MMC_DATA_WRITE; |
Ulf Hansson | bcc3e17 | 2014-01-14 21:24:21 +0100 | [diff] [blame] | 1937 | if (brq->mrq.stop) |
| 1938 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | |
| 1939 | MMC_CMD_AC; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1940 | } |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 1941 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1942 | if (do_rel_wr) |
| 1943 | mmc_apply_rel_rw(brq, card, req); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 1944 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1945 | /* |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1946 | * Data tag is used only during writing meta data to speed |
| 1947 | * up write and any subsequent read of this meta data |
| 1948 | */ |
| 1949 | do_data_tag = (card->ext_csd.data_tag_unit_size) && |
| 1950 | (req->cmd_flags & REQ_META) && |
| 1951 | (rq_data_dir(req) == WRITE) && |
| 1952 | ((brq->data.blocks * brq->data.blksz) >= |
| 1953 | card->ext_csd.data_tag_unit_size); |
| 1954 | |
| 1955 | /* |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1956 | * Pre-defined multi-block transfers are preferable to |
| 1957 | * open ended-ones (and necessary for reliable writes). |
| 1958 | * However, it is not sufficient to just send CMD23, |
| 1959 | * and avoid the final CMD12, as on an error condition |
| 1960 | * CMD12 (stop) needs to be sent anyway. This, coupled |
| 1961 | * with Auto-CMD23 enhancements provided by some |
| 1962 | * hosts, means that the complexity of dealing |
| 1963 | * with this is best left to the host. If CMD23 is |
| 1964 | * supported by card and host, we'll fill sbc in and let |
| 1965 | * the host deal with handling it correctly. This means |
| 1966 | * that for hosts that don't expose MMC_CAP_CMD23, no |
| 1967 | * change of behavior will be observed. |
| 1968 | * |
| 1969 | * N.B: Some MMC cards experience perf degradation. |
| 1970 | * We'll avoid using CMD23-bounded multiblock writes for |
| 1971 | * these, while retaining features like reliable writes. |
| 1972 | */ |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1973 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
| 1974 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || |
| 1975 | do_data_tag)) { |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1976 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 1977 | brq->sbc.arg = brq->data.blocks | |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1978 | (do_rel_wr ? (1 << 31) : 0) | |
| 1979 | (do_data_tag ? (1 << 29) : 0); |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1980 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1981 | brq->mrq.sbc = &brq->sbc; |
| 1982 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1983 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1984 | mmc_set_data_timeout(&brq->data, card); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1985 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1986 | brq->data.sg = mqrq->sg; |
| 1987 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1988 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1989 | /* |
| 1990 | * Adjust the sg list so it is the same size as the |
| 1991 | * request. |
| 1992 | */ |
| 1993 | if (brq->data.blocks != blk_rq_sectors(req)) { |
| 1994 | int i, data_size = brq->data.blocks << 9; |
| 1995 | struct scatterlist *sg; |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 1996 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1997 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { |
| 1998 | data_size -= sg->length; |
| 1999 | if (data_size <= 0) { |
| 2000 | sg->length += data_size; |
| 2001 | i++; |
| 2002 | break; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 2003 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 2004 | } |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2005 | brq->data.sg_len = i; |
| 2006 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 2007 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2008 | mqrq->mmc_active.mrq = &brq->mrq; |
| 2009 | mqrq->mmc_active.err_check = mmc_blk_err_check; |
| 2010 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2011 | mmc_queue_bounce_pre(mqrq); |
| 2012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2014 | static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q, |
| 2015 | struct mmc_card *card) |
| 2016 | { |
| 2017 | unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512; |
| 2018 | unsigned int max_seg_sz = queue_max_segment_size(q); |
| 2019 | unsigned int len, nr_segs = 0; |
| 2020 | |
| 2021 | do { |
| 2022 | len = min(hdr_sz, max_seg_sz); |
| 2023 | hdr_sz -= len; |
| 2024 | nr_segs++; |
| 2025 | } while (hdr_sz); |
| 2026 | |
| 2027 | return nr_segs; |
| 2028 | } |
| 2029 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2030 | static void mmc_blk_write_packing_control(struct mmc_queue *mq, |
| 2031 | struct request *req) |
| 2032 | { |
| 2033 | struct mmc_host *host = mq->card->host; |
| 2034 | int data_dir; |
| 2035 | |
| 2036 | if (!(host->caps2 & MMC_CAP2_PACKED_WR)) |
| 2037 | return; |
| 2038 | |
Maya Erez | 8e2b3c3 | 2012-12-02 13:27:15 +0200 | [diff] [blame] | 2039 | /* Support for the write packing on eMMC 4.5 or later */ |
| 2040 | if (mq->card->ext_csd.rev <= 5) |
| 2041 | return; |
| 2042 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2043 | /* |
| 2044 | * In case the packing control is not supported by the host, it should |
| 2045 | * not have an effect on the write packing. Therefore we have to enable |
| 2046 | * the write packing |
| 2047 | */ |
| 2048 | if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) { |
| 2049 | mq->wr_packing_enabled = true; |
| 2050 | return; |
| 2051 | } |
| 2052 | |
| 2053 | if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) { |
| 2054 | if (mq->num_of_potential_packed_wr_reqs > |
| 2055 | mq->num_wr_reqs_to_start_packing) |
| 2056 | mq->wr_packing_enabled = true; |
Tatyana Brokhman | 843915a | 2012-10-07 10:26:27 +0200 | [diff] [blame] | 2057 | mq->num_of_potential_packed_wr_reqs = 0; |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2058 | return; |
| 2059 | } |
| 2060 | |
| 2061 | data_dir = rq_data_dir(req); |
| 2062 | |
| 2063 | if (data_dir == READ) { |
| 2064 | mq->num_of_potential_packed_wr_reqs = 0; |
| 2065 | mq->wr_packing_enabled = false; |
| 2066 | return; |
| 2067 | } else if (data_dir == WRITE) { |
| 2068 | mq->num_of_potential_packed_wr_reqs++; |
| 2069 | } |
| 2070 | |
| 2071 | if (mq->num_of_potential_packed_wr_reqs > |
| 2072 | mq->num_wr_reqs_to_start_packing) |
| 2073 | mq->wr_packing_enabled = true; |
| 2074 | } |
| 2075 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2076 | struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card) |
| 2077 | { |
| 2078 | if (!card) |
| 2079 | return NULL; |
| 2080 | |
| 2081 | return &card->wr_pack_stats; |
| 2082 | } |
| 2083 | EXPORT_SYMBOL(mmc_blk_get_packed_statistics); |
| 2084 | |
| 2085 | void mmc_blk_init_packed_statistics(struct mmc_card *card) |
| 2086 | { |
| 2087 | int max_num_of_packed_reqs = 0; |
| 2088 | |
| 2089 | if (!card || !card->wr_pack_stats.packing_events) |
| 2090 | return; |
| 2091 | |
| 2092 | max_num_of_packed_reqs = card->ext_csd.max_packed_writes; |
| 2093 | |
| 2094 | spin_lock(&card->wr_pack_stats.lock); |
| 2095 | memset(card->wr_pack_stats.packing_events, 0, |
| 2096 | (max_num_of_packed_reqs + 1) * |
| 2097 | sizeof(*card->wr_pack_stats.packing_events)); |
| 2098 | memset(&card->wr_pack_stats.pack_stop_reason, 0, |
| 2099 | sizeof(card->wr_pack_stats.pack_stop_reason)); |
| 2100 | card->wr_pack_stats.enabled = true; |
| 2101 | spin_unlock(&card->wr_pack_stats.lock); |
| 2102 | } |
| 2103 | EXPORT_SYMBOL(mmc_blk_init_packed_statistics); |
| 2104 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2105 | static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req) |
| 2106 | { |
| 2107 | struct request_queue *q = mq->queue; |
| 2108 | struct mmc_card *card = mq->card; |
| 2109 | struct request *cur = req, *next = NULL; |
| 2110 | struct mmc_blk_data *md = mq->data; |
| 2111 | struct mmc_queue_req *mqrq = mq->mqrq_cur; |
| 2112 | bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN; |
| 2113 | unsigned int req_sectors = 0, phys_segments = 0; |
| 2114 | unsigned int max_blk_count, max_phys_segs; |
| 2115 | bool put_back = true; |
| 2116 | u8 max_packed_rw = 0; |
| 2117 | u8 reqs = 0; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2118 | struct mmc_wr_pack_stats *stats = &card->wr_pack_stats; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2119 | |
Shawn Lin | 96e52da | 2016-08-26 08:49:55 +0800 | [diff] [blame] | 2120 | /* |
| 2121 | * We don't need to check packed for any further |
| 2122 | * operation of packed stuff as we set MMC_PACKED_NONE |
| 2123 | * and return zero for reqs if geting null packed. Also |
| 2124 | * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing |
| 2125 | * it again when removing blk req. |
| 2126 | */ |
| 2127 | if (!mqrq->packed) { |
| 2128 | md->flags &= (~MMC_BLK_PACKED_CMD); |
| 2129 | goto no_packed; |
| 2130 | } |
| 2131 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2132 | if (!(md->flags & MMC_BLK_PACKED_CMD)) |
| 2133 | goto no_packed; |
| 2134 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2135 | if (!mq->wr_packing_enabled) |
| 2136 | goto no_packed; |
| 2137 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2138 | if ((rq_data_dir(cur) == WRITE) && |
| 2139 | mmc_host_packed_wr(card->host)) |
| 2140 | max_packed_rw = card->ext_csd.max_packed_writes; |
| 2141 | |
| 2142 | if (max_packed_rw == 0) |
| 2143 | goto no_packed; |
| 2144 | |
| 2145 | if (mmc_req_rel_wr(cur) && |
| 2146 | (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) |
| 2147 | goto no_packed; |
| 2148 | |
| 2149 | if (mmc_large_sector(card) && |
| 2150 | !IS_ALIGNED(blk_rq_sectors(cur), 8)) |
| 2151 | goto no_packed; |
| 2152 | |
| 2153 | mmc_blk_clear_packed(mqrq); |
| 2154 | |
| 2155 | max_blk_count = min(card->host->max_blk_count, |
| 2156 | card->host->max_req_size >> 9); |
| 2157 | if (unlikely(max_blk_count > 0xffff)) |
| 2158 | max_blk_count = 0xffff; |
| 2159 | |
| 2160 | max_phys_segs = queue_max_segments(q); |
| 2161 | req_sectors += blk_rq_sectors(cur); |
| 2162 | phys_segments += cur->nr_phys_segments; |
| 2163 | |
| 2164 | if (rq_data_dir(cur) == WRITE) { |
| 2165 | req_sectors += mmc_large_sector(card) ? 8 : 1; |
| 2166 | phys_segments += mmc_calc_packed_hdr_segs(q, card); |
| 2167 | } |
| 2168 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2169 | spin_lock(&stats->lock); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2170 | do { |
| 2171 | if (reqs >= max_packed_rw - 1) { |
| 2172 | put_back = false; |
| 2173 | break; |
| 2174 | } |
| 2175 | |
| 2176 | spin_lock_irq(q->queue_lock); |
| 2177 | next = blk_fetch_request(q); |
| 2178 | spin_unlock_irq(q->queue_lock); |
| 2179 | if (!next) { |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2180 | MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2181 | put_back = false; |
| 2182 | break; |
| 2183 | } |
| 2184 | |
| 2185 | if (mmc_large_sector(card) && |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2186 | !IS_ALIGNED(blk_rq_sectors(next), 8)) { |
| 2187 | MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2188 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2189 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2190 | |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 2191 | if (req_op(next) == REQ_OP_DISCARD || |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 2192 | req_op(next) == REQ_OP_SECURE_ERASE || |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2193 | req_op(next) == REQ_OP_FLUSH) { |
| 2194 | if (req_op(next) != REQ_OP_SECURE_ERASE) |
| 2195 | MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2196 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2197 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2198 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2199 | if (rq_data_dir(cur) != rq_data_dir(next)) { |
| 2200 | MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2201 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2202 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2203 | |
| 2204 | if (mmc_req_rel_wr(next) && |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2205 | (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) { |
| 2206 | MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2207 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2208 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2209 | |
| 2210 | req_sectors += blk_rq_sectors(next); |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2211 | if (req_sectors > max_blk_count) { |
| 2212 | if (stats->enabled) |
| 2213 | stats->pack_stop_reason[EXCEEDS_SECTORS]++; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2214 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2215 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2216 | |
| 2217 | phys_segments += next->nr_phys_segments; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2218 | if (phys_segments > max_phys_segs) { |
| 2219 | MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2220 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2221 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2222 | |
Maya Erez | 5a8dae1 | 2014-12-04 15:13:59 +0200 | [diff] [blame^] | 2223 | if (mq->no_pack_for_random) { |
| 2224 | if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) != |
| 2225 | blk_rq_pos(next)) { |
| 2226 | MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM); |
| 2227 | put_back = 1; |
| 2228 | break; |
| 2229 | } |
| 2230 | } |
| 2231 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2232 | if (rq_data_dir(next) == WRITE) |
| 2233 | mq->num_of_potential_packed_wr_reqs++; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2234 | list_add_tail(&next->queuelist, &mqrq->packed->list); |
| 2235 | cur = next; |
| 2236 | reqs++; |
| 2237 | } while (1); |
| 2238 | |
| 2239 | if (put_back) { |
| 2240 | spin_lock_irq(q->queue_lock); |
| 2241 | blk_requeue_request(q, next); |
| 2242 | spin_unlock_irq(q->queue_lock); |
| 2243 | } |
| 2244 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2245 | if (stats->enabled) { |
| 2246 | if (reqs + 1 <= card->ext_csd.max_packed_writes) |
| 2247 | stats->packing_events[reqs + 1]++; |
| 2248 | if (reqs + 1 == max_packed_rw) |
| 2249 | MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD); |
| 2250 | } |
| 2251 | |
| 2252 | spin_unlock(&stats->lock); |
| 2253 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2254 | if (reqs > 0) { |
| 2255 | list_add(&req->queuelist, &mqrq->packed->list); |
| 2256 | mqrq->packed->nr_entries = ++reqs; |
| 2257 | mqrq->packed->retries = reqs; |
| 2258 | return reqs; |
| 2259 | } |
| 2260 | |
| 2261 | no_packed: |
| 2262 | mqrq->cmd_type = MMC_PACKED_NONE; |
| 2263 | return 0; |
| 2264 | } |
| 2265 | |
| 2266 | static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq, |
| 2267 | struct mmc_card *card, |
| 2268 | struct mmc_queue *mq) |
| 2269 | { |
| 2270 | struct mmc_blk_request *brq = &mqrq->brq; |
| 2271 | struct request *req = mqrq->req; |
| 2272 | struct request *prq; |
| 2273 | struct mmc_blk_data *md = mq->data; |
| 2274 | struct mmc_packed *packed = mqrq->packed; |
| 2275 | bool do_rel_wr, do_data_tag; |
Jiri Slaby | 3f2d266 | 2016-10-03 10:58:28 +0200 | [diff] [blame] | 2276 | __le32 *packed_cmd_hdr; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2277 | u8 hdr_blocks; |
| 2278 | u8 i = 1; |
| 2279 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2280 | mqrq->cmd_type = MMC_PACKED_WRITE; |
| 2281 | packed->blocks = 0; |
| 2282 | packed->idx_failure = MMC_PACKED_NR_IDX; |
| 2283 | |
| 2284 | packed_cmd_hdr = packed->cmd_hdr; |
| 2285 | memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr)); |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2286 | packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) | |
| 2287 | (PACKED_CMD_WR << 8) | PACKED_CMD_VER); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2288 | hdr_blocks = mmc_large_sector(card) ? 8 : 1; |
| 2289 | |
| 2290 | /* |
| 2291 | * Argument for each entry of packed group |
| 2292 | */ |
| 2293 | list_for_each_entry(prq, &packed->list, queuelist) { |
| 2294 | do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR); |
| 2295 | do_data_tag = (card->ext_csd.data_tag_unit_size) && |
| 2296 | (prq->cmd_flags & REQ_META) && |
| 2297 | (rq_data_dir(prq) == WRITE) && |
Adrian Hunter | d806b46 | 2016-06-10 16:22:16 +0300 | [diff] [blame] | 2298 | blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2299 | /* Argument of CMD23 */ |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2300 | packed_cmd_hdr[(i * 2)] = cpu_to_le32( |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2301 | (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) | |
| 2302 | (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) | |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2303 | blk_rq_sectors(prq)); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2304 | /* Argument of CMD18 or CMD25 */ |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2305 | packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32( |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2306 | mmc_card_blockaddr(card) ? |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2307 | blk_rq_pos(prq) : blk_rq_pos(prq) << 9); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2308 | packed->blocks += blk_rq_sectors(prq); |
| 2309 | i++; |
| 2310 | } |
| 2311 | |
| 2312 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
| 2313 | brq->mrq.cmd = &brq->cmd; |
| 2314 | brq->mrq.data = &brq->data; |
| 2315 | brq->mrq.sbc = &brq->sbc; |
| 2316 | brq->mrq.stop = &brq->stop; |
| 2317 | |
| 2318 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 2319 | brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks); |
| 2320 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 2321 | |
| 2322 | brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; |
| 2323 | brq->cmd.arg = blk_rq_pos(req); |
| 2324 | if (!mmc_card_blockaddr(card)) |
| 2325 | brq->cmd.arg <<= 9; |
| 2326 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 2327 | |
| 2328 | brq->data.blksz = 512; |
| 2329 | brq->data.blocks = packed->blocks + hdr_blocks; |
Jaehoon Chung | f53f110 | 2016-02-01 21:07:36 +0900 | [diff] [blame] | 2330 | brq->data.flags = MMC_DATA_WRITE; |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 2331 | brq->data.fault_injected = false; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2332 | |
| 2333 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 2334 | brq->stop.arg = 0; |
| 2335 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 2336 | |
| 2337 | mmc_set_data_timeout(&brq->data, card); |
| 2338 | |
| 2339 | brq->data.sg = mqrq->sg; |
| 2340 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); |
| 2341 | |
| 2342 | mqrq->mmc_active.mrq = &brq->mrq; |
Tatyana Brokhman | 71aefb8 | 2012-10-09 13:50:56 +0200 | [diff] [blame] | 2343 | |
| 2344 | /* |
| 2345 | * This is intended for packed commands tests usage - in case these |
| 2346 | * functions are not in use the respective pointers are NULL |
| 2347 | */ |
| 2348 | if (mq->err_check_fn) |
| 2349 | mqrq->mmc_active.err_check = mq->err_check_fn; |
| 2350 | else |
| 2351 | mqrq->mmc_active.err_check = mmc_blk_packed_err_check; |
| 2352 | |
| 2353 | if (mq->packed_test_fn) |
| 2354 | mq->packed_test_fn(mq->queue, mqrq); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2355 | |
| 2356 | mmc_queue_bounce_pre(mqrq); |
| 2357 | } |
| 2358 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2359 | static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
| 2360 | struct mmc_blk_request *brq, struct request *req, |
| 2361 | int ret) |
| 2362 | { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2363 | struct mmc_queue_req *mq_rq; |
| 2364 | mq_rq = container_of(brq, struct mmc_queue_req, brq); |
| 2365 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2366 | /* |
| 2367 | * If this is an SD card and we're writing, we can first |
| 2368 | * mark the known good sectors as ok. |
| 2369 | * |
| 2370 | * If the card is not SD, we can still ok written sectors |
| 2371 | * as reported by the controller (which might be less than |
| 2372 | * the real number of written sectors, but never more). |
| 2373 | */ |
| 2374 | if (mmc_card_sd(card)) { |
| 2375 | u32 blocks; |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 2376 | if (!brq->data.fault_injected) { |
| 2377 | blocks = mmc_sd_num_wr_blocks(card); |
| 2378 | if (blocks != (u32)-1) |
| 2379 | ret = blk_end_request(req, 0, blocks << 9); |
| 2380 | } else |
| 2381 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2382 | } else { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2383 | if (!mmc_packed_cmd(mq_rq->cmd_type)) |
| 2384 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2385 | } |
| 2386 | return ret; |
| 2387 | } |
| 2388 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2389 | static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq) |
| 2390 | { |
| 2391 | struct request *prq; |
| 2392 | struct mmc_packed *packed = mq_rq->packed; |
| 2393 | int idx = packed->idx_failure, i = 0; |
| 2394 | int ret = 0; |
| 2395 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2396 | while (!list_empty(&packed->list)) { |
| 2397 | prq = list_entry_rq(packed->list.next); |
| 2398 | if (idx == i) { |
| 2399 | /* retry from error index */ |
| 2400 | packed->nr_entries -= idx; |
| 2401 | mq_rq->req = prq; |
| 2402 | ret = 1; |
| 2403 | |
| 2404 | if (packed->nr_entries == MMC_PACKED_NR_SINGLE) { |
| 2405 | list_del_init(&prq->queuelist); |
| 2406 | mmc_blk_clear_packed(mq_rq); |
| 2407 | } |
| 2408 | return ret; |
| 2409 | } |
| 2410 | list_del_init(&prq->queuelist); |
| 2411 | blk_end_request(prq, 0, blk_rq_bytes(prq)); |
| 2412 | i++; |
| 2413 | } |
| 2414 | |
| 2415 | mmc_blk_clear_packed(mq_rq); |
| 2416 | return ret; |
| 2417 | } |
| 2418 | |
| 2419 | static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq) |
| 2420 | { |
| 2421 | struct request *prq; |
| 2422 | struct mmc_packed *packed = mq_rq->packed; |
| 2423 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2424 | while (!list_empty(&packed->list)) { |
| 2425 | prq = list_entry_rq(packed->list.next); |
| 2426 | list_del_init(&prq->queuelist); |
| 2427 | blk_end_request(prq, -EIO, blk_rq_bytes(prq)); |
| 2428 | } |
| 2429 | |
| 2430 | mmc_blk_clear_packed(mq_rq); |
| 2431 | } |
| 2432 | |
| 2433 | static void mmc_blk_revert_packed_req(struct mmc_queue *mq, |
| 2434 | struct mmc_queue_req *mq_rq) |
| 2435 | { |
| 2436 | struct request *prq; |
| 2437 | struct request_queue *q = mq->queue; |
| 2438 | struct mmc_packed *packed = mq_rq->packed; |
| 2439 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2440 | while (!list_empty(&packed->list)) { |
| 2441 | prq = list_entry_rq(packed->list.prev); |
| 2442 | if (prq->queuelist.prev != &packed->list) { |
| 2443 | list_del_init(&prq->queuelist); |
| 2444 | spin_lock_irq(q->queue_lock); |
| 2445 | blk_requeue_request(mq->queue, prq); |
| 2446 | spin_unlock_irq(q->queue_lock); |
| 2447 | } else { |
| 2448 | list_del_init(&prq->queuelist); |
| 2449 | } |
| 2450 | } |
| 2451 | |
| 2452 | mmc_blk_clear_packed(mq_rq); |
| 2453 | } |
| 2454 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2455 | 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] | 2456 | { |
| 2457 | struct mmc_blk_data *md = mq->data; |
| 2458 | struct mmc_card *card = md->queue.card; |
| 2459 | struct mmc_blk_request *brq = &mq->mqrq_cur->brq; |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2460 | 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] | 2461 | enum mmc_blk_status status; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2462 | struct mmc_queue_req *mq_rq; |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2463 | struct request *req = rqc; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2464 | struct mmc_async_req *areq; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2465 | const u8 packed_nr = 2; |
| 2466 | u8 reqs = 0; |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2467 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 2468 | unsigned long waitfor = jiffies; |
| 2469 | #endif |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2470 | |
| 2471 | if (!rqc && !mq->mqrq_prev->req) |
| 2472 | return 0; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2473 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2474 | if (rqc) |
| 2475 | reqs = mmc_blk_prep_packed_list(mq, rqc); |
| 2476 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2477 | do { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2478 | if (rqc) { |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2479 | /* |
| 2480 | * When 4KB native sector is enabled, only 8 blocks |
| 2481 | * multiple read or write is allowed |
| 2482 | */ |
Yuan, Juntao | e87c856 | 2016-05-13 07:59:24 +0000 | [diff] [blame] | 2483 | if (mmc_large_sector(card) && |
| 2484 | !IS_ALIGNED(blk_rq_sectors(rqc), 8)) { |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2485 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", |
| 2486 | req->rq_disk->disk_name); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2487 | mq_rq = mq->mqrq_cur; |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2488 | goto cmd_abort; |
| 2489 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2490 | |
| 2491 | if (reqs >= packed_nr) |
| 2492 | mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur, |
| 2493 | card, mq); |
| 2494 | else |
| 2495 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2496 | areq = &mq->mqrq_cur->mmc_active; |
| 2497 | } else |
| 2498 | areq = NULL; |
| 2499 | areq = mmc_start_req(card->host, areq, (int *) &status); |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2500 | if (!areq) { |
| 2501 | if (status == MMC_BLK_NEW_REQUEST) |
| 2502 | mq->flags |= MMC_QUEUE_NEW_REQUEST; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2503 | return 0; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2504 | } |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 2505 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2506 | mq_rq = container_of(areq, struct mmc_queue_req, mmc_active); |
| 2507 | brq = &mq_rq->brq; |
| 2508 | req = mq_rq->req; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2509 | type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2510 | mmc_queue_bounce_post(mq_rq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 2511 | |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2512 | switch (status) { |
| 2513 | case MMC_BLK_SUCCESS: |
| 2514 | case MMC_BLK_PARTIAL: |
| 2515 | /* |
| 2516 | * A block was successfully transferred. |
| 2517 | */ |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2518 | mmc_blk_reset_success(md, type); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2519 | |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2520 | mmc_blk_simulate_delay(mq, rqc, waitfor); |
| 2521 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2522 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2523 | ret = mmc_blk_end_packed_req(mq_rq); |
| 2524 | break; |
| 2525 | } else { |
| 2526 | ret = blk_end_request(req, 0, |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2527 | brq->data.bytes_xfered); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2528 | } |
| 2529 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2530 | /* |
| 2531 | * If the blk_end_request function returns non-zero even |
| 2532 | * though all data has been transferred and no errors |
| 2533 | * were returned by the host controller, it's a bug. |
| 2534 | */ |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2535 | if (status == MMC_BLK_SUCCESS && ret) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2536 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2537 | __func__, blk_rq_bytes(req), |
| 2538 | brq->data.bytes_xfered); |
| 2539 | rqc = NULL; |
| 2540 | goto cmd_abort; |
| 2541 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2542 | break; |
| 2543 | case MMC_BLK_CMD_ERR: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2544 | ret = mmc_blk_cmd_err(md, card, brq, req, ret); |
Ding Wang | 29535f7 | 2015-05-18 20:14:15 +0800 | [diff] [blame] | 2545 | if (mmc_blk_reset(md, card->host, type)) |
| 2546 | goto cmd_abort; |
| 2547 | if (!ret) |
| 2548 | goto start_new_req; |
| 2549 | break; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2550 | case MMC_BLK_RETRY: |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2551 | retune_retry_done = brq->retune_retry_done; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2552 | if (retry++ < 5) |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 2553 | break; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2554 | /* Fall through */ |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2555 | case MMC_BLK_ABORT: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2556 | if (!mmc_blk_reset(md, card->host, type)) |
| 2557 | break; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 2558 | goto cmd_abort; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2559 | case MMC_BLK_DATA_ERR: { |
| 2560 | int err; |
| 2561 | |
| 2562 | err = mmc_blk_reset(md, card->host, type); |
| 2563 | if (!err) |
| 2564 | break; |
Sahitya Tummala | d0a1984 | 2014-10-31 09:46:20 +0530 | [diff] [blame] | 2565 | goto cmd_abort; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2566 | } |
| 2567 | case MMC_BLK_ECC_ERR: |
| 2568 | if (brq->data.blocks > 1) { |
| 2569 | /* Redo read one sector at a time */ |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2570 | pr_warn("%s: retrying using single block read\n", |
| 2571 | req->rq_disk->disk_name); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2572 | disable_multi = 1; |
| 2573 | break; |
| 2574 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2575 | /* |
| 2576 | * After an error, we redo I/O one sector at a |
| 2577 | * time, so we only reach here after trying to |
| 2578 | * read a single sector. |
| 2579 | */ |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 2580 | ret = blk_end_request(req, -EIO, |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2581 | brq->data.blksz); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2582 | if (!ret) |
| 2583 | goto start_new_req; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2584 | break; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 2585 | case MMC_BLK_NOMEDIUM: |
| 2586 | goto cmd_abort; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2587 | default: |
| 2588 | pr_err("%s: Unhandled return value (%d)", |
| 2589 | req->rq_disk->disk_name, status); |
| 2590 | goto cmd_abort; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 2591 | } |
| 2592 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2593 | if (ret) { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2594 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2595 | if (!mq_rq->packed->retries) |
| 2596 | goto cmd_abort; |
| 2597 | mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq); |
| 2598 | mmc_start_req(card->host, |
| 2599 | &mq_rq->mmc_active, NULL); |
| 2600 | } else { |
| 2601 | |
| 2602 | /* |
| 2603 | * In case of a incomplete request |
| 2604 | * prepare it again and resend. |
| 2605 | */ |
| 2606 | mmc_blk_rw_rq_prep(mq_rq, card, |
| 2607 | disable_multi, mq); |
| 2608 | mmc_start_req(card->host, |
| 2609 | &mq_rq->mmc_active, NULL); |
| 2610 | } |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2611 | mq_rq->brq.retune_retry_done = retune_retry_done; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2612 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | } while (ret); |
| 2614 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2615 | return 1; |
| 2616 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 2617 | cmd_abort: |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2618 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2619 | mmc_blk_abort_packed_req(mq_rq); |
| 2620 | } else { |
| 2621 | if (mmc_card_removed(card)) |
| 2622 | req->cmd_flags |= REQ_QUIET; |
| 2623 | while (ret) |
| 2624 | ret = blk_end_request(req, -EIO, |
| 2625 | blk_rq_cur_bytes(req)); |
| 2626 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2628 | start_new_req: |
| 2629 | if (rqc) { |
Seungwon Jeon | 7a81902 | 2013-01-22 19:48:07 +0900 | [diff] [blame] | 2630 | if (mmc_card_removed(card)) { |
| 2631 | rqc->cmd_flags |= REQ_QUIET; |
| 2632 | blk_end_request_all(rqc, -EIO); |
| 2633 | } else { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2634 | /* |
| 2635 | * If current request is packed, it needs to put back. |
| 2636 | */ |
| 2637 | if (mmc_packed_cmd(mq->mqrq_cur->cmd_type)) |
| 2638 | mmc_blk_revert_packed_req(mq, mq->mqrq_cur); |
| 2639 | |
Seungwon Jeon | 7a81902 | 2013-01-22 19:48:07 +0900 | [diff] [blame] | 2640 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
| 2641 | mmc_start_req(card->host, |
| 2642 | &mq->mqrq_cur->mmc_active, NULL); |
| 2643 | } |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2644 | } |
| 2645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | return 0; |
| 2647 | } |
| 2648 | |
Linus Walleij | 29eb7bd | 2016-09-20 11:34:38 +0200 | [diff] [blame] | 2649 | int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2650 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2651 | int ret; |
| 2652 | struct mmc_blk_data *md = mq->data; |
| 2653 | struct mmc_card *card = md->queue.card; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2654 | struct mmc_host *host = card->host; |
| 2655 | unsigned long flags; |
Adrian Hunter | 869c554 | 2016-08-25 14:11:43 -0600 | [diff] [blame] | 2656 | bool req_is_special = mmc_req_is_special(req); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2657 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2658 | if (req && !mq->mqrq_prev->req) |
| 2659 | /* claim host only for the first request */ |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2660 | mmc_get_card(card); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2661 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2662 | ret = mmc_blk_part_switch(card, md); |
| 2663 | if (ret) { |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2664 | if (req) { |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 2665 | blk_end_request_all(req, -EIO); |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2666 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2667 | ret = 0; |
| 2668 | goto out; |
| 2669 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2670 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2671 | mmc_blk_write_packing_control(mq, req); |
| 2672 | |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2673 | mq->flags &= ~MMC_QUEUE_NEW_REQUEST; |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 2674 | if (req && req_op(req) == REQ_OP_DISCARD) { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2675 | /* complete ongoing async transfer before issuing discard */ |
| 2676 | if (card->host->areq) |
| 2677 | mmc_blk_issue_rw_rq(mq, NULL); |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 2678 | ret = mmc_blk_issue_discard_rq(mq, req); |
| 2679 | } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) { |
| 2680 | /* complete ongoing async transfer before issuing secure erase*/ |
| 2681 | if (card->host->areq) |
| 2682 | mmc_blk_issue_rw_rq(mq, NULL); |
| 2683 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 2684 | } else if (req && req_op(req) == REQ_OP_FLUSH) { |
Jaehoon Chung | 393f9a0 | 2011-07-13 17:02:16 +0900 | [diff] [blame] | 2685 | /* complete ongoing async transfer before issuing flush */ |
| 2686 | if (card->host->areq) |
| 2687 | mmc_blk_issue_rw_rq(mq, NULL); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2688 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2689 | } else { |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2690 | if (!req && host->areq) { |
| 2691 | spin_lock_irqsave(&host->context_info.lock, flags); |
| 2692 | host->context_info.is_waiting_last_req = true; |
| 2693 | spin_unlock_irqrestore(&host->context_info.lock, flags); |
| 2694 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2695 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2696 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2697 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2698 | out: |
Adrian Hunter | 869c554 | 2016-08-25 14:11:43 -0600 | [diff] [blame] | 2699 | if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) || req_is_special) |
Seungwon Jeon | ef3a69c7 | 2013-03-14 15:17:13 +0900 | [diff] [blame] | 2700 | /* |
| 2701 | * Release host when there are no more requests |
| 2702 | * and after special request(discard, flush) is done. |
| 2703 | * In case sepecial request, there is no reentry to |
| 2704 | * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'. |
| 2705 | */ |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2706 | mmc_put_card(card); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2707 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2708 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2710 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 2711 | { |
| 2712 | return mmc_card_readonly(card) || |
| 2713 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 2714 | } |
| 2715 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2716 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 2717 | struct device *parent, |
| 2718 | sector_t size, |
| 2719 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2720 | const char *subname, |
| 2721 | int area_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | { |
| 2723 | struct mmc_blk_data *md; |
| 2724 | int devidx, ret; |
| 2725 | |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 2726 | again: |
| 2727 | if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL)) |
| 2728 | return ERR_PTR(-ENOMEM); |
| 2729 | |
| 2730 | spin_lock(&mmc_blk_lock); |
| 2731 | ret = ida_get_new(&mmc_blk_ida, &devidx); |
| 2732 | spin_unlock(&mmc_blk_lock); |
| 2733 | |
| 2734 | if (ret == -EAGAIN) |
| 2735 | goto again; |
| 2736 | else if (ret) |
| 2737 | return ERR_PTR(ret); |
| 2738 | |
| 2739 | if (devidx >= max_devices) { |
| 2740 | ret = -ENOSPC; |
| 2741 | goto out; |
| 2742 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 2744 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2745 | if (!md) { |
| 2746 | ret = -ENOMEM; |
| 2747 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2749 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2750 | md->area_type = area_type; |
| 2751 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2752 | /* |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2753 | * Set the read-only status based on the supported commands |
| 2754 | * and the write protect switch. |
| 2755 | */ |
| 2756 | md->read_only = mmc_blk_readonly(card); |
| 2757 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2758 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2759 | if (md->disk == NULL) { |
| 2760 | ret = -ENOMEM; |
| 2761 | goto err_kfree; |
| 2762 | } |
| 2763 | |
| 2764 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2765 | INIT_LIST_HEAD(&md->part); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2766 | md->usage = 1; |
| 2767 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 2768 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2769 | if (ret) |
| 2770 | goto err_putdisk; |
| 2771 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2772 | md->queue.data = md; |
| 2773 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2774 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2775 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2776 | md->disk->fops = &mmc_bdops; |
| 2777 | md->disk->private_data = md; |
| 2778 | md->disk->queue = md->queue.queue; |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 2779 | md->parent = parent; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2780 | set_disk_ro(md->disk, md->read_only || default_ro); |
Colin Cross | 382c55f | 2015-10-22 10:00:41 -0700 | [diff] [blame] | 2781 | md->disk->flags = GENHD_FL_EXT_DEVT; |
Ulf Hansson | f5b4d71 | 2014-09-03 11:02:23 +0200 | [diff] [blame] | 2782 | 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] | 2783 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2784 | |
| 2785 | /* |
| 2786 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 2787 | * |
| 2788 | * - be set for removable media with permanent block devices |
| 2789 | * - be unset for removable block devices with permanent media |
| 2790 | * |
| 2791 | * Since MMC block devices clearly fall under the second |
| 2792 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 2793 | * should use the block device creation/destruction hotplug |
| 2794 | * messages to tell when the card is present. |
| 2795 | */ |
| 2796 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2797 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
Ulf Hansson | 9aaf343 | 2016-04-06 16:12:08 +0200 | [diff] [blame] | 2798 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2799 | |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2800 | if (mmc_card_mmc(card)) |
| 2801 | blk_queue_logical_block_size(md->queue.queue, |
| 2802 | card->ext_csd.data_sector_size); |
| 2803 | else |
| 2804 | blk_queue_logical_block_size(md->queue.queue, 512); |
| 2805 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2806 | set_capacity(md->disk, size); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2807 | |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2808 | if (mmc_host_cmd23(card->host)) { |
Daniel Glöckner | 0ed50ab | 2016-08-30 14:17:30 +0200 | [diff] [blame] | 2809 | if ((mmc_card_mmc(card) && |
| 2810 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2811 | (mmc_card_sd(card) && |
| 2812 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) |
| 2813 | md->flags |= MMC_BLK_CMD23; |
| 2814 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2815 | |
| 2816 | if (mmc_card_mmc(card) && |
| 2817 | md->flags & MMC_BLK_CMD23 && |
| 2818 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
| 2819 | card->ext_csd.rel_sectors)) { |
| 2820 | md->flags |= MMC_BLK_REL_WR; |
Jens Axboe | e9d5c74 | 2016-03-30 10:17:20 -0600 | [diff] [blame] | 2821 | blk_queue_write_cache(md->queue.queue, true, true); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2822 | } |
| 2823 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2824 | if (mmc_card_mmc(card) && |
| 2825 | (area_type == MMC_BLK_DATA_AREA_MAIN) && |
| 2826 | (md->flags & MMC_BLK_CMD23) && |
| 2827 | card->ext_csd.packed_event_en) { |
| 2828 | if (!mmc_packed_init(&md->queue, card)) |
| 2829 | md->flags |= MMC_BLK_PACKED_CMD; |
| 2830 | } |
| 2831 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2833 | |
| 2834 | err_putdisk: |
| 2835 | put_disk(md->disk); |
| 2836 | err_kfree: |
| 2837 | kfree(md); |
| 2838 | out: |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 2839 | spin_lock(&mmc_blk_lock); |
| 2840 | ida_remove(&mmc_blk_ida, devidx); |
| 2841 | spin_unlock(&mmc_blk_lock); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2842 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | } |
| 2844 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2845 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 2846 | { |
| 2847 | sector_t size; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2848 | |
| 2849 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 2850 | /* |
| 2851 | * The EXT_CSD sector count is in number or 512 byte |
| 2852 | * sectors. |
| 2853 | */ |
| 2854 | size = card->ext_csd.sectors; |
| 2855 | } else { |
| 2856 | /* |
| 2857 | * The CSD capacity field is in units of read_blkbits. |
| 2858 | * set_capacity takes units of 512 bytes. |
| 2859 | */ |
Kuninori Morimoto | 087de9e | 2015-05-11 07:35:28 +0000 | [diff] [blame] | 2860 | size = (typeof(sector_t))card->csd.capacity |
| 2861 | << (card->csd.read_blkbits - 9); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2862 | } |
| 2863 | |
Tobias Klauser | 7a30f2a | 2015-01-21 15:56:44 +0100 | [diff] [blame] | 2864 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2865 | MMC_BLK_DATA_AREA_MAIN); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 2869 | struct mmc_blk_data *md, |
| 2870 | unsigned int part_type, |
| 2871 | sector_t size, |
| 2872 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2873 | const char *subname, |
| 2874 | int area_type) |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2875 | { |
| 2876 | char cap_str[10]; |
| 2877 | struct mmc_blk_data *part_md; |
| 2878 | |
| 2879 | 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] | 2880 | subname, area_type); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2881 | if (IS_ERR(part_md)) |
| 2882 | return PTR_ERR(part_md); |
| 2883 | part_md->part_type = part_type; |
| 2884 | list_add(&part_md->part, &md->part); |
| 2885 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 2886 | 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] | 2887 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2888 | pr_info("%s: %s %s partition %u %s\n", |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2889 | part_md->disk->disk_name, mmc_card_id(card), |
| 2890 | mmc_card_name(card), part_md->part_type, cap_str); |
| 2891 | return 0; |
| 2892 | } |
| 2893 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2894 | /* MMC Physical partitions consist of two boot partitions and |
| 2895 | * up to four general purpose partitions. |
| 2896 | * For each partition enabled in EXT_CSD a block device will be allocatedi |
| 2897 | * to provide access to the partition. |
| 2898 | */ |
| 2899 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2900 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 2901 | { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2902 | int idx, ret = 0; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2903 | |
| 2904 | if (!mmc_card_mmc(card)) |
| 2905 | return 0; |
| 2906 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2907 | for (idx = 0; idx < card->nr_parts; idx++) { |
| 2908 | if (card->part[idx].size) { |
| 2909 | ret = mmc_blk_alloc_part(card, md, |
| 2910 | card->part[idx].part_cfg, |
| 2911 | card->part[idx].size >> 9, |
| 2912 | card->part[idx].force_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2913 | card->part[idx].name, |
| 2914 | card->part[idx].area_type); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2915 | if (ret) |
| 2916 | return ret; |
| 2917 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2918 | } |
| 2919 | |
| 2920 | return ret; |
| 2921 | } |
| 2922 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2923 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 2924 | { |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2925 | struct mmc_card *card; |
| 2926 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2927 | if (md) { |
Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 2928 | /* |
| 2929 | * Flush remaining requests and free queues. It |
| 2930 | * is freeing the queue that stops new requests |
| 2931 | * from being accepted. |
| 2932 | */ |
Franck Jullien | 8efb83a | 2013-07-24 15:17:48 +0200 | [diff] [blame] | 2933 | card = md->queue.card; |
Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 2934 | mmc_cleanup_queue(&md->queue); |
| 2935 | if (md->flags & MMC_BLK_PACKED_CMD) |
| 2936 | mmc_packed_clean(&md->queue); |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2937 | device_remove_file(disk_to_dev(md->disk), |
| 2938 | &md->num_wr_reqs_to_start_packing); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2939 | if (md->disk->flags & GENHD_FL_UP) { |
| 2940 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2941 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 2942 | card->ext_csd.boot_ro_lockable) |
| 2943 | device_remove_file(disk_to_dev(md->disk), |
| 2944 | &md->power_ro_lock); |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2945 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 2946 | device_remove_file(disk_to_dev(md->disk), |
| 2947 | &dev_attr_max_write_speed); |
| 2948 | device_remove_file(disk_to_dev(md->disk), |
| 2949 | &dev_attr_max_read_speed); |
| 2950 | device_remove_file(disk_to_dev(md->disk), |
| 2951 | &dev_attr_cache_size); |
| 2952 | #endif |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2953 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2954 | del_gendisk(md->disk); |
| 2955 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2956 | mmc_blk_put(md); |
| 2957 | } |
| 2958 | } |
| 2959 | |
| 2960 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 2961 | struct mmc_blk_data *md) |
| 2962 | { |
| 2963 | struct list_head *pos, *q; |
| 2964 | struct mmc_blk_data *part_md; |
| 2965 | |
| 2966 | list_for_each_safe(pos, q, &md->part) { |
| 2967 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 2968 | list_del(pos); |
| 2969 | mmc_blk_remove_req(part_md); |
| 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 2974 | { |
| 2975 | int ret; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2976 | struct mmc_card *card = md->queue.card; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2977 | |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 2978 | device_add_disk(md->parent, md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2979 | md->force_ro.show = force_ro_show; |
| 2980 | md->force_ro.store = force_ro_store; |
Rabin Vincent | 641c318 | 2011-04-23 20:52:58 +0530 | [diff] [blame] | 2981 | sysfs_attr_init(&md->force_ro.attr); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2982 | md->force_ro.attr.name = "force_ro"; |
| 2983 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 2984 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 2985 | if (ret) |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2986 | goto force_ro_fail; |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2987 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 2988 | atomic_set(&md->queue.max_write_speed, max_write_speed); |
| 2989 | ret = device_create_file(disk_to_dev(md->disk), |
| 2990 | &dev_attr_max_write_speed); |
| 2991 | if (ret) |
| 2992 | goto max_write_speed_fail; |
| 2993 | atomic_set(&md->queue.max_read_speed, max_read_speed); |
| 2994 | ret = device_create_file(disk_to_dev(md->disk), |
| 2995 | &dev_attr_max_read_speed); |
| 2996 | if (ret) |
| 2997 | goto max_read_speed_fail; |
| 2998 | atomic_set(&md->queue.cache_size, cache_size); |
| 2999 | atomic_long_set(&md->queue.cache_used, 0); |
| 3000 | md->queue.cache_jiffies = jiffies; |
| 3001 | ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size); |
| 3002 | if (ret) |
| 3003 | goto cache_size_fail; |
| 3004 | #endif |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3005 | |
| 3006 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 3007 | card->ext_csd.boot_ro_lockable) { |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 3008 | umode_t mode; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3009 | |
| 3010 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) |
| 3011 | mode = S_IRUGO; |
| 3012 | else |
| 3013 | mode = S_IRUGO | S_IWUSR; |
| 3014 | |
| 3015 | md->power_ro_lock.show = power_ro_lock_show; |
| 3016 | md->power_ro_lock.store = power_ro_lock_store; |
Rabin Vincent | 00d9ac0 | 2012-02-01 16:31:56 +0100 | [diff] [blame] | 3017 | sysfs_attr_init(&md->power_ro_lock.attr); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3018 | md->power_ro_lock.attr.mode = mode; |
| 3019 | md->power_ro_lock.attr.name = |
| 3020 | "ro_lock_until_next_power_on"; |
| 3021 | ret = device_create_file(disk_to_dev(md->disk), |
| 3022 | &md->power_ro_lock); |
| 3023 | if (ret) |
| 3024 | goto power_ro_lock_fail; |
| 3025 | } |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 3026 | |
| 3027 | md->num_wr_reqs_to_start_packing.show = |
| 3028 | num_wr_reqs_to_start_packing_show; |
| 3029 | md->num_wr_reqs_to_start_packing.store = |
| 3030 | num_wr_reqs_to_start_packing_store; |
| 3031 | sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr); |
| 3032 | md->num_wr_reqs_to_start_packing.attr.name = |
| 3033 | "num_wr_reqs_to_start_packing"; |
| 3034 | md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR; |
| 3035 | ret = device_create_file(disk_to_dev(md->disk), |
| 3036 | &md->num_wr_reqs_to_start_packing); |
| 3037 | if (ret) |
Maya Erez | 1702240 | 2014-12-04 00:15:42 +0200 | [diff] [blame] | 3038 | goto num_wr_reqs_to_start_packing_fail; |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 3039 | |
Maya Erez | 5a8dae1 | 2014-12-04 15:13:59 +0200 | [diff] [blame^] | 3040 | md->no_pack_for_random.show = no_pack_for_random_show; |
| 3041 | md->no_pack_for_random.store = no_pack_for_random_store; |
| 3042 | sysfs_attr_init(&md->no_pack_for_random.attr); |
| 3043 | md->no_pack_for_random.attr.name = "no_pack_for_random"; |
| 3044 | md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR; |
| 3045 | ret = device_create_file(disk_to_dev(md->disk), |
| 3046 | &md->no_pack_for_random); |
| 3047 | if (ret) |
| 3048 | goto no_pack_for_random_fails; |
| 3049 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3050 | return ret; |
| 3051 | |
Maya Erez | 5a8dae1 | 2014-12-04 15:13:59 +0200 | [diff] [blame^] | 3052 | no_pack_for_random_fails: |
| 3053 | device_remove_file(disk_to_dev(md->disk), |
| 3054 | &md->num_wr_reqs_to_start_packing); |
Maya Erez | 1702240 | 2014-12-04 00:15:42 +0200 | [diff] [blame] | 3055 | num_wr_reqs_to_start_packing_fail: |
| 3056 | device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3057 | power_ro_lock_fail: |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 3058 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 3059 | device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size); |
| 3060 | cache_size_fail: |
| 3061 | device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed); |
| 3062 | max_read_speed_fail: |
| 3063 | device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed); |
| 3064 | max_write_speed_fail: |
| 3065 | #endif |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3066 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 3067 | force_ro_fail: |
| 3068 | del_gendisk(md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3069 | |
| 3070 | return ret; |
| 3071 | } |
| 3072 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 3073 | static const struct mmc_fixup blk_fixups[] = |
| 3074 | { |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3075 | MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3076 | MMC_QUIRK_INAND_CMD38), |
| 3077 | MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3078 | MMC_QUIRK_INAND_CMD38), |
| 3079 | MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3080 | MMC_QUIRK_INAND_CMD38), |
| 3081 | MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3082 | MMC_QUIRK_INAND_CMD38), |
| 3083 | MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3084 | MMC_QUIRK_INAND_CMD38), |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3085 | |
| 3086 | /* |
| 3087 | * Some MMC cards experience performance degradation with CMD23 |
| 3088 | * instead of CMD12-bounded multiblock transfers. For now we'll |
| 3089 | * black list what's bad... |
| 3090 | * - Certain Toshiba cards. |
| 3091 | * |
| 3092 | * N.B. This doesn't affect SD cards. |
| 3093 | */ |
Yangbo Lu | 7d70d47 | 2015-07-10 11:44:03 +0800 | [diff] [blame] | 3094 | MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, |
| 3095 | MMC_QUIRK_BLK_NO_CMD23), |
| 3096 | MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, |
| 3097 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3098 | MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3099 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3100 | MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3101 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3102 | MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3103 | MMC_QUIRK_BLK_NO_CMD23), |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 3104 | |
| 3105 | /* |
Matt Gumbel | 32ecd32 | 2016-05-20 10:33:46 +0300 | [diff] [blame] | 3106 | * 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] | 3107 | */ |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3108 | 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] | 3109 | MMC_QUIRK_LONG_READ_TIME), |
Matt Gumbel | 32ecd32 | 2016-05-20 10:33:46 +0300 | [diff] [blame] | 3110 | MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
| 3111 | MMC_QUIRK_LONG_READ_TIME), |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 3112 | |
Ian Chen | 3550ccd | 2012-08-29 15:05:36 +0900 | [diff] [blame] | 3113 | /* |
Guoping Yu | 3c984a9 | 2014-08-06 12:44:55 +0800 | [diff] [blame] | 3114 | * Some Samsung MMC cards need longer data read timeout than |
| 3115 | * indicated in CSD. |
| 3116 | */ |
| 3117 | MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc, |
| 3118 | MMC_QUIRK_LONG_READ_TIME), |
| 3119 | |
| 3120 | /* |
Ian Chen | 3550ccd | 2012-08-29 15:05:36 +0900 | [diff] [blame] | 3121 | * On these Samsung MoviNAND parts, performing secure erase or |
| 3122 | * secure trim can result in unrecoverable corruption due to a |
| 3123 | * firmware bug. |
| 3124 | */ |
| 3125 | MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3126 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3127 | MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3128 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3129 | MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3130 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3131 | MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3132 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3133 | MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3134 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3135 | MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3136 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3137 | MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3138 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3139 | MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3140 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3141 | |
Shawn Lin | b5b4ff0 | 2015-08-12 13:08:32 +0800 | [diff] [blame] | 3142 | /* |
| 3143 | * On Some Kingston eMMCs, performing trim can result in |
| 3144 | * unrecoverable data conrruption occasionally due to a firmware bug. |
| 3145 | */ |
| 3146 | MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, |
| 3147 | MMC_QUIRK_TRIM_BROKEN), |
| 3148 | MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, |
| 3149 | MMC_QUIRK_TRIM_BROKEN), |
| 3150 | |
Pratibhasagar V | 8d664e3 | 2014-12-03 18:26:42 +0200 | [diff] [blame] | 3151 | /* Some INAND MCP devices advertise incorrect timeout values */ |
| 3152 | MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc, |
| 3153 | MMC_QUIRK_INAND_DATA_TIMEOUT), |
| 3154 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 3155 | END_FIXUP |
| 3156 | }; |
| 3157 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3158 | static int mmc_blk_probe(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3160 | struct mmc_blk_data *md, *part_md; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3161 | char cap_str[10]; |
| 3162 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 3163 | /* |
| 3164 | * Check that the card supports the command class(es) we need. |
| 3165 | */ |
| 3166 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | return -ENODEV; |
| 3168 | |
Lukas Czerner | 5204d00 | 2014-06-18 13:18:07 +0200 | [diff] [blame] | 3169 | mmc_fixup_device(card, blk_fixups); |
| 3170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3171 | md = mmc_blk_alloc(card); |
| 3172 | if (IS_ERR(md)) |
| 3173 | return PTR_ERR(md); |
| 3174 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 3175 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3176 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3177 | pr_info("%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3179 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3180 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3181 | if (mmc_blk_alloc_parts(card, md)) |
| 3182 | goto out; |
| 3183 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3184 | dev_set_drvdata(&card->dev, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 3185 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3186 | if (mmc_add_disk(md)) |
| 3187 | goto out; |
| 3188 | |
| 3189 | list_for_each_entry(part_md, &md->part, part) { |
| 3190 | if (mmc_add_disk(part_md)) |
| 3191 | goto out; |
| 3192 | } |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3193 | |
| 3194 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); |
| 3195 | pm_runtime_use_autosuspend(&card->dev); |
| 3196 | |
| 3197 | /* |
| 3198 | * Don't enable runtime PM for SD-combo cards here. Leave that |
| 3199 | * decision to be taken during the SDIO init sequence instead. |
| 3200 | */ |
| 3201 | if (card->type != MMC_TYPE_SD_COMBO) { |
| 3202 | pm_runtime_set_active(&card->dev); |
| 3203 | pm_runtime_enable(&card->dev); |
| 3204 | } |
| 3205 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | return 0; |
| 3207 | |
| 3208 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3209 | mmc_blk_remove_parts(card, md); |
| 3210 | mmc_blk_remove_req(md); |
Ulf Hansson | 5865f28 | 2012-03-22 11:47:26 +0100 | [diff] [blame] | 3211 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | } |
| 3213 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3214 | static void mmc_blk_remove(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3216 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3217 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3218 | mmc_blk_remove_parts(card, md); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3219 | pm_runtime_get_sync(&card->dev); |
Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 3220 | mmc_claim_host(card->host); |
| 3221 | mmc_blk_part_switch(card, md); |
| 3222 | mmc_release_host(card->host); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3223 | if (card->type != MMC_TYPE_SD_COMBO) |
| 3224 | pm_runtime_disable(&card->dev); |
| 3225 | pm_runtime_put_noidle(&card->dev); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3226 | mmc_blk_remove_req(md); |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3227 | dev_set_drvdata(&card->dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | } |
| 3229 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3230 | static int _mmc_blk_suspend(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3232 | struct mmc_blk_data *part_md; |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3233 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3234 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | |
| 3236 | if (md) { |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3237 | rc = mmc_queue_suspend(&md->queue); |
| 3238 | if (rc) |
| 3239 | goto out; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3240 | list_for_each_entry(part_md, &md->part, part) { |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3241 | rc = mmc_queue_suspend(&part_md->queue); |
| 3242 | if (rc) |
| 3243 | goto out_resume; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3244 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | } |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3246 | goto out; |
| 3247 | |
| 3248 | out_resume: |
| 3249 | mmc_queue_resume(&md->queue); |
| 3250 | list_for_each_entry(part_md, &md->part, part) { |
| 3251 | mmc_queue_resume(&part_md->queue); |
| 3252 | } |
| 3253 | out: |
| 3254 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3255 | } |
| 3256 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3257 | static void mmc_blk_shutdown(struct mmc_card *card) |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3258 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3259 | _mmc_blk_suspend(card); |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3260 | } |
| 3261 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 3262 | #ifdef CONFIG_PM_SLEEP |
| 3263 | static int mmc_blk_suspend(struct device *dev) |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3264 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3265 | struct mmc_card *card = mmc_dev_to_card(dev); |
| 3266 | |
| 3267 | return _mmc_blk_suspend(card); |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3268 | } |
| 3269 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 3270 | static int mmc_blk_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3272 | struct mmc_blk_data *part_md; |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 3273 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | |
| 3275 | if (md) { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3276 | /* |
| 3277 | * Resume involves the card going into idle state, |
| 3278 | * so current partition is always the main one. |
| 3279 | */ |
| 3280 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3281 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3282 | list_for_each_entry(part_md, &md->part, part) { |
| 3283 | mmc_queue_resume(&part_md->queue); |
| 3284 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | } |
| 3286 | return 0; |
| 3287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3288 | #endif |
| 3289 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 3290 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
| 3291 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3292 | static struct mmc_driver mmc_driver = { |
| 3293 | .drv = { |
| 3294 | .name = "mmcblk", |
| 3295 | .pm = &mmc_blk_pm_ops, |
| 3296 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3297 | .probe = mmc_blk_probe, |
| 3298 | .remove = mmc_blk_remove, |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3299 | .shutdown = mmc_blk_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3300 | }; |
| 3301 | |
| 3302 | static int __init mmc_blk_init(void) |
| 3303 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3304 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3305 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 3306 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 3307 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 3308 | |
Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 3309 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 3310 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 3311 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 3312 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3313 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3315 | res = mmc_register_driver(&mmc_driver); |
| 3316 | if (res) |
| 3317 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3318 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3319 | return 0; |
| 3320 | out2: |
| 3321 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3322 | out: |
| 3323 | return res; |
| 3324 | } |
| 3325 | |
| 3326 | static void __exit mmc_blk_exit(void) |
| 3327 | { |
| 3328 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 3329 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | module_init(mmc_blk_init); |
| 3333 | module_exit(mmc_blk_exit); |
| 3334 | |
| 3335 | MODULE_LICENSE("GPL"); |
| 3336 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 3337 | |