mmc: rk_sdmmc: add MMC_DW_SKIP_CACHE_OP for data manipulation
[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 #include <linux/pm_runtime.h>
38
39
40 #include <linux/mmc/ioctl.h>
41 #include <linux/mmc/card.h>
42 #include <linux/mmc/host.h>
43 #include <linux/mmc/mmc.h>
44 #include <linux/mmc/sd.h>
45
46 #include <asm/uaccess.h>
47
48 #include "queue.h"
49
50 MODULE_ALIAS("mmc:block");
51 #ifdef MODULE_PARAM_PREFIX
52 #undef MODULE_PARAM_PREFIX
53 #endif
54 #define MODULE_PARAM_PREFIX "mmcblk."
55
56 #define INAND_CMD38_ARG_EXT_CSD  113
57 #define INAND_CMD38_ARG_ERASE    0x00
58 #define INAND_CMD38_ARG_TRIM     0x01
59 #define INAND_CMD38_ARG_SECERASE 0x80
60 #define INAND_CMD38_ARG_SECTRIM1 0x81
61 #define INAND_CMD38_ARG_SECTRIM2 0x88
62 #define MMC_BLK_TIMEOUT_MS  (10 * 60 * 1000)        /* 10 minute timeout */
63 #define MMC_SANITIZE_REQ_TIMEOUT 240000
64 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
65
66 #define mmc_req_rel_wr(req)     (((req->cmd_flags & REQ_FUA) || \
67                                   (req->cmd_flags & REQ_META)) && \
68                                   (rq_data_dir(req) == WRITE))
69 #define PACKED_CMD_VER  0x01
70 #define PACKED_CMD_WR   0x02
71
72 static DEFINE_MUTEX(block_mutex);
73
74 /*
75  * The defaults come from config options but can be overriden by module
76  * or bootarg options.
77  */
78 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
79
80 /*
81  * We've only got one major, so number of mmcblk devices is
82  * limited to 256 / number of minors per device.
83  */
84 static int max_devices;
85
86 /* 256 minors, so at most 256 separate devices */
87 static DECLARE_BITMAP(dev_use, 256);
88 static DECLARE_BITMAP(name_use, 256);
89
90 /*
91  * There is one mmc_blk_data per slot.
92  */
93 struct mmc_blk_data {
94         spinlock_t      lock;
95         struct gendisk  *disk;
96         struct mmc_queue queue;
97         struct list_head part;
98
99         unsigned int    flags;
100 #define MMC_BLK_CMD23   (1 << 0)        /* Can do SET_BLOCK_COUNT for multiblock */
101 #define MMC_BLK_REL_WR  (1 << 1)        /* MMC Reliable write support */
102 #define MMC_BLK_PACKED_CMD      (1 << 2)        /* MMC packed command support */
103
104         unsigned int    usage;
105         unsigned int    read_only;
106         unsigned int    part_type;
107         unsigned int    name_idx;
108         unsigned int    reset_done;
109 #define MMC_BLK_READ            BIT(0)
110 #define MMC_BLK_WRITE           BIT(1)
111 #define MMC_BLK_DISCARD         BIT(2)
112 #define MMC_BLK_SECDISCARD      BIT(3)
113
114         /*
115          * Only set in main mmc_blk_data associated
116          * with mmc_card with mmc_set_drvdata, and keeps
117          * track of the current selected device partition.
118          */
119         unsigned int    part_curr;
120         struct device_attribute force_ro;
121         struct device_attribute power_ro_lock;
122         int     area_type;
123 };
124
125 static DEFINE_MUTEX(open_lock);
126
127 enum {
128         MMC_PACKED_NR_IDX = -1,
129         MMC_PACKED_NR_ZERO,
130         MMC_PACKED_NR_SINGLE,
131 };
132
133 module_param(perdev_minors, int, 0444);
134 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
135
136 static inline int mmc_blk_part_switch(struct mmc_card *card,
137                                       struct mmc_blk_data *md);
138 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
139
140 static inline void mmc_blk_clear_packed(struct mmc_queue_req *mqrq)
141 {
142         struct mmc_packed *packed = mqrq->packed;
143
144         BUG_ON(!packed);
145
146         mqrq->cmd_type = MMC_PACKED_NONE;
147         packed->nr_entries = MMC_PACKED_NR_ZERO;
148         packed->idx_failure = MMC_PACKED_NR_IDX;
149         packed->retries = 0;
150         packed->blocks = 0;
151 }
152
153 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
154 {
155         struct mmc_blk_data *md;
156
157         mutex_lock(&open_lock);
158         md = disk->private_data;
159         if (md && md->usage == 0)
160                 md = NULL;
161         if (md)
162                 md->usage++;
163         mutex_unlock(&open_lock);
164
165         return md;
166 }
167
168 static inline int mmc_get_devidx(struct gendisk *disk)
169 {
170         int devidx = disk->first_minor / perdev_minors;
171         return devidx;
172 }
173
174 static void mmc_blk_put(struct mmc_blk_data *md)
175 {
176         mutex_lock(&open_lock);
177         md->usage--;
178         if (md->usage == 0) {
179                 int devidx = mmc_get_devidx(md->disk);
180                 blk_cleanup_queue(md->queue.queue);
181
182                 __clear_bit(devidx, dev_use);
183
184                 put_disk(md->disk);
185                 kfree(md);
186         }
187         mutex_unlock(&open_lock);
188 }
189
190 static ssize_t power_ro_lock_show(struct device *dev,
191                 struct device_attribute *attr, char *buf)
192 {
193         int ret;
194         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
195         struct mmc_card *card = md->queue.card;
196         int locked = 0;
197
198         if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
199                 locked = 2;
200         else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
201                 locked = 1;
202
203         ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
204
205         return ret;
206 }
207
208 static ssize_t power_ro_lock_store(struct device *dev,
209                 struct device_attribute *attr, const char *buf, size_t count)
210 {
211         int ret;
212         struct mmc_blk_data *md, *part_md;
213         struct mmc_card *card;
214         unsigned long set;
215
216         if (kstrtoul(buf, 0, &set))
217                 return -EINVAL;
218
219         if (set != 1)
220                 return count;
221
222         md = mmc_blk_get(dev_to_disk(dev));
223         card = md->queue.card;
224
225         mmc_get_card(card);
226
227         ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
228                                 card->ext_csd.boot_ro_lock |
229                                 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
230                                 card->ext_csd.part_time);
231         if (ret)
232                 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
233         else
234                 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
235
236         mmc_put_card(card);
237
238         if (!ret) {
239                 pr_info("%s: Locking boot partition ro until next power on\n",
240                         md->disk->disk_name);
241                 set_disk_ro(md->disk, 1);
242
243                 list_for_each_entry(part_md, &md->part, part)
244                         if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
245                                 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
246                                 set_disk_ro(part_md->disk, 1);
247                         }
248         }
249
250         mmc_blk_put(md);
251         return count;
252 }
253
254 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
255                              char *buf)
256 {
257         int ret;
258         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
259
260         ret = snprintf(buf, PAGE_SIZE, "%d\n",
261                        get_disk_ro(dev_to_disk(dev)) ^
262                        md->read_only);
263         mmc_blk_put(md);
264         return ret;
265 }
266
267 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
268                               const char *buf, size_t count)
269 {
270         int ret;
271         char *end;
272         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
273         unsigned long set = simple_strtoul(buf, &end, 0);
274         if (end == buf) {
275                 ret = -EINVAL;
276                 goto out;
277         }
278
279         set_disk_ro(dev_to_disk(dev), set || md->read_only);
280         ret = count;
281 out:
282         mmc_blk_put(md);
283         return ret;
284 }
285
286 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
287 {
288         struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
289         int ret = -ENXIO;
290
291         mutex_lock(&block_mutex);
292         if (md) {
293                 if (md->usage == 2)
294                         check_disk_change(bdev);
295                 ret = 0;
296
297                 if ((mode & FMODE_WRITE) && md->read_only) {
298                         mmc_blk_put(md);
299                         ret = -EROFS;
300                 }
301         }
302         mutex_unlock(&block_mutex);
303
304         return ret;
305 }
306
307 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
308 {
309         struct mmc_blk_data *md = disk->private_data;
310
311         mutex_lock(&block_mutex);
312         mmc_blk_put(md);
313         mutex_unlock(&block_mutex);
314 }
315
316 static int
317 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
318 {
319         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
320         geo->heads = 4;
321         geo->sectors = 16;
322         return 0;
323 }
324
325 struct mmc_blk_ioc_data {
326         struct mmc_ioc_cmd ic;
327         unsigned char *buf;
328         u64 buf_bytes;
329 };
330
331 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
332         struct mmc_ioc_cmd __user *user)
333 {
334         struct mmc_blk_ioc_data *idata;
335         int err;
336
337         idata = kzalloc(sizeof(*idata), GFP_KERNEL);
338         if (!idata) {
339                 err = -ENOMEM;
340                 goto out;
341         }
342
343         if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
344                 err = -EFAULT;
345                 goto idata_err;
346         }
347
348         idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
349         if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
350                 err = -EOVERFLOW;
351                 goto idata_err;
352         }
353
354         if (!idata->buf_bytes)
355                 return idata;
356
357         idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
358         if (!idata->buf) {
359                 err = -ENOMEM;
360                 goto idata_err;
361         }
362
363         if (copy_from_user(idata->buf, (void __user *)(unsigned long)
364                                         idata->ic.data_ptr, idata->buf_bytes)) {
365                 err = -EFAULT;
366                 goto copy_err;
367         }
368
369         return idata;
370
371 copy_err:
372         kfree(idata->buf);
373 idata_err:
374         kfree(idata);
375 out:
376         return ERR_PTR(err);
377 }
378
379 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
380                                        u32 retries_max)
381 {
382         int err;
383         u32 retry_count = 0;
384
385         if (!status || !retries_max)
386                 return -EINVAL;
387
388         do {
389                 err = get_card_status(card, status, 5);
390                 if (err)
391                         break;
392
393                 if (!R1_STATUS(*status) &&
394                                 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
395                         break; /* RPMB programming operation complete */
396
397                 /*
398                  * Rechedule to give the MMC device a chance to continue
399                  * processing the previous command without being polled too
400                  * frequently.
401                  */
402                 usleep_range(1000, 5000);
403         } while (++retry_count < retries_max);
404
405         if (retry_count == retries_max)
406                 err = -EPERM;
407
408         return err;
409 }
410
411 static int ioctl_do_sanitize(struct mmc_card *card)
412 {
413         int err;
414
415         if (!(mmc_can_sanitize(card) &&
416               (card->host->caps2 & MMC_CAP2_SANITIZE))) {
417                         pr_warn("%s: %s - SANITIZE is not supported\n",
418                                 mmc_hostname(card->host), __func__);
419                         err = -EOPNOTSUPP;
420                         goto out;
421         }
422
423         pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
424                 mmc_hostname(card->host), __func__);
425
426         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
427                                         EXT_CSD_SANITIZE_START, 1,
428                                         MMC_SANITIZE_REQ_TIMEOUT);
429
430         if (err)
431                 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
432                        mmc_hostname(card->host), __func__, err);
433
434         pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
435                                              __func__);
436 out:
437         return err;
438 }
439
440 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
441         struct mmc_ioc_cmd __user *ic_ptr)
442 {
443         struct mmc_blk_ioc_data *idata;
444         struct mmc_blk_data *md;
445         struct mmc_card *card;
446         struct mmc_command cmd = {0};
447         struct mmc_data data = {0};
448         struct mmc_request mrq = {NULL};
449         struct scatterlist sg;
450         int err;
451         int is_rpmb = false;
452         u32 status = 0;
453
454         /*
455          * The caller must have CAP_SYS_RAWIO, and must be calling this on the
456          * whole block device, not on a partition.  This prevents overspray
457          * between sibling partitions.
458          */
459         if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
460                 return -EPERM;
461
462         idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
463         if (IS_ERR(idata))
464                 return PTR_ERR(idata);
465
466         md = mmc_blk_get(bdev->bd_disk);
467         if (!md) {
468                 err = -EINVAL;
469                 goto cmd_err;
470         }
471
472         if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
473                 is_rpmb = true;
474
475         card = md->queue.card;
476         if (IS_ERR(card)) {
477                 err = PTR_ERR(card);
478                 goto cmd_done;
479         }
480
481         cmd.opcode = idata->ic.opcode;
482         cmd.arg = idata->ic.arg;
483         cmd.flags = idata->ic.flags;
484
485         if (idata->buf_bytes) {
486                 data.sg = &sg;
487                 data.sg_len = 1;
488                 data.blksz = idata->ic.blksz;
489                 data.blocks = idata->ic.blocks;
490
491                 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
492
493                 if (idata->ic.write_flag)
494                         data.flags = MMC_DATA_WRITE;
495                 else
496                         data.flags = MMC_DATA_READ;
497
498                 /* data.flags must already be set before doing this. */
499                 mmc_set_data_timeout(&data, card);
500
501                 /* Allow overriding the timeout_ns for empirical tuning. */
502                 if (idata->ic.data_timeout_ns)
503                         data.timeout_ns = idata->ic.data_timeout_ns;
504
505                 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
506                         /*
507                          * Pretend this is a data transfer and rely on the
508                          * host driver to compute timeout.  When all host
509                          * drivers support cmd.cmd_timeout for R1B, this
510                          * can be changed to:
511                          *
512                          *     mrq.data = NULL;
513                          *     cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
514                          */
515                         data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
516                 }
517
518                 mrq.data = &data;
519         }
520
521         mrq.cmd = &cmd;
522
523         mmc_get_card(card);
524
525         err = mmc_blk_part_switch(card, md);
526         if (err)
527                 goto cmd_rel_host;
528
529         if (idata->ic.is_acmd) {
530                 err = mmc_app_cmd(card->host, card);
531                 if (err)
532                         goto cmd_rel_host;
533         }
534
535         if (is_rpmb) {
536                 err = mmc_set_blockcount(card, data.blocks,
537                         idata->ic.write_flag & (1 << 31));
538                 if (err)
539                         goto cmd_rel_host;
540         }
541
542         if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
543             (cmd.opcode == MMC_SWITCH)) {
544                 err = ioctl_do_sanitize(card);
545
546                 if (err)
547                         pr_err("%s: ioctl_do_sanitize() failed. err = %d",
548                                __func__, err);
549
550                 goto cmd_rel_host;
551         }
552
553         mmc_wait_for_req(card->host, &mrq);
554
555         if (cmd.error) {
556                 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
557                                                 __func__, cmd.error);
558                 err = cmd.error;
559                 goto cmd_rel_host;
560         }
561         if (data.error) {
562                 dev_err(mmc_dev(card->host), "%s: data error %d\n",
563                                                 __func__, data.error);
564                 err = data.error;
565                 goto cmd_rel_host;
566         }
567
568         /*
569          * According to the SD specs, some commands require a delay after
570          * issuing the command.
571          */
572         if (idata->ic.postsleep_min_us)
573                 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
574
575         if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
576                 err = -EFAULT;
577                 goto cmd_rel_host;
578         }
579
580         if (!idata->ic.write_flag) {
581                 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
582                                                 idata->buf, idata->buf_bytes)) {
583                         err = -EFAULT;
584                         goto cmd_rel_host;
585                 }
586         }
587
588         if (is_rpmb) {
589                 /*
590                  * Ensure RPMB command has completed by polling CMD13
591                  * "Send Status".
592                  */
593                 err = ioctl_rpmb_card_status_poll(card, &status, 5);
594                 if (err)
595                         dev_err(mmc_dev(card->host),
596                                         "%s: Card Status=0x%08X, error %d\n",
597                                         __func__, status, err);
598         }
599
600 cmd_rel_host:
601         mmc_put_card(card);
602
603 cmd_done:
604         mmc_blk_put(md);
605 cmd_err:
606         kfree(idata->buf);
607         kfree(idata);
608         return err;
609 }
610
611 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
612         unsigned int cmd, unsigned long arg)
613 {
614         int ret = -EINVAL;
615         if (cmd == MMC_IOC_CMD)
616                 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
617         return ret;
618 }
619
620 #ifdef CONFIG_COMPAT
621 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
622         unsigned int cmd, unsigned long arg)
623 {
624         return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
625 }
626 #endif
627
628 static const struct block_device_operations mmc_bdops = {
629         .open                   = mmc_blk_open,
630         .release                = mmc_blk_release,
631         .getgeo                 = mmc_blk_getgeo,
632         .owner                  = THIS_MODULE,
633         .ioctl                  = mmc_blk_ioctl,
634 #ifdef CONFIG_COMPAT
635         .compat_ioctl           = mmc_blk_compat_ioctl,
636 #endif
637 };
638
639 static inline int mmc_blk_part_switch(struct mmc_card *card,
640                                       struct mmc_blk_data *md)
641 {
642         int ret;
643         struct mmc_blk_data *main_md = mmc_get_drvdata(card);
644
645         if (main_md->part_curr == md->part_type)
646                 return 0;
647
648         if (mmc_card_mmc(card)) {
649                 u8 part_config = card->ext_csd.part_config;
650
651                 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
652                 part_config |= md->part_type;
653
654                 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
655                                  EXT_CSD_PART_CONFIG, part_config,
656                                  card->ext_csd.part_time);
657                 if (ret)
658                         return ret;
659
660                 card->ext_csd.part_config = part_config;
661         }
662
663         main_md->part_curr = md->part_type;
664         return 0;
665 }
666
667 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
668 {
669         int err;
670         u32 result;
671         __be32 *blocks;
672
673         struct mmc_request mrq = {NULL};
674         struct mmc_command cmd = {0};
675         struct mmc_data data = {0};
676
677         struct scatterlist sg;
678
679         cmd.opcode = MMC_APP_CMD;
680         cmd.arg = card->rca << 16;
681         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
682
683         err = mmc_wait_for_cmd(card->host, &cmd, 0);
684         if (err)
685                 return (u32)-1;
686         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
687                 return (u32)-1;
688
689         memset(&cmd, 0, sizeof(struct mmc_command));
690
691         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
692         cmd.arg = 0;
693         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
694
695         data.blksz = 4;
696         data.blocks = 1;
697         data.flags = MMC_DATA_READ;
698         data.sg = &sg;
699         data.sg_len = 1;
700         mmc_set_data_timeout(&data, card);
701
702         mrq.cmd = &cmd;
703         mrq.data = &data;
704
705         blocks = kmalloc(4, GFP_KERNEL);
706         if (!blocks)
707                 return (u32)-1;
708
709         sg_init_one(&sg, blocks, 4);
710
711         mmc_wait_for_req(card->host, &mrq);
712
713         result = ntohl(*blocks);
714         kfree(blocks);
715
716         if (cmd.error || data.error)
717                 result = (u32)-1;
718
719         return result;
720 }
721
722 static int send_stop(struct mmc_card *card, u32 *status)
723 {
724         struct mmc_command cmd = {0};
725         int err;
726
727         cmd.opcode = MMC_STOP_TRANSMISSION;
728         cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
729         err = mmc_wait_for_cmd(card->host, &cmd, 5);
730         if (err == 0)
731                 *status = cmd.resp[0];
732         return err;
733 }
734
735 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
736 {
737         struct mmc_command cmd = {0};
738         int err;
739
740         cmd.opcode = MMC_SEND_STATUS;
741         if (!mmc_host_is_spi(card->host))
742                 cmd.arg = card->rca << 16;
743         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
744         err = mmc_wait_for_cmd(card->host, &cmd, retries);
745         if (err == 0)
746                 *status = cmd.resp[0];
747         return err;
748 }
749
750 #define ERR_NOMEDIUM    3
751 #define ERR_RETRY       2
752 #define ERR_ABORT       1
753 #define ERR_CONTINUE    0
754
755 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
756         bool status_valid, u32 status)
757 {
758         switch (error) {
759         case -EILSEQ:
760                 /* response crc error, retry the r/w cmd */
761                 pr_err("%s: %s sending %s command, card status %#x\n",
762                         req->rq_disk->disk_name, "response CRC error",
763                         name, status);
764                 return ERR_RETRY;
765
766         case -ETIMEDOUT:
767                 pr_err("%s: %s sending %s command, card status %#x\n",
768                         req->rq_disk->disk_name, "timed out", name, status);
769
770                 /* If the status cmd initially failed, retry the r/w cmd */
771                 if (!status_valid) {
772                         pr_err("%s: status not valid, retrying timeout\n", req->rq_disk->disk_name);
773                         return ERR_RETRY;
774                 }
775                 /*
776                  * If it was a r/w cmd crc error, or illegal command
777                  * (eg, issued in wrong state) then retry - we should
778                  * have corrected the state problem above.
779                  */
780                 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
781                         pr_err("%s: command error, retrying timeout\n", req->rq_disk->disk_name);
782                         return ERR_RETRY;
783                 }
784
785                 /* Otherwise abort the command */
786                 pr_err("%s: not retrying timeout\n", req->rq_disk->disk_name);
787                 return ERR_ABORT;
788
789         default:
790                 /* We don't understand the error code the driver gave us */
791                 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
792                        req->rq_disk->disk_name, error, status);
793                 return ERR_ABORT;
794         }
795 }
796
797 /*
798  * Initial r/w and stop cmd error recovery.
799  * We don't know whether the card received the r/w cmd or not, so try to
800  * restore things back to a sane state.  Essentially, we do this as follows:
801  * - Obtain card status.  If the first attempt to obtain card status fails,
802  *   the status word will reflect the failed status cmd, not the failed
803  *   r/w cmd.  If we fail to obtain card status, it suggests we can no
804  *   longer communicate with the card.
805  * - Check the card state.  If the card received the cmd but there was a
806  *   transient problem with the response, it might still be in a data transfer
807  *   mode.  Try to send it a stop command.  If this fails, we can't recover.
808  * - If the r/w cmd failed due to a response CRC error, it was probably
809  *   transient, so retry the cmd.
810  * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
811  * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
812  *   illegal cmd, retry.
813  * Otherwise we don't understand what happened, so abort.
814  */
815 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
816         struct mmc_blk_request *brq, int *ecc_err, int *gen_err)
817 {
818         bool prev_cmd_status_valid = true;
819         u32 status, stop_status = 0;
820         int err, retry;
821
822         if (mmc_card_removed(card))
823                 return ERR_NOMEDIUM;
824
825         /*
826          * Try to get card status which indicates both the card state
827          * and why there was no response.  If the first attempt fails,
828          * we can't be sure the returned status is for the r/w command.
829          */
830         for (retry = 2; retry >= 0; retry--) {
831                 err = get_card_status(card, &status, 0);
832                 if (!err)
833                         break;
834
835                 prev_cmd_status_valid = false;
836                 pr_err("%s: error %d sending status command, %sing\n",
837                        req->rq_disk->disk_name, err, retry ? "retry" : "abort");
838         }
839
840         /* We couldn't get a response from the card.  Give up. */
841         if (err) {
842                 /* Check if the card is removed */
843                 if (mmc_detect_card_removed(card->host))
844                         return ERR_NOMEDIUM;
845                 return ERR_ABORT;
846         }
847
848         /* Flag ECC errors */
849         if ((status & R1_CARD_ECC_FAILED) ||
850             (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
851             (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
852                 *ecc_err = 1;
853
854         /* Flag General errors */
855         if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
856                 if ((status & R1_ERROR) ||
857                         (brq->stop.resp[0] & R1_ERROR)) {
858                         pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
859                                req->rq_disk->disk_name, __func__,
860                                brq->stop.resp[0], status);
861                         *gen_err = 1;
862                 }
863
864         /*
865          * Check the current card state.  If it is in some data transfer
866          * mode, tell it to stop (and hopefully transition back to TRAN.)
867          */
868         if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
869             R1_CURRENT_STATE(status) == R1_STATE_RCV) {
870                 err = send_stop(card, &stop_status);
871                 if (err)
872                         pr_err("%s: error %d sending stop command\n",
873                                req->rq_disk->disk_name, err);
874
875                 /*
876                  * If the stop cmd also timed out, the card is probably
877                  * not present, so abort.  Other errors are bad news too.
878                  */
879                 if (err)
880                         return ERR_ABORT;
881                 if (stop_status & R1_CARD_ECC_FAILED)
882                         *ecc_err = 1;
883                 if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
884                         if (stop_status & R1_ERROR) {
885                                 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
886                                        req->rq_disk->disk_name, __func__,
887                                        stop_status);
888                                 *gen_err = 1;
889                         }
890         }
891
892         /* Check for set block count errors */
893         if (brq->sbc.error)
894                 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
895                                 prev_cmd_status_valid, status);
896
897         /* Check for r/w command errors */
898         if (brq->cmd.error)
899                 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
900                                 prev_cmd_status_valid, status);
901
902         /* Data errors */
903         if (!brq->stop.error)
904                 return ERR_CONTINUE;
905
906         /* Now for stop errors.  These aren't fatal to the transfer. */
907         pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
908                req->rq_disk->disk_name, brq->stop.error,
909                brq->cmd.resp[0], status);
910
911         /*
912          * Subsitute in our own stop status as this will give the error
913          * state which happened during the execution of the r/w command.
914          */
915         if (stop_status) {
916                 brq->stop.resp[0] = stop_status;
917                 brq->stop.error = 0;
918         }
919         return ERR_CONTINUE;
920 }
921
922 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
923                          int type)
924 {
925         int err;
926
927         if (md->reset_done & type)
928                 return -EEXIST;
929
930         md->reset_done |= type;
931         err = mmc_hw_reset(host);
932         /* Ensure we switch back to the correct partition */
933         if (err != -EOPNOTSUPP) {
934                 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
935                 int part_err;
936
937                 main_md->part_curr = main_md->part_type;
938                 part_err = mmc_blk_part_switch(host->card, md);
939                 if (part_err) {
940                         /*
941                          * We have failed to get back into the correct
942                          * partition, so we need to abort the whole request.
943                          */
944                         return -ENODEV;
945                 }
946         }
947         return err;
948 }
949
950 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
951 {
952         md->reset_done &= ~type;
953 }
954
955 int mmc_access_rpmb(struct mmc_queue *mq)
956 {
957         struct mmc_blk_data *md = mq->data;
958         /*
959          * If this is a RPMB partition access, return ture
960          */
961         if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
962                 return true;
963
964         return false;
965 }
966
967 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
968 {
969         struct mmc_blk_data *md = mq->data;
970         struct mmc_card *card = md->queue.card;
971         unsigned int from, nr, arg;
972         int err = 0, type = MMC_BLK_DISCARD;
973
974         if (!mmc_can_erase(card)) {
975                 err = -EOPNOTSUPP;
976                 goto out;
977         }
978
979         from = blk_rq_pos(req);
980         nr = blk_rq_sectors(req);
981
982         if (mmc_can_discard(card))
983                 arg = MMC_DISCARD_ARG;
984         else if (mmc_can_trim(card))
985                 arg = MMC_TRIM_ARG;
986         else
987                 arg = MMC_ERASE_ARG;
988 retry:
989         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
990                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
991                                  INAND_CMD38_ARG_EXT_CSD,
992                                  arg == MMC_TRIM_ARG ?
993                                  INAND_CMD38_ARG_TRIM :
994                                  INAND_CMD38_ARG_ERASE,
995                                  0);
996                 if (err)
997                         goto out;
998         }
999         err = mmc_erase(card, from, nr, arg);
1000 out:
1001         if (err == -EIO && !mmc_blk_reset(md, card->host, type))
1002                 goto retry;
1003         if (!err)
1004                 mmc_blk_reset_success(md, type);
1005         blk_end_request(req, err, blk_rq_bytes(req));
1006
1007         return err ? 0 : 1;
1008 }
1009
1010 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1011                                        struct request *req)
1012 {
1013         struct mmc_blk_data *md = mq->data;
1014         struct mmc_card *card = md->queue.card;
1015         unsigned int from, nr, arg;
1016         int err = 0, type = MMC_BLK_SECDISCARD;
1017
1018         if (!(mmc_can_secure_erase_trim(card))) {
1019                 err = -EOPNOTSUPP;
1020                 goto out;
1021         }
1022
1023         from = blk_rq_pos(req);
1024         nr = blk_rq_sectors(req);
1025
1026         if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1027                 arg = MMC_SECURE_TRIM1_ARG;
1028         else
1029                 arg = MMC_SECURE_ERASE_ARG;
1030
1031 retry:
1032         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1033                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1034                                  INAND_CMD38_ARG_EXT_CSD,
1035                                  arg == MMC_SECURE_TRIM1_ARG ?
1036                                  INAND_CMD38_ARG_SECTRIM1 :
1037                                  INAND_CMD38_ARG_SECERASE,
1038                                  0);
1039                 if (err)
1040                         goto out_retry;
1041         }
1042
1043         err = mmc_erase(card, from, nr, arg);
1044         if (err == -EIO)
1045                 goto out_retry;
1046         if (err)
1047                 goto out;
1048
1049         if (arg == MMC_SECURE_TRIM1_ARG) {
1050                 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1051                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1052                                          INAND_CMD38_ARG_EXT_CSD,
1053                                          INAND_CMD38_ARG_SECTRIM2,
1054                                          0);
1055                         if (err)
1056                                 goto out_retry;
1057                 }
1058
1059                 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1060                 if (err == -EIO)
1061                         goto out_retry;
1062                 if (err)
1063                         goto out;
1064         }
1065
1066 out_retry:
1067         if (err && !mmc_blk_reset(md, card->host, type))
1068                 goto retry;
1069         if (!err)
1070                 mmc_blk_reset_success(md, type);
1071 out:
1072         blk_end_request(req, err, blk_rq_bytes(req));
1073
1074         return err ? 0 : 1;
1075 }
1076
1077 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1078 {
1079         struct mmc_blk_data *md = mq->data;
1080         struct mmc_card *card = md->queue.card;
1081         int ret = 0;
1082
1083         ret = mmc_flush_cache(card);
1084         if (ret)
1085                 ret = -EIO;
1086
1087         blk_end_request_all(req, ret);
1088
1089         return ret ? 0 : 1;
1090 }
1091
1092 /*
1093  * Reformat current write as a reliable write, supporting
1094  * both legacy and the enhanced reliable write MMC cards.
1095  * In each transfer we'll handle only as much as a single
1096  * reliable write can handle, thus finish the request in
1097  * partial completions.
1098  */
1099 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1100                                     struct mmc_card *card,
1101                                     struct request *req)
1102 {
1103         if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1104                 /* Legacy mode imposes restrictions on transfers. */
1105                 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1106                         brq->data.blocks = 1;
1107
1108                 if (brq->data.blocks > card->ext_csd.rel_sectors)
1109                         brq->data.blocks = card->ext_csd.rel_sectors;
1110                 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1111                         brq->data.blocks = 1;
1112         }
1113 }
1114
1115 #define CMD_ERRORS                                                      \
1116         (R1_OUT_OF_RANGE |      /* Command argument out of range */     \
1117          R1_ADDRESS_ERROR |     /* Misaligned address */                \
1118          R1_BLOCK_LEN_ERROR |   /* Transferred block length incorrect */\
1119          R1_WP_VIOLATION |      /* Tried to write to protected block */ \
1120          R1_CC_ERROR |          /* Card controller error */             \
1121          R1_ERROR)              /* General/unknown error */
1122
1123 static int mmc_blk_err_check(struct mmc_card *card,
1124                              struct mmc_async_req *areq)
1125 {
1126         struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1127                                                     mmc_active);
1128         struct mmc_blk_request *brq = &mq_mrq->brq;
1129         struct request *req = mq_mrq->req;
1130         int need_retune = card->host->need_retune;
1131         int ecc_err = 0, gen_err = 0;
1132
1133         /*
1134          * sbc.error indicates a problem with the set block count
1135          * command.  No data will have been transferred.
1136          *
1137          * cmd.error indicates a problem with the r/w command.  No
1138          * data will have been transferred.
1139          *
1140          * stop.error indicates a problem with the stop command.  Data
1141          * may have been transferred, or may still be transferring.
1142          */
1143         if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1144             brq->data.error) {
1145                 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1146                 case ERR_RETRY:
1147                         return MMC_BLK_RETRY;
1148                 case ERR_ABORT:
1149                         return MMC_BLK_ABORT;
1150                 case ERR_NOMEDIUM:
1151                         return MMC_BLK_NOMEDIUM;
1152                 case ERR_CONTINUE:
1153                         break;
1154                 }
1155         }
1156
1157         /*
1158          * Check for errors relating to the execution of the
1159          * initial command - such as address errors.  No data
1160          * has been transferred.
1161          */
1162         if (brq->cmd.resp[0] & CMD_ERRORS) {
1163                 pr_err("%s: r/w command failed, status = %#x\n",
1164                        req->rq_disk->disk_name, brq->cmd.resp[0]);
1165                 return MMC_BLK_ABORT;
1166         }
1167
1168         /*
1169          * Everything else is either success, or a data error of some
1170          * kind.  If it was a write, we may have transitioned to
1171          * program mode, which we have to wait for it to complete.
1172          */
1173         if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1174                 u32 status;
1175                 unsigned long timeout;
1176
1177                 /* Check stop command response */
1178                 if (brq->stop.resp[0] & R1_ERROR) {
1179                         pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1180                                req->rq_disk->disk_name, __func__,
1181                                brq->stop.resp[0]);
1182                         gen_err = 1;
1183                 }
1184
1185                 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1186                 do {
1187                         int err = get_card_status(card, &status, 5);
1188                         if (err) {
1189                                 pr_err("%s: error %d requesting status\n",
1190                                        req->rq_disk->disk_name, err);
1191                                 return MMC_BLK_CMD_ERR;
1192                         }
1193
1194                         if (status & R1_ERROR) {
1195                                 pr_err("%s: %s: general error sending status command, card status %#x\n",
1196                                        req->rq_disk->disk_name, __func__,
1197                                        status);
1198                                 gen_err = 1;
1199                         }
1200
1201                         /* Timeout if the device never becomes ready for data
1202                          * and never leaves the program state.
1203                          */
1204                         if (time_after(jiffies, timeout)) {
1205                                 pr_err("%s: Card stuck in programming state!"\
1206                                         " %s %s\n", mmc_hostname(card->host),
1207                                         req->rq_disk->disk_name, __func__);
1208
1209                                 return MMC_BLK_CMD_ERR;
1210                         }
1211                         /*
1212                          * Some cards mishandle the status bits,
1213                          * so make sure to check both the busy
1214                          * indication and the card state.
1215                          */
1216                 } while (!(status & R1_READY_FOR_DATA) ||
1217                          (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1218         }
1219
1220         /* if general error occurs, retry the write operation. */
1221         if (gen_err) {
1222                 pr_warn("%s: retrying write for general error\n",
1223                                 req->rq_disk->disk_name);
1224                 return MMC_BLK_RETRY;
1225         }
1226
1227         if (brq->data.error) {
1228                 pr_err("need_retune:%d,brq->retune_retry_done:%d.\n",need_retune,brq->retune_retry_done);
1229                 if (need_retune && !brq->retune_retry_done) {
1230                         pr_err("%s: retrying because a re-tune was needed\n",
1231                                req->rq_disk->disk_name);
1232                         brq->retune_retry_done = 1;
1233                         return MMC_BLK_RETRY;
1234                 }
1235
1236                 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1237                        req->rq_disk->disk_name, brq->data.error,
1238                        (unsigned)blk_rq_pos(req),
1239                        (unsigned)blk_rq_sectors(req),
1240                        brq->cmd.resp[0], brq->stop.resp[0]);
1241
1242                 if (rq_data_dir(req) == READ) {
1243                         if (ecc_err)
1244                                 return MMC_BLK_ECC_ERR;
1245                         return MMC_BLK_DATA_ERR;
1246                 } else {
1247                         return MMC_BLK_CMD_ERR;
1248                 }
1249         }
1250
1251         if (!brq->data.bytes_xfered)
1252                 return MMC_BLK_RETRY;
1253
1254         if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1255                 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1256                         return MMC_BLK_PARTIAL;
1257                 else
1258                         return MMC_BLK_SUCCESS;
1259         }
1260
1261         if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1262                 return MMC_BLK_PARTIAL;
1263
1264         return MMC_BLK_SUCCESS;
1265 }
1266
1267 static int mmc_blk_packed_err_check(struct mmc_card *card,
1268                                     struct mmc_async_req *areq)
1269 {
1270         struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1271                         mmc_active);
1272         struct request *req = mq_rq->req;
1273         struct mmc_packed *packed = mq_rq->packed;
1274         int err, check, status;
1275         u8 *ext_csd;
1276
1277         BUG_ON(!packed);
1278
1279         packed->retries--;
1280         check = mmc_blk_err_check(card, areq);
1281         err = get_card_status(card, &status, 0);
1282         if (err) {
1283                 pr_err("%s: error %d sending status command\n",
1284                        req->rq_disk->disk_name, err);
1285                 return MMC_BLK_ABORT;
1286         }
1287
1288         if (status & R1_EXCEPTION_EVENT) {
1289                 ext_csd = kzalloc(512, GFP_KERNEL);
1290                 if (!ext_csd) {
1291                         pr_err("%s: unable to allocate buffer for ext_csd\n",
1292                                req->rq_disk->disk_name);
1293                         return -ENOMEM;
1294                 }
1295
1296                 err = mmc_send_ext_csd(card, ext_csd);
1297                 if (err) {
1298                         pr_err("%s: error %d sending ext_csd\n",
1299                                req->rq_disk->disk_name, err);
1300                         check = MMC_BLK_ABORT;
1301                         goto free;
1302                 }
1303
1304                 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1305                      EXT_CSD_PACKED_FAILURE) &&
1306                     (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1307                      EXT_CSD_PACKED_GENERIC_ERROR)) {
1308                         if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1309                             EXT_CSD_PACKED_INDEXED_ERROR) {
1310                                 packed->idx_failure =
1311                                   ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1312                                 check = MMC_BLK_PARTIAL;
1313                         }
1314                         pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1315                                "failure index: %d\n",
1316                                req->rq_disk->disk_name, packed->nr_entries,
1317                                packed->blocks, packed->idx_failure);
1318                 }
1319 free:
1320                 kfree(ext_csd);
1321         }
1322
1323         return check;
1324 }
1325
1326 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1327                                struct mmc_card *card,
1328                                int disable_multi,
1329                                struct mmc_queue *mq)
1330 {
1331         u32 readcmd, writecmd;
1332         struct mmc_blk_request *brq = &mqrq->brq;
1333         struct request *req = mqrq->req;
1334         struct mmc_blk_data *md = mq->data;
1335         bool do_data_tag;
1336
1337         /*
1338          * Reliable writes are used to implement Forced Unit Access and
1339          * REQ_META accesses, and are supported only on MMCs.
1340          *
1341          * XXX: this really needs a good explanation of why REQ_META
1342          * is treated special.
1343          */
1344         bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1345                           (req->cmd_flags & REQ_META)) &&
1346                 (rq_data_dir(req) == WRITE) &&
1347                 (md->flags & MMC_BLK_REL_WR);
1348
1349         memset(brq, 0, sizeof(struct mmc_blk_request));
1350         brq->mrq.cmd = &brq->cmd;
1351         brq->mrq.data = &brq->data;
1352
1353         brq->cmd.arg = blk_rq_pos(req);
1354         if (!mmc_card_blockaddr(card))
1355                 brq->cmd.arg <<= 9;
1356         brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1357         brq->data.blksz = 512;
1358         brq->stop.opcode = MMC_STOP_TRANSMISSION;
1359         brq->stop.arg = 0;
1360         brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1361         brq->data.blocks = blk_rq_sectors(req);
1362
1363         /*
1364          * The block layer doesn't support all sector count
1365          * restrictions, so we need to be prepared for too big
1366          * requests.
1367          */
1368         if (brq->data.blocks > card->host->max_blk_count)
1369                 brq->data.blocks = card->host->max_blk_count;
1370
1371         if (brq->data.blocks > 1) {
1372                 /*
1373                  * After a read error, we redo the request one sector
1374                  * at a time in order to accurately determine which
1375                  * sectors can be read successfully.
1376                  */
1377                 if (disable_multi)
1378                         brq->data.blocks = 1;
1379
1380                 /* Some controllers can't do multiblock reads due to hw bugs */
1381                 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1382                     rq_data_dir(req) == READ)
1383                         brq->data.blocks = 1;
1384         }
1385
1386         if (brq->data.blocks > 1 || do_rel_wr) {
1387                 /* SPI multiblock writes terminate using a special
1388                  * token, not a STOP_TRANSMISSION request.
1389                  */
1390                 if (!mmc_host_is_spi(card->host) ||
1391                     rq_data_dir(req) == READ)
1392                         brq->mrq.stop = &brq->stop;
1393                 readcmd = MMC_READ_MULTIPLE_BLOCK;
1394                 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1395         } else {
1396                 brq->mrq.stop = NULL;
1397                 readcmd = MMC_READ_SINGLE_BLOCK;
1398                 writecmd = MMC_WRITE_BLOCK;
1399         }
1400         if (rq_data_dir(req) == READ) {
1401                 brq->cmd.opcode = readcmd;
1402                 brq->data.flags |= MMC_DATA_READ;
1403         } else {
1404                 brq->cmd.opcode = writecmd;
1405                 brq->data.flags |= MMC_DATA_WRITE;
1406         }
1407
1408         if (req->cmd_flags & REQ_KERNEL)
1409                 brq->data.flags |= MMC_DATA_DIRECT;
1410
1411         if (do_rel_wr)
1412                 mmc_apply_rel_rw(brq, card, req);
1413
1414         /*
1415          * Data tag is used only during writing meta data to speed
1416          * up write and any subsequent read of this meta data
1417          */
1418         do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1419                 (req->cmd_flags & REQ_META) &&
1420                 (rq_data_dir(req) == WRITE) &&
1421                 ((brq->data.blocks * brq->data.blksz) >=
1422                  card->ext_csd.data_tag_unit_size);
1423
1424         /*
1425          * Pre-defined multi-block transfers are preferable to
1426          * open ended-ones (and necessary for reliable writes).
1427          * However, it is not sufficient to just send CMD23,
1428          * and avoid the final CMD12, as on an error condition
1429          * CMD12 (stop) needs to be sent anyway. This, coupled
1430          * with Auto-CMD23 enhancements provided by some
1431          * hosts, means that the complexity of dealing
1432          * with this is best left to the host. If CMD23 is
1433          * supported by card and host, we'll fill sbc in and let
1434          * the host deal with handling it correctly. This means
1435          * that for hosts that don't expose MMC_CAP_CMD23, no
1436          * change of behavior will be observed.
1437          *
1438          * N.B: Some MMC cards experience perf degradation.
1439          * We'll avoid using CMD23-bounded multiblock writes for
1440          * these, while retaining features like reliable writes.
1441          */
1442         if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1443             (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1444              do_data_tag)) {
1445                 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1446                 brq->sbc.arg = brq->data.blocks |
1447                         (do_rel_wr ? (1 << 31) : 0) |
1448                         (do_data_tag ? (1 << 29) : 0);
1449                 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1450                 brq->mrq.sbc = &brq->sbc;
1451         }
1452
1453         mmc_set_data_timeout(&brq->data, card);
1454
1455         brq->data.sg = mqrq->sg;
1456         brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1457
1458         /*
1459          * Adjust the sg list so it is the same size as the
1460          * request.
1461          */
1462         if (brq->data.blocks != blk_rq_sectors(req)) {
1463                 int i, data_size = brq->data.blocks << 9;
1464                 struct scatterlist *sg;
1465
1466                 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1467                         data_size -= sg->length;
1468                         if (data_size <= 0) {
1469                                 sg->length += data_size;
1470                                 i++;
1471                                 break;
1472                         }
1473                 }
1474                 brq->data.sg_len = i;
1475         }
1476
1477         mqrq->mmc_active.mrq = &brq->mrq;
1478         mqrq->mmc_active.err_check = mmc_blk_err_check;
1479
1480         mmc_queue_bounce_pre(mqrq);
1481 }
1482
1483 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1484                                           struct mmc_card *card)
1485 {
1486         unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1487         unsigned int max_seg_sz = queue_max_segment_size(q);
1488         unsigned int len, nr_segs = 0;
1489
1490         do {
1491                 len = min(hdr_sz, max_seg_sz);
1492                 hdr_sz -= len;
1493                 nr_segs++;
1494         } while (hdr_sz);
1495
1496         return nr_segs;
1497 }
1498
1499 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1500 {
1501         struct request_queue *q = mq->queue;
1502         struct mmc_card *card = mq->card;
1503         struct request *cur = req, *next = NULL;
1504         struct mmc_blk_data *md = mq->data;
1505         struct mmc_queue_req *mqrq = mq->mqrq_cur;
1506         bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1507         unsigned int req_sectors = 0, phys_segments = 0;
1508         unsigned int max_blk_count, max_phys_segs;
1509         bool put_back = true;
1510         u8 max_packed_rw = 0;
1511         u8 reqs = 0;
1512
1513         if (!(md->flags & MMC_BLK_PACKED_CMD))
1514                 goto no_packed;
1515
1516         if ((rq_data_dir(cur) == WRITE) &&
1517             mmc_host_packed_wr(card->host))
1518                 max_packed_rw = card->ext_csd.max_packed_writes;
1519
1520         if (max_packed_rw == 0)
1521                 goto no_packed;
1522
1523         if (mmc_req_rel_wr(cur) &&
1524             (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1525                 goto no_packed;
1526
1527         if (mmc_large_sector(card) &&
1528             !IS_ALIGNED(blk_rq_sectors(cur), 8))
1529                 goto no_packed;
1530
1531         mmc_blk_clear_packed(mqrq);
1532
1533         max_blk_count = min(card->host->max_blk_count,
1534                             card->host->max_req_size >> 9);
1535         if (unlikely(max_blk_count > 0xffff))
1536                 max_blk_count = 0xffff;
1537
1538         max_phys_segs = queue_max_segments(q);
1539         req_sectors += blk_rq_sectors(cur);
1540         phys_segments += cur->nr_phys_segments;
1541
1542         if (rq_data_dir(cur) == WRITE) {
1543                 req_sectors += mmc_large_sector(card) ? 8 : 1;
1544                 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1545         }
1546
1547         do {
1548                 if (reqs >= max_packed_rw - 1) {
1549                         put_back = false;
1550                         break;
1551                 }
1552
1553                 spin_lock_irq(q->queue_lock);
1554                 next = blk_fetch_request(q);
1555                 spin_unlock_irq(q->queue_lock);
1556                 if (!next) {
1557                         put_back = false;
1558                         break;
1559                 }
1560
1561                 if (mmc_large_sector(card) &&
1562                     !IS_ALIGNED(blk_rq_sectors(next), 8))
1563                         break;
1564
1565                 if (next->cmd_flags & REQ_DISCARD ||
1566                     next->cmd_flags & REQ_FLUSH)
1567                         break;
1568
1569                 if (rq_data_dir(cur) != rq_data_dir(next))
1570                         break;
1571
1572                 if (mmc_req_rel_wr(next) &&
1573                     (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1574                         break;
1575
1576                 req_sectors += blk_rq_sectors(next);
1577                 if (req_sectors > max_blk_count)
1578                         break;
1579
1580                 phys_segments +=  next->nr_phys_segments;
1581                 if (phys_segments > max_phys_segs)
1582                         break;
1583
1584                 list_add_tail(&next->queuelist, &mqrq->packed->list);
1585                 cur = next;
1586                 reqs++;
1587         } while (1);
1588
1589         if (put_back) {
1590                 spin_lock_irq(q->queue_lock);
1591                 blk_requeue_request(q, next);
1592                 spin_unlock_irq(q->queue_lock);
1593         }
1594
1595         if (reqs > 0) {
1596                 list_add(&req->queuelist, &mqrq->packed->list);
1597                 mqrq->packed->nr_entries = ++reqs;
1598                 mqrq->packed->retries = reqs;
1599                 return reqs;
1600         }
1601
1602 no_packed:
1603         mqrq->cmd_type = MMC_PACKED_NONE;
1604         return 0;
1605 }
1606
1607 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1608                                         struct mmc_card *card,
1609                                         struct mmc_queue *mq)
1610 {
1611         struct mmc_blk_request *brq = &mqrq->brq;
1612         struct request *req = mqrq->req;
1613         struct request *prq;
1614         struct mmc_blk_data *md = mq->data;
1615         struct mmc_packed *packed = mqrq->packed;
1616         bool do_rel_wr, do_data_tag;
1617         u32 *packed_cmd_hdr;
1618         u8 hdr_blocks;
1619         u8 i = 1;
1620
1621         BUG_ON(!packed);
1622
1623         mqrq->cmd_type = MMC_PACKED_WRITE;
1624         packed->blocks = 0;
1625         packed->idx_failure = MMC_PACKED_NR_IDX;
1626
1627         packed_cmd_hdr = packed->cmd_hdr;
1628         memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1629         packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1630                 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1631         hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1632
1633         /*
1634          * Argument for each entry of packed group
1635          */
1636         list_for_each_entry(prq, &packed->list, queuelist) {
1637                 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1638                 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1639                         (prq->cmd_flags & REQ_META) &&
1640                         (rq_data_dir(prq) == WRITE) &&
1641                         ((brq->data.blocks * brq->data.blksz) >=
1642                          card->ext_csd.data_tag_unit_size);
1643                 /* Argument of CMD23 */
1644                 packed_cmd_hdr[(i * 2)] =
1645                         (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1646                         (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1647                         blk_rq_sectors(prq);
1648                 /* Argument of CMD18 or CMD25 */
1649                 packed_cmd_hdr[((i * 2)) + 1] =
1650                         mmc_card_blockaddr(card) ?
1651                         blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1652                 packed->blocks += blk_rq_sectors(prq);
1653                 i++;
1654         }
1655
1656         memset(brq, 0, sizeof(struct mmc_blk_request));
1657         brq->mrq.cmd = &brq->cmd;
1658         brq->mrq.data = &brq->data;
1659         brq->mrq.sbc = &brq->sbc;
1660         brq->mrq.stop = &brq->stop;
1661
1662         brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1663         brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1664         brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1665
1666         brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1667         brq->cmd.arg = blk_rq_pos(req);
1668         if (!mmc_card_blockaddr(card))
1669                 brq->cmd.arg <<= 9;
1670         brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1671
1672         brq->data.blksz = 512;
1673         brq->data.blocks = packed->blocks + hdr_blocks;
1674         brq->data.flags |= MMC_DATA_WRITE;
1675
1676         brq->stop.opcode = MMC_STOP_TRANSMISSION;
1677         brq->stop.arg = 0;
1678         brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1679
1680         mmc_set_data_timeout(&brq->data, card);
1681
1682         brq->data.sg = mqrq->sg;
1683         brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1684
1685         mqrq->mmc_active.mrq = &brq->mrq;
1686         mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1687
1688         mmc_queue_bounce_pre(mqrq);
1689 }
1690
1691 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1692                            struct mmc_blk_request *brq, struct request *req,
1693                            int ret)
1694 {
1695         struct mmc_queue_req *mq_rq;
1696         mq_rq = container_of(brq, struct mmc_queue_req, brq);
1697
1698         /*
1699          * If this is an SD card and we're writing, we can first
1700          * mark the known good sectors as ok.
1701          *
1702          * If the card is not SD, we can still ok written sectors
1703          * as reported by the controller (which might be less than
1704          * the real number of written sectors, but never more).
1705          */
1706         if (mmc_card_sd(card)) {
1707                 u32 blocks;
1708
1709                 blocks = mmc_sd_num_wr_blocks(card);
1710                 if (blocks != (u32)-1) {
1711                         ret = blk_end_request(req, 0, blocks << 9);
1712                 }
1713         } else {
1714                 if (!mmc_packed_cmd(mq_rq->cmd_type))
1715                         ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1716         }
1717         return ret;
1718 }
1719
1720 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1721 {
1722         struct request *prq;
1723         struct mmc_packed *packed = mq_rq->packed;
1724         int idx = packed->idx_failure, i = 0;
1725         int ret = 0;
1726
1727         BUG_ON(!packed);
1728
1729         while (!list_empty(&packed->list)) {
1730                 prq = list_entry_rq(packed->list.next);
1731                 if (idx == i) {
1732                         /* retry from error index */
1733                         packed->nr_entries -= idx;
1734                         mq_rq->req = prq;
1735                         ret = 1;
1736
1737                         if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1738                                 list_del_init(&prq->queuelist);
1739                                 mmc_blk_clear_packed(mq_rq);
1740                         }
1741                         return ret;
1742                 }
1743                 list_del_init(&prq->queuelist);
1744                 blk_end_request(prq, 0, blk_rq_bytes(prq));
1745                 i++;
1746         }
1747
1748         mmc_blk_clear_packed(mq_rq);
1749         return ret;
1750 }
1751
1752 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1753 {
1754         struct request *prq;
1755         struct mmc_packed *packed = mq_rq->packed;
1756
1757         BUG_ON(!packed);
1758
1759         while (!list_empty(&packed->list)) {
1760                 prq = list_entry_rq(packed->list.next);
1761                 list_del_init(&prq->queuelist);
1762                 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1763         }
1764
1765         mmc_blk_clear_packed(mq_rq);
1766 }
1767
1768 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1769                                       struct mmc_queue_req *mq_rq)
1770 {
1771         struct request *prq;
1772         struct request_queue *q = mq->queue;
1773         struct mmc_packed *packed = mq_rq->packed;
1774
1775         BUG_ON(!packed);
1776
1777         while (!list_empty(&packed->list)) {
1778                 prq = list_entry_rq(packed->list.prev);
1779                 if (prq->queuelist.prev != &packed->list) {
1780                         list_del_init(&prq->queuelist);
1781                         spin_lock_irq(q->queue_lock);
1782                         blk_requeue_request(mq->queue, prq);
1783                         spin_unlock_irq(q->queue_lock);
1784                 } else {
1785                         list_del_init(&prq->queuelist);
1786                 }
1787         }
1788
1789         mmc_blk_clear_packed(mq_rq);
1790 }
1791
1792 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1793 {
1794         struct mmc_blk_data *md = mq->data;
1795         struct mmc_card *card = md->queue.card;
1796         struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1797         int ret = 1, disable_multi = 0, retry = 0, retune_retry_done = 0, type;
1798         enum mmc_blk_status status;
1799         struct mmc_queue_req *mq_rq;
1800         struct request *req = rqc;
1801         struct mmc_async_req *areq;
1802         const u8 packed_nr = 2;
1803         u8 reqs = 0;
1804
1805         if (!rqc && !mq->mqrq_prev->req)
1806                 return 0;
1807
1808         if (rqc)
1809                 reqs = mmc_blk_prep_packed_list(mq, rqc);
1810
1811         do {
1812                 if (rqc) {
1813                         /*
1814                          * When 4KB native sector is enabled, only 8 blocks
1815                          * multiple read or write is allowed
1816                          */
1817                         if ((brq->data.blocks & 0x07) &&
1818                             (card->ext_csd.data_sector_size == 4096)) {
1819                                 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1820                                         req->rq_disk->disk_name);
1821                                 mq_rq = mq->mqrq_cur;
1822                                 goto cmd_abort;
1823                         }
1824
1825                         if (reqs >= packed_nr)
1826                                 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1827                                                             card, mq);
1828                         else
1829                                 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1830                         areq = &mq->mqrq_cur->mmc_active;
1831                 } else
1832                         areq = NULL;
1833                 areq = mmc_start_req(card->host, areq, (int *) &status);
1834                 if (!areq) {
1835                         if (status == MMC_BLK_NEW_REQUEST)
1836                                 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1837                         return 0;
1838                 }
1839
1840                 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1841                 brq = &mq_rq->brq;
1842                 req = mq_rq->req;
1843                 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1844                 mmc_queue_bounce_post(mq_rq);
1845
1846                 switch (status) {
1847                 case MMC_BLK_SUCCESS:
1848                 case MMC_BLK_PARTIAL:
1849                         /*
1850                          * A block was successfully transferred.
1851                          */
1852                         mmc_blk_reset_success(md, type);
1853
1854                         if (mmc_packed_cmd(mq_rq->cmd_type)) {
1855                                 ret = mmc_blk_end_packed_req(mq_rq);
1856                                 break;
1857                         } else {
1858                                 ret = blk_end_request(req, 0,
1859                                                 brq->data.bytes_xfered);
1860                         }
1861
1862                         /*
1863                          * If the blk_end_request function returns non-zero even
1864                          * though all data has been transferred and no errors
1865                          * were returned by the host controller, it's a bug.
1866                          */
1867                         if (status == MMC_BLK_SUCCESS && ret) {
1868                                 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1869                                        __func__, blk_rq_bytes(req),
1870                                        brq->data.bytes_xfered);
1871                                 rqc = NULL;
1872                                 goto cmd_abort;
1873                         }
1874                         break;
1875                 case MMC_BLK_CMD_ERR:
1876                         ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1877                         if (mmc_blk_reset(md, card->host, type))
1878                                 goto cmd_abort;
1879                         if (!ret)
1880                                 goto start_new_req;
1881                         break;
1882                 case MMC_BLK_RETRY:
1883                         retune_retry_done = brq->retune_retry_done;
1884                         if (retry++ < 5)
1885                                 break;
1886                         /* Fall through */
1887                 case MMC_BLK_ABORT:
1888                         if (!mmc_blk_reset(md, card->host, type))
1889                                 break;
1890                         goto cmd_abort;
1891                 case MMC_BLK_DATA_ERR: {
1892                         int err;
1893
1894                         err = mmc_blk_reset(md, card->host, type);
1895                         if (!err)
1896                                 break;
1897                         if (err == -ENODEV ||
1898                                 mmc_packed_cmd(mq_rq->cmd_type))
1899                                 goto cmd_abort;
1900                         /* Fall through */
1901                 }
1902                 case MMC_BLK_ECC_ERR:
1903                         if (brq->data.blocks > 1) {
1904                                 /* Redo read one sector at a time */
1905                                 pr_warning("%s: retrying using single block read\n",
1906                                            req->rq_disk->disk_name);
1907                                 disable_multi = 1;
1908                                 break;
1909                         }
1910                         /*
1911                          * After an error, we redo I/O one sector at a
1912                          * time, so we only reach here after trying to
1913                          * read a single sector.
1914                          */
1915                         ret = blk_end_request(req, -EIO,
1916                                                 brq->data.blksz);
1917                         if (!ret)
1918                                 goto start_new_req;
1919                         break;
1920                 case MMC_BLK_NOMEDIUM:
1921                         goto cmd_abort;
1922                 default:
1923                         pr_err("%s: Unhandled return value (%d)",
1924                                         req->rq_disk->disk_name, status);
1925                         goto cmd_abort;
1926                 }
1927
1928                 if (ret) {
1929                         if (mmc_packed_cmd(mq_rq->cmd_type)) {
1930                                 if (!mq_rq->packed->retries)
1931                                         goto cmd_abort;
1932                                 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1933                                 mmc_start_req(card->host,
1934                                               &mq_rq->mmc_active, NULL);
1935                         } else {
1936
1937                                 /*
1938                                  * In case of a incomplete request
1939                                  * prepare it again and resend.
1940                                  */
1941                                 mmc_blk_rw_rq_prep(mq_rq, card,
1942                                                 disable_multi, mq);
1943                                 mmc_start_req(card->host,
1944                                                 &mq_rq->mmc_active, NULL);
1945                         }
1946                 mq_rq->brq.retune_retry_done = retune_retry_done;
1947                 }
1948         } while (ret);
1949
1950         return 1;
1951
1952  cmd_abort:
1953         if (mmc_packed_cmd(mq_rq->cmd_type)) {
1954                 mmc_blk_abort_packed_req(mq_rq);
1955         } else {
1956                 if (mmc_card_removed(card))
1957                         req->cmd_flags |= REQ_QUIET;
1958                 while (ret)
1959                         ret = blk_end_request(req, -EIO,
1960                                         blk_rq_cur_bytes(req));
1961         }
1962
1963  start_new_req:
1964         if (rqc) {
1965                 if (mmc_card_removed(card)) {
1966                         rqc->cmd_flags |= REQ_QUIET;
1967                         blk_end_request_all(rqc, -EIO);
1968                 } else {
1969                         /*
1970                          * If current request is packed, it needs to put back.
1971                          */
1972                         if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1973                                 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1974
1975                         mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1976                         mmc_start_req(card->host,
1977                                       &mq->mqrq_cur->mmc_active, NULL);
1978                 }
1979         }
1980
1981         return 0;
1982 }
1983
1984 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1985 {
1986         int ret;
1987         struct mmc_blk_data *md = mq->data;
1988         struct mmc_card *card = md->queue.card;
1989         struct mmc_host *host = card->host;
1990         unsigned long flags;
1991         unsigned int cmd_flags = req ? req->cmd_flags : 0;
1992
1993 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1994         if (mmc_bus_needs_resume(card->host))
1995                 mmc_resume_bus(card->host);
1996 #endif
1997
1998         if (req && !mq->mqrq_prev->req)
1999                 /* claim host only for the first request */
2000                 mmc_get_card(card);
2001
2002         ret = mmc_blk_part_switch(card, md);
2003         if (ret) {
2004                 if (req) {
2005                         blk_end_request_all(req, -EIO);
2006                 }
2007                 ret = 0;
2008                 goto out;
2009         }
2010
2011         mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
2012         if (cmd_flags & REQ_DISCARD) {
2013                 /* complete ongoing async transfer before issuing discard */
2014                 if (card->host->areq)
2015                         mmc_blk_issue_rw_rq(mq, NULL);
2016                 if (req->cmd_flags & REQ_SECURE &&
2017                         !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
2018                         ret = mmc_blk_issue_secdiscard_rq(mq, req);
2019                 else
2020                         ret = mmc_blk_issue_discard_rq(mq, req);
2021         } else if (cmd_flags & REQ_FLUSH) {
2022                 /* complete ongoing async transfer before issuing flush */
2023                 if (card->host->areq)
2024                         mmc_blk_issue_rw_rq(mq, NULL);
2025                 ret = mmc_blk_issue_flush(mq, req);
2026         } else {
2027                 if (!req && host->areq) {
2028                         spin_lock_irqsave(&host->context_info.lock, flags);
2029                         host->context_info.is_waiting_last_req = true;
2030                         spin_unlock_irqrestore(&host->context_info.lock, flags);
2031                 }
2032                 ret = mmc_blk_issue_rw_rq(mq, req);
2033         }
2034
2035 out:
2036         if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2037              (cmd_flags & MMC_REQ_SPECIAL_MASK))
2038                 /*
2039                  * Release host when there are no more requests
2040                  * and after special request(discard, flush) is done.
2041                  * In case sepecial request, there is no reentry to
2042                  * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2043                  */
2044                 mmc_put_card(card);
2045         return ret;
2046 }
2047
2048 static inline int mmc_blk_readonly(struct mmc_card *card)
2049 {
2050         return mmc_card_readonly(card) ||
2051                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2052 }
2053
2054 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2055                                               struct device *parent,
2056                                               sector_t size,
2057                                               bool default_ro,
2058                                               const char *subname,
2059                                               int area_type)
2060 {
2061         struct mmc_blk_data *md;
2062         int devidx, ret;
2063
2064         devidx = find_first_zero_bit(dev_use, max_devices);
2065         if (devidx >= max_devices)
2066                 return ERR_PTR(-ENOSPC);
2067         __set_bit(devidx, dev_use);
2068
2069         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2070         if (!md) {
2071                 ret = -ENOMEM;
2072                 goto out;
2073         }
2074
2075         /*
2076          * !subname implies we are creating main mmc_blk_data that will be
2077          * associated with mmc_card with mmc_set_drvdata. Due to device
2078          * partitions, devidx will not coincide with a per-physical card
2079          * index anymore so we keep track of a name index.
2080          */
2081         if (!subname) {
2082                 md->name_idx = find_first_zero_bit(name_use, max_devices);
2083                 __set_bit(md->name_idx, name_use);
2084         } else
2085                 md->name_idx = ((struct mmc_blk_data *)
2086                                 dev_to_disk(parent)->private_data)->name_idx;
2087
2088         md->area_type = area_type;
2089
2090         /*
2091          * Set the read-only status based on the supported commands
2092          * and the write protect switch.
2093          */
2094         md->read_only = mmc_blk_readonly(card);
2095
2096         md->disk = alloc_disk(perdev_minors);
2097         if (md->disk == NULL) {
2098                 ret = -ENOMEM;
2099                 goto err_kfree;
2100         }
2101
2102         spin_lock_init(&md->lock);
2103         INIT_LIST_HEAD(&md->part);
2104         md->usage = 1;
2105
2106         ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2107         if (ret)
2108                 goto err_putdisk;
2109
2110         md->queue.issue_fn = mmc_blk_issue_rq;
2111         md->queue.data = md;
2112
2113         md->disk->major = MMC_BLOCK_MAJOR;
2114         md->disk->first_minor = devidx * perdev_minors;
2115         md->disk->fops = &mmc_bdops;
2116         md->disk->private_data = md;
2117         md->disk->queue = md->queue.queue;
2118         md->disk->driverfs_dev = parent;
2119         set_disk_ro(md->disk, md->read_only || default_ro);
2120         md->disk->flags = GENHD_FL_EXT_DEVT;
2121         if (area_type & MMC_BLK_DATA_AREA_RPMB)
2122                 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2123
2124         /*
2125          * As discussed on lkml, GENHD_FL_REMOVABLE should:
2126          *
2127          * - be set for removable media with permanent block devices
2128          * - be unset for removable block devices with permanent media
2129          *
2130          * Since MMC block devices clearly fall under the second
2131          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
2132          * should use the block device creation/destruction hotplug
2133          * messages to tell when the card is present.
2134          */
2135
2136         snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2137                  "mmcblk%d%s", md->name_idx, subname ? subname : "");
2138
2139         if (mmc_card_mmc(card))
2140                 blk_queue_logical_block_size(md->queue.queue,
2141                                              card->ext_csd.data_sector_size);
2142         else
2143                 blk_queue_logical_block_size(md->queue.queue, 512);
2144
2145         set_capacity(md->disk, size);
2146
2147         if (mmc_host_cmd23(card->host)) {
2148                 if (mmc_card_mmc(card) ||
2149                     (mmc_card_sd(card) &&
2150                      card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2151                         md->flags |= MMC_BLK_CMD23;
2152         }
2153
2154         if (mmc_card_mmc(card) &&
2155             md->flags & MMC_BLK_CMD23 &&
2156             ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2157              card->ext_csd.rel_sectors)) {
2158                 md->flags |= MMC_BLK_REL_WR;
2159                 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2160         }
2161
2162         if (mmc_card_mmc(card) &&
2163             (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2164             (md->flags & MMC_BLK_CMD23) &&
2165             card->ext_csd.packed_event_en) {
2166                 if (!mmc_packed_init(&md->queue, card))
2167                         md->flags |= MMC_BLK_PACKED_CMD;
2168         }
2169
2170         return md;
2171
2172  err_putdisk:
2173         put_disk(md->disk);
2174  err_kfree:
2175         kfree(md);
2176  out:
2177         return ERR_PTR(ret);
2178 }
2179
2180 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2181 {
2182         sector_t size;
2183         struct mmc_blk_data *md;
2184
2185         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2186                 /*
2187                  * The EXT_CSD sector count is in number or 512 byte
2188                  * sectors.
2189                  */
2190                 size = card->ext_csd.sectors;
2191         } else {
2192                 /*
2193                  * The CSD capacity field is in units of read_blkbits.
2194                  * set_capacity takes units of 512 bytes.
2195                  */
2196                 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2197         }
2198
2199         md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2200                                         MMC_BLK_DATA_AREA_MAIN);
2201         return md;
2202 }
2203
2204 static int mmc_blk_alloc_part(struct mmc_card *card,
2205                               struct mmc_blk_data *md,
2206                               unsigned int part_type,
2207                               sector_t size,
2208                               bool default_ro,
2209                               const char *subname,
2210                               int area_type)
2211 {
2212         char cap_str[10];
2213         struct mmc_blk_data *part_md;
2214
2215         part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2216                                     subname, area_type);
2217         if (IS_ERR(part_md))
2218                 return PTR_ERR(part_md);
2219         part_md->part_type = part_type;
2220         list_add(&part_md->part, &md->part);
2221
2222         string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2223                         cap_str, sizeof(cap_str));
2224         pr_info("%s: %s %s partition %u %s\n",
2225                part_md->disk->disk_name, mmc_card_id(card),
2226                mmc_card_name(card), part_md->part_type, cap_str);
2227         return 0;
2228 }
2229
2230 /* MMC Physical partitions consist of two boot partitions and
2231  * up to four general purpose partitions.
2232  * For each partition enabled in EXT_CSD a block device will be allocatedi
2233  * to provide access to the partition.
2234  */
2235
2236 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2237 {
2238         int idx, ret = 0;
2239
2240         if (!mmc_card_mmc(card))
2241                 return 0;
2242
2243         for (idx = 0; idx < card->nr_parts; idx++) {
2244                 if (card->part[idx].size) {
2245                         ret = mmc_blk_alloc_part(card, md,
2246                                 card->part[idx].part_cfg,
2247                                 card->part[idx].size >> 9,
2248                                 card->part[idx].force_ro,
2249                                 card->part[idx].name,
2250                                 card->part[idx].area_type);
2251                         if (ret)
2252                                 return ret;
2253                 }
2254         }
2255
2256         return ret;
2257 }
2258
2259 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2260 {
2261         struct mmc_card *card;
2262
2263         if (md) {
2264                 /*
2265                  * Flush remaining requests and free queues. It
2266                  * is freeing the queue that stops new requests
2267                  * from being accepted.
2268                  */
2269                 card = md->queue.card;
2270                 mmc_cleanup_queue(&md->queue);
2271                 if (md->flags & MMC_BLK_PACKED_CMD)
2272                         mmc_packed_clean(&md->queue);
2273                 if (md->disk->flags & GENHD_FL_UP) {
2274                         device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2275                         if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2276                                         card->ext_csd.boot_ro_lockable)
2277                                 device_remove_file(disk_to_dev(md->disk),
2278                                         &md->power_ro_lock);
2279
2280                         del_gendisk(md->disk);
2281                 }
2282                 mmc_blk_put(md);
2283         }
2284 }
2285
2286 static void mmc_blk_remove_parts(struct mmc_card *card,
2287                                  struct mmc_blk_data *md)
2288 {
2289         struct list_head *pos, *q;
2290         struct mmc_blk_data *part_md;
2291
2292         __clear_bit(md->name_idx, name_use);
2293         list_for_each_safe(pos, q, &md->part) {
2294                 part_md = list_entry(pos, struct mmc_blk_data, part);
2295                 list_del(pos);
2296                 mmc_blk_remove_req(part_md);
2297         }
2298 }
2299
2300 static int mmc_add_disk(struct mmc_blk_data *md)
2301 {
2302         int ret;
2303         struct mmc_card *card = md->queue.card;
2304
2305         add_disk(md->disk);
2306         md->force_ro.show = force_ro_show;
2307         md->force_ro.store = force_ro_store;
2308         sysfs_attr_init(&md->force_ro.attr);
2309         md->force_ro.attr.name = "force_ro";
2310         md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2311         ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2312         if (ret)
2313                 goto force_ro_fail;
2314
2315         if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2316              card->ext_csd.boot_ro_lockable) {
2317                 umode_t mode;
2318
2319                 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2320                         mode = S_IRUGO;
2321                 else
2322                         mode = S_IRUGO | S_IWUSR;
2323
2324                 md->power_ro_lock.show = power_ro_lock_show;
2325                 md->power_ro_lock.store = power_ro_lock_store;
2326                 sysfs_attr_init(&md->power_ro_lock.attr);
2327                 md->power_ro_lock.attr.mode = mode;
2328                 md->power_ro_lock.attr.name =
2329                                         "ro_lock_until_next_power_on";
2330                 ret = device_create_file(disk_to_dev(md->disk),
2331                                 &md->power_ro_lock);
2332                 if (ret)
2333                         goto power_ro_lock_fail;
2334         }
2335         return ret;
2336
2337 power_ro_lock_fail:
2338         device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2339 force_ro_fail:
2340         del_gendisk(md->disk);
2341
2342         return ret;
2343 }
2344
2345 #define CID_MANFID_SANDISK      0x2
2346 #define CID_MANFID_TOSHIBA      0x11
2347 #define CID_MANFID_MICRON       0x13
2348 #define CID_MANFID_SAMSUNG      0x15
2349 #define CID_MANFID_HYNIX        0x90
2350
2351 static const struct mmc_fixup blk_fixups[] =
2352 {
2353         MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2354                   MMC_QUIRK_INAND_CMD38),
2355         MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2356                   MMC_QUIRK_INAND_CMD38),
2357         MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2358                   MMC_QUIRK_INAND_CMD38),
2359         MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2360                   MMC_QUIRK_INAND_CMD38),
2361         MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2362                   MMC_QUIRK_INAND_CMD38),
2363
2364         /*
2365          * Some MMC cards experience performance degradation with CMD23
2366          * instead of CMD12-bounded multiblock transfers. For now we'll
2367          * black list what's bad...
2368          * - Certain Toshiba cards.
2369          *
2370          * N.B. This doesn't affect SD cards.
2371          */
2372         MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2373                   MMC_QUIRK_BLK_NO_CMD23),
2374         MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2375                   MMC_QUIRK_BLK_NO_CMD23),
2376         MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2377                   MMC_QUIRK_BLK_NO_CMD23),
2378
2379         /*
2380          * Some Micron MMC cards needs longer data read timeout than
2381          * indicated in CSD.
2382          */
2383         MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2384                   MMC_QUIRK_LONG_READ_TIME),
2385
2386         /*
2387          * On these Samsung MoviNAND parts, performing secure erase or
2388          * secure trim can result in unrecoverable corruption due to a
2389          * firmware bug.
2390          */
2391         MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2392                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2393         MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2394                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2395         MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2396                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2397         MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2398                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2399         MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2400                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2401         MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2402                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2403         MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2404                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2405         MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2406                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2407
2408         /* Hynix 4.41 iNAND execute trim will lead boot up failed. */
2409         MMC_FIXUP(CID_NAME_ANY, CID_MANFID_HYNIX, CID_OEMID_ANY, add_quirk_mmc,
2410                   MMC_QUIRK_TRIM_UNSTABLE),
2411
2412         END_FIXUP
2413 };
2414
2415 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2416 extern struct mmc_card *this_card;
2417 #endif
2418
2419 static int mmc_blk_probe(struct mmc_card *card)
2420 {
2421         struct mmc_blk_data *md, *part_md;
2422         char cap_str[10];
2423
2424         /*
2425          * Check that the card supports the command class(es) we need.
2426          */
2427         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2428                 return -ENODEV;
2429
2430         md = mmc_blk_alloc(card);
2431         if (IS_ERR(md))
2432                 return PTR_ERR(md);
2433
2434         string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
2435                         cap_str, sizeof(cap_str));
2436         pr_info("%s: %s %s %s %s\n",
2437                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2438                 cap_str, md->read_only ? "(ro)" : "");
2439
2440         if (mmc_blk_alloc_parts(card, md))
2441                 goto out;
2442
2443         mmc_set_drvdata(card, md);
2444         mmc_fixup_device(card, blk_fixups);
2445
2446 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2447         mmc_set_bus_resume_policy(card->host, 1);
2448 #endif
2449 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2450         if (card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC) {
2451                 this_card = card;
2452                 md->disk->emmc_disk = 1;
2453         } else {
2454                 md->disk->emmc_disk = 0;
2455         }
2456 #endif
2457         if (mmc_add_disk(md))
2458                 goto out;
2459
2460         list_for_each_entry(part_md, &md->part, part) {
2461                 if (mmc_add_disk(part_md))
2462                         goto out;
2463         }
2464
2465         pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2466         pm_runtime_use_autosuspend(&card->dev);
2467
2468         /*
2469          * Don't enable runtime PM for SD-combo cards here. Leave that
2470          * decision to be taken during the SDIO init sequence instead.
2471          */
2472         if (card->type != MMC_TYPE_SD_COMBO) {
2473                 pm_runtime_set_active(&card->dev);
2474                 pm_runtime_enable(&card->dev);
2475         }
2476
2477         return 0;
2478
2479  out:
2480         mmc_blk_remove_parts(card, md);
2481         mmc_blk_remove_req(md);
2482         return 0;
2483 }
2484
2485 static void mmc_blk_remove(struct mmc_card *card)
2486 {
2487         struct mmc_blk_data *md = mmc_get_drvdata(card);
2488
2489 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2490         if (card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
2491                 this_card = NULL;
2492 #endif
2493         mmc_blk_remove_parts(card, md);
2494         pm_runtime_get_sync(&card->dev);
2495         mmc_claim_host(card->host);
2496         mmc_blk_part_switch(card, md);
2497         mmc_release_host(card->host);
2498         if (card->type != MMC_TYPE_SD_COMBO)
2499                 pm_runtime_disable(&card->dev);
2500         pm_runtime_put_noidle(&card->dev);
2501         mmc_blk_remove_req(md);
2502         mmc_set_drvdata(card, NULL);
2503 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2504         mmc_set_bus_resume_policy(card->host, 0);
2505 #endif
2506 }
2507
2508 static int _mmc_blk_suspend(struct mmc_card *card)
2509 {
2510         struct mmc_blk_data *part_md;
2511         struct mmc_blk_data *md = mmc_get_drvdata(card);
2512
2513         if (md) {
2514                 mmc_queue_suspend(&md->queue);
2515                 list_for_each_entry(part_md, &md->part, part) {
2516                         mmc_queue_suspend(&part_md->queue);
2517                 }
2518         }
2519         return 0;
2520 }
2521
2522 static void mmc_blk_shutdown(struct mmc_card *card)
2523 {
2524         _mmc_blk_suspend(card);
2525 }
2526
2527 #ifdef CONFIG_PM
2528 static int mmc_blk_suspend(struct mmc_card *card)
2529 {
2530         return _mmc_blk_suspend(card);
2531 }
2532
2533 static int mmc_blk_resume(struct mmc_card *card)
2534 {
2535         struct mmc_blk_data *part_md;
2536         struct mmc_blk_data *md = mmc_get_drvdata(card);
2537
2538         if (md) {
2539                 /*
2540                  * Resume involves the card going into idle state,
2541                  * so current partition is always the main one.
2542                  */
2543                 md->part_curr = md->part_type;
2544                 mmc_queue_resume(&md->queue);
2545                 list_for_each_entry(part_md, &md->part, part) {
2546                         mmc_queue_resume(&part_md->queue);
2547                 }
2548         }
2549         return 0;
2550 }
2551 #else
2552 #define mmc_blk_suspend NULL
2553 #define mmc_blk_resume  NULL
2554 #endif
2555
2556 static struct mmc_driver mmc_driver = {
2557         .drv            = {
2558                 .name   = "mmcblk",
2559         },
2560         .probe          = mmc_blk_probe,
2561         .remove         = mmc_blk_remove,
2562         .suspend        = mmc_blk_suspend,
2563         .resume         = mmc_blk_resume,
2564         .shutdown       = mmc_blk_shutdown,
2565 };
2566
2567 static int __init mmc_blk_init(void)
2568 {
2569         int res;
2570
2571         if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2572                 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2573
2574         max_devices = 256 / perdev_minors;
2575
2576         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2577         if (res)
2578                 goto out;
2579
2580         res = mmc_register_driver(&mmc_driver);
2581         if (res)
2582                 goto out2;
2583
2584         return 0;
2585  out2:
2586         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2587  out:
2588         return res;
2589 }
2590
2591 static void __exit mmc_blk_exit(void)
2592 {
2593         mmc_unregister_driver(&mmc_driver);
2594         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2595 }
2596
2597 module_init(mmc_blk_init);
2598 module_exit(mmc_blk_exit);
2599
2600 MODULE_LICENSE("GPL");
2601 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2602