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