Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Block driver for media (i.e., flash cards) |
| 3 | * |
| 4 | * Copyright 2002 Hewlett-Packard Company |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 5 | * Copyright 2005-2008 Pierre Ossman |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * Use consistent with the GNU GPL is permitted, |
| 8 | * provided that this copyright notice is |
| 9 | * preserved in its entirety in all copies and derived works. |
| 10 | * |
| 11 | * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED, |
| 12 | * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS |
| 13 | * FITNESS FOR ANY PARTICULAR PURPOSE. |
| 14 | * |
| 15 | * Many thanks to Alessandro Rubini and Jonathan Corbet! |
| 16 | * |
| 17 | * Author: Andrew Christian |
| 18 | * 28 May 2002 |
| 19 | */ |
| 20 | #include <linux/moduleparam.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/init.h> |
| 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include <linux/kernel.h> |
| 25 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #include <linux/errno.h> |
| 28 | #include <linux/hdreg.h> |
| 29 | #include <linux/kdev_t.h> |
| 30 | #include <linux/blkdev.h> |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 31 | #include <linux/mutex.h> |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 32 | #include <linux/scatterlist.h> |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 33 | #include <linux/string_helpers.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | #include <linux/mmc/card.h> |
Pierre Ossman | 385e3227 | 2006-06-18 14:34:37 +0200 | [diff] [blame] | 36 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 37 | #include <linux/mmc/mmc.h> |
| 38 | #include <linux/mmc/sd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
| 40 | #include <asm/system.h> |
| 41 | #include <asm/uaccess.h> |
| 42 | |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 43 | #include "queue.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
Andy Whitcroft | 6b0b628 | 2009-02-23 12:38:41 +0000 | [diff] [blame] | 45 | MODULE_ALIAS("mmc:block"); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 46 | #ifdef MODULE_PARAM_PREFIX |
| 47 | #undef MODULE_PARAM_PREFIX |
| 48 | #endif |
| 49 | #define MODULE_PARAM_PREFIX "mmcblk." |
David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 50 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame^] | 51 | #define INAND_CMD38_ARG_EXT_CSD 113 |
| 52 | #define INAND_CMD38_ARG_ERASE 0x00 |
| 53 | #define INAND_CMD38_ARG_TRIM 0x01 |
| 54 | #define INAND_CMD38_ARG_SECERASE 0x80 |
| 55 | #define INAND_CMD38_ARG_SECTRIM1 0x81 |
| 56 | #define INAND_CMD38_ARG_SECTRIM2 0x88 |
| 57 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 58 | #define REL_WRITES_SUPPORTED(card) (mmc_card_mmc((card)) && \ |
| 59 | (((card)->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || \ |
| 60 | ((card)->ext_csd.rel_sectors))) |
| 61 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 62 | static DEFINE_MUTEX(block_mutex); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * The defaults come from config options but can be overriden by module |
| 66 | * or bootarg options. |
| 67 | */ |
| 68 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
| 69 | |
| 70 | /* |
| 71 | * We've only got one major, so number of mmcblk devices is |
| 72 | * limited to 256 / number of minors per device. |
| 73 | */ |
| 74 | static int max_devices; |
| 75 | |
| 76 | /* 256 minors, so at most 256 separate devices */ |
| 77 | static DECLARE_BITMAP(dev_use, 256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | /* |
| 80 | * There is one mmc_blk_data per slot. |
| 81 | */ |
| 82 | struct mmc_blk_data { |
| 83 | spinlock_t lock; |
| 84 | struct gendisk *disk; |
| 85 | struct mmc_queue queue; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 86 | struct list_head part; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
| 88 | unsigned int usage; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 89 | unsigned int read_only; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 90 | unsigned int part_type; |
| 91 | |
| 92 | /* |
| 93 | * Only set in main mmc_blk_data associated |
| 94 | * with mmc_card with mmc_set_drvdata, and keeps |
| 95 | * track of the current selected device partition. |
| 96 | */ |
| 97 | unsigned int part_curr; |
| 98 | struct device_attribute force_ro; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | }; |
| 100 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 101 | static DEFINE_MUTEX(open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 103 | module_param(perdev_minors, int, 0444); |
| 104 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); |
| 105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
| 107 | { |
| 108 | struct mmc_blk_data *md; |
| 109 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 110 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | md = disk->private_data; |
| 112 | if (md && md->usage == 0) |
| 113 | md = NULL; |
| 114 | if (md) |
| 115 | md->usage++; |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 116 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | |
| 118 | return md; |
| 119 | } |
| 120 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 121 | static inline int mmc_get_devidx(struct gendisk *disk) |
| 122 | { |
| 123 | int devmaj = MAJOR(disk_devt(disk)); |
| 124 | int devidx = MINOR(disk_devt(disk)) / perdev_minors; |
| 125 | |
| 126 | if (!devmaj) |
| 127 | devidx = disk->first_minor / perdev_minors; |
| 128 | return devidx; |
| 129 | } |
| 130 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | static void mmc_blk_put(struct mmc_blk_data *md) |
| 132 | { |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 133 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | md->usage--; |
| 135 | if (md->usage == 0) { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 136 | int devidx = mmc_get_devidx(md->disk); |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 137 | blk_cleanup_queue(md->queue.queue); |
| 138 | |
David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 139 | __clear_bit(devidx, dev_use); |
| 140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | put_disk(md->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | kfree(md); |
| 143 | } |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 144 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | } |
| 146 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 147 | static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr, |
| 148 | char *buf) |
| 149 | { |
| 150 | int ret; |
| 151 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 152 | |
| 153 | ret = snprintf(buf, PAGE_SIZE, "%d", |
| 154 | get_disk_ro(dev_to_disk(dev)) ^ |
| 155 | md->read_only); |
| 156 | mmc_blk_put(md); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr, |
| 161 | const char *buf, size_t count) |
| 162 | { |
| 163 | int ret; |
| 164 | char *end; |
| 165 | struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev)); |
| 166 | unsigned long set = simple_strtoul(buf, &end, 0); |
| 167 | if (end == buf) { |
| 168 | ret = -EINVAL; |
| 169 | goto out; |
| 170 | } |
| 171 | |
| 172 | set_disk_ro(dev_to_disk(dev), set || md->read_only); |
| 173 | ret = count; |
| 174 | out: |
| 175 | mmc_blk_put(md); |
| 176 | return ret; |
| 177 | } |
| 178 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 179 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 181 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | int ret = -ENXIO; |
| 183 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 184 | mutex_lock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | if (md) { |
| 186 | if (md->usage == 2) |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 187 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | ret = 0; |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 189 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 190 | if ((mode & FMODE_WRITE) && md->read_only) { |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 191 | mmc_blk_put(md); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 192 | ret = -EROFS; |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 193 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 195 | mutex_unlock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
| 197 | return ret; |
| 198 | } |
| 199 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 200 | static int mmc_blk_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 202 | struct mmc_blk_data *md = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 204 | mutex_lock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | mmc_blk_put(md); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 206 | mutex_unlock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 211 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 213 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
| 214 | geo->heads = 4; |
| 215 | geo->sectors = 16; |
| 216 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 219 | static const struct block_device_operations mmc_bdops = { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 220 | .open = mmc_blk_open, |
| 221 | .release = mmc_blk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 222 | .getgeo = mmc_blk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | .owner = THIS_MODULE, |
| 224 | }; |
| 225 | |
| 226 | struct mmc_blk_request { |
| 227 | struct mmc_request mrq; |
| 228 | struct mmc_command cmd; |
| 229 | struct mmc_command stop; |
| 230 | struct mmc_data data; |
| 231 | }; |
| 232 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 233 | static inline int mmc_blk_part_switch(struct mmc_card *card, |
| 234 | struct mmc_blk_data *md) |
| 235 | { |
| 236 | int ret; |
| 237 | struct mmc_blk_data *main_md = mmc_get_drvdata(card); |
| 238 | if (main_md->part_curr == md->part_type) |
| 239 | return 0; |
| 240 | |
| 241 | if (mmc_card_mmc(card)) { |
| 242 | card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK; |
| 243 | card->ext_csd.part_config |= md->part_type; |
| 244 | |
| 245 | ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 246 | EXT_CSD_PART_CONFIG, card->ext_csd.part_config, |
| 247 | card->ext_csd.part_time); |
| 248 | if (ret) |
| 249 | return ret; |
| 250 | } |
| 251 | |
| 252 | main_md->part_curr = md->part_type; |
| 253 | return 0; |
| 254 | } |
| 255 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 256 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
| 257 | { |
| 258 | int err; |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 259 | u32 result; |
| 260 | __be32 *blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 261 | |
| 262 | struct mmc_request mrq; |
| 263 | struct mmc_command cmd; |
| 264 | struct mmc_data data; |
| 265 | unsigned int timeout_us; |
| 266 | |
| 267 | struct scatterlist sg; |
| 268 | |
| 269 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 270 | |
| 271 | cmd.opcode = MMC_APP_CMD; |
| 272 | cmd.arg = card->rca << 16; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 273 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 274 | |
| 275 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 276 | if (err) |
| 277 | return (u32)-1; |
| 278 | if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD)) |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 279 | return (u32)-1; |
| 280 | |
| 281 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 282 | |
| 283 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 284 | cmd.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 285 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 286 | |
| 287 | memset(&data, 0, sizeof(struct mmc_data)); |
| 288 | |
| 289 | data.timeout_ns = card->csd.tacc_ns * 100; |
| 290 | data.timeout_clks = card->csd.tacc_clks * 100; |
| 291 | |
| 292 | timeout_us = data.timeout_ns / 1000; |
| 293 | timeout_us += data.timeout_clks * 1000 / |
| 294 | (card->host->ios.clock / 1000); |
| 295 | |
| 296 | if (timeout_us > 100000) { |
| 297 | data.timeout_ns = 100000000; |
| 298 | data.timeout_clks = 0; |
| 299 | } |
| 300 | |
| 301 | data.blksz = 4; |
| 302 | data.blocks = 1; |
| 303 | data.flags = MMC_DATA_READ; |
| 304 | data.sg = &sg; |
| 305 | data.sg_len = 1; |
| 306 | |
| 307 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 308 | |
| 309 | mrq.cmd = &cmd; |
| 310 | mrq.data = &data; |
| 311 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 312 | blocks = kmalloc(4, GFP_KERNEL); |
| 313 | if (!blocks) |
| 314 | return (u32)-1; |
| 315 | |
| 316 | sg_init_one(&sg, blocks, 4); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 317 | |
| 318 | mmc_wait_for_req(card->host, &mrq); |
| 319 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 320 | result = ntohl(*blocks); |
| 321 | kfree(blocks); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 322 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 323 | if (cmd.error || data.error) |
| 324 | result = (u32)-1; |
| 325 | |
| 326 | return result; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 329 | static u32 get_card_status(struct mmc_card *card, struct request *req) |
| 330 | { |
| 331 | struct mmc_command cmd; |
| 332 | int err; |
| 333 | |
| 334 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 335 | cmd.opcode = MMC_SEND_STATUS; |
| 336 | if (!mmc_host_is_spi(card->host)) |
| 337 | cmd.arg = card->rca << 16; |
| 338 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; |
| 339 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 340 | if (err) |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 341 | printk(KERN_ERR "%s: error %d sending status command", |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 342 | req->rq_disk->disk_name, err); |
| 343 | return cmd.resp[0]; |
| 344 | } |
| 345 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 346 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
| 347 | { |
| 348 | struct mmc_blk_data *md = mq->data; |
| 349 | struct mmc_card *card = md->queue.card; |
| 350 | unsigned int from, nr, arg; |
| 351 | int err = 0; |
| 352 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 353 | if (!mmc_can_erase(card)) { |
| 354 | err = -EOPNOTSUPP; |
| 355 | goto out; |
| 356 | } |
| 357 | |
| 358 | from = blk_rq_pos(req); |
| 359 | nr = blk_rq_sectors(req); |
| 360 | |
| 361 | if (mmc_can_trim(card)) |
| 362 | arg = MMC_TRIM_ARG; |
| 363 | else |
| 364 | arg = MMC_ERASE_ARG; |
| 365 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame^] | 366 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 367 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 368 | INAND_CMD38_ARG_EXT_CSD, |
| 369 | arg == MMC_TRIM_ARG ? |
| 370 | INAND_CMD38_ARG_TRIM : |
| 371 | INAND_CMD38_ARG_ERASE, |
| 372 | 0); |
| 373 | if (err) |
| 374 | goto out; |
| 375 | } |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 376 | err = mmc_erase(card, from, nr, arg); |
| 377 | out: |
| 378 | spin_lock_irq(&md->lock); |
| 379 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 380 | spin_unlock_irq(&md->lock); |
| 381 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 382 | return err ? 0 : 1; |
| 383 | } |
| 384 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 385 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
| 386 | struct request *req) |
| 387 | { |
| 388 | struct mmc_blk_data *md = mq->data; |
| 389 | struct mmc_card *card = md->queue.card; |
| 390 | unsigned int from, nr, arg; |
| 391 | int err = 0; |
| 392 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 393 | if (!mmc_can_secure_erase_trim(card)) { |
| 394 | err = -EOPNOTSUPP; |
| 395 | goto out; |
| 396 | } |
| 397 | |
| 398 | from = blk_rq_pos(req); |
| 399 | nr = blk_rq_sectors(req); |
| 400 | |
| 401 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
| 402 | arg = MMC_SECURE_TRIM1_ARG; |
| 403 | else |
| 404 | arg = MMC_SECURE_ERASE_ARG; |
| 405 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame^] | 406 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 407 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 408 | INAND_CMD38_ARG_EXT_CSD, |
| 409 | arg == MMC_SECURE_TRIM1_ARG ? |
| 410 | INAND_CMD38_ARG_SECTRIM1 : |
| 411 | INAND_CMD38_ARG_SECERASE, |
| 412 | 0); |
| 413 | if (err) |
| 414 | goto out; |
| 415 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 416 | err = mmc_erase(card, from, nr, arg); |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame^] | 417 | if (!err && arg == MMC_SECURE_TRIM1_ARG) { |
| 418 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 419 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 420 | INAND_CMD38_ARG_EXT_CSD, |
| 421 | INAND_CMD38_ARG_SECTRIM2, |
| 422 | 0); |
| 423 | if (err) |
| 424 | goto out; |
| 425 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 426 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame^] | 427 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 428 | out: |
| 429 | spin_lock_irq(&md->lock); |
| 430 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 431 | spin_unlock_irq(&md->lock); |
| 432 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 433 | return err ? 0 : 1; |
| 434 | } |
| 435 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 436 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
| 437 | { |
| 438 | struct mmc_blk_data *md = mq->data; |
| 439 | |
| 440 | /* |
| 441 | * No-op, only service this because we need REQ_FUA for reliable |
| 442 | * writes. |
| 443 | */ |
| 444 | spin_lock_irq(&md->lock); |
| 445 | __blk_end_request_all(req, 0); |
| 446 | spin_unlock_irq(&md->lock); |
| 447 | |
| 448 | return 1; |
| 449 | } |
| 450 | |
| 451 | /* |
| 452 | * Reformat current write as a reliable write, supporting |
| 453 | * both legacy and the enhanced reliable write MMC cards. |
| 454 | * In each transfer we'll handle only as much as a single |
| 455 | * reliable write can handle, thus finish the request in |
| 456 | * partial completions. |
| 457 | */ |
| 458 | static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 459 | struct mmc_card *card, |
| 460 | struct request *req) |
| 461 | { |
| 462 | int err; |
| 463 | struct mmc_command set_count; |
| 464 | |
| 465 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 466 | /* Legacy mode imposes restrictions on transfers. */ |
| 467 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) |
| 468 | brq->data.blocks = 1; |
| 469 | |
| 470 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 471 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 472 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 473 | brq->data.blocks = 1; |
| 474 | } |
| 475 | |
| 476 | memset(&set_count, 0, sizeof(struct mmc_command)); |
| 477 | set_count.opcode = MMC_SET_BLOCK_COUNT; |
| 478 | set_count.arg = brq->data.blocks | (1 << 31); |
| 479 | set_count.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 480 | err = mmc_wait_for_cmd(card->host, &set_count, 0); |
| 481 | if (err) |
| 482 | printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n", |
| 483 | req->rq_disk->disk_name, err); |
| 484 | return err; |
| 485 | } |
| 486 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 487 | static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 489 | struct mmc_blk_data *md = mq->data; |
| 490 | struct mmc_card *card = md->queue.card; |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 491 | struct mmc_blk_request brq; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 492 | int ret = 1, disable_multi = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 494 | /* |
| 495 | * Reliable writes are used to implement Forced Unit Access and |
| 496 | * REQ_META accesses, and are supported only on MMCs. |
| 497 | */ |
| 498 | bool do_rel_wr = ((req->cmd_flags & REQ_FUA) || |
| 499 | (req->cmd_flags & REQ_META)) && |
| 500 | (rq_data_dir(req) == WRITE) && |
| 501 | REL_WRITES_SUPPORTED(card); |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | struct mmc_command cmd; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 505 | u32 readcmd, writecmd, status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | |
| 507 | memset(&brq, 0, sizeof(struct mmc_blk_request)); |
| 508 | brq.mrq.cmd = &brq.cmd; |
| 509 | brq.mrq.data = &brq.data; |
| 510 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 511 | brq.cmd.arg = blk_rq_pos(req); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 512 | if (!mmc_card_blockaddr(card)) |
| 513 | brq.cmd.arg <<= 9; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 514 | brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | 0884669 | 2008-08-31 14:10:08 +0200 | [diff] [blame] | 515 | brq.data.blksz = 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | brq.stop.opcode = MMC_STOP_TRANSMISSION; |
| 517 | brq.stop.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 518 | brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 519 | brq.data.blocks = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 521 | /* |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 522 | * The block layer doesn't support all sector count |
| 523 | * restrictions, so we need to be prepared for too big |
| 524 | * requests. |
| 525 | */ |
| 526 | if (brq.data.blocks > card->host->max_blk_count) |
| 527 | brq.data.blocks = card->host->max_blk_count; |
| 528 | |
| 529 | /* |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 530 | * After a read error, we redo the request one sector at a time |
| 531 | * in order to accurately determine which sectors can be read |
| 532 | * successfully. |
| 533 | */ |
| 534 | if (disable_multi && brq.data.blocks > 1) |
| 535 | brq.data.blocks = 1; |
| 536 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 537 | if (brq.data.blocks > 1 || do_rel_wr) { |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 538 | /* SPI multiblock writes terminate using a special |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 539 | * token, not a STOP_TRANSMISSION request. Reliable |
| 540 | * writes use SET_BLOCK_COUNT and do not use a |
| 541 | * STOP_TRANSMISSION request either. |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 542 | */ |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 543 | if ((!mmc_host_is_spi(card->host) && !do_rel_wr) || |
| 544 | rq_data_dir(req) == READ) |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 545 | brq.mrq.stop = &brq.stop; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 546 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 547 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 548 | } else { |
| 549 | brq.mrq.stop = NULL; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 550 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 551 | writecmd = MMC_WRITE_BLOCK; |
| 552 | } |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 553 | if (rq_data_dir(req) == READ) { |
| 554 | brq.cmd.opcode = readcmd; |
| 555 | brq.data.flags |= MMC_DATA_READ; |
| 556 | } else { |
| 557 | brq.cmd.opcode = writecmd; |
| 558 | brq.data.flags |= MMC_DATA_WRITE; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 559 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 561 | if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req)) |
| 562 | goto cmd_err; |
| 563 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 564 | mmc_set_data_timeout(&brq.data, card); |
| 565 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 566 | brq.data.sg = mq->sg; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 567 | brq.data.sg_len = mmc_queue_map_sg(mq); |
| 568 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 569 | /* |
| 570 | * Adjust the sg list so it is the same size as the |
| 571 | * request. |
| 572 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 573 | if (brq.data.blocks != blk_rq_sectors(req)) { |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 574 | int i, data_size = brq.data.blocks << 9; |
| 575 | struct scatterlist *sg; |
| 576 | |
| 577 | for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) { |
| 578 | data_size -= sg->length; |
| 579 | if (data_size <= 0) { |
| 580 | sg->length += data_size; |
| 581 | i++; |
| 582 | break; |
| 583 | } |
| 584 | } |
| 585 | brq.data.sg_len = i; |
| 586 | } |
| 587 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 588 | mmc_queue_bounce_pre(mq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | |
| 590 | mmc_wait_for_req(card->host, &brq.mrq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 591 | |
| 592 | mmc_queue_bounce_post(mq); |
| 593 | |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 594 | /* |
| 595 | * Check for errors here, but don't jump to cmd_err |
| 596 | * until later as we need to wait for the card to leave |
| 597 | * programming mode even when things go wrong. |
| 598 | */ |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 599 | if (brq.cmd.error || brq.data.error || brq.stop.error) { |
| 600 | if (brq.data.blocks > 1 && rq_data_dir(req) == READ) { |
| 601 | /* Redo read one sector at a time */ |
| 602 | printk(KERN_WARNING "%s: retrying using single " |
| 603 | "block read\n", req->rq_disk->disk_name); |
| 604 | disable_multi = 1; |
| 605 | continue; |
| 606 | } |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 607 | status = get_card_status(card, req); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 608 | } |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | if (brq.cmd.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 611 | printk(KERN_ERR "%s: error %d sending read/write " |
| 612 | "command, response %#x, card status %#x\n", |
| 613 | req->rq_disk->disk_name, brq.cmd.error, |
| 614 | brq.cmd.resp[0], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | if (brq.data.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 618 | if (brq.data.error == -ETIMEDOUT && brq.mrq.stop) |
| 619 | /* 'Stop' response contains card status */ |
| 620 | status = brq.mrq.stop->resp[0]; |
| 621 | printk(KERN_ERR "%s: error %d transferring data," |
| 622 | " sector %u, nr %u, card status %#x\n", |
| 623 | req->rq_disk->disk_name, brq.data.error, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 624 | (unsigned)blk_rq_pos(req), |
| 625 | (unsigned)blk_rq_sectors(req), status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | } |
| 627 | |
| 628 | if (brq.stop.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 629 | printk(KERN_ERR "%s: error %d sending stop command, " |
| 630 | "response %#x, card status %#x\n", |
| 631 | req->rq_disk->disk_name, brq.stop.error, |
| 632 | brq.stop.resp[0], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | } |
| 634 | |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 635 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 636 | do { |
| 637 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 639 | cmd.opcode = MMC_SEND_STATUS; |
| 640 | cmd.arg = card->rca << 16; |
| 641 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 642 | err = mmc_wait_for_cmd(card->host, &cmd, 5); |
| 643 | if (err) { |
| 644 | printk(KERN_ERR "%s: error %d requesting status\n", |
| 645 | req->rq_disk->disk_name, err); |
| 646 | goto cmd_err; |
| 647 | } |
Pierre Ossman | d198f10 | 2007-11-02 18:21:13 +0100 | [diff] [blame] | 648 | /* |
| 649 | * Some cards mishandle the status bits, |
| 650 | * so make sure to check both the busy |
| 651 | * indication and the card state. |
| 652 | */ |
| 653 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
| 654 | (R1_CURRENT_STATE(cmd.resp[0]) == 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
| 656 | #if 0 |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 657 | if (cmd.resp[0] & ~0x00000900) |
| 658 | printk(KERN_ERR "%s: status = %08x\n", |
| 659 | req->rq_disk->disk_name, cmd.resp[0]); |
| 660 | if (mmc_decode_status(cmd.resp)) |
| 661 | goto cmd_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | #endif |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 663 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 665 | if (brq.cmd.error || brq.stop.error || brq.data.error) { |
| 666 | if (rq_data_dir(req) == READ) { |
| 667 | /* |
| 668 | * After an error, we redo I/O one sector at a |
| 669 | * time, so we only reach here after trying to |
| 670 | * read a single sector. |
| 671 | */ |
| 672 | spin_lock_irq(&md->lock); |
| 673 | ret = __blk_end_request(req, -EIO, brq.data.blksz); |
| 674 | spin_unlock_irq(&md->lock); |
| 675 | continue; |
| 676 | } |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 677 | goto cmd_err; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 678 | } |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 679 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | /* |
| 681 | * A block was successfully transferred. |
| 682 | */ |
| 683 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 684 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | spin_unlock_irq(&md->lock); |
| 686 | } while (ret); |
| 687 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 688 | return 1; |
| 689 | |
| 690 | cmd_err: |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 691 | /* |
| 692 | * If this is an SD card and we're writing, we can first |
| 693 | * mark the known good sectors as ok. |
| 694 | * |
| 695 | * If the card is not SD, we can still ok written sectors |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 696 | * as reported by the controller (which might be less than |
| 697 | * the real number of written sectors, but never more). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | */ |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 699 | if (mmc_card_sd(card)) { |
| 700 | u32 blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 701 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 702 | blocks = mmc_sd_num_wr_blocks(card); |
| 703 | if (blocks != (u32)-1) { |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 704 | spin_lock_irq(&md->lock); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 705 | ret = __blk_end_request(req, 0, blocks << 9); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 706 | spin_unlock_irq(&md->lock); |
| 707 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 708 | } else { |
| 709 | spin_lock_irq(&md->lock); |
| 710 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
| 711 | spin_unlock_irq(&md->lock); |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 712 | } |
| 713 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 715 | while (ret) |
| 716 | ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | spin_unlock_irq(&md->lock); |
| 718 | |
| 719 | return 0; |
| 720 | } |
| 721 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 722 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
| 723 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 724 | int ret; |
| 725 | struct mmc_blk_data *md = mq->data; |
| 726 | struct mmc_card *card = md->queue.card; |
| 727 | |
| 728 | mmc_claim_host(card->host); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 729 | ret = mmc_blk_part_switch(card, md); |
| 730 | if (ret) { |
| 731 | ret = 0; |
| 732 | goto out; |
| 733 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 734 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 735 | if (req->cmd_flags & REQ_DISCARD) { |
| 736 | if (req->cmd_flags & REQ_SECURE) |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 737 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 738 | else |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 739 | ret = mmc_blk_issue_discard_rq(mq, req); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 740 | } else if (req->cmd_flags & REQ_FLUSH) { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 741 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 742 | } else { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 743 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 744 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 745 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 746 | out: |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 747 | mmc_release_host(card->host); |
| 748 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 751 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 752 | { |
| 753 | return mmc_card_readonly(card) || |
| 754 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 755 | } |
| 756 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 757 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 758 | struct device *parent, |
| 759 | sector_t size, |
| 760 | bool default_ro, |
| 761 | const char *subname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | { |
| 763 | struct mmc_blk_data *md; |
| 764 | int devidx, ret; |
| 765 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 766 | devidx = find_first_zero_bit(dev_use, max_devices); |
| 767 | if (devidx >= max_devices) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | return ERR_PTR(-ENOSPC); |
| 769 | __set_bit(devidx, dev_use); |
| 770 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 771 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 772 | if (!md) { |
| 773 | ret = -ENOMEM; |
| 774 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 776 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 777 | /* |
| 778 | * Set the read-only status based on the supported commands |
| 779 | * and the write protect switch. |
| 780 | */ |
| 781 | md->read_only = mmc_blk_readonly(card); |
| 782 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 783 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 784 | if (md->disk == NULL) { |
| 785 | ret = -ENOMEM; |
| 786 | goto err_kfree; |
| 787 | } |
| 788 | |
| 789 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 790 | INIT_LIST_HEAD(&md->part); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 791 | md->usage = 1; |
| 792 | |
| 793 | ret = mmc_init_queue(&md->queue, card, &md->lock); |
| 794 | if (ret) |
| 795 | goto err_putdisk; |
| 796 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 797 | md->queue.issue_fn = mmc_blk_issue_rq; |
| 798 | md->queue.data = md; |
| 799 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 800 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 801 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 802 | md->disk->fops = &mmc_bdops; |
| 803 | md->disk->private_data = md; |
| 804 | md->disk->queue = md->queue.queue; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 805 | md->disk->driverfs_dev = parent; |
| 806 | set_disk_ro(md->disk, md->read_only || default_ro); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 807 | if (REL_WRITES_SUPPORTED(card)) |
| 808 | blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 809 | |
| 810 | /* |
| 811 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 812 | * |
| 813 | * - be set for removable media with permanent block devices |
| 814 | * - be unset for removable block devices with permanent media |
| 815 | * |
| 816 | * Since MMC block devices clearly fall under the second |
| 817 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 818 | * should use the block device creation/destruction hotplug |
| 819 | * messages to tell when the card is present. |
| 820 | */ |
| 821 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 822 | if (subname) |
| 823 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 824 | "mmcblk%d%s", |
| 825 | mmc_get_devidx(dev_to_disk(parent)), subname); |
| 826 | else |
| 827 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 828 | "mmcblk%d", devidx); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 829 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 830 | blk_queue_logical_block_size(md->queue.queue, 512); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 831 | set_capacity(md->disk, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 832 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 833 | |
| 834 | err_putdisk: |
| 835 | put_disk(md->disk); |
| 836 | err_kfree: |
| 837 | kfree(md); |
| 838 | out: |
| 839 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 842 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 843 | { |
| 844 | sector_t size; |
| 845 | struct mmc_blk_data *md; |
| 846 | |
| 847 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 848 | /* |
| 849 | * The EXT_CSD sector count is in number or 512 byte |
| 850 | * sectors. |
| 851 | */ |
| 852 | size = card->ext_csd.sectors; |
| 853 | } else { |
| 854 | /* |
| 855 | * The CSD capacity field is in units of read_blkbits. |
| 856 | * set_capacity takes units of 512 bytes. |
| 857 | */ |
| 858 | size = card->csd.capacity << (card->csd.read_blkbits - 9); |
| 859 | } |
| 860 | |
| 861 | md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL); |
| 862 | return md; |
| 863 | } |
| 864 | |
| 865 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 866 | struct mmc_blk_data *md, |
| 867 | unsigned int part_type, |
| 868 | sector_t size, |
| 869 | bool default_ro, |
| 870 | const char *subname) |
| 871 | { |
| 872 | char cap_str[10]; |
| 873 | struct mmc_blk_data *part_md; |
| 874 | |
| 875 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, |
| 876 | subname); |
| 877 | if (IS_ERR(part_md)) |
| 878 | return PTR_ERR(part_md); |
| 879 | part_md->part_type = part_type; |
| 880 | list_add(&part_md->part, &md->part); |
| 881 | |
| 882 | string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2, |
| 883 | cap_str, sizeof(cap_str)); |
| 884 | printk(KERN_INFO "%s: %s %s partition %u %s\n", |
| 885 | part_md->disk->disk_name, mmc_card_id(card), |
| 886 | mmc_card_name(card), part_md->part_type, cap_str); |
| 887 | return 0; |
| 888 | } |
| 889 | |
| 890 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 891 | { |
| 892 | int ret = 0; |
| 893 | |
| 894 | if (!mmc_card_mmc(card)) |
| 895 | return 0; |
| 896 | |
| 897 | if (card->ext_csd.boot_size) { |
| 898 | ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0, |
| 899 | card->ext_csd.boot_size >> 9, |
| 900 | true, |
| 901 | "boot0"); |
| 902 | if (ret) |
| 903 | return ret; |
| 904 | ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1, |
| 905 | card->ext_csd.boot_size >> 9, |
| 906 | true, |
| 907 | "boot1"); |
| 908 | if (ret) |
| 909 | return ret; |
| 910 | } |
| 911 | |
| 912 | return ret; |
| 913 | } |
| 914 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | static int |
| 916 | mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card) |
| 917 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | int err; |
| 919 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 920 | mmc_claim_host(card->host); |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 921 | err = mmc_set_blocklen(card, 512); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 922 | mmc_release_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | if (err) { |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 925 | printk(KERN_ERR "%s: unable to set block size to 512: %d\n", |
| 926 | md->disk->disk_name, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | return -EINVAL; |
| 928 | } |
| 929 | |
| 930 | return 0; |
| 931 | } |
| 932 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 933 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 934 | { |
| 935 | if (md) { |
| 936 | if (md->disk->flags & GENHD_FL_UP) { |
| 937 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 938 | |
| 939 | /* Stop new requests from getting into the queue */ |
| 940 | del_gendisk(md->disk); |
| 941 | } |
| 942 | |
| 943 | /* Then flush out any already in there */ |
| 944 | mmc_cleanup_queue(&md->queue); |
| 945 | mmc_blk_put(md); |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 950 | struct mmc_blk_data *md) |
| 951 | { |
| 952 | struct list_head *pos, *q; |
| 953 | struct mmc_blk_data *part_md; |
| 954 | |
| 955 | list_for_each_safe(pos, q, &md->part) { |
| 956 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 957 | list_del(pos); |
| 958 | mmc_blk_remove_req(part_md); |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 963 | { |
| 964 | int ret; |
| 965 | |
| 966 | add_disk(md->disk); |
| 967 | md->force_ro.show = force_ro_show; |
| 968 | md->force_ro.store = force_ro_store; |
| 969 | md->force_ro.attr.name = "force_ro"; |
| 970 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 971 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 972 | if (ret) |
| 973 | del_gendisk(md->disk); |
| 974 | |
| 975 | return ret; |
| 976 | } |
| 977 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 978 | static const struct mmc_fixup blk_fixups[] = |
| 979 | { |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame^] | 980 | MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 981 | MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 982 | MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 983 | MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 984 | MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 985 | END_FIXUP |
| 986 | }; |
| 987 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | static int mmc_blk_probe(struct mmc_card *card) |
| 989 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 990 | struct mmc_blk_data *md, *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | int err; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 992 | char cap_str[10]; |
| 993 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 994 | /* |
| 995 | * Check that the card supports the command class(es) we need. |
| 996 | */ |
| 997 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | return -ENODEV; |
| 999 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1000 | md = mmc_blk_alloc(card); |
| 1001 | if (IS_ERR(md)) |
| 1002 | return PTR_ERR(md); |
| 1003 | |
| 1004 | err = mmc_blk_set_blksize(md, card); |
| 1005 | if (err) |
| 1006 | goto out; |
| 1007 | |
Yi Li | 444122f | 2009-02-05 15:31:57 +0800 | [diff] [blame] | 1008 | string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1009 | cap_str, sizeof(cap_str)); |
| 1010 | printk(KERN_INFO "%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1012 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1014 | if (mmc_blk_alloc_parts(card, md)) |
| 1015 | goto out; |
| 1016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | mmc_set_drvdata(card, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 1018 | mmc_fixup_device(card, blk_fixups); |
| 1019 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1020 | if (mmc_add_disk(md)) |
| 1021 | goto out; |
| 1022 | |
| 1023 | list_for_each_entry(part_md, &md->part, part) { |
| 1024 | if (mmc_add_disk(part_md)) |
| 1025 | goto out; |
| 1026 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1027 | return 0; |
| 1028 | |
| 1029 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1030 | mmc_blk_remove_parts(card, md); |
| 1031 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1032 | return err; |
| 1033 | } |
| 1034 | |
| 1035 | static void mmc_blk_remove(struct mmc_card *card) |
| 1036 | { |
| 1037 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1038 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1039 | mmc_blk_remove_parts(card, md); |
| 1040 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1041 | mmc_set_drvdata(card, NULL); |
| 1042 | } |
| 1043 | |
| 1044 | #ifdef CONFIG_PM |
| 1045 | static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state) |
| 1046 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1047 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1049 | |
| 1050 | if (md) { |
| 1051 | mmc_queue_suspend(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1052 | list_for_each_entry(part_md, &md->part, part) { |
| 1053 | mmc_queue_suspend(&part_md->queue); |
| 1054 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | } |
| 1056 | return 0; |
| 1057 | } |
| 1058 | |
| 1059 | static int mmc_blk_resume(struct mmc_card *card) |
| 1060 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1061 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1063 | |
| 1064 | if (md) { |
| 1065 | mmc_blk_set_blksize(md, card); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1066 | |
| 1067 | /* |
| 1068 | * Resume involves the card going into idle state, |
| 1069 | * so current partition is always the main one. |
| 1070 | */ |
| 1071 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1073 | list_for_each_entry(part_md, &md->part, part) { |
| 1074 | mmc_queue_resume(&part_md->queue); |
| 1075 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1076 | } |
| 1077 | return 0; |
| 1078 | } |
| 1079 | #else |
| 1080 | #define mmc_blk_suspend NULL |
| 1081 | #define mmc_blk_resume NULL |
| 1082 | #endif |
| 1083 | |
| 1084 | static struct mmc_driver mmc_driver = { |
| 1085 | .drv = { |
| 1086 | .name = "mmcblk", |
| 1087 | }, |
| 1088 | .probe = mmc_blk_probe, |
| 1089 | .remove = mmc_blk_remove, |
| 1090 | .suspend = mmc_blk_suspend, |
| 1091 | .resume = mmc_blk_resume, |
| 1092 | }; |
| 1093 | |
| 1094 | static int __init mmc_blk_init(void) |
| 1095 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1096 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1098 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 1099 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 1100 | |
| 1101 | max_devices = 256 / perdev_minors; |
| 1102 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1103 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 1104 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1107 | res = mmc_register_driver(&mmc_driver); |
| 1108 | if (res) |
| 1109 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1111 | return 0; |
| 1112 | out2: |
| 1113 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | out: |
| 1115 | return res; |
| 1116 | } |
| 1117 | |
| 1118 | static void __exit mmc_blk_exit(void) |
| 1119 | { |
| 1120 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1121 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | module_init(mmc_blk_init); |
| 1125 | module_exit(mmc_blk_exit); |
| 1126 | |
| 1127 | MODULE_LICENSE("GPL"); |
| 1128 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 1129 | |