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