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; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 263 | struct mmc_command cmd = {0}; |
Chris Ball | a61ad2b | 2011-04-13 23:46:05 -0400 | [diff] [blame^] | 264 | struct mmc_data data = {0}; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 265 | unsigned int timeout_us; |
| 266 | |
| 267 | struct scatterlist sg; |
| 268 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 269 | cmd.opcode = MMC_APP_CMD; |
| 270 | cmd.arg = card->rca << 16; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 271 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 272 | |
| 273 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 274 | if (err) |
| 275 | return (u32)-1; |
| 276 | 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] | 277 | return (u32)-1; |
| 278 | |
| 279 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 280 | |
| 281 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 282 | cmd.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 283 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 284 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 285 | data.timeout_ns = card->csd.tacc_ns * 100; |
| 286 | data.timeout_clks = card->csd.tacc_clks * 100; |
| 287 | |
| 288 | timeout_us = data.timeout_ns / 1000; |
| 289 | timeout_us += data.timeout_clks * 1000 / |
| 290 | (card->host->ios.clock / 1000); |
| 291 | |
| 292 | if (timeout_us > 100000) { |
| 293 | data.timeout_ns = 100000000; |
| 294 | data.timeout_clks = 0; |
| 295 | } |
| 296 | |
| 297 | data.blksz = 4; |
| 298 | data.blocks = 1; |
| 299 | data.flags = MMC_DATA_READ; |
| 300 | data.sg = &sg; |
| 301 | data.sg_len = 1; |
| 302 | |
| 303 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 304 | |
| 305 | mrq.cmd = &cmd; |
| 306 | mrq.data = &data; |
| 307 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 308 | blocks = kmalloc(4, GFP_KERNEL); |
| 309 | if (!blocks) |
| 310 | return (u32)-1; |
| 311 | |
| 312 | sg_init_one(&sg, blocks, 4); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 313 | |
| 314 | mmc_wait_for_req(card->host, &mrq); |
| 315 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 316 | result = ntohl(*blocks); |
| 317 | kfree(blocks); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 318 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 319 | if (cmd.error || data.error) |
| 320 | result = (u32)-1; |
| 321 | |
| 322 | return result; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 325 | static u32 get_card_status(struct mmc_card *card, struct request *req) |
| 326 | { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 327 | struct mmc_command cmd = {0}; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 328 | int err; |
| 329 | |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 330 | cmd.opcode = MMC_SEND_STATUS; |
| 331 | if (!mmc_host_is_spi(card->host)) |
| 332 | cmd.arg = card->rca << 16; |
| 333 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; |
| 334 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 335 | if (err) |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 336 | printk(KERN_ERR "%s: error %d sending status command", |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 337 | req->rq_disk->disk_name, err); |
| 338 | return cmd.resp[0]; |
| 339 | } |
| 340 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 341 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
| 342 | { |
| 343 | struct mmc_blk_data *md = mq->data; |
| 344 | struct mmc_card *card = md->queue.card; |
| 345 | unsigned int from, nr, arg; |
| 346 | int err = 0; |
| 347 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 348 | if (!mmc_can_erase(card)) { |
| 349 | err = -EOPNOTSUPP; |
| 350 | goto out; |
| 351 | } |
| 352 | |
| 353 | from = blk_rq_pos(req); |
| 354 | nr = blk_rq_sectors(req); |
| 355 | |
| 356 | if (mmc_can_trim(card)) |
| 357 | arg = MMC_TRIM_ARG; |
| 358 | else |
| 359 | arg = MMC_ERASE_ARG; |
| 360 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 361 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 362 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 363 | INAND_CMD38_ARG_EXT_CSD, |
| 364 | arg == MMC_TRIM_ARG ? |
| 365 | INAND_CMD38_ARG_TRIM : |
| 366 | INAND_CMD38_ARG_ERASE, |
| 367 | 0); |
| 368 | if (err) |
| 369 | goto out; |
| 370 | } |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 371 | err = mmc_erase(card, from, nr, arg); |
| 372 | out: |
| 373 | spin_lock_irq(&md->lock); |
| 374 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 375 | spin_unlock_irq(&md->lock); |
| 376 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 377 | return err ? 0 : 1; |
| 378 | } |
| 379 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 380 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
| 381 | struct request *req) |
| 382 | { |
| 383 | struct mmc_blk_data *md = mq->data; |
| 384 | struct mmc_card *card = md->queue.card; |
| 385 | unsigned int from, nr, arg; |
| 386 | int err = 0; |
| 387 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 388 | if (!mmc_can_secure_erase_trim(card)) { |
| 389 | err = -EOPNOTSUPP; |
| 390 | goto out; |
| 391 | } |
| 392 | |
| 393 | from = blk_rq_pos(req); |
| 394 | nr = blk_rq_sectors(req); |
| 395 | |
| 396 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
| 397 | arg = MMC_SECURE_TRIM1_ARG; |
| 398 | else |
| 399 | arg = MMC_SECURE_ERASE_ARG; |
| 400 | |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 401 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 402 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 403 | INAND_CMD38_ARG_EXT_CSD, |
| 404 | arg == MMC_SECURE_TRIM1_ARG ? |
| 405 | INAND_CMD38_ARG_SECTRIM1 : |
| 406 | INAND_CMD38_ARG_SECERASE, |
| 407 | 0); |
| 408 | if (err) |
| 409 | goto out; |
| 410 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 411 | err = mmc_erase(card, from, nr, arg); |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 412 | if (!err && arg == MMC_SECURE_TRIM1_ARG) { |
| 413 | if (card->quirks & MMC_QUIRK_INAND_CMD38) { |
| 414 | err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, |
| 415 | INAND_CMD38_ARG_EXT_CSD, |
| 416 | INAND_CMD38_ARG_SECTRIM2, |
| 417 | 0); |
| 418 | if (err) |
| 419 | goto out; |
| 420 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 421 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 422 | } |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 423 | out: |
| 424 | spin_lock_irq(&md->lock); |
| 425 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 426 | spin_unlock_irq(&md->lock); |
| 427 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 428 | return err ? 0 : 1; |
| 429 | } |
| 430 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 431 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
| 432 | { |
| 433 | struct mmc_blk_data *md = mq->data; |
| 434 | |
| 435 | /* |
| 436 | * No-op, only service this because we need REQ_FUA for reliable |
| 437 | * writes. |
| 438 | */ |
| 439 | spin_lock_irq(&md->lock); |
| 440 | __blk_end_request_all(req, 0); |
| 441 | spin_unlock_irq(&md->lock); |
| 442 | |
| 443 | return 1; |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * Reformat current write as a reliable write, supporting |
| 448 | * both legacy and the enhanced reliable write MMC cards. |
| 449 | * In each transfer we'll handle only as much as a single |
| 450 | * reliable write can handle, thus finish the request in |
| 451 | * partial completions. |
| 452 | */ |
| 453 | static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 454 | struct mmc_card *card, |
| 455 | struct request *req) |
| 456 | { |
| 457 | int err; |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 458 | struct mmc_command set_count = {0}; |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 459 | |
| 460 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 461 | /* Legacy mode imposes restrictions on transfers. */ |
| 462 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) |
| 463 | brq->data.blocks = 1; |
| 464 | |
| 465 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 466 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 467 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 468 | brq->data.blocks = 1; |
| 469 | } |
| 470 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 471 | set_count.opcode = MMC_SET_BLOCK_COUNT; |
| 472 | set_count.arg = brq->data.blocks | (1 << 31); |
| 473 | set_count.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 474 | err = mmc_wait_for_cmd(card->host, &set_count, 0); |
| 475 | if (err) |
| 476 | printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n", |
| 477 | req->rq_disk->disk_name, err); |
| 478 | return err; |
| 479 | } |
| 480 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 481 | 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] | 482 | { |
| 483 | struct mmc_blk_data *md = mq->data; |
| 484 | struct mmc_card *card = md->queue.card; |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 485 | struct mmc_blk_request brq; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 486 | int ret = 1, disable_multi = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 488 | /* |
| 489 | * Reliable writes are used to implement Forced Unit Access and |
| 490 | * REQ_META accesses, and are supported only on MMCs. |
| 491 | */ |
| 492 | bool do_rel_wr = ((req->cmd_flags & REQ_FUA) || |
| 493 | (req->cmd_flags & REQ_META)) && |
| 494 | (rq_data_dir(req) == WRITE) && |
| 495 | REL_WRITES_SUPPORTED(card); |
| 496 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | do { |
Chris Ball | 1278dba | 2011-04-13 23:40:30 -0400 | [diff] [blame] | 498 | struct mmc_command cmd = {0}; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 499 | u32 readcmd, writecmd, status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | |
| 501 | memset(&brq, 0, sizeof(struct mmc_blk_request)); |
| 502 | brq.mrq.cmd = &brq.cmd; |
| 503 | brq.mrq.data = &brq.data; |
| 504 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 505 | brq.cmd.arg = blk_rq_pos(req); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 506 | if (!mmc_card_blockaddr(card)) |
| 507 | brq.cmd.arg <<= 9; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 508 | 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] | 509 | brq.data.blksz = 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | brq.stop.opcode = MMC_STOP_TRANSMISSION; |
| 511 | brq.stop.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 512 | 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] | 513 | brq.data.blocks = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 515 | /* |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 516 | * The block layer doesn't support all sector count |
| 517 | * restrictions, so we need to be prepared for too big |
| 518 | * requests. |
| 519 | */ |
| 520 | if (brq.data.blocks > card->host->max_blk_count) |
| 521 | brq.data.blocks = card->host->max_blk_count; |
| 522 | |
| 523 | /* |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 524 | * After a read error, we redo the request one sector at a time |
| 525 | * in order to accurately determine which sectors can be read |
| 526 | * successfully. |
| 527 | */ |
| 528 | if (disable_multi && brq.data.blocks > 1) |
| 529 | brq.data.blocks = 1; |
| 530 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 531 | if (brq.data.blocks > 1 || do_rel_wr) { |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 532 | /* SPI multiblock writes terminate using a special |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 533 | * token, not a STOP_TRANSMISSION request. Reliable |
| 534 | * writes use SET_BLOCK_COUNT and do not use a |
| 535 | * STOP_TRANSMISSION request either. |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 536 | */ |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 537 | if ((!mmc_host_is_spi(card->host) && !do_rel_wr) || |
| 538 | rq_data_dir(req) == READ) |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 539 | brq.mrq.stop = &brq.stop; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 540 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 541 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 542 | } else { |
| 543 | brq.mrq.stop = NULL; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 544 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 545 | writecmd = MMC_WRITE_BLOCK; |
| 546 | } |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 547 | if (rq_data_dir(req) == READ) { |
| 548 | brq.cmd.opcode = readcmd; |
| 549 | brq.data.flags |= MMC_DATA_READ; |
| 550 | } else { |
| 551 | brq.cmd.opcode = writecmd; |
| 552 | brq.data.flags |= MMC_DATA_WRITE; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 553 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 555 | if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req)) |
| 556 | goto cmd_err; |
| 557 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 558 | mmc_set_data_timeout(&brq.data, card); |
| 559 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | brq.data.sg = mq->sg; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 561 | brq.data.sg_len = mmc_queue_map_sg(mq); |
| 562 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 563 | /* |
| 564 | * Adjust the sg list so it is the same size as the |
| 565 | * request. |
| 566 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 567 | if (brq.data.blocks != blk_rq_sectors(req)) { |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 568 | int i, data_size = brq.data.blocks << 9; |
| 569 | struct scatterlist *sg; |
| 570 | |
| 571 | for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) { |
| 572 | data_size -= sg->length; |
| 573 | if (data_size <= 0) { |
| 574 | sg->length += data_size; |
| 575 | i++; |
| 576 | break; |
| 577 | } |
| 578 | } |
| 579 | brq.data.sg_len = i; |
| 580 | } |
| 581 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 582 | mmc_queue_bounce_pre(mq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | |
| 584 | mmc_wait_for_req(card->host, &brq.mrq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 585 | |
| 586 | mmc_queue_bounce_post(mq); |
| 587 | |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 588 | /* |
| 589 | * Check for errors here, but don't jump to cmd_err |
| 590 | * until later as we need to wait for the card to leave |
| 591 | * programming mode even when things go wrong. |
| 592 | */ |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 593 | if (brq.cmd.error || brq.data.error || brq.stop.error) { |
| 594 | if (brq.data.blocks > 1 && rq_data_dir(req) == READ) { |
| 595 | /* Redo read one sector at a time */ |
| 596 | printk(KERN_WARNING "%s: retrying using single " |
| 597 | "block read\n", req->rq_disk->disk_name); |
| 598 | disable_multi = 1; |
| 599 | continue; |
| 600 | } |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 601 | status = get_card_status(card, req); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 602 | } |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 603 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | if (brq.cmd.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 605 | printk(KERN_ERR "%s: error %d sending read/write " |
| 606 | "command, response %#x, card status %#x\n", |
| 607 | req->rq_disk->disk_name, brq.cmd.error, |
| 608 | brq.cmd.resp[0], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | } |
| 610 | |
| 611 | if (brq.data.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 612 | if (brq.data.error == -ETIMEDOUT && brq.mrq.stop) |
| 613 | /* 'Stop' response contains card status */ |
| 614 | status = brq.mrq.stop->resp[0]; |
| 615 | printk(KERN_ERR "%s: error %d transferring data," |
| 616 | " sector %u, nr %u, card status %#x\n", |
| 617 | req->rq_disk->disk_name, brq.data.error, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 618 | (unsigned)blk_rq_pos(req), |
| 619 | (unsigned)blk_rq_sectors(req), status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | } |
| 621 | |
| 622 | if (brq.stop.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 623 | printk(KERN_ERR "%s: error %d sending stop command, " |
| 624 | "response %#x, card status %#x\n", |
| 625 | req->rq_disk->disk_name, brq.stop.error, |
| 626 | brq.stop.resp[0], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | } |
| 628 | |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 629 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 630 | do { |
| 631 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 633 | cmd.opcode = MMC_SEND_STATUS; |
| 634 | cmd.arg = card->rca << 16; |
| 635 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 636 | err = mmc_wait_for_cmd(card->host, &cmd, 5); |
| 637 | if (err) { |
| 638 | printk(KERN_ERR "%s: error %d requesting status\n", |
| 639 | req->rq_disk->disk_name, err); |
| 640 | goto cmd_err; |
| 641 | } |
Pierre Ossman | d198f10 | 2007-11-02 18:21:13 +0100 | [diff] [blame] | 642 | /* |
| 643 | * Some cards mishandle the status bits, |
| 644 | * so make sure to check both the busy |
| 645 | * indication and the card state. |
| 646 | */ |
| 647 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
| 648 | (R1_CURRENT_STATE(cmd.resp[0]) == 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | |
| 650 | #if 0 |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 651 | if (cmd.resp[0] & ~0x00000900) |
| 652 | printk(KERN_ERR "%s: status = %08x\n", |
| 653 | req->rq_disk->disk_name, cmd.resp[0]); |
| 654 | if (mmc_decode_status(cmd.resp)) |
| 655 | goto cmd_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | #endif |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 657 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 658 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 659 | if (brq.cmd.error || brq.stop.error || brq.data.error) { |
| 660 | if (rq_data_dir(req) == READ) { |
| 661 | /* |
| 662 | * After an error, we redo I/O one sector at a |
| 663 | * time, so we only reach here after trying to |
| 664 | * read a single sector. |
| 665 | */ |
| 666 | spin_lock_irq(&md->lock); |
| 667 | ret = __blk_end_request(req, -EIO, brq.data.blksz); |
| 668 | spin_unlock_irq(&md->lock); |
| 669 | continue; |
| 670 | } |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 671 | goto cmd_err; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 672 | } |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | /* |
| 675 | * A block was successfully transferred. |
| 676 | */ |
| 677 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 678 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | spin_unlock_irq(&md->lock); |
| 680 | } while (ret); |
| 681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | return 1; |
| 683 | |
| 684 | cmd_err: |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 685 | /* |
| 686 | * If this is an SD card and we're writing, we can first |
| 687 | * mark the known good sectors as ok. |
| 688 | * |
| 689 | * If the card is not SD, we can still ok written sectors |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 690 | * as reported by the controller (which might be less than |
| 691 | * the real number of written sectors, but never more). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | */ |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 693 | if (mmc_card_sd(card)) { |
| 694 | u32 blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 695 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 696 | blocks = mmc_sd_num_wr_blocks(card); |
| 697 | if (blocks != (u32)-1) { |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 698 | spin_lock_irq(&md->lock); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 699 | ret = __blk_end_request(req, 0, blocks << 9); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 700 | spin_unlock_irq(&md->lock); |
| 701 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 702 | } else { |
| 703 | spin_lock_irq(&md->lock); |
| 704 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
| 705 | spin_unlock_irq(&md->lock); |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 706 | } |
| 707 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 709 | while (ret) |
| 710 | ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | spin_unlock_irq(&md->lock); |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 716 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
| 717 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 718 | int ret; |
| 719 | struct mmc_blk_data *md = mq->data; |
| 720 | struct mmc_card *card = md->queue.card; |
| 721 | |
| 722 | mmc_claim_host(card->host); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 723 | ret = mmc_blk_part_switch(card, md); |
| 724 | if (ret) { |
| 725 | ret = 0; |
| 726 | goto out; |
| 727 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 728 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 729 | if (req->cmd_flags & REQ_DISCARD) { |
| 730 | if (req->cmd_flags & REQ_SECURE) |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 731 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 732 | else |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 733 | ret = mmc_blk_issue_discard_rq(mq, req); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 734 | } else if (req->cmd_flags & REQ_FLUSH) { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 735 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 736 | } else { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 737 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 738 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 739 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 740 | out: |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame] | 741 | mmc_release_host(card->host); |
| 742 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 745 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 746 | { |
| 747 | return mmc_card_readonly(card) || |
| 748 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 749 | } |
| 750 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 751 | static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card, |
| 752 | struct device *parent, |
| 753 | sector_t size, |
| 754 | bool default_ro, |
| 755 | const char *subname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | { |
| 757 | struct mmc_blk_data *md; |
| 758 | int devidx, ret; |
| 759 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 760 | devidx = find_first_zero_bit(dev_use, max_devices); |
| 761 | if (devidx >= max_devices) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | return ERR_PTR(-ENOSPC); |
| 763 | __set_bit(devidx, dev_use); |
| 764 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 765 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 766 | if (!md) { |
| 767 | ret = -ENOMEM; |
| 768 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 770 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 771 | /* |
| 772 | * Set the read-only status based on the supported commands |
| 773 | * and the write protect switch. |
| 774 | */ |
| 775 | md->read_only = mmc_blk_readonly(card); |
| 776 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 777 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 778 | if (md->disk == NULL) { |
| 779 | ret = -ENOMEM; |
| 780 | goto err_kfree; |
| 781 | } |
| 782 | |
| 783 | spin_lock_init(&md->lock); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 784 | INIT_LIST_HEAD(&md->part); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 785 | md->usage = 1; |
| 786 | |
| 787 | ret = mmc_init_queue(&md->queue, card, &md->lock); |
| 788 | if (ret) |
| 789 | goto err_putdisk; |
| 790 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 791 | md->queue.issue_fn = mmc_blk_issue_rq; |
| 792 | md->queue.data = md; |
| 793 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 794 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 795 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 796 | md->disk->fops = &mmc_bdops; |
| 797 | md->disk->private_data = md; |
| 798 | md->disk->queue = md->queue.queue; |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 799 | md->disk->driverfs_dev = parent; |
| 800 | set_disk_ro(md->disk, md->read_only || default_ro); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 801 | if (REL_WRITES_SUPPORTED(card)) |
| 802 | blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 806 | * |
| 807 | * - be set for removable media with permanent block devices |
| 808 | * - be unset for removable block devices with permanent media |
| 809 | * |
| 810 | * Since MMC block devices clearly fall under the second |
| 811 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 812 | * should use the block device creation/destruction hotplug |
| 813 | * messages to tell when the card is present. |
| 814 | */ |
| 815 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 816 | if (subname) |
| 817 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 818 | "mmcblk%d%s", |
| 819 | mmc_get_devidx(dev_to_disk(parent)), subname); |
| 820 | else |
| 821 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 822 | "mmcblk%d", devidx); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 823 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 824 | blk_queue_logical_block_size(md->queue.queue, 512); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 825 | set_capacity(md->disk, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 827 | |
| 828 | err_putdisk: |
| 829 | put_disk(md->disk); |
| 830 | err_kfree: |
| 831 | kfree(md); |
| 832 | out: |
| 833 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | } |
| 835 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 836 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 837 | { |
| 838 | sector_t size; |
| 839 | struct mmc_blk_data *md; |
| 840 | |
| 841 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 842 | /* |
| 843 | * The EXT_CSD sector count is in number or 512 byte |
| 844 | * sectors. |
| 845 | */ |
| 846 | size = card->ext_csd.sectors; |
| 847 | } else { |
| 848 | /* |
| 849 | * The CSD capacity field is in units of read_blkbits. |
| 850 | * set_capacity takes units of 512 bytes. |
| 851 | */ |
| 852 | size = card->csd.capacity << (card->csd.read_blkbits - 9); |
| 853 | } |
| 854 | |
| 855 | md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL); |
| 856 | return md; |
| 857 | } |
| 858 | |
| 859 | static int mmc_blk_alloc_part(struct mmc_card *card, |
| 860 | struct mmc_blk_data *md, |
| 861 | unsigned int part_type, |
| 862 | sector_t size, |
| 863 | bool default_ro, |
| 864 | const char *subname) |
| 865 | { |
| 866 | char cap_str[10]; |
| 867 | struct mmc_blk_data *part_md; |
| 868 | |
| 869 | part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro, |
| 870 | subname); |
| 871 | if (IS_ERR(part_md)) |
| 872 | return PTR_ERR(part_md); |
| 873 | part_md->part_type = part_type; |
| 874 | list_add(&part_md->part, &md->part); |
| 875 | |
| 876 | string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2, |
| 877 | cap_str, sizeof(cap_str)); |
| 878 | printk(KERN_INFO "%s: %s %s partition %u %s\n", |
| 879 | part_md->disk->disk_name, mmc_card_id(card), |
| 880 | mmc_card_name(card), part_md->part_type, cap_str); |
| 881 | return 0; |
| 882 | } |
| 883 | |
| 884 | static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md) |
| 885 | { |
| 886 | int ret = 0; |
| 887 | |
| 888 | if (!mmc_card_mmc(card)) |
| 889 | return 0; |
| 890 | |
| 891 | if (card->ext_csd.boot_size) { |
| 892 | ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0, |
| 893 | card->ext_csd.boot_size >> 9, |
| 894 | true, |
| 895 | "boot0"); |
| 896 | if (ret) |
| 897 | return ret; |
| 898 | ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1, |
| 899 | card->ext_csd.boot_size >> 9, |
| 900 | true, |
| 901 | "boot1"); |
| 902 | if (ret) |
| 903 | return ret; |
| 904 | } |
| 905 | |
| 906 | return ret; |
| 907 | } |
| 908 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | static int |
| 910 | mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card) |
| 911 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | int err; |
| 913 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 914 | mmc_claim_host(card->host); |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 915 | err = mmc_set_blocklen(card, 512); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 916 | mmc_release_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | |
| 918 | if (err) { |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 919 | printk(KERN_ERR "%s: unable to set block size to 512: %d\n", |
| 920 | md->disk->disk_name, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | return -EINVAL; |
| 922 | } |
| 923 | |
| 924 | return 0; |
| 925 | } |
| 926 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 927 | static void mmc_blk_remove_req(struct mmc_blk_data *md) |
| 928 | { |
| 929 | if (md) { |
| 930 | if (md->disk->flags & GENHD_FL_UP) { |
| 931 | device_remove_file(disk_to_dev(md->disk), &md->force_ro); |
| 932 | |
| 933 | /* Stop new requests from getting into the queue */ |
| 934 | del_gendisk(md->disk); |
| 935 | } |
| 936 | |
| 937 | /* Then flush out any already in there */ |
| 938 | mmc_cleanup_queue(&md->queue); |
| 939 | mmc_blk_put(md); |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | static void mmc_blk_remove_parts(struct mmc_card *card, |
| 944 | struct mmc_blk_data *md) |
| 945 | { |
| 946 | struct list_head *pos, *q; |
| 947 | struct mmc_blk_data *part_md; |
| 948 | |
| 949 | list_for_each_safe(pos, q, &md->part) { |
| 950 | part_md = list_entry(pos, struct mmc_blk_data, part); |
| 951 | list_del(pos); |
| 952 | mmc_blk_remove_req(part_md); |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | static int mmc_add_disk(struct mmc_blk_data *md) |
| 957 | { |
| 958 | int ret; |
| 959 | |
| 960 | add_disk(md->disk); |
| 961 | md->force_ro.show = force_ro_show; |
| 962 | md->force_ro.store = force_ro_store; |
| 963 | md->force_ro.attr.name = "force_ro"; |
| 964 | md->force_ro.attr.mode = S_IRUGO | S_IWUSR; |
| 965 | ret = device_create_file(disk_to_dev(md->disk), &md->force_ro); |
| 966 | if (ret) |
| 967 | del_gendisk(md->disk); |
| 968 | |
| 969 | return ret; |
| 970 | } |
| 971 | |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 972 | static const struct mmc_fixup blk_fixups[] = |
| 973 | { |
Andrei Warkentin | 6a7a6b4 | 2011-04-12 15:06:53 -0500 | [diff] [blame] | 974 | MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 975 | MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 976 | MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 977 | MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
| 978 | MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38), |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 979 | END_FIXUP |
| 980 | }; |
| 981 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 982 | static int mmc_blk_probe(struct mmc_card *card) |
| 983 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 984 | struct mmc_blk_data *md, *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | int err; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 986 | char cap_str[10]; |
| 987 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 988 | /* |
| 989 | * Check that the card supports the command class(es) we need. |
| 990 | */ |
| 991 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 992 | return -ENODEV; |
| 993 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | md = mmc_blk_alloc(card); |
| 995 | if (IS_ERR(md)) |
| 996 | return PTR_ERR(md); |
| 997 | |
| 998 | err = mmc_blk_set_blksize(md, card); |
| 999 | if (err) |
| 1000 | goto out; |
| 1001 | |
Yi Li | 444122f | 2009-02-05 15:31:57 +0800 | [diff] [blame] | 1002 | string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1003 | cap_str, sizeof(cap_str)); |
| 1004 | printk(KERN_INFO "%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 1006 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1008 | if (mmc_blk_alloc_parts(card, md)) |
| 1009 | goto out; |
| 1010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | mmc_set_drvdata(card, md); |
Andrei Warkentin | 6f60c22 | 2011-04-11 19:11:04 -0400 | [diff] [blame] | 1012 | mmc_fixup_device(card, blk_fixups); |
| 1013 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1014 | if (mmc_add_disk(md)) |
| 1015 | goto out; |
| 1016 | |
| 1017 | list_for_each_entry(part_md, &md->part, part) { |
| 1018 | if (mmc_add_disk(part_md)) |
| 1019 | goto out; |
| 1020 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | return 0; |
| 1022 | |
| 1023 | out: |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1024 | mmc_blk_remove_parts(card, md); |
| 1025 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | return err; |
| 1027 | } |
| 1028 | |
| 1029 | static void mmc_blk_remove(struct mmc_card *card) |
| 1030 | { |
| 1031 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1032 | |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1033 | mmc_blk_remove_parts(card, md); |
| 1034 | mmc_blk_remove_req(md); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1035 | mmc_set_drvdata(card, NULL); |
| 1036 | } |
| 1037 | |
| 1038 | #ifdef CONFIG_PM |
| 1039 | static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state) |
| 1040 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1041 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1043 | |
| 1044 | if (md) { |
| 1045 | mmc_queue_suspend(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1046 | list_for_each_entry(part_md, &md->part, part) { |
| 1047 | mmc_queue_suspend(&part_md->queue); |
| 1048 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | } |
| 1050 | return 0; |
| 1051 | } |
| 1052 | |
| 1053 | static int mmc_blk_resume(struct mmc_card *card) |
| 1054 | { |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1055 | struct mmc_blk_data *part_md; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 1057 | |
| 1058 | if (md) { |
| 1059 | mmc_blk_set_blksize(md, card); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1060 | |
| 1061 | /* |
| 1062 | * Resume involves the card going into idle state, |
| 1063 | * so current partition is always the main one. |
| 1064 | */ |
| 1065 | md->part_curr = md->part_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | mmc_queue_resume(&md->queue); |
Andrei Warkentin | 371a689 | 2011-04-11 18:10:25 -0500 | [diff] [blame] | 1067 | list_for_each_entry(part_md, &md->part, part) { |
| 1068 | mmc_queue_resume(&part_md->queue); |
| 1069 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1070 | } |
| 1071 | return 0; |
| 1072 | } |
| 1073 | #else |
| 1074 | #define mmc_blk_suspend NULL |
| 1075 | #define mmc_blk_resume NULL |
| 1076 | #endif |
| 1077 | |
| 1078 | static struct mmc_driver mmc_driver = { |
| 1079 | .drv = { |
| 1080 | .name = "mmcblk", |
| 1081 | }, |
| 1082 | .probe = mmc_blk_probe, |
| 1083 | .remove = mmc_blk_remove, |
| 1084 | .suspend = mmc_blk_suspend, |
| 1085 | .resume = mmc_blk_resume, |
| 1086 | }; |
| 1087 | |
| 1088 | static int __init mmc_blk_init(void) |
| 1089 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1090 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 1092 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 1093 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 1094 | |
| 1095 | max_devices = 256 / perdev_minors; |
| 1096 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1097 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 1098 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1101 | res = mmc_register_driver(&mmc_driver); |
| 1102 | if (res) |
| 1103 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 1105 | return 0; |
| 1106 | out2: |
| 1107 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | out: |
| 1109 | return res; |
| 1110 | } |
| 1111 | |
| 1112 | static void __exit mmc_blk_exit(void) |
| 1113 | { |
| 1114 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 1115 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | module_init(mmc_blk_init); |
| 1119 | module_exit(mmc_blk_exit); |
| 1120 | |
| 1121 | MODULE_LICENSE("GPL"); |
| 1122 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 1123 | |