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 | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 51 | #define REL_WRITES_SUPPORTED(card) (mmc_card_mmc((card)) && \ |
| 52 | (((card)->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) || \ |
| 53 | ((card)->ext_csd.rel_sectors))) |
| 54 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 55 | static DEFINE_MUTEX(block_mutex); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 56 | |
| 57 | /* |
| 58 | * The defaults come from config options but can be overriden by module |
| 59 | * or bootarg options. |
| 60 | */ |
| 61 | static int perdev_minors = CONFIG_MMC_BLOCK_MINORS; |
| 62 | |
| 63 | /* |
| 64 | * We've only got one major, so number of mmcblk devices is |
| 65 | * limited to 256 / number of minors per device. |
| 66 | */ |
| 67 | static int max_devices; |
| 68 | |
| 69 | /* 256 minors, so at most 256 separate devices */ |
| 70 | static DECLARE_BITMAP(dev_use, 256); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* |
| 73 | * There is one mmc_blk_data per slot. |
| 74 | */ |
| 75 | struct mmc_blk_data { |
| 76 | spinlock_t lock; |
| 77 | struct gendisk *disk; |
| 78 | struct mmc_queue queue; |
| 79 | |
| 80 | unsigned int usage; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 81 | unsigned int read_only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | }; |
| 83 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 84 | static DEFINE_MUTEX(open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 86 | module_param(perdev_minors, int, 0444); |
| 87 | MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device"); |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
| 90 | { |
| 91 | struct mmc_blk_data *md; |
| 92 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 93 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | md = disk->private_data; |
| 95 | if (md && md->usage == 0) |
| 96 | md = NULL; |
| 97 | if (md) |
| 98 | md->usage++; |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 99 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | |
| 101 | return md; |
| 102 | } |
| 103 | |
| 104 | static void mmc_blk_put(struct mmc_blk_data *md) |
| 105 | { |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 106 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | md->usage--; |
| 108 | if (md->usage == 0) { |
Anna Lemehova | 7d92df6 | 2010-01-08 14:42:58 -0800 | [diff] [blame] | 109 | int devmaj = MAJOR(disk_devt(md->disk)); |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 110 | int devidx = MINOR(disk_devt(md->disk)) / perdev_minors; |
Anna Lemehova | 7d92df6 | 2010-01-08 14:42:58 -0800 | [diff] [blame] | 111 | |
| 112 | if (!devmaj) |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 113 | devidx = md->disk->first_minor / perdev_minors; |
Anna Lemehova | 7d92df6 | 2010-01-08 14:42:58 -0800 | [diff] [blame] | 114 | |
Adrian Hunter | 5fa83ce | 2010-01-08 14:43:00 -0800 | [diff] [blame] | 115 | blk_cleanup_queue(md->queue.queue); |
| 116 | |
David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 117 | __clear_bit(devidx, dev_use); |
| 118 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | put_disk(md->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | kfree(md); |
| 121 | } |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 122 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 125 | static int mmc_blk_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 127 | struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | int ret = -ENXIO; |
| 129 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 130 | mutex_lock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (md) { |
| 132 | if (md->usage == 2) |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 133 | check_disk_change(bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | ret = 0; |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 135 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 136 | if ((mode & FMODE_WRITE) && md->read_only) { |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 137 | mmc_blk_put(md); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 138 | ret = -EROFS; |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | } |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 141 | mutex_unlock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | return ret; |
| 144 | } |
| 145 | |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 146 | static int mmc_blk_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 148 | struct mmc_blk_data *md = disk->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 150 | mutex_lock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | mmc_blk_put(md); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 152 | mutex_unlock(&block_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 157 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 159 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
| 160 | geo->heads = 4; |
| 161 | geo->sectors = 16; |
| 162 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | } |
| 164 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 165 | static const struct block_device_operations mmc_bdops = { |
Al Viro | a5a1561 | 2008-03-02 10:33:30 -0500 | [diff] [blame] | 166 | .open = mmc_blk_open, |
| 167 | .release = mmc_blk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 168 | .getgeo = mmc_blk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | .owner = THIS_MODULE, |
| 170 | }; |
| 171 | |
| 172 | struct mmc_blk_request { |
| 173 | struct mmc_request mrq; |
| 174 | struct mmc_command cmd; |
| 175 | struct mmc_command stop; |
| 176 | struct mmc_data data; |
| 177 | }; |
| 178 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 179 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
| 180 | { |
| 181 | int err; |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 182 | u32 result; |
| 183 | __be32 *blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 184 | |
| 185 | struct mmc_request mrq; |
| 186 | struct mmc_command cmd; |
| 187 | struct mmc_data data; |
| 188 | unsigned int timeout_us; |
| 189 | |
| 190 | struct scatterlist sg; |
| 191 | |
| 192 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 193 | |
| 194 | cmd.opcode = MMC_APP_CMD; |
| 195 | cmd.arg = card->rca << 16; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 196 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 197 | |
| 198 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 199 | if (err) |
| 200 | return (u32)-1; |
| 201 | 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] | 202 | return (u32)-1; |
| 203 | |
| 204 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 205 | |
| 206 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 207 | cmd.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 208 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 209 | |
| 210 | memset(&data, 0, sizeof(struct mmc_data)); |
| 211 | |
| 212 | data.timeout_ns = card->csd.tacc_ns * 100; |
| 213 | data.timeout_clks = card->csd.tacc_clks * 100; |
| 214 | |
| 215 | timeout_us = data.timeout_ns / 1000; |
| 216 | timeout_us += data.timeout_clks * 1000 / |
| 217 | (card->host->ios.clock / 1000); |
| 218 | |
| 219 | if (timeout_us > 100000) { |
| 220 | data.timeout_ns = 100000000; |
| 221 | data.timeout_clks = 0; |
| 222 | } |
| 223 | |
| 224 | data.blksz = 4; |
| 225 | data.blocks = 1; |
| 226 | data.flags = MMC_DATA_READ; |
| 227 | data.sg = &sg; |
| 228 | data.sg_len = 1; |
| 229 | |
| 230 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 231 | |
| 232 | mrq.cmd = &cmd; |
| 233 | mrq.data = &data; |
| 234 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 235 | blocks = kmalloc(4, GFP_KERNEL); |
| 236 | if (!blocks) |
| 237 | return (u32)-1; |
| 238 | |
| 239 | sg_init_one(&sg, blocks, 4); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 240 | |
| 241 | mmc_wait_for_req(card->host, &mrq); |
| 242 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 243 | result = ntohl(*blocks); |
| 244 | kfree(blocks); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 245 | |
Ben Dooks | 051913d | 2009-06-08 23:33:57 +0100 | [diff] [blame] | 246 | if (cmd.error || data.error) |
| 247 | result = (u32)-1; |
| 248 | |
| 249 | return result; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 252 | static u32 get_card_status(struct mmc_card *card, struct request *req) |
| 253 | { |
| 254 | struct mmc_command cmd; |
| 255 | int err; |
| 256 | |
| 257 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 258 | cmd.opcode = MMC_SEND_STATUS; |
| 259 | if (!mmc_host_is_spi(card->host)) |
| 260 | cmd.arg = card->rca << 16; |
| 261 | cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC; |
| 262 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
| 263 | if (err) |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 264 | printk(KERN_ERR "%s: error %d sending status command", |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 265 | req->rq_disk->disk_name, err); |
| 266 | return cmd.resp[0]; |
| 267 | } |
| 268 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 269 | static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req) |
| 270 | { |
| 271 | struct mmc_blk_data *md = mq->data; |
| 272 | struct mmc_card *card = md->queue.card; |
| 273 | unsigned int from, nr, arg; |
| 274 | int err = 0; |
| 275 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 276 | if (!mmc_can_erase(card)) { |
| 277 | err = -EOPNOTSUPP; |
| 278 | goto out; |
| 279 | } |
| 280 | |
| 281 | from = blk_rq_pos(req); |
| 282 | nr = blk_rq_sectors(req); |
| 283 | |
| 284 | if (mmc_can_trim(card)) |
| 285 | arg = MMC_TRIM_ARG; |
| 286 | else |
| 287 | arg = MMC_ERASE_ARG; |
| 288 | |
| 289 | err = mmc_erase(card, from, nr, arg); |
| 290 | out: |
| 291 | spin_lock_irq(&md->lock); |
| 292 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 293 | spin_unlock_irq(&md->lock); |
| 294 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 295 | return err ? 0 : 1; |
| 296 | } |
| 297 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 298 | static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq, |
| 299 | struct request *req) |
| 300 | { |
| 301 | struct mmc_blk_data *md = mq->data; |
| 302 | struct mmc_card *card = md->queue.card; |
| 303 | unsigned int from, nr, arg; |
| 304 | int err = 0; |
| 305 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 306 | if (!mmc_can_secure_erase_trim(card)) { |
| 307 | err = -EOPNOTSUPP; |
| 308 | goto out; |
| 309 | } |
| 310 | |
| 311 | from = blk_rq_pos(req); |
| 312 | nr = blk_rq_sectors(req); |
| 313 | |
| 314 | if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr)) |
| 315 | arg = MMC_SECURE_TRIM1_ARG; |
| 316 | else |
| 317 | arg = MMC_SECURE_ERASE_ARG; |
| 318 | |
| 319 | err = mmc_erase(card, from, nr, arg); |
| 320 | if (!err && arg == MMC_SECURE_TRIM1_ARG) |
| 321 | err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG); |
| 322 | out: |
| 323 | spin_lock_irq(&md->lock); |
| 324 | __blk_end_request(req, err, blk_rq_bytes(req)); |
| 325 | spin_unlock_irq(&md->lock); |
| 326 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 327 | return err ? 0 : 1; |
| 328 | } |
| 329 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 330 | static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req) |
| 331 | { |
| 332 | struct mmc_blk_data *md = mq->data; |
| 333 | |
| 334 | /* |
| 335 | * No-op, only service this because we need REQ_FUA for reliable |
| 336 | * writes. |
| 337 | */ |
| 338 | spin_lock_irq(&md->lock); |
| 339 | __blk_end_request_all(req, 0); |
| 340 | spin_unlock_irq(&md->lock); |
| 341 | |
| 342 | return 1; |
| 343 | } |
| 344 | |
| 345 | /* |
| 346 | * Reformat current write as a reliable write, supporting |
| 347 | * both legacy and the enhanced reliable write MMC cards. |
| 348 | * In each transfer we'll handle only as much as a single |
| 349 | * reliable write can handle, thus finish the request in |
| 350 | * partial completions. |
| 351 | */ |
| 352 | static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq, |
| 353 | struct mmc_card *card, |
| 354 | struct request *req) |
| 355 | { |
| 356 | int err; |
| 357 | struct mmc_command set_count; |
| 358 | |
| 359 | if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) { |
| 360 | /* Legacy mode imposes restrictions on transfers. */ |
| 361 | if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors)) |
| 362 | brq->data.blocks = 1; |
| 363 | |
| 364 | if (brq->data.blocks > card->ext_csd.rel_sectors) |
| 365 | brq->data.blocks = card->ext_csd.rel_sectors; |
| 366 | else if (brq->data.blocks < card->ext_csd.rel_sectors) |
| 367 | brq->data.blocks = 1; |
| 368 | } |
| 369 | |
| 370 | memset(&set_count, 0, sizeof(struct mmc_command)); |
| 371 | set_count.opcode = MMC_SET_BLOCK_COUNT; |
| 372 | set_count.arg = brq->data.blocks | (1 << 31); |
| 373 | set_count.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 374 | err = mmc_wait_for_cmd(card->host, &set_count, 0); |
| 375 | if (err) |
| 376 | printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n", |
| 377 | req->rq_disk->disk_name, err); |
| 378 | return err; |
| 379 | } |
| 380 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 381 | 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] | 382 | { |
| 383 | struct mmc_blk_data *md = mq->data; |
| 384 | struct mmc_card *card = md->queue.card; |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 385 | struct mmc_blk_request brq; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 386 | int ret = 1, disable_multi = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 388 | /* |
| 389 | * Reliable writes are used to implement Forced Unit Access and |
| 390 | * REQ_META accesses, and are supported only on MMCs. |
| 391 | */ |
| 392 | bool do_rel_wr = ((req->cmd_flags & REQ_FUA) || |
| 393 | (req->cmd_flags & REQ_META)) && |
| 394 | (rq_data_dir(req) == WRITE) && |
| 395 | REL_WRITES_SUPPORTED(card); |
| 396 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | struct mmc_command cmd; |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 399 | u32 readcmd, writecmd, status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | |
| 401 | memset(&brq, 0, sizeof(struct mmc_blk_request)); |
| 402 | brq.mrq.cmd = &brq.cmd; |
| 403 | brq.mrq.data = &brq.data; |
| 404 | |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 405 | brq.cmd.arg = blk_rq_pos(req); |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 406 | if (!mmc_card_blockaddr(card)) |
| 407 | brq.cmd.arg <<= 9; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 408 | 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] | 409 | brq.data.blksz = 512; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | brq.stop.opcode = MMC_STOP_TRANSMISSION; |
| 411 | brq.stop.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 412 | 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] | 413 | brq.data.blocks = blk_rq_sectors(req); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 415 | /* |
Pierre Ossman | 548d2de | 2009-04-10 17:52:57 +0200 | [diff] [blame] | 416 | * The block layer doesn't support all sector count |
| 417 | * restrictions, so we need to be prepared for too big |
| 418 | * requests. |
| 419 | */ |
| 420 | if (brq.data.blocks > card->host->max_blk_count) |
| 421 | brq.data.blocks = card->host->max_blk_count; |
| 422 | |
| 423 | /* |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 424 | * After a read error, we redo the request one sector at a time |
| 425 | * in order to accurately determine which sectors can be read |
| 426 | * successfully. |
| 427 | */ |
| 428 | if (disable_multi && brq.data.blocks > 1) |
| 429 | brq.data.blocks = 1; |
| 430 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 431 | if (brq.data.blocks > 1 || do_rel_wr) { |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 432 | /* SPI multiblock writes terminate using a special |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 433 | * token, not a STOP_TRANSMISSION request. Reliable |
| 434 | * writes use SET_BLOCK_COUNT and do not use a |
| 435 | * STOP_TRANSMISSION request either. |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 436 | */ |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 437 | if ((!mmc_host_is_spi(card->host) && !do_rel_wr) || |
| 438 | rq_data_dir(req) == READ) |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 439 | brq.mrq.stop = &brq.stop; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 440 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 441 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 442 | } else { |
| 443 | brq.mrq.stop = NULL; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 444 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 445 | writecmd = MMC_WRITE_BLOCK; |
| 446 | } |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 447 | if (rq_data_dir(req) == READ) { |
| 448 | brq.cmd.opcode = readcmd; |
| 449 | brq.data.flags |= MMC_DATA_READ; |
| 450 | } else { |
| 451 | brq.cmd.opcode = writecmd; |
| 452 | brq.data.flags |= MMC_DATA_WRITE; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 453 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 455 | if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req)) |
| 456 | goto cmd_err; |
| 457 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 458 | mmc_set_data_timeout(&brq.data, card); |
| 459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | brq.data.sg = mq->sg; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 461 | brq.data.sg_len = mmc_queue_map_sg(mq); |
| 462 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 463 | /* |
| 464 | * Adjust the sg list so it is the same size as the |
| 465 | * request. |
| 466 | */ |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 467 | if (brq.data.blocks != blk_rq_sectors(req)) { |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 468 | int i, data_size = brq.data.blocks << 9; |
| 469 | struct scatterlist *sg; |
| 470 | |
| 471 | for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) { |
| 472 | data_size -= sg->length; |
| 473 | if (data_size <= 0) { |
| 474 | sg->length += data_size; |
| 475 | i++; |
| 476 | break; |
| 477 | } |
| 478 | } |
| 479 | brq.data.sg_len = i; |
| 480 | } |
| 481 | |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 482 | mmc_queue_bounce_pre(mq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | mmc_wait_for_req(card->host, &brq.mrq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 485 | |
| 486 | mmc_queue_bounce_post(mq); |
| 487 | |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 488 | /* |
| 489 | * Check for errors here, but don't jump to cmd_err |
| 490 | * until later as we need to wait for the card to leave |
| 491 | * programming mode even when things go wrong. |
| 492 | */ |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 493 | if (brq.cmd.error || brq.data.error || brq.stop.error) { |
| 494 | if (brq.data.blocks > 1 && rq_data_dir(req) == READ) { |
| 495 | /* Redo read one sector at a time */ |
| 496 | printk(KERN_WARNING "%s: retrying using single " |
| 497 | "block read\n", req->rq_disk->disk_name); |
| 498 | disable_multi = 1; |
| 499 | continue; |
| 500 | } |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 501 | status = get_card_status(card, req); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 502 | } |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | if (brq.cmd.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 505 | printk(KERN_ERR "%s: error %d sending read/write " |
| 506 | "command, response %#x, card status %#x\n", |
| 507 | req->rq_disk->disk_name, brq.cmd.error, |
| 508 | brq.cmd.resp[0], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | if (brq.data.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 512 | if (brq.data.error == -ETIMEDOUT && brq.mrq.stop) |
| 513 | /* 'Stop' response contains card status */ |
| 514 | status = brq.mrq.stop->resp[0]; |
| 515 | printk(KERN_ERR "%s: error %d transferring data," |
| 516 | " sector %u, nr %u, card status %#x\n", |
| 517 | req->rq_disk->disk_name, brq.data.error, |
Tejun Heo | 83096eb | 2009-05-07 22:24:39 +0900 | [diff] [blame] | 518 | (unsigned)blk_rq_pos(req), |
| 519 | (unsigned)blk_rq_sectors(req), status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | if (brq.stop.error) { |
Adrian Hunter | 504f191 | 2008-10-16 12:55:25 +0300 | [diff] [blame] | 523 | printk(KERN_ERR "%s: error %d sending stop command, " |
| 524 | "response %#x, card status %#x\n", |
| 525 | req->rq_disk->disk_name, brq.stop.error, |
| 526 | brq.stop.resp[0], status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | } |
| 528 | |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 529 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 530 | do { |
| 531 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 533 | cmd.opcode = MMC_SEND_STATUS; |
| 534 | cmd.arg = card->rca << 16; |
| 535 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 536 | err = mmc_wait_for_cmd(card->host, &cmd, 5); |
| 537 | if (err) { |
| 538 | printk(KERN_ERR "%s: error %d requesting status\n", |
| 539 | req->rq_disk->disk_name, err); |
| 540 | goto cmd_err; |
| 541 | } |
Pierre Ossman | d198f10 | 2007-11-02 18:21:13 +0100 | [diff] [blame] | 542 | /* |
| 543 | * Some cards mishandle the status bits, |
| 544 | * so make sure to check both the busy |
| 545 | * indication and the card state. |
| 546 | */ |
| 547 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
| 548 | (R1_CURRENT_STATE(cmd.resp[0]) == 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
| 550 | #if 0 |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 551 | if (cmd.resp[0] & ~0x00000900) |
| 552 | printk(KERN_ERR "%s: status = %08x\n", |
| 553 | req->rq_disk->disk_name, cmd.resp[0]); |
| 554 | if (mmc_decode_status(cmd.resp)) |
| 555 | goto cmd_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | #endif |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 557 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 559 | if (brq.cmd.error || brq.stop.error || brq.data.error) { |
| 560 | if (rq_data_dir(req) == READ) { |
| 561 | /* |
| 562 | * After an error, we redo I/O one sector at a |
| 563 | * time, so we only reach here after trying to |
| 564 | * read a single sector. |
| 565 | */ |
| 566 | spin_lock_irq(&md->lock); |
| 567 | ret = __blk_end_request(req, -EIO, brq.data.blksz); |
| 568 | spin_unlock_irq(&md->lock); |
| 569 | continue; |
| 570 | } |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 571 | goto cmd_err; |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 572 | } |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 573 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | /* |
| 575 | * A block was successfully transferred. |
| 576 | */ |
| 577 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 578 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 579 | spin_unlock_irq(&md->lock); |
| 580 | } while (ret); |
| 581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | return 1; |
| 583 | |
| 584 | cmd_err: |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 585 | /* |
| 586 | * If this is an SD card and we're writing, we can first |
| 587 | * mark the known good sectors as ok. |
| 588 | * |
| 589 | * If the card is not SD, we can still ok written sectors |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 590 | * as reported by the controller (which might be less than |
| 591 | * the real number of written sectors, but never more). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | */ |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 593 | if (mmc_card_sd(card)) { |
| 594 | u32 blocks; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 595 | |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 596 | blocks = mmc_sd_num_wr_blocks(card); |
| 597 | if (blocks != (u32)-1) { |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 598 | spin_lock_irq(&md->lock); |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 599 | ret = __blk_end_request(req, 0, blocks << 9); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 600 | spin_unlock_irq(&md->lock); |
| 601 | } |
Adrian Hunter | 6a79e39 | 2008-12-31 18:21:17 +0100 | [diff] [blame] | 602 | } else { |
| 603 | spin_lock_irq(&md->lock); |
| 604 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
| 605 | spin_unlock_irq(&md->lock); |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 606 | } |
| 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 609 | while (ret) |
| 610 | ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | spin_unlock_irq(&md->lock); |
| 612 | |
| 613 | return 0; |
| 614 | } |
| 615 | |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 616 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
| 617 | { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame^] | 618 | int ret; |
| 619 | struct mmc_blk_data *md = mq->data; |
| 620 | struct mmc_card *card = md->queue.card; |
| 621 | |
| 622 | mmc_claim_host(card->host); |
| 623 | |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 624 | if (req->cmd_flags & REQ_DISCARD) { |
| 625 | if (req->cmd_flags & REQ_SECURE) |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame^] | 626 | ret = mmc_blk_issue_secdiscard_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 627 | else |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame^] | 628 | ret = mmc_blk_issue_discard_rq(mq, req); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 629 | } else if (req->cmd_flags & REQ_FLUSH) { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame^] | 630 | ret = mmc_blk_issue_flush(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 631 | } else { |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame^] | 632 | ret = mmc_blk_issue_rw_rq(mq, req); |
Adrian Hunter | 4980454 | 2010-08-11 14:17:50 -0700 | [diff] [blame] | 633 | } |
Andrei Warkentin | 1a258db | 2011-04-11 18:10:24 -0500 | [diff] [blame^] | 634 | |
| 635 | mmc_release_host(card->host); |
| 636 | return ret; |
Adrian Hunter | bd788c9 | 2010-08-11 14:17:47 -0700 | [diff] [blame] | 637 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 638 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 639 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 640 | { |
| 641 | return mmc_card_readonly(card) || |
| 642 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 643 | } |
| 644 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 646 | { |
| 647 | struct mmc_blk_data *md; |
| 648 | int devidx, ret; |
| 649 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 650 | devidx = find_first_zero_bit(dev_use, max_devices); |
| 651 | if (devidx >= max_devices) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | return ERR_PTR(-ENOSPC); |
| 653 | __set_bit(devidx, dev_use); |
| 654 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 655 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 656 | if (!md) { |
| 657 | ret = -ENOMEM; |
| 658 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 659 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 660 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 661 | |
| 662 | /* |
| 663 | * Set the read-only status based on the supported commands |
| 664 | * and the write protect switch. |
| 665 | */ |
| 666 | md->read_only = mmc_blk_readonly(card); |
| 667 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 668 | md->disk = alloc_disk(perdev_minors); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 669 | if (md->disk == NULL) { |
| 670 | ret = -ENOMEM; |
| 671 | goto err_kfree; |
| 672 | } |
| 673 | |
| 674 | spin_lock_init(&md->lock); |
| 675 | md->usage = 1; |
| 676 | |
| 677 | ret = mmc_init_queue(&md->queue, card, &md->lock); |
| 678 | if (ret) |
| 679 | goto err_putdisk; |
| 680 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 681 | md->queue.issue_fn = mmc_blk_issue_rq; |
| 682 | md->queue.data = md; |
| 683 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 684 | md->disk->major = MMC_BLOCK_MAJOR; |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 685 | md->disk->first_minor = devidx * perdev_minors; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 686 | md->disk->fops = &mmc_bdops; |
| 687 | md->disk->private_data = md; |
| 688 | md->disk->queue = md->queue.queue; |
| 689 | md->disk->driverfs_dev = &card->dev; |
Marc-André Hébert | 3362177 | 2011-01-31 12:31:24 -0500 | [diff] [blame] | 690 | set_disk_ro(md->disk, md->read_only); |
Andrei Warkentin | f4c5522 | 2011-03-31 18:40:00 -0500 | [diff] [blame] | 691 | if (REL_WRITES_SUPPORTED(card)) |
| 692 | blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 693 | |
| 694 | /* |
| 695 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 696 | * |
| 697 | * - be set for removable media with permanent block devices |
| 698 | * - be unset for removable block devices with permanent media |
| 699 | * |
| 700 | * Since MMC block devices clearly fall under the second |
| 701 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 702 | * should use the block device creation/destruction hotplug |
| 703 | * messages to tell when the card is present. |
| 704 | */ |
| 705 | |
JiebingLi | 12578f6 | 2010-06-17 15:58:46 +0100 | [diff] [blame] | 706 | snprintf(md->disk->disk_name, sizeof(md->disk->disk_name), |
| 707 | "mmcblk%d", devidx); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 708 | |
Martin K. Petersen | e1defc4 | 2009-05-22 17:17:49 -0400 | [diff] [blame] | 709 | blk_queue_logical_block_size(md->queue.queue, 512); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 710 | |
Pierre Ossman | 85a18ad | 2007-02-17 22:15:27 +0100 | [diff] [blame] | 711 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 712 | /* |
| 713 | * The EXT_CSD sector count is in number or 512 byte |
| 714 | * sectors. |
| 715 | */ |
| 716 | set_capacity(md->disk, card->ext_csd.sectors); |
| 717 | } else { |
| 718 | /* |
| 719 | * The CSD capacity field is in units of read_blkbits. |
| 720 | * set_capacity takes units of 512 bytes. |
| 721 | */ |
| 722 | set_capacity(md->disk, |
| 723 | card->csd.capacity << (card->csd.read_blkbits - 9)); |
| 724 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 726 | |
| 727 | err_putdisk: |
| 728 | put_disk(md->disk); |
| 729 | err_kfree: |
| 730 | kfree(md); |
| 731 | out: |
| 732 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | static int |
| 736 | mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card) |
| 737 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | int err; |
| 739 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 740 | mmc_claim_host(card->host); |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 741 | err = mmc_set_blocklen(card, 512); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 742 | mmc_release_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
| 744 | if (err) { |
Adrian Hunter | 0f8d8ea | 2010-08-24 13:20:26 +0300 | [diff] [blame] | 745 | printk(KERN_ERR "%s: unable to set block size to 512: %d\n", |
| 746 | md->disk->disk_name, err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | return -EINVAL; |
| 748 | } |
| 749 | |
| 750 | return 0; |
| 751 | } |
| 752 | |
| 753 | static int mmc_blk_probe(struct mmc_card *card) |
| 754 | { |
| 755 | struct mmc_blk_data *md; |
| 756 | int err; |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 757 | char cap_str[10]; |
| 758 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 759 | /* |
| 760 | * Check that the card supports the command class(es) we need. |
| 761 | */ |
| 762 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | return -ENODEV; |
| 764 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | md = mmc_blk_alloc(card); |
| 766 | if (IS_ERR(md)) |
| 767 | return PTR_ERR(md); |
| 768 | |
| 769 | err = mmc_blk_set_blksize(md, card); |
| 770 | if (err) |
| 771 | goto out; |
| 772 | |
Yi Li | 444122f | 2009-02-05 15:31:57 +0800 | [diff] [blame] | 773 | string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2, |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 774 | cap_str, sizeof(cap_str)); |
| 775 | printk(KERN_INFO "%s: %s %s %s %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 776 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Pierre Ossman | a7bbb57 | 2008-09-06 10:57:57 +0200 | [diff] [blame] | 777 | cap_str, md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | |
| 779 | mmc_set_drvdata(card, md); |
| 780 | add_disk(md->disk); |
| 781 | return 0; |
| 782 | |
| 783 | out: |
Jarkko Lavinen | 0a74ff2 | 2010-01-08 14:42:59 -0800 | [diff] [blame] | 784 | mmc_cleanup_queue(&md->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | mmc_blk_put(md); |
| 786 | |
| 787 | return err; |
| 788 | } |
| 789 | |
| 790 | static void mmc_blk_remove(struct mmc_card *card) |
| 791 | { |
| 792 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 793 | |
| 794 | if (md) { |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 795 | /* Stop new requests from getting into the queue */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | del_gendisk(md->disk); |
| 797 | |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 798 | /* Then flush out any already in there */ |
| 799 | mmc_cleanup_queue(&md->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | mmc_blk_put(md); |
| 802 | } |
| 803 | mmc_set_drvdata(card, NULL); |
| 804 | } |
| 805 | |
| 806 | #ifdef CONFIG_PM |
| 807 | static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state) |
| 808 | { |
| 809 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 810 | |
| 811 | if (md) { |
| 812 | mmc_queue_suspend(&md->queue); |
| 813 | } |
| 814 | return 0; |
| 815 | } |
| 816 | |
| 817 | static int mmc_blk_resume(struct mmc_card *card) |
| 818 | { |
| 819 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 820 | |
| 821 | if (md) { |
| 822 | mmc_blk_set_blksize(md, card); |
| 823 | mmc_queue_resume(&md->queue); |
| 824 | } |
| 825 | return 0; |
| 826 | } |
| 827 | #else |
| 828 | #define mmc_blk_suspend NULL |
| 829 | #define mmc_blk_resume NULL |
| 830 | #endif |
| 831 | |
| 832 | static struct mmc_driver mmc_driver = { |
| 833 | .drv = { |
| 834 | .name = "mmcblk", |
| 835 | }, |
| 836 | .probe = mmc_blk_probe, |
| 837 | .remove = mmc_blk_remove, |
| 838 | .suspend = mmc_blk_suspend, |
| 839 | .resume = mmc_blk_resume, |
| 840 | }; |
| 841 | |
| 842 | static int __init mmc_blk_init(void) |
| 843 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 844 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | |
Olof Johansson | 5e71b7a | 2010-09-17 21:19:57 -0400 | [diff] [blame] | 846 | if (perdev_minors != CONFIG_MMC_BLOCK_MINORS) |
| 847 | pr_info("mmcblk: using %d minors per device\n", perdev_minors); |
| 848 | |
| 849 | max_devices = 256 / perdev_minors; |
| 850 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 851 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 852 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 855 | res = mmc_register_driver(&mmc_driver); |
| 856 | if (res) |
| 857 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 858 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 859 | return 0; |
| 860 | out2: |
| 861 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | out: |
| 863 | return res; |
| 864 | } |
| 865 | |
| 866 | static void __exit mmc_blk_exit(void) |
| 867 | { |
| 868 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 869 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | module_init(mmc_blk_init); |
| 873 | module_exit(mmc_blk_exit); |
| 874 | |
| 875 | MODULE_LICENSE("GPL"); |
| 876 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 877 | |