[PATCH] x86_64: CPU hotplug sibling map cleanup
[linux/fpc-iii.git] / drivers / mmc / mmc_block.c
blobd4eee99c2bf65ffe86798deb86cdd5ef0a2e81da
1 /*
2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
6 * Use consistent with the GNU GPL is permitted,
7 * provided that this copyright notice is
8 * preserved in its entirety in all copies and derived works.
10 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
11 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
12 * FITNESS FOR ANY PARTICULAR PURPOSE.
14 * Many thanks to Alessandro Rubini and Jonathan Corbet!
16 * Author: Andrew Christian
17 * 28 May 2002
19 #include <linux/moduleparam.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
23 #include <linux/sched.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/devfs_fs_kernel.h>
32 #include <linux/mmc/card.h>
33 #include <linux/mmc/protocol.h>
35 #include <asm/system.h>
36 #include <asm/uaccess.h>
38 #include "mmc_queue.h"
41 * max 8 partitions per card
43 #define MMC_SHIFT 3
45 static int major;
48 * There is one mmc_blk_data per slot.
50 struct mmc_blk_data {
51 spinlock_t lock;
52 struct gendisk *disk;
53 struct mmc_queue queue;
55 unsigned int usage;
56 unsigned int block_bits;
59 static DECLARE_MUTEX(open_lock);
61 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
63 struct mmc_blk_data *md;
65 down(&open_lock);
66 md = disk->private_data;
67 if (md && md->usage == 0)
68 md = NULL;
69 if (md)
70 md->usage++;
71 up(&open_lock);
73 return md;
76 static void mmc_blk_put(struct mmc_blk_data *md)
78 down(&open_lock);
79 md->usage--;
80 if (md->usage == 0) {
81 put_disk(md->disk);
82 mmc_cleanup_queue(&md->queue);
83 kfree(md);
85 up(&open_lock);
88 static int mmc_blk_open(struct inode *inode, struct file *filp)
90 struct mmc_blk_data *md;
91 int ret = -ENXIO;
93 md = mmc_blk_get(inode->i_bdev->bd_disk);
94 if (md) {
95 if (md->usage == 2)
96 check_disk_change(inode->i_bdev);
97 ret = 0;
100 return ret;
103 static int mmc_blk_release(struct inode *inode, struct file *filp)
105 struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
107 mmc_blk_put(md);
108 return 0;
111 static int
112 mmc_blk_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
114 struct block_device *bdev = inode->i_bdev;
116 if (cmd == HDIO_GETGEO) {
117 struct hd_geometry geo;
119 memset(&geo, 0, sizeof(struct hd_geometry));
121 geo.cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
122 geo.heads = 4;
123 geo.sectors = 16;
124 geo.start = get_start_sect(bdev);
126 return copy_to_user((void __user *)arg, &geo, sizeof(geo))
127 ? -EFAULT : 0;
130 return -ENOTTY;
133 static struct block_device_operations mmc_bdops = {
134 .open = mmc_blk_open,
135 .release = mmc_blk_release,
136 .ioctl = mmc_blk_ioctl,
137 .owner = THIS_MODULE,
140 struct mmc_blk_request {
141 struct mmc_request mrq;
142 struct mmc_command cmd;
143 struct mmc_command stop;
144 struct mmc_data data;
147 static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
149 struct mmc_blk_data *md = mq->data;
150 int stat = BLKPREP_OK;
153 * If we have no device, we haven't finished initialising.
155 if (!md || !mq->card) {
156 printk(KERN_ERR "%s: killing request - no device/host\n",
157 req->rq_disk->disk_name);
158 stat = BLKPREP_KILL;
161 return stat;
164 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
166 struct mmc_blk_data *md = mq->data;
167 struct mmc_card *card = md->queue.card;
168 int ret;
170 if (mmc_card_claim_host(card))
171 goto cmd_err;
173 do {
174 struct mmc_blk_request brq;
175 struct mmc_command cmd;
177 memset(&brq, 0, sizeof(struct mmc_blk_request));
178 brq.mrq.cmd = &brq.cmd;
179 brq.mrq.data = &brq.data;
181 brq.cmd.arg = req->sector << 9;
182 brq.cmd.flags = MMC_RSP_R1;
183 brq.data.timeout_ns = card->csd.tacc_ns * 10;
184 brq.data.timeout_clks = card->csd.tacc_clks * 10;
185 brq.data.blksz_bits = md->block_bits;
186 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
187 brq.stop.opcode = MMC_STOP_TRANSMISSION;
188 brq.stop.arg = 0;
189 brq.stop.flags = MMC_RSP_R1B;
191 if (rq_data_dir(req) == READ) {
192 brq.cmd.opcode = brq.data.blocks > 1 ? MMC_READ_MULTIPLE_BLOCK : MMC_READ_SINGLE_BLOCK;
193 brq.data.flags |= MMC_DATA_READ;
194 } else {
195 brq.cmd.opcode = MMC_WRITE_BLOCK;
196 brq.cmd.flags = MMC_RSP_R1B;
197 brq.data.flags |= MMC_DATA_WRITE;
198 brq.data.blocks = 1;
200 brq.mrq.stop = brq.data.blocks > 1 ? &brq.stop : NULL;
202 brq.data.sg = mq->sg;
203 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
205 mmc_wait_for_req(card->host, &brq.mrq);
206 if (brq.cmd.error) {
207 printk(KERN_ERR "%s: error %d sending read/write command\n",
208 req->rq_disk->disk_name, brq.cmd.error);
209 goto cmd_err;
212 if (brq.data.error) {
213 printk(KERN_ERR "%s: error %d transferring data\n",
214 req->rq_disk->disk_name, brq.data.error);
215 goto cmd_err;
218 if (brq.stop.error) {
219 printk(KERN_ERR "%s: error %d sending stop command\n",
220 req->rq_disk->disk_name, brq.stop.error);
221 goto cmd_err;
224 do {
225 int err;
227 cmd.opcode = MMC_SEND_STATUS;
228 cmd.arg = card->rca << 16;
229 cmd.flags = MMC_RSP_R1;
230 err = mmc_wait_for_cmd(card->host, &cmd, 5);
231 if (err) {
232 printk(KERN_ERR "%s: error %d requesting status\n",
233 req->rq_disk->disk_name, err);
234 goto cmd_err;
236 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
238 #if 0
239 if (cmd.resp[0] & ~0x00000900)
240 printk(KERN_ERR "%s: status = %08x\n",
241 req->rq_disk->disk_name, cmd.resp[0]);
242 if (mmc_decode_status(cmd.resp))
243 goto cmd_err;
244 #endif
247 * A block was successfully transferred.
249 spin_lock_irq(&md->lock);
250 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
251 if (!ret) {
253 * The whole request completed successfully.
255 add_disk_randomness(req->rq_disk);
256 blkdev_dequeue_request(req);
257 end_that_request_last(req);
259 spin_unlock_irq(&md->lock);
260 } while (ret);
262 mmc_card_release_host(card);
264 return 1;
266 cmd_err:
267 mmc_card_release_host(card);
270 * This is a little draconian, but until we get proper
271 * error handling sorted out here, its the best we can
272 * do - especially as some hosts have no idea how much
273 * data was transferred before the error occurred.
275 spin_lock_irq(&md->lock);
276 do {
277 ret = end_that_request_chunk(req, 0,
278 req->current_nr_sectors << 9);
279 } while (ret);
281 add_disk_randomness(req->rq_disk);
282 blkdev_dequeue_request(req);
283 end_that_request_last(req);
284 spin_unlock_irq(&md->lock);
286 return 0;
289 #define MMC_NUM_MINORS (256 >> MMC_SHIFT)
291 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
293 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
295 struct mmc_blk_data *md;
296 int devidx, ret;
298 devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
299 if (devidx >= MMC_NUM_MINORS)
300 return ERR_PTR(-ENOSPC);
301 __set_bit(devidx, dev_use);
303 md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
304 if (md) {
305 memset(md, 0, sizeof(struct mmc_blk_data));
307 md->disk = alloc_disk(1 << MMC_SHIFT);
308 if (md->disk == NULL) {
309 kfree(md);
310 md = ERR_PTR(-ENOMEM);
311 goto out;
314 spin_lock_init(&md->lock);
315 md->usage = 1;
317 ret = mmc_init_queue(&md->queue, card, &md->lock);
318 if (ret) {
319 put_disk(md->disk);
320 kfree(md);
321 md = ERR_PTR(ret);
322 goto out;
324 md->queue.prep_fn = mmc_blk_prep_rq;
325 md->queue.issue_fn = mmc_blk_issue_rq;
326 md->queue.data = md;
328 md->disk->major = major;
329 md->disk->first_minor = devidx << MMC_SHIFT;
330 md->disk->fops = &mmc_bdops;
331 md->disk->private_data = md;
332 md->disk->queue = md->queue.queue;
333 md->disk->driverfs_dev = &card->dev;
336 * As discussed on lkml, GENHD_FL_REMOVABLE should:
338 * - be set for removable media with permanent block devices
339 * - be unset for removable block devices with permanent media
341 * Since MMC block devices clearly fall under the second
342 * case, we do not set GENHD_FL_REMOVABLE. Userspace
343 * should use the block device creation/destruction hotplug
344 * messages to tell when the card is present.
347 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
348 sprintf(md->disk->devfs_name, "mmc/blk%d", devidx);
350 md->block_bits = card->csd.read_blkbits;
352 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
353 set_capacity(md->disk, card->csd.capacity);
355 out:
356 return md;
359 static int
360 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
362 struct mmc_command cmd;
363 int err;
365 mmc_card_claim_host(card);
366 cmd.opcode = MMC_SET_BLOCKLEN;
367 cmd.arg = 1 << card->csd.read_blkbits;
368 cmd.flags = MMC_RSP_R1;
369 err = mmc_wait_for_cmd(card->host, &cmd, 5);
370 mmc_card_release_host(card);
372 if (err) {
373 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
374 md->disk->disk_name, cmd.arg, err);
375 return -EINVAL;
378 return 0;
381 static int mmc_blk_probe(struct mmc_card *card)
383 struct mmc_blk_data *md;
384 int err;
387 * Check that the card supports the command class(es) we need.
389 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
390 return -ENODEV;
392 if (card->csd.read_blkbits < 9) {
393 printk(KERN_WARNING "%s: read blocksize too small (%u)\n",
394 mmc_card_id(card), 1 << card->csd.read_blkbits);
395 return -ENODEV;
398 md = mmc_blk_alloc(card);
399 if (IS_ERR(md))
400 return PTR_ERR(md);
402 err = mmc_blk_set_blksize(md, card);
403 if (err)
404 goto out;
406 printk(KERN_INFO "%s: %s %s %dKiB\n",
407 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
408 (card->csd.capacity << card->csd.read_blkbits) / 1024);
410 mmc_set_drvdata(card, md);
411 add_disk(md->disk);
412 return 0;
414 out:
415 mmc_blk_put(md);
417 return err;
420 static void mmc_blk_remove(struct mmc_card *card)
422 struct mmc_blk_data *md = mmc_get_drvdata(card);
424 if (md) {
425 int devidx;
427 del_gendisk(md->disk);
430 * I think this is needed.
432 md->disk->queue = NULL;
434 devidx = md->disk->first_minor >> MMC_SHIFT;
435 __clear_bit(devidx, dev_use);
437 mmc_blk_put(md);
439 mmc_set_drvdata(card, NULL);
442 #ifdef CONFIG_PM
443 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
445 struct mmc_blk_data *md = mmc_get_drvdata(card);
447 if (md) {
448 mmc_queue_suspend(&md->queue);
450 return 0;
453 static int mmc_blk_resume(struct mmc_card *card)
455 struct mmc_blk_data *md = mmc_get_drvdata(card);
457 if (md) {
458 mmc_blk_set_blksize(md, card);
459 mmc_queue_resume(&md->queue);
461 return 0;
463 #else
464 #define mmc_blk_suspend NULL
465 #define mmc_blk_resume NULL
466 #endif
468 static struct mmc_driver mmc_driver = {
469 .drv = {
470 .name = "mmcblk",
472 .probe = mmc_blk_probe,
473 .remove = mmc_blk_remove,
474 .suspend = mmc_blk_suspend,
475 .resume = mmc_blk_resume,
478 static int __init mmc_blk_init(void)
480 int res = -ENOMEM;
482 res = register_blkdev(major, "mmc");
483 if (res < 0) {
484 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
485 major, res);
486 goto out;
488 if (major == 0)
489 major = res;
491 devfs_mk_dir("mmc");
492 return mmc_register_driver(&mmc_driver);
494 out:
495 return res;
498 static void __exit mmc_blk_exit(void)
500 mmc_unregister_driver(&mmc_driver);
501 devfs_remove("mmc");
502 unregister_blkdev(major, "mmc");
505 module_init(mmc_blk_init);
506 module_exit(mmc_blk_exit);
508 MODULE_LICENSE("GPL");
509 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
511 module_param(major, int, 0444);
512 MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");