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