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