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