Merge branch 'msm-video' of git://codeaurora.org/quic/kernel/dwalker/linux-msm
[pandora-kernel.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-2008 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/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/smp_lock.h>
33 #include <linux/scatterlist.h>
34 #include <linux/string_helpers.h>
35
36 #include <linux/mmc/card.h>
37 #include <linux/mmc/host.h>
38 #include <linux/mmc/mmc.h>
39 #include <linux/mmc/sd.h>
40
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43
44 #include "queue.h"
45
46 MODULE_ALIAS("mmc:block");
47
48 /*
49  * max 8 partitions per card
50  */
51 #define MMC_SHIFT       3
52 #define MMC_NUM_MINORS  (256 >> MMC_SHIFT)
53
54 static DECLARE_BITMAP(dev_use, MMC_NUM_MINORS);
55
56 /*
57  * There is one mmc_blk_data per slot.
58  */
59 struct mmc_blk_data {
60         spinlock_t      lock;
61         struct gendisk  *disk;
62         struct mmc_queue queue;
63
64         unsigned int    usage;
65         unsigned int    read_only;
66 };
67
68 static DEFINE_MUTEX(open_lock);
69
70 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
71 {
72         struct mmc_blk_data *md;
73
74         mutex_lock(&open_lock);
75         md = disk->private_data;
76         if (md && md->usage == 0)
77                 md = NULL;
78         if (md)
79                 md->usage++;
80         mutex_unlock(&open_lock);
81
82         return md;
83 }
84
85 static void mmc_blk_put(struct mmc_blk_data *md)
86 {
87         mutex_lock(&open_lock);
88         md->usage--;
89         if (md->usage == 0) {
90                 int devmaj = MAJOR(disk_devt(md->disk));
91                 int devidx = MINOR(disk_devt(md->disk)) >> MMC_SHIFT;
92
93                 if (!devmaj)
94                         devidx = md->disk->first_minor >> MMC_SHIFT;
95
96                 blk_cleanup_queue(md->queue.queue);
97
98                 __clear_bit(devidx, dev_use);
99
100                 put_disk(md->disk);
101                 kfree(md);
102         }
103         mutex_unlock(&open_lock);
104 }
105
106 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
107 {
108         struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
109         int ret = -ENXIO;
110
111         lock_kernel();
112         if (md) {
113                 if (md->usage == 2)
114                         check_disk_change(bdev);
115                 ret = 0;
116
117                 if ((mode & FMODE_WRITE) && md->read_only) {
118                         mmc_blk_put(md);
119                         ret = -EROFS;
120                 }
121         }
122         unlock_kernel();
123
124         return ret;
125 }
126
127 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
128 {
129         struct mmc_blk_data *md = disk->private_data;
130
131         lock_kernel();
132         mmc_blk_put(md);
133         unlock_kernel();
134         return 0;
135 }
136
137 static int
138 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
139 {
140         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
141         geo->heads = 4;
142         geo->sectors = 16;
143         return 0;
144 }
145
146 static const struct block_device_operations mmc_bdops = {
147         .open                   = mmc_blk_open,
148         .release                = mmc_blk_release,
149         .getgeo                 = mmc_blk_getgeo,
150         .owner                  = THIS_MODULE,
151 };
152
153 struct mmc_blk_request {
154         struct mmc_request      mrq;
155         struct mmc_command      cmd;
156         struct mmc_command      stop;
157         struct mmc_data         data;
158 };
159
160 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
161 {
162         int err;
163         u32 result;
164         __be32 *blocks;
165
166         struct mmc_request mrq;
167         struct mmc_command cmd;
168         struct mmc_data data;
169         unsigned int timeout_us;
170
171         struct scatterlist sg;
172
173         memset(&cmd, 0, sizeof(struct mmc_command));
174
175         cmd.opcode = MMC_APP_CMD;
176         cmd.arg = card->rca << 16;
177         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
178
179         err = mmc_wait_for_cmd(card->host, &cmd, 0);
180         if (err)
181                 return (u32)-1;
182         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
183                 return (u32)-1;
184
185         memset(&cmd, 0, sizeof(struct mmc_command));
186
187         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
188         cmd.arg = 0;
189         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
190
191         memset(&data, 0, sizeof(struct mmc_data));
192
193         data.timeout_ns = card->csd.tacc_ns * 100;
194         data.timeout_clks = card->csd.tacc_clks * 100;
195
196         timeout_us = data.timeout_ns / 1000;
197         timeout_us += data.timeout_clks * 1000 /
198                 (card->host->ios.clock / 1000);
199
200         if (timeout_us > 100000) {
201                 data.timeout_ns = 100000000;
202                 data.timeout_clks = 0;
203         }
204
205         data.blksz = 4;
206         data.blocks = 1;
207         data.flags = MMC_DATA_READ;
208         data.sg = &sg;
209         data.sg_len = 1;
210
211         memset(&mrq, 0, sizeof(struct mmc_request));
212
213         mrq.cmd = &cmd;
214         mrq.data = &data;
215
216         blocks = kmalloc(4, GFP_KERNEL);
217         if (!blocks)
218                 return (u32)-1;
219
220         sg_init_one(&sg, blocks, 4);
221
222         mmc_wait_for_req(card->host, &mrq);
223
224         result = ntohl(*blocks);
225         kfree(blocks);
226
227         if (cmd.error || data.error)
228                 result = (u32)-1;
229
230         return result;
231 }
232
233 static u32 get_card_status(struct mmc_card *card, struct request *req)
234 {
235         struct mmc_command cmd;
236         int err;
237
238         memset(&cmd, 0, sizeof(struct mmc_command));
239         cmd.opcode = MMC_SEND_STATUS;
240         if (!mmc_host_is_spi(card->host))
241                 cmd.arg = card->rca << 16;
242         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
243         err = mmc_wait_for_cmd(card->host, &cmd, 0);
244         if (err)
245                 printk(KERN_ERR "%s: error %d sending status comand",
246                        req->rq_disk->disk_name, err);
247         return cmd.resp[0];
248 }
249
250 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
251 {
252         struct mmc_blk_data *md = mq->data;
253         struct mmc_card *card = md->queue.card;
254         struct mmc_blk_request brq;
255         int ret = 1, disable_multi = 0;
256
257         mmc_claim_host(card->host);
258
259         do {
260                 struct mmc_command cmd;
261                 u32 readcmd, writecmd, status = 0;
262
263                 memset(&brq, 0, sizeof(struct mmc_blk_request));
264                 brq.mrq.cmd = &brq.cmd;
265                 brq.mrq.data = &brq.data;
266
267                 brq.cmd.arg = blk_rq_pos(req);
268                 if (!mmc_card_blockaddr(card))
269                         brq.cmd.arg <<= 9;
270                 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
271                 brq.data.blksz = 512;
272                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
273                 brq.stop.arg = 0;
274                 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
275                 brq.data.blocks = blk_rq_sectors(req);
276
277                 /*
278                  * The block layer doesn't support all sector count
279                  * restrictions, so we need to be prepared for too big
280                  * requests.
281                  */
282                 if (brq.data.blocks > card->host->max_blk_count)
283                         brq.data.blocks = card->host->max_blk_count;
284
285                 /*
286                  * After a read error, we redo the request one sector at a time
287                  * in order to accurately determine which sectors can be read
288                  * successfully.
289                  */
290                 if (disable_multi && brq.data.blocks > 1)
291                         brq.data.blocks = 1;
292
293                 if (brq.data.blocks > 1) {
294                         /* SPI multiblock writes terminate using a special
295                          * token, not a STOP_TRANSMISSION request.
296                          */
297                         if (!mmc_host_is_spi(card->host)
298                                         || rq_data_dir(req) == READ)
299                                 brq.mrq.stop = &brq.stop;
300                         readcmd = MMC_READ_MULTIPLE_BLOCK;
301                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
302                 } else {
303                         brq.mrq.stop = NULL;
304                         readcmd = MMC_READ_SINGLE_BLOCK;
305                         writecmd = MMC_WRITE_BLOCK;
306                 }
307
308                 if (rq_data_dir(req) == READ) {
309                         brq.cmd.opcode = readcmd;
310                         brq.data.flags |= MMC_DATA_READ;
311                 } else {
312                         brq.cmd.opcode = writecmd;
313                         brq.data.flags |= MMC_DATA_WRITE;
314                 }
315
316                 mmc_set_data_timeout(&brq.data, card);
317
318                 brq.data.sg = mq->sg;
319                 brq.data.sg_len = mmc_queue_map_sg(mq);
320
321                 /*
322                  * Adjust the sg list so it is the same size as the
323                  * request.
324                  */
325                 if (brq.data.blocks != blk_rq_sectors(req)) {
326                         int i, data_size = brq.data.blocks << 9;
327                         struct scatterlist *sg;
328
329                         for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
330                                 data_size -= sg->length;
331                                 if (data_size <= 0) {
332                                         sg->length += data_size;
333                                         i++;
334                                         break;
335                                 }
336                         }
337                         brq.data.sg_len = i;
338                 }
339
340                 mmc_queue_bounce_pre(mq);
341
342                 mmc_wait_for_req(card->host, &brq.mrq);
343
344                 mmc_queue_bounce_post(mq);
345
346                 /*
347                  * Check for errors here, but don't jump to cmd_err
348                  * until later as we need to wait for the card to leave
349                  * programming mode even when things go wrong.
350                  */
351                 if (brq.cmd.error || brq.data.error || brq.stop.error) {
352                         if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
353                                 /* Redo read one sector at a time */
354                                 printk(KERN_WARNING "%s: retrying using single "
355                                        "block read\n", req->rq_disk->disk_name);
356                                 disable_multi = 1;
357                                 continue;
358                         }
359                         status = get_card_status(card, req);
360                 }
361
362                 if (brq.cmd.error) {
363                         printk(KERN_ERR "%s: error %d sending read/write "
364                                "command, response %#x, card status %#x\n",
365                                req->rq_disk->disk_name, brq.cmd.error,
366                                brq.cmd.resp[0], status);
367                 }
368
369                 if (brq.data.error) {
370                         if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
371                                 /* 'Stop' response contains card status */
372                                 status = brq.mrq.stop->resp[0];
373                         printk(KERN_ERR "%s: error %d transferring data,"
374                                " sector %u, nr %u, card status %#x\n",
375                                req->rq_disk->disk_name, brq.data.error,
376                                (unsigned)blk_rq_pos(req),
377                                (unsigned)blk_rq_sectors(req), status);
378                 }
379
380                 if (brq.stop.error) {
381                         printk(KERN_ERR "%s: error %d sending stop command, "
382                                "response %#x, card status %#x\n",
383                                req->rq_disk->disk_name, brq.stop.error,
384                                brq.stop.resp[0], status);
385                 }
386
387                 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
388                         do {
389                                 int err;
390
391                                 cmd.opcode = MMC_SEND_STATUS;
392                                 cmd.arg = card->rca << 16;
393                                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
394                                 err = mmc_wait_for_cmd(card->host, &cmd, 5);
395                                 if (err) {
396                                         printk(KERN_ERR "%s: error %d requesting status\n",
397                                                req->rq_disk->disk_name, err);
398                                         goto cmd_err;
399                                 }
400                                 /*
401                                  * Some cards mishandle the status bits,
402                                  * so make sure to check both the busy
403                                  * indication and the card state.
404                                  */
405                         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
406                                 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
407
408 #if 0
409                         if (cmd.resp[0] & ~0x00000900)
410                                 printk(KERN_ERR "%s: status = %08x\n",
411                                        req->rq_disk->disk_name, cmd.resp[0]);
412                         if (mmc_decode_status(cmd.resp))
413                                 goto cmd_err;
414 #endif
415                 }
416
417                 if (brq.cmd.error || brq.stop.error || brq.data.error) {
418                         if (rq_data_dir(req) == READ) {
419                                 /*
420                                  * After an error, we redo I/O one sector at a
421                                  * time, so we only reach here after trying to
422                                  * read a single sector.
423                                  */
424                                 spin_lock_irq(&md->lock);
425                                 ret = __blk_end_request(req, -EIO, brq.data.blksz);
426                                 spin_unlock_irq(&md->lock);
427                                 continue;
428                         }
429                         goto cmd_err;
430                 }
431
432                 /*
433                  * A block was successfully transferred.
434                  */
435                 spin_lock_irq(&md->lock);
436                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
437                 spin_unlock_irq(&md->lock);
438         } while (ret);
439
440         mmc_release_host(card->host);
441
442         return 1;
443
444  cmd_err:
445         /*
446          * If this is an SD card and we're writing, we can first
447          * mark the known good sectors as ok.
448          *
449          * If the card is not SD, we can still ok written sectors
450          * as reported by the controller (which might be less than
451          * the real number of written sectors, but never more).
452          */
453         if (mmc_card_sd(card)) {
454                 u32 blocks;
455
456                 blocks = mmc_sd_num_wr_blocks(card);
457                 if (blocks != (u32)-1) {
458                         spin_lock_irq(&md->lock);
459                         ret = __blk_end_request(req, 0, blocks << 9);
460                         spin_unlock_irq(&md->lock);
461                 }
462         } else {
463                 spin_lock_irq(&md->lock);
464                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
465                 spin_unlock_irq(&md->lock);
466         }
467
468         mmc_release_host(card->host);
469
470         spin_lock_irq(&md->lock);
471         while (ret)
472                 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
473         spin_unlock_irq(&md->lock);
474
475         return 0;
476 }
477
478
479 static inline int mmc_blk_readonly(struct mmc_card *card)
480 {
481         return mmc_card_readonly(card) ||
482                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
483 }
484
485 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
486 {
487         struct mmc_blk_data *md;
488         int devidx, ret;
489
490         devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
491         if (devidx >= MMC_NUM_MINORS)
492                 return ERR_PTR(-ENOSPC);
493         __set_bit(devidx, dev_use);
494
495         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
496         if (!md) {
497                 ret = -ENOMEM;
498                 goto out;
499         }
500
501
502         /*
503          * Set the read-only status based on the supported commands
504          * and the write protect switch.
505          */
506         md->read_only = mmc_blk_readonly(card);
507
508         md->disk = alloc_disk(1 << MMC_SHIFT);
509         if (md->disk == NULL) {
510                 ret = -ENOMEM;
511                 goto err_kfree;
512         }
513
514         spin_lock_init(&md->lock);
515         md->usage = 1;
516
517         ret = mmc_init_queue(&md->queue, card, &md->lock);
518         if (ret)
519                 goto err_putdisk;
520
521         md->queue.issue_fn = mmc_blk_issue_rq;
522         md->queue.data = md;
523
524         md->disk->major = MMC_BLOCK_MAJOR;
525         md->disk->first_minor = devidx << MMC_SHIFT;
526         md->disk->fops = &mmc_bdops;
527         md->disk->private_data = md;
528         md->disk->queue = md->queue.queue;
529         md->disk->driverfs_dev = &card->dev;
530
531         /*
532          * As discussed on lkml, GENHD_FL_REMOVABLE should:
533          *
534          * - be set for removable media with permanent block devices
535          * - be unset for removable block devices with permanent media
536          *
537          * Since MMC block devices clearly fall under the second
538          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
539          * should use the block device creation/destruction hotplug
540          * messages to tell when the card is present.
541          */
542
543         sprintf(md->disk->disk_name, "mmcblk%d", devidx);
544
545         blk_queue_logical_block_size(md->queue.queue, 512);
546
547         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
548                 /*
549                  * The EXT_CSD sector count is in number or 512 byte
550                  * sectors.
551                  */
552                 set_capacity(md->disk, card->ext_csd.sectors);
553         } else {
554                 /*
555                  * The CSD capacity field is in units of read_blkbits.
556                  * set_capacity takes units of 512 bytes.
557                  */
558                 set_capacity(md->disk,
559                         card->csd.capacity << (card->csd.read_blkbits - 9));
560         }
561         return md;
562
563  err_putdisk:
564         put_disk(md->disk);
565  err_kfree:
566         kfree(md);
567  out:
568         return ERR_PTR(ret);
569 }
570
571 static int
572 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
573 {
574         struct mmc_command cmd;
575         int err;
576
577         /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
578         if (mmc_card_blockaddr(card))
579                 return 0;
580
581         mmc_claim_host(card->host);
582         cmd.opcode = MMC_SET_BLOCKLEN;
583         cmd.arg = 512;
584         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
585         err = mmc_wait_for_cmd(card->host, &cmd, 5);
586         mmc_release_host(card->host);
587
588         if (err) {
589                 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
590                         md->disk->disk_name, cmd.arg, err);
591                 return -EINVAL;
592         }
593
594         return 0;
595 }
596
597 static int mmc_blk_probe(struct mmc_card *card)
598 {
599         struct mmc_blk_data *md;
600         int err;
601
602         char cap_str[10];
603
604         /*
605          * Check that the card supports the command class(es) we need.
606          */
607         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
608                 return -ENODEV;
609
610         md = mmc_blk_alloc(card);
611         if (IS_ERR(md))
612                 return PTR_ERR(md);
613
614         err = mmc_blk_set_blksize(md, card);
615         if (err)
616                 goto out;
617
618         string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
619                         cap_str, sizeof(cap_str));
620         printk(KERN_INFO "%s: %s %s %s %s\n",
621                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
622                 cap_str, md->read_only ? "(ro)" : "");
623
624         mmc_set_drvdata(card, md);
625         add_disk(md->disk);
626         return 0;
627
628  out:
629         mmc_cleanup_queue(&md->queue);
630         mmc_blk_put(md);
631
632         return err;
633 }
634
635 static void mmc_blk_remove(struct mmc_card *card)
636 {
637         struct mmc_blk_data *md = mmc_get_drvdata(card);
638
639         if (md) {
640                 /* Stop new requests from getting into the queue */
641                 del_gendisk(md->disk);
642
643                 /* Then flush out any already in there */
644                 mmc_cleanup_queue(&md->queue);
645
646                 mmc_blk_put(md);
647         }
648         mmc_set_drvdata(card, NULL);
649 }
650
651 #ifdef CONFIG_PM
652 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
653 {
654         struct mmc_blk_data *md = mmc_get_drvdata(card);
655
656         if (md) {
657                 mmc_queue_suspend(&md->queue);
658         }
659         return 0;
660 }
661
662 static int mmc_blk_resume(struct mmc_card *card)
663 {
664         struct mmc_blk_data *md = mmc_get_drvdata(card);
665
666         if (md) {
667                 mmc_blk_set_blksize(md, card);
668                 mmc_queue_resume(&md->queue);
669         }
670         return 0;
671 }
672 #else
673 #define mmc_blk_suspend NULL
674 #define mmc_blk_resume  NULL
675 #endif
676
677 static struct mmc_driver mmc_driver = {
678         .drv            = {
679                 .name   = "mmcblk",
680         },
681         .probe          = mmc_blk_probe,
682         .remove         = mmc_blk_remove,
683         .suspend        = mmc_blk_suspend,
684         .resume         = mmc_blk_resume,
685 };
686
687 static int __init mmc_blk_init(void)
688 {
689         int res;
690
691         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
692         if (res)
693                 goto out;
694
695         res = mmc_register_driver(&mmc_driver);
696         if (res)
697                 goto out2;
698
699         return 0;
700  out2:
701         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
702  out:
703         return res;
704 }
705
706 static void __exit mmc_blk_exit(void)
707 {
708         mmc_unregister_driver(&mmc_driver);
709         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
710 }
711
712 module_init(mmc_blk_init);
713 module_exit(mmc_blk_exit);
714
715 MODULE_LICENSE("GPL");
716 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
717