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