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