blob: cb9fbc83b09095f8a0aa77572c0e9304641f300b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Block driver for media (i.e., flash cards)
3 *
4 * Copyright 2002 Hewlett-Packard Company
Pierre Ossman979ce722008-06-29 12:19:47 +02005 * Copyright 2005-2008 Pierre Ossman
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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 Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/kernel.h>
25#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/errno.h>
28#include <linux/hdreg.h>
29#include <linux/kdev_t.h>
30#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000031#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070032#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020033#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020036#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010037#include <linux/mmc/mmc.h>
38#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/system.h>
41#include <asm/uaccess.h>
42
Pierre Ossman98ac2162006-12-23 20:03:02 +010043#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000045MODULE_ALIAS("mmc:block");
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047/*
48 * max 8 partitions per card
49 */
50#define MMC_SHIFT 3
David Woodhouse1dff3142007-11-21 18:45:12 +010051#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
52
Ben Collins203c8012008-06-05 19:10:21 -040053static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/*
56 * There is one mmc_blk_data per slot.
57 */
58struct mmc_blk_data {
59 spinlock_t lock;
60 struct gendisk *disk;
61 struct mmc_queue queue;
62
63 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000064 unsigned int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065};
66
Arjan van de Vena621aae2006-01-12 18:43:35 +000067static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
70{
71 struct mmc_blk_data *md;
72
Arjan van de Vena621aae2006-01-12 18:43:35 +000073 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 md = disk->private_data;
75 if (md && md->usage == 0)
76 md = NULL;
77 if (md)
78 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +000079 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81 return md;
82}
83
84static void mmc_blk_put(struct mmc_blk_data *md)
85{
Arjan van de Vena621aae2006-01-12 18:43:35 +000086 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 md->usage--;
88 if (md->usage == 0) {
Anna Lemehova7d92df62010-01-08 14:42:58 -080089 int devmaj = MAJOR(disk_devt(md->disk));
Tejun Heof331c022008-09-03 09:01:48 +020090 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
Anna Lemehova7d92df62010-01-08 14:42:58 -080091
92 if (!devmaj)
93 devidx = md->disk->first_minor >> MMC_SHIFT;
94
Adrian Hunter5fa83ce2010-01-08 14:43:00 -080095 blk_cleanup_queue(md->queue.queue);
96
David Woodhouse1dff3142007-11-21 18:45:12 +010097 __clear_bit(devidx, dev_use);
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 kfree(md);
101 }
Arjan van de Vena621aae2006-01-12 18:43:35 +0000102 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Al Viroa5a15612008-03-02 10:33:30 -0500105static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Al Viroa5a15612008-03-02 10:33:30 -0500107 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 int ret = -ENXIO;
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (md) {
111 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500112 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700114
Al Viroa5a15612008-03-02 10:33:30 -0500115 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700116 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700117 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
120
121 return ret;
122}
123
Al Viroa5a15612008-03-02 10:33:30 -0500124static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Al Viroa5a15612008-03-02 10:33:30 -0500126 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 mmc_blk_put(md);
129 return 0;
130}
131
132static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800133mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800135 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
136 geo->heads = 4;
137 geo->sectors = 16;
138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700141static const struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500142 .open = mmc_blk_open,
143 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800144 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 .owner = THIS_MODULE,
146};
147
148struct mmc_blk_request {
149 struct mmc_request mrq;
150 struct mmc_command cmd;
151 struct mmc_command stop;
152 struct mmc_data data;
153};
154
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700155static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
156{
157 int err;
Ben Dooks051913d2009-06-08 23:33:57 +0100158 u32 result;
159 __be32 *blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700160
161 struct mmc_request mrq;
162 struct mmc_command cmd;
163 struct mmc_data data;
164 unsigned int timeout_us;
165
166 struct scatterlist sg;
167
168 memset(&cmd, 0, sizeof(struct mmc_command));
169
170 cmd.opcode = MMC_APP_CMD;
171 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700172 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700173
174 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700175 if (err)
176 return (u32)-1;
177 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700178 return (u32)-1;
179
180 memset(&cmd, 0, sizeof(struct mmc_command));
181
182 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
183 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700184 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700185
186 memset(&data, 0, sizeof(struct mmc_data));
187
188 data.timeout_ns = card->csd.tacc_ns * 100;
189 data.timeout_clks = card->csd.tacc_clks * 100;
190
191 timeout_us = data.timeout_ns / 1000;
192 timeout_us += data.timeout_clks * 1000 /
193 (card->host->ios.clock / 1000);
194
195 if (timeout_us > 100000) {
196 data.timeout_ns = 100000000;
197 data.timeout_clks = 0;
198 }
199
200 data.blksz = 4;
201 data.blocks = 1;
202 data.flags = MMC_DATA_READ;
203 data.sg = &sg;
204 data.sg_len = 1;
205
206 memset(&mrq, 0, sizeof(struct mmc_request));
207
208 mrq.cmd = &cmd;
209 mrq.data = &data;
210
Ben Dooks051913d2009-06-08 23:33:57 +0100211 blocks = kmalloc(4, GFP_KERNEL);
212 if (!blocks)
213 return (u32)-1;
214
215 sg_init_one(&sg, blocks, 4);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700216
217 mmc_wait_for_req(card->host, &mrq);
218
Ben Dooks051913d2009-06-08 23:33:57 +0100219 result = ntohl(*blocks);
220 kfree(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700221
Ben Dooks051913d2009-06-08 23:33:57 +0100222 if (cmd.error || data.error)
223 result = (u32)-1;
224
225 return result;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700226}
227
Adrian Hunter504f1912008-10-16 12:55:25 +0300228static u32 get_card_status(struct mmc_card *card, struct request *req)
229{
230 struct mmc_command cmd;
231 int err;
232
233 memset(&cmd, 0, sizeof(struct mmc_command));
234 cmd.opcode = MMC_SEND_STATUS;
235 if (!mmc_host_is_spi(card->host))
236 cmd.arg = card->rca << 16;
237 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
238 err = mmc_wait_for_cmd(card->host, &cmd, 0);
239 if (err)
240 printk(KERN_ERR "%s: error %d sending status comand",
241 req->rq_disk->disk_name, err);
242 return cmd.resp[0];
243}
244
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
246{
247 struct mmc_blk_data *md = mq->data;
248 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700249 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100250 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Pierre Ossmanb8558852007-01-03 19:47:29 +0100252 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
254 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct mmc_command cmd;
Adrian Hunter504f1912008-10-16 12:55:25 +0300256 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 memset(&brq, 0, sizeof(struct mmc_blk_request));
259 brq.mrq.cmd = &brq.cmd;
260 brq.mrq.data = &brq.data;
261
Tejun Heo83096eb2009-05-07 22:24:39 +0900262 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800263 if (!mmc_card_blockaddr(card))
264 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700265 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200266 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 brq.stop.opcode = MMC_STOP_TRANSMISSION;
268 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700269 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900270 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Adrian Hunter6a79e392008-12-31 18:21:17 +0100272 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200273 * The block layer doesn't support all sector count
274 * restrictions, so we need to be prepared for too big
275 * requests.
276 */
277 if (brq.data.blocks > card->host->max_blk_count)
278 brq.data.blocks = card->host->max_blk_count;
279
280 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100281 * After a read error, we redo the request one sector at a time
282 * in order to accurately determine which sectors can be read
283 * successfully.
284 */
285 if (disable_multi && brq.data.blocks > 1)
286 brq.data.blocks = 1;
287
Russell King788ee7b2006-01-09 21:12:17 +0000288 if (brq.data.blocks > 1) {
David Brownell7213d172007-08-08 09:10:23 -0700289 /* SPI multiblock writes terminate using a special
290 * token, not a STOP_TRANSMISSION request.
291 */
292 if (!mmc_host_is_spi(card->host)
293 || rq_data_dir(req) == READ)
294 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100295 readcmd = MMC_READ_MULTIPLE_BLOCK;
296 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000297 } else {
298 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100299 readcmd = MMC_READ_SINGLE_BLOCK;
300 writecmd = MMC_WRITE_BLOCK;
301 }
302
303 if (rq_data_dir(req) == READ) {
304 brq.cmd.opcode = readcmd;
305 brq.data.flags |= MMC_DATA_READ;
306 } else {
307 brq.cmd.opcode = writecmd;
308 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Pierre Ossmanb146d262007-07-24 19:16:54 +0200311 mmc_set_data_timeout(&brq.data, card);
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200314 brq.data.sg_len = mmc_queue_map_sg(mq);
315
Adrian Hunter6a79e392008-12-31 18:21:17 +0100316 /*
317 * Adjust the sg list so it is the same size as the
318 * request.
319 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900320 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100321 int i, data_size = brq.data.blocks << 9;
322 struct scatterlist *sg;
323
324 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
325 data_size -= sg->length;
326 if (data_size <= 0) {
327 sg->length += data_size;
328 i++;
329 break;
330 }
331 }
332 brq.data.sg_len = i;
333 }
334
Pierre Ossman98ccf142007-05-12 00:26:16 +0200335 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
337 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200338
339 mmc_queue_bounce_post(mq);
340
Pierre Ossman979ce722008-06-29 12:19:47 +0200341 /*
342 * Check for errors here, but don't jump to cmd_err
343 * until later as we need to wait for the card to leave
344 * programming mode even when things go wrong.
345 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100346 if (brq.cmd.error || brq.data.error || brq.stop.error) {
347 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
348 /* Redo read one sector at a time */
349 printk(KERN_WARNING "%s: retrying using single "
350 "block read\n", req->rq_disk->disk_name);
351 disable_multi = 1;
352 continue;
353 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300354 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100355 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300358 printk(KERN_ERR "%s: error %d sending read/write "
359 "command, response %#x, card status %#x\n",
360 req->rq_disk->disk_name, brq.cmd.error,
361 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 }
363
364 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300365 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
366 /* 'Stop' response contains card status */
367 status = brq.mrq.stop->resp[0];
368 printk(KERN_ERR "%s: error %d transferring data,"
369 " sector %u, nr %u, card status %#x\n",
370 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900371 (unsigned)blk_rq_pos(req),
372 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300376 printk(KERN_ERR "%s: error %d sending stop command, "
377 "response %#x, card status %#x\n",
378 req->rq_disk->disk_name, brq.stop.error,
379 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 }
381
David Brownell7213d172007-08-08 09:10:23 -0700382 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100383 do {
384 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Russell King2ed6d222006-09-24 10:46:43 +0100386 cmd.opcode = MMC_SEND_STATUS;
387 cmd.arg = card->rca << 16;
388 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
389 err = mmc_wait_for_cmd(card->host, &cmd, 5);
390 if (err) {
391 printk(KERN_ERR "%s: error %d requesting status\n",
392 req->rq_disk->disk_name, err);
393 goto cmd_err;
394 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100395 /*
396 * Some cards mishandle the status bits,
397 * so make sure to check both the busy
398 * indication and the card state.
399 */
400 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
401 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100404 if (cmd.resp[0] & ~0x00000900)
405 printk(KERN_ERR "%s: status = %08x\n",
406 req->rq_disk->disk_name, cmd.resp[0]);
407 if (mmc_decode_status(cmd.resp))
408 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#endif
Russell King2ed6d222006-09-24 10:46:43 +0100410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Adrian Hunter6a79e392008-12-31 18:21:17 +0100412 if (brq.cmd.error || brq.stop.error || brq.data.error) {
413 if (rq_data_dir(req) == READ) {
414 /*
415 * After an error, we redo I/O one sector at a
416 * time, so we only reach here after trying to
417 * read a single sector.
418 */
419 spin_lock_irq(&md->lock);
420 ret = __blk_end_request(req, -EIO, brq.data.blksz);
421 spin_unlock_irq(&md->lock);
422 continue;
423 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200424 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100425 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 /*
428 * A block was successfully transferred.
429 */
430 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500431 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 spin_unlock_irq(&md->lock);
433 } while (ret);
434
Pierre Ossmanb8558852007-01-03 19:47:29 +0100435 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
437 return 1;
438
439 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700440 /*
441 * If this is an SD card and we're writing, we can first
442 * mark the known good sectors as ok.
443 *
444 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200445 * as reported by the controller (which might be less than
446 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100448 if (mmc_card_sd(card)) {
449 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700450
Adrian Hunter6a79e392008-12-31 18:21:17 +0100451 blocks = mmc_sd_num_wr_blocks(card);
452 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700453 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100454 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700455 spin_unlock_irq(&md->lock);
456 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100457 } else {
458 spin_lock_irq(&md->lock);
459 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
460 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700461 }
462
Pierre Ossmanb8558852007-01-03 19:47:29 +0100463 mmc_release_host(card->host);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500466 while (ret)
467 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 spin_unlock_irq(&md->lock);
469
470 return 0;
471}
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Russell Kinga6f6c962006-01-03 22:38:44 +0000474static inline int mmc_blk_readonly(struct mmc_card *card)
475{
476 return mmc_card_readonly(card) ||
477 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
481{
482 struct mmc_blk_data *md;
483 int devidx, ret;
484
485 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
486 if (devidx >= MMC_NUM_MINORS)
487 return ERR_PTR(-ENOSPC);
488 __set_bit(devidx, dev_use);
489
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700490 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000491 if (!md) {
492 ret = -ENOMEM;
493 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000495
Russell Kinga6f6c962006-01-03 22:38:44 +0000496
497 /*
498 * Set the read-only status based on the supported commands
499 * and the write protect switch.
500 */
501 md->read_only = mmc_blk_readonly(card);
502
Russell Kinga6f6c962006-01-03 22:38:44 +0000503 md->disk = alloc_disk(1 << MMC_SHIFT);
504 if (md->disk == NULL) {
505 ret = -ENOMEM;
506 goto err_kfree;
507 }
508
509 spin_lock_init(&md->lock);
510 md->usage = 1;
511
512 ret = mmc_init_queue(&md->queue, card, &md->lock);
513 if (ret)
514 goto err_putdisk;
515
Russell Kinga6f6c962006-01-03 22:38:44 +0000516 md->queue.issue_fn = mmc_blk_issue_rq;
517 md->queue.data = md;
518
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200519 md->disk->major = MMC_BLOCK_MAJOR;
Russell Kinga6f6c962006-01-03 22:38:44 +0000520 md->disk->first_minor = devidx << MMC_SHIFT;
521 md->disk->fops = &mmc_bdops;
522 md->disk->private_data = md;
523 md->disk->queue = md->queue.queue;
524 md->disk->driverfs_dev = &card->dev;
525
526 /*
527 * As discussed on lkml, GENHD_FL_REMOVABLE should:
528 *
529 * - be set for removable media with permanent block devices
530 * - be unset for removable block devices with permanent media
531 *
532 * Since MMC block devices clearly fall under the second
533 * case, we do not set GENHD_FL_REMOVABLE. Userspace
534 * should use the block device creation/destruction hotplug
535 * messages to tell when the card is present.
536 */
537
538 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000539
Martin K. Petersene1defc42009-05-22 17:17:49 -0400540 blk_queue_logical_block_size(md->queue.queue, 512);
Russell Kinga6f6c962006-01-03 22:38:44 +0000541
Pierre Ossman85a18ad2007-02-17 22:15:27 +0100542 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
543 /*
544 * The EXT_CSD sector count is in number or 512 byte
545 * sectors.
546 */
547 set_capacity(md->disk, card->ext_csd.sectors);
548 } else {
549 /*
550 * The CSD capacity field is in units of read_blkbits.
551 * set_capacity takes units of 512 bytes.
552 */
553 set_capacity(md->disk,
554 card->csd.capacity << (card->csd.read_blkbits - 9));
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000557
558 err_putdisk:
559 put_disk(md->disk);
560 err_kfree:
561 kfree(md);
562 out:
563 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
566static int
567mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
568{
569 struct mmc_command cmd;
570 int err;
571
Philip Langdalefba68bd2007-01-04 06:57:32 -0800572 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
573 if (mmc_card_blockaddr(card))
574 return 0;
575
Pierre Ossmanb8558852007-01-03 19:47:29 +0100576 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 cmd.opcode = MMC_SET_BLOCKLEN;
Pierre Ossman08846692008-08-31 14:10:08 +0200578 cmd.arg = 512;
David Brownell7213d172007-08-08 09:10:23 -0700579 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 err = mmc_wait_for_cmd(card->host, &cmd, 5);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100581 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
583 if (err) {
584 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
585 md->disk->disk_name, cmd.arg, err);
586 return -EINVAL;
587 }
588
589 return 0;
590}
591
592static int mmc_blk_probe(struct mmc_card *card)
593{
594 struct mmc_blk_data *md;
595 int err;
596
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200597 char cap_str[10];
598
Pierre Ossman912490d2005-05-21 10:27:02 +0100599 /*
600 * Check that the card supports the command class(es) we need.
601 */
602 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 return -ENODEV;
604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 md = mmc_blk_alloc(card);
606 if (IS_ERR(md))
607 return PTR_ERR(md);
608
609 err = mmc_blk_set_blksize(md, card);
610 if (err)
611 goto out;
612
Yi Li444122f2009-02-05 15:31:57 +0800613 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200614 cap_str, sizeof(cap_str));
615 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200617 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 mmc_set_drvdata(card, md);
620 add_disk(md->disk);
621 return 0;
622
623 out:
Jarkko Lavinen0a74ff22010-01-08 14:42:59 -0800624 mmc_cleanup_queue(&md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 mmc_blk_put(md);
626
627 return err;
628}
629
630static void mmc_blk_remove(struct mmc_card *card)
631{
632 struct mmc_blk_data *md = mmc_get_drvdata(card);
633
634 if (md) {
Pierre Ossman89b4e132006-11-14 22:08:16 +0100635 /* Stop new requests from getting into the queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 del_gendisk(md->disk);
637
Pierre Ossman89b4e132006-11-14 22:08:16 +0100638 /* Then flush out any already in there */
639 mmc_cleanup_queue(&md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 mmc_blk_put(md);
642 }
643 mmc_set_drvdata(card, NULL);
644}
645
646#ifdef CONFIG_PM
647static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
648{
649 struct mmc_blk_data *md = mmc_get_drvdata(card);
650
651 if (md) {
652 mmc_queue_suspend(&md->queue);
653 }
654 return 0;
655}
656
657static int mmc_blk_resume(struct mmc_card *card)
658{
659 struct mmc_blk_data *md = mmc_get_drvdata(card);
660
661 if (md) {
662 mmc_blk_set_blksize(md, card);
663 mmc_queue_resume(&md->queue);
664 }
665 return 0;
666}
667#else
668#define mmc_blk_suspend NULL
669#define mmc_blk_resume NULL
670#endif
671
672static struct mmc_driver mmc_driver = {
673 .drv = {
674 .name = "mmcblk",
675 },
676 .probe = mmc_blk_probe,
677 .remove = mmc_blk_remove,
678 .suspend = mmc_blk_suspend,
679 .resume = mmc_blk_resume,
680};
681
682static int __init mmc_blk_init(void)
683{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900684 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200686 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
687 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900690 res = mmc_register_driver(&mmc_driver);
691 if (res)
692 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900694 return 0;
695 out2:
696 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 out:
698 return res;
699}
700
701static void __exit mmc_blk_exit(void)
702{
703 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200704 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
707module_init(mmc_blk_init);
708module_exit(mmc_blk_exit);
709
710MODULE_LICENSE("GPL");
711MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
712