blob: 98ffc41eaf2c631bebdfed37a9847b17f28b97cd [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>
26#include <linux/errno.h>
27#include <linux/hdreg.h>
28#include <linux/kdev_t.h>
29#include <linux/blkdev.h>
Arjan van de Vena621aae2006-01-12 18:43:35 +000030#include <linux/mutex.h>
Pierre Ossmanec5a19d2006-10-06 00:44:03 -070031#include <linux/scatterlist.h>
Pierre Ossmana7bbb572008-09-06 10:57:57 +020032#include <linux/string_helpers.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34#include <linux/mmc/card.h>
Pierre Ossman385e32272006-06-18 14:34:37 +020035#include <linux/mmc/host.h>
Pierre Ossmanda7fbe52006-12-24 22:46:55 +010036#include <linux/mmc/mmc.h>
37#include <linux/mmc/sd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/system.h>
40#include <asm/uaccess.h>
41
Pierre Ossman98ac2162006-12-23 20:03:02 +010042#include "queue.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Andy Whitcroft6b0b6282009-02-23 12:38:41 +000044MODULE_ALIAS("mmc:block");
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046/*
47 * max 8 partitions per card
48 */
49#define MMC_SHIFT 3
David Woodhouse1dff3142007-11-21 18:45:12 +010050#define MMC_NUM_MINORS (256 >> MMC_SHIFT)
51
Ben Collins203c8012008-06-05 19:10:21 -040052static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Linus Torvalds1da177e2005-04-16 15:20:36 -070054/*
55 * There is one mmc_blk_data per slot.
56 */
57struct mmc_blk_data {
58 spinlock_t lock;
59 struct gendisk *disk;
60 struct mmc_queue queue;
61
62 unsigned int usage;
Russell Kinga6f6c962006-01-03 22:38:44 +000063 unsigned int read_only;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064};
65
Arjan van de Vena621aae2006-01-12 18:43:35 +000066static DEFINE_MUTEX(open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
69{
70 struct mmc_blk_data *md;
71
Arjan van de Vena621aae2006-01-12 18:43:35 +000072 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 md = disk->private_data;
74 if (md && md->usage == 0)
75 md = NULL;
76 if (md)
77 md->usage++;
Arjan van de Vena621aae2006-01-12 18:43:35 +000078 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 return md;
81}
82
83static void mmc_blk_put(struct mmc_blk_data *md)
84{
Arjan van de Vena621aae2006-01-12 18:43:35 +000085 mutex_lock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 md->usage--;
87 if (md->usage == 0) {
Tejun Heof331c022008-09-03 09:01:48 +020088 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
David Woodhouse1dff3142007-11-21 18:45:12 +010089 __clear_bit(devidx, dev_use);
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 put_disk(md->disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 kfree(md);
93 }
Arjan van de Vena621aae2006-01-12 18:43:35 +000094 mutex_unlock(&open_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
96
Al Viroa5a15612008-03-02 10:33:30 -050097static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Al Viroa5a15612008-03-02 10:33:30 -050099 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 int ret = -ENXIO;
101
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (md) {
103 if (md->usage == 2)
Al Viroa5a15612008-03-02 10:33:30 -0500104 check_disk_change(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 ret = 0;
Pierre Ossmana00fc092005-09-06 15:18:52 -0700106
Al Viroa5a15612008-03-02 10:33:30 -0500107 if ((mode & FMODE_WRITE) && md->read_only) {
Andrew Morton70bb0892008-09-05 14:00:24 -0700108 mmc_blk_put(md);
Pierre Ossmana00fc092005-09-06 15:18:52 -0700109 ret = -EROFS;
Andrew Morton70bb0892008-09-05 14:00:24 -0700110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 }
112
113 return ret;
114}
115
Al Viroa5a15612008-03-02 10:33:30 -0500116static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
Al Viroa5a15612008-03-02 10:33:30 -0500118 struct mmc_blk_data *md = disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 mmc_blk_put(md);
121 return 0;
122}
123
124static int
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800125mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800127 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
128 geo->heads = 4;
129 geo->sectors = 16;
130 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static struct block_device_operations mmc_bdops = {
Al Viroa5a15612008-03-02 10:33:30 -0500134 .open = mmc_blk_open,
135 .release = mmc_blk_release,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800136 .getgeo = mmc_blk_getgeo,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .owner = THIS_MODULE,
138};
139
140struct mmc_blk_request {
141 struct mmc_request mrq;
142 struct mmc_command cmd;
143 struct mmc_command stop;
144 struct mmc_data data;
145};
146
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700147static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
148{
149 int err;
Harvey Harrisonb7a032102008-10-22 17:09:00 -0700150 __be32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700151
152 struct mmc_request mrq;
153 struct mmc_command cmd;
154 struct mmc_data data;
155 unsigned int timeout_us;
156
157 struct scatterlist sg;
158
159 memset(&cmd, 0, sizeof(struct mmc_command));
160
161 cmd.opcode = MMC_APP_CMD;
162 cmd.arg = card->rca << 16;
David Brownell7213d172007-08-08 09:10:23 -0700163 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700164
165 err = mmc_wait_for_cmd(card->host, &cmd, 0);
David Brownell7213d172007-08-08 09:10:23 -0700166 if (err)
167 return (u32)-1;
168 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700169 return (u32)-1;
170
171 memset(&cmd, 0, sizeof(struct mmc_command));
172
173 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
174 cmd.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700175 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700176
177 memset(&data, 0, sizeof(struct mmc_data));
178
179 data.timeout_ns = card->csd.tacc_ns * 100;
180 data.timeout_clks = card->csd.tacc_clks * 100;
181
182 timeout_us = data.timeout_ns / 1000;
183 timeout_us += data.timeout_clks * 1000 /
184 (card->host->ios.clock / 1000);
185
186 if (timeout_us > 100000) {
187 data.timeout_ns = 100000000;
188 data.timeout_clks = 0;
189 }
190
191 data.blksz = 4;
192 data.blocks = 1;
193 data.flags = MMC_DATA_READ;
194 data.sg = &sg;
195 data.sg_len = 1;
196
197 memset(&mrq, 0, sizeof(struct mmc_request));
198
199 mrq.cmd = &cmd;
200 mrq.data = &data;
201
202 sg_init_one(&sg, &blocks, 4);
203
204 mmc_wait_for_req(card->host, &mrq);
205
Pierre Ossman17b04292007-07-22 22:18:46 +0200206 if (cmd.error || data.error)
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700207 return (u32)-1;
208
Harvey Harrisonb7a032102008-10-22 17:09:00 -0700209 return ntohl(blocks);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700210}
211
Adrian Hunter504f1912008-10-16 12:55:25 +0300212static u32 get_card_status(struct mmc_card *card, struct request *req)
213{
214 struct mmc_command cmd;
215 int err;
216
217 memset(&cmd, 0, sizeof(struct mmc_command));
218 cmd.opcode = MMC_SEND_STATUS;
219 if (!mmc_host_is_spi(card->host))
220 cmd.arg = card->rca << 16;
221 cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
222 err = mmc_wait_for_cmd(card->host, &cmd, 0);
223 if (err)
224 printk(KERN_ERR "%s: error %d sending status comand",
225 req->rq_disk->disk_name, err);
226 return cmd.resp[0];
227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
230{
231 struct mmc_blk_data *md = mq->data;
232 struct mmc_card *card = md->queue.card;
Pierre Ossman176f00f2006-10-04 02:15:41 -0700233 struct mmc_blk_request brq;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100234 int ret = 1, disable_multi = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
Pierre Ossmanb8558852007-01-03 19:47:29 +0100236 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 struct mmc_command cmd;
Adrian Hunter504f1912008-10-16 12:55:25 +0300240 u32 readcmd, writecmd, status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 memset(&brq, 0, sizeof(struct mmc_blk_request));
243 brq.mrq.cmd = &brq.cmd;
244 brq.mrq.data = &brq.data;
245
Tejun Heo83096eb2009-05-07 22:24:39 +0900246 brq.cmd.arg = blk_rq_pos(req);
Philip Langdalefba68bd2007-01-04 06:57:32 -0800247 if (!mmc_card_blockaddr(card))
248 brq.cmd.arg <<= 9;
David Brownell7213d172007-08-08 09:10:23 -0700249 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
Pierre Ossman08846692008-08-31 14:10:08 +0200250 brq.data.blksz = 512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 brq.stop.opcode = MMC_STOP_TRANSMISSION;
252 brq.stop.arg = 0;
David Brownell7213d172007-08-08 09:10:23 -0700253 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
Tejun Heo83096eb2009-05-07 22:24:39 +0900254 brq.data.blocks = blk_rq_sectors(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Adrian Hunter6a79e392008-12-31 18:21:17 +0100256 /*
Pierre Ossman548d2de2009-04-10 17:52:57 +0200257 * The block layer doesn't support all sector count
258 * restrictions, so we need to be prepared for too big
259 * requests.
260 */
261 if (brq.data.blocks > card->host->max_blk_count)
262 brq.data.blocks = card->host->max_blk_count;
263
264 /*
Adrian Hunter6a79e392008-12-31 18:21:17 +0100265 * After a read error, we redo the request one sector at a time
266 * in order to accurately determine which sectors can be read
267 * successfully.
268 */
269 if (disable_multi && brq.data.blocks > 1)
270 brq.data.blocks = 1;
271
Russell King788ee7b2006-01-09 21:12:17 +0000272 if (brq.data.blocks > 1) {
David Brownell7213d172007-08-08 09:10:23 -0700273 /* SPI multiblock writes terminate using a special
274 * token, not a STOP_TRANSMISSION request.
275 */
276 if (!mmc_host_is_spi(card->host)
277 || rq_data_dir(req) == READ)
278 brq.mrq.stop = &brq.stop;
Russell Kingdb53f282006-08-30 15:14:56 +0100279 readcmd = MMC_READ_MULTIPLE_BLOCK;
280 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
Russell King788ee7b2006-01-09 21:12:17 +0000281 } else {
282 brq.mrq.stop = NULL;
Russell Kingdb53f282006-08-30 15:14:56 +0100283 readcmd = MMC_READ_SINGLE_BLOCK;
284 writecmd = MMC_WRITE_BLOCK;
285 }
286
287 if (rq_data_dir(req) == READ) {
288 brq.cmd.opcode = readcmd;
289 brq.data.flags |= MMC_DATA_READ;
290 } else {
291 brq.cmd.opcode = writecmd;
292 brq.data.flags |= MMC_DATA_WRITE;
Russell King788ee7b2006-01-09 21:12:17 +0000293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Pierre Ossmanb146d262007-07-24 19:16:54 +0200295 mmc_set_data_timeout(&brq.data, card);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 brq.data.sg = mq->sg;
Pierre Ossman98ccf142007-05-12 00:26:16 +0200298 brq.data.sg_len = mmc_queue_map_sg(mq);
299
Adrian Hunter6a79e392008-12-31 18:21:17 +0100300 /*
301 * Adjust the sg list so it is the same size as the
302 * request.
303 */
Tejun Heo83096eb2009-05-07 22:24:39 +0900304 if (brq.data.blocks != blk_rq_sectors(req)) {
Adrian Hunter6a79e392008-12-31 18:21:17 +0100305 int i, data_size = brq.data.blocks << 9;
306 struct scatterlist *sg;
307
308 for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
309 data_size -= sg->length;
310 if (data_size <= 0) {
311 sg->length += data_size;
312 i++;
313 break;
314 }
315 }
316 brq.data.sg_len = i;
317 }
318
Pierre Ossman98ccf142007-05-12 00:26:16 +0200319 mmc_queue_bounce_pre(mq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 mmc_wait_for_req(card->host, &brq.mrq);
Pierre Ossman98ccf142007-05-12 00:26:16 +0200322
323 mmc_queue_bounce_post(mq);
324
Pierre Ossman979ce722008-06-29 12:19:47 +0200325 /*
326 * Check for errors here, but don't jump to cmd_err
327 * until later as we need to wait for the card to leave
328 * programming mode even when things go wrong.
329 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100330 if (brq.cmd.error || brq.data.error || brq.stop.error) {
331 if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
332 /* Redo read one sector at a time */
333 printk(KERN_WARNING "%s: retrying using single "
334 "block read\n", req->rq_disk->disk_name);
335 disable_multi = 1;
336 continue;
337 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300338 status = get_card_status(card, req);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100339 }
Adrian Hunter504f1912008-10-16 12:55:25 +0300340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (brq.cmd.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300342 printk(KERN_ERR "%s: error %d sending read/write "
343 "command, response %#x, card status %#x\n",
344 req->rq_disk->disk_name, brq.cmd.error,
345 brq.cmd.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 }
347
348 if (brq.data.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300349 if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
350 /* 'Stop' response contains card status */
351 status = brq.mrq.stop->resp[0];
352 printk(KERN_ERR "%s: error %d transferring data,"
353 " sector %u, nr %u, card status %#x\n",
354 req->rq_disk->disk_name, brq.data.error,
Tejun Heo83096eb2009-05-07 22:24:39 +0900355 (unsigned)blk_rq_pos(req),
356 (unsigned)blk_rq_sectors(req), status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
358
359 if (brq.stop.error) {
Adrian Hunter504f1912008-10-16 12:55:25 +0300360 printk(KERN_ERR "%s: error %d sending stop command, "
361 "response %#x, card status %#x\n",
362 req->rq_disk->disk_name, brq.stop.error,
363 brq.stop.resp[0], status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
David Brownell7213d172007-08-08 09:10:23 -0700366 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
Russell King2ed6d222006-09-24 10:46:43 +0100367 do {
368 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Russell King2ed6d222006-09-24 10:46:43 +0100370 cmd.opcode = MMC_SEND_STATUS;
371 cmd.arg = card->rca << 16;
372 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
373 err = mmc_wait_for_cmd(card->host, &cmd, 5);
374 if (err) {
375 printk(KERN_ERR "%s: error %d requesting status\n",
376 req->rq_disk->disk_name, err);
377 goto cmd_err;
378 }
Pierre Ossmand198f102007-11-02 18:21:13 +0100379 /*
380 * Some cards mishandle the status bits,
381 * so make sure to check both the busy
382 * indication and the card state.
383 */
384 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
385 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387#if 0
Russell King2ed6d222006-09-24 10:46:43 +0100388 if (cmd.resp[0] & ~0x00000900)
389 printk(KERN_ERR "%s: status = %08x\n",
390 req->rq_disk->disk_name, cmd.resp[0]);
391 if (mmc_decode_status(cmd.resp))
392 goto cmd_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#endif
Russell King2ed6d222006-09-24 10:46:43 +0100394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Adrian Hunter6a79e392008-12-31 18:21:17 +0100396 if (brq.cmd.error || brq.stop.error || brq.data.error) {
397 if (rq_data_dir(req) == READ) {
398 /*
399 * After an error, we redo I/O one sector at a
400 * time, so we only reach here after trying to
401 * read a single sector.
402 */
403 spin_lock_irq(&md->lock);
404 ret = __blk_end_request(req, -EIO, brq.data.blksz);
405 spin_unlock_irq(&md->lock);
406 continue;
407 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200408 goto cmd_err;
Adrian Hunter6a79e392008-12-31 18:21:17 +0100409 }
Pierre Ossman979ce722008-06-29 12:19:47 +0200410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /*
412 * A block was successfully transferred.
413 */
414 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500415 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 spin_unlock_irq(&md->lock);
417 } while (ret);
418
Pierre Ossmanb8558852007-01-03 19:47:29 +0100419 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420
421 return 1;
422
423 cmd_err:
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700424 /*
425 * If this is an SD card and we're writing, we can first
426 * mark the known good sectors as ok.
427 *
428 * If the card is not SD, we can still ok written sectors
Pierre Ossman23af6032008-07-06 01:10:27 +0200429 * as reported by the controller (which might be less than
430 * the real number of written sectors, but never more).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 */
Adrian Hunter6a79e392008-12-31 18:21:17 +0100432 if (mmc_card_sd(card)) {
433 u32 blocks;
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700434
Adrian Hunter6a79e392008-12-31 18:21:17 +0100435 blocks = mmc_sd_num_wr_blocks(card);
436 if (blocks != (u32)-1) {
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700437 spin_lock_irq(&md->lock);
Adrian Hunter6a79e392008-12-31 18:21:17 +0100438 ret = __blk_end_request(req, 0, blocks << 9);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700439 spin_unlock_irq(&md->lock);
440 }
Adrian Hunter6a79e392008-12-31 18:21:17 +0100441 } else {
442 spin_lock_irq(&md->lock);
443 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
444 spin_unlock_irq(&md->lock);
Pierre Ossman176f00f2006-10-04 02:15:41 -0700445 }
446
Pierre Ossmanb8558852007-01-03 19:47:29 +0100447 mmc_release_host(card->host);
Pierre Ossmanec5a19d2006-10-06 00:44:03 -0700448
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 spin_lock_irq(&md->lock);
Kiyoshi Uedafd539832007-12-11 17:48:29 -0500450 while (ret)
451 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 spin_unlock_irq(&md->lock);
453
454 return 0;
455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Russell Kinga6f6c962006-01-03 22:38:44 +0000458static inline int mmc_blk_readonly(struct mmc_card *card)
459{
460 return mmc_card_readonly(card) ||
461 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
462}
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
465{
466 struct mmc_blk_data *md;
467 int devidx, ret;
468
469 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
470 if (devidx >= MMC_NUM_MINORS)
471 return ERR_PTR(-ENOSPC);
472 __set_bit(devidx, dev_use);
473
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700474 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
Russell Kinga6f6c962006-01-03 22:38:44 +0000475 if (!md) {
476 ret = -ENOMEM;
477 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 }
Russell Kinga6f6c962006-01-03 22:38:44 +0000479
Russell Kinga6f6c962006-01-03 22:38:44 +0000480
481 /*
482 * Set the read-only status based on the supported commands
483 * and the write protect switch.
484 */
485 md->read_only = mmc_blk_readonly(card);
486
Russell Kinga6f6c962006-01-03 22:38:44 +0000487 md->disk = alloc_disk(1 << MMC_SHIFT);
488 if (md->disk == NULL) {
489 ret = -ENOMEM;
490 goto err_kfree;
491 }
492
493 spin_lock_init(&md->lock);
494 md->usage = 1;
495
496 ret = mmc_init_queue(&md->queue, card, &md->lock);
497 if (ret)
498 goto err_putdisk;
499
Russell Kinga6f6c962006-01-03 22:38:44 +0000500 md->queue.issue_fn = mmc_blk_issue_rq;
501 md->queue.data = md;
502
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200503 md->disk->major = MMC_BLOCK_MAJOR;
Russell Kinga6f6c962006-01-03 22:38:44 +0000504 md->disk->first_minor = devidx << MMC_SHIFT;
505 md->disk->fops = &mmc_bdops;
506 md->disk->private_data = md;
507 md->disk->queue = md->queue.queue;
508 md->disk->driverfs_dev = &card->dev;
509
510 /*
511 * As discussed on lkml, GENHD_FL_REMOVABLE should:
512 *
513 * - be set for removable media with permanent block devices
514 * - be unset for removable block devices with permanent media
515 *
516 * Since MMC block devices clearly fall under the second
517 * case, we do not set GENHD_FL_REMOVABLE. Userspace
518 * should use the block device creation/destruction hotplug
519 * messages to tell when the card is present.
520 */
521
522 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
Russell Kinga6f6c962006-01-03 22:38:44 +0000523
Martin K. Petersene1defc42009-05-22 17:17:49 -0400524 blk_queue_logical_block_size(md->queue.queue, 512);
Russell Kinga6f6c962006-01-03 22:38:44 +0000525
Pierre Ossman85a18ad2007-02-17 22:15:27 +0100526 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
527 /*
528 * The EXT_CSD sector count is in number or 512 byte
529 * sectors.
530 */
531 set_capacity(md->disk, card->ext_csd.sectors);
532 } else {
533 /*
534 * The CSD capacity field is in units of read_blkbits.
535 * set_capacity takes units of 512 bytes.
536 */
537 set_capacity(md->disk,
538 card->csd.capacity << (card->csd.read_blkbits - 9));
539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return md;
Russell Kinga6f6c962006-01-03 22:38:44 +0000541
542 err_putdisk:
543 put_disk(md->disk);
544 err_kfree:
545 kfree(md);
546 out:
547 return ERR_PTR(ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
550static int
551mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
552{
553 struct mmc_command cmd;
554 int err;
555
Philip Langdalefba68bd2007-01-04 06:57:32 -0800556 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
557 if (mmc_card_blockaddr(card))
558 return 0;
559
Pierre Ossmanb8558852007-01-03 19:47:29 +0100560 mmc_claim_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 cmd.opcode = MMC_SET_BLOCKLEN;
Pierre Ossman08846692008-08-31 14:10:08 +0200562 cmd.arg = 512;
David Brownell7213d172007-08-08 09:10:23 -0700563 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 err = mmc_wait_for_cmd(card->host, &cmd, 5);
Pierre Ossmanb8558852007-01-03 19:47:29 +0100565 mmc_release_host(card->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 if (err) {
568 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
569 md->disk->disk_name, cmd.arg, err);
570 return -EINVAL;
571 }
572
573 return 0;
574}
575
576static int mmc_blk_probe(struct mmc_card *card)
577{
578 struct mmc_blk_data *md;
579 int err;
580
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200581 char cap_str[10];
582
Pierre Ossman912490d2005-05-21 10:27:02 +0100583 /*
584 * Check that the card supports the command class(es) we need.
585 */
586 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return -ENODEV;
588
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 md = mmc_blk_alloc(card);
590 if (IS_ERR(md))
591 return PTR_ERR(md);
592
593 err = mmc_blk_set_blksize(md, card);
594 if (err)
595 goto out;
596
Yi Li444122f2009-02-05 15:31:57 +0800597 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200598 cap_str, sizeof(cap_str));
599 printk(KERN_INFO "%s: %s %s %s %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
Pierre Ossmana7bbb572008-09-06 10:57:57 +0200601 cap_str, md->read_only ? "(ro)" : "");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 mmc_set_drvdata(card, md);
604 add_disk(md->disk);
605 return 0;
606
607 out:
608 mmc_blk_put(md);
609
610 return err;
611}
612
613static void mmc_blk_remove(struct mmc_card *card)
614{
615 struct mmc_blk_data *md = mmc_get_drvdata(card);
616
617 if (md) {
Pierre Ossman89b4e132006-11-14 22:08:16 +0100618 /* Stop new requests from getting into the queue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 del_gendisk(md->disk);
620
Pierre Ossman89b4e132006-11-14 22:08:16 +0100621 /* Then flush out any already in there */
622 mmc_cleanup_queue(&md->queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 mmc_blk_put(md);
625 }
626 mmc_set_drvdata(card, NULL);
627}
628
629#ifdef CONFIG_PM
630static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
631{
632 struct mmc_blk_data *md = mmc_get_drvdata(card);
633
634 if (md) {
635 mmc_queue_suspend(&md->queue);
636 }
637 return 0;
638}
639
640static int mmc_blk_resume(struct mmc_card *card)
641{
642 struct mmc_blk_data *md = mmc_get_drvdata(card);
643
644 if (md) {
645 mmc_blk_set_blksize(md, card);
646 mmc_queue_resume(&md->queue);
647 }
648 return 0;
649}
650#else
651#define mmc_blk_suspend NULL
652#define mmc_blk_resume NULL
653#endif
654
655static struct mmc_driver mmc_driver = {
656 .drv = {
657 .name = "mmcblk",
658 },
659 .probe = mmc_blk_probe,
660 .remove = mmc_blk_remove,
661 .suspend = mmc_blk_suspend,
662 .resume = mmc_blk_resume,
663};
664
665static int __init mmc_blk_init(void)
666{
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900667 int res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200669 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
670 if (res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900673 res = mmc_register_driver(&mmc_driver);
674 if (res)
675 goto out2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Akinobu Mita9d4e98e2008-09-13 19:02:07 +0900677 return 0;
678 out2:
679 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 out:
681 return res;
682}
683
684static void __exit mmc_blk_exit(void)
685{
686 mmc_unregister_driver(&mmc_driver);
Pierre Ossmanfe6b4c82007-05-14 17:27:29 +0200687 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690module_init(mmc_blk_init);
691module_exit(mmc_blk_exit);
692
693MODULE_LICENSE("GPL");
694MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
695