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