Merge remote-tracking branch 'origin/develop-3.0' into develop-3.0-jb
[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-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 static DEFINE_MUTEX(block_mutex);
63
64 /*
65  * The defaults come from config options but can be overriden by module
66  * or bootarg options.
67  */
68 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
69
70 /*
71  * We've only got one major, so number of mmcblk devices is
72  * limited to 256 / number of minors per device.
73  */
74 static int max_devices;
75
76 /* 256 minors, so at most 256 separate devices */
77 static DECLARE_BITMAP(dev_use, 256);
78 static DECLARE_BITMAP(name_use, 256);
79
80 /*
81  * There is one mmc_blk_data per slot.
82  */
83 struct mmc_blk_data {
84         spinlock_t      lock;
85         struct gendisk  *disk;
86         struct mmc_queue queue;
87         struct list_head part;
88
89         unsigned int    flags;
90 #define MMC_BLK_CMD23   (1 << 0)        /* Can do SET_BLOCK_COUNT for multiblock */
91 #define MMC_BLK_REL_WR  (1 << 1)        /* MMC Reliable write support */
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 devidx = disk->first_minor / perdev_minors;
130         return devidx;
131 }
132
133 static void mmc_blk_put(struct mmc_blk_data *md)
134 {
135         mutex_lock(&open_lock);
136         md->usage--;
137         if (md->usage == 0) {
138                 int devidx = mmc_get_devidx(md->disk);
139                 blk_cleanup_queue(md->queue.queue);
140
141                 __clear_bit(devidx, dev_use);
142
143                 put_disk(md->disk);
144                 kfree(md);
145         }
146         mutex_unlock(&open_lock);
147 }
148
149 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
150                              char *buf)
151 {
152         int ret;
153         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
154
155         ret = snprintf(buf, PAGE_SIZE, "%d",
156                        get_disk_ro(dev_to_disk(dev)) ^
157                        md->read_only);
158         mmc_blk_put(md);
159         return ret;
160 }
161
162 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
163                               const char *buf, size_t count)
164 {
165         int ret;
166         char *end;
167         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
168         unsigned long set = simple_strtoul(buf, &end, 0);
169         if (end == buf) {
170                 ret = -EINVAL;
171                 goto out;
172         }
173
174         set_disk_ro(dev_to_disk(dev), set || md->read_only);
175         ret = count;
176 out:
177         mmc_blk_put(md);
178         return ret;
179 }
180
181 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
182 {
183         struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
184         int ret = -ENXIO;
185
186         mutex_lock(&block_mutex);
187         if (md) {
188                 if (md->usage == 2)
189                         check_disk_change(bdev);
190                 ret = 0;
191
192                 if ((mode & FMODE_WRITE) && md->read_only) {
193                         mmc_blk_put(md);
194                         ret = -EROFS;
195                 }
196         }
197         mutex_unlock(&block_mutex);
198
199         return ret;
200 }
201
202 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
203 {
204         struct mmc_blk_data *md = disk->private_data;
205
206         mutex_lock(&block_mutex);
207         mmc_blk_put(md);
208         mutex_unlock(&block_mutex);
209         return 0;
210 }
211
212 static int
213 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
214 {
215         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
216         geo->heads = 4;
217         geo->sectors = 16;
218         return 0;
219 }
220
221 struct mmc_blk_ioc_data {
222         struct mmc_ioc_cmd ic;
223         unsigned char *buf;
224         u64 buf_bytes;
225 };
226
227 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
228         struct mmc_ioc_cmd __user *user)
229 {
230         struct mmc_blk_ioc_data *idata;
231         int err;
232
233         idata = kzalloc(sizeof(*idata), GFP_KERNEL);
234         if (!idata) {
235                 err = -ENOMEM;
236                 goto out;
237         }
238
239         if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
240                 err = -EFAULT;
241                 goto idata_err;
242         }
243
244         idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
245         if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
246                 err = -EOVERFLOW;
247                 goto idata_err;
248         }
249
250         if (!idata->buf_bytes)
251                 return idata;
252
253         idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
254         if (!idata->buf) {
255                 err = -ENOMEM;
256                 goto idata_err;
257         }
258
259         if (copy_from_user(idata->buf, (void __user *)(unsigned long)
260                                         idata->ic.data_ptr, idata->buf_bytes)) {
261                 err = -EFAULT;
262                 goto copy_err;
263         }
264
265         return idata;
266
267 copy_err:
268         kfree(idata->buf);
269 idata_err:
270         kfree(idata);
271 out:
272         return ERR_PTR(err);
273 }
274
275 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
276         struct mmc_ioc_cmd __user *ic_ptr)
277 {
278         struct mmc_blk_ioc_data *idata;
279         struct mmc_blk_data *md;
280         struct mmc_card *card;
281         struct mmc_command cmd = {0};
282         struct mmc_data data = {0};
283         struct mmc_request mrq = {0};
284         struct scatterlist sg;
285         int err;
286
287         /*
288          * The caller must have CAP_SYS_RAWIO, and must be calling this on the
289          * whole block device, not on a partition.  This prevents overspray
290          * between sibling partitions.
291          */
292         if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
293                 return -EPERM;
294
295         idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
296         if (IS_ERR(idata))
297                 return PTR_ERR(idata);
298
299         md = mmc_blk_get(bdev->bd_disk);
300         if (!md) {
301                 err = -EINVAL;
302                 goto cmd_done;
303         }
304
305         card = md->queue.card;
306         if (IS_ERR(card)) {
307                 err = PTR_ERR(card);
308                 goto cmd_done;
309         }
310
311         cmd.opcode = idata->ic.opcode;
312         cmd.arg = idata->ic.arg;
313         cmd.flags = idata->ic.flags;
314
315         if (idata->buf_bytes) {
316                 data.sg = &sg;
317                 data.sg_len = 1;
318                 data.blksz = idata->ic.blksz;
319                 data.blocks = idata->ic.blocks;
320
321                 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
322
323                 if (idata->ic.write_flag)
324                         data.flags = MMC_DATA_WRITE;
325                 else
326                         data.flags = MMC_DATA_READ;
327
328                 /* data.flags must already be set before doing this. */
329                 mmc_set_data_timeout(&data, card);
330
331                 /* Allow overriding the timeout_ns for empirical tuning. */
332                 if (idata->ic.data_timeout_ns)
333                         data.timeout_ns = idata->ic.data_timeout_ns;
334
335                 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
336                         /*
337                          * Pretend this is a data transfer and rely on the
338                          * host driver to compute timeout.  When all host
339                          * drivers support cmd.cmd_timeout for R1B, this
340                          * can be changed to:
341                          *
342                          *     mrq.data = NULL;
343                          *     cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
344                          */
345                         data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
346                 }
347
348                 mrq.data = &data;
349         }
350
351         mrq.cmd = &cmd;
352
353         mmc_claim_host(card->host);
354
355         if (idata->ic.is_acmd) {
356                 err = mmc_app_cmd(card->host, card);
357                 if (err)
358                         goto cmd_rel_host;
359         }
360
361         mmc_wait_for_req(card->host, &mrq);
362
363         if (cmd.error) {
364                 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
365                                                 __func__, cmd.error);
366                 err = cmd.error;
367                 goto cmd_rel_host;
368         }
369         if (data.error) {
370                 dev_err(mmc_dev(card->host), "%s: data error %d\n",
371                                                 __func__, data.error);
372                 err = data.error;
373                 goto cmd_rel_host;
374         }
375
376         /*
377          * According to the SD specs, some commands require a delay after
378          * issuing the command.
379          */
380         if (idata->ic.postsleep_min_us)
381                 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
382
383         if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
384                 err = -EFAULT;
385                 goto cmd_rel_host;
386         }
387
388         if (!idata->ic.write_flag) {
389                 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
390                                                 idata->buf, idata->buf_bytes)) {
391                         err = -EFAULT;
392                         goto cmd_rel_host;
393                 }
394         }
395
396 cmd_rel_host:
397         mmc_release_host(card->host);
398
399 cmd_done:
400         mmc_blk_put(md);
401         kfree(idata->buf);
402         kfree(idata);
403         return err;
404 }
405
406 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
407         unsigned int cmd, unsigned long arg)
408 {
409         int ret = -EINVAL;
410         if (cmd == MMC_IOC_CMD)
411                 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
412         return ret;
413 }
414
415 #ifdef CONFIG_COMPAT
416 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
417         unsigned int cmd, unsigned long arg)
418 {
419         return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
420 }
421 #endif
422
423 static const struct block_device_operations mmc_bdops = {
424         .open                   = mmc_blk_open,
425         .release                = mmc_blk_release,
426         .getgeo                 = mmc_blk_getgeo,
427         .owner                  = THIS_MODULE,
428         .ioctl                  = mmc_blk_ioctl,
429 #ifdef CONFIG_COMPAT
430         .compat_ioctl           = mmc_blk_compat_ioctl,
431 #endif
432 };
433
434 struct mmc_blk_request {
435         struct mmc_request      mrq;
436         struct mmc_command      sbc;
437         struct mmc_command      cmd;
438         struct mmc_command      stop;
439         struct mmc_data         data;
440 };
441
442 static inline int mmc_blk_part_switch(struct mmc_card *card,
443                                       struct mmc_blk_data *md)
444 {
445         int ret;
446         struct mmc_blk_data *main_md = mmc_get_drvdata(card);
447         if (main_md->part_curr == md->part_type)
448                 return 0;
449
450         if (mmc_card_mmc(card)) {
451                 card->ext_csd.part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
452                 card->ext_csd.part_config |= md->part_type;
453
454                 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
455                                  EXT_CSD_PART_CONFIG, card->ext_csd.part_config,
456                                  card->ext_csd.part_time);
457                 if (ret)
458                         return ret;
459 }
460
461         main_md->part_curr = md->part_type;
462         return 0;
463 }
464
465 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
466 {
467         int err;
468         u32 result;
469         __be32 *blocks;
470
471         struct mmc_request mrq = {0};
472         struct mmc_command cmd = {0};
473         struct mmc_data data = {0};
474         unsigned int timeout_us;
475
476         struct scatterlist sg;
477
478         cmd.opcode = MMC_APP_CMD;
479         cmd.arg = card->rca << 16;
480         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
481
482         err = mmc_wait_for_cmd(card->host, &cmd, 0);
483         if (err)
484                 return (u32)-1;
485         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
486                 return (u32)-1;
487
488         memset(&cmd, 0, sizeof(struct mmc_command));
489
490         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
491         cmd.arg = 0;
492         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
493
494         data.timeout_ns = card->csd.tacc_ns * 100;
495         data.timeout_clks = card->csd.tacc_clks * 100;
496
497         timeout_us = data.timeout_ns / 1000;
498         timeout_us += data.timeout_clks * 1000 /
499                 (card->host->ios.clock / 1000);
500
501         if (timeout_us > 100000) {
502                 data.timeout_ns = 100000000;
503                 data.timeout_clks = 0;
504         }
505
506         data.blksz = 4;
507         data.blocks = 1;
508         data.flags = MMC_DATA_READ;
509         data.sg = &sg;
510         data.sg_len = 1;
511
512         mrq.cmd = &cmd;
513         mrq.data = &data;
514
515         blocks = kmalloc(4, GFP_KERNEL);
516         if (!blocks)
517                 return (u32)-1;
518
519         sg_init_one(&sg, blocks, 4);
520
521         mmc_wait_for_req(card->host, &mrq);
522
523         result = ntohl(*blocks);
524         kfree(blocks);
525
526         if (cmd.error || data.error)
527                 result = (u32)-1;
528
529         return result;
530 }
531
532 static int send_stop(struct mmc_card *card, u32 *status)
533 {
534         struct mmc_command cmd = {0};
535         int err;
536
537         cmd.opcode = MMC_STOP_TRANSMISSION;
538         cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
539         err = mmc_wait_for_cmd(card->host, &cmd, 5);
540         if (err == 0)
541                 *status = cmd.resp[0];
542         return err;
543 }
544
545 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
546 {
547         struct mmc_command cmd = {0};
548         int err;
549
550         cmd.opcode = MMC_SEND_STATUS;
551         if (!mmc_host_is_spi(card->host))
552                 cmd.arg = card->rca << 16;
553         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
554         err = mmc_wait_for_cmd(card->host, &cmd, retries);
555         if (err == 0)
556                 *status = cmd.resp[0];
557         return err;
558 }
559
560 #define ERR_RETRY       2
561 #define ERR_ABORT       1
562 #define ERR_CONTINUE    0
563
564 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
565         bool status_valid, u32 status)
566 {
567         switch (error) {
568         case -EILSEQ:
569                 /* response crc error, retry the r/w cmd */
570                 pr_err("%s: %s sending %s command, card status %#x\n",
571                         req->rq_disk->disk_name, "response CRC error",
572                         name, status);
573                 return ERR_RETRY;
574
575         case -ETIMEDOUT:
576                 pr_err("%s: %s sending %s command, card status %#x\n",
577                         req->rq_disk->disk_name, "timed out", name, status);
578
579                 /* If the status cmd initially failed, retry the r/w cmd */
580                 if (!status_valid) {
581                         pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
582                         return ERR_RETRY;
583                 }
584                 /*
585                  * If it was a r/w cmd crc error, or illegal command
586                  * (eg, issued in wrong state) then retry - we should
587                  * have corrected the state problem above.
588                  */
589                 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
590                         pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
591                         return ERR_RETRY;
592                 }
593
594                 /* Otherwise abort the command */
595                 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
596                 return ERR_ABORT;
597
598         default:
599                 /* We don't understand the error code the driver gave us */
600                 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
601                        req->rq_disk->disk_name, error, status);
602                 return ERR_ABORT;
603         }
604 }
605
606 /*
607  * Initial r/w and stop cmd error recovery.
608  * We don't know whether the card received the r/w cmd or not, so try to
609  * restore things back to a sane state.  Essentially, we do this as follows:
610  * - Obtain card status.  If the first attempt to obtain card status fails,
611  *   the status word will reflect the failed status cmd, not the failed
612  *   r/w cmd.  If we fail to obtain card status, it suggests we can no
613  *   longer communicate with the card.
614  * - Check the card state.  If the card received the cmd but there was a
615  *   transient problem with the response, it might still be in a data transfer
616  *   mode.  Try to send it a stop command.  If this fails, we can't recover.
617  * - If the r/w cmd failed due to a response CRC error, it was probably
618  *   transient, so retry the cmd.
619  * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
620  * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
621  *   illegal cmd, retry.
622  * Otherwise we don't understand what happened, so abort.
623  */
624 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
625         struct mmc_blk_request *brq)
626 {
627         bool prev_cmd_status_valid = true;
628         u32 status, stop_status = 0;
629         int err, retry;
630
631         /*
632          * Try to get card status which indicates both the card state
633          * and why there was no response.  If the first attempt fails,
634          * we can't be sure the returned status is for the r/w command.
635          */
636         for (retry = 2; retry >= 0; retry--) {
637                 err = get_card_status(card, &status, 0);
638                 if (!err)
639                         break;
640
641                 prev_cmd_status_valid = false;
642                 pr_err("%s: error %d sending status command, %sing\n",
643                        req->rq_disk->disk_name, err, retry ? "retry" : "abort");
644         }
645
646         /* We couldn't get a response from the card.  Give up. */
647         if (err)
648                 return ERR_ABORT;
649
650         /*
651          * Check the current card state.  If it is in some data transfer
652          * mode, tell it to stop (and hopefully transition back to TRAN.)
653          */
654         if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
655             R1_CURRENT_STATE(status) == R1_STATE_RCV) {
656                 err = send_stop(card, &stop_status);
657                 if (err)
658                         pr_err("%s: error %d sending stop command\n",
659                                req->rq_disk->disk_name, err);
660
661                 /*
662                  * If the stop cmd also timed out, the card is probably
663                  * not present, so abort.  Other errors are bad news too.
664                  */
665                 if (err)
666                         return ERR_ABORT;
667         }
668
669         /* Check for set block count errors */
670         if (brq->sbc.error)
671                 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
672                                 prev_cmd_status_valid, status);
673
674         /* Check for r/w command errors */
675         if (brq->cmd.error)
676                 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
677                                 prev_cmd_status_valid, status);
678
679         /* Now for stop errors.  These aren't fatal to the transfer. */
680         pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
681                req->rq_disk->disk_name, brq->stop.error,
682                brq->cmd.resp[0], status);
683
684         /*
685          * Subsitute in our own stop status as this will give the error
686          * state which happened during the execution of the r/w command.
687          */
688         if (stop_status) {
689                 brq->stop.resp[0] = stop_status;
690                 brq->stop.error = 0;
691         }
692         return ERR_CONTINUE;
693 }
694
695 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
696 {
697         struct mmc_blk_data *md = mq->data;
698         struct mmc_card *card = md->queue.card;
699         unsigned int from, nr, arg;
700         int err = 0;
701
702         if (!mmc_can_erase(card)) {
703                 err = -EOPNOTSUPP;
704                 goto out;
705         }
706
707         from = blk_rq_pos(req);
708         nr = blk_rq_sectors(req);
709
710         if (mmc_can_trim(card))
711                 arg = MMC_TRIM_ARG;
712         else
713                 arg = MMC_ERASE_ARG;
714
715         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
716                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
717                                  INAND_CMD38_ARG_EXT_CSD,
718                                  arg == MMC_TRIM_ARG ?
719                                  INAND_CMD38_ARG_TRIM :
720                                  INAND_CMD38_ARG_ERASE,
721                                  0);
722                 if (err)
723                         goto out;
724         }
725         err = mmc_erase(card, from, nr, arg);
726 out:
727         spin_lock_irq(&md->lock);
728         __blk_end_request(req, err, blk_rq_bytes(req));
729         spin_unlock_irq(&md->lock);
730
731         return err ? 0 : 1;
732 }
733
734 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
735                                        struct request *req)
736 {
737         struct mmc_blk_data *md = mq->data;
738         struct mmc_card *card = md->queue.card;
739         unsigned int from, nr, arg;
740         int err = 0;
741
742         if (!mmc_can_secure_erase_trim(card)) {
743                 err = -EOPNOTSUPP;
744                 goto out;
745         }
746
747         from = blk_rq_pos(req);
748         nr = blk_rq_sectors(req);
749
750         if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
751                 arg = MMC_SECURE_TRIM1_ARG;
752         else
753                 arg = MMC_SECURE_ERASE_ARG;
754
755         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
756                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
757                                  INAND_CMD38_ARG_EXT_CSD,
758                                  arg == MMC_SECURE_TRIM1_ARG ?
759                                  INAND_CMD38_ARG_SECTRIM1 :
760                                  INAND_CMD38_ARG_SECERASE,
761                                  0);
762                 if (err)
763                         goto out;
764         }
765         err = mmc_erase(card, from, nr, arg);
766         if (!err && arg == MMC_SECURE_TRIM1_ARG) {
767                 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
768                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
769                                          INAND_CMD38_ARG_EXT_CSD,
770                                          INAND_CMD38_ARG_SECTRIM2,
771                                          0);
772                         if (err)
773                                 goto out;
774                 }
775                 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
776         }
777 out:
778         spin_lock_irq(&md->lock);
779         __blk_end_request(req, err, blk_rq_bytes(req));
780         spin_unlock_irq(&md->lock);
781
782         return err ? 0 : 1;
783 }
784
785 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
786 {
787         struct mmc_blk_data *md = mq->data;
788
789         /*
790          * No-op, only service this because we need REQ_FUA for reliable
791          * writes.
792          */
793         spin_lock_irq(&md->lock);
794         __blk_end_request_all(req, 0);
795         spin_unlock_irq(&md->lock);
796
797         return 1;
798 }
799
800 /*
801  * Reformat current write as a reliable write, supporting
802  * both legacy and the enhanced reliable write MMC cards.
803  * In each transfer we'll handle only as much as a single
804  * reliable write can handle, thus finish the request in
805  * partial completions.
806  */
807 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
808                                     struct mmc_card *card,
809                                     struct request *req)
810 {
811         if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
812                 /* Legacy mode imposes restrictions on transfers. */
813                 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
814                         brq->data.blocks = 1;
815
816                 if (brq->data.blocks > card->ext_csd.rel_sectors)
817                         brq->data.blocks = card->ext_csd.rel_sectors;
818                 else if (brq->data.blocks < card->ext_csd.rel_sectors)
819                         brq->data.blocks = 1;
820         }
821 }
822
823 #define CMD_ERRORS                                                      \
824         (R1_OUT_OF_RANGE |      /* Command argument out of range */     \
825          R1_ADDRESS_ERROR |     /* Misaligned address */                \
826          R1_BLOCK_LEN_ERROR |   /* Transferred block length incorrect */\
827          R1_WP_VIOLATION |      /* Tried to write to protected block */ \
828          R1_CC_ERROR |          /* Card controller error */             \
829          R1_ERROR)              /* General/unknown error */
830
831 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
832 {
833         struct mmc_blk_data *md = mq->data;
834         struct mmc_card *card = md->queue.card;
835         struct mmc_blk_request brq;
836         int ret = 1, disable_multi = 0, retry = 0;
837
838         /*
839          * Reliable writes are used to implement Forced Unit Access and
840          * REQ_META accesses, and are supported only on MMCs.
841          */
842         bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
843                           (req->cmd_flags & REQ_META)) &&
844                 (rq_data_dir(req) == WRITE) &&
845                 (md->flags & MMC_BLK_REL_WR);
846
847         do {
848                 u32 readcmd, writecmd;
849
850                 memset(&brq, 0, sizeof(struct mmc_blk_request));
851                 brq.mrq.cmd = &brq.cmd;
852                 brq.mrq.data = &brq.data;
853
854             #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
855                 brq.cmd.retries = 2; //suppot retry read-write; added by xbw@2012-07-14
856             #endif
857
858                 brq.cmd.arg = blk_rq_pos(req);
859                 if (!mmc_card_blockaddr(card))
860                         brq.cmd.arg <<= 9;
861                 brq.cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
862                 brq.data.blksz = 512;
863                 brq.stop.opcode = MMC_STOP_TRANSMISSION;
864                 brq.stop.arg = 0;
865                 brq.stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
866                 brq.data.blocks = blk_rq_sectors(req);
867
868                 /*
869                  * The block layer doesn't support all sector count
870                  * restrictions, so we need to be prepared for too big
871                  * requests.
872                  */
873                 if (brq.data.blocks > card->host->max_blk_count)
874                         brq.data.blocks = card->host->max_blk_count;
875
876                 /*
877                  * After a read error, we redo the request one sector at a time
878                  * in order to accurately determine which sectors can be read
879                  * successfully.
880                  */
881                 if (disable_multi && brq.data.blocks > 1)
882                         brq.data.blocks = 1;
883
884                 if (brq.data.blocks > 1 || do_rel_wr) {
885                         /* SPI multiblock writes terminate using a special
886                          * token, not a STOP_TRANSMISSION request.
887                          */
888                         if (!mmc_host_is_spi(card->host) ||
889                             rq_data_dir(req) == READ)
890                                 brq.mrq.stop = &brq.stop;
891                         readcmd = MMC_READ_MULTIPLE_BLOCK;
892                         writecmd = MMC_WRITE_MULTIPLE_BLOCK;
893                 } else {
894                         brq.mrq.stop = NULL;
895                         readcmd = MMC_READ_SINGLE_BLOCK;
896                         writecmd = MMC_WRITE_BLOCK;
897                 }
898                 if (rq_data_dir(req) == READ) {
899                         brq.cmd.opcode = readcmd;
900                         brq.data.flags |= MMC_DATA_READ;
901                 } else {
902                         brq.cmd.opcode = writecmd;
903                         brq.data.flags |= MMC_DATA_WRITE;
904                 }
905
906                 if (do_rel_wr)
907                         mmc_apply_rel_rw(&brq, card, req);
908
909                 /*
910                  * Pre-defined multi-block transfers are preferable to
911                  * open ended-ones (and necessary for reliable writes).
912                  * However, it is not sufficient to just send CMD23,
913                  * and avoid the final CMD12, as on an error condition
914                  * CMD12 (stop) needs to be sent anyway. This, coupled
915                  * with Auto-CMD23 enhancements provided by some
916                  * hosts, means that the complexity of dealing
917                  * with this is best left to the host. If CMD23 is
918                  * supported by card and host, we'll fill sbc in and let
919                  * the host deal with handling it correctly. This means
920                  * that for hosts that don't expose MMC_CAP_CMD23, no
921                  * change of behavior will be observed.
922                  *
923                  * N.B: Some MMC cards experience perf degradation.
924                  * We'll avoid using CMD23-bounded multiblock writes for
925                  * these, while retaining features like reliable writes.
926                  */
927
928                 if ((md->flags & MMC_BLK_CMD23) &&
929                     mmc_op_multi(brq.cmd.opcode) &&
930                     (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23))) {
931                         brq.sbc.opcode = MMC_SET_BLOCK_COUNT;
932                         brq.sbc.arg = brq.data.blocks |
933                                 (do_rel_wr ? (1 << 31) : 0);
934                         brq.sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
935                         brq.mrq.sbc = &brq.sbc;
936                 }
937
938                 mmc_set_data_timeout(&brq.data, card);
939
940                 brq.data.sg = mq->sg;
941                 brq.data.sg_len = mmc_queue_map_sg(mq);
942
943                 /*
944                  * Adjust the sg list so it is the same size as the
945                  * request.
946                  */
947                 if (brq.data.blocks != blk_rq_sectors(req)) {
948                         int i, data_size = brq.data.blocks << 9;
949                         struct scatterlist *sg;
950
951                         for_each_sg(brq.data.sg, sg, brq.data.sg_len, i) {
952                                 data_size -= sg->length;
953                                 if (data_size <= 0) {
954                                         sg->length += data_size;
955                                         i++;
956                                         break;
957                                 }
958                         }
959                         brq.data.sg_len = i;
960                 }
961
962                 mmc_queue_bounce_pre(mq);
963
964                 mmc_wait_for_req(card->host, &brq.mrq);
965
966                 mmc_queue_bounce_post(mq);
967
968 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
969     //delete all retry code. modifyed by xbw at 2011-11-17
970 #else
971                 /*
972                  * sbc.error indicates a problem with the set block count
973                  * command.  No data will have been transferred.
974                  *
975                  * cmd.error indicates a problem with the r/w command.  No
976                  * data will have been transferred.
977                  *
978                  * stop.error indicates a problem with the stop command.  Data
979                  * may have been transferred, or may still be transferring.
980                  */
981                 if (brq.sbc.error || brq.cmd.error || brq.stop.error) {
982                         switch (mmc_blk_cmd_recovery(card, req, &brq)) {
983                         case ERR_RETRY:
984                                 if (retry++ < 5)
985                                         continue;
986                         case ERR_ABORT:
987                                 goto cmd_abort;
988                         case ERR_CONTINUE:
989                                 break;
990                         }
991                 }
992 #endif          
993
994                 /*
995                  * Check for errors relating to the execution of the
996                  * initial command - such as address errors.  No data
997                  * has been transferred.
998                  */
999                 if (brq.cmd.resp[0] & CMD_ERRORS) {
1000                         pr_err("%s: r/w command failed, status = %#x\n",
1001                                 req->rq_disk->disk_name, brq.cmd.resp[0]);
1002                         goto cmd_abort;
1003                 }
1004
1005 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1006     //delete all retry code. modifyed by xbw at 2011-11-17
1007 #else    
1008                 /*
1009                  * Everything else is either success, or a data error of some
1010                  * kind.  If it was a write, we may have transitioned to
1011                  * program mode, which we have to wait for it to complete.
1012                  */
1013                 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1014                         u32 status;
1015                         do {
1016                                 int err = get_card_status(card, &status, 5);
1017                                 if (err) {
1018                                         printk(KERN_ERR "%s: error %d requesting status\n",
1019                                                req->rq_disk->disk_name, err);
1020                                         goto cmd_err;
1021                                 }
1022                                 /*
1023                                  * Some cards mishandle the status bits,
1024                                  * so make sure to check both the busy
1025                                  * indication and the card state.
1026                                  */
1027                         } while (!(status & R1_READY_FOR_DATA) ||
1028                                  (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1029                 }
1030 #endif
1031
1032 #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1033         if (brq.sbc.error || brq.cmd.error || brq.stop.error || brq.data.error) {   //modifyed by xbw at 2011-11-17
1034 #else
1035                 if (brq.data.error) {
1036                         pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1037                                 req->rq_disk->disk_name, brq.data.error,
1038                                 (unsigned)blk_rq_pos(req),
1039                                 (unsigned)blk_rq_sectors(req),
1040                                 brq.cmd.resp[0], brq.stop.resp[0]);
1041 #endif
1042                         if (rq_data_dir(req) == READ) {
1043                           #if defined(CONFIG_SDMMC_RK29) && !defined(CONFIG_SDMMC_RK29_OLD)
1044                           //direct to exit when error happen; deleted by xbw at 2011-12-14
1045                           #else
1046                                 if (brq.data.blocks > 1) {
1047                                         /* Redo read one sector at a time */
1048                                         pr_warning("%s: retrying using single block read\n",
1049                                                 req->rq_disk->disk_name);
1050                                         disable_multi = 1;
1051                                         continue;
1052                                 }
1053                                 #endif
1054
1055                                 /*
1056                                  * After an error, we redo I/O one sector at a
1057                                  * time, so we only reach here after trying to
1058                                  * read a single sector.
1059                                  */
1060                                 spin_lock_irq(&md->lock);
1061                                 ret = __blk_end_request(req, -EIO, brq.data.blksz);
1062                                 spin_unlock_irq(&md->lock);
1063                                 continue;
1064                         } else {
1065                                 goto cmd_err;
1066                         }
1067                 }
1068
1069                 /*
1070                  * A block was successfully transferred.
1071                  */
1072                 spin_lock_irq(&md->lock);
1073                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
1074                 spin_unlock_irq(&md->lock);
1075         } while (ret);
1076
1077         return 1;
1078
1079  cmd_err:
1080         /*
1081          * If this is an SD card and we're writing, we can first
1082          * mark the known good sectors as ok.
1083          *
1084          * If the card is not SD, we can still ok written sectors
1085          * as reported by the controller (which might be less than
1086          * the real number of written sectors, but never more).
1087          */
1088         if (mmc_card_sd(card)) {
1089                 u32 blocks;
1090
1091                 blocks = mmc_sd_num_wr_blocks(card);
1092                 if (blocks != (u32)-1) {
1093                         spin_lock_irq(&md->lock);
1094                         ret = __blk_end_request(req, 0, blocks << 9);
1095                         spin_unlock_irq(&md->lock);
1096                 }
1097         } else {
1098                 spin_lock_irq(&md->lock);
1099                 ret = __blk_end_request(req, 0, brq.data.bytes_xfered);
1100                 spin_unlock_irq(&md->lock);
1101         }
1102
1103  cmd_abort:
1104         spin_lock_irq(&md->lock);
1105         while (ret)
1106                 ret = __blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
1107         spin_unlock_irq(&md->lock);
1108
1109         return 0;
1110 }
1111
1112 static int
1113 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card);
1114
1115 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1116 {
1117         int ret;
1118         struct mmc_blk_data *md = mq->data;
1119         struct mmc_card *card = md->queue.card;
1120
1121 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1122         if (mmc_bus_needs_resume(card->host)) {
1123                 mmc_resume_bus(card->host);
1124                 mmc_blk_set_blksize(md, card);
1125         }
1126 #endif
1127
1128         mmc_claim_host(card->host);
1129         ret = mmc_blk_part_switch(card, md);
1130         if (ret) {
1131                 ret = 0;
1132                 goto out;
1133         }
1134
1135         if (req->cmd_flags & REQ_DISCARD) {
1136                 if (req->cmd_flags & REQ_SECURE)
1137                         ret = mmc_blk_issue_secdiscard_rq(mq, req);
1138                 else
1139                         ret = mmc_blk_issue_discard_rq(mq, req);
1140         } else if (req->cmd_flags & REQ_FLUSH) {
1141                 ret = mmc_blk_issue_flush(mq, req);
1142         } else {
1143                 ret = mmc_blk_issue_rw_rq(mq, req);
1144         }
1145
1146 out:
1147         mmc_release_host(card->host);
1148         return ret;
1149 }
1150
1151 static inline int mmc_blk_readonly(struct mmc_card *card)
1152 {
1153         return mmc_card_readonly(card) ||
1154                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1155 }
1156
1157 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1158                                               struct device *parent,
1159                                               sector_t size,
1160                                               bool default_ro,
1161                                               const char *subname)
1162 {
1163         struct mmc_blk_data *md;
1164         int devidx, ret;
1165
1166         devidx = find_first_zero_bit(dev_use, max_devices);
1167         if (devidx >= max_devices)
1168                 return ERR_PTR(-ENOSPC);
1169         __set_bit(devidx, dev_use);
1170
1171         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1172         if (!md) {
1173                 ret = -ENOMEM;
1174                 goto out;
1175         }
1176
1177         /*
1178          * !subname implies we are creating main mmc_blk_data that will be
1179          * associated with mmc_card with mmc_set_drvdata. Due to device
1180          * partitions, devidx will not coincide with a per-physical card
1181          * index anymore so we keep track of a name index.
1182          */
1183         if (!subname) {
1184                 md->name_idx = find_first_zero_bit(name_use, max_devices);
1185                 __set_bit(md->name_idx, name_use);
1186         }
1187         else
1188                 md->name_idx = ((struct mmc_blk_data *)
1189                                 dev_to_disk(parent)->private_data)->name_idx;
1190
1191         /*
1192          * Set the read-only status based on the supported commands
1193          * and the write protect switch.
1194          */
1195         md->read_only = mmc_blk_readonly(card);
1196
1197         md->disk = alloc_disk(perdev_minors);
1198         if (md->disk == NULL) {
1199                 ret = -ENOMEM;
1200                 goto err_kfree;
1201         }
1202
1203         spin_lock_init(&md->lock);
1204         INIT_LIST_HEAD(&md->part);
1205         md->usage = 1;
1206
1207         ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1208         if (ret)
1209                 goto err_putdisk;
1210
1211         md->queue.issue_fn = mmc_blk_issue_rq;
1212         md->queue.data = md;
1213
1214         md->disk->major = MMC_BLOCK_MAJOR;
1215         md->disk->first_minor = devidx * perdev_minors;
1216         md->disk->fops = &mmc_bdops;
1217         md->disk->private_data = md;
1218         md->disk->queue = md->queue.queue;
1219         md->disk->driverfs_dev = parent;
1220         set_disk_ro(md->disk, md->read_only || default_ro);
1221         md->disk->flags = GENHD_FL_EXT_DEVT;
1222
1223         /*
1224          * As discussed on lkml, GENHD_FL_REMOVABLE should:
1225          *
1226          * - be set for removable media with permanent block devices
1227          * - be unset for removable block devices with permanent media
1228          *
1229          * Since MMC block devices clearly fall under the second
1230          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
1231          * should use the block device creation/destruction hotplug
1232          * messages to tell when the card is present.
1233          */
1234
1235         snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1236                  "mmcblk%d%s", md->name_idx, subname ? subname : "");
1237
1238         blk_queue_logical_block_size(md->queue.queue, 512);
1239         set_capacity(md->disk, size);
1240
1241         if (mmc_host_cmd23(card->host)) {
1242                 if (mmc_card_mmc(card) ||
1243                     (mmc_card_sd(card) &&
1244                      card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1245                         md->flags |= MMC_BLK_CMD23;
1246         }
1247
1248         if (mmc_card_mmc(card) &&
1249             md->flags & MMC_BLK_CMD23 &&
1250             ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1251              card->ext_csd.rel_sectors)) {
1252                 md->flags |= MMC_BLK_REL_WR;
1253                 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1254         }
1255
1256         return md;
1257
1258  err_putdisk:
1259         put_disk(md->disk);
1260  err_kfree:
1261         kfree(md);
1262  out:
1263         return ERR_PTR(ret);
1264 }
1265
1266 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1267 {
1268         sector_t size;
1269         struct mmc_blk_data *md;
1270
1271         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1272                 /*
1273                  * The EXT_CSD sector count is in number or 512 byte
1274                  * sectors.
1275                  */
1276                 size = card->ext_csd.sectors;
1277         } else {
1278                 /*
1279                  * The CSD capacity field is in units of read_blkbits.
1280                  * set_capacity takes units of 512 bytes.
1281                  */
1282                 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1283         }
1284
1285         md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL);
1286         return md;
1287 }
1288
1289 static int mmc_blk_alloc_part(struct mmc_card *card,
1290                               struct mmc_blk_data *md,
1291                               unsigned int part_type,
1292                               sector_t size,
1293                               bool default_ro,
1294                               const char *subname)
1295 {
1296         char cap_str[10];
1297         struct mmc_blk_data *part_md;
1298
1299         part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1300                                     subname);
1301         if (IS_ERR(part_md))
1302                 return PTR_ERR(part_md);
1303         part_md->part_type = part_type;
1304         list_add(&part_md->part, &md->part);
1305
1306         string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1307                         cap_str, sizeof(cap_str));
1308         printk(KERN_INFO "%s: %s %s partition %u %s\n",
1309                part_md->disk->disk_name, mmc_card_id(card),
1310                mmc_card_name(card), part_md->part_type, cap_str);
1311         return 0;
1312 }
1313
1314 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1315 {
1316         int ret = 0;
1317
1318         if (!mmc_card_mmc(card))
1319                 return 0;
1320
1321         if (card->ext_csd.boot_size) {
1322                 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT0,
1323                                          card->ext_csd.boot_size >> 9,
1324                                          true,
1325                                          "boot0");
1326                 if (ret)
1327                         return ret;
1328                 ret = mmc_blk_alloc_part(card, md, EXT_CSD_PART_CONFIG_ACC_BOOT1,
1329                                          card->ext_csd.boot_size >> 9,
1330                                          true,
1331                                          "boot1");
1332                 if (ret)
1333                         return ret;
1334         }
1335
1336         return ret;
1337 }
1338
1339 static int
1340 mmc_blk_set_blksize(struct mmc_blk_data *md, struct mmc_card *card)
1341 {
1342         int err;
1343
1344         mmc_claim_host(card->host);
1345         err = mmc_set_blocklen(card, 512);
1346         mmc_release_host(card->host);
1347
1348         if (err) {
1349                 printk(KERN_ERR "%s: unable to set block size to 512: %d\n",
1350                         md->disk->disk_name, err);
1351                 return -EINVAL;
1352         }
1353
1354         return 0;
1355 }
1356
1357 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1358 {
1359         if (md) {
1360                 if (md->disk->flags & GENHD_FL_UP) {
1361                         device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1362
1363                         /* Stop new requests from getting into the queue */
1364                         del_gendisk(md->disk);
1365                 }
1366
1367                 /* Then flush out any already in there */
1368                 mmc_cleanup_queue(&md->queue);
1369                 mmc_blk_put(md);
1370         }
1371 }
1372
1373 static void mmc_blk_remove_parts(struct mmc_card *card,
1374                                  struct mmc_blk_data *md)
1375 {
1376         struct list_head *pos, *q;
1377         struct mmc_blk_data *part_md;
1378
1379         __clear_bit(md->name_idx, name_use);
1380         list_for_each_safe(pos, q, &md->part) {
1381                 part_md = list_entry(pos, struct mmc_blk_data, part);
1382                 list_del(pos);
1383                 mmc_blk_remove_req(part_md);
1384         }
1385 }
1386
1387 static int mmc_add_disk(struct mmc_blk_data *md)
1388 {
1389         int ret;
1390
1391         add_disk(md->disk);
1392         md->force_ro.show = force_ro_show;
1393         md->force_ro.store = force_ro_store;
1394         sysfs_attr_init(&md->force_ro.attr);
1395         md->force_ro.attr.name = "force_ro";
1396         md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1397         ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1398         if (ret)
1399                 del_gendisk(md->disk);
1400
1401         return ret;
1402 }
1403
1404 static const struct mmc_fixup blk_fixups[] =
1405 {
1406         MMC_FIXUP("SEM02G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1407         MMC_FIXUP("SEM04G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1408         MMC_FIXUP("SEM08G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1409         MMC_FIXUP("SEM16G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1410         MMC_FIXUP("SEM32G", 0x2, 0x100, add_quirk, MMC_QUIRK_INAND_CMD38),
1411
1412         /*
1413          * Some MMC cards experience performance degradation with CMD23
1414          * instead of CMD12-bounded multiblock transfers. For now we'll
1415          * black list what's bad...
1416          * - Certain Toshiba cards.
1417          *
1418          * N.B. This doesn't affect SD cards.
1419          */
1420         MMC_FIXUP("MMC08G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1421                   MMC_QUIRK_BLK_NO_CMD23),
1422         MMC_FIXUP("MMC16G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1423                   MMC_QUIRK_BLK_NO_CMD23),
1424         MMC_FIXUP("MMC32G", 0x11, CID_OEMID_ANY, add_quirk_mmc,
1425                   MMC_QUIRK_BLK_NO_CMD23),
1426         END_FIXUP
1427 };
1428
1429 static int mmc_blk_probe(struct mmc_card *card)
1430 {
1431         struct mmc_blk_data *md, *part_md;
1432         int err;
1433         char cap_str[10];
1434
1435         /*
1436          * Check that the card supports the command class(es) we need.
1437          */
1438         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1439                 return -ENODEV;
1440
1441         md = mmc_blk_alloc(card);
1442         if (IS_ERR(md))
1443                 return PTR_ERR(md);
1444
1445         err = mmc_blk_set_blksize(md, card);
1446         if (err)
1447                 goto out;
1448
1449         string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1450                         cap_str, sizeof(cap_str));
1451         printk(KERN_INFO "%s: %s %s %s %s\n",
1452                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1453                 cap_str, md->read_only ? "(ro)" : "");
1454
1455         if (mmc_blk_alloc_parts(card, md))
1456                 goto out;
1457
1458         mmc_set_drvdata(card, md);
1459         mmc_fixup_device(card, blk_fixups);
1460
1461 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1462         mmc_set_bus_resume_policy(card->host, 1);
1463 #endif
1464         if (mmc_add_disk(md))
1465                 goto out;
1466
1467         list_for_each_entry(part_md, &md->part, part) {
1468                 if (mmc_add_disk(part_md))
1469                         goto out;
1470         }
1471         return 0;
1472
1473  out:
1474         mmc_blk_remove_parts(card, md);
1475         mmc_blk_remove_req(md);
1476         return err;
1477 }
1478
1479 static void mmc_blk_remove(struct mmc_card *card)
1480 {
1481         struct mmc_blk_data *md = mmc_get_drvdata(card);
1482
1483         mmc_blk_remove_parts(card, md);
1484         mmc_claim_host(card->host);
1485         mmc_blk_part_switch(card, md);
1486         mmc_release_host(card->host);
1487         mmc_blk_remove_req(md);
1488         mmc_set_drvdata(card, NULL);
1489 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1490         mmc_set_bus_resume_policy(card->host, 0);
1491 #endif
1492 }
1493
1494 #ifdef CONFIG_PM
1495 static int mmc_blk_suspend(struct mmc_card *card, pm_message_t state)
1496 {
1497         struct mmc_blk_data *part_md;
1498         struct mmc_blk_data *md = mmc_get_drvdata(card);
1499
1500         if (md) {
1501                 mmc_queue_suspend(&md->queue);
1502                 list_for_each_entry(part_md, &md->part, part) {
1503                         mmc_queue_suspend(&part_md->queue);
1504                 }
1505         }
1506         return 0;
1507 }
1508
1509 static int mmc_blk_resume(struct mmc_card *card)
1510 {
1511         struct mmc_blk_data *part_md;
1512         struct mmc_blk_data *md = mmc_get_drvdata(card);
1513
1514         if (md) {
1515 #ifndef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1516                 mmc_blk_set_blksize(md, card);
1517 #endif
1518
1519                 /*
1520                  * Resume involves the card going into idle state,
1521                  * so current partition is always the main one.
1522                  */
1523                 md->part_curr = md->part_type;
1524                 mmc_queue_resume(&md->queue);
1525                 list_for_each_entry(part_md, &md->part, part) {
1526                         mmc_queue_resume(&part_md->queue);
1527                 }
1528         }
1529         return 0;
1530 }
1531 #else
1532 #define mmc_blk_suspend NULL
1533 #define mmc_blk_resume  NULL
1534 #endif
1535
1536 static struct mmc_driver mmc_driver = {
1537         .drv            = {
1538                 .name   = "mmcblk",
1539         },
1540         .probe          = mmc_blk_probe,
1541         .remove         = mmc_blk_remove,
1542         .suspend        = mmc_blk_suspend,
1543         .resume         = mmc_blk_resume,
1544 };
1545
1546 static int __init mmc_blk_init(void)
1547 {
1548         int res;
1549
1550         if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1551                 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1552
1553         max_devices = 256 / perdev_minors;
1554
1555         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1556         if (res)
1557                 goto out;
1558
1559         res = mmc_register_driver(&mmc_driver);
1560         if (res)
1561                 goto out2;
1562
1563         return 0;
1564  out2:
1565         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1566  out:
1567         return res;
1568 }
1569
1570 static void __exit mmc_blk_exit(void)
1571 {
1572         mmc_unregister_driver(&mmc_driver);
1573         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1574 }
1575
1576 module_init(mmc_blk_init);
1577 module_exit(mmc_blk_exit);
1578
1579 MODULE_LICENSE("GPL");
1580 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
1581