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