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 */ |
Talel Shenhar | 0821fe85 | 2015-01-28 14:44:57 +0200 | [diff] [blame] | 1510 | pr_err_ratelimited( |
| 1511 | "%s: response CRC error sending %s command, card status %#x\n", |
| 1512 | req->rq_disk->disk_name, |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1513 | name, status); |
| 1514 | return ERR_RETRY; |
| 1515 | |
| 1516 | case -ETIMEDOUT: |
Talel Shenhar | 0821fe85 | 2015-01-28 14:44:57 +0200 | [diff] [blame] | 1517 | pr_err_ratelimited( |
| 1518 | "%s: timed out sending %s command, card status %#x\n", |
| 1519 | req->rq_disk->disk_name, name, status); |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1520 | |
| 1521 | /* If the status cmd initially failed, retry the r/w cmd */ |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1522 | if (!status_valid) { |
Talel Shenhar | 0821fe85 | 2015-01-28 14:44:57 +0200 | [diff] [blame] | 1523 | pr_err_ratelimited("%s: status not valid, retrying timeout\n", |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1524 | req->rq_disk->disk_name); |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1525 | return ERR_RETRY; |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1526 | } |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1527 | |
| 1528 | /* |
| 1529 | * If it was a r/w cmd crc error, or illegal command |
| 1530 | * (eg, issued in wrong state) then retry - we should |
| 1531 | * have corrected the state problem above. |
| 1532 | */ |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1533 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { |
Talel Shenhar | 0821fe85 | 2015-01-28 14:44:57 +0200 | [diff] [blame] | 1534 | pr_err_ratelimited( |
| 1535 | "%s: command error, retrying timeout\n", |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1536 | req->rq_disk->disk_name); |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1537 | return ERR_RETRY; |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1538 | } |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1539 | |
| 1540 | /* Otherwise abort the command */ |
Talel Shenhar | 0821fe85 | 2015-01-28 14:44:57 +0200 | [diff] [blame] | 1541 | pr_err_ratelimited( |
| 1542 | "%s: not retrying timeout\n", |
| 1543 | req->rq_disk->disk_name); |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1544 | return ERR_ABORT; |
| 1545 | |
| 1546 | default: |
| 1547 | /* We don't understand the error code the driver gave us */ |
Talel Shenhar | 0821fe85 | 2015-01-28 14:44:57 +0200 | [diff] [blame] | 1548 | pr_err_ratelimited( |
| 1549 | "%s: unknown error %d sending read/write command, card status %#x\n", |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1550 | req->rq_disk->disk_name, error, status); |
| 1551 | return ERR_ABORT; |
| 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | /* |
| 1556 | * Initial r/w and stop cmd error recovery. |
| 1557 | * We don't know whether the card received the r/w cmd or not, so try to |
| 1558 | * restore things back to a sane state. Essentially, we do this as follows: |
| 1559 | * - Obtain card status. If the first attempt to obtain card status fails, |
| 1560 | * the status word will reflect the failed status cmd, not the failed |
| 1561 | * r/w cmd. If we fail to obtain card status, it suggests we can no |
| 1562 | * longer communicate with the card. |
| 1563 | * - Check the card state. If the card received the cmd but there was a |
| 1564 | * transient problem with the response, it might still be in a data transfer |
| 1565 | * mode. Try to send it a stop command. If this fails, we can't recover. |
| 1566 | * - If the r/w cmd failed due to a response CRC error, it was probably |
| 1567 | * transient, so retry the cmd. |
| 1568 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. |
| 1569 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or |
| 1570 | * illegal cmd, retry. |
| 1571 | * Otherwise we don't understand what happened, so abort. |
| 1572 | */ |
| 1573 | 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] | 1574 | 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] | 1575 | { |
| 1576 | bool prev_cmd_status_valid = true; |
| 1577 | u32 status, stop_status = 0; |
| 1578 | int err, retry; |
| 1579 | |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1580 | if (mmc_card_removed(card)) |
| 1581 | return ERR_NOMEDIUM; |
| 1582 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1583 | /* |
| 1584 | * Try to get card status which indicates both the card state |
| 1585 | * and why there was no response. If the first attempt fails, |
| 1586 | * we can't be sure the returned status is for the r/w command. |
| 1587 | */ |
| 1588 | for (retry = 2; retry >= 0; retry--) { |
| 1589 | err = get_card_status(card, &status, 0); |
| 1590 | if (!err) |
| 1591 | break; |
| 1592 | |
Adrian Hunter | 6f398ad | 2015-05-07 13:10:23 +0300 | [diff] [blame] | 1593 | /* Re-tune if needed */ |
| 1594 | mmc_retune_recheck(card->host); |
| 1595 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1596 | prev_cmd_status_valid = false; |
| 1597 | pr_err("%s: error %d sending status command, %sing\n", |
| 1598 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); |
| 1599 | } |
| 1600 | |
| 1601 | /* We couldn't get a response from the card. Give up. */ |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1602 | if (err) { |
| 1603 | /* Check if the card is removed */ |
| 1604 | if (mmc_detect_card_removed(card->host)) |
| 1605 | return ERR_NOMEDIUM; |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1606 | return ERR_ABORT; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1607 | } |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1608 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1609 | /* Flag ECC errors */ |
| 1610 | if ((status & R1_CARD_ECC_FAILED) || |
| 1611 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || |
| 1612 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) |
| 1613 | *ecc_err = 1; |
| 1614 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1615 | /* Flag General errors */ |
| 1616 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) |
| 1617 | if ((status & R1_ERROR) || |
| 1618 | (brq->stop.resp[0] & R1_ERROR)) { |
| 1619 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", |
| 1620 | req->rq_disk->disk_name, __func__, |
| 1621 | brq->stop.resp[0], status); |
| 1622 | *gen_err = 1; |
| 1623 | } |
| 1624 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1625 | /* |
| 1626 | * Check the current card state. If it is in some data transfer |
| 1627 | * mode, tell it to stop (and hopefully transition back to TRAN.) |
| 1628 | */ |
| 1629 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || |
| 1630 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1631 | err = send_stop(card, |
| 1632 | DIV_ROUND_UP(brq->data.timeout_ns, 1000000), |
| 1633 | req, gen_err, &stop_status); |
| 1634 | if (err) { |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1635 | pr_err("%s: error %d sending stop command\n", |
| 1636 | req->rq_disk->disk_name, err); |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1637 | /* |
| 1638 | * If the stop cmd also timed out, the card is probably |
| 1639 | * not present, so abort. Other errors are bad news too. |
| 1640 | */ |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1641 | return ERR_ABORT; |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1642 | } |
| 1643 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1644 | if (stop_status & R1_CARD_ECC_FAILED) |
| 1645 | *ecc_err = 1; |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | /* Check for set block count errors */ |
| 1649 | if (brq->sbc.error) |
| 1650 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, |
| 1651 | prev_cmd_status_valid, status); |
| 1652 | |
| 1653 | /* Check for r/w command errors */ |
| 1654 | if (brq->cmd.error) |
| 1655 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, |
| 1656 | prev_cmd_status_valid, status); |
| 1657 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1658 | /* Data errors */ |
| 1659 | if (!brq->stop.error) |
| 1660 | return ERR_CONTINUE; |
| 1661 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1662 | /* Now for stop errors. These aren't fatal to the transfer. */ |
Johan Rudholm | 5e1344e | 2014-09-17 09:50:42 +0200 | [diff] [blame] | 1663 | 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] | 1664 | req->rq_disk->disk_name, brq->stop.error, |
| 1665 | brq->cmd.resp[0], status); |
| 1666 | |
| 1667 | /* |
| 1668 | * Subsitute in our own stop status as this will give the error |
| 1669 | * state which happened during the execution of the r/w command. |
| 1670 | */ |
| 1671 | if (stop_status) { |
| 1672 | brq->stop.resp[0] = stop_status; |
| 1673 | brq->stop.error = 0; |
| 1674 | } |
| 1675 | return ERR_CONTINUE; |
| 1676 | } |
| 1677 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1678 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
| 1679 | int type) |
| 1680 | { |
| 1681 | int err; |
| 1682 | |
| 1683 | if (md->reset_done & type) |
| 1684 | return -EEXIST; |
| 1685 | |
| 1686 | md->reset_done |= type; |
| 1687 | err = mmc_hw_reset(host); |
Sahitya Tummala | 943f51b | 2014-05-30 09:22:35 +0530 | [diff] [blame] | 1688 | if (err && err != -EOPNOTSUPP) { |
| 1689 | /* We failed to reset so we need to abort the request */ |
| 1690 | pr_err("%s: %s: failed to reset %d\n", mmc_hostname(host), |
| 1691 | __func__, err); |
| 1692 | return -ENODEV; |
| 1693 | } |
| 1694 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1695 | /* Ensure we switch back to the correct partition */ |
Sahitya Tummala | 943f51b | 2014-05-30 09:22:35 +0530 | [diff] [blame] | 1696 | if (host->card) { |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 1697 | struct mmc_blk_data *main_md = |
| 1698 | dev_get_drvdata(&host->card->dev); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1699 | int part_err; |
| 1700 | |
| 1701 | main_md->part_curr = main_md->part_type; |
| 1702 | part_err = mmc_blk_part_switch(host->card, md); |
| 1703 | if (part_err) { |
| 1704 | /* |
| 1705 | * We have failed to get back into the correct |
| 1706 | * partition, so we need to abort the whole request. |
| 1707 | */ |
| 1708 | return -ENODEV; |
| 1709 | } |
| 1710 | } |
| 1711 | return err; |
| 1712 | } |
| 1713 | |
| 1714 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) |
| 1715 | { |
| 1716 | md->reset_done &= ~type; |
| 1717 | } |
| 1718 | |
Chuanxiao Dong | 4e93b9a | 2014-08-12 12:01:30 +0800 | [diff] [blame] | 1719 | int mmc_access_rpmb(struct mmc_queue *mq) |
| 1720 | { |
| 1721 | struct mmc_blk_data *md = mq->data; |
| 1722 | /* |
| 1723 | * If this is a RPMB partition access, return ture |
| 1724 | */ |
| 1725 | if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) |
| 1726 | return true; |
| 1727 | |
| 1728 | return false; |
| 1729 | } |
| 1730 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1731 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
| 1732 | { |
| 1733 | struct mmc_blk_data *md = mq->data; |
| 1734 | struct mmc_card *card = md->queue.card; |
| 1735 | unsigned int from, nr, arg; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1736 | int err = 0, type = MMC_BLK_DISCARD; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1737 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1738 | if (!mmc_can_erase(card)) { |
| 1739 | err = -EOPNOTSUPP; |
| 1740 | goto out; |
| 1741 | } |
| 1742 | |
| 1743 | from = blk_rq_pos(req); |
| 1744 | nr = blk_rq_sectors(req); |
| 1745 | |
Kyungmin Park | b3bf915 | 2011-10-18 09:34:04 +0900 | [diff] [blame] | 1746 | if (mmc_can_discard(card)) |
| 1747 | arg = MMC_DISCARD_ARG; |
| 1748 | else if (mmc_can_trim(card)) |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1749 | arg = MMC_TRIM_ARG; |
| 1750 | else |
| 1751 | arg = MMC_ERASE_ARG; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1752 | retry: |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1753 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1754 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1755 | INAND_CMD38_ARG_EXT_CSD, |
| 1756 | arg == MMC_TRIM_ARG ? |
| 1757 | INAND_CMD38_ARG_TRIM : |
| 1758 | INAND_CMD38_ARG_ERASE, |
| 1759 | 0); |
| 1760 | if (err) |
| 1761 | goto out; |
| 1762 | } |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1763 | err = mmc_erase(card, from, nr, arg); |
| 1764 | out: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1765 | if (err == -EIO && !mmc_blk_reset(md, card->host, type)) |
| 1766 | goto retry; |
| 1767 | if (!err) |
| 1768 | mmc_blk_reset_success(md, type); |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1769 | blk_end_request(req, err, blk_rq_bytes(req)); |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1770 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1771 | return err ? 0 : 1; |
| 1772 | } |
| 1773 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1774 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
| 1775 | struct request *req) |
| 1776 | { |
| 1777 | struct mmc_blk_data *md = mq->data; |
| 1778 | struct mmc_card *card = md->queue.card; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1779 | unsigned int from, nr, arg; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1780 | int err = 0, type = MMC_BLK_SECDISCARD; |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1781 | |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1782 | if (!(mmc_can_secure_erase_trim(card))) { |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1783 | err = -EOPNOTSUPP; |
| 1784 | goto out; |
| 1785 | } |
| 1786 | |
| 1787 | from = blk_rq_pos(req); |
| 1788 | nr = blk_rq_sectors(req); |
| 1789 | |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1790 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
| 1791 | arg = MMC_SECURE_TRIM1_ARG; |
| 1792 | else |
| 1793 | arg = MMC_SECURE_ERASE_ARG; |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1794 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1795 | retry: |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1796 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1797 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1798 | INAND_CMD38_ARG_EXT_CSD, |
| 1799 | arg == MMC_SECURE_TRIM1_ARG ? |
| 1800 | INAND_CMD38_ARG_SECTRIM1 : |
| 1801 | INAND_CMD38_ARG_SECERASE, |
| 1802 | 0); |
| 1803 | if (err) |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1804 | goto out_retry; |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1805 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1806 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1807 | err = mmc_erase(card, from, nr, arg); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1808 | if (err == -EIO) |
| 1809 | goto out_retry; |
| 1810 | if (err) |
| 1811 | goto out; |
| 1812 | |
| 1813 | if (arg == MMC_SECURE_TRIM1_ARG) { |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1814 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1815 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1816 | INAND_CMD38_ARG_EXT_CSD, |
| 1817 | INAND_CMD38_ARG_SECTRIM2, |
| 1818 | 0); |
| 1819 | if (err) |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1820 | goto out_retry; |
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 | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1823 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1824 | if (err == -EIO) |
| 1825 | goto out_retry; |
| 1826 | if (err) |
| 1827 | goto out; |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1828 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1829 | |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1830 | out_retry: |
| 1831 | if (err && !mmc_blk_reset(md, card->host, type)) |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1832 | goto retry; |
| 1833 | if (!err) |
| 1834 | mmc_blk_reset_success(md, type); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1835 | out: |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1836 | blk_end_request(req, err, blk_rq_bytes(req)); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1837 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1838 | return err ? 0 : 1; |
| 1839 | } |
| 1840 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1841 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
| 1842 | { |
| 1843 | struct mmc_blk_data *md = mq->data; |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1844 | struct mmc_card *card = md->queue.card; |
| 1845 | int ret = 0; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1846 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1847 | ret = mmc_flush_cache(card); |
Talel Shenhar | 8a8e3b4 | 2015-02-11 12:58:16 +0200 | [diff] [blame] | 1848 | if (ret == -ENODEV) { |
| 1849 | pr_err("%s: %s: restart mmc card", |
| 1850 | req->rq_disk->disk_name, __func__); |
| 1851 | if (mmc_blk_reset(md, card->host, MMC_BLK_FLUSH)) |
| 1852 | pr_err("%s: %s: fail to restart mmc", |
| 1853 | req->rq_disk->disk_name, __func__); |
| 1854 | else |
| 1855 | mmc_blk_reset_success(md, MMC_BLK_FLUSH); |
| 1856 | } |
| 1857 | |
| 1858 | if (ret) { |
| 1859 | pr_err("%s: %s: notify flush error to upper layers", |
| 1860 | req->rq_disk->disk_name, __func__); |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1861 | ret = -EIO; |
Talel Shenhar | 8a8e3b4 | 2015-02-11 12:58:16 +0200 | [diff] [blame] | 1862 | } |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1863 | |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 1864 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 1865 | else if (atomic_read(&mq->cache_size)) { |
| 1866 | long used = mmc_blk_cache_used(mq, jiffies); |
| 1867 | |
| 1868 | if (used) { |
| 1869 | int speed = atomic_read(&mq->max_write_speed); |
| 1870 | |
| 1871 | if (speed_valid(speed)) { |
| 1872 | unsigned long msecs = jiffies_to_msecs( |
| 1873 | size_and_speed_to_jiffies( |
| 1874 | used, speed)); |
| 1875 | if (msecs) |
| 1876 | msleep(msecs); |
| 1877 | } |
| 1878 | } |
| 1879 | } |
| 1880 | #endif |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 1881 | blk_end_request_all(req, ret); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1882 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1883 | return ret ? 0 : 1; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1884 | } |
| 1885 | |
| 1886 | /* |
| 1887 | * Reformat current write as a reliable write, supporting |
| 1888 | * both legacy and the enhanced reliable write MMC cards. |
| 1889 | * In each transfer we'll handle only as much as a single |
| 1890 | * reliable write can handle, thus finish the request in |
| 1891 | * partial completions. |
| 1892 | */ |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1893 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 1894 | struct mmc_card *card, |
| 1895 | struct request *req) |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1896 | { |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1897 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 1898 | /* Legacy mode imposes restrictions on transfers. */ |
| 1899 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) |
| 1900 | brq->data.blocks = 1; |
| 1901 | |
| 1902 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 1903 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 1904 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 1905 | brq->data.blocks = 1; |
| 1906 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1907 | } |
| 1908 | |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1909 | #define CMD_ERRORS \ |
| 1910 | (R1_OUT_OF_RANGE | /* Command argument out of range */ \ |
| 1911 | R1_ADDRESS_ERROR | /* Misaligned address */ \ |
| 1912 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ |
| 1913 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ |
| 1914 | R1_CC_ERROR | /* Card controller error */ \ |
| 1915 | R1_ERROR) /* General/unknown error */ |
| 1916 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1917 | static int mmc_blk_err_check(struct mmc_card *card, |
| 1918 | struct mmc_async_req *areq) |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1919 | { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1920 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, |
| 1921 | mmc_active); |
| 1922 | struct mmc_blk_request *brq = &mq_mrq->brq; |
| 1923 | struct request *req = mq_mrq->req; |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1924 | int need_retune = card->host->need_retune; |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1925 | int ecc_err = 0, gen_err = 0; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1926 | |
| 1927 | /* |
| 1928 | * sbc.error indicates a problem with the set block count |
| 1929 | * command. No data will have been transferred. |
| 1930 | * |
| 1931 | * cmd.error indicates a problem with the r/w command. No |
| 1932 | * data will have been transferred. |
| 1933 | * |
| 1934 | * stop.error indicates a problem with the stop command. Data |
| 1935 | * may have been transferred, or may still be transferring. |
| 1936 | */ |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1937 | if (brq->sbc.error || brq->cmd.error || brq->stop.error || |
| 1938 | brq->data.error) { |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1939 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1940 | case ERR_RETRY: |
| 1941 | return MMC_BLK_RETRY; |
| 1942 | case ERR_ABORT: |
| 1943 | return MMC_BLK_ABORT; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1944 | case ERR_NOMEDIUM: |
| 1945 | return MMC_BLK_NOMEDIUM; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1946 | case ERR_CONTINUE: |
| 1947 | break; |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | /* |
| 1952 | * Check for errors relating to the execution of the |
| 1953 | * initial command - such as address errors. No data |
| 1954 | * has been transferred. |
| 1955 | */ |
| 1956 | if (brq->cmd.resp[0] & CMD_ERRORS) { |
| 1957 | pr_err("%s: r/w command failed, status = %#x\n", |
| 1958 | req->rq_disk->disk_name, brq->cmd.resp[0]); |
| 1959 | return MMC_BLK_ABORT; |
| 1960 | } |
| 1961 | |
| 1962 | /* |
| 1963 | * Everything else is either success, or a data error of some |
| 1964 | * kind. If it was a write, we may have transitioned to |
| 1965 | * program mode, which we have to wait for it to complete. |
| 1966 | */ |
| 1967 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1968 | int err; |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 1969 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1970 | /* Check stop command response */ |
| 1971 | if (brq->stop.resp[0] & R1_ERROR) { |
| 1972 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", |
| 1973 | req->rq_disk->disk_name, __func__, |
| 1974 | brq->stop.resp[0]); |
| 1975 | gen_err = 1; |
| 1976 | } |
| 1977 | |
Ulf Hansson | 95a9129 | 2014-01-29 13:11:27 +0100 | [diff] [blame] | 1978 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, |
| 1979 | &gen_err); |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1980 | if (err) |
| 1981 | return MMC_BLK_CMD_ERR; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1982 | } |
| 1983 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1984 | /* if general error occurs, retry the write operation. */ |
| 1985 | if (gen_err) { |
| 1986 | pr_warn("%s: retrying write for general error\n", |
| 1987 | req->rq_disk->disk_name); |
| 1988 | return MMC_BLK_RETRY; |
| 1989 | } |
| 1990 | |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1991 | if (brq->data.error) { |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1992 | if (need_retune && !brq->retune_retry_done) { |
Russell King | 09faf61 | 2016-01-29 09:44:00 +0000 | [diff] [blame] | 1993 | pr_debug("%s: retrying because a re-tune was needed\n", |
| 1994 | req->rq_disk->disk_name); |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1995 | brq->retune_retry_done = 1; |
| 1996 | return MMC_BLK_RETRY; |
| 1997 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1998 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
| 1999 | req->rq_disk->disk_name, brq->data.error, |
| 2000 | (unsigned)blk_rq_pos(req), |
| 2001 | (unsigned)blk_rq_sectors(req), |
| 2002 | brq->cmd.resp[0], brq->stop.resp[0]); |
| 2003 | |
| 2004 | if (rq_data_dir(req) == READ) { |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2005 | if (ecc_err) |
| 2006 | return MMC_BLK_ECC_ERR; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2007 | return MMC_BLK_DATA_ERR; |
| 2008 | } else { |
| 2009 | return MMC_BLK_CMD_ERR; |
| 2010 | } |
| 2011 | } |
| 2012 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2013 | if (!brq->data.bytes_xfered) |
| 2014 | return MMC_BLK_RETRY; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2015 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2016 | if (mmc_packed_cmd(mq_mrq->cmd_type)) { |
| 2017 | if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered)) |
| 2018 | return MMC_BLK_PARTIAL; |
| 2019 | else |
| 2020 | return MMC_BLK_SUCCESS; |
| 2021 | } |
| 2022 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2023 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) |
| 2024 | return MMC_BLK_PARTIAL; |
| 2025 | |
| 2026 | return MMC_BLK_SUCCESS; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2027 | } |
| 2028 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2029 | static int mmc_blk_packed_err_check(struct mmc_card *card, |
| 2030 | struct mmc_async_req *areq) |
| 2031 | { |
| 2032 | struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req, |
| 2033 | mmc_active); |
| 2034 | struct request *req = mq_rq->req; |
| 2035 | struct mmc_packed *packed = mq_rq->packed; |
| 2036 | int err, check, status; |
| 2037 | u8 *ext_csd; |
| 2038 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2039 | packed->retries--; |
| 2040 | check = mmc_blk_err_check(card, areq); |
| 2041 | err = get_card_status(card, &status, 0); |
| 2042 | if (err) { |
| 2043 | pr_err("%s: error %d sending status command\n", |
| 2044 | req->rq_disk->disk_name, err); |
| 2045 | return MMC_BLK_ABORT; |
| 2046 | } |
| 2047 | |
| 2048 | if (status & R1_EXCEPTION_EVENT) { |
Ulf Hansson | 86817ff | 2014-10-17 11:39:05 +0200 | [diff] [blame] | 2049 | err = mmc_get_ext_csd(card, &ext_csd); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2050 | if (err) { |
| 2051 | pr_err("%s: error %d sending ext_csd\n", |
| 2052 | req->rq_disk->disk_name, err); |
Ulf Hansson | 86817ff | 2014-10-17 11:39:05 +0200 | [diff] [blame] | 2053 | return MMC_BLK_ABORT; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] & |
| 2057 | EXT_CSD_PACKED_FAILURE) && |
| 2058 | (ext_csd[EXT_CSD_PACKED_CMD_STATUS] & |
| 2059 | EXT_CSD_PACKED_GENERIC_ERROR)) { |
| 2060 | if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] & |
| 2061 | EXT_CSD_PACKED_INDEXED_ERROR) { |
| 2062 | packed->idx_failure = |
| 2063 | ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1; |
| 2064 | check = MMC_BLK_PARTIAL; |
| 2065 | } |
| 2066 | pr_err("%s: packed cmd failed, nr %u, sectors %u, " |
| 2067 | "failure index: %d\n", |
| 2068 | req->rq_disk->disk_name, packed->nr_entries, |
| 2069 | packed->blocks, packed->idx_failure); |
| 2070 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2071 | kfree(ext_csd); |
| 2072 | } |
| 2073 | |
| 2074 | return check; |
| 2075 | } |
| 2076 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2077 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
| 2078 | struct mmc_card *card, |
| 2079 | int disable_multi, |
| 2080 | struct mmc_queue *mq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2081 | { |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2082 | u32 readcmd, writecmd; |
| 2083 | struct mmc_blk_request *brq = &mqrq->brq; |
| 2084 | struct request *req = mqrq->req; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | struct mmc_blk_data *md = mq->data; |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 2086 | bool do_data_tag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 2088 | /* |
| 2089 | * Reliable writes are used to implement Forced Unit Access and |
Luca Porzio | d3df046 | 2015-11-06 15:12:26 +0000 | [diff] [blame] | 2090 | * are supported only on MMCs. |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 2091 | */ |
Luca Porzio | d3df046 | 2015-11-06 15:12:26 +0000 | [diff] [blame] | 2092 | bool do_rel_wr = (req->cmd_flags & REQ_FUA) && |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 2093 | (rq_data_dir(req) == WRITE) && |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2094 | (md->flags & MMC_BLK_REL_WR); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 2095 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2096 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
| 2097 | brq->mrq.cmd = &brq->cmd; |
| 2098 | brq->mrq.data = &brq->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2100 | brq->cmd.arg = blk_rq_pos(req); |
| 2101 | if (!mmc_card_blockaddr(card)) |
| 2102 | brq->cmd.arg <<= 9; |
| 2103 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 2104 | brq->data.blksz = 512; |
| 2105 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 2106 | brq->stop.arg = 0; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2107 | brq->data.blocks = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 2109 | brq->data.fault_injected = false; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2110 | /* |
| 2111 | * The block layer doesn't support all sector count |
| 2112 | * restrictions, so we need to be prepared for too big |
| 2113 | * requests. |
| 2114 | */ |
| 2115 | if (brq->data.blocks > card->host->max_blk_count) |
| 2116 | brq->data.blocks = card->host->max_blk_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2117 | |
Paul Walmsley | 2bf22b3 | 2011-10-06 14:50:33 -0600 | [diff] [blame] | 2118 | if (brq->data.blocks > 1) { |
| 2119 | /* |
| 2120 | * After a read error, we redo the request one sector |
| 2121 | * at a time in order to accurately determine which |
| 2122 | * sectors can be read successfully. |
| 2123 | */ |
| 2124 | if (disable_multi) |
| 2125 | brq->data.blocks = 1; |
| 2126 | |
Kuninori Morimoto | 2e47e84 | 2014-09-02 19:08:53 -0700 | [diff] [blame] | 2127 | /* |
| 2128 | * Some controllers have HW issues while operating |
| 2129 | * in multiple I/O mode |
| 2130 | */ |
| 2131 | if (card->host->ops->multi_io_quirk) |
| 2132 | brq->data.blocks = card->host->ops->multi_io_quirk(card, |
| 2133 | (rq_data_dir(req) == READ) ? |
| 2134 | MMC_DATA_READ : MMC_DATA_WRITE, |
| 2135 | brq->data.blocks); |
Paul Walmsley | 2bf22b3 | 2011-10-06 14:50:33 -0600 | [diff] [blame] | 2136 | } |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2137 | |
| 2138 | if (brq->data.blocks > 1 || do_rel_wr) { |
| 2139 | /* SPI multiblock writes terminate using a special |
| 2140 | * token, not a STOP_TRANSMISSION request. |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 2141 | */ |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2142 | if (!mmc_host_is_spi(card->host) || |
| 2143 | rq_data_dir(req) == READ) |
| 2144 | brq->mrq.stop = &brq->stop; |
| 2145 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 2146 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
| 2147 | } else { |
| 2148 | brq->mrq.stop = NULL; |
| 2149 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 2150 | writecmd = MMC_WRITE_BLOCK; |
| 2151 | } |
| 2152 | if (rq_data_dir(req) == READ) { |
| 2153 | brq->cmd.opcode = readcmd; |
Jaehoon Chung | f53f110 | 2016-02-01 21:07:36 +0900 | [diff] [blame] | 2154 | brq->data.flags = MMC_DATA_READ; |
Ulf Hansson | bcc3e17 | 2014-01-14 21:24:21 +0100 | [diff] [blame] | 2155 | if (brq->mrq.stop) |
| 2156 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | |
| 2157 | MMC_CMD_AC; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2158 | } else { |
| 2159 | brq->cmd.opcode = writecmd; |
Jaehoon Chung | f53f110 | 2016-02-01 21:07:36 +0900 | [diff] [blame] | 2160 | brq->data.flags = MMC_DATA_WRITE; |
Ulf Hansson | bcc3e17 | 2014-01-14 21:24:21 +0100 | [diff] [blame] | 2161 | if (brq->mrq.stop) |
| 2162 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | |
| 2163 | MMC_CMD_AC; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2164 | } |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 2165 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2166 | if (do_rel_wr) |
| 2167 | mmc_apply_rel_rw(brq, card, req); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 2168 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2169 | /* |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 2170 | * Data tag is used only during writing meta data to speed |
| 2171 | * up write and any subsequent read of this meta data |
| 2172 | */ |
| 2173 | do_data_tag = (card->ext_csd.data_tag_unit_size) && |
| 2174 | (req->cmd_flags & REQ_META) && |
| 2175 | (rq_data_dir(req) == WRITE) && |
| 2176 | ((brq->data.blocks * brq->data.blksz) >= |
| 2177 | card->ext_csd.data_tag_unit_size); |
| 2178 | |
| 2179 | /* |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2180 | * Pre-defined multi-block transfers are preferable to |
| 2181 | * open ended-ones (and necessary for reliable writes). |
| 2182 | * However, it is not sufficient to just send CMD23, |
| 2183 | * and avoid the final CMD12, as on an error condition |
| 2184 | * CMD12 (stop) needs to be sent anyway. This, coupled |
| 2185 | * with Auto-CMD23 enhancements provided by some |
| 2186 | * hosts, means that the complexity of dealing |
| 2187 | * with this is best left to the host. If CMD23 is |
| 2188 | * supported by card and host, we'll fill sbc in and let |
| 2189 | * the host deal with handling it correctly. This means |
| 2190 | * that for hosts that don't expose MMC_CAP_CMD23, no |
| 2191 | * change of behavior will be observed. |
| 2192 | * |
| 2193 | * N.B: Some MMC cards experience perf degradation. |
| 2194 | * We'll avoid using CMD23-bounded multiblock writes for |
| 2195 | * these, while retaining features like reliable writes. |
| 2196 | */ |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 2197 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
| 2198 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || |
| 2199 | do_data_tag)) { |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2200 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 2201 | brq->sbc.arg = brq->data.blocks | |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 2202 | (do_rel_wr ? (1 << 31) : 0) | |
| 2203 | (do_data_tag ? (1 << 29) : 0); |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2204 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 2205 | brq->mrq.sbc = &brq->sbc; |
| 2206 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2207 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2208 | mmc_set_data_timeout(&brq->data, card); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2209 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2210 | brq->data.sg = mqrq->sg; |
| 2211 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 2212 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2213 | /* |
| 2214 | * Adjust the sg list so it is the same size as the |
| 2215 | * request. |
| 2216 | */ |
| 2217 | if (brq->data.blocks != blk_rq_sectors(req)) { |
| 2218 | int i, data_size = brq->data.blocks << 9; |
| 2219 | struct scatterlist *sg; |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 2220 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2221 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { |
| 2222 | data_size -= sg->length; |
| 2223 | if (data_size <= 0) { |
| 2224 | sg->length += data_size; |
| 2225 | i++; |
| 2226 | break; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 2227 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 2228 | } |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2229 | brq->data.sg_len = i; |
| 2230 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 2231 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2232 | mqrq->mmc_active.mrq = &brq->mrq; |
Sahitya Tummala | c44de84 | 2015-05-08 11:12:30 +0530 | [diff] [blame^] | 2233 | mqrq->mmc_active.mrq->req = mqrq->req; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2234 | mqrq->mmc_active.err_check = mmc_blk_err_check; |
| 2235 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2236 | mmc_queue_bounce_pre(mqrq); |
| 2237 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2239 | static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q, |
| 2240 | struct mmc_card *card) |
| 2241 | { |
| 2242 | unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512; |
| 2243 | unsigned int max_seg_sz = queue_max_segment_size(q); |
| 2244 | unsigned int len, nr_segs = 0; |
| 2245 | |
| 2246 | do { |
| 2247 | len = min(hdr_sz, max_seg_sz); |
| 2248 | hdr_sz -= len; |
| 2249 | nr_segs++; |
| 2250 | } while (hdr_sz); |
| 2251 | |
| 2252 | return nr_segs; |
| 2253 | } |
| 2254 | |
Konstantin Dorfman | 225c9c7 | 2013-02-05 15:45:53 +0200 | [diff] [blame] | 2255 | /** |
| 2256 | * mmc_blk_disable_wr_packing() - disables packing mode |
| 2257 | * @mq: MMC queue. |
| 2258 | * |
| 2259 | */ |
| 2260 | void mmc_blk_disable_wr_packing(struct mmc_queue *mq) |
| 2261 | { |
| 2262 | if (mq) { |
| 2263 | mq->wr_packing_enabled = false; |
| 2264 | mq->num_of_potential_packed_wr_reqs = 0; |
| 2265 | } |
| 2266 | } |
| 2267 | EXPORT_SYMBOL(mmc_blk_disable_wr_packing); |
| 2268 | |
Lee Susman | 841fd13 | 2013-04-23 17:59:26 +0300 | [diff] [blame] | 2269 | static int get_packed_trigger(int potential, struct mmc_card *card, |
| 2270 | struct request *req, int curr_trigger) |
| 2271 | { |
| 2272 | static int num_mean_elements = 1; |
| 2273 | static unsigned long mean_potential = PCKD_TRGR_INIT_MEAN_POTEN; |
| 2274 | unsigned int trigger = curr_trigger; |
| 2275 | unsigned int pckd_trgr_upper_bound = card->ext_csd.max_packed_writes; |
| 2276 | |
| 2277 | /* scale down the upper bound to 75% */ |
| 2278 | pckd_trgr_upper_bound = (pckd_trgr_upper_bound * 3) / 4; |
| 2279 | |
| 2280 | /* |
| 2281 | * since the most common calls for this function are with small |
| 2282 | * potential write values and since we don't want these calls to affect |
| 2283 | * the packed trigger, set a lower bound and ignore calls with |
| 2284 | * potential lower than that bound |
| 2285 | */ |
| 2286 | if (potential <= PCKD_TRGR_POTEN_LOWER_BOUND) |
| 2287 | return trigger; |
| 2288 | |
| 2289 | /* |
| 2290 | * this is to prevent integer overflow in the following calculation: |
| 2291 | * once every PACKED_TRIGGER_MAX_ELEMENTS reset the algorithm |
| 2292 | */ |
| 2293 | if (num_mean_elements > PACKED_TRIGGER_MAX_ELEMENTS) { |
| 2294 | num_mean_elements = 1; |
| 2295 | mean_potential = PCKD_TRGR_INIT_MEAN_POTEN; |
| 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * get next mean value based on previous mean value and current |
| 2300 | * potential packed writes. Calculation is as follows: |
| 2301 | * mean_pot[i+1] = |
| 2302 | * ((mean_pot[i] * num_mean_elem) + potential)/(num_mean_elem + 1) |
| 2303 | */ |
| 2304 | mean_potential *= num_mean_elements; |
| 2305 | /* |
| 2306 | * add num_mean_elements so that the division of two integers doesn't |
| 2307 | * lower mean_potential too much |
| 2308 | */ |
| 2309 | if (potential > mean_potential) |
| 2310 | mean_potential += num_mean_elements; |
| 2311 | mean_potential += potential; |
| 2312 | /* this is for gaining more precision when dividing two integers */ |
| 2313 | mean_potential *= PCKD_TRGR_PRECISION_MULTIPLIER; |
| 2314 | /* this completes the mean calculation */ |
| 2315 | mean_potential /= ++num_mean_elements; |
| 2316 | mean_potential /= PCKD_TRGR_PRECISION_MULTIPLIER; |
| 2317 | |
| 2318 | /* |
| 2319 | * if current potential packed writes is greater than the mean potential |
| 2320 | * then the heuristic is that the following workload will contain many |
| 2321 | * write requests, therefore we lower the packed trigger. In the |
| 2322 | * opposite case we want to increase the trigger in order to get less |
| 2323 | * packing events. |
| 2324 | */ |
| 2325 | if (potential >= mean_potential) |
| 2326 | trigger = (trigger <= PCKD_TRGR_LOWER_BOUND) ? |
| 2327 | PCKD_TRGR_LOWER_BOUND : trigger - 1; |
| 2328 | else |
| 2329 | trigger = (trigger >= pckd_trgr_upper_bound) ? |
| 2330 | pckd_trgr_upper_bound : trigger + 1; |
| 2331 | |
| 2332 | /* |
| 2333 | * an urgent read request indicates a packed list being interrupted |
| 2334 | * by this read, therefore we aim for less packing, hence the trigger |
| 2335 | * gets increased |
| 2336 | */ |
| 2337 | if (req && (req->cmd_flags & REQ_URGENT) && (rq_data_dir(req) == READ)) |
| 2338 | trigger += PCKD_TRGR_URGENT_PENALTY; |
| 2339 | |
| 2340 | return trigger; |
| 2341 | } |
| 2342 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2343 | static void mmc_blk_write_packing_control(struct mmc_queue *mq, |
| 2344 | struct request *req) |
| 2345 | { |
| 2346 | struct mmc_host *host = mq->card->host; |
| 2347 | int data_dir; |
| 2348 | |
| 2349 | if (!(host->caps2 & MMC_CAP2_PACKED_WR)) |
| 2350 | return; |
| 2351 | |
Maya Erez | 8e2b3c3 | 2012-12-02 13:27:15 +0200 | [diff] [blame] | 2352 | /* Support for the write packing on eMMC 4.5 or later */ |
| 2353 | if (mq->card->ext_csd.rev <= 5) |
| 2354 | return; |
| 2355 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2356 | /* |
| 2357 | * In case the packing control is not supported by the host, it should |
| 2358 | * not have an effect on the write packing. Therefore we have to enable |
| 2359 | * the write packing |
| 2360 | */ |
| 2361 | if (!(host->caps2 & MMC_CAP2_PACKED_WR_CONTROL)) { |
| 2362 | mq->wr_packing_enabled = true; |
| 2363 | return; |
| 2364 | } |
| 2365 | |
| 2366 | if (!req || (req && (req->cmd_flags & REQ_PREFLUSH))) { |
| 2367 | if (mq->num_of_potential_packed_wr_reqs > |
| 2368 | mq->num_wr_reqs_to_start_packing) |
| 2369 | mq->wr_packing_enabled = true; |
Lee Susman | 841fd13 | 2013-04-23 17:59:26 +0300 | [diff] [blame] | 2370 | mq->num_wr_reqs_to_start_packing = |
| 2371 | get_packed_trigger(mq->num_of_potential_packed_wr_reqs, |
| 2372 | mq->card, req, |
| 2373 | mq->num_wr_reqs_to_start_packing); |
Tatyana Brokhman | 843915a | 2012-10-07 10:26:27 +0200 | [diff] [blame] | 2374 | mq->num_of_potential_packed_wr_reqs = 0; |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2375 | return; |
| 2376 | } |
| 2377 | |
| 2378 | data_dir = rq_data_dir(req); |
| 2379 | |
| 2380 | if (data_dir == READ) { |
Konstantin Dorfman | 225c9c7 | 2013-02-05 15:45:53 +0200 | [diff] [blame] | 2381 | mmc_blk_disable_wr_packing(mq); |
Lee Susman | 841fd13 | 2013-04-23 17:59:26 +0300 | [diff] [blame] | 2382 | mq->num_wr_reqs_to_start_packing = |
| 2383 | get_packed_trigger(mq->num_of_potential_packed_wr_reqs, |
| 2384 | mq->card, req, |
| 2385 | mq->num_wr_reqs_to_start_packing); |
| 2386 | mq->num_of_potential_packed_wr_reqs = 0; |
| 2387 | mq->wr_packing_enabled = false; |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2388 | return; |
| 2389 | } else if (data_dir == WRITE) { |
| 2390 | mq->num_of_potential_packed_wr_reqs++; |
| 2391 | } |
| 2392 | |
| 2393 | if (mq->num_of_potential_packed_wr_reqs > |
| 2394 | mq->num_wr_reqs_to_start_packing) |
| 2395 | mq->wr_packing_enabled = true; |
| 2396 | } |
| 2397 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2398 | struct mmc_wr_pack_stats *mmc_blk_get_packed_statistics(struct mmc_card *card) |
| 2399 | { |
| 2400 | if (!card) |
| 2401 | return NULL; |
| 2402 | |
| 2403 | return &card->wr_pack_stats; |
| 2404 | } |
| 2405 | EXPORT_SYMBOL(mmc_blk_get_packed_statistics); |
| 2406 | |
| 2407 | void mmc_blk_init_packed_statistics(struct mmc_card *card) |
| 2408 | { |
| 2409 | int max_num_of_packed_reqs = 0; |
| 2410 | |
| 2411 | if (!card || !card->wr_pack_stats.packing_events) |
| 2412 | return; |
| 2413 | |
| 2414 | max_num_of_packed_reqs = card->ext_csd.max_packed_writes; |
| 2415 | |
| 2416 | spin_lock(&card->wr_pack_stats.lock); |
| 2417 | memset(card->wr_pack_stats.packing_events, 0, |
| 2418 | (max_num_of_packed_reqs + 1) * |
| 2419 | sizeof(*card->wr_pack_stats.packing_events)); |
| 2420 | memset(&card->wr_pack_stats.pack_stop_reason, 0, |
| 2421 | sizeof(card->wr_pack_stats.pack_stop_reason)); |
| 2422 | card->wr_pack_stats.enabled = true; |
| 2423 | spin_unlock(&card->wr_pack_stats.lock); |
| 2424 | } |
| 2425 | EXPORT_SYMBOL(mmc_blk_init_packed_statistics); |
| 2426 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2427 | static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req) |
| 2428 | { |
| 2429 | struct request_queue *q = mq->queue; |
| 2430 | struct mmc_card *card = mq->card; |
| 2431 | struct request *cur = req, *next = NULL; |
| 2432 | struct mmc_blk_data *md = mq->data; |
| 2433 | struct mmc_queue_req *mqrq = mq->mqrq_cur; |
| 2434 | bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN; |
| 2435 | unsigned int req_sectors = 0, phys_segments = 0; |
| 2436 | unsigned int max_blk_count, max_phys_segs; |
| 2437 | bool put_back = true; |
| 2438 | u8 max_packed_rw = 0; |
| 2439 | u8 reqs = 0; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2440 | struct mmc_wr_pack_stats *stats = &card->wr_pack_stats; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2441 | |
Shawn Lin | 96e52da | 2016-08-26 08:49:55 +0800 | [diff] [blame] | 2442 | /* |
| 2443 | * We don't need to check packed for any further |
| 2444 | * operation of packed stuff as we set MMC_PACKED_NONE |
| 2445 | * and return zero for reqs if geting null packed. Also |
| 2446 | * we clean the flag of MMC_BLK_PACKED_CMD to avoid doing |
| 2447 | * it again when removing blk req. |
| 2448 | */ |
| 2449 | if (!mqrq->packed) { |
| 2450 | md->flags &= (~MMC_BLK_PACKED_CMD); |
| 2451 | goto no_packed; |
| 2452 | } |
| 2453 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2454 | if (!(md->flags & MMC_BLK_PACKED_CMD)) |
| 2455 | goto no_packed; |
| 2456 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2457 | if (!mq->wr_packing_enabled) |
| 2458 | goto no_packed; |
| 2459 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2460 | if ((rq_data_dir(cur) == WRITE) && |
| 2461 | mmc_host_packed_wr(card->host)) |
| 2462 | max_packed_rw = card->ext_csd.max_packed_writes; |
| 2463 | |
| 2464 | if (max_packed_rw == 0) |
| 2465 | goto no_packed; |
| 2466 | |
| 2467 | if (mmc_req_rel_wr(cur) && |
| 2468 | (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) |
| 2469 | goto no_packed; |
| 2470 | |
| 2471 | if (mmc_large_sector(card) && |
| 2472 | !IS_ALIGNED(blk_rq_sectors(cur), 8)) |
| 2473 | goto no_packed; |
| 2474 | |
Konstantin Dorfman | 31a482d | 2013-02-05 16:26:19 +0200 | [diff] [blame] | 2475 | if (cur->cmd_flags & REQ_FUA) |
| 2476 | goto no_packed; |
| 2477 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2478 | mmc_blk_clear_packed(mqrq); |
| 2479 | |
| 2480 | max_blk_count = min(card->host->max_blk_count, |
| 2481 | card->host->max_req_size >> 9); |
| 2482 | if (unlikely(max_blk_count > 0xffff)) |
| 2483 | max_blk_count = 0xffff; |
| 2484 | |
| 2485 | max_phys_segs = queue_max_segments(q); |
| 2486 | req_sectors += blk_rq_sectors(cur); |
| 2487 | phys_segments += cur->nr_phys_segments; |
| 2488 | |
| 2489 | if (rq_data_dir(cur) == WRITE) { |
| 2490 | req_sectors += mmc_large_sector(card) ? 8 : 1; |
| 2491 | phys_segments += mmc_calc_packed_hdr_segs(q, card); |
| 2492 | } |
| 2493 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2494 | spin_lock(&stats->lock); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2495 | do { |
| 2496 | if (reqs >= max_packed_rw - 1) { |
| 2497 | put_back = false; |
| 2498 | break; |
| 2499 | } |
| 2500 | |
| 2501 | spin_lock_irq(q->queue_lock); |
| 2502 | next = blk_fetch_request(q); |
| 2503 | spin_unlock_irq(q->queue_lock); |
| 2504 | if (!next) { |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2505 | MMC_BLK_UPDATE_STOP_REASON(stats, EMPTY_QUEUE); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2506 | put_back = false; |
| 2507 | break; |
| 2508 | } |
| 2509 | |
| 2510 | if (mmc_large_sector(card) && |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2511 | !IS_ALIGNED(blk_rq_sectors(next), 8)) { |
| 2512 | MMC_BLK_UPDATE_STOP_REASON(stats, LARGE_SEC_ALIGN); |
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 | |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 2516 | if (req_op(next) == REQ_OP_DISCARD || |
Adrian Hunter | 7afafc8 | 2016-08-16 10:59:35 +0300 | [diff] [blame] | 2517 | req_op(next) == REQ_OP_SECURE_ERASE || |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2518 | req_op(next) == REQ_OP_FLUSH) { |
| 2519 | if (req_op(next) != REQ_OP_SECURE_ERASE) |
| 2520 | MMC_BLK_UPDATE_STOP_REASON(stats, FLUSH_OR_DISCARD); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2521 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2522 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2523 | |
Konstantin Dorfman | 31a482d | 2013-02-05 16:26:19 +0200 | [diff] [blame] | 2524 | if (next->cmd_flags & REQ_FUA) { |
| 2525 | MMC_BLK_UPDATE_STOP_REASON(stats, FUA); |
| 2526 | break; |
| 2527 | } |
| 2528 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2529 | if (rq_data_dir(cur) != rq_data_dir(next)) { |
| 2530 | MMC_BLK_UPDATE_STOP_REASON(stats, WRONG_DATA_DIR); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2531 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2532 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2533 | |
| 2534 | if (mmc_req_rel_wr(next) && |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2535 | (md->flags & MMC_BLK_REL_WR) && !en_rel_wr) { |
| 2536 | MMC_BLK_UPDATE_STOP_REASON(stats, REL_WRITE); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2537 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2538 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2539 | |
| 2540 | req_sectors += blk_rq_sectors(next); |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2541 | if (req_sectors > max_blk_count) { |
| 2542 | if (stats->enabled) |
| 2543 | stats->pack_stop_reason[EXCEEDS_SECTORS]++; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2544 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2545 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2546 | |
| 2547 | phys_segments += next->nr_phys_segments; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2548 | if (phys_segments > max_phys_segs) { |
| 2549 | MMC_BLK_UPDATE_STOP_REASON(stats, EXCEEDS_SEGMENTS); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2550 | break; |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2551 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2552 | |
Maya Erez | 5a8dae1 | 2014-12-04 15:13:59 +0200 | [diff] [blame] | 2553 | if (mq->no_pack_for_random) { |
| 2554 | if ((blk_rq_pos(cur) + blk_rq_sectors(cur)) != |
| 2555 | blk_rq_pos(next)) { |
| 2556 | MMC_BLK_UPDATE_STOP_REASON(stats, RANDOM); |
| 2557 | put_back = 1; |
| 2558 | break; |
| 2559 | } |
| 2560 | } |
| 2561 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 2562 | if (rq_data_dir(next) == WRITE) |
| 2563 | mq->num_of_potential_packed_wr_reqs++; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2564 | list_add_tail(&next->queuelist, &mqrq->packed->list); |
| 2565 | cur = next; |
| 2566 | reqs++; |
| 2567 | } while (1); |
| 2568 | |
| 2569 | if (put_back) { |
| 2570 | spin_lock_irq(q->queue_lock); |
| 2571 | blk_requeue_request(q, next); |
| 2572 | spin_unlock_irq(q->queue_lock); |
| 2573 | } |
| 2574 | |
Tatyana Brokhman | 08238ce | 2012-10-07 10:33:13 +0200 | [diff] [blame] | 2575 | if (stats->enabled) { |
| 2576 | if (reqs + 1 <= card->ext_csd.max_packed_writes) |
| 2577 | stats->packing_events[reqs + 1]++; |
| 2578 | if (reqs + 1 == max_packed_rw) |
| 2579 | MMC_BLK_UPDATE_STOP_REASON(stats, THRESHOLD); |
| 2580 | } |
| 2581 | |
| 2582 | spin_unlock(&stats->lock); |
| 2583 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2584 | if (reqs > 0) { |
| 2585 | list_add(&req->queuelist, &mqrq->packed->list); |
| 2586 | mqrq->packed->nr_entries = ++reqs; |
| 2587 | mqrq->packed->retries = reqs; |
| 2588 | return reqs; |
| 2589 | } |
| 2590 | |
| 2591 | no_packed: |
| 2592 | mqrq->cmd_type = MMC_PACKED_NONE; |
| 2593 | return 0; |
| 2594 | } |
| 2595 | |
| 2596 | static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq, |
| 2597 | struct mmc_card *card, |
| 2598 | struct mmc_queue *mq) |
| 2599 | { |
| 2600 | struct mmc_blk_request *brq = &mqrq->brq; |
| 2601 | struct request *req = mqrq->req; |
| 2602 | struct request *prq; |
| 2603 | struct mmc_blk_data *md = mq->data; |
| 2604 | struct mmc_packed *packed = mqrq->packed; |
| 2605 | bool do_rel_wr, do_data_tag; |
Jiri Slaby | 3f2d266 | 2016-10-03 10:58:28 +0200 | [diff] [blame] | 2606 | __le32 *packed_cmd_hdr; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2607 | u8 hdr_blocks; |
| 2608 | u8 i = 1; |
| 2609 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2610 | mqrq->cmd_type = MMC_PACKED_WRITE; |
| 2611 | packed->blocks = 0; |
| 2612 | packed->idx_failure = MMC_PACKED_NR_IDX; |
| 2613 | |
| 2614 | packed_cmd_hdr = packed->cmd_hdr; |
| 2615 | memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr)); |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2616 | packed_cmd_hdr[0] = cpu_to_le32((packed->nr_entries << 16) | |
| 2617 | (PACKED_CMD_WR << 8) | PACKED_CMD_VER); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2618 | hdr_blocks = mmc_large_sector(card) ? 8 : 1; |
| 2619 | |
| 2620 | /* |
| 2621 | * Argument for each entry of packed group |
| 2622 | */ |
| 2623 | list_for_each_entry(prq, &packed->list, queuelist) { |
| 2624 | do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR); |
| 2625 | do_data_tag = (card->ext_csd.data_tag_unit_size) && |
| 2626 | (prq->cmd_flags & REQ_META) && |
| 2627 | (rq_data_dir(prq) == WRITE) && |
Adrian Hunter | d806b46 | 2016-06-10 16:22:16 +0300 | [diff] [blame] | 2628 | blk_rq_bytes(prq) >= card->ext_csd.data_tag_unit_size; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2629 | /* Argument of CMD23 */ |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2630 | packed_cmd_hdr[(i * 2)] = cpu_to_le32( |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2631 | (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) | |
| 2632 | (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) | |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2633 | blk_rq_sectors(prq)); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2634 | /* Argument of CMD18 or CMD25 */ |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2635 | packed_cmd_hdr[((i * 2)) + 1] = cpu_to_le32( |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2636 | mmc_card_blockaddr(card) ? |
Taras Kondratiuk | f68381a | 2016-07-13 22:05:38 +0000 | [diff] [blame] | 2637 | blk_rq_pos(prq) : blk_rq_pos(prq) << 9); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2638 | packed->blocks += blk_rq_sectors(prq); |
| 2639 | i++; |
| 2640 | } |
| 2641 | |
| 2642 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
| 2643 | brq->mrq.cmd = &brq->cmd; |
| 2644 | brq->mrq.data = &brq->data; |
| 2645 | brq->mrq.sbc = &brq->sbc; |
| 2646 | brq->mrq.stop = &brq->stop; |
| 2647 | |
| 2648 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 2649 | brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks); |
| 2650 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 2651 | |
| 2652 | brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK; |
| 2653 | brq->cmd.arg = blk_rq_pos(req); |
| 2654 | if (!mmc_card_blockaddr(card)) |
| 2655 | brq->cmd.arg <<= 9; |
| 2656 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 2657 | |
| 2658 | brq->data.blksz = 512; |
| 2659 | brq->data.blocks = packed->blocks + hdr_blocks; |
Jaehoon Chung | f53f110 | 2016-02-01 21:07:36 +0900 | [diff] [blame] | 2660 | brq->data.flags = MMC_DATA_WRITE; |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 2661 | brq->data.fault_injected = false; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2662 | |
| 2663 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 2664 | brq->stop.arg = 0; |
| 2665 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 2666 | |
| 2667 | mmc_set_data_timeout(&brq->data, card); |
| 2668 | |
| 2669 | brq->data.sg = mqrq->sg; |
| 2670 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); |
| 2671 | |
| 2672 | mqrq->mmc_active.mrq = &brq->mrq; |
Tatyana Brokhman | 71aefb8 | 2012-10-09 13:50:56 +0200 | [diff] [blame] | 2673 | |
| 2674 | /* |
| 2675 | * This is intended for packed commands tests usage - in case these |
| 2676 | * functions are not in use the respective pointers are NULL |
| 2677 | */ |
| 2678 | if (mq->err_check_fn) |
| 2679 | mqrq->mmc_active.err_check = mq->err_check_fn; |
| 2680 | else |
| 2681 | mqrq->mmc_active.err_check = mmc_blk_packed_err_check; |
| 2682 | |
| 2683 | if (mq->packed_test_fn) |
| 2684 | mq->packed_test_fn(mq->queue, mqrq); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2685 | |
| 2686 | mmc_queue_bounce_pre(mqrq); |
| 2687 | } |
| 2688 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2689 | static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
| 2690 | struct mmc_blk_request *brq, struct request *req, |
| 2691 | int ret) |
| 2692 | { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2693 | struct mmc_queue_req *mq_rq; |
| 2694 | mq_rq = container_of(brq, struct mmc_queue_req, brq); |
| 2695 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2696 | /* |
| 2697 | * If this is an SD card and we're writing, we can first |
| 2698 | * mark the known good sectors as ok. |
| 2699 | * |
| 2700 | * If the card is not SD, we can still ok written sectors |
| 2701 | * as reported by the controller (which might be less than |
| 2702 | * the real number of written sectors, but never more). |
| 2703 | */ |
| 2704 | if (mmc_card_sd(card)) { |
| 2705 | u32 blocks; |
Asutosh Das | f066541 | 2012-07-27 18:10:19 +0530 | [diff] [blame] | 2706 | if (!brq->data.fault_injected) { |
| 2707 | blocks = mmc_sd_num_wr_blocks(card); |
| 2708 | if (blocks != (u32)-1) |
| 2709 | ret = blk_end_request(req, 0, blocks << 9); |
| 2710 | } else |
| 2711 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2712 | } else { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2713 | if (!mmc_packed_cmd(mq_rq->cmd_type)) |
| 2714 | ret = blk_end_request(req, 0, brq->data.bytes_xfered); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2715 | } |
| 2716 | return ret; |
| 2717 | } |
| 2718 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2719 | static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq) |
| 2720 | { |
| 2721 | struct request *prq; |
| 2722 | struct mmc_packed *packed = mq_rq->packed; |
| 2723 | int idx = packed->idx_failure, i = 0; |
| 2724 | int ret = 0; |
| 2725 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2726 | while (!list_empty(&packed->list)) { |
| 2727 | prq = list_entry_rq(packed->list.next); |
| 2728 | if (idx == i) { |
| 2729 | /* retry from error index */ |
| 2730 | packed->nr_entries -= idx; |
| 2731 | mq_rq->req = prq; |
| 2732 | ret = 1; |
| 2733 | |
| 2734 | if (packed->nr_entries == MMC_PACKED_NR_SINGLE) { |
| 2735 | list_del_init(&prq->queuelist); |
| 2736 | mmc_blk_clear_packed(mq_rq); |
| 2737 | } |
| 2738 | return ret; |
| 2739 | } |
| 2740 | list_del_init(&prq->queuelist); |
| 2741 | blk_end_request(prq, 0, blk_rq_bytes(prq)); |
| 2742 | i++; |
| 2743 | } |
| 2744 | |
| 2745 | mmc_blk_clear_packed(mq_rq); |
| 2746 | return ret; |
| 2747 | } |
| 2748 | |
| 2749 | static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq) |
| 2750 | { |
| 2751 | struct request *prq; |
| 2752 | struct mmc_packed *packed = mq_rq->packed; |
| 2753 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2754 | while (!list_empty(&packed->list)) { |
| 2755 | prq = list_entry_rq(packed->list.next); |
| 2756 | list_del_init(&prq->queuelist); |
| 2757 | blk_end_request(prq, -EIO, blk_rq_bytes(prq)); |
| 2758 | } |
| 2759 | |
| 2760 | mmc_blk_clear_packed(mq_rq); |
| 2761 | } |
| 2762 | |
| 2763 | static void mmc_blk_revert_packed_req(struct mmc_queue *mq, |
| 2764 | struct mmc_queue_req *mq_rq) |
| 2765 | { |
| 2766 | struct request *prq; |
| 2767 | struct request_queue *q = mq->queue; |
| 2768 | struct mmc_packed *packed = mq_rq->packed; |
| 2769 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2770 | while (!list_empty(&packed->list)) { |
| 2771 | prq = list_entry_rq(packed->list.prev); |
| 2772 | if (prq->queuelist.prev != &packed->list) { |
| 2773 | list_del_init(&prq->queuelist); |
| 2774 | spin_lock_irq(q->queue_lock); |
| 2775 | blk_requeue_request(mq->queue, prq); |
| 2776 | spin_unlock_irq(q->queue_lock); |
| 2777 | } else { |
| 2778 | list_del_init(&prq->queuelist); |
| 2779 | } |
| 2780 | } |
| 2781 | |
| 2782 | mmc_blk_clear_packed(mq_rq); |
| 2783 | } |
| 2784 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2785 | 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] | 2786 | { |
| 2787 | struct mmc_blk_data *md = mq->data; |
| 2788 | struct mmc_card *card = md->queue.card; |
| 2789 | struct mmc_blk_request *brq = &mq->mqrq_cur->brq; |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2790 | 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] | 2791 | enum mmc_blk_status status; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2792 | struct mmc_queue_req *mq_rq; |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2793 | struct request *req = rqc; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2794 | struct mmc_async_req *areq; |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2795 | const u8 packed_nr = 2; |
| 2796 | u8 reqs = 0; |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2797 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 2798 | unsigned long waitfor = jiffies; |
| 2799 | #endif |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2800 | |
| 2801 | if (!rqc && !mq->mqrq_prev->req) |
| 2802 | return 0; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2803 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2804 | if (rqc) |
| 2805 | reqs = mmc_blk_prep_packed_list(mq, rqc); |
| 2806 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 2807 | do { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2808 | if (rqc) { |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2809 | /* |
| 2810 | * When 4KB native sector is enabled, only 8 blocks |
| 2811 | * multiple read or write is allowed |
| 2812 | */ |
Yuan, Juntao | e87c856 | 2016-05-13 07:59:24 +0000 | [diff] [blame] | 2813 | if (mmc_large_sector(card) && |
| 2814 | !IS_ALIGNED(blk_rq_sectors(rqc), 8)) { |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2815 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", |
| 2816 | req->rq_disk->disk_name); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2817 | mq_rq = mq->mqrq_cur; |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2818 | goto cmd_abort; |
| 2819 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2820 | |
| 2821 | if (reqs >= packed_nr) |
| 2822 | mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur, |
| 2823 | card, mq); |
| 2824 | else |
| 2825 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2826 | areq = &mq->mqrq_cur->mmc_active; |
| 2827 | } else |
| 2828 | areq = NULL; |
| 2829 | areq = mmc_start_req(card->host, areq, (int *) &status); |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2830 | if (!areq) { |
| 2831 | if (status == MMC_BLK_NEW_REQUEST) |
Sujit Reddy Thumma | 5529199 | 2014-12-09 20:40:16 +0200 | [diff] [blame] | 2832 | set_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2833 | return 0; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2834 | } |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 2835 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2836 | mq_rq = container_of(areq, struct mmc_queue_req, mmc_active); |
| 2837 | brq = &mq_rq->brq; |
| 2838 | req = mq_rq->req; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2839 | type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2840 | mmc_queue_bounce_post(mq_rq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 2841 | |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2842 | switch (status) { |
| 2843 | case MMC_BLK_SUCCESS: |
| 2844 | case MMC_BLK_PARTIAL: |
| 2845 | /* |
| 2846 | * A block was successfully transferred. |
| 2847 | */ |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2848 | mmc_blk_reset_success(md, type); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2849 | |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 2850 | mmc_blk_simulate_delay(mq, rqc, waitfor); |
| 2851 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2852 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2853 | ret = mmc_blk_end_packed_req(mq_rq); |
| 2854 | break; |
| 2855 | } else { |
| 2856 | ret = blk_end_request(req, 0, |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2857 | brq->data.bytes_xfered); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2858 | } |
| 2859 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2860 | /* |
| 2861 | * If the blk_end_request function returns non-zero even |
| 2862 | * though all data has been transferred and no errors |
| 2863 | * were returned by the host controller, it's a bug. |
| 2864 | */ |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2865 | if (status == MMC_BLK_SUCCESS && ret) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2866 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2867 | __func__, blk_rq_bytes(req), |
| 2868 | brq->data.bytes_xfered); |
| 2869 | rqc = NULL; |
| 2870 | goto cmd_abort; |
| 2871 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2872 | break; |
| 2873 | case MMC_BLK_CMD_ERR: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2874 | ret = mmc_blk_cmd_err(md, card, brq, req, ret); |
Ding Wang | 29535f7 | 2015-05-18 20:14:15 +0800 | [diff] [blame] | 2875 | if (mmc_blk_reset(md, card->host, type)) |
| 2876 | goto cmd_abort; |
| 2877 | if (!ret) |
| 2878 | goto start_new_req; |
| 2879 | break; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2880 | case MMC_BLK_RETRY: |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2881 | retune_retry_done = brq->retune_retry_done; |
Maya Erez | f93ca0a | 2014-12-09 23:34:41 +0200 | [diff] [blame] | 2882 | if (retry++ < MMC_BLK_MAX_RETRIES) |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 2883 | break; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2884 | /* Fall through */ |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2885 | case MMC_BLK_ABORT: |
Maya Erez | f93ca0a | 2014-12-09 23:34:41 +0200 | [diff] [blame] | 2886 | if (!mmc_blk_reset(md, card->host, type) && |
| 2887 | (retry++ < (MMC_BLK_MAX_RETRIES + 1))) |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2888 | break; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 2889 | goto cmd_abort; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2890 | case MMC_BLK_DATA_ERR: { |
| 2891 | int err; |
| 2892 | |
| 2893 | err = mmc_blk_reset(md, card->host, type); |
| 2894 | if (!err) |
| 2895 | break; |
Sahitya Tummala | d0a1984 | 2014-10-31 09:46:20 +0530 | [diff] [blame] | 2896 | goto cmd_abort; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2897 | } |
| 2898 | case MMC_BLK_ECC_ERR: |
| 2899 | if (brq->data.blocks > 1) { |
| 2900 | /* Redo read one sector at a time */ |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 2901 | pr_warn("%s: retrying using single block read\n", |
| 2902 | req->rq_disk->disk_name); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 2903 | disable_multi = 1; |
| 2904 | break; |
| 2905 | } |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2906 | /* |
| 2907 | * After an error, we redo I/O one sector at a |
| 2908 | * time, so we only reach here after trying to |
| 2909 | * read a single sector. |
| 2910 | */ |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 2911 | ret = blk_end_request(req, -EIO, |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2912 | brq->data.blksz); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2913 | if (!ret) |
| 2914 | goto start_new_req; |
Per Forlin | d78d4a8 | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 2915 | break; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 2916 | case MMC_BLK_NOMEDIUM: |
| 2917 | goto cmd_abort; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2918 | default: |
| 2919 | pr_err("%s: Unhandled return value (%d)", |
| 2920 | req->rq_disk->disk_name, status); |
| 2921 | goto cmd_abort; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 2922 | } |
| 2923 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2924 | if (ret) { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2925 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2926 | if (!mq_rq->packed->retries) |
| 2927 | goto cmd_abort; |
| 2928 | mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq); |
| 2929 | mmc_start_req(card->host, |
| 2930 | &mq_rq->mmc_active, NULL); |
| 2931 | } else { |
| 2932 | |
| 2933 | /* |
| 2934 | * In case of a incomplete request |
| 2935 | * prepare it again and resend. |
| 2936 | */ |
| 2937 | mmc_blk_rw_rq_prep(mq_rq, card, |
| 2938 | disable_multi, mq); |
| 2939 | mmc_start_req(card->host, |
| 2940 | &mq_rq->mmc_active, NULL); |
| 2941 | } |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 2942 | mq_rq->brq.retune_retry_done = retune_retry_done; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2943 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2944 | } while (ret); |
| 2945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2946 | return 1; |
| 2947 | |
Russell King - ARM Linux | a01f3ccf | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 2948 | cmd_abort: |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2949 | if (mmc_packed_cmd(mq_rq->cmd_type)) { |
| 2950 | mmc_blk_abort_packed_req(mq_rq); |
| 2951 | } else { |
| 2952 | if (mmc_card_removed(card)) |
| 2953 | req->cmd_flags |= REQ_QUIET; |
| 2954 | while (ret) |
| 2955 | ret = blk_end_request(req, -EIO, |
| 2956 | blk_rq_cur_bytes(req)); |
| 2957 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2959 | start_new_req: |
| 2960 | if (rqc) { |
Seungwon Jeon | 7a81902 | 2013-01-22 19:48:07 +0900 | [diff] [blame] | 2961 | if (mmc_card_removed(card)) { |
| 2962 | rqc->cmd_flags |= REQ_QUIET; |
| 2963 | blk_end_request_all(rqc, -EIO); |
| 2964 | } else { |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 2965 | /* |
| 2966 | * If current request is packed, it needs to put back. |
| 2967 | */ |
| 2968 | if (mmc_packed_cmd(mq->mqrq_cur->cmd_type)) |
| 2969 | mmc_blk_revert_packed_req(mq, mq->mqrq_cur); |
| 2970 | |
Seungwon Jeon | 7a81902 | 2013-01-22 19:48:07 +0900 | [diff] [blame] | 2971 | mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq); |
| 2972 | mmc_start_req(card->host, |
| 2973 | &mq->mqrq_cur->mmc_active, NULL); |
| 2974 | } |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2975 | } |
| 2976 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | return 0; |
| 2978 | } |
| 2979 | |
Linus Walleij | 29eb7bd | 2016-09-20 11:34:38 +0200 | [diff] [blame] | 2980 | int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2981 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2982 | int ret; |
| 2983 | struct mmc_blk_data *md = mq->data; |
| 2984 | struct mmc_card *card = md->queue.card; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 2985 | struct mmc_host *host = card->host; |
| 2986 | unsigned long flags; |
Adrian Hunter | 869c554 | 2016-08-25 14:11:43 -0600 | [diff] [blame] | 2987 | bool req_is_special = mmc_req_is_special(req); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2988 | |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2989 | if (req && !mq->mqrq_prev->req) |
| 2990 | /* claim host only for the first request */ |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2991 | mmc_get_card(card); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2992 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2993 | ret = mmc_blk_part_switch(card, md); |
| 2994 | if (ret) { |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2995 | if (req) { |
Subhash Jadavani | ecf8b5d | 2012-06-07 15:46:58 +0530 | [diff] [blame] | 2996 | blk_end_request_all(req, -EIO); |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2997 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2998 | ret = 0; |
| 2999 | goto out; |
| 3000 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 3001 | |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 3002 | mmc_blk_write_packing_control(mq, req); |
| 3003 | |
Sujit Reddy Thumma | 5529199 | 2014-12-09 20:40:16 +0200 | [diff] [blame] | 3004 | clear_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags); |
Mike Christie | c2df40d | 2016-06-05 14:32:17 -0500 | [diff] [blame] | 3005 | if (req && req_op(req) == REQ_OP_DISCARD) { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 3006 | /* complete ongoing async transfer before issuing discard */ |
| 3007 | if (card->host->areq) |
| 3008 | mmc_blk_issue_rw_rq(mq, NULL); |
Christoph Hellwig | 288dab8 | 2016-06-09 16:00:36 +0200 | [diff] [blame] | 3009 | ret = mmc_blk_issue_discard_rq(mq, req); |
| 3010 | } else if (req && req_op(req) == REQ_OP_SECURE_ERASE) { |
| 3011 | /* complete ongoing async transfer before issuing secure erase*/ |
| 3012 | if (card->host->areq) |
| 3013 | mmc_blk_issue_rw_rq(mq, NULL); |
Maya Erez | 0c0609f | 2014-12-09 23:31:55 +0200 | [diff] [blame] | 3014 | if (!(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN)) |
| 3015 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
| 3016 | else |
| 3017 | ret = mmc_blk_issue_discard_rq(mq, req); |
Mike Christie | 3a5e02c | 2016-06-05 14:32:23 -0500 | [diff] [blame] | 3018 | } else if (req && req_op(req) == REQ_OP_FLUSH) { |
Jaehoon Chung | 393f9a0 | 2011-07-13 17:02:16 +0900 | [diff] [blame] | 3019 | /* complete ongoing async transfer before issuing flush */ |
| 3020 | if (card->host->areq) |
| 3021 | mmc_blk_issue_rw_rq(mq, NULL); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 3022 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 3023 | } else { |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 3024 | if (!req && host->areq) { |
| 3025 | spin_lock_irqsave(&host->context_info.lock, flags); |
| 3026 | host->context_info.is_waiting_last_req = true; |
| 3027 | spin_unlock_irqrestore(&host->context_info.lock, flags); |
| 3028 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 3029 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 3030 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 3031 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3032 | out: |
Sujit Reddy Thumma | 5529199 | 2014-12-09 20:40:16 +0200 | [diff] [blame] | 3033 | if ((!req && !(test_bit(MMC_QUEUE_NEW_REQUEST, &mq->flags))) || |
| 3034 | req_is_special) |
Seungwon Jeon | ef3a69c7 | 2013-03-14 15:17:13 +0900 | [diff] [blame] | 3035 | /* |
| 3036 | * Release host when there are no more requests |
| 3037 | * and after special request(discard, flush) is done. |
| 3038 | * In case sepecial request, there is no reentry to |
| 3039 | * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'. |
| 3040 | */ |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3041 | mmc_put_card(card); |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 3042 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 3043 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3044 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3045 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 3046 | { |
| 3047 | return mmc_card_readonly(card) || |
| 3048 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 3049 | } |
| 3050 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3051 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 3052 | struct device *parent, |
| 3053 | sector_t size, |
| 3054 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3055 | const char *subname, |
| 3056 | int area_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | { |
| 3058 | struct mmc_blk_data *md; |
| 3059 | int devidx, ret; |
| 3060 | |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 3061 | again: |
| 3062 | if (!ida_pre_get(&mmc_blk_ida, GFP_KERNEL)) |
| 3063 | return ERR_PTR(-ENOMEM); |
| 3064 | |
| 3065 | spin_lock(&mmc_blk_lock); |
| 3066 | ret = ida_get_new(&mmc_blk_ida, &devidx); |
| 3067 | spin_unlock(&mmc_blk_lock); |
| 3068 | |
| 3069 | if (ret == -EAGAIN) |
| 3070 | goto again; |
| 3071 | else if (ret) |
| 3072 | return ERR_PTR(ret); |
| 3073 | |
| 3074 | if (devidx >= max_devices) { |
| 3075 | ret = -ENOSPC; |
| 3076 | goto out; |
| 3077 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 3079 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3080 | if (!md) { |
| 3081 | ret = -ENOMEM; |
| 3082 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3083 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3084 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3085 | md->area_type = area_type; |
| 3086 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 3087 | /* |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3088 | * Set the read-only status based on the supported commands |
| 3089 | * and the write protect switch. |
| 3090 | */ |
| 3091 | md->read_only = mmc_blk_readonly(card); |
| 3092 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 3093 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3094 | if (md->disk == NULL) { |
| 3095 | ret = -ENOMEM; |
| 3096 | goto err_kfree; |
| 3097 | } |
| 3098 | |
| 3099 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3100 | INIT_LIST_HEAD(&md->part); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3101 | md->usage = 1; |
| 3102 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 3103 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3104 | if (ret) |
| 3105 | goto err_putdisk; |
| 3106 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3107 | md->queue.data = md; |
| 3108 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 3109 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 3110 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3111 | md->disk->fops = &mmc_bdops; |
| 3112 | md->disk->private_data = md; |
| 3113 | md->disk->queue = md->queue.queue; |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 3114 | md->parent = parent; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3115 | set_disk_ro(md->disk, md->read_only || default_ro); |
Colin Cross | 382c55f | 2015-10-22 10:00:41 -0700 | [diff] [blame] | 3116 | md->disk->flags = GENHD_FL_EXT_DEVT; |
Ulf Hansson | f5b4d71 | 2014-09-03 11:02:23 +0200 | [diff] [blame] | 3117 | 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] | 3118 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3119 | |
| 3120 | /* |
| 3121 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 3122 | * |
| 3123 | * - be set for removable media with permanent block devices |
| 3124 | * - be unset for removable block devices with permanent media |
| 3125 | * |
| 3126 | * Since MMC block devices clearly fall under the second |
| 3127 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 3128 | * should use the block device creation/destruction hotplug |
| 3129 | * messages to tell when the card is present. |
| 3130 | */ |
| 3131 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 3132 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
Ulf Hansson | 9aaf343 | 2016-04-06 16:12:08 +0200 | [diff] [blame] | 3133 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3134 | |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 3135 | if (mmc_card_mmc(card)) |
| 3136 | blk_queue_logical_block_size(md->queue.queue, |
| 3137 | card->ext_csd.data_sector_size); |
| 3138 | else |
| 3139 | blk_queue_logical_block_size(md->queue.queue, 512); |
| 3140 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3141 | set_capacity(md->disk, size); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3142 | |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 3143 | if (mmc_host_cmd23(card->host)) { |
Daniel Glöckner | 0ed50ab | 2016-08-30 14:17:30 +0200 | [diff] [blame] | 3144 | if ((mmc_card_mmc(card) && |
| 3145 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 3146 | (mmc_card_sd(card) && |
| 3147 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) |
| 3148 | md->flags |= MMC_BLK_CMD23; |
| 3149 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3150 | |
| 3151 | if (mmc_card_mmc(card) && |
| 3152 | md->flags & MMC_BLK_CMD23 && |
| 3153 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
| 3154 | card->ext_csd.rel_sectors)) { |
| 3155 | md->flags |= MMC_BLK_REL_WR; |
Jens Axboe | e9d5c74 | 2016-03-30 10:17:20 -0600 | [diff] [blame] | 3156 | blk_queue_write_cache(md->queue.queue, true, true); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3157 | } |
| 3158 | |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 3159 | if (mmc_card_mmc(card) && |
| 3160 | (area_type == MMC_BLK_DATA_AREA_MAIN) && |
| 3161 | (md->flags & MMC_BLK_CMD23) && |
| 3162 | card->ext_csd.packed_event_en) { |
| 3163 | if (!mmc_packed_init(&md->queue, card)) |
| 3164 | md->flags |= MMC_BLK_PACKED_CMD; |
| 3165 | } |
| 3166 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3167 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3168 | |
| 3169 | err_putdisk: |
| 3170 | put_disk(md->disk); |
| 3171 | err_kfree: |
| 3172 | kfree(md); |
| 3173 | out: |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 3174 | spin_lock(&mmc_blk_lock); |
| 3175 | ida_remove(&mmc_blk_ida, devidx); |
| 3176 | spin_unlock(&mmc_blk_lock); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 3177 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3180 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 3181 | { |
| 3182 | sector_t size; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3183 | |
| 3184 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 3185 | /* |
| 3186 | * The EXT_CSD sector count is in number or 512 byte |
| 3187 | * sectors. |
| 3188 | */ |
| 3189 | size = card->ext_csd.sectors; |
| 3190 | } else { |
| 3191 | /* |
| 3192 | * The CSD capacity field is in units of read_blkbits. |
| 3193 | * set_capacity takes units of 512 bytes. |
| 3194 | */ |
Kuninori Morimoto | 087de9e | 2015-05-11 07:35:28 +0000 | [diff] [blame] | 3195 | size = (typeof(sector_t))card->csd.capacity |
| 3196 | << (card->csd.read_blkbits - 9); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3197 | } |
| 3198 | |
Tobias Klauser | 7a30f2a | 2015-01-21 15:56:44 +0100 | [diff] [blame] | 3199 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3200 | MMC_BLK_DATA_AREA_MAIN); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 3204 | struct mmc_blk_data *md, |
| 3205 | unsigned int part_type, |
| 3206 | sector_t size, |
| 3207 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3208 | const char *subname, |
| 3209 | int area_type) |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3210 | { |
| 3211 | char cap_str[10]; |
| 3212 | struct mmc_blk_data *part_md; |
| 3213 | |
| 3214 | 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] | 3215 | subname, area_type); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3216 | if (IS_ERR(part_md)) |
| 3217 | return PTR_ERR(part_md); |
| 3218 | part_md->part_type = part_type; |
| 3219 | list_add(&part_md->part, &md->part); |
| 3220 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 3221 | 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] | 3222 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3223 | pr_info("%s: %s %s partition %u %s\n", |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3224 | part_md->disk->disk_name, mmc_card_id(card), |
| 3225 | mmc_card_name(card), part_md->part_type, cap_str); |
| 3226 | return 0; |
| 3227 | } |
| 3228 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 3229 | /* MMC Physical partitions consist of two boot partitions and |
| 3230 | * up to four general purpose partitions. |
| 3231 | * For each partition enabled in EXT_CSD a block device will be allocatedi |
| 3232 | * to provide access to the partition. |
| 3233 | */ |
| 3234 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3235 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 3236 | { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 3237 | int idx, ret = 0; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3238 | |
| 3239 | if (!mmc_card_mmc(card)) |
| 3240 | return 0; |
| 3241 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 3242 | for (idx = 0; idx < card->nr_parts; idx++) { |
| 3243 | if (card->part[idx].size) { |
| 3244 | ret = mmc_blk_alloc_part(card, md, |
| 3245 | card->part[idx].part_cfg, |
| 3246 | card->part[idx].size >> 9, |
| 3247 | card->part[idx].force_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3248 | card->part[idx].name, |
| 3249 | card->part[idx].area_type); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 3250 | if (ret) |
| 3251 | return ret; |
| 3252 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | return ret; |
| 3256 | } |
| 3257 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3258 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 3259 | { |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3260 | struct mmc_card *card; |
| 3261 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3262 | if (md) { |
Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 3263 | /* |
| 3264 | * Flush remaining requests and free queues. It |
| 3265 | * is freeing the queue that stops new requests |
| 3266 | * from being accepted. |
| 3267 | */ |
Franck Jullien | 8efb83a | 2013-07-24 15:17:48 +0200 | [diff] [blame] | 3268 | card = md->queue.card; |
Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 3269 | mmc_cleanup_queue(&md->queue); |
| 3270 | if (md->flags & MMC_BLK_PACKED_CMD) |
| 3271 | mmc_packed_clean(&md->queue); |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 3272 | device_remove_file(disk_to_dev(md->disk), |
| 3273 | &md->num_wr_reqs_to_start_packing); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3274 | if (md->disk->flags & GENHD_FL_UP) { |
| 3275 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3276 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 3277 | card->ext_csd.boot_ro_lockable) |
| 3278 | device_remove_file(disk_to_dev(md->disk), |
| 3279 | &md->power_ro_lock); |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 3280 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 3281 | device_remove_file(disk_to_dev(md->disk), |
| 3282 | &dev_attr_max_write_speed); |
| 3283 | device_remove_file(disk_to_dev(md->disk), |
| 3284 | &dev_attr_max_read_speed); |
| 3285 | device_remove_file(disk_to_dev(md->disk), |
| 3286 | &dev_attr_cache_size); |
| 3287 | #endif |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3288 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3289 | del_gendisk(md->disk); |
| 3290 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3291 | mmc_blk_put(md); |
| 3292 | } |
| 3293 | } |
| 3294 | |
| 3295 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 3296 | struct mmc_blk_data *md) |
| 3297 | { |
| 3298 | struct list_head *pos, *q; |
| 3299 | struct mmc_blk_data *part_md; |
| 3300 | |
| 3301 | list_for_each_safe(pos, q, &md->part) { |
| 3302 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 3303 | list_del(pos); |
| 3304 | mmc_blk_remove_req(part_md); |
| 3305 | } |
| 3306 | } |
| 3307 | |
| 3308 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 3309 | { |
| 3310 | int ret; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3311 | struct mmc_card *card = md->queue.card; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3312 | |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 3313 | device_add_disk(md->parent, md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3314 | md->force_ro.show = force_ro_show; |
| 3315 | md->force_ro.store = force_ro_store; |
Rabin Vincent | 641c318 | 2011-04-23 20:52:58 +0530 | [diff] [blame] | 3316 | sysfs_attr_init(&md->force_ro.attr); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3317 | md->force_ro.attr.name = "force_ro"; |
| 3318 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 3319 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 3320 | if (ret) |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3321 | goto force_ro_fail; |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 3322 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 3323 | atomic_set(&md->queue.max_write_speed, max_write_speed); |
| 3324 | ret = device_create_file(disk_to_dev(md->disk), |
| 3325 | &dev_attr_max_write_speed); |
| 3326 | if (ret) |
| 3327 | goto max_write_speed_fail; |
| 3328 | atomic_set(&md->queue.max_read_speed, max_read_speed); |
| 3329 | ret = device_create_file(disk_to_dev(md->disk), |
| 3330 | &dev_attr_max_read_speed); |
| 3331 | if (ret) |
| 3332 | goto max_read_speed_fail; |
| 3333 | atomic_set(&md->queue.cache_size, cache_size); |
| 3334 | atomic_long_set(&md->queue.cache_used, 0); |
| 3335 | md->queue.cache_jiffies = jiffies; |
| 3336 | ret = device_create_file(disk_to_dev(md->disk), &dev_attr_cache_size); |
| 3337 | if (ret) |
| 3338 | goto cache_size_fail; |
| 3339 | #endif |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3340 | |
| 3341 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 3342 | card->ext_csd.boot_ro_lockable) { |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 3343 | umode_t mode; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3344 | |
| 3345 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) |
| 3346 | mode = S_IRUGO; |
| 3347 | else |
| 3348 | mode = S_IRUGO | S_IWUSR; |
| 3349 | |
| 3350 | md->power_ro_lock.show = power_ro_lock_show; |
| 3351 | md->power_ro_lock.store = power_ro_lock_store; |
Rabin Vincent | 00d9ac0 | 2012-02-01 16:31:56 +0100 | [diff] [blame] | 3352 | sysfs_attr_init(&md->power_ro_lock.attr); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3353 | md->power_ro_lock.attr.mode = mode; |
| 3354 | md->power_ro_lock.attr.name = |
| 3355 | "ro_lock_until_next_power_on"; |
| 3356 | ret = device_create_file(disk_to_dev(md->disk), |
| 3357 | &md->power_ro_lock); |
| 3358 | if (ret) |
| 3359 | goto power_ro_lock_fail; |
| 3360 | } |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 3361 | |
| 3362 | md->num_wr_reqs_to_start_packing.show = |
| 3363 | num_wr_reqs_to_start_packing_show; |
| 3364 | md->num_wr_reqs_to_start_packing.store = |
| 3365 | num_wr_reqs_to_start_packing_store; |
| 3366 | sysfs_attr_init(&md->num_wr_reqs_to_start_packing.attr); |
| 3367 | md->num_wr_reqs_to_start_packing.attr.name = |
| 3368 | "num_wr_reqs_to_start_packing"; |
| 3369 | md->num_wr_reqs_to_start_packing.attr.mode = S_IRUGO | S_IWUSR; |
| 3370 | ret = device_create_file(disk_to_dev(md->disk), |
| 3371 | &md->num_wr_reqs_to_start_packing); |
| 3372 | if (ret) |
Maya Erez | 1702240 | 2014-12-04 00:15:42 +0200 | [diff] [blame] | 3373 | goto num_wr_reqs_to_start_packing_fail; |
Tatyana Brokhman | c879b06 | 2014-12-03 23:38:06 +0200 | [diff] [blame] | 3374 | |
Maya Erez | 5a8dae1 | 2014-12-04 15:13:59 +0200 | [diff] [blame] | 3375 | md->no_pack_for_random.show = no_pack_for_random_show; |
| 3376 | md->no_pack_for_random.store = no_pack_for_random_store; |
| 3377 | sysfs_attr_init(&md->no_pack_for_random.attr); |
| 3378 | md->no_pack_for_random.attr.name = "no_pack_for_random"; |
| 3379 | md->no_pack_for_random.attr.mode = S_IRUGO | S_IWUSR; |
| 3380 | ret = device_create_file(disk_to_dev(md->disk), |
| 3381 | &md->no_pack_for_random); |
| 3382 | if (ret) |
| 3383 | goto no_pack_for_random_fails; |
| 3384 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3385 | return ret; |
| 3386 | |
Maya Erez | 5a8dae1 | 2014-12-04 15:13:59 +0200 | [diff] [blame] | 3387 | no_pack_for_random_fails: |
| 3388 | device_remove_file(disk_to_dev(md->disk), |
| 3389 | &md->num_wr_reqs_to_start_packing); |
Maya Erez | 1702240 | 2014-12-04 00:15:42 +0200 | [diff] [blame] | 3390 | num_wr_reqs_to_start_packing_fail: |
| 3391 | device_remove_file(disk_to_dev(md->disk), &md->power_ro_lock); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3392 | power_ro_lock_fail: |
Mark Salyzyn | 6904e43 | 2016-01-28 11:12:25 -0800 | [diff] [blame] | 3393 | #ifdef CONFIG_MMC_SIMULATE_MAX_SPEED |
| 3394 | device_remove_file(disk_to_dev(md->disk), &dev_attr_cache_size); |
| 3395 | cache_size_fail: |
| 3396 | device_remove_file(disk_to_dev(md->disk), &dev_attr_max_read_speed); |
| 3397 | max_read_speed_fail: |
| 3398 | device_remove_file(disk_to_dev(md->disk), &dev_attr_max_write_speed); |
| 3399 | max_write_speed_fail: |
| 3400 | #endif |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 3401 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 3402 | force_ro_fail: |
| 3403 | del_gendisk(md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3404 | |
| 3405 | return ret; |
| 3406 | } |
| 3407 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 3408 | static const struct mmc_fixup blk_fixups[] = |
| 3409 | { |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3410 | MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3411 | MMC_QUIRK_INAND_CMD38), |
| 3412 | MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3413 | MMC_QUIRK_INAND_CMD38), |
| 3414 | MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3415 | MMC_QUIRK_INAND_CMD38), |
| 3416 | MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3417 | MMC_QUIRK_INAND_CMD38), |
| 3418 | MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk, |
| 3419 | MMC_QUIRK_INAND_CMD38), |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3420 | |
| 3421 | /* |
| 3422 | * Some MMC cards experience performance degradation with CMD23 |
| 3423 | * instead of CMD12-bounded multiblock transfers. For now we'll |
| 3424 | * black list what's bad... |
| 3425 | * - Certain Toshiba cards. |
| 3426 | * |
| 3427 | * N.B. This doesn't affect SD cards. |
| 3428 | */ |
Yangbo Lu | 7d70d47 | 2015-07-10 11:44:03 +0800 | [diff] [blame] | 3429 | MMC_FIXUP("SDMB-32", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, |
| 3430 | MMC_QUIRK_BLK_NO_CMD23), |
| 3431 | MMC_FIXUP("SDM032", CID_MANFID_SANDISK, CID_OEMID_ANY, add_quirk_mmc, |
| 3432 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3433 | MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3434 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3435 | MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3436 | MMC_QUIRK_BLK_NO_CMD23), |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3437 | MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 3438 | MMC_QUIRK_BLK_NO_CMD23), |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 3439 | |
| 3440 | /* |
Matt Gumbel | 32ecd32 | 2016-05-20 10:33:46 +0300 | [diff] [blame] | 3441 | * 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] | 3442 | */ |
Chris Ball | c59d447 | 2011-11-11 22:01:43 -0500 | [diff] [blame] | 3443 | 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] | 3444 | MMC_QUIRK_LONG_READ_TIME), |
Matt Gumbel | 32ecd32 | 2016-05-20 10:33:46 +0300 | [diff] [blame] | 3445 | MMC_FIXUP("008GE0", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc, |
| 3446 | MMC_QUIRK_LONG_READ_TIME), |
Stefan Nilsson XK | 6de5fc9 | 2011-11-03 09:44:12 +0100 | [diff] [blame] | 3447 | |
Ian Chen | 3550ccd | 2012-08-29 15:05:36 +0900 | [diff] [blame] | 3448 | /* |
Guoping Yu | 3c984a9 | 2014-08-06 12:44:55 +0800 | [diff] [blame] | 3449 | * Some Samsung MMC cards need longer data read timeout than |
| 3450 | * indicated in CSD. |
| 3451 | */ |
| 3452 | MMC_FIXUP("Q7XSAB", CID_MANFID_SAMSUNG, 0x100, add_quirk_mmc, |
| 3453 | MMC_QUIRK_LONG_READ_TIME), |
| 3454 | |
| 3455 | /* |
Ian Chen | 3550ccd | 2012-08-29 15:05:36 +0900 | [diff] [blame] | 3456 | * On these Samsung MoviNAND parts, performing secure erase or |
| 3457 | * secure trim can result in unrecoverable corruption due to a |
| 3458 | * firmware bug. |
| 3459 | */ |
| 3460 | MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3461 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3462 | MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3463 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3464 | MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3465 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3466 | MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3467 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3468 | MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3469 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3470 | MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3471 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3472 | MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3473 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3474 | MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc, |
| 3475 | MMC_QUIRK_SEC_ERASE_TRIM_BROKEN), |
| 3476 | |
Shawn Lin | b5b4ff0 | 2015-08-12 13:08:32 +0800 | [diff] [blame] | 3477 | /* |
| 3478 | * On Some Kingston eMMCs, performing trim can result in |
| 3479 | * unrecoverable data conrruption occasionally due to a firmware bug. |
| 3480 | */ |
| 3481 | MMC_FIXUP("V10008", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, |
| 3482 | MMC_QUIRK_TRIM_BROKEN), |
| 3483 | MMC_FIXUP("V10016", CID_MANFID_KINGSTON, CID_OEMID_ANY, add_quirk_mmc, |
| 3484 | MMC_QUIRK_TRIM_BROKEN), |
| 3485 | |
Pratibhasagar V | 8d664e3 | 2014-12-03 18:26:42 +0200 | [diff] [blame] | 3486 | /* Some INAND MCP devices advertise incorrect timeout values */ |
| 3487 | MMC_FIXUP("SEM04G", 0x45, CID_OEMID_ANY, add_quirk_mmc, |
| 3488 | MMC_QUIRK_INAND_DATA_TIMEOUT), |
| 3489 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 3490 | END_FIXUP |
| 3491 | }; |
| 3492 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3493 | static int mmc_blk_probe(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3495 | struct mmc_blk_data *md, *part_md; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3496 | char cap_str[10]; |
| 3497 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 3498 | /* |
| 3499 | * Check that the card supports the command class(es) we need. |
| 3500 | */ |
| 3501 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3502 | return -ENODEV; |
| 3503 | |
Lukas Czerner | 5204d00 | 2014-06-18 13:18:07 +0200 | [diff] [blame] | 3504 | mmc_fixup_device(card, blk_fixups); |
| 3505 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3506 | md = mmc_blk_alloc(card); |
| 3507 | if (IS_ERR(md)) |
| 3508 | return PTR_ERR(md); |
| 3509 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 3510 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3511 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 3512 | pr_info("%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3513 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 3514 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3515 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3516 | if (mmc_blk_alloc_parts(card, md)) |
| 3517 | goto out; |
| 3518 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3519 | dev_set_drvdata(&card->dev, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 3520 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3521 | if (mmc_add_disk(md)) |
| 3522 | goto out; |
| 3523 | |
| 3524 | list_for_each_entry(part_md, &md->part, part) { |
| 3525 | if (mmc_add_disk(part_md)) |
| 3526 | goto out; |
| 3527 | } |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3528 | |
| 3529 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); |
| 3530 | pm_runtime_use_autosuspend(&card->dev); |
| 3531 | |
| 3532 | /* |
| 3533 | * Don't enable runtime PM for SD-combo cards here. Leave that |
| 3534 | * decision to be taken during the SDIO init sequence instead. |
| 3535 | */ |
| 3536 | if (card->type != MMC_TYPE_SD_COMBO) { |
| 3537 | pm_runtime_set_active(&card->dev); |
| 3538 | pm_runtime_enable(&card->dev); |
| 3539 | } |
| 3540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3541 | return 0; |
| 3542 | |
| 3543 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3544 | mmc_blk_remove_parts(card, md); |
| 3545 | mmc_blk_remove_req(md); |
Ulf Hansson | 5865f28 | 2012-03-22 11:47:26 +0100 | [diff] [blame] | 3546 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | } |
| 3548 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3549 | static void mmc_blk_remove(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3550 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3551 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3553 | mmc_blk_remove_parts(card, md); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3554 | pm_runtime_get_sync(&card->dev); |
Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 3555 | mmc_claim_host(card->host); |
| 3556 | mmc_blk_part_switch(card, md); |
| 3557 | mmc_release_host(card->host); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 3558 | if (card->type != MMC_TYPE_SD_COMBO) |
| 3559 | pm_runtime_disable(&card->dev); |
| 3560 | pm_runtime_put_noidle(&card->dev); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3561 | mmc_blk_remove_req(md); |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3562 | dev_set_drvdata(&card->dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | } |
| 3564 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3565 | static int _mmc_blk_suspend(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3566 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3567 | struct mmc_blk_data *part_md; |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3568 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3569 | int rc = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3570 | |
| 3571 | if (md) { |
Subhash Jadavani | 4893b39 | 2013-06-20 18:15:50 +0530 | [diff] [blame] | 3572 | rc = mmc_queue_suspend(&md->queue, 0); |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3573 | if (rc) |
| 3574 | goto out; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3575 | list_for_each_entry(part_md, &md->part, part) { |
Subhash Jadavani | 4893b39 | 2013-06-20 18:15:50 +0530 | [diff] [blame] | 3576 | rc = mmc_queue_suspend(&part_md->queue, 0); |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3577 | if (rc) |
| 3578 | goto out_resume; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3580 | } |
Subhash Jadavani | 5cd341a | 2013-02-26 17:32:58 +0530 | [diff] [blame] | 3581 | goto out; |
| 3582 | |
| 3583 | out_resume: |
| 3584 | mmc_queue_resume(&md->queue); |
| 3585 | list_for_each_entry(part_md, &md->part, part) { |
| 3586 | mmc_queue_resume(&part_md->queue); |
| 3587 | } |
| 3588 | out: |
| 3589 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3590 | } |
| 3591 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3592 | static void mmc_blk_shutdown(struct mmc_card *card) |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3593 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3594 | _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 | #ifdef CONFIG_PM_SLEEP |
| 3598 | static int mmc_blk_suspend(struct device *dev) |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3599 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3600 | struct mmc_card *card = mmc_dev_to_card(dev); |
| 3601 | |
| 3602 | return _mmc_blk_suspend(card); |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3603 | } |
| 3604 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 3605 | static int mmc_blk_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3607 | struct mmc_blk_data *part_md; |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 3608 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3609 | |
| 3610 | if (md) { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3611 | /* |
| 3612 | * Resume involves the card going into idle state, |
| 3613 | * so current partition is always the main one. |
| 3614 | */ |
| 3615 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3616 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 3617 | list_for_each_entry(part_md, &md->part, part) { |
| 3618 | mmc_queue_resume(&part_md->queue); |
| 3619 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3620 | } |
| 3621 | return 0; |
| 3622 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3623 | #endif |
| 3624 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 3625 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
| 3626 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 3627 | static struct mmc_driver mmc_driver = { |
| 3628 | .drv = { |
| 3629 | .name = "mmcblk", |
| 3630 | .pm = &mmc_blk_pm_ops, |
| 3631 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3632 | .probe = mmc_blk_probe, |
| 3633 | .remove = mmc_blk_remove, |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 3634 | .shutdown = mmc_blk_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3635 | }; |
| 3636 | |
| 3637 | static int __init mmc_blk_init(void) |
| 3638 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3639 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3640 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 3641 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 3642 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 3643 | |
Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 3644 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 3645 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 3646 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 3647 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3650 | res = mmc_register_driver(&mmc_driver); |
| 3651 | if (res) |
| 3652 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3653 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 3654 | return 0; |
| 3655 | out2: |
| 3656 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3657 | out: |
| 3658 | return res; |
| 3659 | } |
| 3660 | |
| 3661 | static void __exit mmc_blk_exit(void) |
| 3662 | { |
| 3663 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 3664 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | } |
| 3666 | |
| 3667 | module_init(mmc_blk_init); |
| 3668 | module_exit(mmc_blk_exit); |
| 3669 | |
| 3670 | MODULE_LICENSE("GPL"); |
| 3671 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 3672 | |