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