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