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> |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 31 | #include <linux/cdev.h> |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 32 | #include <linux/mutex.h> |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 33 | #include <linux/scatterlist.h> |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 34 | #include <linux/string_helpers.h> |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 35 | #include <linux/delay.h> |
| 36 | #include <linux/capability.h> |
| 37 | #include <linux/compat.h> |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 38 | #include <linux/pm_runtime.h> |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 39 | #include <linux/idr.h> |
Linus Walleij | 627c3cc | 2017-08-20 23:39:08 +0200 | [diff] [blame] | 40 | #include <linux/debugfs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 42 | #include <linux/mmc/ioctl.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | #include <linux/mmc/card.h> |
Pierre Ossman | 385e3227 | 2006-06-18 14:34:37 +0200 | [diff] [blame] | 44 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 45 | #include <linux/mmc/mmc.h> |
| 46 | #include <linux/mmc/sd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 48 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 50 | #include "queue.h" |
Baoyou Xie | 48ab086 | 2016-09-30 09:37:38 +0800 | [diff] [blame] | 51 | #include "block.h" |
Ulf Hansson | 55244c5 | 2017-01-13 14:14:08 +0100 | [diff] [blame] | 52 | #include "core.h" |
Ulf Hansson | 4facdde | 2017-01-13 14:14:14 +0100 | [diff] [blame] | 53 | #include "card.h" |
Ulf Hansson | 5857b29 | 2017-01-13 14:14:15 +0100 | [diff] [blame] | 54 | #include "host.h" |
Ulf Hansson | 4facdde | 2017-01-13 14:14:14 +0100 | [diff] [blame] | 55 | #include "bus.h" |
Ulf Hansson | 55244c5 | 2017-01-13 14:14:08 +0100 | [diff] [blame] | 56 | #include "mmc_ops.h" |
Shawn Lin | 28fc64a | 2017-02-15 16:35:28 +0800 | [diff] [blame] | 57 | #include "quirks.h" |
Ulf Hansson | 55244c5 | 2017-01-13 14:14:08 +0100 | [diff] [blame] | 58 | #include "sd_ops.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Andy Whitcroft | 6b0b628 | 2009-02-23 12:38:41 +0000 | [diff] [blame] | 60 | MODULE_ALIAS("mmc:block"); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 61 | #ifdef MODULE_PARAM_PREFIX |
| 62 | #undef MODULE_PARAM_PREFIX |
| 63 | #endif |
| 64 | #define MODULE_PARAM_PREFIX "mmcblk." |
David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 65 | |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 66 | #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute 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) |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 69 | |
Luca Porzio | d3df046 | 2015-11-06 15:12:26 +0000 | [diff] [blame] | 70 | #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \ |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 71 | (rq_data_dir(req) == WRITE)) |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 72 | static DEFINE_MUTEX(block_mutex); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * The defaults come from config options but can be overriden by module |
| 76 | * or bootarg options. |
| 77 | */ |
| 78 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
| 79 | |
| 80 | /* |
| 81 | * We've only got one major, so number of mmcblk devices is |
Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 82 | * limited to (1 << 20) / number of minors per device. It is also |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 83 | * limited by the MAX_DEVICES below. |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 84 | */ |
| 85 | static int max_devices; |
| 86 | |
Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 87 | #define MAX_DEVICES 256 |
| 88 | |
Ulf Hansson | b10fa99 | 2016-04-07 14:36:46 +0200 | [diff] [blame] | 89 | static DEFINE_IDA(mmc_blk_ida); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 90 | static DEFINE_IDA(mmc_rpmb_ida); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | /* |
| 93 | * There is one mmc_blk_data per slot. |
| 94 | */ |
| 95 | struct mmc_blk_data { |
| 96 | spinlock_t lock; |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 97 | struct device *parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | struct gendisk *disk; |
| 99 | struct mmc_queue queue; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 100 | struct list_head part; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 101 | struct list_head rpmbs; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 103 | unsigned int flags; |
| 104 | #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */ |
| 105 | #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */ |
| 106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | unsigned int usage; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 108 | unsigned int read_only; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 109 | unsigned int part_type; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 110 | unsigned int reset_done; |
| 111 | #define MMC_BLK_READ BIT(0) |
| 112 | #define MMC_BLK_WRITE BIT(1) |
| 113 | #define MMC_BLK_DISCARD BIT(2) |
| 114 | #define MMC_BLK_SECDISCARD BIT(3) |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Only set in main mmc_blk_data associated |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 118 | * with mmc_card with dev_set_drvdata, and keeps |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 119 | * track of the current selected device partition. |
| 120 | */ |
| 121 | unsigned int part_curr; |
| 122 | struct device_attribute force_ro; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 123 | struct device_attribute power_ro_lock; |
| 124 | int area_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | }; |
| 126 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 127 | /* Device type for RPMB character devices */ |
| 128 | static dev_t mmc_rpmb_devt; |
| 129 | |
| 130 | /* Bus type for RPMB character devices */ |
| 131 | static struct bus_type mmc_rpmb_bus_type = { |
| 132 | .name = "mmc_rpmb", |
| 133 | }; |
| 134 | |
| 135 | /** |
| 136 | * struct mmc_rpmb_data - special RPMB device type for these areas |
| 137 | * @dev: the device for the RPMB area |
| 138 | * @chrdev: character device for the RPMB area |
| 139 | * @id: unique device ID number |
| 140 | * @part_index: partition index (0 on first) |
| 141 | * @md: parent MMC block device |
| 142 | * @node: list item, so we can put this device on a list |
| 143 | */ |
| 144 | struct mmc_rpmb_data { |
| 145 | struct device dev; |
| 146 | struct cdev chrdev; |
| 147 | int id; |
| 148 | unsigned int part_index; |
| 149 | struct mmc_blk_data *md; |
| 150 | struct list_head node; |
| 151 | }; |
| 152 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 153 | static DEFINE_MUTEX(open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 155 | module_param(perdev_minors, int, 0444); |
| 156 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); |
| 157 | |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 158 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 159 | unsigned int part_type); |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
| 162 | { |
| 163 | struct mmc_blk_data *md; |
| 164 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 165 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | md = disk->private_data; |
| 167 | if (md && md->usage == 0) |
| 168 | md = NULL; |
| 169 | if (md) |
| 170 | md->usage++; |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 171 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | return md; |
| 174 | } |
| 175 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 176 | static inline int mmc_get_devidx(struct gendisk *disk) |
| 177 | { |
Colin Cross | 382c55f | 2015-10-22 10:00:41 -0700 | [diff] [blame] | 178 | int devidx = disk->first_minor / perdev_minors; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 179 | return devidx; |
| 180 | } |
| 181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | static void mmc_blk_put(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->usage--; |
| 186 | if (md->usage == 0) { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 187 | int devidx = mmc_get_devidx(md->disk); |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 188 | blk_cleanup_queue(md->queue.queue); |
Heiner Kallweit | a04848c | 2017-02-01 19:44:22 +0100 | [diff] [blame] | 189 | ida_simple_remove(&mmc_blk_ida, devidx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | put_disk(md->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | kfree(md); |
| 192 | } |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 193 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 196 | static ssize_t power_ro_lock_show(struct device *dev, |
| 197 | struct device_attribute *attr, char *buf) |
| 198 | { |
| 199 | int ret; |
| 200 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 201 | struct mmc_card *card = md->queue.card; |
| 202 | int locked = 0; |
| 203 | |
| 204 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN) |
| 205 | locked = 2; |
| 206 | else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN) |
| 207 | locked = 1; |
| 208 | |
| 209 | ret = snprintf(buf, PAGE_SIZE, "%d\n", locked); |
| 210 | |
Tomas Winkler | 9098f84 | 2015-07-16 15:50:45 +0200 | [diff] [blame] | 211 | mmc_blk_put(md); |
| 212 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 213 | return ret; |
| 214 | } |
| 215 | |
| 216 | static ssize_t power_ro_lock_store(struct device *dev, |
| 217 | struct device_attribute *attr, const char *buf, size_t count) |
| 218 | { |
| 219 | int ret; |
| 220 | struct mmc_blk_data *md, *part_md; |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 221 | struct mmc_queue *mq; |
| 222 | struct request *req; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 223 | unsigned long set; |
| 224 | |
| 225 | if (kstrtoul(buf, 0, &set)) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | if (set != 1) |
| 229 | return count; |
| 230 | |
| 231 | md = mmc_blk_get(dev_to_disk(dev)); |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 232 | mq = &md->queue; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 233 | |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 234 | /* Dispatch locking to the block layer */ |
| 235 | req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM); |
| 236 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP; |
| 237 | blk_execute_rq(mq->queue, NULL, req, 0); |
| 238 | ret = req_to_mmc_queue_req(req)->drv_op_result; |
Adrian Hunter | 34c089e | 2017-11-21 15:42:27 +0200 | [diff] [blame^] | 239 | blk_put_request(req); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 240 | |
| 241 | if (!ret) { |
| 242 | pr_info("%s: Locking boot partition ro until next power on\n", |
| 243 | md->disk->disk_name); |
| 244 | set_disk_ro(md->disk, 1); |
| 245 | |
| 246 | list_for_each_entry(part_md, &md->part, part) |
| 247 | if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) { |
| 248 | pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name); |
| 249 | set_disk_ro(part_md->disk, 1); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | mmc_blk_put(md); |
| 254 | return count; |
| 255 | } |
| 256 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 257 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, |
| 258 | char *buf) |
| 259 | { |
| 260 | int ret; |
| 261 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 262 | |
Baruch Siach | 0031a98 | 2014-09-22 10:12:51 +0300 | [diff] [blame] | 263 | ret = snprintf(buf, PAGE_SIZE, "%d\n", |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 264 | get_disk_ro(dev_to_disk(dev)) ^ |
| 265 | md->read_only); |
| 266 | mmc_blk_put(md); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, |
| 271 | const char *buf, size_t count) |
| 272 | { |
| 273 | int ret; |
| 274 | char *end; |
| 275 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 276 | unsigned long set = simple_strtoul(buf, &end, 0); |
| 277 | if (end == buf) { |
| 278 | ret = -EINVAL; |
| 279 | goto out; |
| 280 | } |
| 281 | |
| 282 | set_disk_ro(dev_to_disk(dev), set || md->read_only); |
| 283 | ret = count; |
| 284 | out: |
| 285 | mmc_blk_put(md); |
| 286 | return ret; |
| 287 | } |
| 288 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 289 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 291 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | int ret = -ENXIO; |
| 293 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 294 | mutex_lock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | if (md) { |
| 296 | if (md->usage == 2) |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 297 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | ret = 0; |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 299 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 300 | if ((mode & FMODE_WRITE) && md->read_only) { |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 301 | mmc_blk_put(md); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 302 | ret = -EROFS; |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 303 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | } |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 305 | mutex_unlock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
| 307 | return ret; |
| 308 | } |
| 309 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 310 | static void mmc_blk_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 312 | struct mmc_blk_data *md = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 314 | mutex_lock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | mmc_blk_put(md); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 316 | mutex_unlock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 320 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 321 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 322 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
| 323 | geo->heads = 4; |
| 324 | geo->sectors = 16; |
| 325 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | } |
| 327 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 328 | struct mmc_blk_ioc_data { |
| 329 | struct mmc_ioc_cmd ic; |
| 330 | unsigned char *buf; |
| 331 | u64 buf_bytes; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 332 | struct mmc_rpmb_data *rpmb; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user( |
| 336 | struct mmc_ioc_cmd __user *user) |
| 337 | { |
| 338 | struct mmc_blk_ioc_data *idata; |
| 339 | int err; |
| 340 | |
yalin wang | 1ff8950 | 2015-11-12 19:27:11 +0800 | [diff] [blame] | 341 | idata = kmalloc(sizeof(*idata), GFP_KERNEL); |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 342 | if (!idata) { |
| 343 | err = -ENOMEM; |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 344 | goto out; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 345 | } |
| 346 | |
| 347 | if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) { |
| 348 | err = -EFAULT; |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 349 | goto idata_err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks; |
| 353 | if (idata->buf_bytes > MMC_IOC_MAX_BYTES) { |
| 354 | err = -EOVERFLOW; |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 355 | goto idata_err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 356 | } |
| 357 | |
Ville Viinikka | bfe5b1b | 2016-07-08 18:27:02 +0300 | [diff] [blame] | 358 | if (!idata->buf_bytes) { |
| 359 | idata->buf = NULL; |
Johan Rudholm | 4d6144d | 2011-11-23 09:05:58 +0100 | [diff] [blame] | 360 | return idata; |
Ville Viinikka | bfe5b1b | 2016-07-08 18:27:02 +0300 | [diff] [blame] | 361 | } |
Johan Rudholm | 4d6144d | 2011-11-23 09:05:58 +0100 | [diff] [blame] | 362 | |
yalin wang | 1ff8950 | 2015-11-12 19:27:11 +0800 | [diff] [blame] | 363 | idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL); |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 364 | if (!idata->buf) { |
| 365 | err = -ENOMEM; |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 366 | goto idata_err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | if (copy_from_user(idata->buf, (void __user *)(unsigned long) |
| 370 | idata->ic.data_ptr, idata->buf_bytes)) { |
| 371 | err = -EFAULT; |
| 372 | goto copy_err; |
| 373 | } |
| 374 | |
| 375 | return idata; |
| 376 | |
| 377 | copy_err: |
| 378 | kfree(idata->buf); |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 379 | idata_err: |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 380 | kfree(idata); |
Vladimir Motyka | aea253e | 2011-05-11 00:00:43 -0400 | [diff] [blame] | 381 | out: |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 382 | return ERR_PTR(err); |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 383 | } |
| 384 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 385 | static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr, |
| 386 | struct mmc_blk_ioc_data *idata) |
| 387 | { |
| 388 | struct mmc_ioc_cmd *ic = &idata->ic; |
| 389 | |
| 390 | if (copy_to_user(&(ic_ptr->response), ic->response, |
| 391 | sizeof(ic->response))) |
| 392 | return -EFAULT; |
| 393 | |
| 394 | if (!idata->ic.write_flag) { |
| 395 | if (copy_to_user((void __user *)(unsigned long)ic->data_ptr, |
| 396 | idata->buf, idata->buf_bytes)) |
| 397 | return -EFAULT; |
| 398 | } |
| 399 | |
| 400 | return 0; |
| 401 | } |
| 402 | |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 403 | static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status, |
| 404 | u32 retries_max) |
| 405 | { |
| 406 | int err; |
| 407 | u32 retry_count = 0; |
| 408 | |
| 409 | if (!status || !retries_max) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | do { |
Ulf Hansson | 2185bc2 | 2017-05-22 10:23:58 +0200 | [diff] [blame] | 413 | err = __mmc_send_status(card, status, 5); |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 414 | if (err) |
| 415 | break; |
| 416 | |
| 417 | if (!R1_STATUS(*status) && |
| 418 | (R1_CURRENT_STATE(*status) != R1_STATE_PRG)) |
| 419 | break; /* RPMB programming operation complete */ |
| 420 | |
| 421 | /* |
| 422 | * Rechedule to give the MMC device a chance to continue |
| 423 | * processing the previous command without being polled too |
| 424 | * frequently. |
| 425 | */ |
| 426 | usleep_range(1000, 5000); |
| 427 | } while (++retry_count < retries_max); |
| 428 | |
| 429 | if (retry_count == retries_max) |
| 430 | err = -EPERM; |
| 431 | |
| 432 | return err; |
| 433 | } |
| 434 | |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 435 | static int ioctl_do_sanitize(struct mmc_card *card) |
| 436 | { |
| 437 | int err; |
| 438 | |
Ulf Hansson | a2d1086 | 2013-12-16 14:37:26 +0100 | [diff] [blame] | 439 | if (!mmc_can_sanitize(card)) { |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 440 | pr_warn("%s: %s - SANITIZE is not supported\n", |
| 441 | mmc_hostname(card->host), __func__); |
| 442 | err = -EOPNOTSUPP; |
| 443 | goto out; |
| 444 | } |
| 445 | |
| 446 | pr_debug("%s: %s - SANITIZE IN PROGRESS...\n", |
| 447 | mmc_hostname(card->host), __func__); |
| 448 | |
| 449 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 450 | EXT_CSD_SANITIZE_START, 1, |
| 451 | MMC_SANITIZE_REQ_TIMEOUT); |
| 452 | |
| 453 | if (err) |
| 454 | pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n", |
| 455 | mmc_hostname(card->host), __func__, err); |
| 456 | |
| 457 | pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host), |
| 458 | __func__); |
| 459 | out: |
| 460 | return err; |
| 461 | } |
| 462 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 463 | static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md, |
| 464 | struct mmc_blk_ioc_data *idata) |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 465 | { |
Masahiro Yamada | c7836d1 | 2016-12-19 20:51:18 +0900 | [diff] [blame] | 466 | struct mmc_command cmd = {}; |
| 467 | struct mmc_data data = {}; |
| 468 | struct mmc_request mrq = {}; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 469 | struct scatterlist sg; |
| 470 | int err; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 471 | unsigned int target_part; |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 472 | u32 status = 0; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 473 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 474 | if (!card || !md || !idata) |
| 475 | return -EINVAL; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 476 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 477 | /* |
| 478 | * The RPMB accesses comes in from the character device, so we |
| 479 | * need to target these explicitly. Else we just target the |
| 480 | * partition type for the block device the ioctl() was issued |
| 481 | * on. |
| 482 | */ |
| 483 | if (idata->rpmb) { |
| 484 | /* Support multiple RPMB partitions */ |
| 485 | target_part = idata->rpmb->part_index; |
| 486 | target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB; |
| 487 | } else { |
| 488 | target_part = md->part_type; |
| 489 | } |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 490 | |
Johan Rudholm | 4d6144d | 2011-11-23 09:05:58 +0100 | [diff] [blame] | 491 | cmd.opcode = idata->ic.opcode; |
| 492 | cmd.arg = idata->ic.arg; |
| 493 | cmd.flags = idata->ic.flags; |
| 494 | |
| 495 | if (idata->buf_bytes) { |
| 496 | data.sg = &sg; |
| 497 | data.sg_len = 1; |
| 498 | data.blksz = idata->ic.blksz; |
| 499 | data.blocks = idata->ic.blocks; |
| 500 | |
| 501 | sg_init_one(data.sg, idata->buf, idata->buf_bytes); |
| 502 | |
| 503 | if (idata->ic.write_flag) |
| 504 | data.flags = MMC_DATA_WRITE; |
| 505 | else |
| 506 | data.flags = MMC_DATA_READ; |
| 507 | |
| 508 | /* data.flags must already be set before doing this. */ |
| 509 | mmc_set_data_timeout(&data, card); |
| 510 | |
| 511 | /* Allow overriding the timeout_ns for empirical tuning. */ |
| 512 | if (idata->ic.data_timeout_ns) |
| 513 | data.timeout_ns = idata->ic.data_timeout_ns; |
| 514 | |
| 515 | if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) { |
| 516 | /* |
| 517 | * Pretend this is a data transfer and rely on the |
| 518 | * host driver to compute timeout. When all host |
| 519 | * drivers support cmd.cmd_timeout for R1B, this |
| 520 | * can be changed to: |
| 521 | * |
| 522 | * mrq.data = NULL; |
| 523 | * cmd.cmd_timeout = idata->ic.cmd_timeout_ms; |
| 524 | */ |
| 525 | data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000; |
| 526 | } |
| 527 | |
| 528 | mrq.data = &data; |
| 529 | } |
| 530 | |
| 531 | mrq.cmd = &cmd; |
| 532 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 533 | err = mmc_blk_part_switch(card, target_part); |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 534 | if (err) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 535 | return err; |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 536 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 537 | if (idata->ic.is_acmd) { |
| 538 | err = mmc_app_cmd(card->host, card); |
| 539 | if (err) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 540 | return err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 543 | if (idata->rpmb) { |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 544 | err = mmc_set_blockcount(card, data.blocks, |
| 545 | idata->ic.write_flag & (1 << 31)); |
| 546 | if (err) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 547 | return err; |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 548 | } |
| 549 | |
Yaniv Gardi | a82e484 | 2013-06-05 14:13:08 +0300 | [diff] [blame] | 550 | if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) && |
| 551 | (cmd.opcode == MMC_SWITCH)) { |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 552 | err = ioctl_do_sanitize(card); |
| 553 | |
| 554 | if (err) |
| 555 | pr_err("%s: ioctl_do_sanitize() failed. err = %d", |
| 556 | __func__, err); |
| 557 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 558 | return err; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 559 | } |
| 560 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 561 | mmc_wait_for_req(card->host, &mrq); |
| 562 | |
| 563 | if (cmd.error) { |
| 564 | dev_err(mmc_dev(card->host), "%s: cmd error %d\n", |
| 565 | __func__, cmd.error); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 566 | return cmd.error; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 567 | } |
| 568 | if (data.error) { |
| 569 | dev_err(mmc_dev(card->host), "%s: data error %d\n", |
| 570 | __func__, data.error); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 571 | return data.error; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | /* |
| 575 | * According to the SD specs, some commands require a delay after |
| 576 | * issuing the command. |
| 577 | */ |
| 578 | if (idata->ic.postsleep_min_us) |
| 579 | usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us); |
| 580 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 581 | memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp)); |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 582 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 583 | if (idata->rpmb) { |
Loic Pallardy | 8d1e977 | 2012-08-06 17:12:31 +0200 | [diff] [blame] | 584 | /* |
| 585 | * Ensure RPMB command has completed by polling CMD13 |
| 586 | * "Send Status". |
| 587 | */ |
| 588 | err = ioctl_rpmb_card_status_poll(card, &status, 5); |
| 589 | if (err) |
| 590 | dev_err(mmc_dev(card->host), |
| 591 | "%s: Card Status=0x%08X, error %d\n", |
| 592 | __func__, status, err); |
| 593 | } |
| 594 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 595 | return err; |
| 596 | } |
| 597 | |
Linus Walleij | 2fe20ba | 2017-08-20 23:39:11 +0200 | [diff] [blame] | 598 | static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md, |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 599 | struct mmc_ioc_cmd __user *ic_ptr, |
| 600 | struct mmc_rpmb_data *rpmb) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 601 | { |
| 602 | struct mmc_blk_ioc_data *idata; |
Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 603 | struct mmc_blk_ioc_data *idatas[1]; |
Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 604 | struct mmc_queue *mq; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 605 | struct mmc_card *card; |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 606 | int err = 0, ioc_err = 0; |
Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 607 | struct request *req; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 608 | |
| 609 | idata = mmc_blk_ioctl_copy_from_user(ic_ptr); |
| 610 | if (IS_ERR(idata)) |
| 611 | return PTR_ERR(idata); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 612 | /* This will be NULL on non-RPMB ioctl():s */ |
| 613 | idata->rpmb = rpmb; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 614 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 615 | card = md->queue.card; |
| 616 | if (IS_ERR(card)) { |
| 617 | err = PTR_ERR(card); |
| 618 | goto cmd_done; |
| 619 | } |
| 620 | |
Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 621 | /* |
| 622 | * Dispatch the ioctl() into the block request queue. |
| 623 | */ |
| 624 | mq = &md->queue; |
| 625 | req = blk_get_request(mq->queue, |
| 626 | idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, |
| 627 | __GFP_RECLAIM); |
Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 628 | idatas[0] = idata; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 629 | req_to_mmc_queue_req(req)->drv_op = |
| 630 | rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL; |
Linus Walleij | 69f7599 | 2017-08-20 23:39:06 +0200 | [diff] [blame] | 631 | req_to_mmc_queue_req(req)->drv_op_data = idatas; |
Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 632 | req_to_mmc_queue_req(req)->ioc_count = 1; |
Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 633 | blk_execute_rq(mq->queue, NULL, req, 0); |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 634 | ioc_err = req_to_mmc_queue_req(req)->drv_op_result; |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 635 | err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata); |
Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 636 | blk_put_request(req); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 637 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 638 | cmd_done: |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 639 | kfree(idata->buf); |
| 640 | kfree(idata); |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 641 | return ioc_err ? ioc_err : err; |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 642 | } |
| 643 | |
Linus Walleij | 2fe20ba | 2017-08-20 23:39:11 +0200 | [diff] [blame] | 644 | static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md, |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 645 | struct mmc_ioc_multi_cmd __user *user, |
| 646 | struct mmc_rpmb_data *rpmb) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 647 | { |
| 648 | struct mmc_blk_ioc_data **idata = NULL; |
| 649 | struct mmc_ioc_cmd __user *cmds = user->cmds; |
| 650 | struct mmc_card *card; |
Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 651 | struct mmc_queue *mq; |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 652 | int i, err = 0, ioc_err = 0; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 653 | __u64 num_of_cmds; |
Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 654 | struct request *req; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 655 | |
| 656 | if (copy_from_user(&num_of_cmds, &user->num_of_cmds, |
| 657 | sizeof(num_of_cmds))) |
| 658 | return -EFAULT; |
| 659 | |
Geert Uytterhoeven | aab2ee0 | 2017-07-05 17:09:42 +0200 | [diff] [blame] | 660 | if (!num_of_cmds) |
| 661 | return 0; |
| 662 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 663 | if (num_of_cmds > MMC_IOC_MAX_CMDS) |
| 664 | return -EINVAL; |
| 665 | |
| 666 | idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL); |
| 667 | if (!idata) |
| 668 | return -ENOMEM; |
| 669 | |
| 670 | for (i = 0; i < num_of_cmds; i++) { |
| 671 | idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]); |
| 672 | if (IS_ERR(idata[i])) { |
| 673 | err = PTR_ERR(idata[i]); |
| 674 | num_of_cmds = i; |
| 675 | goto cmd_err; |
| 676 | } |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 677 | /* This will be NULL on non-RPMB ioctl():s */ |
| 678 | idata[i]->rpmb = rpmb; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 681 | card = md->queue.card; |
| 682 | if (IS_ERR(card)) { |
| 683 | err = PTR_ERR(card); |
Linus Walleij | 2fe20ba | 2017-08-20 23:39:11 +0200 | [diff] [blame] | 684 | goto cmd_err; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 687 | |
Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 688 | /* |
| 689 | * Dispatch the ioctl()s into the block request queue. |
| 690 | */ |
| 691 | mq = &md->queue; |
| 692 | req = blk_get_request(mq->queue, |
| 693 | idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, |
| 694 | __GFP_RECLAIM); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 695 | req_to_mmc_queue_req(req)->drv_op = |
| 696 | rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL; |
Linus Walleij | 69f7599 | 2017-08-20 23:39:06 +0200 | [diff] [blame] | 697 | req_to_mmc_queue_req(req)->drv_op_data = idata; |
Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 698 | req_to_mmc_queue_req(req)->ioc_count = num_of_cmds; |
| 699 | blk_execute_rq(mq->queue, NULL, req, 0); |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 700 | ioc_err = req_to_mmc_queue_req(req)->drv_op_result; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 701 | |
| 702 | /* copy to user if data and response */ |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 703 | for (i = 0; i < num_of_cmds && !err; i++) |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 704 | err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]); |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 705 | |
Linus Walleij | 3ecd8cf | 2017-05-18 11:29:35 +0200 | [diff] [blame] | 706 | blk_put_request(req); |
| 707 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 708 | cmd_err: |
| 709 | for (i = 0; i < num_of_cmds; i++) { |
| 710 | kfree(idata[i]->buf); |
| 711 | kfree(idata[i]); |
| 712 | } |
| 713 | kfree(idata); |
Grant Grundler | b093410 | 2015-09-23 18:30:33 -0700 | [diff] [blame] | 714 | return ioc_err ? ioc_err : err; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 715 | } |
| 716 | |
Linus Walleij | 61fe0e2 | 2017-08-20 23:39:09 +0200 | [diff] [blame] | 717 | static int mmc_blk_check_blkdev(struct block_device *bdev) |
| 718 | { |
| 719 | /* |
| 720 | * The caller must have CAP_SYS_RAWIO, and must be calling this on the |
| 721 | * whole block device, not on a partition. This prevents overspray |
| 722 | * between sibling partitions. |
| 723 | */ |
| 724 | if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains)) |
| 725 | return -EPERM; |
| 726 | return 0; |
| 727 | } |
| 728 | |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 729 | static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode, |
| 730 | unsigned int cmd, unsigned long arg) |
| 731 | { |
Linus Walleij | 2fe20ba | 2017-08-20 23:39:11 +0200 | [diff] [blame] | 732 | struct mmc_blk_data *md; |
Linus Walleij | 61fe0e2 | 2017-08-20 23:39:09 +0200 | [diff] [blame] | 733 | int ret; |
| 734 | |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 735 | switch (cmd) { |
| 736 | case MMC_IOC_CMD: |
Linus Walleij | 61fe0e2 | 2017-08-20 23:39:09 +0200 | [diff] [blame] | 737 | ret = mmc_blk_check_blkdev(bdev); |
| 738 | if (ret) |
| 739 | return ret; |
Linus Walleij | 2fe20ba | 2017-08-20 23:39:11 +0200 | [diff] [blame] | 740 | md = mmc_blk_get(bdev->bd_disk); |
| 741 | if (!md) |
| 742 | return -EINVAL; |
| 743 | ret = mmc_blk_ioctl_cmd(md, |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 744 | (struct mmc_ioc_cmd __user *)arg, |
| 745 | NULL); |
Linus Walleij | 2fe20ba | 2017-08-20 23:39:11 +0200 | [diff] [blame] | 746 | mmc_blk_put(md); |
| 747 | return ret; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 748 | case MMC_IOC_MULTI_CMD: |
Linus Walleij | 61fe0e2 | 2017-08-20 23:39:09 +0200 | [diff] [blame] | 749 | ret = mmc_blk_check_blkdev(bdev); |
| 750 | if (ret) |
| 751 | return ret; |
Linus Walleij | 2fe20ba | 2017-08-20 23:39:11 +0200 | [diff] [blame] | 752 | md = mmc_blk_get(bdev->bd_disk); |
| 753 | if (!md) |
| 754 | return -EINVAL; |
| 755 | ret = mmc_blk_ioctl_multi_cmd(md, |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 756 | (struct mmc_ioc_multi_cmd __user *)arg, |
| 757 | NULL); |
Linus Walleij | 2fe20ba | 2017-08-20 23:39:11 +0200 | [diff] [blame] | 758 | mmc_blk_put(md); |
| 759 | return ret; |
Jon Hunter | a5f5774 | 2015-09-22 10:27:53 +0100 | [diff] [blame] | 760 | default: |
| 761 | return -EINVAL; |
| 762 | } |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | #ifdef CONFIG_COMPAT |
| 766 | static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode, |
| 767 | unsigned int cmd, unsigned long arg) |
| 768 | { |
| 769 | return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg)); |
| 770 | } |
| 771 | #endif |
| 772 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 773 | static const struct block_device_operations mmc_bdops = { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 774 | .open = mmc_blk_open, |
| 775 | .release = mmc_blk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 776 | .getgeo = mmc_blk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | .owner = THIS_MODULE, |
John Calixto | cb87ea2 | 2011-04-26 18:56:29 -0400 | [diff] [blame] | 778 | .ioctl = mmc_blk_ioctl, |
| 779 | #ifdef CONFIG_COMPAT |
| 780 | .compat_ioctl = mmc_blk_compat_ioctl, |
| 781 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | }; |
| 783 | |
Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 784 | static int mmc_blk_part_switch_pre(struct mmc_card *card, |
| 785 | unsigned int part_type) |
| 786 | { |
| 787 | int ret = 0; |
| 788 | |
| 789 | if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { |
| 790 | if (card->ext_csd.cmdq_en) { |
| 791 | ret = mmc_cmdq_disable(card); |
| 792 | if (ret) |
| 793 | return ret; |
| 794 | } |
| 795 | mmc_retune_pause(card->host); |
| 796 | } |
| 797 | |
| 798 | return ret; |
| 799 | } |
| 800 | |
| 801 | static int mmc_blk_part_switch_post(struct mmc_card *card, |
| 802 | unsigned int part_type) |
| 803 | { |
| 804 | int ret = 0; |
| 805 | |
| 806 | if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) { |
| 807 | mmc_retune_unpause(card->host); |
| 808 | if (card->reenable_cmdq && !card->ext_csd.cmdq_en) |
| 809 | ret = mmc_cmdq_enable(card); |
| 810 | } |
| 811 | |
| 812 | return ret; |
| 813 | } |
| 814 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 815 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 816 | unsigned int part_type) |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 817 | { |
Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 818 | int ret = 0; |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 819 | struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev); |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 820 | |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 821 | if (main_md->part_curr == part_type) |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 822 | return 0; |
| 823 | |
| 824 | if (mmc_card_mmc(card)) { |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 825 | u8 part_config = card->ext_csd.part_config; |
| 826 | |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 827 | ret = mmc_blk_part_switch_pre(card, part_type); |
Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 828 | if (ret) |
| 829 | return ret; |
Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 830 | |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 831 | part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 832 | part_config |= part_type; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 833 | |
| 834 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 835 | EXT_CSD_PART_CONFIG, part_config, |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 836 | card->ext_csd.part_time); |
Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 837 | if (ret) { |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 838 | mmc_blk_part_switch_post(card, part_type); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 839 | return ret; |
Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 840 | } |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 841 | |
| 842 | card->ext_csd.part_config = part_config; |
Adrian Hunter | 57da0c0 | 2016-05-04 14:38:13 +0300 | [diff] [blame] | 843 | |
Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 844 | ret = mmc_blk_part_switch_post(card, main_md->part_curr); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 845 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 846 | |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 847 | main_md->part_curr = part_type; |
Adrian Hunter | 025e3d5 | 2017-03-13 14:36:39 +0200 | [diff] [blame] | 848 | return ret; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 849 | } |
| 850 | |
Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 851 | static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks) |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 852 | { |
| 853 | int err; |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 854 | u32 result; |
| 855 | __be32 *blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 856 | |
Masahiro Yamada | c7836d1 | 2016-12-19 20:51:18 +0900 | [diff] [blame] | 857 | struct mmc_request mrq = {}; |
| 858 | struct mmc_command cmd = {}; |
| 859 | struct mmc_data data = {}; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 860 | |
| 861 | struct scatterlist sg; |
| 862 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 863 | cmd.opcode = MMC_APP_CMD; |
| 864 | cmd.arg = card->rca << 16; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 865 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 866 | |
| 867 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 868 | if (err) |
Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 869 | return err; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 870 | if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) |
Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 871 | return -EIO; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 872 | |
| 873 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 874 | |
| 875 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 876 | cmd.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 877 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 878 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 879 | data.blksz = 4; |
| 880 | data.blocks = 1; |
| 881 | data.flags = MMC_DATA_READ; |
| 882 | data.sg = &sg; |
| 883 | data.sg_len = 1; |
Subhash Jadavani | d380443 | 2012-06-13 17:10:43 +0530 | [diff] [blame] | 884 | mmc_set_data_timeout(&data, card); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 885 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 886 | mrq.cmd = &cmd; |
| 887 | mrq.data = &data; |
| 888 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 889 | blocks = kmalloc(4, GFP_KERNEL); |
| 890 | if (!blocks) |
Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 891 | return -ENOMEM; |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 892 | |
| 893 | sg_init_one(&sg, blocks, 4); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 894 | |
| 895 | mmc_wait_for_req(card->host, &mrq); |
| 896 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 897 | result = ntohl(*blocks); |
| 898 | kfree(blocks); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 899 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 900 | if (cmd.error || data.error) |
Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 901 | return -EIO; |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 902 | |
Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 903 | *written_blocks = result; |
| 904 | |
| 905 | return 0; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 906 | } |
| 907 | |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 908 | static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms, |
Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 909 | bool hw_busy_detect, struct request *req, bool *gen_err) |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 910 | { |
| 911 | unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms); |
| 912 | int err = 0; |
| 913 | u32 status; |
| 914 | |
| 915 | do { |
Ulf Hansson | 2185bc2 | 2017-05-22 10:23:58 +0200 | [diff] [blame] | 916 | err = __mmc_send_status(card, &status, 5); |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 917 | if (err) { |
| 918 | pr_err("%s: error %d requesting status\n", |
| 919 | req->rq_disk->disk_name, err); |
| 920 | return err; |
| 921 | } |
| 922 | |
| 923 | if (status & R1_ERROR) { |
| 924 | pr_err("%s: %s: error sending status cmd, status %#x\n", |
| 925 | req->rq_disk->disk_name, __func__, status); |
Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 926 | *gen_err = true; |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 927 | } |
| 928 | |
Ulf Hansson | 95a9129 | 2014-01-29 13:11:27 +0100 | [diff] [blame] | 929 | /* We may rely on the host hw to handle busy detection.*/ |
| 930 | if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) && |
| 931 | hw_busy_detect) |
| 932 | break; |
| 933 | |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 934 | /* |
| 935 | * Timeout if the device never becomes ready for data and never |
| 936 | * leaves the program state. |
| 937 | */ |
| 938 | if (time_after(jiffies, timeout)) { |
| 939 | pr_err("%s: Card stuck in programming state! %s %s\n", |
| 940 | mmc_hostname(card->host), |
| 941 | req->rq_disk->disk_name, __func__); |
| 942 | return -ETIMEDOUT; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * Some cards mishandle the status bits, |
| 947 | * so make sure to check both the busy |
| 948 | * indication and the card state. |
| 949 | */ |
| 950 | } while (!(status & R1_READY_FOR_DATA) || |
| 951 | (R1_CURRENT_STATE(status) == R1_STATE_PRG)); |
| 952 | |
| 953 | return err; |
| 954 | } |
| 955 | |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 956 | static int send_stop(struct mmc_card *card, unsigned int timeout_ms, |
Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 957 | struct request *req, bool *gen_err, u32 *stop_status) |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 958 | { |
| 959 | struct mmc_host *host = card->host; |
Masahiro Yamada | c7836d1 | 2016-12-19 20:51:18 +0900 | [diff] [blame] | 960 | struct mmc_command cmd = {}; |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 961 | int err; |
| 962 | bool use_r1b_resp = rq_data_dir(req) == WRITE; |
| 963 | |
| 964 | /* |
| 965 | * Normally we use R1B responses for WRITE, but in cases where the host |
| 966 | * has specified a max_busy_timeout we need to validate it. A failure |
| 967 | * means we need to prevent the host from doing hw busy detection, which |
| 968 | * is done by converting to a R1 response instead. |
| 969 | */ |
| 970 | if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout)) |
| 971 | use_r1b_resp = false; |
| 972 | |
| 973 | cmd.opcode = MMC_STOP_TRANSMISSION; |
| 974 | if (use_r1b_resp) { |
| 975 | cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 976 | cmd.busy_timeout = timeout_ms; |
| 977 | } else { |
| 978 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 979 | } |
| 980 | |
| 981 | err = mmc_wait_for_cmd(host, &cmd, 5); |
| 982 | if (err) |
| 983 | return err; |
| 984 | |
| 985 | *stop_status = cmd.resp[0]; |
| 986 | |
| 987 | /* No need to check card status in case of READ. */ |
| 988 | if (rq_data_dir(req) == READ) |
| 989 | return 0; |
| 990 | |
| 991 | if (!mmc_host_is_spi(host) && |
| 992 | (*stop_status & R1_ERROR)) { |
| 993 | pr_err("%s: %s: general error sending stop command, resp %#x\n", |
| 994 | req->rq_disk->disk_name, __func__, *stop_status); |
Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 995 | *gen_err = true; |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err); |
| 999 | } |
| 1000 | |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1001 | #define ERR_NOMEDIUM 3 |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1002 | #define ERR_RETRY 2 |
| 1003 | #define ERR_ABORT 1 |
| 1004 | #define ERR_CONTINUE 0 |
| 1005 | |
| 1006 | static int mmc_blk_cmd_error(struct request *req, const char *name, int error, |
| 1007 | bool status_valid, u32 status) |
| 1008 | { |
| 1009 | switch (error) { |
| 1010 | case -EILSEQ: |
| 1011 | /* response crc error, retry the r/w cmd */ |
| 1012 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 1013 | req->rq_disk->disk_name, "response CRC error", |
| 1014 | name, status); |
| 1015 | return ERR_RETRY; |
| 1016 | |
| 1017 | case -ETIMEDOUT: |
| 1018 | pr_err("%s: %s sending %s command, card status %#x\n", |
| 1019 | req->rq_disk->disk_name, "timed out", name, status); |
| 1020 | |
| 1021 | /* If the status cmd initially failed, retry the r/w cmd */ |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1022 | if (!status_valid) { |
| 1023 | pr_err("%s: status not valid, retrying timeout\n", |
| 1024 | req->rq_disk->disk_name); |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1025 | return ERR_RETRY; |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1026 | } |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1027 | |
| 1028 | /* |
| 1029 | * If it was a r/w cmd crc error, or illegal command |
| 1030 | * (eg, issued in wrong state) then retry - we should |
| 1031 | * have corrected the state problem above. |
| 1032 | */ |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1033 | if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) { |
| 1034 | pr_err("%s: command error, retrying timeout\n", |
| 1035 | req->rq_disk->disk_name); |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1036 | return ERR_RETRY; |
Ken Sumrall | cc4d04b | 2016-05-10 14:53:13 +0530 | [diff] [blame] | 1037 | } |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1038 | |
| 1039 | /* Otherwise abort the command */ |
| 1040 | return ERR_ABORT; |
| 1041 | |
| 1042 | default: |
| 1043 | /* We don't understand the error code the driver gave us */ |
| 1044 | pr_err("%s: unknown error %d sending read/write command, card status %#x\n", |
| 1045 | req->rq_disk->disk_name, error, status); |
| 1046 | return ERR_ABORT; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | /* |
| 1051 | * Initial r/w and stop cmd error recovery. |
| 1052 | * We don't know whether the card received the r/w cmd or not, so try to |
| 1053 | * restore things back to a sane state. Essentially, we do this as follows: |
| 1054 | * - Obtain card status. If the first attempt to obtain card status fails, |
| 1055 | * the status word will reflect the failed status cmd, not the failed |
| 1056 | * r/w cmd. If we fail to obtain card status, it suggests we can no |
| 1057 | * longer communicate with the card. |
| 1058 | * - Check the card state. If the card received the cmd but there was a |
| 1059 | * transient problem with the response, it might still be in a data transfer |
| 1060 | * mode. Try to send it a stop command. If this fails, we can't recover. |
| 1061 | * - If the r/w cmd failed due to a response CRC error, it was probably |
| 1062 | * transient, so retry the cmd. |
| 1063 | * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry. |
| 1064 | * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or |
| 1065 | * illegal cmd, retry. |
| 1066 | * Otherwise we don't understand what happened, so abort. |
| 1067 | */ |
| 1068 | static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req, |
Linus Walleij | 2cc6458 | 2016-11-04 11:05:18 +0100 | [diff] [blame] | 1069 | struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err) |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1070 | { |
| 1071 | bool prev_cmd_status_valid = true; |
| 1072 | u32 status, stop_status = 0; |
| 1073 | int err, retry; |
| 1074 | |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1075 | if (mmc_card_removed(card)) |
| 1076 | return ERR_NOMEDIUM; |
| 1077 | |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1078 | /* |
| 1079 | * Try to get card status which indicates both the card state |
| 1080 | * and why there was no response. If the first attempt fails, |
| 1081 | * we can't be sure the returned status is for the r/w command. |
| 1082 | */ |
| 1083 | for (retry = 2; retry >= 0; retry--) { |
Ulf Hansson | 2185bc2 | 2017-05-22 10:23:58 +0200 | [diff] [blame] | 1084 | err = __mmc_send_status(card, &status, 0); |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1085 | if (!err) |
| 1086 | break; |
| 1087 | |
Adrian Hunter | 6f398ad | 2015-05-07 13:10:23 +0300 | [diff] [blame] | 1088 | /* Re-tune if needed */ |
| 1089 | mmc_retune_recheck(card->host); |
| 1090 | |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1091 | prev_cmd_status_valid = false; |
| 1092 | pr_err("%s: error %d sending status command, %sing\n", |
| 1093 | req->rq_disk->disk_name, err, retry ? "retry" : "abort"); |
| 1094 | } |
| 1095 | |
| 1096 | /* We couldn't get a response from the card. Give up. */ |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1097 | if (err) { |
| 1098 | /* Check if the card is removed */ |
| 1099 | if (mmc_detect_card_removed(card->host)) |
| 1100 | return ERR_NOMEDIUM; |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1101 | return ERR_ABORT; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1102 | } |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1103 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1104 | /* Flag ECC errors */ |
| 1105 | if ((status & R1_CARD_ECC_FAILED) || |
| 1106 | (brq->stop.resp[0] & R1_CARD_ECC_FAILED) || |
| 1107 | (brq->cmd.resp[0] & R1_CARD_ECC_FAILED)) |
Linus Walleij | 2cc6458 | 2016-11-04 11:05:18 +0100 | [diff] [blame] | 1108 | *ecc_err = true; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1109 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1110 | /* Flag General errors */ |
| 1111 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) |
| 1112 | if ((status & R1_ERROR) || |
| 1113 | (brq->stop.resp[0] & R1_ERROR)) { |
| 1114 | pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n", |
| 1115 | req->rq_disk->disk_name, __func__, |
| 1116 | brq->stop.resp[0], status); |
Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 1117 | *gen_err = true; |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1118 | } |
| 1119 | |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1120 | /* |
| 1121 | * Check the current card state. If it is in some data transfer |
| 1122 | * mode, tell it to stop (and hopefully transition back to TRAN.) |
| 1123 | */ |
| 1124 | if (R1_CURRENT_STATE(status) == R1_STATE_DATA || |
| 1125 | R1_CURRENT_STATE(status) == R1_STATE_RCV) { |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1126 | err = send_stop(card, |
| 1127 | DIV_ROUND_UP(brq->data.timeout_ns, 1000000), |
| 1128 | req, gen_err, &stop_status); |
| 1129 | if (err) { |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1130 | pr_err("%s: error %d sending stop command\n", |
| 1131 | req->rq_disk->disk_name, err); |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1132 | /* |
| 1133 | * If the stop cmd also timed out, the card is probably |
| 1134 | * not present, so abort. Other errors are bad news too. |
| 1135 | */ |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1136 | return ERR_ABORT; |
Ulf Hansson | bb5cba4 | 2014-01-14 21:31:35 +0100 | [diff] [blame] | 1137 | } |
| 1138 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1139 | if (stop_status & R1_CARD_ECC_FAILED) |
Linus Walleij | 2cc6458 | 2016-11-04 11:05:18 +0100 | [diff] [blame] | 1140 | *ecc_err = true; |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /* Check for set block count errors */ |
| 1144 | if (brq->sbc.error) |
| 1145 | return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error, |
| 1146 | prev_cmd_status_valid, status); |
| 1147 | |
| 1148 | /* Check for r/w command errors */ |
| 1149 | if (brq->cmd.error) |
| 1150 | return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error, |
| 1151 | prev_cmd_status_valid, status); |
| 1152 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1153 | /* Data errors */ |
| 1154 | if (!brq->stop.error) |
| 1155 | return ERR_CONTINUE; |
| 1156 | |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1157 | /* Now for stop errors. These aren't fatal to the transfer. */ |
Johan Rudholm | 5e1344e | 2014-09-17 09:50:42 +0200 | [diff] [blame] | 1158 | pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n", |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1159 | req->rq_disk->disk_name, brq->stop.error, |
| 1160 | brq->cmd.resp[0], status); |
| 1161 | |
| 1162 | /* |
| 1163 | * Subsitute in our own stop status as this will give the error |
| 1164 | * state which happened during the execution of the r/w command. |
| 1165 | */ |
| 1166 | if (stop_status) { |
| 1167 | brq->stop.resp[0] = stop_status; |
| 1168 | brq->stop.error = 0; |
| 1169 | } |
| 1170 | return ERR_CONTINUE; |
| 1171 | } |
| 1172 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1173 | static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host, |
| 1174 | int type) |
| 1175 | { |
| 1176 | int err; |
| 1177 | |
| 1178 | if (md->reset_done & type) |
| 1179 | return -EEXIST; |
| 1180 | |
| 1181 | md->reset_done |= type; |
| 1182 | err = mmc_hw_reset(host); |
| 1183 | /* Ensure we switch back to the correct partition */ |
| 1184 | if (err != -EOPNOTSUPP) { |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 1185 | struct mmc_blk_data *main_md = |
| 1186 | dev_get_drvdata(&host->card->dev); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1187 | int part_err; |
| 1188 | |
| 1189 | main_md->part_curr = main_md->part_type; |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 1190 | part_err = mmc_blk_part_switch(host->card, md->part_type); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1191 | if (part_err) { |
| 1192 | /* |
| 1193 | * We have failed to get back into the correct |
| 1194 | * partition, so we need to abort the whole request. |
| 1195 | */ |
| 1196 | return -ENODEV; |
| 1197 | } |
| 1198 | } |
| 1199 | return err; |
| 1200 | } |
| 1201 | |
| 1202 | static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type) |
| 1203 | { |
| 1204 | md->reset_done &= ~type; |
| 1205 | } |
| 1206 | |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1207 | /* |
| 1208 | * The non-block commands come back from the block layer after it queued it and |
| 1209 | * processed it with all other requests and then they get issued in this |
| 1210 | * function. |
| 1211 | */ |
| 1212 | static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req) |
| 1213 | { |
| 1214 | struct mmc_queue_req *mq_rq; |
| 1215 | struct mmc_card *card = mq->card; |
| 1216 | struct mmc_blk_data *md = mq->blkdata; |
Linus Walleij | 69f7599 | 2017-08-20 23:39:06 +0200 | [diff] [blame] | 1217 | struct mmc_blk_ioc_data **idata; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 1218 | bool rpmb_ioctl; |
Linus Walleij | 627c3cc | 2017-08-20 23:39:08 +0200 | [diff] [blame] | 1219 | u8 **ext_csd; |
| 1220 | u32 status; |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 1221 | int ret; |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1222 | int i; |
| 1223 | |
| 1224 | mq_rq = req_to_mmc_queue_req(req); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 1225 | rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB); |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1226 | |
| 1227 | switch (mq_rq->drv_op) { |
| 1228 | case MMC_DRV_OP_IOCTL: |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 1229 | case MMC_DRV_OP_IOCTL_RPMB: |
Linus Walleij | 69f7599 | 2017-08-20 23:39:06 +0200 | [diff] [blame] | 1230 | idata = mq_rq->drv_op_data; |
Geert Uytterhoeven | 7432b49 | 2017-07-05 17:09:41 +0200 | [diff] [blame] | 1231 | for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) { |
Linus Walleij | 69f7599 | 2017-08-20 23:39:06 +0200 | [diff] [blame] | 1232 | ret = __mmc_blk_ioctl_cmd(card, md, idata[i]); |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 1233 | if (ret) |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1234 | break; |
| 1235 | } |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1236 | /* Always switch back to main area after RPMB access */ |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 1237 | if (rpmb_ioctl) |
| 1238 | mmc_blk_part_switch(card, 0); |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 1239 | break; |
| 1240 | case MMC_DRV_OP_BOOT_WP: |
| 1241 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP, |
| 1242 | card->ext_csd.boot_ro_lock | |
| 1243 | EXT_CSD_BOOT_WP_B_PWR_WP_EN, |
| 1244 | card->ext_csd.part_time); |
| 1245 | if (ret) |
| 1246 | pr_err("%s: Locking boot partition ro until next power on failed: %d\n", |
| 1247 | md->disk->disk_name, ret); |
| 1248 | else |
| 1249 | card->ext_csd.boot_ro_lock |= |
| 1250 | EXT_CSD_BOOT_WP_B_PWR_WP_EN; |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1251 | break; |
Linus Walleij | 627c3cc | 2017-08-20 23:39:08 +0200 | [diff] [blame] | 1252 | case MMC_DRV_OP_GET_CARD_STATUS: |
| 1253 | ret = mmc_send_status(card, &status); |
| 1254 | if (!ret) |
| 1255 | ret = status; |
| 1256 | break; |
| 1257 | case MMC_DRV_OP_GET_EXT_CSD: |
| 1258 | ext_csd = mq_rq->drv_op_data; |
| 1259 | ret = mmc_get_ext_csd(card, ext_csd); |
| 1260 | break; |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1261 | default: |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 1262 | pr_err("%s: unknown driver specific operation\n", |
| 1263 | md->disk->disk_name); |
| 1264 | ret = -EINVAL; |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1265 | break; |
| 1266 | } |
Linus Walleij | 0493f6f | 2017-05-19 15:37:30 +0200 | [diff] [blame] | 1267 | mq_rq->drv_op_result = ret; |
Adrian Hunter | a7c17d8 | 2017-08-25 15:43:45 +0300 | [diff] [blame] | 1268 | blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK); |
Linus Walleij | 5ec1239 | 2017-05-19 15:37:29 +0200 | [diff] [blame] | 1269 | } |
| 1270 | |
Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1271 | static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1272 | { |
Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1273 | struct mmc_blk_data *md = mq->blkdata; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1274 | struct mmc_card *card = md->queue.card; |
| 1275 | unsigned int from, nr, arg; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1276 | int err = 0, type = MMC_BLK_DISCARD; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1277 | blk_status_t status = BLK_STS_OK; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1278 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1279 | if (!mmc_can_erase(card)) { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1280 | status = BLK_STS_NOTSUPP; |
Geert Uytterhoeven | 8cb6ed1 | 2016-12-19 15:03:44 +0100 | [diff] [blame] | 1281 | goto fail; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | from = blk_rq_pos(req); |
| 1285 | nr = blk_rq_sectors(req); |
| 1286 | |
Kyungmin Park | b3bf915 | 2011-10-18 09:34:04 +0900 | [diff] [blame] | 1287 | if (mmc_can_discard(card)) |
| 1288 | arg = MMC_DISCARD_ARG; |
| 1289 | else if (mmc_can_trim(card)) |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1290 | arg = MMC_TRIM_ARG; |
| 1291 | else |
| 1292 | arg = MMC_ERASE_ARG; |
Geert Uytterhoeven | 164b50b | 2016-12-19 15:03:45 +0100 | [diff] [blame] | 1293 | do { |
| 1294 | err = 0; |
| 1295 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1296 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1297 | INAND_CMD38_ARG_EXT_CSD, |
| 1298 | arg == MMC_TRIM_ARG ? |
| 1299 | INAND_CMD38_ARG_TRIM : |
| 1300 | INAND_CMD38_ARG_ERASE, |
| 1301 | 0); |
| 1302 | } |
| 1303 | if (!err) |
| 1304 | err = mmc_erase(card, from, nr, arg); |
| 1305 | } while (err == -EIO && !mmc_blk_reset(md, card->host, type)); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1306 | if (err) |
| 1307 | status = BLK_STS_IOERR; |
| 1308 | else |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1309 | mmc_blk_reset_success(md, type); |
Geert Uytterhoeven | 8cb6ed1 | 2016-12-19 15:03:44 +0100 | [diff] [blame] | 1310 | fail: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1311 | blk_end_request(req, status, blk_rq_bytes(req)); |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1314 | static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1315 | struct request *req) |
| 1316 | { |
Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1317 | struct mmc_blk_data *md = mq->blkdata; |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1318 | struct mmc_card *card = md->queue.card; |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1319 | unsigned int from, nr, arg; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1320 | int err = 0, type = MMC_BLK_SECDISCARD; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1321 | blk_status_t status = BLK_STS_OK; |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1322 | |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1323 | if (!(mmc_can_secure_erase_trim(card))) { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1324 | status = BLK_STS_NOTSUPP; |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1325 | goto out; |
| 1326 | } |
| 1327 | |
| 1328 | from = blk_rq_pos(req); |
| 1329 | nr = blk_rq_sectors(req); |
| 1330 | |
Maya Erez | 775a936 | 2013-04-18 15:41:55 +0300 | [diff] [blame] | 1331 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
| 1332 | arg = MMC_SECURE_TRIM1_ARG; |
| 1333 | else |
| 1334 | arg = MMC_SECURE_ERASE_ARG; |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1335 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1336 | retry: |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1337 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1338 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1339 | INAND_CMD38_ARG_EXT_CSD, |
| 1340 | arg == MMC_SECURE_TRIM1_ARG ? |
| 1341 | INAND_CMD38_ARG_SECTRIM1 : |
| 1342 | INAND_CMD38_ARG_SECERASE, |
| 1343 | 0); |
| 1344 | if (err) |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1345 | goto out_retry; |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1346 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1347 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1348 | err = mmc_erase(card, from, nr, arg); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1349 | if (err == -EIO) |
| 1350 | goto out_retry; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1351 | if (err) { |
| 1352 | status = BLK_STS_IOERR; |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1353 | goto out; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1354 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1355 | |
| 1356 | if (arg == MMC_SECURE_TRIM1_ARG) { |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1357 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 1358 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 1359 | INAND_CMD38_ARG_EXT_CSD, |
| 1360 | INAND_CMD38_ARG_SECTRIM2, |
| 1361 | 0); |
| 1362 | if (err) |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1363 | goto out_retry; |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1364 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1365 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1366 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1367 | if (err == -EIO) |
| 1368 | goto out_retry; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1369 | if (err) { |
| 1370 | status = BLK_STS_IOERR; |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1371 | goto out; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1372 | } |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 1373 | } |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1374 | |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1375 | out_retry: |
| 1376 | if (err && !mmc_blk_reset(md, card->host, type)) |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1377 | goto retry; |
| 1378 | if (!err) |
| 1379 | mmc_blk_reset_success(md, type); |
Adrian Hunter | 2830281 | 2012-04-05 14:45:48 +0300 | [diff] [blame] | 1380 | out: |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1381 | blk_end_request(req, status, blk_rq_bytes(req)); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 1382 | } |
| 1383 | |
Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1384 | static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1385 | { |
Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1386 | struct mmc_blk_data *md = mq->blkdata; |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1387 | struct mmc_card *card = md->queue.card; |
| 1388 | int ret = 0; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1389 | |
Seungwon Jeon | 881d1c2 | 2011-10-14 14:03:21 +0900 | [diff] [blame] | 1390 | ret = mmc_flush_cache(card); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1391 | blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | /* |
| 1395 | * Reformat current write as a reliable write, supporting |
| 1396 | * both legacy and the enhanced reliable write MMC cards. |
| 1397 | * In each transfer we'll handle only as much as a single |
| 1398 | * reliable write can handle, thus finish the request in |
| 1399 | * partial completions. |
| 1400 | */ |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1401 | static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 1402 | struct mmc_card *card, |
| 1403 | struct request *req) |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1404 | { |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1405 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 1406 | /* Legacy mode imposes restrictions on transfers. */ |
Adrian Hunter | 9cb38f7 | 2017-03-13 14:36:40 +0200 | [diff] [blame] | 1407 | if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors)) |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1408 | brq->data.blocks = 1; |
| 1409 | |
| 1410 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 1411 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 1412 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 1413 | brq->data.blocks = 1; |
| 1414 | } |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1415 | } |
| 1416 | |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1417 | #define CMD_ERRORS \ |
| 1418 | (R1_OUT_OF_RANGE | /* Command argument out of range */ \ |
| 1419 | R1_ADDRESS_ERROR | /* Misaligned address */ \ |
| 1420 | R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\ |
| 1421 | R1_WP_VIOLATION | /* Tried to write to protected block */ \ |
Wolfram Sang | a04e6ba | 2017-04-08 22:20:05 +0200 | [diff] [blame] | 1422 | R1_CARD_ECC_FAILED | /* Card ECC failed */ \ |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1423 | R1_CC_ERROR | /* Card controller error */ \ |
| 1424 | R1_ERROR) /* General/unknown error */ |
| 1425 | |
Shawn Lin | d83c2db | 2017-08-18 09:16:08 +0800 | [diff] [blame] | 1426 | static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq) |
Wolfram Sang | a04e6ba | 2017-04-08 22:20:05 +0200 | [diff] [blame] | 1427 | { |
Shawn Lin | d83c2db | 2017-08-18 09:16:08 +0800 | [diff] [blame] | 1428 | u32 val; |
Wolfram Sang | a04e6ba | 2017-04-08 22:20:05 +0200 | [diff] [blame] | 1429 | |
Shawn Lin | d83c2db | 2017-08-18 09:16:08 +0800 | [diff] [blame] | 1430 | /* |
| 1431 | * Per the SD specification(physical layer version 4.10)[1], |
| 1432 | * section 4.3.3, it explicitly states that "When the last |
| 1433 | * block of user area is read using CMD18, the host should |
| 1434 | * ignore OUT_OF_RANGE error that may occur even the sequence |
| 1435 | * is correct". And JESD84-B51 for eMMC also has a similar |
| 1436 | * statement on section 6.8.3. |
| 1437 | * |
| 1438 | * Multiple block read/write could be done by either predefined |
| 1439 | * method, namely CMD23, or open-ending mode. For open-ending mode, |
| 1440 | * we should ignore the OUT_OF_RANGE error as it's normal behaviour. |
| 1441 | * |
| 1442 | * However the spec[1] doesn't tell us whether we should also |
| 1443 | * ignore that for predefined method. But per the spec[1], section |
| 1444 | * 4.15 Set Block Count Command, it says"If illegal block count |
| 1445 | * is set, out of range error will be indicated during read/write |
| 1446 | * operation (For example, data transfer is stopped at user area |
| 1447 | * boundary)." In another word, we could expect a out of range error |
| 1448 | * in the response for the following CMD18/25. And if argument of |
| 1449 | * CMD23 + the argument of CMD18/25 exceed the max number of blocks, |
| 1450 | * we could also expect to get a -ETIMEDOUT or any error number from |
| 1451 | * the host drivers due to missing data response(for write)/data(for |
| 1452 | * read), as the cards will stop the data transfer by itself per the |
| 1453 | * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode. |
| 1454 | */ |
| 1455 | |
| 1456 | if (!brq->stop.error) { |
| 1457 | bool oor_with_open_end; |
| 1458 | /* If there is no error yet, check R1 response */ |
| 1459 | |
| 1460 | val = brq->stop.resp[0] & CMD_ERRORS; |
| 1461 | oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc; |
| 1462 | |
| 1463 | if (val && !oor_with_open_end) |
| 1464 | brq->stop.error = -EIO; |
| 1465 | } |
Wolfram Sang | a04e6ba | 2017-04-08 22:20:05 +0200 | [diff] [blame] | 1466 | } |
| 1467 | |
Linus Walleij | 8e8b3f5 | 2016-11-04 11:05:19 +0100 | [diff] [blame] | 1468 | static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card, |
| 1469 | struct mmc_async_req *areq) |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1470 | { |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1471 | struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req, |
Linus Walleij | 74f5ba3 | 2017-02-01 13:47:55 +0100 | [diff] [blame] | 1472 | areq); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1473 | struct mmc_blk_request *brq = &mq_mrq->brq; |
Linus Walleij | 67e69d5 | 2017-05-19 15:37:27 +0200 | [diff] [blame] | 1474 | struct request *req = mmc_queue_req_to_req(mq_mrq); |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1475 | int need_retune = card->host->need_retune; |
Linus Walleij | 2cc6458 | 2016-11-04 11:05:18 +0100 | [diff] [blame] | 1476 | bool ecc_err = false; |
Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 1477 | bool gen_err = false; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1478 | |
| 1479 | /* |
| 1480 | * sbc.error indicates a problem with the set block count |
| 1481 | * command. No data will have been transferred. |
| 1482 | * |
| 1483 | * cmd.error indicates a problem with the r/w command. No |
| 1484 | * data will have been transferred. |
| 1485 | * |
| 1486 | * stop.error indicates a problem with the stop command. Data |
| 1487 | * may have been transferred, or may still be transferring. |
| 1488 | */ |
Shawn Lin | d83c2db | 2017-08-18 09:16:08 +0800 | [diff] [blame] | 1489 | |
| 1490 | mmc_blk_eval_resp_error(brq); |
| 1491 | |
| 1492 | if (brq->sbc.error || brq->cmd.error || |
| 1493 | brq->stop.error || brq->data.error) { |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1494 | switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) { |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1495 | case ERR_RETRY: |
| 1496 | return MMC_BLK_RETRY; |
| 1497 | case ERR_ABORT: |
| 1498 | return MMC_BLK_ABORT; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1499 | case ERR_NOMEDIUM: |
| 1500 | return MMC_BLK_NOMEDIUM; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1501 | case ERR_CONTINUE: |
| 1502 | break; |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | /* |
| 1507 | * Check for errors relating to the execution of the |
| 1508 | * initial command - such as address errors. No data |
| 1509 | * has been transferred. |
| 1510 | */ |
| 1511 | if (brq->cmd.resp[0] & CMD_ERRORS) { |
| 1512 | pr_err("%s: r/w command failed, status = %#x\n", |
| 1513 | req->rq_disk->disk_name, brq->cmd.resp[0]); |
| 1514 | return MMC_BLK_ABORT; |
| 1515 | } |
| 1516 | |
| 1517 | /* |
| 1518 | * Everything else is either success, or a data error of some |
| 1519 | * kind. If it was a write, we may have transitioned to |
| 1520 | * program mode, which we have to wait for it to complete. |
| 1521 | */ |
| 1522 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1523 | int err; |
Trey Ramsay | 8fee476 | 2012-11-16 09:31:41 -0600 | [diff] [blame] | 1524 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1525 | /* Check stop command response */ |
| 1526 | if (brq->stop.resp[0] & R1_ERROR) { |
| 1527 | pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n", |
| 1528 | req->rq_disk->disk_name, __func__, |
| 1529 | brq->stop.resp[0]); |
Linus Walleij | c44d6ce | 2016-11-04 11:05:17 +0100 | [diff] [blame] | 1530 | gen_err = true; |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1531 | } |
| 1532 | |
Ulf Hansson | 95a9129 | 2014-01-29 13:11:27 +0100 | [diff] [blame] | 1533 | err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req, |
| 1534 | &gen_err); |
Ulf Hansson | c49433f | 2014-01-29 11:01:55 +0100 | [diff] [blame] | 1535 | if (err) |
| 1536 | return MMC_BLK_CMD_ERR; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1537 | } |
| 1538 | |
KOBAYASHI Yoshitake | c876006 | 2013-07-07 07:35:45 +0900 | [diff] [blame] | 1539 | /* if general error occurs, retry the write operation. */ |
| 1540 | if (gen_err) { |
| 1541 | pr_warn("%s: retrying write for general error\n", |
| 1542 | req->rq_disk->disk_name); |
| 1543 | return MMC_BLK_RETRY; |
| 1544 | } |
| 1545 | |
Wolfram Sang | 9820a5b | 2017-04-08 22:20:06 +0200 | [diff] [blame] | 1546 | /* Some errors (ECC) are flagged on the next commmand, so check stop, too */ |
| 1547 | if (brq->data.error || brq->stop.error) { |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1548 | if (need_retune && !brq->retune_retry_done) { |
Russell King | 09faf61 | 2016-01-29 09:44:00 +0000 | [diff] [blame] | 1549 | pr_debug("%s: retrying because a re-tune was needed\n", |
| 1550 | req->rq_disk->disk_name); |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1551 | brq->retune_retry_done = 1; |
| 1552 | return MMC_BLK_RETRY; |
| 1553 | } |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1554 | pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n", |
Wolfram Sang | 9820a5b | 2017-04-08 22:20:06 +0200 | [diff] [blame] | 1555 | req->rq_disk->disk_name, brq->data.error ?: brq->stop.error, |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1556 | (unsigned)blk_rq_pos(req), |
| 1557 | (unsigned)blk_rq_sectors(req), |
| 1558 | brq->cmd.resp[0], brq->stop.resp[0]); |
| 1559 | |
| 1560 | if (rq_data_dir(req) == READ) { |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1561 | if (ecc_err) |
| 1562 | return MMC_BLK_ECC_ERR; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1563 | return MMC_BLK_DATA_ERR; |
| 1564 | } else { |
| 1565 | return MMC_BLK_CMD_ERR; |
| 1566 | } |
| 1567 | } |
| 1568 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1569 | if (!brq->data.bytes_xfered) |
| 1570 | return MMC_BLK_RETRY; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1571 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1572 | if (blk_rq_bytes(req) != brq->data.bytes_xfered) |
| 1573 | return MMC_BLK_PARTIAL; |
| 1574 | |
| 1575 | return MMC_BLK_SUCCESS; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1576 | } |
| 1577 | |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1578 | static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq, |
Adrian Hunter | d3377c0 | 2017-09-22 15:36:55 +0300 | [diff] [blame] | 1579 | int disable_multi, bool *do_rel_wr_p, |
| 1580 | bool *do_data_tag_p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | { |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1582 | struct mmc_blk_data *md = mq->blkdata; |
| 1583 | struct mmc_card *card = md->queue.card; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1584 | struct mmc_blk_request *brq = &mqrq->brq; |
Linus Walleij | 67e69d5 | 2017-05-19 15:37:27 +0200 | [diff] [blame] | 1585 | struct request *req = mmc_queue_req_to_req(mqrq); |
Adrian Hunter | d3377c0 | 2017-09-22 15:36:55 +0300 | [diff] [blame] | 1586 | bool do_rel_wr, do_data_tag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1588 | /* |
| 1589 | * Reliable writes are used to implement Forced Unit Access and |
Luca Porzio | d3df046 | 2015-11-06 15:12:26 +0000 | [diff] [blame] | 1590 | * are supported only on MMCs. |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1591 | */ |
Adrian Hunter | d3377c0 | 2017-09-22 15:36:55 +0300 | [diff] [blame] | 1592 | do_rel_wr = (req->cmd_flags & REQ_FUA) && |
| 1593 | rq_data_dir(req) == WRITE && |
| 1594 | (md->flags & MMC_BLK_REL_WR); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 1595 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1596 | memset(brq, 0, sizeof(struct mmc_blk_request)); |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1597 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1598 | brq->mrq.data = &brq->data; |
Adrian Hunter | 93482b3 | 2017-09-22 15:36:56 +0300 | [diff] [blame] | 1599 | brq->mrq.tag = req->tag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1601 | brq->stop.opcode = MMC_STOP_TRANSMISSION; |
| 1602 | brq->stop.arg = 0; |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1603 | |
| 1604 | if (rq_data_dir(req) == READ) { |
| 1605 | brq->data.flags = MMC_DATA_READ; |
| 1606 | brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
| 1607 | } else { |
| 1608 | brq->data.flags = MMC_DATA_WRITE; |
| 1609 | brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
| 1610 | } |
| 1611 | |
| 1612 | brq->data.blksz = 512; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1613 | brq->data.blocks = blk_rq_sectors(req); |
Adrian Hunter | 93482b3 | 2017-09-22 15:36:56 +0300 | [diff] [blame] | 1614 | brq->data.blk_addr = blk_rq_pos(req); |
| 1615 | |
| 1616 | /* |
| 1617 | * The command queue supports 2 priorities: "high" (1) and "simple" (0). |
| 1618 | * The eMMC will give "high" priority tasks priority over "simple" |
| 1619 | * priority tasks. Here we always set "simple" priority by not setting |
| 1620 | * MMC_DATA_PRIO. |
| 1621 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1622 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1623 | /* |
| 1624 | * The block layer doesn't support all sector count |
| 1625 | * restrictions, so we need to be prepared for too big |
| 1626 | * requests. |
| 1627 | */ |
| 1628 | if (brq->data.blocks > card->host->max_blk_count) |
| 1629 | brq->data.blocks = card->host->max_blk_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | |
Paul Walmsley | 2bf22b3 | 2011-10-06 14:50:33 -0600 | [diff] [blame] | 1631 | if (brq->data.blocks > 1) { |
| 1632 | /* |
| 1633 | * After a read error, we redo the request one sector |
| 1634 | * at a time in order to accurately determine which |
| 1635 | * sectors can be read successfully. |
| 1636 | */ |
| 1637 | if (disable_multi) |
| 1638 | brq->data.blocks = 1; |
| 1639 | |
Kuninori Morimoto | 2e47e84 | 2014-09-02 19:08:53 -0700 | [diff] [blame] | 1640 | /* |
| 1641 | * Some controllers have HW issues while operating |
| 1642 | * in multiple I/O mode |
| 1643 | */ |
| 1644 | if (card->host->ops->multi_io_quirk) |
| 1645 | brq->data.blocks = card->host->ops->multi_io_quirk(card, |
| 1646 | (rq_data_dir(req) == READ) ? |
| 1647 | MMC_DATA_READ : MMC_DATA_WRITE, |
| 1648 | brq->data.blocks); |
Paul Walmsley | 2bf22b3 | 2011-10-06 14:50:33 -0600 | [diff] [blame] | 1649 | } |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1650 | |
Adrian Hunter | 93482b3 | 2017-09-22 15:36:56 +0300 | [diff] [blame] | 1651 | if (do_rel_wr) { |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1652 | mmc_apply_rel_rw(brq, card, req); |
Adrian Hunter | 93482b3 | 2017-09-22 15:36:56 +0300 | [diff] [blame] | 1653 | brq->data.flags |= MMC_DATA_REL_WR; |
| 1654 | } |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1655 | |
| 1656 | /* |
| 1657 | * Data tag is used only during writing meta data to speed |
| 1658 | * up write and any subsequent read of this meta data |
| 1659 | */ |
Adrian Hunter | d3377c0 | 2017-09-22 15:36:55 +0300 | [diff] [blame] | 1660 | do_data_tag = card->ext_csd.data_tag_unit_size && |
| 1661 | (req->cmd_flags & REQ_META) && |
| 1662 | (rq_data_dir(req) == WRITE) && |
| 1663 | ((brq->data.blocks * brq->data.blksz) >= |
| 1664 | card->ext_csd.data_tag_unit_size); |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1665 | |
Adrian Hunter | 93482b3 | 2017-09-22 15:36:56 +0300 | [diff] [blame] | 1666 | if (do_data_tag) |
| 1667 | brq->data.flags |= MMC_DATA_DAT_TAG; |
| 1668 | |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1669 | mmc_set_data_timeout(&brq->data, card); |
| 1670 | |
| 1671 | brq->data.sg = mqrq->sg; |
| 1672 | brq->data.sg_len = mmc_queue_map_sg(mq, mqrq); |
| 1673 | |
| 1674 | /* |
| 1675 | * Adjust the sg list so it is the same size as the |
| 1676 | * request. |
| 1677 | */ |
| 1678 | if (brq->data.blocks != blk_rq_sectors(req)) { |
| 1679 | int i, data_size = brq->data.blocks << 9; |
| 1680 | struct scatterlist *sg; |
| 1681 | |
| 1682 | for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) { |
| 1683 | data_size -= sg->length; |
| 1684 | if (data_size <= 0) { |
| 1685 | sg->length += data_size; |
| 1686 | i++; |
| 1687 | break; |
| 1688 | } |
| 1689 | } |
| 1690 | brq->data.sg_len = i; |
| 1691 | } |
| 1692 | |
| 1693 | mqrq->areq.mrq = &brq->mrq; |
Adrian Hunter | d3377c0 | 2017-09-22 15:36:55 +0300 | [diff] [blame] | 1694 | |
| 1695 | if (do_rel_wr_p) |
| 1696 | *do_rel_wr_p = do_rel_wr; |
| 1697 | |
| 1698 | if (do_data_tag_p) |
| 1699 | *do_data_tag_p = do_data_tag; |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1700 | } |
| 1701 | |
| 1702 | static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq, |
| 1703 | struct mmc_card *card, |
| 1704 | int disable_multi, |
| 1705 | struct mmc_queue *mq) |
| 1706 | { |
| 1707 | u32 readcmd, writecmd; |
| 1708 | struct mmc_blk_request *brq = &mqrq->brq; |
Linus Walleij | 67e69d5 | 2017-05-19 15:37:27 +0200 | [diff] [blame] | 1709 | struct request *req = mmc_queue_req_to_req(mqrq); |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1710 | struct mmc_blk_data *md = mq->blkdata; |
| 1711 | bool do_rel_wr, do_data_tag; |
| 1712 | |
| 1713 | mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag); |
| 1714 | |
| 1715 | brq->mrq.cmd = &brq->cmd; |
| 1716 | |
| 1717 | brq->cmd.arg = blk_rq_pos(req); |
| 1718 | if (!mmc_card_blockaddr(card)) |
| 1719 | brq->cmd.arg <<= 9; |
| 1720 | brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
| 1721 | |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1722 | if (brq->data.blocks > 1 || do_rel_wr) { |
| 1723 | /* SPI multiblock writes terminate using a special |
| 1724 | * token, not a STOP_TRANSMISSION request. |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 1725 | */ |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1726 | if (!mmc_host_is_spi(card->host) || |
| 1727 | rq_data_dir(req) == READ) |
| 1728 | brq->mrq.stop = &brq->stop; |
| 1729 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 1730 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
| 1731 | } else { |
| 1732 | brq->mrq.stop = NULL; |
| 1733 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 1734 | writecmd = MMC_WRITE_BLOCK; |
| 1735 | } |
Adrian Hunter | ca5717f | 2017-03-13 14:36:41 +0200 | [diff] [blame] | 1736 | brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd; |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1737 | |
| 1738 | /* |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1739 | * Pre-defined multi-block transfers are preferable to |
| 1740 | * open ended-ones (and necessary for reliable writes). |
| 1741 | * However, it is not sufficient to just send CMD23, |
| 1742 | * and avoid the final CMD12, as on an error condition |
| 1743 | * CMD12 (stop) needs to be sent anyway. This, coupled |
| 1744 | * with Auto-CMD23 enhancements provided by some |
| 1745 | * hosts, means that the complexity of dealing |
| 1746 | * with this is best left to the host. If CMD23 is |
| 1747 | * supported by card and host, we'll fill sbc in and let |
| 1748 | * the host deal with handling it correctly. This means |
| 1749 | * that for hosts that don't expose MMC_CAP_CMD23, no |
| 1750 | * change of behavior will be observed. |
| 1751 | * |
| 1752 | * N.B: Some MMC cards experience perf degradation. |
| 1753 | * We'll avoid using CMD23-bounded multiblock writes for |
| 1754 | * these, while retaining features like reliable writes. |
| 1755 | */ |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1756 | if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) && |
| 1757 | (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) || |
| 1758 | do_data_tag)) { |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1759 | brq->sbc.opcode = MMC_SET_BLOCK_COUNT; |
| 1760 | brq->sbc.arg = brq->data.blocks | |
Saugata Das | 4265900 | 2011-12-21 13:09:17 +0530 | [diff] [blame] | 1761 | (do_rel_wr ? (1 << 31) : 0) | |
| 1762 | (do_data_tag ? (1 << 29) : 0); |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1763 | brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 1764 | brq->mrq.sbc = &brq->sbc; |
| 1765 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 1766 | |
Linus Walleij | 74f5ba3 | 2017-02-01 13:47:55 +0100 | [diff] [blame] | 1767 | mqrq->areq.err_check = mmc_blk_err_check; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1770 | static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card, |
| 1771 | struct mmc_blk_request *brq, struct request *req, |
| 1772 | bool old_req_pending) |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1773 | { |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1774 | bool req_pending; |
| 1775 | |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1776 | /* |
| 1777 | * If this is an SD card and we're writing, we can first |
| 1778 | * mark the known good sectors as ok. |
| 1779 | * |
| 1780 | * If the card is not SD, we can still ok written sectors |
| 1781 | * as reported by the controller (which might be less than |
| 1782 | * the real number of written sectors, but never more). |
| 1783 | */ |
| 1784 | if (mmc_card_sd(card)) { |
| 1785 | u32 blocks; |
Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 1786 | int err; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1787 | |
Linus Walleij | 169f03a | 2017-02-01 13:47:57 +0100 | [diff] [blame] | 1788 | err = mmc_sd_num_wr_blocks(card, &blocks); |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1789 | if (err) |
| 1790 | req_pending = old_req_pending; |
| 1791 | else |
Adrian Hunter | a7c17d8 | 2017-08-25 15:43:45 +0300 | [diff] [blame] | 1792 | req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9); |
Adrian Hunter | 5dd784d2 | 2016-11-29 12:09:08 +0200 | [diff] [blame] | 1793 | } else { |
Adrian Hunter | a7c17d8 | 2017-08-25 15:43:45 +0300 | [diff] [blame] | 1794 | req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1795 | } |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1796 | return req_pending; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1797 | } |
| 1798 | |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1799 | static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card, |
| 1800 | struct request *req, |
| 1801 | struct mmc_queue_req *mqrq) |
Linus Walleij | 4e1f780 | 2017-01-24 11:17:52 +0100 | [diff] [blame] | 1802 | { |
Linus Walleij | 4e1f780 | 2017-01-24 11:17:52 +0100 | [diff] [blame] | 1803 | if (mmc_card_removed(card)) |
| 1804 | req->rq_flags |= RQF_QUIET; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1805 | while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req))); |
Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1806 | mq->qcnt--; |
Linus Walleij | 4e1f780 | 2017-01-24 11:17:52 +0100 | [diff] [blame] | 1807 | } |
| 1808 | |
Linus Walleij | b2928e1 | 2017-02-01 13:47:54 +0100 | [diff] [blame] | 1809 | /** |
| 1810 | * mmc_blk_rw_try_restart() - tries to restart the current async request |
| 1811 | * @mq: the queue with the card and host to restart |
| 1812 | * @req: a new request that want to be started after the current one |
| 1813 | */ |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1814 | static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req, |
| 1815 | struct mmc_queue_req *mqrq) |
Linus Walleij | efb5a05 | 2017-01-24 11:17:53 +0100 | [diff] [blame] | 1816 | { |
| 1817 | if (!req) |
| 1818 | return; |
| 1819 | |
Linus Walleij | b2928e1 | 2017-02-01 13:47:54 +0100 | [diff] [blame] | 1820 | /* |
| 1821 | * If the card was removed, just cancel everything and return. |
| 1822 | */ |
| 1823 | if (mmc_card_removed(mq->card)) { |
Linus Walleij | efb5a05 | 2017-01-24 11:17:53 +0100 | [diff] [blame] | 1824 | req->rq_flags |= RQF_QUIET; |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1825 | blk_end_request_all(req, BLK_STS_IOERR); |
Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1826 | mq->qcnt--; /* FIXME: just set to 0? */ |
Linus Walleij | b2928e1 | 2017-02-01 13:47:54 +0100 | [diff] [blame] | 1827 | return; |
Linus Walleij | efb5a05 | 2017-01-24 11:17:53 +0100 | [diff] [blame] | 1828 | } |
Linus Walleij | b2928e1 | 2017-02-01 13:47:54 +0100 | [diff] [blame] | 1829 | /* Else proceed and try to restart the current async request */ |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1830 | mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq); |
| 1831 | mmc_start_areq(mq->card->host, &mqrq->areq, NULL); |
Linus Walleij | efb5a05 | 2017-01-24 11:17:53 +0100 | [diff] [blame] | 1832 | } |
| 1833 | |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1834 | static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req) |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1835 | { |
Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 1836 | struct mmc_blk_data *md = mq->blkdata; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1837 | struct mmc_card *card = md->queue.card; |
Adrian Hunter | 5be8037 | 2016-11-29 12:09:09 +0200 | [diff] [blame] | 1838 | struct mmc_blk_request *brq; |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1839 | int disable_multi = 0, retry = 0, type, retune_retry_done = 0; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1840 | enum mmc_blk_status status; |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1841 | struct mmc_queue_req *mqrq_cur = NULL; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1842 | struct mmc_queue_req *mq_rq; |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1843 | struct request *old_req; |
Linus Walleij | 7d552a4 | 2017-01-24 11:17:56 +0100 | [diff] [blame] | 1844 | struct mmc_async_req *new_areq; |
| 1845 | struct mmc_async_req *old_areq; |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1846 | bool req_pending = true; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1847 | |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1848 | if (new_req) { |
Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1849 | mqrq_cur = req_to_mmc_queue_req(new_req); |
| 1850 | mq->qcnt++; |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1851 | } |
| 1852 | |
| 1853 | if (!mq->qcnt) |
Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1854 | return; |
Per Forlin | 54d49d7 | 2011-07-01 18:55:29 +0200 | [diff] [blame] | 1855 | |
| 1856 | do { |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1857 | if (new_req) { |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 1858 | /* |
| 1859 | * When 4KB native sector is enabled, only 8 blocks |
| 1860 | * multiple read or write is allowed |
| 1861 | */ |
Yuan, Juntao | e87c856 | 2016-05-13 07:59:24 +0000 | [diff] [blame] | 1862 | if (mmc_large_sector(card) && |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1863 | !IS_ALIGNED(blk_rq_sectors(new_req), 8)) { |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 1864 | pr_err("%s: Transfer size is not 4KB sector size aligned\n", |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1865 | new_req->rq_disk->disk_name); |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1866 | mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur); |
Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1867 | return; |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 1868 | } |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1869 | |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1870 | mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq); |
| 1871 | new_areq = &mqrq_cur->areq; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1872 | } else |
Linus Walleij | 7d552a4 | 2017-01-24 11:17:56 +0100 | [diff] [blame] | 1873 | new_areq = NULL; |
| 1874 | |
Linus Walleij | c3399ef | 2017-02-01 13:47:53 +0100 | [diff] [blame] | 1875 | old_areq = mmc_start_areq(card->host, new_areq, &status); |
Linus Walleij | 7d552a4 | 2017-01-24 11:17:56 +0100 | [diff] [blame] | 1876 | if (!old_areq) { |
Linus Walleij | da0dbaf | 2017-01-24 11:17:55 +0100 | [diff] [blame] | 1877 | /* |
| 1878 | * We have just put the first request into the pipeline |
| 1879 | * and there is nothing more to do until it is |
| 1880 | * complete. |
| 1881 | */ |
Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1882 | return; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1883 | } |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 1884 | |
Linus Walleij | da0dbaf | 2017-01-24 11:17:55 +0100 | [diff] [blame] | 1885 | /* |
| 1886 | * An asynchronous request has been completed and we proceed |
| 1887 | * to handle the result of it. |
| 1888 | */ |
Linus Walleij | 74f5ba3 | 2017-02-01 13:47:55 +0100 | [diff] [blame] | 1889 | mq_rq = container_of(old_areq, struct mmc_queue_req, areq); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1890 | brq = &mq_rq->brq; |
Linus Walleij | 67e69d5 | 2017-05-19 15:37:27 +0200 | [diff] [blame] | 1891 | old_req = mmc_queue_req_to_req(mq_rq); |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1892 | type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 1893 | |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1894 | switch (status) { |
| 1895 | case MMC_BLK_SUCCESS: |
| 1896 | case MMC_BLK_PARTIAL: |
| 1897 | /* |
| 1898 | * A block was successfully transferred. |
| 1899 | */ |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1900 | mmc_blk_reset_success(md, type); |
Seungwon Jeon | ce39f9d | 2013-02-06 17:02:46 +0900 | [diff] [blame] | 1901 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1902 | req_pending = blk_end_request(old_req, BLK_STS_OK, |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1903 | brq->data.bytes_xfered); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1904 | /* |
| 1905 | * If the blk_end_request function returns non-zero even |
| 1906 | * though all data has been transferred and no errors |
| 1907 | * were returned by the host controller, it's a bug. |
| 1908 | */ |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1909 | if (status == MMC_BLK_SUCCESS && req_pending) { |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 1910 | pr_err("%s BUG rq_tot %d d_xfer %d\n", |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1911 | __func__, blk_rq_bytes(old_req), |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1912 | brq->data.bytes_xfered); |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1913 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 1914 | return; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 1915 | } |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1916 | break; |
| 1917 | case MMC_BLK_CMD_ERR: |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1918 | req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending); |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1919 | if (mmc_blk_reset(md, card->host, type)) { |
Adrian Hunter | 8ecc344 | 2017-03-13 14:36:33 +0200 | [diff] [blame] | 1920 | if (req_pending) |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1921 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
| 1922 | else |
Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1923 | mq->qcnt--; |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1924 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1925 | return; |
| 1926 | } |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1927 | if (!req_pending) { |
Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1928 | mq->qcnt--; |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1929 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1930 | return; |
| 1931 | } |
Ding Wang | 29535f7 | 2015-05-18 20:14:15 +0800 | [diff] [blame] | 1932 | break; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1933 | case MMC_BLK_RETRY: |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1934 | retune_retry_done = brq->retune_retry_done; |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1935 | if (retry++ < 5) |
Russell King - ARM Linux | a01f3cc | 2011-06-20 20:10:28 +0100 | [diff] [blame] | 1936 | break; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1937 | /* Fall through */ |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1938 | case MMC_BLK_ABORT: |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1939 | if (!mmc_blk_reset(md, card->host, type)) |
| 1940 | break; |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1941 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1942 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1943 | return; |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1944 | case MMC_BLK_DATA_ERR: { |
| 1945 | int err; |
| 1946 | |
| 1947 | err = mmc_blk_reset(md, card->host, type); |
| 1948 | if (!err) |
| 1949 | break; |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1950 | if (err == -ENODEV) { |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1951 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1952 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1953 | return; |
| 1954 | } |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1955 | /* Fall through */ |
| 1956 | } |
| 1957 | case MMC_BLK_ECC_ERR: |
| 1958 | if (brq->data.blocks > 1) { |
| 1959 | /* Redo read one sector at a time */ |
Joe Perches | 6606110 | 2014-09-12 14:56:56 -0700 | [diff] [blame] | 1960 | pr_warn("%s: retrying using single block read\n", |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1961 | old_req->rq_disk->disk_name); |
Adrian Hunter | 6771632 | 2011-08-29 16:42:15 +0300 | [diff] [blame] | 1962 | disable_multi = 1; |
| 1963 | break; |
| 1964 | } |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1965 | /* |
| 1966 | * After an error, we redo I/O one sector at a |
| 1967 | * time, so we only reach here after trying to |
| 1968 | * read a single sector. |
| 1969 | */ |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1970 | req_pending = blk_end_request(old_req, BLK_STS_IOERR, |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1971 | brq->data.blksz); |
| 1972 | if (!req_pending) { |
Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 1973 | mq->qcnt--; |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1974 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1975 | return; |
| 1976 | } |
Per Forlin | d78d4a8a | 2011-07-01 18:55:30 +0200 | [diff] [blame] | 1977 | break; |
Sujit Reddy Thumma | a8ad82cc | 2011-12-08 14:05:50 +0530 | [diff] [blame] | 1978 | case MMC_BLK_NOMEDIUM: |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1979 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1980 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1981 | return; |
Konstantin Dorfman | 2220eed | 2013-01-14 14:28:17 -0500 | [diff] [blame] | 1982 | default: |
| 1983 | pr_err("%s: Unhandled return value (%d)", |
Linus Walleij | acd8dbd | 2017-02-01 13:47:52 +0100 | [diff] [blame] | 1984 | old_req->rq_disk->disk_name, status); |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 1985 | mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq); |
Adrian Hunter | 8ddfe07 | 2017-03-13 14:36:34 +0200 | [diff] [blame] | 1986 | mmc_blk_rw_try_restart(mq, new_req, mqrq_cur); |
Linus Walleij | db43550 | 2017-02-01 13:47:51 +0100 | [diff] [blame] | 1987 | return; |
Russell King - ARM Linux | 4c2b8f2 | 2011-06-20 20:10:49 +0100 | [diff] [blame] | 1988 | } |
| 1989 | |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 1990 | if (req_pending) { |
Linus Walleij | 03d640a | 2016-11-25 10:35:00 +0100 | [diff] [blame] | 1991 | /* |
| 1992 | * In case of a incomplete request |
| 1993 | * prepare it again and resend. |
| 1994 | */ |
| 1995 | mmc_blk_rw_rq_prep(mq_rq, card, |
| 1996 | disable_multi, mq); |
Linus Walleij | c3399ef | 2017-02-01 13:47:53 +0100 | [diff] [blame] | 1997 | mmc_start_areq(card->host, |
Linus Walleij | 74f5ba3 | 2017-02-01 13:47:55 +0100 | [diff] [blame] | 1998 | &mq_rq->areq, NULL); |
Adrian Hunter | b8360a4 | 2015-05-07 13:10:24 +0300 | [diff] [blame] | 1999 | mq_rq->brq.retune_retry_done = retune_retry_done; |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2000 | } |
Linus Walleij | 0e65f10 | 2017-02-01 13:47:58 +0100 | [diff] [blame] | 2001 | } while (req_pending); |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 2002 | |
Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 2003 | mq->qcnt--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | } |
| 2005 | |
Linus Walleij | df06158 | 2017-01-24 11:17:57 +0100 | [diff] [blame] | 2006 | void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2007 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2008 | int ret; |
Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 2009 | struct mmc_blk_data *md = mq->blkdata; |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2010 | struct mmc_card *card = md->queue.card; |
| 2011 | |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 2012 | if (req && !mq->qcnt) |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2013 | /* claim host only for the first request */ |
Adrian Hunter | 6c0cedd | 2017-09-22 15:36:51 +0300 | [diff] [blame] | 2014 | mmc_get_card(card, NULL); |
Per Forlin | ee8a43a | 2011-07-01 18:55:33 +0200 | [diff] [blame] | 2015 | |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 2016 | ret = mmc_blk_part_switch(card, md->part_type); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2017 | if (ret) { |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2018 | if (req) { |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 2019 | blk_end_request_all(req, BLK_STS_IOERR); |
Adrian Hunter | 0d7d85c | 2011-09-23 12:48:20 +0300 | [diff] [blame] | 2020 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2021 | goto out; |
| 2022 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2023 | |
Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 2024 | if (req) { |
| 2025 | switch (req_op(req)) { |
| 2026 | case REQ_OP_DRV_IN: |
| 2027 | case REQ_OP_DRV_OUT: |
| 2028 | /* |
| 2029 | * Complete ongoing async transfer before issuing |
| 2030 | * ioctl()s |
| 2031 | */ |
| 2032 | if (mq->qcnt) |
| 2033 | mmc_blk_issue_rw_rq(mq, NULL); |
Linus Walleij | 02166a0 | 2017-05-19 15:37:28 +0200 | [diff] [blame] | 2034 | mmc_blk_issue_drv_op(mq, req); |
Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 2035 | break; |
| 2036 | case REQ_OP_DISCARD: |
| 2037 | /* |
| 2038 | * Complete ongoing async transfer before issuing |
| 2039 | * discard. |
| 2040 | */ |
| 2041 | if (mq->qcnt) |
| 2042 | mmc_blk_issue_rw_rq(mq, NULL); |
| 2043 | mmc_blk_issue_discard_rq(mq, req); |
| 2044 | break; |
| 2045 | case REQ_OP_SECURE_ERASE: |
| 2046 | /* |
| 2047 | * Complete ongoing async transfer before issuing |
| 2048 | * secure erase. |
| 2049 | */ |
| 2050 | if (mq->qcnt) |
| 2051 | mmc_blk_issue_rw_rq(mq, NULL); |
| 2052 | mmc_blk_issue_secdiscard_rq(mq, req); |
| 2053 | break; |
| 2054 | case REQ_OP_FLUSH: |
| 2055 | /* |
| 2056 | * Complete ongoing async transfer before issuing |
| 2057 | * flush. |
| 2058 | */ |
| 2059 | if (mq->qcnt) |
| 2060 | mmc_blk_issue_rw_rq(mq, NULL); |
| 2061 | mmc_blk_issue_flush(mq, req); |
| 2062 | break; |
| 2063 | default: |
| 2064 | /* Normal request, just issue it */ |
| 2065 | mmc_blk_issue_rw_rq(mq, req); |
| 2066 | card->host->context_info.is_waiting_last_req = false; |
| 2067 | break; |
Wu Fengguang | 7322238 | 2017-05-23 05:11:33 +0800 | [diff] [blame] | 2068 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2069 | } else { |
Linus Walleij | 614f038 | 2017-05-18 11:29:34 +0200 | [diff] [blame] | 2070 | /* No request, flushing the pipeline with NULL */ |
| 2071 | mmc_blk_issue_rw_rq(mq, NULL); |
Adrian Hunter | 2602b74 | 2017-03-13 14:36:32 +0200 | [diff] [blame] | 2072 | card->host->context_info.is_waiting_last_req = false; |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 2073 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 2074 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2075 | out: |
Adrian Hunter | cdf8a6f | 2017-03-13 14:36:35 +0200 | [diff] [blame] | 2076 | if (!mq->qcnt) |
Adrian Hunter | 6c0cedd | 2017-09-22 15:36:51 +0300 | [diff] [blame] | 2077 | mmc_put_card(card, NULL); |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 2078 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2080 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 2081 | { |
| 2082 | return mmc_card_readonly(card) || |
| 2083 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 2084 | } |
| 2085 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2086 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 2087 | struct device *parent, |
| 2088 | sector_t size, |
| 2089 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2090 | const char *subname, |
| 2091 | int area_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | { |
| 2093 | struct mmc_blk_data *md; |
| 2094 | int devidx, ret; |
| 2095 | |
Heiner Kallweit | a04848c | 2017-02-01 19:44:22 +0100 | [diff] [blame] | 2096 | devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL); |
Shawn Lin | e7b4276 | 2017-08-23 15:38:31 +0800 | [diff] [blame] | 2097 | if (devidx < 0) { |
| 2098 | /* |
| 2099 | * We get -ENOSPC because there are no more any available |
| 2100 | * devidx. The reason may be that, either userspace haven't yet |
| 2101 | * unmounted the partitions, which postpones mmc_blk_release() |
| 2102 | * from being called, or the device has more partitions than |
| 2103 | * what we support. |
| 2104 | */ |
| 2105 | if (devidx == -ENOSPC) |
| 2106 | dev_err(mmc_dev(card->host), |
| 2107 | "no more device IDs available\n"); |
| 2108 | |
Heiner Kallweit | a04848c | 2017-02-01 19:44:22 +0100 | [diff] [blame] | 2109 | return ERR_PTR(devidx); |
Shawn Lin | e7b4276 | 2017-08-23 15:38:31 +0800 | [diff] [blame] | 2110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 2112 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2113 | if (!md) { |
| 2114 | ret = -ENOMEM; |
| 2115 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2117 | |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2118 | md->area_type = area_type; |
| 2119 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2120 | /* |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2121 | * Set the read-only status based on the supported commands |
| 2122 | * and the write protect switch. |
| 2123 | */ |
| 2124 | md->read_only = mmc_blk_readonly(card); |
| 2125 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2126 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2127 | if (md->disk == NULL) { |
| 2128 | ret = -ENOMEM; |
| 2129 | goto err_kfree; |
| 2130 | } |
| 2131 | |
| 2132 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2133 | INIT_LIST_HEAD(&md->part); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2134 | INIT_LIST_HEAD(&md->rpmbs); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2135 | md->usage = 1; |
| 2136 | |
Adrian Hunter | d09408a | 2011-06-23 13:40:28 +0300 | [diff] [blame] | 2137 | ret = mmc_init_queue(&md->queue, card, &md->lock, subname); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2138 | if (ret) |
| 2139 | goto err_putdisk; |
| 2140 | |
Linus Walleij | 7db3028 | 2016-11-18 13:36:15 +0100 | [diff] [blame] | 2141 | md->queue.blkdata = md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2142 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2143 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2144 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2145 | md->disk->fops = &mmc_bdops; |
| 2146 | md->disk->private_data = md; |
| 2147 | md->disk->queue = md->queue.queue; |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 2148 | md->parent = parent; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2149 | set_disk_ro(md->disk, md->read_only || default_ro); |
Colin Cross | 382c55f | 2015-10-22 10:00:41 -0700 | [diff] [blame] | 2150 | md->disk->flags = GENHD_FL_EXT_DEVT; |
Ulf Hansson | f5b4d71 | 2014-09-03 11:02:23 +0200 | [diff] [blame] | 2151 | 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] | 2152 | md->disk->flags |= GENHD_FL_NO_PART_SCAN; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2153 | |
| 2154 | /* |
| 2155 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 2156 | * |
| 2157 | * - be set for removable media with permanent block devices |
| 2158 | * - be unset for removable block devices with permanent media |
| 2159 | * |
| 2160 | * Since MMC block devices clearly fall under the second |
| 2161 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 2162 | * should use the block device creation/destruction hotplug |
| 2163 | * messages to tell when the card is present. |
| 2164 | */ |
| 2165 | |
Andrei Warkentin | f06c915 | 2011-04-21 22:46:13 -0500 | [diff] [blame] | 2166 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
Ulf Hansson | 9aaf343 | 2016-04-06 16:12:08 +0200 | [diff] [blame] | 2167 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2168 | |
Saugata Das | a5075eb | 2012-05-17 16:32:21 +0530 | [diff] [blame] | 2169 | if (mmc_card_mmc(card)) |
| 2170 | blk_queue_logical_block_size(md->queue.queue, |
| 2171 | card->ext_csd.data_sector_size); |
| 2172 | else |
| 2173 | blk_queue_logical_block_size(md->queue.queue, 512); |
| 2174 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2175 | set_capacity(md->disk, size); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2176 | |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2177 | if (mmc_host_cmd23(card->host)) { |
Daniel Glöckner | 0ed50ab | 2016-08-30 14:17:30 +0200 | [diff] [blame] | 2178 | if ((mmc_card_mmc(card) && |
| 2179 | card->csd.mmca_vsn >= CSD_SPEC_VER_3) || |
Andrei Warkentin | f0d8997 | 2011-05-23 15:06:38 -0500 | [diff] [blame] | 2180 | (mmc_card_sd(card) && |
| 2181 | card->scr.cmds & SD_SCR_CMD23_SUPPORT)) |
| 2182 | md->flags |= MMC_BLK_CMD23; |
| 2183 | } |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2184 | |
| 2185 | if (mmc_card_mmc(card) && |
| 2186 | md->flags & MMC_BLK_CMD23 && |
| 2187 | ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || |
| 2188 | card->ext_csd.rel_sectors)) { |
| 2189 | md->flags |= MMC_BLK_REL_WR; |
Jens Axboe | e9d5c74 | 2016-03-30 10:17:20 -0600 | [diff] [blame] | 2190 | blk_queue_write_cache(md->queue.queue, true, true); |
Andrei Warkentin | d0c97cf | 2011-05-23 15:06:36 -0500 | [diff] [blame] | 2191 | } |
| 2192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2194 | |
| 2195 | err_putdisk: |
| 2196 | put_disk(md->disk); |
| 2197 | err_kfree: |
| 2198 | kfree(md); |
| 2199 | out: |
Heiner Kallweit | a04848c | 2017-02-01 19:44:22 +0100 | [diff] [blame] | 2200 | ida_simple_remove(&mmc_blk_ida, devidx); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 2201 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | } |
| 2203 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2204 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 2205 | { |
| 2206 | sector_t size; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2207 | |
| 2208 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 2209 | /* |
| 2210 | * The EXT_CSD sector count is in number or 512 byte |
| 2211 | * sectors. |
| 2212 | */ |
| 2213 | size = card->ext_csd.sectors; |
| 2214 | } else { |
| 2215 | /* |
| 2216 | * The CSD capacity field is in units of read_blkbits. |
| 2217 | * set_capacity takes units of 512 bytes. |
| 2218 | */ |
Kuninori Morimoto | 087de9e | 2015-05-11 07:35:28 +0000 | [diff] [blame] | 2219 | size = (typeof(sector_t))card->csd.capacity |
| 2220 | << (card->csd.read_blkbits - 9); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2221 | } |
| 2222 | |
Tobias Klauser | 7a30f2a | 2015-01-21 15:56:44 +0100 | [diff] [blame] | 2223 | return mmc_blk_alloc_req(card, &card->dev, size, false, NULL, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2224 | MMC_BLK_DATA_AREA_MAIN); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 2228 | struct mmc_blk_data *md, |
| 2229 | unsigned int part_type, |
| 2230 | sector_t size, |
| 2231 | bool default_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2232 | const char *subname, |
| 2233 | int area_type) |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2234 | { |
| 2235 | char cap_str[10]; |
| 2236 | struct mmc_blk_data *part_md; |
| 2237 | |
| 2238 | 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] | 2239 | subname, area_type); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2240 | if (IS_ERR(part_md)) |
| 2241 | return PTR_ERR(part_md); |
| 2242 | part_md->part_type = part_type; |
| 2243 | list_add(&part_md->part, &md->part); |
| 2244 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 2245 | 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] | 2246 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2247 | pr_info("%s: %s %s partition %u %s\n", |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2248 | part_md->disk->disk_name, mmc_card_id(card), |
| 2249 | mmc_card_name(card), part_md->part_type, cap_str); |
| 2250 | return 0; |
| 2251 | } |
| 2252 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2253 | /** |
| 2254 | * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev |
| 2255 | * @filp: the character device file |
| 2256 | * @cmd: the ioctl() command |
| 2257 | * @arg: the argument from userspace |
| 2258 | * |
| 2259 | * This will essentially just redirect the ioctl()s coming in over to |
| 2260 | * the main block device spawning the RPMB character device. |
| 2261 | */ |
| 2262 | static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd, |
| 2263 | unsigned long arg) |
| 2264 | { |
| 2265 | struct mmc_rpmb_data *rpmb = filp->private_data; |
| 2266 | int ret; |
| 2267 | |
| 2268 | switch (cmd) { |
| 2269 | case MMC_IOC_CMD: |
| 2270 | ret = mmc_blk_ioctl_cmd(rpmb->md, |
| 2271 | (struct mmc_ioc_cmd __user *)arg, |
| 2272 | rpmb); |
| 2273 | break; |
| 2274 | case MMC_IOC_MULTI_CMD: |
| 2275 | ret = mmc_blk_ioctl_multi_cmd(rpmb->md, |
| 2276 | (struct mmc_ioc_multi_cmd __user *)arg, |
| 2277 | rpmb); |
| 2278 | break; |
| 2279 | default: |
| 2280 | ret = -EINVAL; |
| 2281 | break; |
| 2282 | } |
| 2283 | |
| 2284 | return 0; |
| 2285 | } |
| 2286 | |
| 2287 | #ifdef CONFIG_COMPAT |
| 2288 | static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd, |
| 2289 | unsigned long arg) |
| 2290 | { |
| 2291 | return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)); |
| 2292 | } |
| 2293 | #endif |
| 2294 | |
| 2295 | static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp) |
| 2296 | { |
| 2297 | struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, |
| 2298 | struct mmc_rpmb_data, chrdev); |
| 2299 | |
| 2300 | get_device(&rpmb->dev); |
| 2301 | filp->private_data = rpmb; |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2302 | mmc_blk_get(rpmb->md->disk); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2303 | |
| 2304 | return nonseekable_open(inode, filp); |
| 2305 | } |
| 2306 | |
| 2307 | static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp) |
| 2308 | { |
| 2309 | struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev, |
| 2310 | struct mmc_rpmb_data, chrdev); |
| 2311 | |
| 2312 | put_device(&rpmb->dev); |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2313 | mmc_blk_put(rpmb->md); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2314 | |
| 2315 | return 0; |
| 2316 | } |
| 2317 | |
| 2318 | static const struct file_operations mmc_rpmb_fileops = { |
| 2319 | .release = mmc_rpmb_chrdev_release, |
| 2320 | .open = mmc_rpmb_chrdev_open, |
| 2321 | .owner = THIS_MODULE, |
| 2322 | .llseek = no_llseek, |
| 2323 | .unlocked_ioctl = mmc_rpmb_ioctl, |
| 2324 | #ifdef CONFIG_COMPAT |
| 2325 | .compat_ioctl = mmc_rpmb_ioctl_compat, |
| 2326 | #endif |
| 2327 | }; |
| 2328 | |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2329 | static void mmc_blk_rpmb_device_release(struct device *dev) |
| 2330 | { |
| 2331 | struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev); |
| 2332 | |
| 2333 | ida_simple_remove(&mmc_rpmb_ida, rpmb->id); |
| 2334 | kfree(rpmb); |
| 2335 | } |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2336 | |
| 2337 | static int mmc_blk_alloc_rpmb_part(struct mmc_card *card, |
| 2338 | struct mmc_blk_data *md, |
| 2339 | unsigned int part_index, |
| 2340 | sector_t size, |
| 2341 | const char *subname) |
| 2342 | { |
| 2343 | int devidx, ret; |
| 2344 | char rpmb_name[DISK_NAME_LEN]; |
| 2345 | char cap_str[10]; |
| 2346 | struct mmc_rpmb_data *rpmb; |
| 2347 | |
| 2348 | /* This creates the minor number for the RPMB char device */ |
| 2349 | devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL); |
| 2350 | if (devidx < 0) |
| 2351 | return devidx; |
| 2352 | |
| 2353 | rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL); |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2354 | if (!rpmb) { |
| 2355 | ida_simple_remove(&mmc_rpmb_ida, devidx); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2356 | return -ENOMEM; |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2357 | } |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2358 | |
| 2359 | snprintf(rpmb_name, sizeof(rpmb_name), |
| 2360 | "mmcblk%u%s", card->host->index, subname ? subname : ""); |
| 2361 | |
| 2362 | rpmb->id = devidx; |
| 2363 | rpmb->part_index = part_index; |
| 2364 | rpmb->dev.init_name = rpmb_name; |
| 2365 | rpmb->dev.bus = &mmc_rpmb_bus_type; |
| 2366 | rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id); |
| 2367 | rpmb->dev.parent = &card->dev; |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2368 | rpmb->dev.release = mmc_blk_rpmb_device_release; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2369 | device_initialize(&rpmb->dev); |
| 2370 | dev_set_drvdata(&rpmb->dev, rpmb); |
| 2371 | rpmb->md = md; |
| 2372 | |
| 2373 | cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops); |
| 2374 | rpmb->chrdev.owner = THIS_MODULE; |
| 2375 | ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev); |
| 2376 | if (ret) { |
| 2377 | pr_err("%s: could not add character device\n", rpmb_name); |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2378 | goto out_put_device; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | list_add(&rpmb->node, &md->rpmbs); |
| 2382 | |
| 2383 | string_get_size((u64)size, 512, STRING_UNITS_2, |
| 2384 | cap_str, sizeof(cap_str)); |
| 2385 | |
| 2386 | pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n", |
| 2387 | rpmb_name, mmc_card_id(card), |
| 2388 | mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str, |
| 2389 | MAJOR(mmc_rpmb_devt), rpmb->id); |
| 2390 | |
| 2391 | return 0; |
| 2392 | |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2393 | out_put_device: |
| 2394 | put_device(&rpmb->dev); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2395 | return ret; |
| 2396 | } |
| 2397 | |
| 2398 | static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb) |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2399 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2400 | { |
| 2401 | cdev_device_del(&rpmb->chrdev, &rpmb->dev); |
Linus Walleij | 1c87f73 | 2017-10-04 11:10:07 +0200 | [diff] [blame] | 2402 | put_device(&rpmb->dev); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2403 | } |
| 2404 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2405 | /* MMC Physical partitions consist of two boot partitions and |
| 2406 | * up to four general purpose partitions. |
| 2407 | * For each partition enabled in EXT_CSD a block device will be allocatedi |
| 2408 | * to provide access to the partition. |
| 2409 | */ |
| 2410 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2411 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 2412 | { |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2413 | int idx, ret; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2414 | |
| 2415 | if (!mmc_card_mmc(card)) |
| 2416 | return 0; |
| 2417 | |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2418 | for (idx = 0; idx < card->nr_parts; idx++) { |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2419 | if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) { |
| 2420 | /* |
| 2421 | * RPMB partitions does not provide block access, they |
| 2422 | * are only accessed using ioctl():s. Thus create |
| 2423 | * special RPMB block devices that do not have a |
| 2424 | * backing block queue for these. |
| 2425 | */ |
| 2426 | ret = mmc_blk_alloc_rpmb_part(card, md, |
| 2427 | card->part[idx].part_cfg, |
| 2428 | card->part[idx].size >> 9, |
| 2429 | card->part[idx].name); |
| 2430 | if (ret) |
| 2431 | return ret; |
| 2432 | } else if (card->part[idx].size) { |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2433 | ret = mmc_blk_alloc_part(card, md, |
| 2434 | card->part[idx].part_cfg, |
| 2435 | card->part[idx].size >> 9, |
| 2436 | card->part[idx].force_ro, |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2437 | card->part[idx].name, |
| 2438 | card->part[idx].area_type); |
Namjae Jeon | e0c368d | 2011-10-06 23:41:38 +0900 | [diff] [blame] | 2439 | if (ret) |
| 2440 | return ret; |
| 2441 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2442 | } |
| 2443 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2444 | return 0; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2445 | } |
| 2446 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2447 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 2448 | { |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2449 | struct mmc_card *card; |
| 2450 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2451 | if (md) { |
Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 2452 | /* |
| 2453 | * Flush remaining requests and free queues. It |
| 2454 | * is freeing the queue that stops new requests |
| 2455 | * from being accepted. |
| 2456 | */ |
Franck Jullien | 8efb83a | 2013-07-24 15:17:48 +0200 | [diff] [blame] | 2457 | card = md->queue.card; |
Michał Mirosław | 3f8b23a | 2017-08-09 01:48:59 +0200 | [diff] [blame] | 2458 | spin_lock_irq(md->queue.queue->queue_lock); |
Shawn Lin | 7c84b8b | 2017-07-25 09:11:28 +0800 | [diff] [blame] | 2459 | queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue); |
Michał Mirosław | 3f8b23a | 2017-08-09 01:48:59 +0200 | [diff] [blame] | 2460 | spin_unlock_irq(md->queue.queue->queue_lock); |
Grzegorz Sluja | bbdc74d | 2017-07-13 11:17:58 +0200 | [diff] [blame] | 2461 | blk_set_queue_dying(md->queue.queue); |
Paul Taysom | fdfa20c | 2013-06-04 14:42:40 -0700 | [diff] [blame] | 2462 | mmc_cleanup_queue(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2463 | if (md->disk->flags & GENHD_FL_UP) { |
| 2464 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2465 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 2466 | card->ext_csd.boot_ro_lockable) |
| 2467 | device_remove_file(disk_to_dev(md->disk), |
| 2468 | &md->power_ro_lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2469 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2470 | del_gendisk(md->disk); |
| 2471 | } |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2472 | mmc_blk_put(md); |
| 2473 | } |
| 2474 | } |
| 2475 | |
| 2476 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 2477 | struct mmc_blk_data *md) |
| 2478 | { |
| 2479 | struct list_head *pos, *q; |
| 2480 | struct mmc_blk_data *part_md; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2481 | struct mmc_rpmb_data *rpmb; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2482 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2483 | /* Remove RPMB partitions */ |
| 2484 | list_for_each_safe(pos, q, &md->rpmbs) { |
| 2485 | rpmb = list_entry(pos, struct mmc_rpmb_data, node); |
| 2486 | list_del(pos); |
| 2487 | mmc_blk_remove_rpmb_part(rpmb); |
| 2488 | } |
| 2489 | /* Remove block partitions */ |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2490 | list_for_each_safe(pos, q, &md->part) { |
| 2491 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 2492 | list_del(pos); |
| 2493 | mmc_blk_remove_req(part_md); |
| 2494 | } |
| 2495 | } |
| 2496 | |
| 2497 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 2498 | { |
| 2499 | int ret; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2500 | struct mmc_card *card = md->queue.card; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2501 | |
Dan Williams | 307d8e6 | 2016-06-20 10:40:44 -0700 | [diff] [blame] | 2502 | device_add_disk(md->parent, md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2503 | md->force_ro.show = force_ro_show; |
| 2504 | md->force_ro.store = force_ro_store; |
Rabin Vincent | 641c318 | 2011-04-23 20:52:58 +0530 | [diff] [blame] | 2505 | sysfs_attr_init(&md->force_ro.attr); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2506 | md->force_ro.attr.name = "force_ro"; |
| 2507 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 2508 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 2509 | if (ret) |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2510 | goto force_ro_fail; |
| 2511 | |
| 2512 | if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) && |
| 2513 | card->ext_csd.boot_ro_lockable) { |
Al Viro | 8818739 | 2012-03-20 06:00:24 -0400 | [diff] [blame] | 2514 | umode_t mode; |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2515 | |
| 2516 | if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS) |
| 2517 | mode = S_IRUGO; |
| 2518 | else |
| 2519 | mode = S_IRUGO | S_IWUSR; |
| 2520 | |
| 2521 | md->power_ro_lock.show = power_ro_lock_show; |
| 2522 | md->power_ro_lock.store = power_ro_lock_store; |
Rabin Vincent | 00d9ac0 | 2012-02-01 16:31:56 +0100 | [diff] [blame] | 2523 | sysfs_attr_init(&md->power_ro_lock.attr); |
Johan Rudholm | add710e | 2011-12-02 08:51:06 +0100 | [diff] [blame] | 2524 | md->power_ro_lock.attr.mode = mode; |
| 2525 | md->power_ro_lock.attr.name = |
| 2526 | "ro_lock_until_next_power_on"; |
| 2527 | ret = device_create_file(disk_to_dev(md->disk), |
| 2528 | &md->power_ro_lock); |
| 2529 | if (ret) |
| 2530 | goto power_ro_lock_fail; |
| 2531 | } |
| 2532 | return ret; |
| 2533 | |
| 2534 | power_ro_lock_fail: |
| 2535 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 2536 | force_ro_fail: |
| 2537 | del_gendisk(md->disk); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2538 | |
| 2539 | return ret; |
| 2540 | } |
| 2541 | |
Linus Walleij | 627c3cc | 2017-08-20 23:39:08 +0200 | [diff] [blame] | 2542 | #ifdef CONFIG_DEBUG_FS |
| 2543 | |
| 2544 | static int mmc_dbg_card_status_get(void *data, u64 *val) |
| 2545 | { |
| 2546 | struct mmc_card *card = data; |
| 2547 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
| 2548 | struct mmc_queue *mq = &md->queue; |
| 2549 | struct request *req; |
| 2550 | int ret; |
| 2551 | |
| 2552 | /* Ask the block layer about the card status */ |
| 2553 | req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM); |
| 2554 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS; |
| 2555 | blk_execute_rq(mq->queue, NULL, req, 0); |
| 2556 | ret = req_to_mmc_queue_req(req)->drv_op_result; |
| 2557 | if (ret >= 0) { |
| 2558 | *val = ret; |
| 2559 | ret = 0; |
| 2560 | } |
Adrian Hunter | 34c089e | 2017-11-21 15:42:27 +0200 | [diff] [blame^] | 2561 | blk_put_request(req); |
Linus Walleij | 627c3cc | 2017-08-20 23:39:08 +0200 | [diff] [blame] | 2562 | |
| 2563 | return ret; |
| 2564 | } |
| 2565 | DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get, |
| 2566 | NULL, "%08llx\n"); |
| 2567 | |
| 2568 | /* That is two digits * 512 + 1 for newline */ |
| 2569 | #define EXT_CSD_STR_LEN 1025 |
| 2570 | |
| 2571 | static int mmc_ext_csd_open(struct inode *inode, struct file *filp) |
| 2572 | { |
| 2573 | struct mmc_card *card = inode->i_private; |
| 2574 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
| 2575 | struct mmc_queue *mq = &md->queue; |
| 2576 | struct request *req; |
| 2577 | char *buf; |
| 2578 | ssize_t n = 0; |
| 2579 | u8 *ext_csd; |
| 2580 | int err, i; |
| 2581 | |
| 2582 | buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL); |
| 2583 | if (!buf) |
| 2584 | return -ENOMEM; |
| 2585 | |
| 2586 | /* Ask the block layer for the EXT CSD */ |
| 2587 | req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM); |
| 2588 | req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD; |
| 2589 | req_to_mmc_queue_req(req)->drv_op_data = &ext_csd; |
| 2590 | blk_execute_rq(mq->queue, NULL, req, 0); |
| 2591 | err = req_to_mmc_queue_req(req)->drv_op_result; |
Adrian Hunter | 34c089e | 2017-11-21 15:42:27 +0200 | [diff] [blame^] | 2592 | blk_put_request(req); |
Linus Walleij | 627c3cc | 2017-08-20 23:39:08 +0200 | [diff] [blame] | 2593 | if (err) { |
| 2594 | pr_err("FAILED %d\n", err); |
| 2595 | goto out_free; |
| 2596 | } |
| 2597 | |
| 2598 | for (i = 0; i < 512; i++) |
| 2599 | n += sprintf(buf + n, "%02x", ext_csd[i]); |
| 2600 | n += sprintf(buf + n, "\n"); |
| 2601 | |
| 2602 | if (n != EXT_CSD_STR_LEN) { |
| 2603 | err = -EINVAL; |
| 2604 | goto out_free; |
| 2605 | } |
| 2606 | |
| 2607 | filp->private_data = buf; |
| 2608 | kfree(ext_csd); |
| 2609 | return 0; |
| 2610 | |
| 2611 | out_free: |
| 2612 | kfree(buf); |
| 2613 | return err; |
| 2614 | } |
| 2615 | |
| 2616 | static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf, |
| 2617 | size_t cnt, loff_t *ppos) |
| 2618 | { |
| 2619 | char *buf = filp->private_data; |
| 2620 | |
| 2621 | return simple_read_from_buffer(ubuf, cnt, ppos, |
| 2622 | buf, EXT_CSD_STR_LEN); |
| 2623 | } |
| 2624 | |
| 2625 | static int mmc_ext_csd_release(struct inode *inode, struct file *file) |
| 2626 | { |
| 2627 | kfree(file->private_data); |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | static const struct file_operations mmc_dbg_ext_csd_fops = { |
| 2632 | .open = mmc_ext_csd_open, |
| 2633 | .read = mmc_ext_csd_read, |
| 2634 | .release = mmc_ext_csd_release, |
| 2635 | .llseek = default_llseek, |
| 2636 | }; |
| 2637 | |
| 2638 | static int mmc_blk_add_debugfs(struct mmc_card *card) |
| 2639 | { |
| 2640 | struct dentry *root; |
| 2641 | |
| 2642 | if (!card->debugfs_root) |
| 2643 | return 0; |
| 2644 | |
| 2645 | root = card->debugfs_root; |
| 2646 | |
| 2647 | if (mmc_card_mmc(card) || mmc_card_sd(card)) { |
| 2648 | if (!debugfs_create_file("status", S_IRUSR, root, card, |
| 2649 | &mmc_dbg_card_status_fops)) |
| 2650 | return -EIO; |
| 2651 | } |
| 2652 | |
| 2653 | if (mmc_card_mmc(card)) { |
| 2654 | if (!debugfs_create_file("ext_csd", S_IRUSR, root, card, |
| 2655 | &mmc_dbg_ext_csd_fops)) |
| 2656 | return -EIO; |
| 2657 | } |
| 2658 | |
| 2659 | return 0; |
| 2660 | } |
| 2661 | |
| 2662 | |
| 2663 | #else |
| 2664 | |
| 2665 | static int mmc_blk_add_debugfs(struct mmc_card *card) |
| 2666 | { |
| 2667 | return 0; |
| 2668 | } |
| 2669 | |
| 2670 | #endif /* CONFIG_DEBUG_FS */ |
| 2671 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2672 | static int mmc_blk_probe(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2674 | struct mmc_blk_data *md, *part_md; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2675 | char cap_str[10]; |
| 2676 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 2677 | /* |
| 2678 | * Check that the card supports the command class(es) we need. |
| 2679 | */ |
| 2680 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | return -ENODEV; |
| 2682 | |
Shawn Lin | 8c7cdbf | 2017-02-15 16:36:47 +0800 | [diff] [blame] | 2683 | mmc_fixup_device(card, mmc_blk_fixups); |
Lukas Czerner | 5204d00 | 2014-06-18 13:18:07 +0200 | [diff] [blame] | 2684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2685 | md = mmc_blk_alloc(card); |
Linus Walleij | 304419d | 2017-05-18 11:29:32 +0200 | [diff] [blame] | 2686 | if (IS_ERR(md)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | return PTR_ERR(md); |
| 2688 | |
James Bottomley | b9f28d8 | 2015-03-05 18:47:01 -0800 | [diff] [blame] | 2689 | string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2690 | cap_str, sizeof(cap_str)); |
Girish K S | a3c76eb | 2011-10-11 11:44:09 +0530 | [diff] [blame] | 2691 | pr_info("%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 2693 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2694 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2695 | if (mmc_blk_alloc_parts(card, md)) |
| 2696 | goto out; |
| 2697 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2698 | dev_set_drvdata(&card->dev, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 2699 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2700 | if (mmc_add_disk(md)) |
| 2701 | goto out; |
| 2702 | |
| 2703 | list_for_each_entry(part_md, &md->part, part) { |
| 2704 | if (mmc_add_disk(part_md)) |
| 2705 | goto out; |
| 2706 | } |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2707 | |
Linus Walleij | 627c3cc | 2017-08-20 23:39:08 +0200 | [diff] [blame] | 2708 | /* Add two debugfs entries */ |
| 2709 | mmc_blk_add_debugfs(card); |
| 2710 | |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2711 | pm_runtime_set_autosuspend_delay(&card->dev, 3000); |
| 2712 | pm_runtime_use_autosuspend(&card->dev); |
| 2713 | |
| 2714 | /* |
| 2715 | * Don't enable runtime PM for SD-combo cards here. Leave that |
| 2716 | * decision to be taken during the SDIO init sequence instead. |
| 2717 | */ |
| 2718 | if (card->type != MMC_TYPE_SD_COMBO) { |
| 2719 | pm_runtime_set_active(&card->dev); |
| 2720 | pm_runtime_enable(&card->dev); |
| 2721 | } |
| 2722 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | return 0; |
| 2724 | |
| 2725 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2726 | mmc_blk_remove_parts(card, md); |
| 2727 | mmc_blk_remove_req(md); |
Ulf Hansson | 5865f28 | 2012-03-22 11:47:26 +0100 | [diff] [blame] | 2728 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | } |
| 2730 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2731 | static void mmc_blk_remove(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2733 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2735 | mmc_blk_remove_parts(card, md); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2736 | pm_runtime_get_sync(&card->dev); |
Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 2737 | mmc_claim_host(card->host); |
Linus Walleij | 1f797ed | 2017-08-20 23:39:10 +0200 | [diff] [blame] | 2738 | mmc_blk_part_switch(card, md->part_type); |
Adrian Hunter | ddd6fa7 | 2011-06-23 13:40:26 +0300 | [diff] [blame] | 2739 | mmc_release_host(card->host); |
Ulf Hansson | e94cfef | 2013-05-02 14:02:38 +0200 | [diff] [blame] | 2740 | if (card->type != MMC_TYPE_SD_COMBO) |
| 2741 | pm_runtime_disable(&card->dev); |
| 2742 | pm_runtime_put_noidle(&card->dev); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2743 | mmc_blk_remove_req(md); |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2744 | dev_set_drvdata(&card->dev, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | } |
| 2746 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2747 | static int _mmc_blk_suspend(struct mmc_card *card) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2748 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2749 | struct mmc_blk_data *part_md; |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2750 | struct mmc_blk_data *md = dev_get_drvdata(&card->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | |
| 2752 | if (md) { |
| 2753 | mmc_queue_suspend(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2754 | list_for_each_entry(part_md, &md->part, part) { |
| 2755 | mmc_queue_suspend(&part_md->queue); |
| 2756 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | } |
| 2758 | return 0; |
| 2759 | } |
| 2760 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2761 | static void mmc_blk_shutdown(struct mmc_card *card) |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2762 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2763 | _mmc_blk_suspend(card); |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2764 | } |
| 2765 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 2766 | #ifdef CONFIG_PM_SLEEP |
| 2767 | static int mmc_blk_suspend(struct device *dev) |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2768 | { |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2769 | struct mmc_card *card = mmc_dev_to_card(dev); |
| 2770 | |
| 2771 | return _mmc_blk_suspend(card); |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2772 | } |
| 2773 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 2774 | static int mmc_blk_resume(struct device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2776 | struct mmc_blk_data *part_md; |
Ulf Hansson | fc95e30 | 2014-10-06 14:34:09 +0200 | [diff] [blame] | 2777 | struct mmc_blk_data *md = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | |
| 2779 | if (md) { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2780 | /* |
| 2781 | * Resume involves the card going into idle state, |
| 2782 | * so current partition is always the main one. |
| 2783 | */ |
| 2784 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 2786 | list_for_each_entry(part_md, &md->part, part) { |
| 2787 | mmc_queue_resume(&part_md->queue); |
| 2788 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | } |
| 2790 | return 0; |
| 2791 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | #endif |
| 2793 | |
Ulf Hansson | 0967edc | 2014-10-06 11:29:42 +0200 | [diff] [blame] | 2794 | static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume); |
| 2795 | |
Ulf Hansson | 96541ba | 2015-04-14 13:06:12 +0200 | [diff] [blame] | 2796 | static struct mmc_driver mmc_driver = { |
| 2797 | .drv = { |
| 2798 | .name = "mmcblk", |
| 2799 | .pm = &mmc_blk_pm_ops, |
| 2800 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2801 | .probe = mmc_blk_probe, |
| 2802 | .remove = mmc_blk_remove, |
Ulf Hansson | 7628774 | 2013-06-10 17:03:40 +0200 | [diff] [blame] | 2803 | .shutdown = mmc_blk_shutdown, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | }; |
| 2805 | |
| 2806 | static int __init mmc_blk_init(void) |
| 2807 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2808 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2810 | res = bus_register(&mmc_rpmb_bus_type); |
| 2811 | if (res < 0) { |
| 2812 | pr_err("mmcblk: could not register RPMB bus type\n"); |
| 2813 | return res; |
| 2814 | } |
| 2815 | res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb"); |
| 2816 | if (res < 0) { |
| 2817 | pr_err("mmcblk: failed to allocate rpmb chrdev region\n"); |
| 2818 | goto out_bus_unreg; |
| 2819 | } |
| 2820 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2821 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 2822 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 2823 | |
Ben Hutchings | a26eba6 | 2014-11-06 03:35:09 +0000 | [diff] [blame] | 2824 | max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 2825 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2826 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 2827 | if (res) |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2828 | goto out_chrdev_unreg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2830 | res = mmc_register_driver(&mmc_driver); |
| 2831 | if (res) |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2832 | goto out_blkdev_unreg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2833 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2834 | return 0; |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2835 | |
| 2836 | out_blkdev_unreg: |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 2837 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2838 | out_chrdev_unreg: |
| 2839 | unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); |
| 2840 | out_bus_unreg: |
| 2841 | bus_unregister(&mmc_rpmb_bus_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | return res; |
| 2843 | } |
| 2844 | |
| 2845 | static void __exit mmc_blk_exit(void) |
| 2846 | { |
| 2847 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 2848 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Walleij | 9754857 | 2017-09-20 10:02:00 +0200 | [diff] [blame] | 2849 | unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | } |
| 2851 | |
| 2852 | module_init(mmc_blk_init); |
| 2853 | module_exit(mmc_blk_exit); |
| 2854 | |
| 2855 | MODULE_LICENSE("GPL"); |
| 2856 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 2857 | |