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