]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/card/block.c
mmc: remove confusing flag
[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 || !(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 || data.error)
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                 /*
233                  * If the host doesn't support multiple block writes, force
234                  * block writes to single block. SD cards are excepted from
235                  * this rule as they support querying the number of
236                  * successfully written sectors.
237                  */
238                 if (rq_data_dir(req) != READ &&
239                     !(card->host->caps & MMC_CAP_MULTIWRITE) &&
240                     !mmc_card_sd(card))
241                         brq.data.blocks = 1;
242
243                 if (brq.data.blocks > 1) {
244                         brq.mrq.stop = &brq.stop;
245                         readcmd = MMC_READ_MULTIPLE_BLOCK;
246                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
247                 } else {
248                         brq.mrq.stop = NULL;
249                         readcmd = MMC_READ_SINGLE_BLOCK;
250                         writecmd = MMC_WRITE_BLOCK;
251                 }
252
253                 if (rq_data_dir(req) == READ) {
254                         brq.cmd.opcode = readcmd;
255                         brq.data.flags |= MMC_DATA_READ;
256                 } else {
257                         brq.cmd.opcode = writecmd;
258                         brq.data.flags |= MMC_DATA_WRITE;
259                 }
260
261                 mmc_set_data_timeout(&brq.data, card);
262
263                 brq.data.sg = mq->sg;
264                 brq.data.sg_len = mmc_queue_map_sg(mq);
265
266                 mmc_queue_bounce_pre(mq);
267
268                 if (brq.data.blocks !=
269                     (req->nr_sectors >> (md->block_bits - 9))) {
270                         data_size = brq.data.blocks * brq.data.blksz;
271                         for (sg_pos = 0; sg_pos < brq.data.sg_len; sg_pos++) {
272                                 data_size -= mq->sg[sg_pos].length;
273                                 if (data_size <= 0) {
274                                         mq->sg[sg_pos].length += data_size;
275                                         sg_pos++;
276                                         break;
277                                 }
278                         }
279                         brq.data.sg_len = sg_pos;
280                 }
281
282                 mmc_wait_for_req(card->host, &brq.mrq);
283
284                 mmc_queue_bounce_post(mq);
285
286                 if (brq.cmd.error) {
287                         printk(KERN_ERR "%s: error %d sending read/write command\n",
288                                req->rq_disk->disk_name, brq.cmd.error);
289                         goto cmd_err;
290                 }
291
292                 if (brq.data.error) {
293                         printk(KERN_ERR "%s: error %d transferring data\n",
294                                req->rq_disk->disk_name, brq.data.error);
295                         goto cmd_err;
296                 }
297
298                 if (brq.stop.error) {
299                         printk(KERN_ERR "%s: error %d sending stop command\n",
300                                req->rq_disk->disk_name, brq.stop.error);
301                         goto cmd_err;
302                 }
303
304                 if (rq_data_dir(req) != READ) {
305                         do {
306                                 int err;
307
308                                 cmd.opcode = MMC_SEND_STATUS;
309                                 cmd.arg = card->rca << 16;
310                                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
311                                 err = mmc_wait_for_cmd(card->host, &cmd, 5);
312                                 if (err) {
313                                         printk(KERN_ERR "%s: error %d requesting status\n",
314                                                req->rq_disk->disk_name, err);
315                                         goto cmd_err;
316                                 }
317                         } while (!(cmd.resp[0] & R1_READY_FOR_DATA));
318
319 #if 0
320                         if (cmd.resp[0] & ~0x00000900)
321                                 printk(KERN_ERR "%s: status = %08x\n",
322                                        req->rq_disk->disk_name, cmd.resp[0]);
323                         if (mmc_decode_status(cmd.resp))
324                                 goto cmd_err;
325 #endif
326                 }
327
328                 /*
329                  * A block was successfully transferred.
330                  */
331                 spin_lock_irq(&md->lock);
332                 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
333                 if (!ret) {
334                         /*
335                          * The whole request completed successfully.
336                          */
337                         add_disk_randomness(req->rq_disk);
338                         blkdev_dequeue_request(req);
339                         end_that_request_last(req, 1);
340                 }
341                 spin_unlock_irq(&md->lock);
342         } while (ret);
343
344         mmc_release_host(card->host);
345
346         return 1;
347
348  cmd_err:
349         /*
350          * If this is an SD card and we're writing, we can first
351          * mark the known good sectors as ok.
352          *
353          * If the card is not SD, we can still ok written sectors
354          * if the controller can do proper error reporting.
355          *
356          * For reads we just fail the entire chunk as that should
357          * be safe in all cases.
358          */
359         if (rq_data_dir(req) != READ && mmc_card_sd(card)) {
360                 u32 blocks;
361                 unsigned int bytes;
362
363                 blocks = mmc_sd_num_wr_blocks(card);
364                 if (blocks != (u32)-1) {
365                         if (card->csd.write_partial)
366                                 bytes = blocks << md->block_bits;
367                         else
368                                 bytes = blocks << 9;
369                         spin_lock_irq(&md->lock);
370                         ret = end_that_request_chunk(req, 1, bytes);
371                         spin_unlock_irq(&md->lock);
372                 }
373         } else if (rq_data_dir(req) != READ &&
374                    (card->host->caps & MMC_CAP_MULTIWRITE)) {
375                 spin_lock_irq(&md->lock);
376                 ret = end_that_request_chunk(req, 1, brq.data.bytes_xfered);
377                 spin_unlock_irq(&md->lock);
378         }
379
380         mmc_release_host(card->host);
381
382         spin_lock_irq(&md->lock);
383         while (ret) {
384                 ret = end_that_request_chunk(req, 0,
385                                 req->current_nr_sectors << 9);
386         }
387
388         add_disk_randomness(req->rq_disk);
389         blkdev_dequeue_request(req);
390         end_that_request_last(req, 0);
391         spin_unlock_irq(&md->lock);
392
393         return 0;
394 }
395
396 #define MMC_NUM_MINORS  (256 >> MMC_SHIFT)
397
398 static unsigned long dev_use[MMC_NUM_MINORS/(8*sizeof(unsigned long))];
399
400 static inline int mmc_blk_readonly(struct mmc_card *card)
401 {
402         return mmc_card_readonly(card) ||
403                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
404 }
405
406 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
407 {
408         struct mmc_blk_data *md;
409         int devidx, ret;
410
411         devidx = find_first_zero_bit(dev_use, MMC_NUM_MINORS);
412         if (devidx >= MMC_NUM_MINORS)
413                 return ERR_PTR(-ENOSPC);
414         __set_bit(devidx, dev_use);
415
416         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
417         if (!md) {
418                 ret = -ENOMEM;
419                 goto out;
420         }
421
422
423         /*
424          * Set the read-only status based on the supported commands
425          * and the write protect switch.
426          */
427         md->read_only = mmc_blk_readonly(card);
428
429         /*
430          * Both SD and MMC specifications state (although a bit
431          * unclearly in the MMC case) that a block size of 512
432          * bytes must always be supported by the card.
433          */
434         md->block_bits = 9;
435
436         md->disk = alloc_disk(1 << MMC_SHIFT);
437         if (md->disk == NULL) {
438                 ret = -ENOMEM;
439                 goto err_kfree;
440         }
441
442         spin_lock_init(&md->lock);
443         md->usage = 1;
444
445         ret = mmc_init_queue(&md->queue, card, &md->lock);
446         if (ret)
447                 goto err_putdisk;
448
449         md->queue.issue_fn = mmc_blk_issue_rq;
450         md->queue.data = md;
451
452         md->disk->major = MMC_BLOCK_MAJOR;
453         md->disk->first_minor = devidx << MMC_SHIFT;
454         md->disk->fops = &mmc_bdops;
455         md->disk->private_data = md;
456         md->disk->queue = md->queue.queue;
457         md->disk->driverfs_dev = &card->dev;
458
459         /*
460          * As discussed on lkml, GENHD_FL_REMOVABLE should:
461          *
462          * - be set for removable media with permanent block devices
463          * - be unset for removable block devices with permanent media
464          *
465          * Since MMC block devices clearly fall under the second
466          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
467          * should use the block device creation/destruction hotplug
468          * messages to tell when the card is present.
469          */
470
471         sprintf(md->disk->disk_name, "mmcblk%d", devidx);
472
473         blk_queue_hardsect_size(md->queue.queue, 1 << md->block_bits);
474
475         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
476                 /*
477                  * The EXT_CSD sector count is in number or 512 byte
478                  * sectors.
479                  */
480                 set_capacity(md->disk, card->ext_csd.sectors);
481         } else {
482                 /*
483                  * The CSD capacity field is in units of read_blkbits.
484                  * set_capacity takes units of 512 bytes.
485                  */
486                 set_capacity(md->disk,
487                         card->csd.capacity << (card->csd.read_blkbits - 9));
488         }
489         return md;
490
491  err_putdisk:
492         put_disk(md->disk);
493  err_kfree:
494         kfree(md);
495  out:
496         return ERR_PTR(ret);
497 }
498
499 static int
500 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
501 {
502         struct mmc_command cmd;
503         int err;
504
505         /* Block-addressed cards ignore MMC_SET_BLOCKLEN. */
506         if (mmc_card_blockaddr(card))
507                 return 0;
508
509         mmc_claim_host(card->host);
510         cmd.opcode = MMC_SET_BLOCKLEN;
511         cmd.arg = 1 << md->block_bits;
512         cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
513         err = mmc_wait_for_cmd(card->host, &cmd, 5);
514         mmc_release_host(card->host);
515
516         if (err) {
517                 printk(KERN_ERR "%s: unable to set block size to %d: %d\n",
518                         md->disk->disk_name, cmd.arg, err);
519                 return -EINVAL;
520         }
521
522         return 0;
523 }
524
525 static int mmc_blk_probe(struct mmc_card *card)
526 {
527         struct mmc_blk_data *md;
528         int err;
529
530         /*
531          * Check that the card supports the command class(es) we need.
532          */
533         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
534                 return -ENODEV;
535
536         md = mmc_blk_alloc(card);
537         if (IS_ERR(md))
538                 return PTR_ERR(md);
539
540         err = mmc_blk_set_blksize(md, card);
541         if (err)
542                 goto out;
543
544         printk(KERN_INFO "%s: %s %s %lluKiB %s\n",
545                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
546                 (unsigned long long)(get_capacity(md->disk) >> 1),
547                 md->read_only ? "(ro)" : "");
548
549         mmc_set_drvdata(card, md);
550         add_disk(md->disk);
551         return 0;
552
553  out:
554         mmc_blk_put(md);
555
556         return err;
557 }
558
559 static void mmc_blk_remove(struct mmc_card *card)
560 {
561         struct mmc_blk_data *md = mmc_get_drvdata(card);
562
563         if (md) {
564                 int devidx;
565
566                 /* Stop new requests from getting into the queue */
567                 del_gendisk(md->disk);
568
569                 /* Then flush out any already in there */
570                 mmc_cleanup_queue(&md->queue);
571
572                 devidx = md->disk->first_minor >> MMC_SHIFT;
573                 __clear_bit(devidx, dev_use);
574
575                 mmc_blk_put(md);
576         }
577         mmc_set_drvdata(card, NULL);
578 }
579
580 #ifdef CONFIG_PM
581 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
582 {
583         struct mmc_blk_data *md = mmc_get_drvdata(card);
584
585         if (md) {
586                 mmc_queue_suspend(&md->queue);
587         }
588         return 0;
589 }
590
591 static int mmc_blk_resume(struct mmc_card *card)
592 {
593         struct mmc_blk_data *md = mmc_get_drvdata(card);
594
595         if (md) {
596                 mmc_blk_set_blksize(md, card);
597                 mmc_queue_resume(&md->queue);
598         }
599         return 0;
600 }
601 #else
602 #define mmc_blk_suspend NULL
603 #define mmc_blk_resume  NULL
604 #endif
605
606 static struct mmc_driver mmc_driver = {
607         .drv            = {
608                 .name   = "mmcblk",
609         },
610         .probe          = mmc_blk_probe,
611         .remove         = mmc_blk_remove,
612         .suspend        = mmc_blk_suspend,
613         .resume         = mmc_blk_resume,
614 };
615
616 static int __init mmc_blk_init(void)
617 {
618         int res = -ENOMEM;
619
620         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
621         if (res)
622                 goto out;
623
624         return mmc_register_driver(&mmc_driver);
625
626  out:
627         return res;
628 }
629
630 static void __exit mmc_blk_exit(void)
631 {
632         mmc_unregister_driver(&mmc_driver);
633         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
634 }
635
636 module_init(mmc_blk_init);
637 module_exit(mmc_blk_exit);
638
639 MODULE_LICENSE("GPL");
640 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
641