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