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