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