[NETFILTER]: nf_conntrack: export hash allocation/destruction functions
[linux-2.6/verdex.git] / drivers / mmc / card / block.c
blobcbd4b6e3e17c6a30cb91fc24aff63fe12dd14384
1 /*
2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2007 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/errno.h>
27 #include <linux/hdreg.h>
28 #include <linux/kdev_t.h>
29 #include <linux/blkdev.h>
30 #include <linux/mutex.h>
31 #include <linux/scatterlist.h>
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/mmc.h>
36 #include <linux/mmc/sd.h>
38 #include <asm/system.h>
39 #include <asm/uaccess.h>
41 #include "queue.h"
44 * max 8 partitions per card
46 #define MMC_SHIFT 3
49 * There is one mmc_blk_data per slot.
51 struct mmc_blk_data {
52 spinlock_t lock;
53 struct gendisk *disk;
54 struct mmc_queue queue;
56 unsigned int usage;
57 unsigned int block_bits;
58 unsigned int read_only;
61 static DEFINE_MUTEX(open_lock);
63 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
65 struct mmc_blk_data *md;
67 mutex_lock(&open_lock);
68 md = disk->private_data;
69 if (md && md->usage == 0)
70 md = NULL;
71 if (md)
72 md->usage++;
73 mutex_unlock(&open_lock);
75 return md;
78 static void mmc_blk_put(struct mmc_blk_data *md)
80 mutex_lock(&open_lock);
81 md->usage--;
82 if (md->usage == 0) {
83 put_disk(md->disk);
84 kfree(md);
86 mutex_unlock(&open_lock);
89 static int mmc_blk_open(struct inode *inode, struct file *filp)
91 struct mmc_blk_data *md;
92 int ret = -ENXIO;
94 md = mmc_blk_get(inode->i_bdev->bd_disk);
95 if (md) {
96 if (md->usage == 2)
97 check_disk_change(inode->i_bdev);
98 ret = 0;
100 if ((filp->f_mode & FMODE_WRITE) && md->read_only)
101 ret = -EROFS;
104 return ret;
107 static int mmc_blk_release(struct inode *inode, struct file *filp)
109 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
111 mmc_blk_put(md);
112 return 0;
115 static int
116 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
118 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
119 geo->heads = 4;
120 geo->sectors = 16;
121 return 0;
124 static struct block_device_operations mmc_bdops = {
125 .open = mmc_blk_open,
126 .release = mmc_blk_release,
127 .getgeo = mmc_blk_getgeo,
128 .owner = THIS_MODULE,
131 struct mmc_blk_request {
132 struct mmc_request mrq;
133 struct mmc_command cmd;
134 struct mmc_command stop;
135 struct mmc_data data;
138 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
140 int err;
141 u32 blocks;
143 struct mmc_request mrq;
144 struct mmc_command cmd;
145 struct mmc_data data;
146 unsigned int timeout_us;
148 struct scatterlist sg;
150 memset(&cmd, 0, sizeof(struct mmc_command));
152 cmd.opcode = MMC_APP_CMD;
153 cmd.arg = card->rca << 16;
154 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
156 err = mmc_wait_for_cmd(card->host, &cmd, 0);
157 if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
158 return (u32)-1;
160 memset(&cmd, 0, sizeof(struct mmc_command));
162 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
163 cmd.arg = 0;
164 cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
166 memset(&data, 0, sizeof(struct mmc_data));
168 data.timeout_ns = card->csd.tacc_ns * 100;
169 data.timeout_clks = card->csd.tacc_clks * 100;
171 timeout_us = data.timeout_ns / 1000;
172 timeout_us += data.timeout_clks * 1000 /
173 (card->host->ios.clock / 1000);
175 if (timeout_us > 100000) {
176 data.timeout_ns = 100000000;
177 data.timeout_clks = 0;
180 data.blksz = 4;
181 data.blocks = 1;
182 data.flags = MMC_DATA_READ;
183 data.sg = &sg;
184 data.sg_len = 1;
186 memset(&mrq, 0, sizeof(struct mmc_request));
188 mrq.cmd = &cmd;
189 mrq.data = &data;
191 sg_init_one(&sg, &blocks, 4);
193 mmc_wait_for_req(card->host, &mrq);
195 if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
196 return (u32)-1;
198 blocks = ntohl(blocks);
200 return blocks;
203 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
205 struct mmc_blk_data *md = mq->data;
206 struct mmc_card *card = md->queue.card;
207 struct mmc_blk_request brq;
208 int ret = 1, sg_pos, data_size;
210 mmc_claim_host(card->host);
212 do {
213 struct mmc_command cmd;
214 u32 readcmd, writecmd;
216 memset(&brq, 0, sizeof(struct mmc_blk_request));
217 brq.mrq.cmd = &brq.cmd;
218 brq.mrq.data = &brq.data;
220 brq.cmd.arg = req->sector;
221 if (!mmc_card_blockaddr(card))
222 brq.cmd.arg <<= 9;
223 brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
224 brq.data.blksz = 1 << md->block_bits;
225 brq.stop.opcode = MMC_STOP_TRANSMISSION;
226 brq.stop.arg = 0;
227 brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
228 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
229 if (brq.data.blocks > card->host->max_blk_count)
230 brq.data.blocks = card->host->max_blk_count;
232 mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
235 * If the host doesn't support multiple block writes, force
236 * block writes to single block. SD cards are excepted from
237 * this rule as they support querying the number of
238 * successfully written sectors.
240 if (rq_data_dir(req) != READ &&
241 !(card->host->caps & MMC_CAP_MULTIWRITE) &&
242 !mmc_card_sd(card))
243 brq.data.blocks = 1;
245 if (brq.data.blocks > 1) {
246 brq.data.flags |= MMC_DATA_MULTI;
247 brq.mrq.stop = &brq.stop;
248 readcmd = MMC_READ_MULTIPLE_BLOCK;
249 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
250 } else {
251 brq.mrq.stop = NULL;
252 readcmd = MMC_READ_SINGLE_BLOCK;
253 writecmd = MMC_WRITE_BLOCK;
256 if (rq_data_dir(req) == READ) {
257 brq.cmd.opcode = readcmd;
258 brq.data.flags |= MMC_DATA_READ;
259 } else {
260 brq.cmd.opcode = writecmd;
261 brq.data.flags |= MMC_DATA_WRITE;
264 brq.data.sg = mq->sg;
265 brq.data.sg_len = mmc_queue_map_sg(mq);
267 mmc_queue_bounce_pre(mq);
269 if (brq.data.blocks !=
270 (req->nr_sectors >> (md->block_bits - 9))) {
271 data_size = brq.data.blocks * brq.data.blksz;
272 for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
273 data_size -= mq->sg[sg_pos].length;
274 if (data_size <= 0) {
275 mq->sg[sg_pos].length += data_size;
276 sg_pos++;
277 break;
280 brq.data.sg_len = sg_pos;
283 mmc_wait_for_req(card->host, &brq.mrq);
285 mmc_queue_bounce_post(mq);
287 if (brq.cmd.error) {
288 printk(KERN_ERR "%s: error %d sending read/write command\n",
289 req->rq_disk->disk_name, brq.cmd.error);
290 goto cmd_err;
293 if (brq.data.error) {
294 printk(KERN_ERR "%s: error %d transferring data\n",
295 req->rq_disk->disk_name, brq.data.error);
296 goto cmd_err;
299 if (brq.stop.error) {
300 printk(KERN_ERR "%s: error %d sending stop command\n",
301 req->rq_disk->disk_name, brq.stop.error);
302 goto cmd_err;
305 if (rq_data_dir(req) != READ) {
306 do {
307 int err;
309 cmd.opcode = MMC_SEND_STATUS;
310 cmd.arg = card->rca << 16;
311 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
312 err = mmc_wait_for_cmd(card->host, &cmd, 5);
313 if (err) {
314 printk(KERN_ERR "%s: error %d requesting status\n",
315 req->rq_disk->disk_name, err);
316 goto cmd_err;
318 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
320 #if 0
321 if (cmd.resp[0] & ~0x00000900)
322 printk(KERN_ERR "%s: status = %08x\n",
323 req->rq_disk->disk_name, cmd.resp[0]);
324 if (mmc_decode_status(cmd.resp))
325 goto cmd_err;
326 #endif
330 * A block was successfully transferred.
332 spin_lock_irq(&md->lock);
333 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
334 if (!ret) {
336 * The whole request completed successfully.
338 add_disk_randomness(req->rq_disk);
339 blkdev_dequeue_request(req);
340 end_that_request_last(req, 1);
342 spin_unlock_irq(&md->lock);
343 } while (ret);
345 mmc_release_host(card->host);
347 return 1;
349 cmd_err:
351 * If this is an SD card and we're writing, we can first
352 * mark the known good sectors as ok.
354 * If the card is not SD, we can still ok written sectors
355 * if the controller can do proper error reporting.
357 * For reads we just fail the entire chunk as that should
358 * be safe in all cases.
360 if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
361 u32 blocks;
362 unsigned int bytes;
364 blocks = mmc_sd_num_wr_blocks(card);
365 if (blocks != (u32)-1) {
366 if (card->csd.write_partial)
367 bytes = blocks << md->block_bits;
368 else
369 bytes = blocks << 9;
370 spin_lock_irq(&md->lock);
371 ret = end_that_request_chunk(req, 1, bytes);
372 spin_unlock_irq(&md->lock);
374 } else if (rq_data_dir(req) != READ &&
375 (card->host->caps & MMC_CAP_MULTIWRITE)) {
376 spin_lock_irq(&md->lock);
377 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
378 spin_unlock_irq(&md->lock);
381 mmc_release_host(card->host);
383 spin_lock_irq(&md->lock);
384 while (ret) {
385 ret = end_that_request_chunk(req, 0,
386 req->current_nr_sectors << 9);
389 add_disk_randomness(req->rq_disk);
390 blkdev_dequeue_request(req);
391 end_that_request_last(req, 0);
392 spin_unlock_irq(&md->lock);
394 return 0;
397 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
399 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
401 static inline int mmc_blk_readonly(struct mmc_card *card)
403 return mmc_card_readonly(card) ||
404 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
407 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
409 struct mmc_blk_data *md;
410 int devidx, ret;
412 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
413 if (devidx >= MMC_NUM_MINORS)
414 return ERR_PTR(-ENOSPC);
415 __set_bit(devidx, dev_use);
417 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
418 if (!md) {
419 ret = -ENOMEM;
420 goto out;
423 memset(md, 0, sizeof(struct mmc_blk_data));
426 * Set the read-only status based on the supported commands
427 * and the write protect switch.
429 md->read_only = mmc_blk_readonly(card);
432 * Both SD and MMC specifications state (although a bit
433 * unclearly in the MMC case) that a block size of 512
434 * bytes must always be supported by the card.
436 md->block_bits = 9;
438 md->disk = alloc_disk(1 << MMC_SHIFT);
439 if (md->disk == NULL) {
440 ret = -ENOMEM;
441 goto err_kfree;
444 spin_lock_init(&md->lock);
445 md->usage = 1;
447 ret = mmc_init_queue(&md->queue, card, &md->lock);
448 if (ret)
449 goto err_putdisk;
451 md->queue.issue_fn = mmc_blk_issue_rq;
452 md->queue.data = md;
454 md->disk->major = MMC_BLOCK_MAJOR;
455 md->disk->first_minor = devidx << MMC_SHIFT;
456 md->disk->fops = &mmc_bdops;
457 md->disk->private_data = md;
458 md->disk->queue = md->queue.queue;
459 md->disk->driverfs_dev = &card->dev;
462 * As discussed on lkml, GENHD_FL_REMOVABLE should:
464 * - be set for removable media with permanent block devices
465 * - be unset for removable block devices with permanent media
467 * Since MMC block devices clearly fall under the second
468 * case, we do not set GENHD_FL_REMOVABLE. Userspace
469 * should use the block device creation/destruction hotplug
470 * messages to tell when the card is present.
473 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
475 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
477 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
479 * The EXT_CSD sector count is in number or 512 byte
480 * sectors.
482 set_capacity(md->disk, card->ext_csd.sectors);
483 } else {
485 * The CSD capacity field is in units of read_blkbits.
486 * set_capacity takes units of 512 bytes.
488 set_capacity(md->disk,
489 card->csd.capacity << (card->csd.read_blkbits - 9));
491 return md;
493 err_putdisk:
494 put_disk(md->disk);
495 err_kfree:
496 kfree(md);
497 out:
498 return ERR_PTR(ret);
501 static int
502 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
504 struct mmc_command cmd;
505 int err;
507 /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
508 if (mmc_card_blockaddr(card))
509 return 0;
511 mmc_claim_host(card->host);
512 cmd.opcode = MMC_SET_BLOCKLEN;
513 cmd.arg = 1 << md->block_bits;
514 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
515 err = mmc_wait_for_cmd(card->host, &cmd, 5);
516 mmc_release_host(card->host);
518 if (err) {
519 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
520 md->disk->disk_name, cmd.arg, err);
521 return -EINVAL;
524 return 0;
527 static int mmc_blk_probe(struct mmc_card *card)
529 struct mmc_blk_data *md;
530 int err;
533 * Check that the card supports the command class(es) we need.
535 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
536 return -ENODEV;
538 md = mmc_blk_alloc(card);
539 if (IS_ERR(md))
540 return PTR_ERR(md);
542 err = mmc_blk_set_blksize(md, card);
543 if (err)
544 goto out;
546 printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
547 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
548 (unsigned long long)(get_capacity(md->disk) >> 1),
549 md->read_only ? "(ro)" : "");
551 mmc_set_drvdata(card, md);
552 add_disk(md->disk);
553 return 0;
555 out:
556 mmc_blk_put(md);
558 return err;
561 static void mmc_blk_remove(struct mmc_card *card)
563 struct mmc_blk_data *md = mmc_get_drvdata(card);
565 if (md) {
566 int devidx;
568 /* Stop new requests from getting into the queue */
569 del_gendisk(md->disk);
571 /* Then flush out any already in there */
572 mmc_cleanup_queue(&md->queue);
574 devidx = md->disk->first_minor >> MMC_SHIFT;
575 __clear_bit(devidx, dev_use);
577 mmc_blk_put(md);
579 mmc_set_drvdata(card, NULL);
582 #ifdef CONFIG_PM
583 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
585 struct mmc_blk_data *md = mmc_get_drvdata(card);
587 if (md) {
588 mmc_queue_suspend(&md->queue);
590 return 0;
593 static int mmc_blk_resume(struct mmc_card *card)
595 struct mmc_blk_data *md = mmc_get_drvdata(card);
597 if (md) {
598 mmc_blk_set_blksize(md, card);
599 mmc_queue_resume(&md->queue);
601 return 0;
603 #else
604 #define mmc_blk_suspend NULL
605 #define mmc_blk_resume NULL
606 #endif
608 static struct mmc_driver mmc_driver = {
609 .drv = {
610 .name = "mmcblk",
612 .probe = mmc_blk_probe,
613 .remove = mmc_blk_remove,
614 .suspend = mmc_blk_suspend,
615 .resume = mmc_blk_resume,
618 static int __init mmc_blk_init(void)
620 int res = -ENOMEM;
622 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
623 if (res)
624 goto out;
626 return mmc_register_driver(&mmc_driver);
628 out:
629 return res;
632 static void __exit mmc_blk_exit(void)
634 mmc_unregister_driver(&mmc_driver);
635 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
638 module_init(mmc_blk_init);
639 module_exit(mmc_blk_exit);
641 MODULE_LICENSE("GPL");
642 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");