Merge with ../linux-2.6
[pandora-kernel.git] / drivers / mmc / mmc_block.c
1 /*
2  * Block driver for media (i.e., flash cards)
3  *
4  * Copyright 2002 Hewlett-Packard Company
5  *
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.
9  *
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.
13  *
14  * Many thanks to Alessandro Rubini and Jonathan Corbet!
15  *
16  * Author:  Andrew Christian
17  *          28 May 2002
18  */
19 #include <linux/moduleparam.h>
20 #include <linux/module.h>
21 #include <linux/init.h>
22
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>
31
32 #include <linux/mmc/card.h>
33 #include <linux/mmc/protocol.h>
34 #include <linux/mmc/host.h>
35
36 #include <asm/system.h>
37 #include <asm/uaccess.h>
38
39 #include "mmc_queue.h"
40
41 /*
42  * max 8 partitions per card
43  */
44 #define MMC_SHIFT       3
45
46 static int major;
47
48 /*
49  * There is one mmc_blk_data per slot.
50  */
51 struct mmc_blk_data {
52         spinlock_t      lock;
53         struct gendisk  *disk;
54         struct mmc_queue queue;
55
56         unsigned int    usage;
57         unsigned int    block_bits;
58         unsigned int    suspended;
59 };
60
61 static DECLARE_MUTEX(open_lock);
62
63 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
64 {
65         struct mmc_blk_data *md;
66
67         down(&open_lock);
68         md = disk->private_data;
69         if (md && md->usage == 0)
70                 md = NULL;
71         if (md)
72                 md->usage++;
73         up(&open_lock);
74
75         return md;
76 }
77
78 static void mmc_blk_put(struct mmc_blk_data *md)
79 {
80         down(&open_lock);
81         md->usage--;
82         if (md->usage == 0) {
83                 put_disk(md->disk);
84                 mmc_cleanup_queue(&md->queue);
85                 kfree(md);
86         }
87         up(&open_lock);
88 }
89
90 static int mmc_blk_open(struct inode *inode, struct file *filp)
91 {
92         struct mmc_blk_data *md;
93         int ret = -ENXIO;
94
95         md = mmc_blk_get(inode->i_bdev->bd_disk);
96         if (md) {
97                 if (md->usage == 2)
98                         check_disk_change(inode->i_bdev);
99                 ret = 0;
100
101                 if ((filp->f_mode & FMODE_WRITE) &&
102                         mmc_card_readonly(md->queue.card))
103                         ret = -EROFS;
104         }
105
106         return ret;
107 }
108
109 static int mmc_blk_release(struct inode *inode, struct file *filp)
110 {
111         struct mmc_blk_data *md = inode->i_bdev->bd_disk->private_data;
112
113         mmc_blk_put(md);
114         return 0;
115 }
116
117 static int
118 mmc_blk_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
119 {
120         struct block_device *bdev = inode->i_bdev;
121
122         if (cmd == HDIO_GETGEO) {
123                 struct hd_geometry geo;
124
125                 memset(&geo, 0, sizeof(struct hd_geometry));
126
127                 geo.cylinders   = get_capacity(bdev->bd_disk) / (4 * 16);
128                 geo.heads       = 4;
129                 geo.sectors     = 16;
130                 geo.start       = get_start_sect(bdev);
131
132                 return copy_to_user((void __user *)arg, &geo, sizeof(geo))
133                         ? -EFAULT : 0;
134         }
135
136         return -ENOTTY;
137 }
138
139 static struct block_device_operations mmc_bdops = {
140         .open                   = mmc_blk_open,
141         .release                = mmc_blk_release,
142         .ioctl                  = mmc_blk_ioctl,
143         .owner                  = THIS_MODULE,
144 };
145
146 struct mmc_blk_request {
147         struct mmc_request      mrq;
148         struct mmc_command      cmd;
149         struct mmc_command      stop;
150         struct mmc_data         data;
151 };
152
153 static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
154 {
155         struct mmc_blk_data *md = mq->data;
156         int stat = BLKPREP_OK;
157
158         /*
159          * If we have no device, we haven't finished initialising.
160          */
161         if (!md || !mq->card) {
162                 printk(KERN_ERR "%s: killing request - no device/host\n",
163                        req->rq_disk->disk_name);
164                 stat = BLKPREP_KILL;
165         }
166
167         if (md->suspended) {
168                 blk_plug_device(md->queue.queue);
169                 stat = BLKPREP_DEFER;
170         }
171
172         /*
173          * Check for excessive requests.
174          */
175         if (req->sector + req->nr_sectors > get_capacity(req->rq_disk)) {
176                 printk("bad request size\n");
177                 stat = BLKPREP_KILL;
178         }
179
180         return stat;
181 }
182
183 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
184 {
185         struct mmc_blk_data *md = mq->data;
186         struct mmc_card *card = md->queue.card;
187         int ret;
188         
189 #ifdef CONFIG_MMC_BULKTRANSFER
190         int failsafe;
191 #endif
192
193         if (mmc_card_claim_host(card))
194                 goto cmd_err;
195         
196 #ifdef CONFIG_MMC_BULKTRANSFER
197         /*
198          * We first try transfering multiple blocks. If this fails
199          * we fall back to single block transfers.
200          *
201          * This gives us good performance when all is well and the
202          * possibility to determine which sector fails when all
203          * is not well.
204          */
205         failsafe = 0;
206 #endif
207
208         do {
209                 struct mmc_blk_request brq;
210                 struct mmc_command cmd;
211
212                 memset(&brq, 0, sizeof(struct mmc_blk_request));
213                 brq.mrq.cmd = &brq.cmd;
214                 brq.mrq.data = &brq.data;
215
216                 brq.cmd.arg = req->sector << 9;
217                 brq.cmd.flags = MMC_RSP_R1;
218                 brq.data.timeout_ns = card->csd.tacc_ns * 10;
219                 brq.data.timeout_clks = card->csd.tacc_clks * 10;
220                 brq.data.blksz_bits = md->block_bits;
221                 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
222                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
223                 brq.stop.arg = 0;
224                 brq.stop.flags = MMC_RSP_R1B;
225
226 #ifdef CONFIG_MMC_BULKTRANSFER          
227                 /*
228                  * A multi-block transfer failed. Falling back to single
229                  * blocks.
230                  */
231                 if (failsafe)
232                         brq.data.blocks = 1;
233                 
234 #else
235                 /*
236                  * Writes are done one sector at a time.
237                  */
238                 if (rq_data_dir(req) != READ)
239                         brq.data.blocks = 1;
240 #endif
241                 
242                 ret = 1;
243
244                 if (rq_data_dir(req) == READ) {
245                         brq.cmd.opcode = brq.data.blocks > 1 ? MMC_READ_MULTIPLE_BLOCK : MMC_READ_SINGLE_BLOCK;
246                         brq.data.flags |= MMC_DATA_READ;
247                 } else {
248                         brq.cmd.opcode = brq.data.blocks > 1 ? MMC_WRITE_MULTIPLE_BLOCK :
249                                 MMC_WRITE_BLOCK;
250                         brq.cmd.flags = MMC_RSP_R1B;
251                         brq.data.flags |= MMC_DATA_WRITE;
252                 }
253                 brq.mrq.stop = brq.data.blocks > 1 ? &brq.stop : NULL;
254
255                 brq.data.sg = mq->sg;
256                 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
257
258                 mmc_wait_for_req(card->host, &brq.mrq);
259                 if (brq.cmd.error) {
260                         printk(KERN_ERR "%s: error %d sending read/write command\n",
261                                req->rq_disk->disk_name, brq.cmd.error);
262                         goto cmd_fail;
263                 }
264
265                 if (brq.data.error) {
266                         printk(KERN_ERR "%s: error %d transferring data\n",
267                                req->rq_disk->disk_name, brq.data.error);
268                         goto cmd_fail;
269                 }
270
271                 if (brq.stop.error) {
272                         printk(KERN_ERR "%s: error %d sending stop command\n",
273                                req->rq_disk->disk_name, brq.stop.error);
274                         goto cmd_fail;
275                 }
276
277                 /* No need to check card status after a read */
278                 if (rq_data_dir(req) == READ)
279                         goto card_ready;
280
281                 do {
282                         int err;
283
284                         cmd.opcode = MMC_SEND_STATUS;
285                         cmd.arg = card->rca << 16;
286                         cmd.flags = MMC_RSP_R1;
287                         err = mmc_wait_for_cmd(card->host, &cmd, 5);
288                         if (err) {
289                                 printk(KERN_ERR "%s: error %d requesting status\n",
290                                        req->rq_disk->disk_name, err);
291                                 goto cmd_fail;
292                         }
293 #ifdef CONFIG_MMC_BLOCK_BROKEN_RFD
294                         /* Work-around for broken cards setting READY_FOR_DATA
295                          * when not actually ready.
296                          */
297                         if (R1_CURRENT_STATE(cmd.resp[0]) == 7)
298                                 cmd.resp[0] &= ~R1_READY_FOR_DATA;
299 #endif
300                 } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
301
302 #if 0
303                 if (cmd.resp[0] & ~0x00000900)
304                         printk(KERN_ERR "%s: status = %08x\n",
305                                req->rq_disk->disk_name, cmd.resp[0]);
306                 if (mmc_decode_status(cmd.resp))
307                         goto cmd_err;
308 #endif
309
310         card_ready:
311
312                 /*
313                  * A block was successfully transferred.
314                  */
315                 spin_lock_irq(&md->lock);
316                 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
317                 if (!ret) {
318                         /*
319                          * The whole request completed successfully.
320                          */
321                         add_disk_randomness(req->rq_disk);
322                         blkdev_dequeue_request(req);
323                         end_that_request_last(req);
324                 }
325                 spin_unlock_irq(&md->lock);
326                 
327 #ifdef CONFIG_MMC_BULKTRANSFER
328                 /*
329                  * Go back to bulk mode if in failsafe mode.
330                  */
331                 failsafe = 0;
332 #endif
333
334                 continue;
335
336  cmd_fail:
337
338 #ifdef CONFIG_MMC_BULKTRANSFER
339                 if (failsafe)
340                         goto cmd_err;
341                 else
342                         failsafe = 1;
343 #else
344                 goto cmd_err;
345 #endif
346
347         } while (ret);
348
349         mmc_card_release_host(card);
350
351         return 1;
352
353  cmd_err:
354         mmc_card_release_host(card);
355
356         /*
357          * This is a little draconian, but until we get proper
358          * error handling sorted out here, its the best we can
359          * do - especially as some hosts have no idea how much
360          * data was transferred before the error occurred.
361          */
362         spin_lock_irq(&md->lock);
363         do {
364                 ret = end_that_request_chunk(req, 0,
365                                 req->current_nr_sectors << 9);
366         } while (ret);
367
368         add_disk_randomness(req->rq_disk);
369         blkdev_dequeue_request(req);
370         end_that_request_last(req);
371         spin_unlock_irq(&md->lock);
372
373         /* If a command fails, the card might be removed. */
374         mmc_detect_change(card->host);
375
376         return 0;
377 }
378
379 #define MMC_NUM_MINORS  (256 >> MMC_SHIFT)
380
381 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
382
383 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
384 {
385         struct mmc_blk_data *md;
386         int devidx, ret;
387
388         devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
389         if (devidx >= MMC_NUM_MINORS)
390                 return ERR_PTR(-ENOSPC);
391         __set_bit(devidx, dev_use);
392
393         md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
394         if (md) {
395                 memset(md, 0, sizeof(struct mmc_blk_data));
396
397                 md->disk = alloc_disk(1 << MMC_SHIFT);
398                 if (md->disk == NULL) {
399                         kfree(md);
400                         md = ERR_PTR(-ENOMEM);
401                         goto out;
402                 }
403
404                 spin_lock_init(&md->lock);
405                 md->usage = 1;
406
407                 ret = mmc_init_queue(&md->queue, card, &md->lock);
408                 if (ret) {
409                         put_disk(md->disk);
410                         kfree(md);
411                         md = ERR_PTR(ret);
412                         goto out;
413                 }
414                 md->queue.prep_fn = mmc_blk_prep_rq;
415                 md->queue.issue_fn = mmc_blk_issue_rq;
416                 md->queue.data = md;
417
418                 md->disk->major = major;
419                 md->disk->first_minor = devidx << MMC_SHIFT;
420                 md->disk->fops = &mmc_bdops;
421                 md->disk->private_data = md;
422                 md->disk->queue = md->queue.queue;
423                 md->disk->driverfs_dev = &card->dev;
424
425                 /*
426                  * As discussed on lkml, GENHD_FL_REMOVABLE should:
427                  *
428                  * - be set for removable media with permanent block devices
429                  * - be unset for removable block devices with permanent media
430                  *
431                  * Since MMC block devices clearly fall under the second
432                  * case, we do not set GENHD_FL_REMOVABLE.  Userspace
433                  * should use the block device creation/destruction hotplug
434                  * messages to tell when the card is present.
435                  */
436
437                 sprintf(md->disk->disk_name, "mmcblk%d", devidx);
438                 sprintf(md->disk->devfs_name, "mmc/blk%d", devidx);
439
440                 md->block_bits = card->csd.read_blkbits;
441
442                 blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
443                 set_capacity(md->disk, card->csd.capacity);
444         }
445  out:
446         return md;
447 }
448
449 static int
450 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
451 {
452         struct mmc_command cmd;
453         int err;
454
455         mmc_card_claim_host(card);
456         cmd.opcode = MMC_SET_BLOCKLEN;
457         cmd.arg = 1 << card->csd.read_blkbits;
458         cmd.flags = MMC_RSP_R1;
459         err = mmc_wait_for_cmd(card->host, &cmd, 5);
460         mmc_card_release_host(card);
461
462         if (err) {
463                 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
464                         md->disk->disk_name, cmd.arg, err);
465                 return -EINVAL;
466         }
467
468         return 0;
469 }
470
471 static int mmc_blk_probe(struct mmc_card *card)
472 {
473         struct mmc_blk_data *md;
474         int err;
475
476         /*
477          * Check that the card supports the command class(es) we need.
478          */
479         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
480                 return -ENODEV;
481
482         if (card->csd.read_blkbits < 9) {
483                 printk(KERN_WARNING "%s: read blocksize too small (%u)\n",
484                         mmc_card_id(card), 1 << card->csd.read_blkbits);
485                 return -ENODEV;
486         }
487
488         md = mmc_blk_alloc(card);
489         if (IS_ERR(md))
490                 return PTR_ERR(md);
491
492         err = mmc_blk_set_blksize(md, card);
493         if (err)
494                 goto out;
495
496         printk(KERN_INFO "%s: %s %s %dKiB %s\n",
497                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
498                 (card->csd.capacity << card->csd.read_blkbits) / 1024,
499                 mmc_card_readonly(card)?"(ro)":"");
500
501         mmc_set_drvdata(card, md);
502         add_disk(md->disk);
503         return 0;
504
505  out:
506         mmc_blk_put(md);
507
508         return err;
509 }
510
511 static void mmc_blk_remove(struct mmc_card *card)
512 {
513         struct mmc_blk_data *md = mmc_get_drvdata(card);
514
515         if (md) {
516                 int devidx;
517
518                 del_gendisk(md->disk);
519
520                 /*
521                  * I think this is needed.
522                  */
523                 md->disk->queue = NULL;
524
525                 devidx = md->disk->first_minor >> MMC_SHIFT;
526                 __clear_bit(devidx, dev_use);
527
528                 mmc_blk_put(md);
529         }
530         mmc_set_drvdata(card, NULL);
531 }
532
533 #ifdef CONFIG_PM
534 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
535 {
536         struct mmc_blk_data *md = mmc_get_drvdata(card);
537
538         if (md) {
539                 mmc_queue_suspend(&md->queue);
540         }
541         return 0;
542 }
543
544 static int mmc_blk_resume(struct mmc_card *card)
545 {
546         struct mmc_blk_data *md = mmc_get_drvdata(card);
547
548         if (md) {
549                 mmc_blk_set_blksize(md, card);
550                 mmc_queue_resume(&md->queue);
551         }
552         return 0;
553 }
554 #else
555 #define mmc_blk_suspend NULL
556 #define mmc_blk_resume  NULL
557 #endif
558
559 static struct mmc_driver mmc_driver = {
560         .drv            = {
561                 .name   = "mmcblk",
562         },
563         .probe          = mmc_blk_probe,
564         .remove         = mmc_blk_remove,
565         .suspend        = mmc_blk_suspend,
566         .resume         = mmc_blk_resume,
567 };
568
569 static int __init mmc_blk_init(void)
570 {
571         int res = -ENOMEM;
572
573         res = register_blkdev(major, "mmc");
574         if (res < 0) {
575                 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
576                        major, res);
577                 goto out;
578         }
579         if (major == 0)
580                 major = res;
581
582         devfs_mk_dir("mmc");
583         return mmc_register_driver(&mmc_driver);
584
585  out:
586         return res;
587 }
588
589 static void __exit mmc_blk_exit(void)
590 {
591         mmc_unregister_driver(&mmc_driver);
592         devfs_remove("mmc");
593         unregister_blkdev(major, "mmc");
594 }
595
596 module_init(mmc_blk_init);
597 module_exit(mmc_blk_exit);
598
599 MODULE_LICENSE("GPL");
600 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
601
602 module_param(major, int, 0444);
603 MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");