126c7f41c5a33a9702185b0b947f1dadf1fbef7e
[platform/adaptation/renesas_rcar/renesas_kernel.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 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
37
38 #include <linux/mmc/ioctl.h>
39 #include <linux/mmc/card.h>
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/mmc.h>
42 #include <linux/mmc/sd.h>
43
44 #include <asm/system.h>
45 #include <asm/uaccess.h>
46
47 #include "queue.h"
48
49 MODULE_ALIAS("mmc:block");
50 #ifdef MODULE_PARAM_PREFIX
51 #undef MODULE_PARAM_PREFIX
52 #endif
53 #define MODULE_PARAM_PREFIX "mmcblk."
54
55 #define INAND_CMD38_ARG_EXT_CSD  113
56 #define INAND_CMD38_ARG_ERASE    0x00
57 #define INAND_CMD38_ARG_TRIM     0x01
58 #define INAND_CMD38_ARG_SECERASE 0x80
59 #define INAND_CMD38_ARG_SECTRIM1 0x81
60 #define INAND_CMD38_ARG_SECTRIM2 0x88
61
62 #define REL_WRITES_SUPPORTED(card) (mmc_card_mmc((card)) &&     \
63     (((card)->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||   \
64      ((card)->ext_csd.rel_sectors)))
65
66 static DEFINE_MUTEX(block_mutex);
67
68 /*
69  * The defaults come from config options but can be overriden by module
70  * or bootarg options.
71  */
72 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
73
74 /*
75  * We've only got one major, so number of mmcblk devices is
76  * limited to 256 / number of minors per device.
77  */
78 static int max_devices;
79
80 /* 256 minors, so at most 256 separate devices */
81 static DECLARE_BITMAP(dev_use, 256);
82 static DECLARE_BITMAP(name_use, 256);
83
84 /*
85  * There is one mmc_blk_data per slot.
86  */
87 struct mmc_blk_data {
88         spinlock_t      lock;
89         struct gendisk  *disk;
90         struct mmc_queue queue;
91         struct list_head part;
92
93         unsigned int    usage;
94         unsigned int    read_only;
95         unsigned int    part_type;
96         unsigned int    name_idx;
97
98         /*
99          * Only set in main mmc_blk_data associated
100          * with mmc_card with mmc_set_drvdata, and keeps
101          * track of the current selected device partition.
102          */
103         unsigned int    part_curr;
104         struct device_attribute force_ro;
105 };
106
107 static DEFINE_MUTEX(open_lock);
108
109 module_param(perdev_minors, int, 0444);
110 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
111
112 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
113 {
114         struct mmc_blk_data *md;
115
116         mutex_lock(&open_lock);
117         md = disk->private_data;
118         if (md && md->usage == 0)
119                 md = NULL;
120         if (md)
121                 md->usage++;
122         mutex_unlock(&open_lock);
123
124         return md;
125 }
126
127 static inline int mmc_get_devidx(struct gendisk *disk)
128 {
129         int devmaj = MAJOR(disk_devt(disk));
130         int devidx = MINOR(disk_devt(disk)) / perdev_minors;
131
132         if (!devmaj)
133                 devidx = disk->first_minor / perdev_minors;
134         return devidx;
135 }
136
137 static void mmc_blk_put(struct mmc_blk_data *md)
138 {
139         mutex_lock(&open_lock);
140         md->usage--;
141         if (md->usage == 0) {
142                 int devidx = mmc_get_devidx(md->disk);
143                 blk_cleanup_queue(md->queue.queue);
144
145                 __clear_bit(devidx, dev_use);
146
147                 put_disk(md->disk);
148                 kfree(md);
149         }
150         mutex_unlock(&open_lock);
151 }
152
153 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
154                              char *buf)
155 {
156         int ret;
157         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
158
159         ret = snprintf(buf, PAGE_SIZE, "%d",
160                        get_disk_ro(dev_to_disk(dev)) ^
161                        md->read_only);
162         mmc_blk_put(md);
163         return ret;
164 }
165
166 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
167                               const char *buf, size_t count)
168 {
169         int ret;
170         char *end;
171         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172         unsigned long set = simple_strtoul(buf, &end, 0);
173         if (end == buf) {
174                 ret = -EINVAL;
175                 goto out;
176         }
177
178         set_disk_ro(dev_to_disk(dev), set || md->read_only);
179         ret = count;
180 out:
181         mmc_blk_put(md);
182         return ret;
183 }
184
185 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
186 {
187         struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
188         int ret = -ENXIO;
189
190         mutex_lock(&block_mutex);
191         if (md) {
192                 if (md->usage == 2)
193                         check_disk_change(bdev);
194                 ret = 0;
195
196                 if ((mode & FMODE_WRITE) && md->read_only) {
197                         mmc_blk_put(md);
198                         ret = -EROFS;
199                 }
200         }
201         mutex_unlock(&block_mutex);
202
203         return ret;
204 }
205
206 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
207 {
208         struct mmc_blk_data *md = disk->private_data;
209
210         mutex_lock(&block_mutex);
211         mmc_blk_put(md);
212         mutex_unlock(&block_mutex);
213         return 0;
214 }
215
216 static int
217 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
218 {
219         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
220         geo->heads = 4;
221         geo->sectors = 16;
222         return 0;
223 }
224
225 struct mmc_blk_ioc_data {
226         struct mmc_ioc_cmd ic;
227         unsigned char *buf;
228         u64 buf_bytes;
229 };
230
231 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
232         struct mmc_ioc_cmd __user *user)
233 {
234         struct mmc_blk_ioc_data *idata;
235         int err;
236
237         idata = kzalloc(sizeof(*idata), GFP_KERNEL);
238         if (!idata) {
239                 err = -ENOMEM;
240                 goto out;
241         }
242
243         if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
244                 err = -EFAULT;
245                 goto idata_err;
246         }
247
248         idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
249         if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
250                 err = -EOVERFLOW;
251                 goto idata_err;
252         }
253
254         idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
255         if (!idata->buf) {
256                 err = -ENOMEM;
257                 goto idata_err;
258         }
259
260         if (copy_from_user(idata->buf, (void __user *)(unsigned long)
261                                         idata->ic.data_ptr, idata->buf_bytes)) {
262                 err = -EFAULT;
263                 goto copy_err;
264         }
265
266         return idata;
267
268 copy_err:
269         kfree(idata->buf);
270 idata_err:
271         kfree(idata);
272 out:
273         return ERR_PTR(err);
274 }
275
276 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
277         struct mmc_ioc_cmd __user *ic_ptr)
278 {
279         struct mmc_blk_ioc_data *idata;
280         struct mmc_blk_data *md;
281         struct mmc_card *card;
282         struct mmc_command cmd = {0};
283         struct mmc_data data = {0};
284         struct mmc_request mrq = {0};
285         struct scatterlist sg;
286         int err;
287
288         /*
289          * The caller must have CAP_SYS_RAWIO, and must be calling this on the
290          * whole block device, not on a partition.  This prevents overspray
291          * between sibling partitions.
292          */
293         if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
294                 return -EPERM;
295
296         idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
297         if (IS_ERR(idata))
298                 return PTR_ERR(idata);
299
300         cmd.opcode = idata->ic.opcode;
301         cmd.arg = idata->ic.arg;
302         cmd.flags = idata->ic.flags;
303
304         data.sg = &sg;
305         data.sg_len = 1;
306         data.blksz = idata->ic.blksz;
307         data.blocks = idata->ic.blocks;
308
309         sg_init_one(data.sg, idata->buf, idata->buf_bytes);
310
311         if (idata->ic.write_flag)
312                 data.flags = MMC_DATA_WRITE;
313         else
314                 data.flags = MMC_DATA_READ;
315
316         mrq.cmd = &cmd;
317         mrq.data = &data;
318
319         md = mmc_blk_get(bdev->bd_disk);
320         if (!md) {
321                 err = -EINVAL;
322                 goto cmd_done;
323         }
324
325         card = md->queue.card;
326         if (IS_ERR(card)) {
327                 err = PTR_ERR(card);
328                 goto cmd_done;
329         }
330
331         mmc_claim_host(card->host);
332
333         if (idata->ic.is_acmd) {
334                 err = mmc_app_cmd(card->host, card);
335                 if (err)
336                         goto cmd_rel_host;
337         }
338
339         /* data.flags must already be set before doing this. */
340         mmc_set_data_timeout(&data, card);
341         /* Allow overriding the timeout_ns for empirical tuning. */
342         if (idata->ic.data_timeout_ns)
343                 data.timeout_ns = idata->ic.data_timeout_ns;
344
345         if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
346                 /*
347                  * Pretend this is a data transfer and rely on the host driver
348                  * to compute timeout.  When all host drivers support
349                  * cmd.cmd_timeout for R1B, this can be changed to:
350                  *
351                  *     mrq.data = NULL;
352                  *     cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
353                  */
354                 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
355         }
356
357         mmc_wait_for_req(card->host, &mrq);
358
359         if (cmd.error) {
360                 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
361                                                 __func__, cmd.error);
362                 err = cmd.error;
363                 goto cmd_rel_host;
364         }
365         if (data.error) {
366                 dev_err(mmc_dev(card->host), "%s: data error %d\n",
367                                                 __func__, data.error);
368                 err = data.error;
369                 goto cmd_rel_host;
370         }
371
372         /*
373          * According to the SD specs, some commands require a delay after
374          * issuing the command.
375          */
376         if (idata->ic.postsleep_min_us)
377                 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
378
379         if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
380                 err = -EFAULT;
381                 goto cmd_rel_host;
382         }
383
384         if (!idata->ic.write_flag) {
385                 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
386                                                 idata->buf, idata->buf_bytes)) {
387                         err = -EFAULT;
388                         goto cmd_rel_host;
389                 }
390         }
391
392 cmd_rel_host:
393         mmc_release_host(card->host);
394
395 cmd_done:
396         mmc_blk_put(md);
397         kfree(idata->buf);
398         kfree(idata);
399         return err;
400 }
401
402 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
403         unsigned int cmd, unsigned long arg)
404 {
405         int ret = -EINVAL;
406         if (cmd == MMC_IOC_CMD)
407                 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
408         return ret;
409 }
410
411 #ifdef CONFIG_COMPAT
412 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
413         unsigned int cmd, unsigned long arg)
414 {
415         return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
416 }
417 #endif
418
419 static const struct block_device_operations mmc_bdops = {
420         .open                   = mmc_blk_open,
421         .release                = mmc_blk_release,
422         .getgeo                 = mmc_blk_getgeo,
423         .owner                  = THIS_MODULE,
424         .ioctl                  = mmc_blk_ioctl,
425 #ifdef CONFIG_COMPAT
426         .compat_ioctl           = mmc_blk_compat_ioctl,
427 #endif
428 };
429
430 struct mmc_blk_request {
431         struct mmc_request      mrq;
432         struct mmc_command      cmd;
433         struct mmc_command      stop;
434         struct mmc_data         data;
435 };
436
437 static inline int mmc_blk_part_switch(struct mmc_card *card,
438                                       struct mmc_blk_data *md)
439 {
440         int ret;
441         struct mmc_blk_data *main_md = mmc_get_drvdata(card);
442         if (main_md->part_curr == md->part_type)
443                 return 0;
444
445         if (mmc_card_mmc(card)) {
446                 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
447                 card->ext_csd.part_config |= md->part_type;
448
449                 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
450                                  EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
451                                  card->ext_csd.part_time);
452                 if (ret)
453                         return ret;
454 }
455
456         main_md->part_curr = md->part_type;
457         return 0;
458 }
459
460 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
461 {
462         int err;
463         u32 result;
464         __be32 *blocks;
465
466         struct mmc_request mrq = {0};
467         struct mmc_command cmd = {0};
468         struct mmc_data data = {0};
469         unsigned int timeout_us;
470
471         struct scatterlist sg;
472
473         cmd.opcode = MMC_APP_CMD;
474         cmd.arg = card->rca << 16;
475         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
476
477         err = mmc_wait_for_cmd(card->host, &cmd, 0);
478         if (err)
479                 return (u32)-1;
480         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
481                 return (u32)-1;
482
483         memset(&cmd, 0, sizeof(struct mmc_command));
484
485         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
486         cmd.arg = 0;
487         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
488
489         data.timeout_ns = card->csd.tacc_ns * 100;
490         data.timeout_clks = card->csd.tacc_clks * 100;
491
492         timeout_us = data.timeout_ns / 1000;
493         timeout_us += data.timeout_clks * 1000 /
494                 (card->host->ios.clock / 1000);
495
496         if (timeout_us > 100000) {
497                 data.timeout_ns = 100000000;
498                 data.timeout_clks = 0;
499         }
500
501         data.blksz = 4;
502         data.blocks = 1;
503         data.flags = MMC_DATA_READ;
504         data.sg = &sg;
505         data.sg_len = 1;
506
507         mrq.cmd = &cmd;
508         mrq.data = &data;
509
510         blocks = kmalloc(4, GFP_KERNEL);
511         if (!blocks)
512                 return (u32)-1;
513
514         sg_init_one(&sg, blocks, 4);
515
516         mmc_wait_for_req(card->host, &mrq);
517
518         result = ntohl(*blocks);
519         kfree(blocks);
520
521         if (cmd.error || data.error)
522                 result = (u32)-1;
523
524         return result;
525 }
526
527 static u32 get_card_status(struct mmc_card *card, struct request *req)
528 {
529         struct mmc_command cmd = {0};
530         int err;
531
532         cmd.opcode = MMC_SEND_STATUS;
533         if (!mmc_host_is_spi(card->host))
534                 cmd.arg = card->rca << 16;
535         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
536         err = mmc_wait_for_cmd(card->host, &cmd, 0);
537         if (err)
538                 printk(KERN_ERR "%s: error %d sending status command",
539                        req->rq_disk->disk_name, err);
540         return cmd.resp[0];
541 }
542
543 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
544 {
545         struct mmc_blk_data *md = mq->data;
546         struct mmc_card *card = md->queue.card;
547         unsigned int from, nr, arg;
548         int err = 0;
549
550         if (!mmc_can_erase(card)) {
551                 err = -EOPNOTSUPP;
552                 goto out;
553         }
554
555         from = blk_rq_pos(req);
556         nr = blk_rq_sectors(req);
557
558         if (mmc_can_trim(card))
559                 arg = MMC_TRIM_ARG;
560         else
561                 arg = MMC_ERASE_ARG;
562
563         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
564                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
565                                  INAND_CMD38_ARG_EXT_CSD,
566                                  arg == MMC_TRIM_ARG ?
567                                  INAND_CMD38_ARG_TRIM :
568                                  INAND_CMD38_ARG_ERASE,
569                                  0);
570                 if (err)
571                         goto out;
572         }
573         err = mmc_erase(card, from, nr, arg);
574 out:
575         spin_lock_irq(&md->lock);
576         __blk_end_request(req, err, blk_rq_bytes(req));
577         spin_unlock_irq(&md->lock);
578
579         return err ? 0 : 1;
580 }
581
582 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
583                                        struct request *req)
584 {
585         struct mmc_blk_data *md = mq->data;
586         struct mmc_card *card = md->queue.card;
587         unsigned int from, nr, arg;
588         int err = 0;
589
590         if (!mmc_can_secure_erase_trim(card)) {
591                 err = -EOPNOTSUPP;
592                 goto out;
593         }
594
595         from = blk_rq_pos(req);
596         nr = blk_rq_sectors(req);
597
598         if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
599                 arg = MMC_SECURE_TRIM1_ARG;
600         else
601                 arg = MMC_SECURE_ERASE_ARG;
602
603         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
604                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
605                                  INAND_CMD38_ARG_EXT_CSD,
606                                  arg == MMC_SECURE_TRIM1_ARG ?
607                                  INAND_CMD38_ARG_SECTRIM1 :
608                                  INAND_CMD38_ARG_SECERASE,
609                                  0);
610                 if (err)
611                         goto out;
612         }
613         err = mmc_erase(card, from, nr, arg);
614         if (!err && arg == MMC_SECURE_TRIM1_ARG) {
615                 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
616                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
617                                          INAND_CMD38_ARG_EXT_CSD,
618                                          INAND_CMD38_ARG_SECTRIM2,
619                                          0);
620                         if (err)
621                                 goto out;
622                 }
623                 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
624         }
625 out:
626         spin_lock_irq(&md->lock);
627         __blk_end_request(req, err, blk_rq_bytes(req));
628         spin_unlock_irq(&md->lock);
629
630         return err ? 0 : 1;
631 }
632
633 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
634 {
635         struct mmc_blk_data *md = mq->data;
636
637         /*
638          * No-op, only service this because we need REQ_FUA for reliable
639          * writes.
640          */
641         spin_lock_irq(&md->lock);
642         __blk_end_request_all(req, 0);
643         spin_unlock_irq(&md->lock);
644
645         return 1;
646 }
647
648 /*
649  * Reformat current write as a reliable write, supporting
650  * both legacy and the enhanced reliable write MMC cards.
651  * In each transfer we'll handle only as much as a single
652  * reliable write can handle, thus finish the request in
653  * partial completions.
654  */
655 static inline int mmc_apply_rel_rw(struct mmc_blk_request *brq,
656                                    struct mmc_card *card,
657                                    struct request *req)
658 {
659         int err;
660         struct mmc_command set_count = {0};
661
662         if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
663                 /* Legacy mode imposes restrictions on transfers. */
664                 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
665                         brq->data.blocks = 1;
666
667                 if (brq->data.blocks > card->ext_csd.rel_sectors)
668                         brq->data.blocks = card->ext_csd.rel_sectors;
669                 else if (brq->data.blocks < card->ext_csd.rel_sectors)
670                         brq->data.blocks = 1;
671         }
672
673         set_count.opcode = MMC_SET_BLOCK_COUNT;
674         set_count.arg = brq->data.blocks | (1 << 31);
675         set_count.flags = MMC_RSP_R1 | MMC_CMD_AC;
676         err = mmc_wait_for_cmd(card->host, &set_count, 0);
677         if (err)
678                 printk(KERN_ERR "%s: error %d SET_BLOCK_COUNT\n",
679                        req->rq_disk->disk_name, err);
680         return err;
681 }
682
683 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
684 {
685         struct mmc_blk_data *md = mq->data;
686         struct mmc_card *card = md->queue.card;
687         struct mmc_blk_request brq;
688         int ret = 1, disable_multi = 0;
689
690         /*
691          * Reliable writes are used to implement Forced Unit Access and
692          * REQ_META accesses, and are supported only on MMCs.
693          */
694         bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
695                           (req->cmd_flags & REQ_META)) &&
696                 (rq_data_dir(req) == WRITE) &&
697                 REL_WRITES_SUPPORTED(card);
698
699         do {
700                 struct mmc_command cmd = {0};
701                 u32 readcmd, writecmd, status = 0;
702
703                 memset(&brq, 0, sizeof(struct mmc_blk_request));
704                 brq.mrq.cmd = &brq.cmd;
705                 brq.mrq.data = &brq.data;
706
707                 brq.cmd.arg = blk_rq_pos(req);
708                 if (!mmc_card_blockaddr(card))
709                         brq.cmd.arg <<= 9;
710                 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
711                 brq.data.blksz = 512;
712                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
713                 brq.stop.arg = 0;
714                 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
715                 brq.data.blocks = blk_rq_sectors(req);
716
717                 /*
718                  * The block layer doesn't support all sector count
719                  * restrictions, so we need to be prepared for too big
720                  * requests.
721                  */
722                 if (brq.data.blocks > card->host->max_blk_count)
723                         brq.data.blocks = card->host->max_blk_count;
724
725                 /*
726                  * After a read error, we redo the request one sector at a time
727                  * in order to accurately determine which sectors can be read
728                  * successfully.
729                  */
730                 if (disable_multi && brq.data.blocks > 1)
731                         brq.data.blocks = 1;
732
733                 if (brq.data.blocks > 1 || do_rel_wr) {
734                         /* SPI multiblock writes terminate using a special
735                          * token, not a STOP_TRANSMISSION request. Reliable
736                          * writes use SET_BLOCK_COUNT and do not use a
737                          * STOP_TRANSMISSION request either.
738                          */
739                         if ((!mmc_host_is_spi(card->host) && !do_rel_wr) ||
740                             rq_data_dir(req) == READ)
741                                 brq.mrq.stop = &brq.stop;
742                         readcmd = MMC_READ_MULTIPLE_BLOCK;
743                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
744                 } else {
745                         brq.mrq.stop = NULL;
746                         readcmd = MMC_READ_SINGLE_BLOCK;
747                         writecmd = MMC_WRITE_BLOCK;
748                 }
749                 if (rq_data_dir(req) == READ) {
750                         brq.cmd.opcode = readcmd;
751                         brq.data.flags |= MMC_DATA_READ;
752                 } else {
753                         brq.cmd.opcode = writecmd;
754                         brq.data.flags |= MMC_DATA_WRITE;
755                 }
756
757                 if (do_rel_wr && mmc_apply_rel_rw(&brq, card, req))
758                         goto cmd_err;
759
760                 mmc_set_data_timeout(&brq.data, card);
761
762                 brq.data.sg = mq->sg;
763                 brq.data.sg_len = mmc_queue_map_sg(mq);
764
765                 /*
766                  * Adjust the sg list so it is the same size as the
767                  * request.
768                  */
769                 if (brq.data.blocks != blk_rq_sectors(req)) {
770                         int i, data_size = brq.data.blocks << 9;
771                         struct scatterlist *sg;
772
773                         for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
774                                 data_size -= sg->length;
775                                 if (data_size <= 0) {
776                                         sg->length += data_size;
777                                         i++;
778                                         break;
779                                 }
780                         }
781                         brq.data.sg_len = i;
782                 }
783
784                 mmc_queue_bounce_pre(mq);
785
786                 mmc_wait_for_req(card->host, &brq.mrq);
787
788                 mmc_queue_bounce_post(mq);
789
790                 /*
791                  * Check for errors here, but don't jump to cmd_err
792                  * until later as we need to wait for the card to leave
793                  * programming mode even when things go wrong.
794                  */
795                 if (brq.cmd.error || brq.data.error || brq.stop.error) {
796                         if (brq.data.blocks > 1 && rq_data_dir(req) == READ) {
797                                 /* Redo read one sector at a time */
798                                 printk(KERN_WARNING "%s: retrying using single "
799                                        "block read\n", req->rq_disk->disk_name);
800                                 disable_multi = 1;
801                                 continue;
802                         }
803                         status = get_card_status(card, req);
804                 }
805
806                 if (brq.cmd.error) {
807                         printk(KERN_ERR "%s: error %d sending read/write "
808                                "command, response %#x, card status %#x\n",
809                                req->rq_disk->disk_name, brq.cmd.error,
810                                brq.cmd.resp[0], status);
811                 }
812
813                 if (brq.data.error) {
814                         if (brq.data.error == -ETIMEDOUT && brq.mrq.stop)
815                                 /* 'Stop' response contains card status */
816                                 status = brq.mrq.stop->resp[0];
817                         printk(KERN_ERR "%s: error %d transferring data,"
818                                " sector %u, nr %u, card status %#x\n",
819                                req->rq_disk->disk_name, brq.data.error,
820                                (unsigned)blk_rq_pos(req),
821                                (unsigned)blk_rq_sectors(req), status);
822                 }
823
824                 if (brq.stop.error) {
825                         printk(KERN_ERR "%s: error %d sending stop command, "
826                                "response %#x, card status %#x\n",
827                                req->rq_disk->disk_name, brq.stop.error,
828                                brq.stop.resp[0], status);
829                 }
830
831                 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
832                         do {
833                                 int err;
834
835                                 cmd.opcode = MMC_SEND_STATUS;
836                                 cmd.arg = card->rca << 16;
837                                 cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
838                                 err = mmc_wait_for_cmd(card->host, &cmd, 5);
839                                 if (err) {
840                                         printk(KERN_ERR "%s: error %d requesting status\n",
841                                                req->rq_disk->disk_name, err);
842                                         goto cmd_err;
843                                 }
844                                 /*
845                                  * Some cards mishandle the status bits,
846                                  * so make sure to check both the busy
847                                  * indication and the card state.
848                                  */
849                         } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
850                                 (R1_CURRENT_STATE(cmd.resp[0]) == 7));
851
852 #if 0
853                         if (cmd.resp[0] & ~0x00000900)
854                                 printk(KERN_ERR "%s: status = %08x\n",
855                                        req->rq_disk->disk_name, cmd.resp[0]);
856                         if (mmc_decode_status(cmd.resp))
857                                 goto cmd_err;
858 #endif
859                 }
860
861                 if (brq.cmd.error || brq.stop.error || brq.data.error) {
862                         if (rq_data_dir(req) == READ) {
863                                 /*
864                                  * After an error, we redo I/O one sector at a
865                                  * time, so we only reach here after trying to
866                                  * read a single sector.
867                                  */
868                                 spin_lock_irq(&md->lock);
869                                 ret = __blk_end_request(req, -EIO, brq.data.blksz);
870                                 spin_unlock_irq(&md->lock);
871                                 continue;
872                         }
873                         goto cmd_err;
874                 }
875
876                 /*
877                  * A block was successfully transferred.
878                  */
879                 spin_lock_irq(&md->lock);
880                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
881                 spin_unlock_irq(&md->lock);
882         } while (ret);
883
884         return 1;
885
886  cmd_err:
887         /*
888          * If this is an SD card and we're writing, we can first
889          * mark the known good sectors as ok.
890          *
891          * If the card is not SD, we can still ok written sectors
892          * as reported by the controller (which might be less than
893          * the real number of written sectors, but never more).
894          */
895         if (mmc_card_sd(card)) {
896                 u32 blocks;
897
898                 blocks = mmc_sd_num_wr_blocks(card);
899                 if (blocks != (u32)-1) {
900                         spin_lock_irq(&md->lock);
901                         ret = __blk_end_request(req, 0, blocks << 9);
902                         spin_unlock_irq(&md->lock);
903                 }
904         } else {
905                 spin_lock_irq(&md->lock);
906                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
907                 spin_unlock_irq(&md->lock);
908         }
909
910         spin_lock_irq(&md->lock);
911         while (ret)
912                 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
913         spin_unlock_irq(&md->lock);
914
915         return 0;
916 }
917
918 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
919 {
920         int ret;
921         struct mmc_blk_data *md = mq->data;
922         struct mmc_card *card = md->queue.card;
923
924         mmc_claim_host(card->host);
925         ret = mmc_blk_part_switch(card, md);
926         if (ret) {
927                 ret = 0;
928                 goto out;
929         }
930
931         if (req->cmd_flags & REQ_DISCARD) {
932                 if (req->cmd_flags & REQ_SECURE)
933                         ret = mmc_blk_issue_secdiscard_rq(mq, req);
934                 else
935                         ret = mmc_blk_issue_discard_rq(mq, req);
936         } else if (req->cmd_flags & REQ_FLUSH) {
937                 ret = mmc_blk_issue_flush(mq, req);
938         } else {
939                 ret = mmc_blk_issue_rw_rq(mq, req);
940         }
941
942 out:
943         mmc_release_host(card->host);
944         return ret;
945 }
946
947 static inline int mmc_blk_readonly(struct mmc_card *card)
948 {
949         return mmc_card_readonly(card) ||
950                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
951 }
952
953 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
954                                               struct device *parent,
955                                               sector_t size,
956                                               bool default_ro,
957                                               const char *subname)
958 {
959         struct mmc_blk_data *md;
960         int devidx, ret;
961
962         devidx = find_first_zero_bit(dev_use, max_devices);
963         if (devidx >= max_devices)
964                 return ERR_PTR(-ENOSPC);
965         __set_bit(devidx, dev_use);
966
967         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
968         if (!md) {
969                 ret = -ENOMEM;
970                 goto out;
971         }
972
973         /*
974          * !subname implies we are creating main mmc_blk_data that will be
975          * associated with mmc_card with mmc_set_drvdata. Due to device
976          * partitions, devidx will not coincide with a per-physical card
977          * index anymore so we keep track of a name index.
978          */
979         if (!subname) {
980                 md->name_idx = find_first_zero_bit(name_use, max_devices);
981                 __set_bit(md->name_idx, name_use);
982         }
983         else
984                 md->name_idx = ((struct mmc_blk_data *)
985                                 dev_to_disk(parent)->private_data)->name_idx;
986
987         /*
988          * Set the read-only status based on the supported commands
989          * and the write protect switch.
990          */
991         md->read_only = mmc_blk_readonly(card);
992
993         md->disk = alloc_disk(perdev_minors);
994         if (md->disk == NULL) {
995                 ret = -ENOMEM;
996                 goto err_kfree;
997         }
998
999         spin_lock_init(&md->lock);
1000         INIT_LIST_HEAD(&md->part);
1001         md->usage = 1;
1002
1003         ret = mmc_init_queue(&md->queue, card, &md->lock);
1004         if (ret)
1005                 goto err_putdisk;
1006
1007         md->queue.issue_fn = mmc_blk_issue_rq;
1008         md->queue.data = md;
1009
1010         md->disk->major = MMC_BLOCK_MAJOR;
1011         md->disk->first_minor = devidx * perdev_minors;
1012         md->disk->fops = &mmc_bdops;
1013         md->disk->private_data = md;
1014         md->disk->queue = md->queue.queue;
1015         md->disk->driverfs_dev = parent;
1016         set_disk_ro(md->disk, md->read_only || default_ro);
1017         if (REL_WRITES_SUPPORTED(card))
1018                 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1019
1020         /*
1021          * As discussed on lkml, GENHD_FL_REMOVABLE should:
1022          *
1023          * - be set for removable media with permanent block devices
1024          * - be unset for removable block devices with permanent media
1025          *
1026          * Since MMC block devices clearly fall under the second
1027          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
1028          * should use the block device creation/destruction hotplug
1029          * messages to tell when the card is present.
1030          */
1031
1032         snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1033                  "mmcblk%d%s", md->name_idx, subname ? subname : "");
1034
1035         blk_queue_logical_block_size(md->queue.queue, 512);
1036         set_capacity(md->disk, size);
1037         return md;
1038
1039  err_putdisk:
1040         put_disk(md->disk);
1041  err_kfree:
1042         kfree(md);
1043  out:
1044         return ERR_PTR(ret);
1045 }
1046
1047 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1048 {
1049         sector_t size;
1050         struct mmc_blk_data *md;
1051
1052         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1053                 /*
1054                  * The EXT_CSD sector count is in number or 512 byte
1055                  * sectors.
1056                  */
1057                 size = card->ext_csd.sectors;
1058         } else {
1059                 /*
1060                  * The CSD capacity field is in units of read_blkbits.
1061                  * set_capacity takes units of 512 bytes.
1062                  */
1063                 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1064         }
1065
1066         md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1067         return md;
1068 }
1069
1070 static int mmc_blk_alloc_part(struct mmc_card *card,
1071                               struct mmc_blk_data *md,
1072                               unsigned int part_type,
1073                               sector_t size,
1074                               bool default_ro,
1075                               const char *subname)
1076 {
1077         char cap_str[10];
1078         struct mmc_blk_data *part_md;
1079
1080         part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1081                                     subname);
1082         if (IS_ERR(part_md))
1083                 return PTR_ERR(part_md);
1084         part_md->part_type = part_type;
1085         list_add(&part_md->part, &md->part);
1086
1087         string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1088                         cap_str, sizeof(cap_str));
1089         printk(KERN_INFO "%s: %s %s partition %u %s\n",
1090                part_md->disk->disk_name, mmc_card_id(card),
1091                mmc_card_name(card), part_md->part_type, cap_str);
1092         return 0;
1093 }
1094
1095 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1096 {
1097         int ret = 0;
1098
1099         if (!mmc_card_mmc(card))
1100                 return 0;
1101
1102         if (card->ext_csd.boot_size) {
1103                 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1104                                          card->ext_csd.boot_size >> 9,
1105                                          true,
1106                                          "boot0");
1107                 if (ret)
1108                         return ret;
1109                 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1110                                          card->ext_csd.boot_size >> 9,
1111                                          true,
1112                                          "boot1");
1113                 if (ret)
1114                         return ret;
1115         }
1116
1117         return ret;
1118 }
1119
1120 static int
1121 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1122 {
1123         int err;
1124
1125         mmc_claim_host(card->host);
1126         err = mmc_set_blocklen(card, 512);
1127         mmc_release_host(card->host);
1128
1129         if (err) {
1130                 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1131                         md->disk->disk_name, err);
1132                 return -EINVAL;
1133         }
1134
1135         return 0;
1136 }
1137
1138 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1139 {
1140         if (md) {
1141                 if (md->disk->flags & GENHD_FL_UP) {
1142                         device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1143
1144                         /* Stop new requests from getting into the queue */
1145                         del_gendisk(md->disk);
1146                 }
1147
1148                 /* Then flush out any already in there */
1149                 mmc_cleanup_queue(&md->queue);
1150                 mmc_blk_put(md);
1151         }
1152 }
1153
1154 static void mmc_blk_remove_parts(struct mmc_card *card,
1155                                  struct mmc_blk_data *md)
1156 {
1157         struct list_head *pos, *q;
1158         struct mmc_blk_data *part_md;
1159
1160         __clear_bit(md->name_idx, name_use);
1161         list_for_each_safe(pos, q, &md->part) {
1162                 part_md = list_entry(pos, struct mmc_blk_data, part);
1163                 list_del(pos);
1164                 mmc_blk_remove_req(part_md);
1165         }
1166 }
1167
1168 static int mmc_add_disk(struct mmc_blk_data *md)
1169 {
1170         int ret;
1171
1172         add_disk(md->disk);
1173         md->force_ro.show = force_ro_show;
1174         md->force_ro.store = force_ro_store;
1175         sysfs_attr_init(&md->force_ro.attr);
1176         md->force_ro.attr.name = "force_ro";
1177         md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1178         ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1179         if (ret)
1180                 del_gendisk(md->disk);
1181
1182         return ret;
1183 }
1184
1185 static const struct mmc_fixup blk_fixups[] =
1186 {
1187         MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1188         MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1189         MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1190         MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1191         MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1192         END_FIXUP
1193 };
1194
1195 static int mmc_blk_probe(struct mmc_card *card)
1196 {
1197         struct mmc_blk_data *md, *part_md;
1198         int err;
1199         char cap_str[10];
1200
1201         /*
1202          * Check that the card supports the command class(es) we need.
1203          */
1204         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1205                 return -ENODEV;
1206
1207         md = mmc_blk_alloc(card);
1208         if (IS_ERR(md))
1209                 return PTR_ERR(md);
1210
1211         err = mmc_blk_set_blksize(md, card);
1212         if (err)
1213                 goto out;
1214
1215         string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1216                         cap_str, sizeof(cap_str));
1217         printk(KERN_INFO "%s: %s %s %s %s\n",
1218                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1219                 cap_str, md->read_only ? "(ro)" : "");
1220
1221         if (mmc_blk_alloc_parts(card, md))
1222                 goto out;
1223
1224         mmc_set_drvdata(card, md);
1225         mmc_fixup_device(card, blk_fixups);
1226
1227         if (mmc_add_disk(md))
1228                 goto out;
1229
1230         list_for_each_entry(part_md, &md->part, part) {
1231                 if (mmc_add_disk(part_md))
1232                         goto out;
1233         }
1234         return 0;
1235
1236  out:
1237         mmc_blk_remove_parts(card, md);
1238         mmc_blk_remove_req(md);
1239         return err;
1240 }
1241
1242 static void mmc_blk_remove(struct mmc_card *card)
1243 {
1244         struct mmc_blk_data *md = mmc_get_drvdata(card);
1245
1246         mmc_blk_remove_parts(card, md);
1247         mmc_blk_remove_req(md);
1248         mmc_set_drvdata(card, NULL);
1249 }
1250
1251 #ifdef CONFIG_PM
1252 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1253 {
1254         struct mmc_blk_data *part_md;
1255         struct mmc_blk_data *md = mmc_get_drvdata(card);
1256
1257         if (md) {
1258                 mmc_queue_suspend(&md->queue);
1259                 list_for_each_entry(part_md, &md->part, part) {
1260                         mmc_queue_suspend(&part_md->queue);
1261                 }
1262         }
1263         return 0;
1264 }
1265
1266 static int mmc_blk_resume(struct mmc_card *card)
1267 {
1268         struct mmc_blk_data *part_md;
1269         struct mmc_blk_data *md = mmc_get_drvdata(card);
1270
1271         if (md) {
1272                 mmc_blk_set_blksize(md, card);
1273
1274                 /*
1275                  * Resume involves the card going into idle state,
1276                  * so current partition is always the main one.
1277                  */
1278                 md->part_curr = md->part_type;
1279                 mmc_queue_resume(&md->queue);
1280                 list_for_each_entry(part_md, &md->part, part) {
1281                         mmc_queue_resume(&part_md->queue);
1282                 }
1283         }
1284         return 0;
1285 }
1286 #else
1287 #define mmc_blk_suspend NULL
1288 #define mmc_blk_resume  NULL
1289 #endif
1290
1291 static struct mmc_driver mmc_driver = {
1292         .drv            = {
1293                 .name   = "mmcblk",
1294         },
1295         .probe          = mmc_blk_probe,
1296         .remove         = mmc_blk_remove,
1297         .suspend        = mmc_blk_suspend,
1298         .resume         = mmc_blk_resume,
1299 };
1300
1301 static int __init mmc_blk_init(void)
1302 {
1303         int res;
1304
1305         if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1306                 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1307
1308         max_devices = 256 / perdev_minors;
1309
1310         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1311         if (res)
1312                 goto out;
1313
1314         res = mmc_register_driver(&mmc_driver);
1315         if (res)
1316                 goto out2;
1317
1318         return 0;
1319  out2:
1320         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1321  out:
1322         return res;
1323 }
1324
1325 static void __exit mmc_blk_exit(void)
1326 {
1327         mmc_unregister_driver(&mmc_driver);
1328         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1329 }
1330
1331 module_init(mmc_blk_init);
1332 module_exit(mmc_blk_exit);
1333
1334 MODULE_LICENSE("GPL");
1335 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1336