Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[cascardo/linux.git] / drivers / mmc / card / block.c
1 /*
2  * Block driver for media (i.e., flash cards)
3  *
4  * Copyright 2002 Hewlett-Packard Company
5  * Copyright 2005-2007 Pierre Ossman
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
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>
32
33 #include <linux/mmc/card.h>
34 #include <linux/mmc/host.h>
35 #include <linux/mmc/mmc.h>
36 #include <linux/mmc/sd.h>
37
38 #include <asm/system.h>
39 #include <asm/uaccess.h>
40
41 #include "queue.h"
42
43 /*
44  * max 8 partitions per card
45  */
46 #define MMC_SHIFT       3
47
48 static int major;
49
50 /*
51  * There is one mmc_blk_data per slot.
52  */
53 struct mmc_blk_data {
54         spinlock_t      lock;
55         struct gendisk  *disk;
56         struct mmc_queue queue;
57
58         unsigned int    usage;
59         unsigned int    block_bits;
60         unsigned int    read_only;
61 };
62
63 static DEFINE_MUTEX(open_lock);
64
65 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
66 {
67         struct mmc_blk_data *md;
68
69         mutex_lock(&open_lock);
70         md = disk->private_data;
71         if (md && md->usage == 0)
72                 md = NULL;
73         if (md)
74                 md->usage++;
75         mutex_unlock(&open_lock);
76
77         return md;
78 }
79
80 static void mmc_blk_put(struct mmc_blk_data *md)
81 {
82         mutex_lock(&open_lock);
83         md->usage--;
84         if (md->usage == 0) {
85                 put_disk(md->disk);
86                 kfree(md);
87         }
88         mutex_unlock(&open_lock);
89 }
90
91 static int mmc_blk_open(struct inode *inode, struct file *filp)
92 {
93         struct mmc_blk_data *md;
94         int ret = -ENXIO;
95
96         md = mmc_blk_get(inode->i_bdev->bd_disk);
97         if (md) {
98                 if (md->usage == 2)
99                         check_disk_change(inode->i_bdev);
100                 ret = 0;
101
102                 if ((filp->f_mode & FMODE_WRITE) && md->read_only)
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_getgeo(struct block_device *bdev, struct hd_geometry *geo)
119 {
120         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
121         geo->heads = 4;
122         geo->sectors = 16;
123         return 0;
124 }
125
126 static struct block_device_operations mmc_bdops = {
127         .open                   = mmc_blk_open,
128         .release                = mmc_blk_release,
129         .getgeo                 = mmc_blk_getgeo,
130         .owner                  = THIS_MODULE,
131 };
132
133 struct mmc_blk_request {
134         struct mmc_request      mrq;
135         struct mmc_command      cmd;
136         struct mmc_command      stop;
137         struct mmc_data         data;
138 };
139
140 static int mmc_blk_prep_rq(struct mmc_queue *mq, struct request *req)
141 {
142         struct mmc_blk_data *md = mq->data;
143         int stat = BLKPREP_OK;
144
145         /*
146          * If we have no device, we haven't finished initialising.
147          */
148         if (!md || !mq->card) {
149                 printk(KERN_ERR "%s: killing request - no device/host\n",
150                        req->rq_disk->disk_name);
151                 stat = BLKPREP_KILL;
152         }
153
154         return stat;
155 }
156
157 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
158 {
159         int err;
160         u32 blocks;
161
162         struct mmc_request mrq;
163         struct mmc_command cmd;
164         struct mmc_data data;
165         unsigned int timeout_us;
166
167         struct scatterlist sg;
168
169         memset(&cmd, 0, sizeof(struct mmc_command));
170
171         cmd.opcode = MMC_APP_CMD;
172         cmd.arg = card->rca << 16;
173         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
174
175         err = mmc_wait_for_cmd(card->host, &cmd, 0);
176         if ((err != MMC_ERR_NONE) || !(cmd.resp[0] & R1_APP_CMD))
177                 return (u32)-1;
178
179         memset(&cmd, 0, sizeof(struct mmc_command));
180
181         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
182         cmd.arg = 0;
183         cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
184
185         memset(&data, 0, sizeof(struct mmc_data));
186
187         data.timeout_ns = card->csd.tacc_ns * 100;
188         data.timeout_clks = card->csd.tacc_clks * 100;
189
190         timeout_us = data.timeout_ns / 1000;
191         timeout_us += data.timeout_clks * 1000 /
192                 (card->host->ios.clock / 1000);
193
194         if (timeout_us > 100000) {
195                 data.timeout_ns = 100000000;
196                 data.timeout_clks = 0;
197         }
198
199         data.blksz = 4;
200         data.blocks = 1;
201         data.flags = MMC_DATA_READ;
202         data.sg = &sg;
203         data.sg_len = 1;
204
205         memset(&mrq, 0, sizeof(struct mmc_request));
206
207         mrq.cmd = &cmd;
208         mrq.data = &data;
209
210         sg_init_one(&sg, &blocks, 4);
211
212         mmc_wait_for_req(card->host, &mrq);
213
214         if (cmd.error != MMC_ERR_NONE || data.error != MMC_ERR_NONE)
215                 return (u32)-1;
216
217         blocks = ntohl(blocks);
218
219         return blocks;
220 }
221
222 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
223 {
224         struct mmc_blk_data *md = mq->data;
225         struct mmc_card *card = md->queue.card;
226         struct mmc_blk_request brq;
227         int ret = 1, sg_pos, data_size;
228
229         mmc_claim_host(card->host);
230
231         do {
232                 struct mmc_command cmd;
233                 u32 readcmd, writecmd;
234
235                 memset(&brq, 0, sizeof(struct mmc_blk_request));
236                 brq.mrq.cmd = &brq.cmd;
237                 brq.mrq.data = &brq.data;
238
239                 brq.cmd.arg = req->sector;
240                 if (!mmc_card_blockaddr(card))
241                         brq.cmd.arg <<= 9;
242                 brq.cmd.flags = MMC_RSP_R1 | MMC_CMD_ADTC;
243                 brq.data.blksz = 1 << md->block_bits;
244                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
245                 brq.stop.arg = 0;
246                 brq.stop.flags = MMC_RSP_R1B | MMC_CMD_AC;
247                 brq.data.blocks = req->nr_sectors >> (md->block_bits - 9);
248                 if (brq.data.blocks > card->host->max_blk_count)
249                         brq.data.blocks = card->host->max_blk_count;
250
251                 mmc_set_data_timeout(&brq.data, card, rq_data_dir(req) != READ);
252
253                 /*
254                  * If the host doesn't support multiple block writes, force
255                  * block writes to single block. SD cards are excepted from
256                  * this rule as they support querying the number of
257                  * successfully written sectors.
258                  */
259                 if (rq_data_dir(req) != READ &&
260                     !(card->host->caps & MMC_CAP_MULTIWRITE) &&
261                     !mmc_card_sd(card))
262                         brq.data.blocks = 1;
263
264                 if (brq.data.blocks > 1) {
265                         brq.data.flags |= MMC_DATA_MULTI;
266                         brq.mrq.stop = &brq.stop;
267                         readcmd = MMC_READ_MULTIPLE_BLOCK;
268                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
269                 } else {
270                         brq.mrq.stop = NULL;
271                         readcmd = MMC_READ_SINGLE_BLOCK;
272                         writecmd = MMC_WRITE_BLOCK;
273                 }
274
275                 if (rq_data_dir(req) == READ) {
276                         brq.cmd.opcode = readcmd;
277                         brq.data.flags |= MMC_DATA_READ;
278                 } else {
279                         brq.cmd.opcode = writecmd;
280                         brq.data.flags |= MMC_DATA_WRITE;
281                 }
282
283                 brq.data.sg = mq->sg;
284                 brq.data.sg_len = blk_rq_map_sg(req->q, req, brq.data.sg);
285
286                 if (brq.data.blocks !=
287                     (req->nr_sectors >> (md->block_bits - 9))) {
288                         data_size = brq.data.blocks * brq.data.blksz;
289                         for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
290                                 data_size -= mq->sg[sg_pos].length;
291                                 if (data_size <= 0) {
292                                         mq->sg[sg_pos].length += data_size;
293                                         sg_pos++;
294                                         break;
295                                 }
296                         }
297                         brq.data.sg_len = sg_pos;
298                 }
299
300                 mmc_wait_for_req(card->host, &brq.mrq);
301                 if (brq.cmd.error) {
302                         printk(KERN_ERR "%s: error %d sending read/write command\n",
303                                req->rq_disk->disk_name, brq.cmd.error);
304                         goto cmd_err;
305                 }
306
307                 if (brq.data.error) {
308                         printk(KERN_ERR "%s: error %d transferring data\n",
309                                req->rq_disk->disk_name, brq.data.error);
310                         goto cmd_err;
311                 }
312
313                 if (brq.stop.error) {
314                         printk(KERN_ERR "%s: error %d sending stop command\n",
315                                req->rq_disk->disk_name, brq.stop.error);
316                         goto cmd_err;
317                 }
318
319                 if (rq_data_dir(req) != READ) {
320                         do {
321                                 int err;
322
323                                 cmd.opcode = MMC_SEND_STATUS;
324                                 cmd.arg = card->rca << 16;
325                                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
326                                 err = mmc_wait_for_cmd(card->host, &cmd, 5);
327                                 if (err) {
328                                         printk(KERN_ERR "%s: error %d requesting status\n",
329                                                req->rq_disk->disk_name, err);
330                                         goto cmd_err;
331                                 }
332                         } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
333
334 #if 0
335                         if (cmd.resp[0] & ~0x00000900)
336                                 printk(KERN_ERR "%s: status = %08x\n",
337                                        req->rq_disk->disk_name, cmd.resp[0]);
338                         if (mmc_decode_status(cmd.resp))
339                                 goto cmd_err;
340 #endif
341                 }
342
343                 /*
344                  * A block was successfully transferred.
345                  */
346                 spin_lock_irq(&md->lock);
347                 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
348                 if (!ret) {
349                         /*
350                          * The whole request completed successfully.
351                          */
352                         add_disk_randomness(req->rq_disk);
353                         blkdev_dequeue_request(req);
354                         end_that_request_last(req, 1);
355                 }
356                 spin_unlock_irq(&md->lock);
357         } while (ret);
358
359         mmc_release_host(card->host);
360
361         return 1;
362
363  cmd_err:
364         /*
365          * If this is an SD card and we're writing, we can first
366          * mark the known good sectors as ok.
367          *
368          * If the card is not SD, we can still ok written sectors
369          * if the controller can do proper error reporting.
370          *
371          * For reads we just fail the entire chunk as that should
372          * be safe in all cases.
373          */
374         if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
375                 u32 blocks;
376                 unsigned int bytes;
377
378                 blocks = mmc_sd_num_wr_blocks(card);
379                 if (blocks != (u32)-1) {
380                         if (card->csd.write_partial)
381                                 bytes = blocks << md->block_bits;
382                         else
383                                 bytes = blocks << 9;
384                         spin_lock_irq(&md->lock);
385                         ret = end_that_request_chunk(req, 1, bytes);
386                         spin_unlock_irq(&md->lock);
387                 }
388         } else if (rq_data_dir(req) != READ &&
389                    (card->host->caps & MMC_CAP_MULTIWRITE)) {
390                 spin_lock_irq(&md->lock);
391                 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
392                 spin_unlock_irq(&md->lock);
393         }
394
395         mmc_release_host(card->host);
396
397         spin_lock_irq(&md->lock);
398         while (ret) {
399                 ret = end_that_request_chunk(req, 0,
400                                 req->current_nr_sectors << 9);
401         }
402
403         add_disk_randomness(req->rq_disk);
404         blkdev_dequeue_request(req);
405         end_that_request_last(req, 0);
406         spin_unlock_irq(&md->lock);
407
408         return 0;
409 }
410
411 #define MMC_NUM_MINORS  (256 >> MMC_SHIFT)
412
413 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
414
415 static inline int mmc_blk_readonly(struct mmc_card *card)
416 {
417         return mmc_card_readonly(card) ||
418                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
419 }
420
421 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
422 {
423         struct mmc_blk_data *md;
424         int devidx, ret;
425
426         devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
427         if (devidx >= MMC_NUM_MINORS)
428                 return ERR_PTR(-ENOSPC);
429         __set_bit(devidx, dev_use);
430
431         md = kmalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
432         if (!md) {
433                 ret = -ENOMEM;
434                 goto out;
435         }
436
437         memset(md, 0, sizeof(struct mmc_blk_data));
438
439         /*
440          * Set the read-only status based on the supported commands
441          * and the write protect switch.
442          */
443         md->read_only = mmc_blk_readonly(card);
444
445         /*
446          * Both SD and MMC specifications state (although a bit
447          * unclearly in the MMC case) that a block size of 512
448          * bytes must always be supported by the card.
449          */
450         md->block_bits = 9;
451
452         md->disk = alloc_disk(1 << MMC_SHIFT);
453         if (md->disk == NULL) {
454                 ret = -ENOMEM;
455                 goto err_kfree;
456         }
457
458         spin_lock_init(&md->lock);
459         md->usage = 1;
460
461         ret = mmc_init_queue(&md->queue, card, &md->lock);
462         if (ret)
463                 goto err_putdisk;
464
465         md->queue.prep_fn = mmc_blk_prep_rq;
466         md->queue.issue_fn = mmc_blk_issue_rq;
467         md->queue.data = md;
468
469         md->disk->major = major;
470         md->disk->first_minor = devidx << MMC_SHIFT;
471         md->disk->fops = &mmc_bdops;
472         md->disk->private_data = md;
473         md->disk->queue = md->queue.queue;
474         md->disk->driverfs_dev = &card->dev;
475
476         /*
477          * As discussed on lkml, GENHD_FL_REMOVABLE should:
478          *
479          * - be set for removable media with permanent block devices
480          * - be unset for removable block devices with permanent media
481          *
482          * Since MMC block devices clearly fall under the second
483          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
484          * should use the block device creation/destruction hotplug
485          * messages to tell when the card is present.
486          */
487
488         sprintf(md->disk->disk_name, "mmcblk%d", devidx);
489
490         blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
491
492         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
493                 /*
494                  * The EXT_CSD sector count is in number or 512 byte
495                  * sectors.
496                  */
497                 set_capacity(md->disk, card->ext_csd.sectors);
498         } else {
499                 /*
500                  * The CSD capacity field is in units of read_blkbits.
501                  * set_capacity takes units of 512 bytes.
502                  */
503                 set_capacity(md->disk,
504                         card->csd.capacity << (card->csd.read_blkbits - 9));
505         }
506         return md;
507
508  err_putdisk:
509         put_disk(md->disk);
510  err_kfree:
511         kfree(md);
512  out:
513         return ERR_PTR(ret);
514 }
515
516 static int
517 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
518 {
519         struct mmc_command cmd;
520         int err;
521
522         /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
523         if (mmc_card_blockaddr(card))
524                 return 0;
525
526         mmc_claim_host(card->host);
527         cmd.opcode = MMC_SET_BLOCKLEN;
528         cmd.arg = 1 << md->block_bits;
529         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
530         err = mmc_wait_for_cmd(card->host, &cmd, 5);
531         mmc_release_host(card->host);
532
533         if (err) {
534                 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
535                         md->disk->disk_name, cmd.arg, err);
536                 return -EINVAL;
537         }
538
539         return 0;
540 }
541
542 static int mmc_blk_probe(struct mmc_card *card)
543 {
544         struct mmc_blk_data *md;
545         int err;
546
547         /*
548          * Check that the card supports the command class(es) we need.
549          */
550         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
551                 return -ENODEV;
552
553         md = mmc_blk_alloc(card);
554         if (IS_ERR(md))
555                 return PTR_ERR(md);
556
557         err = mmc_blk_set_blksize(md, card);
558         if (err)
559                 goto out;
560
561         printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
562                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
563                 (unsigned long long)(get_capacity(md->disk) >> 1),
564                 md->read_only ? "(ro)" : "");
565
566         mmc_set_drvdata(card, md);
567         add_disk(md->disk);
568         return 0;
569
570  out:
571         mmc_blk_put(md);
572
573         return err;
574 }
575
576 static void mmc_blk_remove(struct mmc_card *card)
577 {
578         struct mmc_blk_data *md = mmc_get_drvdata(card);
579
580         if (md) {
581                 int devidx;
582
583                 /* Stop new requests from getting into the queue */
584                 del_gendisk(md->disk);
585
586                 /* Then flush out any already in there */
587                 mmc_cleanup_queue(&md->queue);
588
589                 devidx = md->disk->first_minor >> MMC_SHIFT;
590                 __clear_bit(devidx, dev_use);
591
592                 mmc_blk_put(md);
593         }
594         mmc_set_drvdata(card, NULL);
595 }
596
597 #ifdef CONFIG_PM
598 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
599 {
600         struct mmc_blk_data *md = mmc_get_drvdata(card);
601
602         if (md) {
603                 mmc_queue_suspend(&md->queue);
604         }
605         return 0;
606 }
607
608 static int mmc_blk_resume(struct mmc_card *card)
609 {
610         struct mmc_blk_data *md = mmc_get_drvdata(card);
611
612         if (md) {
613                 mmc_blk_set_blksize(md, card);
614                 mmc_queue_resume(&md->queue);
615         }
616         return 0;
617 }
618 #else
619 #define mmc_blk_suspend NULL
620 #define mmc_blk_resume  NULL
621 #endif
622
623 static struct mmc_driver mmc_driver = {
624         .drv            = {
625                 .name   = "mmcblk",
626         },
627         .probe          = mmc_blk_probe,
628         .remove         = mmc_blk_remove,
629         .suspend        = mmc_blk_suspend,
630         .resume         = mmc_blk_resume,
631 };
632
633 static int __init mmc_blk_init(void)
634 {
635         int res = -ENOMEM;
636
637         res = register_blkdev(major, "mmc");
638         if (res < 0) {
639                 printk(KERN_WARNING "Unable to get major %d for MMC media: %d\n",
640                        major, res);
641                 goto out;
642         }
643         if (major == 0)
644                 major = res;
645
646         return mmc_register_driver(&mmc_driver);
647
648  out:
649         return res;
650 }
651
652 static void __exit mmc_blk_exit(void)
653 {
654         mmc_unregister_driver(&mmc_driver);
655         unregister_blkdev(major, "mmc");
656 }
657
658 module_init(mmc_blk_init);
659 module_exit(mmc_blk_exit);
660
661 MODULE_LICENSE("GPL");
662 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
663
664 module_param(major, int, 0444);
665 MODULE_PARM_DESC(major, "specify the major device number for MMC block driver");