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