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