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