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> |
| 26 | #include <linux/errno.h> |
| 27 | #include <linux/hdreg.h> |
| 28 | #include <linux/kdev_t.h> |
| 29 | #include <linux/blkdev.h> |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 30 | #include <linux/mutex.h> |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 31 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | #include <linux/mmc/card.h> |
Pierre Ossman | 385e3227 | 2006-06-18 14:34:37 +0200 | [diff] [blame] | 34 | #include <linux/mmc/host.h> |
Pierre Ossman | da7fbe5 | 2006-12-24 22:46:55 +0100 | [diff] [blame] | 35 | #include <linux/mmc/mmc.h> |
| 36 | #include <linux/mmc/sd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #include <asm/system.h> |
| 39 | #include <asm/uaccess.h> |
| 40 | |
Pierre Ossman | 98ac216 | 2006-12-23 20:03:02 +0100 | [diff] [blame] | 41 | #include "queue.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * max 8 partitions per card |
| 45 | */ |
| 46 | #define MMC_SHIFT 3 |
David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 47 | #define MMC_NUM_MINORS (256 >> MMC_SHIFT) |
| 48 | |
Ben Collins | 203c801 | 2008-06-05 19:10:21 -0400 | [diff] [blame] | 49 | static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | /* |
| 52 | * There is one mmc_blk_data per slot. |
| 53 | */ |
| 54 | struct mmc_blk_data { |
| 55 | spinlock_t lock; |
| 56 | struct gendisk *disk; |
| 57 | struct mmc_queue queue; |
| 58 | |
| 59 | unsigned int usage; |
| 60 | unsigned int block_bits; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 61 | unsigned int read_only; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | }; |
| 63 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 64 | static DEFINE_MUTEX(open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | |
| 66 | static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk) |
| 67 | { |
| 68 | struct mmc_blk_data *md; |
| 69 | |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 70 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | md = disk->private_data; |
| 72 | if (md && md->usage == 0) |
| 73 | md = NULL; |
| 74 | if (md) |
| 75 | md->usage++; |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 76 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | return md; |
| 79 | } |
| 80 | |
| 81 | static void mmc_blk_put(struct mmc_blk_data *md) |
| 82 | { |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 83 | mutex_lock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | md->usage--; |
| 85 | if (md->usage == 0) { |
David Woodhouse | 1dff314 | 2007-11-21 18:45:12 +0100 | [diff] [blame] | 86 | int devidx = md->disk->first_minor >> MMC_SHIFT; |
| 87 | __clear_bit(devidx, dev_use); |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | put_disk(md->disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | kfree(md); |
| 91 | } |
Arjan van de Ven | a621aae | 2006-01-12 18:43:35 +0000 | [diff] [blame] | 92 | mutex_unlock(&open_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | static int mmc_blk_open(struct inode *inode, struct file *filp) |
| 96 | { |
| 97 | struct mmc_blk_data *md; |
| 98 | int ret = -ENXIO; |
| 99 | |
| 100 | md = mmc_blk_get(inode->i_bdev->bd_disk); |
| 101 | if (md) { |
| 102 | if (md->usage == 2) |
| 103 | check_disk_change(inode->i_bdev); |
| 104 | ret = 0; |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 105 | |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 106 | if ((filp->f_mode & FMODE_WRITE) && md->read_only) { |
| 107 | mmc_blk_put(md); |
Pierre Ossman | a00fc09 | 2005-09-06 15:18:52 -0700 | [diff] [blame] | 108 | ret = -EROFS; |
Andrew Morton | 70bb089 | 2008-09-05 14:00:24 -0700 | [diff] [blame] | 109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return ret; |
| 113 | } |
| 114 | |
| 115 | static int mmc_blk_release(struct inode *inode, struct file *filp) |
| 116 | { |
| 117 | struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data; |
| 118 | |
| 119 | mmc_blk_put(md); |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 124 | mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 126 | geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16); |
| 127 | geo->heads = 4; |
| 128 | geo->sectors = 16; |
| 129 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static struct block_device_operations mmc_bdops = { |
| 133 | .open = mmc_blk_open, |
| 134 | .release = mmc_blk_release, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 135 | .getgeo = mmc_blk_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | .owner = THIS_MODULE, |
| 137 | }; |
| 138 | |
| 139 | struct mmc_blk_request { |
| 140 | struct mmc_request mrq; |
| 141 | struct mmc_command cmd; |
| 142 | struct mmc_command stop; |
| 143 | struct mmc_data data; |
| 144 | }; |
| 145 | |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 146 | static u32 mmc_sd_num_wr_blocks(struct mmc_card *card) |
| 147 | { |
| 148 | int err; |
| 149 | u32 blocks; |
| 150 | |
| 151 | struct mmc_request mrq; |
| 152 | struct mmc_command cmd; |
| 153 | struct mmc_data data; |
| 154 | unsigned int timeout_us; |
| 155 | |
| 156 | struct scatterlist sg; |
| 157 | |
| 158 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 159 | |
| 160 | cmd.opcode = MMC_APP_CMD; |
| 161 | cmd.arg = card->rca << 16; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 162 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 163 | |
| 164 | err = mmc_wait_for_cmd(card->host, &cmd, 0); |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 165 | if (err) |
| 166 | return (u32)-1; |
| 167 | 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] | 168 | return (u32)-1; |
| 169 | |
| 170 | memset(&cmd, 0, sizeof(struct mmc_command)); |
| 171 | |
| 172 | cmd.opcode = SD_APP_SEND_NUM_WR_BLKS; |
| 173 | cmd.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 174 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 175 | |
| 176 | memset(&data, 0, sizeof(struct mmc_data)); |
| 177 | |
| 178 | data.timeout_ns = card->csd.tacc_ns * 100; |
| 179 | data.timeout_clks = card->csd.tacc_clks * 100; |
| 180 | |
| 181 | timeout_us = data.timeout_ns / 1000; |
| 182 | timeout_us += data.timeout_clks * 1000 / |
| 183 | (card->host->ios.clock / 1000); |
| 184 | |
| 185 | if (timeout_us > 100000) { |
| 186 | data.timeout_ns = 100000000; |
| 187 | data.timeout_clks = 0; |
| 188 | } |
| 189 | |
| 190 | data.blksz = 4; |
| 191 | data.blocks = 1; |
| 192 | data.flags = MMC_DATA_READ; |
| 193 | data.sg = &sg; |
| 194 | data.sg_len = 1; |
| 195 | |
| 196 | memset(&mrq, 0, sizeof(struct mmc_request)); |
| 197 | |
| 198 | mrq.cmd = &cmd; |
| 199 | mrq.data = &data; |
| 200 | |
| 201 | sg_init_one(&sg, &blocks, 4); |
| 202 | |
| 203 | mmc_wait_for_req(card->host, &mrq); |
| 204 | |
Pierre Ossman | 17b0429 | 2007-07-22 22:18:46 +0200 | [diff] [blame] | 205 | if (cmd.error || data.error) |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 206 | return (u32)-1; |
| 207 | |
| 208 | blocks = ntohl(blocks); |
| 209 | |
| 210 | return blocks; |
| 211 | } |
| 212 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req) |
| 214 | { |
| 215 | struct mmc_blk_data *md = mq->data; |
| 216 | struct mmc_card *card = md->queue.card; |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 217 | struct mmc_blk_request brq; |
Pierre Ossman | b41e9c7 | 2008-07-29 01:23:24 +0200 | [diff] [blame] | 218 | int ret = 1, data_size, i; |
| 219 | struct scatterlist *sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 221 | mmc_claim_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
| 223 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | struct mmc_command cmd; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 225 | u32 readcmd, writecmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | memset(&brq, 0, sizeof(struct mmc_blk_request)); |
| 228 | brq.mrq.cmd = &brq.cmd; |
| 229 | brq.mrq.data = &brq.data; |
| 230 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 231 | brq.cmd.arg = req->sector; |
| 232 | if (!mmc_card_blockaddr(card)) |
| 233 | brq.cmd.arg <<= 9; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 234 | brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC; |
Pavel Pisa | 2c171bf | 2006-05-19 21:48:03 +0100 | [diff] [blame] | 235 | brq.data.blksz = 1 << md->block_bits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | brq.stop.opcode = MMC_STOP_TRANSMISSION; |
| 237 | brq.stop.arg = 0; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 238 | brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; |
Pierre Ossman | 55db890 | 2006-11-21 17:55:45 +0100 | [diff] [blame] | 239 | brq.data.blocks = req->nr_sectors >> (md->block_bits - 9); |
| 240 | if (brq.data.blocks > card->host->max_blk_count) |
| 241 | brq.data.blocks = card->host->max_blk_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 243 | if (brq.data.blocks > 1) { |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 244 | /* SPI multiblock writes terminate using a special |
| 245 | * token, not a STOP_TRANSMISSION request. |
| 246 | */ |
| 247 | if (!mmc_host_is_spi(card->host) |
| 248 | || rq_data_dir(req) == READ) |
| 249 | brq.mrq.stop = &brq.stop; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 250 | readcmd = MMC_READ_MULTIPLE_BLOCK; |
| 251 | writecmd = MMC_WRITE_MULTIPLE_BLOCK; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 252 | } else { |
| 253 | brq.mrq.stop = NULL; |
Russell King | db53f28 | 2006-08-30 15:14:56 +0100 | [diff] [blame] | 254 | readcmd = MMC_READ_SINGLE_BLOCK; |
| 255 | writecmd = MMC_WRITE_BLOCK; |
| 256 | } |
| 257 | |
| 258 | if (rq_data_dir(req) == READ) { |
| 259 | brq.cmd.opcode = readcmd; |
| 260 | brq.data.flags |= MMC_DATA_READ; |
| 261 | } else { |
| 262 | brq.cmd.opcode = writecmd; |
| 263 | brq.data.flags |= MMC_DATA_WRITE; |
Russell King | 788ee7b | 2006-01-09 21:12:17 +0000 | [diff] [blame] | 264 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Pierre Ossman | b146d26 | 2007-07-24 19:16:54 +0200 | [diff] [blame] | 266 | mmc_set_data_timeout(&brq.data, card); |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | brq.data.sg = mq->sg; |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 269 | brq.data.sg_len = mmc_queue_map_sg(mq); |
| 270 | |
| 271 | mmc_queue_bounce_pre(mq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | |
Pierre Ossman | b41e9c7 | 2008-07-29 01:23:24 +0200 | [diff] [blame] | 273 | /* |
| 274 | * Adjust the sg list so it is the same size as the |
| 275 | * request. |
| 276 | */ |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 277 | if (brq.data.blocks != |
| 278 | (req->nr_sectors >> (md->block_bits - 9))) { |
| 279 | data_size = brq.data.blocks * brq.data.blksz; |
Pierre Ossman | b41e9c7 | 2008-07-29 01:23:24 +0200 | [diff] [blame] | 280 | for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) { |
| 281 | data_size -= sg->length; |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 282 | if (data_size <= 0) { |
Pierre Ossman | b41e9c7 | 2008-07-29 01:23:24 +0200 | [diff] [blame] | 283 | sg->length += data_size; |
| 284 | i++; |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 285 | break; |
| 286 | } |
| 287 | } |
Pierre Ossman | b41e9c7 | 2008-07-29 01:23:24 +0200 | [diff] [blame] | 288 | brq.data.sg_len = i; |
Alex Dubov | 14d836e | 2007-04-13 19:04:38 +0200 | [diff] [blame] | 289 | } |
| 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | mmc_wait_for_req(card->host, &brq.mrq); |
Pierre Ossman | 98ccf14 | 2007-05-12 00:26:16 +0200 | [diff] [blame] | 292 | |
| 293 | mmc_queue_bounce_post(mq); |
| 294 | |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 295 | /* |
| 296 | * Check for errors here, but don't jump to cmd_err |
| 297 | * until later as we need to wait for the card to leave |
| 298 | * programming mode even when things go wrong. |
| 299 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | if (brq.cmd.error) { |
| 301 | printk(KERN_ERR "%s: error %d sending read/write command\n", |
| 302 | req->rq_disk->disk_name, brq.cmd.error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | if (brq.data.error) { |
| 306 | printk(KERN_ERR "%s: error %d transferring data\n", |
| 307 | req->rq_disk->disk_name, brq.data.error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | if (brq.stop.error) { |
| 311 | printk(KERN_ERR "%s: error %d sending stop command\n", |
| 312 | req->rq_disk->disk_name, brq.stop.error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | } |
| 314 | |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 315 | if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) { |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 316 | do { |
| 317 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 319 | cmd.opcode = MMC_SEND_STATUS; |
| 320 | cmd.arg = card->rca << 16; |
| 321 | cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; |
| 322 | err = mmc_wait_for_cmd(card->host, &cmd, 5); |
| 323 | if (err) { |
| 324 | printk(KERN_ERR "%s: error %d requesting status\n", |
| 325 | req->rq_disk->disk_name, err); |
| 326 | goto cmd_err; |
| 327 | } |
Pierre Ossman | d198f10 | 2007-11-02 18:21:13 +0100 | [diff] [blame] | 328 | /* |
| 329 | * Some cards mishandle the status bits, |
| 330 | * so make sure to check both the busy |
| 331 | * indication and the card state. |
| 332 | */ |
| 333 | } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || |
| 334 | (R1_CURRENT_STATE(cmd.resp[0]) == 7)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | #if 0 |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 337 | if (cmd.resp[0] & ~0x00000900) |
| 338 | printk(KERN_ERR "%s: status = %08x\n", |
| 339 | req->rq_disk->disk_name, cmd.resp[0]); |
| 340 | if (mmc_decode_status(cmd.resp)) |
| 341 | goto cmd_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | #endif |
Russell King | 2ed6d22 | 2006-09-24 10:46:43 +0100 | [diff] [blame] | 343 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | |
Pierre Ossman | 979ce72 | 2008-06-29 12:19:47 +0200 | [diff] [blame] | 345 | if (brq.cmd.error || brq.data.error || brq.stop.error) |
| 346 | goto cmd_err; |
| 347 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | /* |
| 349 | * A block was successfully transferred. |
| 350 | */ |
| 351 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 352 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | spin_unlock_irq(&md->lock); |
| 354 | } while (ret); |
| 355 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 356 | mmc_release_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
| 358 | return 1; |
| 359 | |
| 360 | cmd_err: |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 361 | /* |
| 362 | * If this is an SD card and we're writing, we can first |
| 363 | * mark the known good sectors as ok. |
| 364 | * |
| 365 | * If the card is not SD, we can still ok written sectors |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 366 | * as reported by the controller (which might be less than |
| 367 | * the real number of written sectors, but never more). |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 368 | * |
| 369 | * For reads we just fail the entire chunk as that should |
| 370 | * be safe in all cases. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | */ |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 372 | if (rq_data_dir(req) != READ) { |
| 373 | if (mmc_card_sd(card)) { |
| 374 | u32 blocks; |
| 375 | unsigned int bytes; |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 376 | |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 377 | blocks = mmc_sd_num_wr_blocks(card); |
| 378 | if (blocks != (u32)-1) { |
| 379 | if (card->csd.write_partial) |
| 380 | bytes = blocks << md->block_bits; |
| 381 | else |
| 382 | bytes = blocks << 9; |
| 383 | spin_lock_irq(&md->lock); |
| 384 | ret = __blk_end_request(req, 0, bytes); |
| 385 | spin_unlock_irq(&md->lock); |
| 386 | } |
| 387 | } else { |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 388 | spin_lock_irq(&md->lock); |
Pierre Ossman | 23af603 | 2008-07-06 01:10:27 +0200 | [diff] [blame] | 389 | ret = __blk_end_request(req, 0, brq.data.bytes_xfered); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 390 | spin_unlock_irq(&md->lock); |
| 391 | } |
Pierre Ossman | 176f00f | 2006-10-04 02:15:41 -0700 | [diff] [blame] | 392 | } |
| 393 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 394 | mmc_release_host(card->host); |
Pierre Ossman | ec5a19d | 2006-10-06 00:44:03 -0700 | [diff] [blame] | 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | spin_lock_irq(&md->lock); |
Kiyoshi Ueda | fd53983 | 2007-12-11 17:48:29 -0500 | [diff] [blame] | 397 | while (ret) |
| 398 | ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | spin_unlock_irq(&md->lock); |
| 400 | |
| 401 | return 0; |
| 402 | } |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 405 | static inline int mmc_blk_readonly(struct mmc_card *card) |
| 406 | { |
| 407 | return mmc_card_readonly(card) || |
| 408 | !(card->csd.cmdclass & CCC_BLOCK_WRITE); |
| 409 | } |
| 410 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card) |
| 412 | { |
| 413 | struct mmc_blk_data *md; |
| 414 | int devidx, ret; |
| 415 | |
| 416 | devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS); |
| 417 | if (devidx >= MMC_NUM_MINORS) |
| 418 | return ERR_PTR(-ENOSPC); |
| 419 | __set_bit(devidx, dev_use); |
| 420 | |
Yoann Padioleau | dd00cc4 | 2007-07-19 01:49:03 -0700 | [diff] [blame] | 421 | md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 422 | if (!md) { |
| 423 | ret = -ENOMEM; |
| 424 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | } |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 426 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 427 | |
| 428 | /* |
| 429 | * Set the read-only status based on the supported commands |
| 430 | * and the write protect switch. |
| 431 | */ |
| 432 | md->read_only = mmc_blk_readonly(card); |
| 433 | |
| 434 | /* |
Pierre Ossman | 6fe9feb | 2006-09-07 16:01:30 +0100 | [diff] [blame] | 435 | * Both SD and MMC specifications state (although a bit |
| 436 | * unclearly in the MMC case) that a block size of 512 |
| 437 | * bytes must always be supported by the card. |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 438 | */ |
Pierre Ossman | 6fe9feb | 2006-09-07 16:01:30 +0100 | [diff] [blame] | 439 | md->block_bits = 9; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 440 | |
| 441 | md->disk = alloc_disk(1 << MMC_SHIFT); |
| 442 | if (md->disk == NULL) { |
| 443 | ret = -ENOMEM; |
| 444 | goto err_kfree; |
| 445 | } |
| 446 | |
| 447 | spin_lock_init(&md->lock); |
| 448 | md->usage = 1; |
| 449 | |
| 450 | ret = mmc_init_queue(&md->queue, card, &md->lock); |
| 451 | if (ret) |
| 452 | goto err_putdisk; |
| 453 | |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 454 | md->queue.issue_fn = mmc_blk_issue_rq; |
| 455 | md->queue.data = md; |
| 456 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 457 | md->disk->major = MMC_BLOCK_MAJOR; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 458 | md->disk->first_minor = devidx << MMC_SHIFT; |
| 459 | md->disk->fops = &mmc_bdops; |
| 460 | md->disk->private_data = md; |
| 461 | md->disk->queue = md->queue.queue; |
| 462 | md->disk->driverfs_dev = &card->dev; |
| 463 | |
| 464 | /* |
| 465 | * As discussed on lkml, GENHD_FL_REMOVABLE should: |
| 466 | * |
| 467 | * - be set for removable media with permanent block devices |
| 468 | * - be unset for removable block devices with permanent media |
| 469 | * |
| 470 | * Since MMC block devices clearly fall under the second |
| 471 | * case, we do not set GENHD_FL_REMOVABLE. Userspace |
| 472 | * should use the block device creation/destruction hotplug |
| 473 | * messages to tell when the card is present. |
| 474 | */ |
| 475 | |
| 476 | sprintf(md->disk->disk_name, "mmcblk%d", devidx); |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 477 | |
| 478 | blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits); |
| 479 | |
Pierre Ossman | 85a18ad | 2007-02-17 22:15:27 +0100 | [diff] [blame] | 480 | if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) { |
| 481 | /* |
| 482 | * The EXT_CSD sector count is in number or 512 byte |
| 483 | * sectors. |
| 484 | */ |
| 485 | set_capacity(md->disk, card->ext_csd.sectors); |
| 486 | } else { |
| 487 | /* |
| 488 | * The CSD capacity field is in units of read_blkbits. |
| 489 | * set_capacity takes units of 512 bytes. |
| 490 | */ |
| 491 | set_capacity(md->disk, |
| 492 | card->csd.capacity << (card->csd.read_blkbits - 9)); |
| 493 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | return md; |
Russell King | a6f6c96 | 2006-01-03 22:38:44 +0000 | [diff] [blame] | 495 | |
| 496 | err_putdisk: |
| 497 | put_disk(md->disk); |
| 498 | err_kfree: |
| 499 | kfree(md); |
| 500 | out: |
| 501 | return ERR_PTR(ret); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | static int |
| 505 | mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card) |
| 506 | { |
| 507 | struct mmc_command cmd; |
| 508 | int err; |
| 509 | |
Philip Langdale | fba68bd | 2007-01-04 06:57:32 -0800 | [diff] [blame] | 510 | /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */ |
| 511 | if (mmc_card_blockaddr(card)) |
| 512 | return 0; |
| 513 | |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 514 | mmc_claim_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | cmd.opcode = MMC_SET_BLOCKLEN; |
Russell King | d2b1839 | 2005-12-22 23:21:38 +0000 | [diff] [blame] | 516 | cmd.arg = 1 << md->block_bits; |
David Brownell | 7213d17 | 2007-08-08 09:10:23 -0700 | [diff] [blame] | 517 | cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | err = mmc_wait_for_cmd(card->host, &cmd, 5); |
Pierre Ossman | b855885 | 2007-01-03 19:47:29 +0100 | [diff] [blame] | 519 | mmc_release_host(card->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | |
| 521 | if (err) { |
| 522 | printk(KERN_ERR "%s: unable to set block size to %d: %d\n", |
| 523 | md->disk->disk_name, cmd.arg, err); |
| 524 | return -EINVAL; |
| 525 | } |
| 526 | |
| 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | static int mmc_blk_probe(struct mmc_card *card) |
| 531 | { |
| 532 | struct mmc_blk_data *md; |
| 533 | int err; |
| 534 | |
Pierre Ossman | 912490d | 2005-05-21 10:27:02 +0100 | [diff] [blame] | 535 | /* |
| 536 | * Check that the card supports the command class(es) we need. |
| 537 | */ |
| 538 | if (!(card->csd.cmdclass & CCC_BLOCK_READ)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return -ENODEV; |
| 540 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | md = mmc_blk_alloc(card); |
| 542 | if (IS_ERR(md)) |
| 543 | return PTR_ERR(md); |
| 544 | |
| 545 | err = mmc_blk_set_blksize(md, card); |
| 546 | if (err) |
| 547 | goto out; |
| 548 | |
Andrew Morton | 51828ab | 2006-01-13 21:44:18 +0000 | [diff] [blame] | 549 | printk(KERN_INFO "%s: %s %s %lluKiB %s\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | md->disk->disk_name, mmc_card_id(card), mmc_card_name(card), |
Andrew Morton | 51828ab | 2006-01-13 21:44:18 +0000 | [diff] [blame] | 551 | (unsigned long long)(get_capacity(md->disk) >> 1), |
| 552 | md->read_only ? "(ro)" : ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | |
| 554 | mmc_set_drvdata(card, md); |
| 555 | add_disk(md->disk); |
| 556 | return 0; |
| 557 | |
| 558 | out: |
| 559 | mmc_blk_put(md); |
| 560 | |
| 561 | return err; |
| 562 | } |
| 563 | |
| 564 | static void mmc_blk_remove(struct mmc_card *card) |
| 565 | { |
| 566 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 567 | |
| 568 | if (md) { |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 569 | /* Stop new requests from getting into the queue */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | del_gendisk(md->disk); |
| 571 | |
Pierre Ossman | 89b4e13 | 2006-11-14 22:08:16 +0100 | [diff] [blame] | 572 | /* Then flush out any already in there */ |
| 573 | mmc_cleanup_queue(&md->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | mmc_blk_put(md); |
| 576 | } |
| 577 | mmc_set_drvdata(card, NULL); |
| 578 | } |
| 579 | |
| 580 | #ifdef CONFIG_PM |
| 581 | static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state) |
| 582 | { |
| 583 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 584 | |
| 585 | if (md) { |
| 586 | mmc_queue_suspend(&md->queue); |
| 587 | } |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | static int mmc_blk_resume(struct mmc_card *card) |
| 592 | { |
| 593 | struct mmc_blk_data *md = mmc_get_drvdata(card); |
| 594 | |
| 595 | if (md) { |
| 596 | mmc_blk_set_blksize(md, card); |
| 597 | mmc_queue_resume(&md->queue); |
| 598 | } |
| 599 | return 0; |
| 600 | } |
| 601 | #else |
| 602 | #define mmc_blk_suspend NULL |
| 603 | #define mmc_blk_resume NULL |
| 604 | #endif |
| 605 | |
| 606 | static struct mmc_driver mmc_driver = { |
| 607 | .drv = { |
| 608 | .name = "mmcblk", |
| 609 | }, |
| 610 | .probe = mmc_blk_probe, |
| 611 | .remove = mmc_blk_remove, |
| 612 | .suspend = mmc_blk_suspend, |
| 613 | .resume = mmc_blk_resume, |
| 614 | }; |
| 615 | |
| 616 | static int __init mmc_blk_init(void) |
| 617 | { |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 618 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 620 | res = register_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
| 621 | if (res) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 624 | res = mmc_register_driver(&mmc_driver); |
| 625 | if (res) |
| 626 | goto out2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | |
Akinobu Mita | 9d4e98e | 2008-09-13 19:02:07 +0900 | [diff] [blame] | 628 | return 0; |
| 629 | out2: |
| 630 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | out: |
| 632 | return res; |
| 633 | } |
| 634 | |
| 635 | static void __exit mmc_blk_exit(void) |
| 636 | { |
| 637 | mmc_unregister_driver(&mmc_driver); |
Pierre Ossman | fe6b4c8 | 2007-05-14 17:27:29 +0200 | [diff] [blame] | 638 | unregister_blkdev(MMC_BLOCK_MAJOR, "mmc"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | } |
| 640 | |
| 641 | module_init(mmc_blk_init); |
| 642 | module_exit(mmc_blk_exit); |
| 643 | |
| 644 | MODULE_LICENSE("GPL"); |
| 645 | MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver"); |
| 646 | |