sfc: Get port number from CS_PORT_NUM, not PCI function number
[zen-stable.git] / drivers / mmc / card / block.c
blobcb9fbc83b09095f8a0aa77572c0e9304641f300b
1 /*
2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2008 Pierre Ossman
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.
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.
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
17 * Author: Andrew Christian
18 * 28 May 2002
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/fs.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
35 #include <linux/mmc/card.h>
36 #include <linux/mmc/host.h>
37 #include <linux/mmc/mmc.h>
38 #include <linux/mmc/sd.h>
40 #include <asm/system.h>
41 #include <asm/uaccess.h>
43 #include "queue.h"
45 MODULE_ALIAS("mmc:block");
48 * max 8 partitions per card
50 #define MMC_SHIFT 3
51 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
53 static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
56 * There is one mmc_blk_data per slot.
58 struct mmc_blk_data {
59 spinlock_t lock;
60 struct gendisk *disk;
61 struct mmc_queue queue;
63 unsigned int usage;
64 unsigned int read_only;
67 static DEFINE_MUTEX(open_lock);
69 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
71 struct mmc_blk_data *md;
73 mutex_lock(&open_lock);
74 md = disk->private_data;
75 if (md && md->usage == 0)
76 md = NULL;
77 if (md)
78 md->usage++;
79 mutex_unlock(&open_lock);
81 return md;
84 static void mmc_blk_put(struct mmc_blk_data *md)
86 mutex_lock(&open_lock);
87 md->usage--;
88 if (md->usage == 0) {
89 int devmaj = MAJOR(disk_devt(md->disk));
90 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
92 if (!devmaj)
93 devidx = md->disk->first_minor >> MMC_SHIFT;
95 blk_cleanup_queue(md->queue.queue);
97 __clear_bit(devidx, dev_use);
99 put_disk(md->disk);
100 kfree(md);
102 mutex_unlock(&open_lock);
105 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
107 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
108 int ret = -ENXIO;
110 if (md) {
111 if (md->usage == 2)
112 check_disk_change(bdev);
113 ret = 0;
115 if ((mode & FMODE_WRITE) && md->read_only) {
116 mmc_blk_put(md);
117 ret = -EROFS;
121 return ret;
124 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
126 struct mmc_blk_data *md = disk->private_data;
128 mmc_blk_put(md);
129 return 0;
132 static int
133 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
135 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
136 geo->heads = 4;
137 geo->sectors = 16;
138 return 0;
141 static const struct block_device_operations mmc_bdops = {
142 .open = mmc_blk_open,
143 .release = mmc_blk_release,
144 .getgeo = mmc_blk_getgeo,
145 .owner = THIS_MODULE,
148 struct mmc_blk_request {
149 struct mmc_request mrq;
150 struct mmc_command cmd;
151 struct mmc_command stop;
152 struct mmc_data data;
155 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
157 int err;
158 u32 result;
159 __be32 *blocks;
161 struct mmc_request mrq;
162 struct mmc_command cmd;
163 struct mmc_data data;
164 unsigned int timeout_us;
166 struct scatterlist sg;
168 memset(&cmd, 0, sizeof(struct mmc_command));
170 cmd.opcode = MMC_APP_CMD;
171 cmd.arg = card->rca << 16;
172 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
174 err = mmc_wait_for_cmd(card->host, &cmd, 0);
175 if (err)
176 return (u32)-1;
177 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
178 return (u32)-1;
180 memset(&cmd, 0, sizeof(struct mmc_command));
182 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
183 cmd.arg = 0;
184 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
186 memset(&data, 0, sizeof(struct mmc_data));
188 data.timeout_ns = card->csd.tacc_ns * 100;
189 data.timeout_clks = card->csd.tacc_clks * 100;
191 timeout_us = data.timeout_ns / 1000;
192 timeout_us += data.timeout_clks * 1000 /
193 (card->host->ios.clock / 1000);
195 if (timeout_us > 100000) {
196 data.timeout_ns = 100000000;
197 data.timeout_clks = 0;
200 data.blksz = 4;
201 data.blocks = 1;
202 data.flags = MMC_DATA_READ;
203 data.sg = &sg;
204 data.sg_len = 1;
206 memset(&mrq, 0, sizeof(struct mmc_request));
208 mrq.cmd = &cmd;
209 mrq.data = &data;
211 blocks = kmalloc(4, GFP_KERNEL);
212 if (!blocks)
213 return (u32)-1;
215 sg_init_one(&sg, blocks, 4);
217 mmc_wait_for_req(card->host, &mrq);
219 result = ntohl(*blocks);
220 kfree(blocks);
222 if (cmd.error || data.error)
223 result = (u32)-1;
225 return result;
228 static u32 get_card_status(struct mmc_card *card, struct request *req)
230 struct mmc_command cmd;
231 int err;
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];
245 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
247 struct mmc_blk_data *md = mq->data;
248 struct mmc_card *card = md->queue.card;
249 struct mmc_blk_request brq;
250 int ret = 1, disable_multi = 0;
252 mmc_claim_host(card->host);
254 do {
255 struct mmc_command cmd;
256 u32 readcmd, writecmd, status = 0;
258 memset(&brq, 0, sizeof(struct mmc_blk_request));
259 brq.mrq.cmd = &brq.cmd;
260 brq.mrq.data = &brq.data;
262 brq.cmd.arg = blk_rq_pos(req);
263 if (!mmc_card_blockaddr(card))
264 brq.cmd.arg <<= 9;
265 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
266 brq.data.blksz = 512;
267 brq.stop.opcode = MMC_STOP_TRANSMISSION;
268 brq.stop.arg = 0;
269 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
270 brq.data.blocks = blk_rq_sectors(req);
273 * The block layer doesn't support all sector count
274 * restrictions, so we need to be prepared for too big
275 * requests.
277 if (brq.data.blocks > card->host->max_blk_count)
278 brq.data.blocks = card->host->max_blk_count;
281 * 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.
285 if (disable_multi && brq.data.blocks > 1)
286 brq.data.blocks = 1;
288 if (brq.data.blocks > 1) {
289 /* SPI multiblock writes terminate using a special
290 * token, not a STOP_TRANSMISSION request.
292 if (!mmc_host_is_spi(card->host)
293 || rq_data_dir(req) == READ)
294 brq.mrq.stop = &brq.stop;
295 readcmd = MMC_READ_MULTIPLE_BLOCK;
296 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
297 } else {
298 brq.mrq.stop = NULL;
299 readcmd = MMC_READ_SINGLE_BLOCK;
300 writecmd = MMC_WRITE_BLOCK;
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;
311 mmc_set_data_timeout(&brq.data, card);
313 brq.data.sg = mq->sg;
314 brq.data.sg_len = mmc_queue_map_sg(mq);
317 * Adjust the sg list so it is the same size as the
318 * request.
320 if (brq.data.blocks != blk_rq_sectors(req)) {
321 int i, data_size = brq.data.blocks << 9;
322 struct scatterlist *sg;
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;
332 brq.data.sg_len = i;
335 mmc_queue_bounce_pre(mq);
337 mmc_wait_for_req(card->host, &brq.mrq);
339 mmc_queue_bounce_post(mq);
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.
346 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;
354 status = get_card_status(card, req);
357 if (brq.cmd.error) {
358 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);
364 if (brq.data.error) {
365 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,
371 (unsigned)blk_rq_pos(req),
372 (unsigned)blk_rq_sectors(req), status);
375 if (brq.stop.error) {
376 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);
382 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
383 do {
384 int err;
386 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;
396 * Some cards mishandle the status bits,
397 * so make sure to check both the busy
398 * indication and the card state.
400 } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
401 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
403 #if 0
404 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;
409 #endif
412 if (brq.cmd.error || brq.stop.error || brq.data.error) {
413 if (rq_data_dir(req) == READ) {
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.
419 spin_lock_irq(&md->lock);
420 ret = __blk_end_request(req, -EIO, brq.data.blksz);
421 spin_unlock_irq(&md->lock);
422 continue;
424 goto cmd_err;
428 * A block was successfully transferred.
430 spin_lock_irq(&md->lock);
431 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
432 spin_unlock_irq(&md->lock);
433 } while (ret);
435 mmc_release_host(card->host);
437 return 1;
439 cmd_err:
441 * If this is an SD card and we're writing, we can first
442 * mark the known good sectors as ok.
444 * If the card is not SD, we can still ok written sectors
445 * as reported by the controller (which might be less than
446 * the real number of written sectors, but never more).
448 if (mmc_card_sd(card)) {
449 u32 blocks;
451 blocks = mmc_sd_num_wr_blocks(card);
452 if (blocks != (u32)-1) {
453 spin_lock_irq(&md->lock);
454 ret = __blk_end_request(req, 0, blocks << 9);
455 spin_unlock_irq(&md->lock);
457 } else {
458 spin_lock_irq(&md->lock);
459 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
460 spin_unlock_irq(&md->lock);
463 mmc_release_host(card->host);
465 spin_lock_irq(&md->lock);
466 while (ret)
467 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
468 spin_unlock_irq(&md->lock);
470 return 0;
474 static inline int mmc_blk_readonly(struct mmc_card *card)
476 return mmc_card_readonly(card) ||
477 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
480 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
482 struct mmc_blk_data *md;
483 int devidx, ret;
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);
490 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
491 if (!md) {
492 ret = -ENOMEM;
493 goto out;
498 * Set the read-only status based on the supported commands
499 * and the write protect switch.
501 md->read_only = mmc_blk_readonly(card);
503 md->disk = alloc_disk(1 << MMC_SHIFT);
504 if (md->disk == NULL) {
505 ret = -ENOMEM;
506 goto err_kfree;
509 spin_lock_init(&md->lock);
510 md->usage = 1;
512 ret = mmc_init_queue(&md->queue, card, &md->lock);
513 if (ret)
514 goto err_putdisk;
516 md->queue.issue_fn = mmc_blk_issue_rq;
517 md->queue.data = md;
519 md->disk->major = MMC_BLOCK_MAJOR;
520 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;
527 * As discussed on lkml, GENHD_FL_REMOVABLE should:
529 * - be set for removable media with permanent block devices
530 * - be unset for removable block devices with permanent media
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.
538 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
540 blk_queue_logical_block_size(md->queue.queue, 512);
542 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
544 * The EXT_CSD sector count is in number or 512 byte
545 * sectors.
547 set_capacity(md->disk, card->ext_csd.sectors);
548 } else {
550 * The CSD capacity field is in units of read_blkbits.
551 * set_capacity takes units of 512 bytes.
553 set_capacity(md->disk,
554 card->csd.capacity << (card->csd.read_blkbits - 9));
556 return md;
558 err_putdisk:
559 put_disk(md->disk);
560 err_kfree:
561 kfree(md);
562 out:
563 return ERR_PTR(ret);
566 static int
567 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
569 struct mmc_command cmd;
570 int err;
572 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
573 if (mmc_card_blockaddr(card))
574 return 0;
576 mmc_claim_host(card->host);
577 cmd.opcode = MMC_SET_BLOCKLEN;
578 cmd.arg = 512;
579 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
580 err = mmc_wait_for_cmd(card->host, &cmd, 5);
581 mmc_release_host(card->host);
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;
589 return 0;
592 static int mmc_blk_probe(struct mmc_card *card)
594 struct mmc_blk_data *md;
595 int err;
597 char cap_str[10];
600 * Check that the card supports the command class(es) we need.
602 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
603 return -ENODEV;
605 md = mmc_blk_alloc(card);
606 if (IS_ERR(md))
607 return PTR_ERR(md);
609 err = mmc_blk_set_blksize(md, card);
610 if (err)
611 goto out;
613 string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
614 cap_str, sizeof(cap_str));
615 printk(KERN_INFO "%s: %s %s %s %s\n",
616 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
617 cap_str, md->read_only ? "(ro)" : "");
619 mmc_set_drvdata(card, md);
620 add_disk(md->disk);
621 return 0;
623 out:
624 mmc_cleanup_queue(&md->queue);
625 mmc_blk_put(md);
627 return err;
630 static void mmc_blk_remove(struct mmc_card *card)
632 struct mmc_blk_data *md = mmc_get_drvdata(card);
634 if (md) {
635 /* Stop new requests from getting into the queue */
636 del_gendisk(md->disk);
638 /* Then flush out any already in there */
639 mmc_cleanup_queue(&md->queue);
641 mmc_blk_put(md);
643 mmc_set_drvdata(card, NULL);
646 #ifdef CONFIG_PM
647 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
649 struct mmc_blk_data *md = mmc_get_drvdata(card);
651 if (md) {
652 mmc_queue_suspend(&md->queue);
654 return 0;
657 static int mmc_blk_resume(struct mmc_card *card)
659 struct mmc_blk_data *md = mmc_get_drvdata(card);
661 if (md) {
662 mmc_blk_set_blksize(md, card);
663 mmc_queue_resume(&md->queue);
665 return 0;
667 #else
668 #define mmc_blk_suspend NULL
669 #define mmc_blk_resume NULL
670 #endif
672 static struct mmc_driver mmc_driver = {
673 .drv = {
674 .name = "mmcblk",
676 .probe = mmc_blk_probe,
677 .remove = mmc_blk_remove,
678 .suspend = mmc_blk_suspend,
679 .resume = mmc_blk_resume,
682 static int __init mmc_blk_init(void)
684 int res;
686 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
687 if (res)
688 goto out;
690 res = mmc_register_driver(&mmc_driver);
691 if (res)
692 goto out2;
694 return 0;
695 out2:
696 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
697 out:
698 return res;
701 static void __exit mmc_blk_exit(void)
703 mmc_unregister_driver(&mmc_driver);
704 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
707 module_init(mmc_blk_init);
708 module_exit(mmc_blk_exit);
710 MODULE_LICENSE("GPL");
711 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");