mmc: block: don't start new request when the card is removed
[~shefty/rdma-dev.git] / drivers / mmc / card / block.c
1 /*
2  * Block driver for media (i.e., flash cards)
3  *
4  * Copyright 2002 Hewlett-Packard Company
5  * Copyright 2005-2008 Pierre Ossman
6  *
7  * Use consistent with the GNU GPL is permitted,
8  * provided that this copyright notice is
9  * preserved in its entirety in all copies and derived works.
10  *
11  * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12  * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13  * FITNESS FOR ANY PARTICULAR PURPOSE.
14  *
15  * Many thanks to Alessandro Rubini and Jonathan Corbet!
16  *
17  * Author:  Andrew Christian
18  *          28 May 2002
19  */
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23
24 #include <linux/kernel.h>
25 #include <linux/fs.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
34 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
37
38 #include <linux/mmc/ioctl.h>
39 #include <linux/mmc/card.h>
40 #include <linux/mmc/host.h>
41 #include <linux/mmc/mmc.h>
42 #include <linux/mmc/sd.h>
43
44 #include <asm/uaccess.h>
45
46 #include "queue.h"
47
48 MODULE_ALIAS("mmc:block");
49 #ifdef MODULE_PARAM_PREFIX
50 #undef MODULE_PARAM_PREFIX
51 #endif
52 #define MODULE_PARAM_PREFIX "mmcblk."
53
54 #define INAND_CMD38_ARG_EXT_CSD  113
55 #define INAND_CMD38_ARG_ERASE    0x00
56 #define INAND_CMD38_ARG_TRIM     0x01
57 #define INAND_CMD38_ARG_SECERASE 0x80
58 #define INAND_CMD38_ARG_SECTRIM1 0x81
59 #define INAND_CMD38_ARG_SECTRIM2 0x88
60 #define MMC_BLK_TIMEOUT_MS  (10 * 60 * 1000)        /* 10 minute timeout */
61
62 static DEFINE_MUTEX(block_mutex);
63
64 /*
65  * The defaults come from config options but can be overriden by module
66  * or bootarg options.
67  */
68 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
69
70 /*
71  * We've only got one major, so number of mmcblk devices is
72  * limited to 256 / number of minors per device.
73  */
74 static int max_devices;
75
76 /* 256 minors, so at most 256 separate devices */
77 static DECLARE_BITMAP(dev_use, 256);
78 static DECLARE_BITMAP(name_use, 256);
79
80 /*
81  * There is one mmc_blk_data per slot.
82  */
83 struct mmc_blk_data {
84         spinlock_t      lock;
85         struct gendisk  *disk;
86         struct mmc_queue queue;
87         struct list_head part;
88
89         unsigned int    flags;
90 #define MMC_BLK_CMD23   (1 << 0)        /* Can do SET_BLOCK_COUNT for multiblock */
91 #define MMC_BLK_REL_WR  (1 << 1)        /* MMC Reliable write support */
92
93         unsigned int    usage;
94         unsigned int    read_only;
95         unsigned int    part_type;
96         unsigned int    name_idx;
97         unsigned int    reset_done;
98 #define MMC_BLK_READ            BIT(0)
99 #define MMC_BLK_WRITE           BIT(1)
100 #define MMC_BLK_DISCARD         BIT(2)
101 #define MMC_BLK_SECDISCARD      BIT(3)
102
103         /*
104          * Only set in main mmc_blk_data associated
105          * with mmc_card with mmc_set_drvdata, and keeps
106          * track of the current selected device partition.
107          */
108         unsigned int    part_curr;
109         struct device_attribute force_ro;
110         struct device_attribute power_ro_lock;
111         int     area_type;
112 };
113
114 static DEFINE_MUTEX(open_lock);
115
116 module_param(perdev_minors, int, 0444);
117 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
118
119 static inline int mmc_blk_part_switch(struct mmc_card *card,
120                                       struct mmc_blk_data *md);
121 static int get_card_status(struct mmc_card *card, u32 *status, int retries);
122
123 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
124 {
125         struct mmc_blk_data *md;
126
127         mutex_lock(&open_lock);
128         md = disk->private_data;
129         if (md && md->usage == 0)
130                 md = NULL;
131         if (md)
132                 md->usage++;
133         mutex_unlock(&open_lock);
134
135         return md;
136 }
137
138 static inline int mmc_get_devidx(struct gendisk *disk)
139 {
140         int devmaj = MAJOR(disk_devt(disk));
141         int devidx = MINOR(disk_devt(disk)) / perdev_minors;
142
143         if (!devmaj)
144                 devidx = disk->first_minor / perdev_minors;
145         return devidx;
146 }
147
148 static void mmc_blk_put(struct mmc_blk_data *md)
149 {
150         mutex_lock(&open_lock);
151         md->usage--;
152         if (md->usage == 0) {
153                 int devidx = mmc_get_devidx(md->disk);
154                 blk_cleanup_queue(md->queue.queue);
155
156                 __clear_bit(devidx, dev_use);
157
158                 put_disk(md->disk);
159                 kfree(md);
160         }
161         mutex_unlock(&open_lock);
162 }
163
164 static ssize_t power_ro_lock_show(struct device *dev,
165                 struct device_attribute *attr, char *buf)
166 {
167         int ret;
168         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
169         struct mmc_card *card = md->queue.card;
170         int locked = 0;
171
172         if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
173                 locked = 2;
174         else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
175                 locked = 1;
176
177         ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
178
179         return ret;
180 }
181
182 static ssize_t power_ro_lock_store(struct device *dev,
183                 struct device_attribute *attr, const char *buf, size_t count)
184 {
185         int ret;
186         struct mmc_blk_data *md, *part_md;
187         struct mmc_card *card;
188         unsigned long set;
189
190         if (kstrtoul(buf, 0, &set))
191                 return -EINVAL;
192
193         if (set != 1)
194                 return count;
195
196         md = mmc_blk_get(dev_to_disk(dev));
197         card = md->queue.card;
198
199         mmc_claim_host(card->host);
200
201         ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
202                                 card->ext_csd.boot_ro_lock |
203                                 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
204                                 card->ext_csd.part_time);
205         if (ret)
206                 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md->disk->disk_name, ret);
207         else
208                 card->ext_csd.boot_ro_lock |= EXT_CSD_BOOT_WP_B_PWR_WP_EN;
209
210         mmc_release_host(card->host);
211
212         if (!ret) {
213                 pr_info("%s: Locking boot partition ro until next power on\n",
214                         md->disk->disk_name);
215                 set_disk_ro(md->disk, 1);
216
217                 list_for_each_entry(part_md, &md->part, part)
218                         if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
219                                 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
220                                 set_disk_ro(part_md->disk, 1);
221                         }
222         }
223
224         mmc_blk_put(md);
225         return count;
226 }
227
228 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
229                              char *buf)
230 {
231         int ret;
232         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
233
234         ret = snprintf(buf, PAGE_SIZE, "%d",
235                        get_disk_ro(dev_to_disk(dev)) ^
236                        md->read_only);
237         mmc_blk_put(md);
238         return ret;
239 }
240
241 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
242                               const char *buf, size_t count)
243 {
244         int ret;
245         char *end;
246         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
247         unsigned long set = simple_strtoul(buf, &end, 0);
248         if (end == buf) {
249                 ret = -EINVAL;
250                 goto out;
251         }
252
253         set_disk_ro(dev_to_disk(dev), set || md->read_only);
254         ret = count;
255 out:
256         mmc_blk_put(md);
257         return ret;
258 }
259
260 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
261 {
262         struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
263         int ret = -ENXIO;
264
265         mutex_lock(&block_mutex);
266         if (md) {
267                 if (md->usage == 2)
268                         check_disk_change(bdev);
269                 ret = 0;
270
271                 if ((mode & FMODE_WRITE) && md->read_only) {
272                         mmc_blk_put(md);
273                         ret = -EROFS;
274                 }
275         }
276         mutex_unlock(&block_mutex);
277
278         return ret;
279 }
280
281 static int mmc_blk_release(struct gendisk *disk, fmode_t mode)
282 {
283         struct mmc_blk_data *md = disk->private_data;
284
285         mutex_lock(&block_mutex);
286         mmc_blk_put(md);
287         mutex_unlock(&block_mutex);
288         return 0;
289 }
290
291 static int
292 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
293 {
294         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
295         geo->heads = 4;
296         geo->sectors = 16;
297         return 0;
298 }
299
300 struct mmc_blk_ioc_data {
301         struct mmc_ioc_cmd ic;
302         unsigned char *buf;
303         u64 buf_bytes;
304 };
305
306 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
307         struct mmc_ioc_cmd __user *user)
308 {
309         struct mmc_blk_ioc_data *idata;
310         int err;
311
312         idata = kzalloc(sizeof(*idata), GFP_KERNEL);
313         if (!idata) {
314                 err = -ENOMEM;
315                 goto out;
316         }
317
318         if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
319                 err = -EFAULT;
320                 goto idata_err;
321         }
322
323         idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
324         if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
325                 err = -EOVERFLOW;
326                 goto idata_err;
327         }
328
329         if (!idata->buf_bytes)
330                 return idata;
331
332         idata->buf = kzalloc(idata->buf_bytes, GFP_KERNEL);
333         if (!idata->buf) {
334                 err = -ENOMEM;
335                 goto idata_err;
336         }
337
338         if (copy_from_user(idata->buf, (void __user *)(unsigned long)
339                                         idata->ic.data_ptr, idata->buf_bytes)) {
340                 err = -EFAULT;
341                 goto copy_err;
342         }
343
344         return idata;
345
346 copy_err:
347         kfree(idata->buf);
348 idata_err:
349         kfree(idata);
350 out:
351         return ERR_PTR(err);
352 }
353
354 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
355                                        u32 retries_max)
356 {
357         int err;
358         u32 retry_count = 0;
359
360         if (!status || !retries_max)
361                 return -EINVAL;
362
363         do {
364                 err = get_card_status(card, status, 5);
365                 if (err)
366                         break;
367
368                 if (!R1_STATUS(*status) &&
369                                 (R1_CURRENT_STATE(*status) != R1_STATE_PRG))
370                         break; /* RPMB programming operation complete */
371
372                 /*
373                  * Rechedule to give the MMC device a chance to continue
374                  * processing the previous command without being polled too
375                  * frequently.
376                  */
377                 usleep_range(1000, 5000);
378         } while (++retry_count < retries_max);
379
380         if (retry_count == retries_max)
381                 err = -EPERM;
382
383         return err;
384 }
385
386 static int mmc_blk_ioctl_cmd(struct block_device *bdev,
387         struct mmc_ioc_cmd __user *ic_ptr)
388 {
389         struct mmc_blk_ioc_data *idata;
390         struct mmc_blk_data *md;
391         struct mmc_card *card;
392         struct mmc_command cmd = {0};
393         struct mmc_data data = {0};
394         struct mmc_request mrq = {NULL};
395         struct scatterlist sg;
396         int err;
397         int is_rpmb = false;
398         u32 status = 0;
399
400         /*
401          * The caller must have CAP_SYS_RAWIO, and must be calling this on the
402          * whole block device, not on a partition.  This prevents overspray
403          * between sibling partitions.
404          */
405         if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
406                 return -EPERM;
407
408         idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
409         if (IS_ERR(idata))
410                 return PTR_ERR(idata);
411
412         md = mmc_blk_get(bdev->bd_disk);
413         if (!md) {
414                 err = -EINVAL;
415                 goto cmd_err;
416         }
417
418         if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
419                 is_rpmb = true;
420
421         card = md->queue.card;
422         if (IS_ERR(card)) {
423                 err = PTR_ERR(card);
424                 goto cmd_done;
425         }
426
427         cmd.opcode = idata->ic.opcode;
428         cmd.arg = idata->ic.arg;
429         cmd.flags = idata->ic.flags;
430
431         if (idata->buf_bytes) {
432                 data.sg = &sg;
433                 data.sg_len = 1;
434                 data.blksz = idata->ic.blksz;
435                 data.blocks = idata->ic.blocks;
436
437                 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
438
439                 if (idata->ic.write_flag)
440                         data.flags = MMC_DATA_WRITE;
441                 else
442                         data.flags = MMC_DATA_READ;
443
444                 /* data.flags must already be set before doing this. */
445                 mmc_set_data_timeout(&data, card);
446
447                 /* Allow overriding the timeout_ns for empirical tuning. */
448                 if (idata->ic.data_timeout_ns)
449                         data.timeout_ns = idata->ic.data_timeout_ns;
450
451                 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
452                         /*
453                          * Pretend this is a data transfer and rely on the
454                          * host driver to compute timeout.  When all host
455                          * drivers support cmd.cmd_timeout for R1B, this
456                          * can be changed to:
457                          *
458                          *     mrq.data = NULL;
459                          *     cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
460                          */
461                         data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
462                 }
463
464                 mrq.data = &data;
465         }
466
467         mrq.cmd = &cmd;
468
469         mmc_claim_host(card->host);
470
471         err = mmc_blk_part_switch(card, md);
472         if (err)
473                 goto cmd_rel_host;
474
475         if (idata->ic.is_acmd) {
476                 err = mmc_app_cmd(card->host, card);
477                 if (err)
478                         goto cmd_rel_host;
479         }
480
481         if (is_rpmb) {
482                 err = mmc_set_blockcount(card, data.blocks,
483                         idata->ic.write_flag & (1 << 31));
484                 if (err)
485                         goto cmd_rel_host;
486         }
487
488         mmc_wait_for_req(card->host, &mrq);
489
490         if (cmd.error) {
491                 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
492                                                 __func__, cmd.error);
493                 err = cmd.error;
494                 goto cmd_rel_host;
495         }
496         if (data.error) {
497                 dev_err(mmc_dev(card->host), "%s: data error %d\n",
498                                                 __func__, data.error);
499                 err = data.error;
500                 goto cmd_rel_host;
501         }
502
503         /*
504          * According to the SD specs, some commands require a delay after
505          * issuing the command.
506          */
507         if (idata->ic.postsleep_min_us)
508                 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
509
510         if (copy_to_user(&(ic_ptr->response), cmd.resp, sizeof(cmd.resp))) {
511                 err = -EFAULT;
512                 goto cmd_rel_host;
513         }
514
515         if (!idata->ic.write_flag) {
516                 if (copy_to_user((void __user *)(unsigned long) idata->ic.data_ptr,
517                                                 idata->buf, idata->buf_bytes)) {
518                         err = -EFAULT;
519                         goto cmd_rel_host;
520                 }
521         }
522
523         if (is_rpmb) {
524                 /*
525                  * Ensure RPMB command has completed by polling CMD13
526                  * "Send Status".
527                  */
528                 err = ioctl_rpmb_card_status_poll(card, &status, 5);
529                 if (err)
530                         dev_err(mmc_dev(card->host),
531                                         "%s: Card Status=0x%08X, error %d\n",
532                                         __func__, status, err);
533         }
534
535 cmd_rel_host:
536         mmc_release_host(card->host);
537
538 cmd_done:
539         mmc_blk_put(md);
540 cmd_err:
541         kfree(idata->buf);
542         kfree(idata);
543         return err;
544 }
545
546 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
547         unsigned int cmd, unsigned long arg)
548 {
549         int ret = -EINVAL;
550         if (cmd == MMC_IOC_CMD)
551                 ret = mmc_blk_ioctl_cmd(bdev, (struct mmc_ioc_cmd __user *)arg);
552         return ret;
553 }
554
555 #ifdef CONFIG_COMPAT
556 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
557         unsigned int cmd, unsigned long arg)
558 {
559         return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
560 }
561 #endif
562
563 static const struct block_device_operations mmc_bdops = {
564         .open                   = mmc_blk_open,
565         .release                = mmc_blk_release,
566         .getgeo                 = mmc_blk_getgeo,
567         .owner                  = THIS_MODULE,
568         .ioctl                  = mmc_blk_ioctl,
569 #ifdef CONFIG_COMPAT
570         .compat_ioctl           = mmc_blk_compat_ioctl,
571 #endif
572 };
573
574 static inline int mmc_blk_part_switch(struct mmc_card *card,
575                                       struct mmc_blk_data *md)
576 {
577         int ret;
578         struct mmc_blk_data *main_md = mmc_get_drvdata(card);
579
580         if (main_md->part_curr == md->part_type)
581                 return 0;
582
583         if (mmc_card_mmc(card)) {
584                 u8 part_config = card->ext_csd.part_config;
585
586                 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
587                 part_config |= md->part_type;
588
589                 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
590                                  EXT_CSD_PART_CONFIG, part_config,
591                                  card->ext_csd.part_time);
592                 if (ret)
593                         return ret;
594
595                 card->ext_csd.part_config = part_config;
596         }
597
598         main_md->part_curr = md->part_type;
599         return 0;
600 }
601
602 static u32 mmc_sd_num_wr_blocks(struct mmc_card *card)
603 {
604         int err;
605         u32 result;
606         __be32 *blocks;
607
608         struct mmc_request mrq = {NULL};
609         struct mmc_command cmd = {0};
610         struct mmc_data data = {0};
611
612         struct scatterlist sg;
613
614         cmd.opcode = MMC_APP_CMD;
615         cmd.arg = card->rca << 16;
616         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
617
618         err = mmc_wait_for_cmd(card->host, &cmd, 0);
619         if (err)
620                 return (u32)-1;
621         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
622                 return (u32)-1;
623
624         memset(&cmd, 0, sizeof(struct mmc_command));
625
626         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
627         cmd.arg = 0;
628         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
629
630         data.blksz = 4;
631         data.blocks = 1;
632         data.flags = MMC_DATA_READ;
633         data.sg = &sg;
634         data.sg_len = 1;
635         mmc_set_data_timeout(&data, card);
636
637         mrq.cmd = &cmd;
638         mrq.data = &data;
639
640         blocks = kmalloc(4, GFP_KERNEL);
641         if (!blocks)
642                 return (u32)-1;
643
644         sg_init_one(&sg, blocks, 4);
645
646         mmc_wait_for_req(card->host, &mrq);
647
648         result = ntohl(*blocks);
649         kfree(blocks);
650
651         if (cmd.error || data.error)
652                 result = (u32)-1;
653
654         return result;
655 }
656
657 static int send_stop(struct mmc_card *card, u32 *status)
658 {
659         struct mmc_command cmd = {0};
660         int err;
661
662         cmd.opcode = MMC_STOP_TRANSMISSION;
663         cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
664         err = mmc_wait_for_cmd(card->host, &cmd, 5);
665         if (err == 0)
666                 *status = cmd.resp[0];
667         return err;
668 }
669
670 static int get_card_status(struct mmc_card *card, u32 *status, int retries)
671 {
672         struct mmc_command cmd = {0};
673         int err;
674
675         cmd.opcode = MMC_SEND_STATUS;
676         if (!mmc_host_is_spi(card->host))
677                 cmd.arg = card->rca << 16;
678         cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_AC;
679         err = mmc_wait_for_cmd(card->host, &cmd, retries);
680         if (err == 0)
681                 *status = cmd.resp[0];
682         return err;
683 }
684
685 #define ERR_NOMEDIUM    3
686 #define ERR_RETRY       2
687 #define ERR_ABORT       1
688 #define ERR_CONTINUE    0
689
690 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
691         bool status_valid, u32 status)
692 {
693         switch (error) {
694         case -EILSEQ:
695                 /* response crc error, retry the r/w cmd */
696                 pr_err("%s: %s sending %s command, card status %#x\n",
697                         req->rq_disk->disk_name, "response CRC error",
698                         name, status);
699                 return ERR_RETRY;
700
701         case -ETIMEDOUT:
702                 pr_err("%s: %s sending %s command, card status %#x\n",
703                         req->rq_disk->disk_name, "timed out", name, status);
704
705                 /* If the status cmd initially failed, retry the r/w cmd */
706                 if (!status_valid)
707                         return ERR_RETRY;
708
709                 /*
710                  * If it was a r/w cmd crc error, or illegal command
711                  * (eg, issued in wrong state) then retry - we should
712                  * have corrected the state problem above.
713                  */
714                 if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND))
715                         return ERR_RETRY;
716
717                 /* Otherwise abort the command */
718                 return ERR_ABORT;
719
720         default:
721                 /* We don't understand the error code the driver gave us */
722                 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
723                        req->rq_disk->disk_name, error, status);
724                 return ERR_ABORT;
725         }
726 }
727
728 /*
729  * Initial r/w and stop cmd error recovery.
730  * We don't know whether the card received the r/w cmd or not, so try to
731  * restore things back to a sane state.  Essentially, we do this as follows:
732  * - Obtain card status.  If the first attempt to obtain card status fails,
733  *   the status word will reflect the failed status cmd, not the failed
734  *   r/w cmd.  If we fail to obtain card status, it suggests we can no
735  *   longer communicate with the card.
736  * - Check the card state.  If the card received the cmd but there was a
737  *   transient problem with the response, it might still be in a data transfer
738  *   mode.  Try to send it a stop command.  If this fails, we can't recover.
739  * - If the r/w cmd failed due to a response CRC error, it was probably
740  *   transient, so retry the cmd.
741  * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
742  * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
743  *   illegal cmd, retry.
744  * Otherwise we don't understand what happened, so abort.
745  */
746 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
747         struct mmc_blk_request *brq, int *ecc_err)
748 {
749         bool prev_cmd_status_valid = true;
750         u32 status, stop_status = 0;
751         int err, retry;
752
753         if (mmc_card_removed(card))
754                 return ERR_NOMEDIUM;
755
756         /*
757          * Try to get card status which indicates both the card state
758          * and why there was no response.  If the first attempt fails,
759          * we can't be sure the returned status is for the r/w command.
760          */
761         for (retry = 2; retry >= 0; retry--) {
762                 err = get_card_status(card, &status, 0);
763                 if (!err)
764                         break;
765
766                 prev_cmd_status_valid = false;
767                 pr_err("%s: error %d sending status command, %sing\n",
768                        req->rq_disk->disk_name, err, retry ? "retry" : "abort");
769         }
770
771         /* We couldn't get a response from the card.  Give up. */
772         if (err) {
773                 /* Check if the card is removed */
774                 if (mmc_detect_card_removed(card->host))
775                         return ERR_NOMEDIUM;
776                 return ERR_ABORT;
777         }
778
779         /* Flag ECC errors */
780         if ((status & R1_CARD_ECC_FAILED) ||
781             (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
782             (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
783                 *ecc_err = 1;
784
785         /*
786          * Check the current card state.  If it is in some data transfer
787          * mode, tell it to stop (and hopefully transition back to TRAN.)
788          */
789         if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
790             R1_CURRENT_STATE(status) == R1_STATE_RCV) {
791                 err = send_stop(card, &stop_status);
792                 if (err)
793                         pr_err("%s: error %d sending stop command\n",
794                                req->rq_disk->disk_name, err);
795
796                 /*
797                  * If the stop cmd also timed out, the card is probably
798                  * not present, so abort.  Other errors are bad news too.
799                  */
800                 if (err)
801                         return ERR_ABORT;
802                 if (stop_status & R1_CARD_ECC_FAILED)
803                         *ecc_err = 1;
804         }
805
806         /* Check for set block count errors */
807         if (brq->sbc.error)
808                 return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
809                                 prev_cmd_status_valid, status);
810
811         /* Check for r/w command errors */
812         if (brq->cmd.error)
813                 return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
814                                 prev_cmd_status_valid, status);
815
816         /* Data errors */
817         if (!brq->stop.error)
818                 return ERR_CONTINUE;
819
820         /* Now for stop errors.  These aren't fatal to the transfer. */
821         pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
822                req->rq_disk->disk_name, brq->stop.error,
823                brq->cmd.resp[0], status);
824
825         /*
826          * Subsitute in our own stop status as this will give the error
827          * state which happened during the execution of the r/w command.
828          */
829         if (stop_status) {
830                 brq->stop.resp[0] = stop_status;
831                 brq->stop.error = 0;
832         }
833         return ERR_CONTINUE;
834 }
835
836 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
837                          int type)
838 {
839         int err;
840
841         if (md->reset_done & type)
842                 return -EEXIST;
843
844         md->reset_done |= type;
845         err = mmc_hw_reset(host);
846         /* Ensure we switch back to the correct partition */
847         if (err != -EOPNOTSUPP) {
848                 struct mmc_blk_data *main_md = mmc_get_drvdata(host->card);
849                 int part_err;
850
851                 main_md->part_curr = main_md->part_type;
852                 part_err = mmc_blk_part_switch(host->card, md);
853                 if (part_err) {
854                         /*
855                          * We have failed to get back into the correct
856                          * partition, so we need to abort the whole request.
857                          */
858                         return -ENODEV;
859                 }
860         }
861         return err;
862 }
863
864 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
865 {
866         md->reset_done &= ~type;
867 }
868
869 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
870 {
871         struct mmc_blk_data *md = mq->data;
872         struct mmc_card *card = md->queue.card;
873         unsigned int from, nr, arg;
874         int err = 0, type = MMC_BLK_DISCARD;
875
876         if (!mmc_can_erase(card)) {
877                 err = -EOPNOTSUPP;
878                 goto out;
879         }
880
881         from = blk_rq_pos(req);
882         nr = blk_rq_sectors(req);
883
884         if (mmc_can_discard(card))
885                 arg = MMC_DISCARD_ARG;
886         else if (mmc_can_trim(card))
887                 arg = MMC_TRIM_ARG;
888         else
889                 arg = MMC_ERASE_ARG;
890 retry:
891         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
892                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
893                                  INAND_CMD38_ARG_EXT_CSD,
894                                  arg == MMC_TRIM_ARG ?
895                                  INAND_CMD38_ARG_TRIM :
896                                  INAND_CMD38_ARG_ERASE,
897                                  0);
898                 if (err)
899                         goto out;
900         }
901         err = mmc_erase(card, from, nr, arg);
902 out:
903         if (err == -EIO && !mmc_blk_reset(md, card->host, type))
904                 goto retry;
905         if (!err)
906                 mmc_blk_reset_success(md, type);
907         blk_end_request(req, err, blk_rq_bytes(req));
908
909         return err ? 0 : 1;
910 }
911
912 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
913                                        struct request *req)
914 {
915         struct mmc_blk_data *md = mq->data;
916         struct mmc_card *card = md->queue.card;
917         unsigned int from, nr, arg, trim_arg, erase_arg;
918         int err = 0, type = MMC_BLK_SECDISCARD;
919
920         if (!(mmc_can_secure_erase_trim(card) || mmc_can_sanitize(card))) {
921                 err = -EOPNOTSUPP;
922                 goto out;
923         }
924
925         from = blk_rq_pos(req);
926         nr = blk_rq_sectors(req);
927
928         /* The sanitize operation is supported at v4.5 only */
929         if (mmc_can_sanitize(card)) {
930                 erase_arg = MMC_ERASE_ARG;
931                 trim_arg = MMC_TRIM_ARG;
932         } else {
933                 erase_arg = MMC_SECURE_ERASE_ARG;
934                 trim_arg = MMC_SECURE_TRIM1_ARG;
935         }
936
937         if (mmc_erase_group_aligned(card, from, nr))
938                 arg = erase_arg;
939         else if (mmc_can_trim(card))
940                 arg = trim_arg;
941         else {
942                 err = -EINVAL;
943                 goto out;
944         }
945 retry:
946         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
947                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
948                                  INAND_CMD38_ARG_EXT_CSD,
949                                  arg == MMC_SECURE_TRIM1_ARG ?
950                                  INAND_CMD38_ARG_SECTRIM1 :
951                                  INAND_CMD38_ARG_SECERASE,
952                                  0);
953                 if (err)
954                         goto out_retry;
955         }
956
957         err = mmc_erase(card, from, nr, arg);
958         if (err == -EIO)
959                 goto out_retry;
960         if (err)
961                 goto out;
962
963         if (arg == MMC_SECURE_TRIM1_ARG) {
964                 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
965                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
966                                          INAND_CMD38_ARG_EXT_CSD,
967                                          INAND_CMD38_ARG_SECTRIM2,
968                                          0);
969                         if (err)
970                                 goto out_retry;
971                 }
972
973                 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
974                 if (err == -EIO)
975                         goto out_retry;
976                 if (err)
977                         goto out;
978         }
979
980         if (mmc_can_sanitize(card))
981                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
982                                  EXT_CSD_SANITIZE_START, 1, 0);
983 out_retry:
984         if (err && !mmc_blk_reset(md, card->host, type))
985                 goto retry;
986         if (!err)
987                 mmc_blk_reset_success(md, type);
988 out:
989         blk_end_request(req, err, blk_rq_bytes(req));
990
991         return err ? 0 : 1;
992 }
993
994 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
995 {
996         struct mmc_blk_data *md = mq->data;
997         struct mmc_card *card = md->queue.card;
998         int ret = 0;
999
1000         ret = mmc_flush_cache(card);
1001         if (ret)
1002                 ret = -EIO;
1003
1004         blk_end_request_all(req, ret);
1005
1006         return ret ? 0 : 1;
1007 }
1008
1009 /*
1010  * Reformat current write as a reliable write, supporting
1011  * both legacy and the enhanced reliable write MMC cards.
1012  * In each transfer we'll handle only as much as a single
1013  * reliable write can handle, thus finish the request in
1014  * partial completions.
1015  */
1016 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1017                                     struct mmc_card *card,
1018                                     struct request *req)
1019 {
1020         if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1021                 /* Legacy mode imposes restrictions on transfers. */
1022                 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1023                         brq->data.blocks = 1;
1024
1025                 if (brq->data.blocks > card->ext_csd.rel_sectors)
1026                         brq->data.blocks = card->ext_csd.rel_sectors;
1027                 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1028                         brq->data.blocks = 1;
1029         }
1030 }
1031
1032 #define CMD_ERRORS                                                      \
1033         (R1_OUT_OF_RANGE |      /* Command argument out of range */     \
1034          R1_ADDRESS_ERROR |     /* Misaligned address */                \
1035          R1_BLOCK_LEN_ERROR |   /* Transferred block length incorrect */\
1036          R1_WP_VIOLATION |      /* Tried to write to protected block */ \
1037          R1_CC_ERROR |          /* Card controller error */             \
1038          R1_ERROR)              /* General/unknown error */
1039
1040 static int mmc_blk_err_check(struct mmc_card *card,
1041                              struct mmc_async_req *areq)
1042 {
1043         struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1044                                                     mmc_active);
1045         struct mmc_blk_request *brq = &mq_mrq->brq;
1046         struct request *req = mq_mrq->req;
1047         int ecc_err = 0;
1048
1049         /*
1050          * sbc.error indicates a problem with the set block count
1051          * command.  No data will have been transferred.
1052          *
1053          * cmd.error indicates a problem with the r/w command.  No
1054          * data will have been transferred.
1055          *
1056          * stop.error indicates a problem with the stop command.  Data
1057          * may have been transferred, or may still be transferring.
1058          */
1059         if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1060             brq->data.error) {
1061                 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err)) {
1062                 case ERR_RETRY:
1063                         return MMC_BLK_RETRY;
1064                 case ERR_ABORT:
1065                         return MMC_BLK_ABORT;
1066                 case ERR_NOMEDIUM:
1067                         return MMC_BLK_NOMEDIUM;
1068                 case ERR_CONTINUE:
1069                         break;
1070                 }
1071         }
1072
1073         /*
1074          * Check for errors relating to the execution of the
1075          * initial command - such as address errors.  No data
1076          * has been transferred.
1077          */
1078         if (brq->cmd.resp[0] & CMD_ERRORS) {
1079                 pr_err("%s: r/w command failed, status = %#x\n",
1080                        req->rq_disk->disk_name, brq->cmd.resp[0]);
1081                 return MMC_BLK_ABORT;
1082         }
1083
1084         /*
1085          * Everything else is either success, or a data error of some
1086          * kind.  If it was a write, we may have transitioned to
1087          * program mode, which we have to wait for it to complete.
1088          */
1089         if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1090                 u32 status;
1091                 unsigned long timeout;
1092
1093                 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1094                 do {
1095                         int err = get_card_status(card, &status, 5);
1096                         if (err) {
1097                                 pr_err("%s: error %d requesting status\n",
1098                                        req->rq_disk->disk_name, err);
1099                                 return MMC_BLK_CMD_ERR;
1100                         }
1101
1102                         /* Timeout if the device never becomes ready for data
1103                          * and never leaves the program state.
1104                          */
1105                         if (time_after(jiffies, timeout)) {
1106                                 pr_err("%s: Card stuck in programming state!"\
1107                                         " %s %s\n", mmc_hostname(card->host),
1108                                         req->rq_disk->disk_name, __func__);
1109
1110                                 return MMC_BLK_CMD_ERR;
1111                         }
1112                         /*
1113                          * Some cards mishandle the status bits,
1114                          * so make sure to check both the busy
1115                          * indication and the card state.
1116                          */
1117                 } while (!(status & R1_READY_FOR_DATA) ||
1118                          (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1119         }
1120
1121         if (brq->data.error) {
1122                 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1123                        req->rq_disk->disk_name, brq->data.error,
1124                        (unsigned)blk_rq_pos(req),
1125                        (unsigned)blk_rq_sectors(req),
1126                        brq->cmd.resp[0], brq->stop.resp[0]);
1127
1128                 if (rq_data_dir(req) == READ) {
1129                         if (ecc_err)
1130                                 return MMC_BLK_ECC_ERR;
1131                         return MMC_BLK_DATA_ERR;
1132                 } else {
1133                         return MMC_BLK_CMD_ERR;
1134                 }
1135         }
1136
1137         if (!brq->data.bytes_xfered)
1138                 return MMC_BLK_RETRY;
1139
1140         if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1141                 return MMC_BLK_PARTIAL;
1142
1143         return MMC_BLK_SUCCESS;
1144 }
1145
1146 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1147                                struct mmc_card *card,
1148                                int disable_multi,
1149                                struct mmc_queue *mq)
1150 {
1151         u32 readcmd, writecmd;
1152         struct mmc_blk_request *brq = &mqrq->brq;
1153         struct request *req = mqrq->req;
1154         struct mmc_blk_data *md = mq->data;
1155         bool do_data_tag;
1156
1157         /*
1158          * Reliable writes are used to implement Forced Unit Access and
1159          * REQ_META accesses, and are supported only on MMCs.
1160          *
1161          * XXX: this really needs a good explanation of why REQ_META
1162          * is treated special.
1163          */
1164         bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1165                           (req->cmd_flags & REQ_META)) &&
1166                 (rq_data_dir(req) == WRITE) &&
1167                 (md->flags & MMC_BLK_REL_WR);
1168
1169         memset(brq, 0, sizeof(struct mmc_blk_request));
1170         brq->mrq.cmd = &brq->cmd;
1171         brq->mrq.data = &brq->data;
1172
1173         brq->cmd.arg = blk_rq_pos(req);
1174         if (!mmc_card_blockaddr(card))
1175                 brq->cmd.arg <<= 9;
1176         brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1177         brq->data.blksz = 512;
1178         brq->stop.opcode = MMC_STOP_TRANSMISSION;
1179         brq->stop.arg = 0;
1180         brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1181         brq->data.blocks = blk_rq_sectors(req);
1182
1183         /*
1184          * The block layer doesn't support all sector count
1185          * restrictions, so we need to be prepared for too big
1186          * requests.
1187          */
1188         if (brq->data.blocks > card->host->max_blk_count)
1189                 brq->data.blocks = card->host->max_blk_count;
1190
1191         if (brq->data.blocks > 1) {
1192                 /*
1193                  * After a read error, we redo the request one sector
1194                  * at a time in order to accurately determine which
1195                  * sectors can be read successfully.
1196                  */
1197                 if (disable_multi)
1198                         brq->data.blocks = 1;
1199
1200                 /* Some controllers can't do multiblock reads due to hw bugs */
1201                 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1202                     rq_data_dir(req) == READ)
1203                         brq->data.blocks = 1;
1204         }
1205
1206         if (brq->data.blocks > 1 || do_rel_wr) {
1207                 /* SPI multiblock writes terminate using a special
1208                  * token, not a STOP_TRANSMISSION request.
1209                  */
1210                 if (!mmc_host_is_spi(card->host) ||
1211                     rq_data_dir(req) == READ)
1212                         brq->mrq.stop = &brq->stop;
1213                 readcmd = MMC_READ_MULTIPLE_BLOCK;
1214                 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1215         } else {
1216                 brq->mrq.stop = NULL;
1217                 readcmd = MMC_READ_SINGLE_BLOCK;
1218                 writecmd = MMC_WRITE_BLOCK;
1219         }
1220         if (rq_data_dir(req) == READ) {
1221                 brq->cmd.opcode = readcmd;
1222                 brq->data.flags |= MMC_DATA_READ;
1223         } else {
1224                 brq->cmd.opcode = writecmd;
1225                 brq->data.flags |= MMC_DATA_WRITE;
1226         }
1227
1228         if (do_rel_wr)
1229                 mmc_apply_rel_rw(brq, card, req);
1230
1231         /*
1232          * Data tag is used only during writing meta data to speed
1233          * up write and any subsequent read of this meta data
1234          */
1235         do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1236                 (req->cmd_flags & REQ_META) &&
1237                 (rq_data_dir(req) == WRITE) &&
1238                 ((brq->data.blocks * brq->data.blksz) >=
1239                  card->ext_csd.data_tag_unit_size);
1240
1241         /*
1242          * Pre-defined multi-block transfers are preferable to
1243          * open ended-ones (and necessary for reliable writes).
1244          * However, it is not sufficient to just send CMD23,
1245          * and avoid the final CMD12, as on an error condition
1246          * CMD12 (stop) needs to be sent anyway. This, coupled
1247          * with Auto-CMD23 enhancements provided by some
1248          * hosts, means that the complexity of dealing
1249          * with this is best left to the host. If CMD23 is
1250          * supported by card and host, we'll fill sbc in and let
1251          * the host deal with handling it correctly. This means
1252          * that for hosts that don't expose MMC_CAP_CMD23, no
1253          * change of behavior will be observed.
1254          *
1255          * N.B: Some MMC cards experience perf degradation.
1256          * We'll avoid using CMD23-bounded multiblock writes for
1257          * these, while retaining features like reliable writes.
1258          */
1259         if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1260             (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1261              do_data_tag)) {
1262                 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1263                 brq->sbc.arg = brq->data.blocks |
1264                         (do_rel_wr ? (1 << 31) : 0) |
1265                         (do_data_tag ? (1 << 29) : 0);
1266                 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1267                 brq->mrq.sbc = &brq->sbc;
1268         }
1269
1270         mmc_set_data_timeout(&brq->data, card);
1271
1272         brq->data.sg = mqrq->sg;
1273         brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1274
1275         /*
1276          * Adjust the sg list so it is the same size as the
1277          * request.
1278          */
1279         if (brq->data.blocks != blk_rq_sectors(req)) {
1280                 int i, data_size = brq->data.blocks << 9;
1281                 struct scatterlist *sg;
1282
1283                 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1284                         data_size -= sg->length;
1285                         if (data_size <= 0) {
1286                                 sg->length += data_size;
1287                                 i++;
1288                                 break;
1289                         }
1290                 }
1291                 brq->data.sg_len = i;
1292         }
1293
1294         mqrq->mmc_active.mrq = &brq->mrq;
1295         mqrq->mmc_active.err_check = mmc_blk_err_check;
1296
1297         mmc_queue_bounce_pre(mqrq);
1298 }
1299
1300 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1301                            struct mmc_blk_request *brq, struct request *req,
1302                            int ret)
1303 {
1304         /*
1305          * If this is an SD card and we're writing, we can first
1306          * mark the known good sectors as ok.
1307          *
1308          * If the card is not SD, we can still ok written sectors
1309          * as reported by the controller (which might be less than
1310          * the real number of written sectors, but never more).
1311          */
1312         if (mmc_card_sd(card)) {
1313                 u32 blocks;
1314
1315                 blocks = mmc_sd_num_wr_blocks(card);
1316                 if (blocks != (u32)-1) {
1317                         ret = blk_end_request(req, 0, blocks << 9);
1318                 }
1319         } else {
1320                 ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1321         }
1322         return ret;
1323 }
1324
1325 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1326 {
1327         struct mmc_blk_data *md = mq->data;
1328         struct mmc_card *card = md->queue.card;
1329         struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1330         int ret = 1, disable_multi = 0, retry = 0, type;
1331         enum mmc_blk_status status;
1332         struct mmc_queue_req *mq_rq;
1333         struct request *req = rqc;
1334         struct mmc_async_req *areq;
1335
1336         if (!rqc && !mq->mqrq_prev->req)
1337                 return 0;
1338
1339         do {
1340                 if (rqc) {
1341                         /*
1342                          * When 4KB native sector is enabled, only 8 blocks
1343                          * multiple read or write is allowed
1344                          */
1345                         if ((brq->data.blocks & 0x07) &&
1346                             (card->ext_csd.data_sector_size == 4096)) {
1347                                 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1348                                         req->rq_disk->disk_name);
1349                                 goto cmd_abort;
1350                         }
1351                         mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1352                         areq = &mq->mqrq_cur->mmc_active;
1353                 } else
1354                         areq = NULL;
1355                 areq = mmc_start_req(card->host, areq, (int *) &status);
1356                 if (!areq) {
1357                         if (status == MMC_BLK_NEW_REQUEST)
1358                                 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1359                         return 0;
1360                 }
1361
1362                 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1363                 brq = &mq_rq->brq;
1364                 req = mq_rq->req;
1365                 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1366                 mmc_queue_bounce_post(mq_rq);
1367
1368                 switch (status) {
1369                 case MMC_BLK_SUCCESS:
1370                 case MMC_BLK_PARTIAL:
1371                         /*
1372                          * A block was successfully transferred.
1373                          */
1374                         mmc_blk_reset_success(md, type);
1375                         ret = blk_end_request(req, 0,
1376                                                 brq->data.bytes_xfered);
1377                         /*
1378                          * If the blk_end_request function returns non-zero even
1379                          * though all data has been transferred and no errors
1380                          * were returned by the host controller, it's a bug.
1381                          */
1382                         if (status == MMC_BLK_SUCCESS && ret) {
1383                                 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1384                                        __func__, blk_rq_bytes(req),
1385                                        brq->data.bytes_xfered);
1386                                 rqc = NULL;
1387                                 goto cmd_abort;
1388                         }
1389                         break;
1390                 case MMC_BLK_CMD_ERR:
1391                         ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1392                         if (!mmc_blk_reset(md, card->host, type))
1393                                 break;
1394                         goto cmd_abort;
1395                 case MMC_BLK_RETRY:
1396                         if (retry++ < 5)
1397                                 break;
1398                         /* Fall through */
1399                 case MMC_BLK_ABORT:
1400                         if (!mmc_blk_reset(md, card->host, type))
1401                                 break;
1402                         goto cmd_abort;
1403                 case MMC_BLK_DATA_ERR: {
1404                         int err;
1405
1406                         err = mmc_blk_reset(md, card->host, type);
1407                         if (!err)
1408                                 break;
1409                         if (err == -ENODEV)
1410                                 goto cmd_abort;
1411                         /* Fall through */
1412                 }
1413                 case MMC_BLK_ECC_ERR:
1414                         if (brq->data.blocks > 1) {
1415                                 /* Redo read one sector at a time */
1416                                 pr_warning("%s: retrying using single block read\n",
1417                                            req->rq_disk->disk_name);
1418                                 disable_multi = 1;
1419                                 break;
1420                         }
1421                         /*
1422                          * After an error, we redo I/O one sector at a
1423                          * time, so we only reach here after trying to
1424                          * read a single sector.
1425                          */
1426                         ret = blk_end_request(req, -EIO,
1427                                                 brq->data.blksz);
1428                         if (!ret)
1429                                 goto start_new_req;
1430                         break;
1431                 case MMC_BLK_NOMEDIUM:
1432                         goto cmd_abort;
1433                 default:
1434                         pr_err("%s: Unhandled return value (%d)",
1435                                         req->rq_disk->disk_name, status);
1436                         goto cmd_abort;
1437                 }
1438
1439                 if (ret) {
1440                         /*
1441                          * In case of a incomplete request
1442                          * prepare it again and resend.
1443                          */
1444                         mmc_blk_rw_rq_prep(mq_rq, card, disable_multi, mq);
1445                         mmc_start_req(card->host, &mq_rq->mmc_active, NULL);
1446                 }
1447         } while (ret);
1448
1449         return 1;
1450
1451  cmd_abort:
1452         if (mmc_card_removed(card))
1453                 req->cmd_flags |= REQ_QUIET;
1454         while (ret)
1455                 ret = blk_end_request(req, -EIO, blk_rq_cur_bytes(req));
1456
1457  start_new_req:
1458         if (rqc) {
1459                 if (mmc_card_removed(card)) {
1460                         rqc->cmd_flags |= REQ_QUIET;
1461                         blk_end_request_all(rqc, -EIO);
1462                 } else {
1463                         mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1464                         mmc_start_req(card->host,
1465                                       &mq->mqrq_cur->mmc_active, NULL);
1466                 }
1467         }
1468
1469         return 0;
1470 }
1471
1472 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1473 {
1474         int ret;
1475         struct mmc_blk_data *md = mq->data;
1476         struct mmc_card *card = md->queue.card;
1477         struct mmc_host *host = card->host;
1478         unsigned long flags;
1479
1480         if (req && !mq->mqrq_prev->req)
1481                 /* claim host only for the first request */
1482                 mmc_claim_host(card->host);
1483
1484         ret = mmc_blk_part_switch(card, md);
1485         if (ret) {
1486                 if (req) {
1487                         blk_end_request_all(req, -EIO);
1488                 }
1489                 ret = 0;
1490                 goto out;
1491         }
1492
1493         mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
1494         if (req && req->cmd_flags & REQ_DISCARD) {
1495                 /* complete ongoing async transfer before issuing discard */
1496                 if (card->host->areq)
1497                         mmc_blk_issue_rw_rq(mq, NULL);
1498                 if (req->cmd_flags & REQ_SECURE &&
1499                         !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1500                         ret = mmc_blk_issue_secdiscard_rq(mq, req);
1501                 else
1502                         ret = mmc_blk_issue_discard_rq(mq, req);
1503         } else if (req && req->cmd_flags & REQ_FLUSH) {
1504                 /* complete ongoing async transfer before issuing flush */
1505                 if (card->host->areq)
1506                         mmc_blk_issue_rw_rq(mq, NULL);
1507                 ret = mmc_blk_issue_flush(mq, req);
1508         } else {
1509                 if (!req && host->areq) {
1510                         spin_lock_irqsave(&host->context_info.lock, flags);
1511                         host->context_info.is_waiting_last_req = true;
1512                         spin_unlock_irqrestore(&host->context_info.lock, flags);
1513                 }
1514                 ret = mmc_blk_issue_rw_rq(mq, req);
1515         }
1516
1517 out:
1518         if (!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST))
1519                 /* release host only when there are no more requests */
1520                 mmc_release_host(card->host);
1521         return ret;
1522 }
1523
1524 static inline int mmc_blk_readonly(struct mmc_card *card)
1525 {
1526         return mmc_card_readonly(card) ||
1527                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
1528 }
1529
1530 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
1531                                               struct device *parent,
1532                                               sector_t size,
1533                                               bool default_ro,
1534                                               const char *subname,
1535                                               int area_type)
1536 {
1537         struct mmc_blk_data *md;
1538         int devidx, ret;
1539
1540         devidx = find_first_zero_bit(dev_use, max_devices);
1541         if (devidx >= max_devices)
1542                 return ERR_PTR(-ENOSPC);
1543         __set_bit(devidx, dev_use);
1544
1545         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
1546         if (!md) {
1547                 ret = -ENOMEM;
1548                 goto out;
1549         }
1550
1551         /*
1552          * !subname implies we are creating main mmc_blk_data that will be
1553          * associated with mmc_card with mmc_set_drvdata. Due to device
1554          * partitions, devidx will not coincide with a per-physical card
1555          * index anymore so we keep track of a name index.
1556          */
1557         if (!subname) {
1558                 md->name_idx = find_first_zero_bit(name_use, max_devices);
1559                 __set_bit(md->name_idx, name_use);
1560         } else
1561                 md->name_idx = ((struct mmc_blk_data *)
1562                                 dev_to_disk(parent)->private_data)->name_idx;
1563
1564         md->area_type = area_type;
1565
1566         /*
1567          * Set the read-only status based on the supported commands
1568          * and the write protect switch.
1569          */
1570         md->read_only = mmc_blk_readonly(card);
1571
1572         md->disk = alloc_disk(perdev_minors);
1573         if (md->disk == NULL) {
1574                 ret = -ENOMEM;
1575                 goto err_kfree;
1576         }
1577
1578         spin_lock_init(&md->lock);
1579         INIT_LIST_HEAD(&md->part);
1580         md->usage = 1;
1581
1582         ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
1583         if (ret)
1584                 goto err_putdisk;
1585
1586         md->queue.issue_fn = mmc_blk_issue_rq;
1587         md->queue.data = md;
1588
1589         md->disk->major = MMC_BLOCK_MAJOR;
1590         md->disk->first_minor = devidx * perdev_minors;
1591         md->disk->fops = &mmc_bdops;
1592         md->disk->private_data = md;
1593         md->disk->queue = md->queue.queue;
1594         md->disk->driverfs_dev = parent;
1595         set_disk_ro(md->disk, md->read_only || default_ro);
1596         if (area_type & MMC_BLK_DATA_AREA_RPMB)
1597                 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
1598
1599         /*
1600          * As discussed on lkml, GENHD_FL_REMOVABLE should:
1601          *
1602          * - be set for removable media with permanent block devices
1603          * - be unset for removable block devices with permanent media
1604          *
1605          * Since MMC block devices clearly fall under the second
1606          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
1607          * should use the block device creation/destruction hotplug
1608          * messages to tell when the card is present.
1609          */
1610
1611         snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
1612                  "mmcblk%d%s", md->name_idx, subname ? subname : "");
1613
1614         if (mmc_card_mmc(card))
1615                 blk_queue_logical_block_size(md->queue.queue,
1616                                              card->ext_csd.data_sector_size);
1617         else
1618                 blk_queue_logical_block_size(md->queue.queue, 512);
1619
1620         set_capacity(md->disk, size);
1621
1622         if (mmc_host_cmd23(card->host)) {
1623                 if (mmc_card_mmc(card) ||
1624                     (mmc_card_sd(card) &&
1625                      card->scr.cmds & SD_SCR_CMD23_SUPPORT))
1626                         md->flags |= MMC_BLK_CMD23;
1627         }
1628
1629         if (mmc_card_mmc(card) &&
1630             md->flags & MMC_BLK_CMD23 &&
1631             ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
1632              card->ext_csd.rel_sectors)) {
1633                 md->flags |= MMC_BLK_REL_WR;
1634                 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
1635         }
1636
1637         return md;
1638
1639  err_putdisk:
1640         put_disk(md->disk);
1641  err_kfree:
1642         kfree(md);
1643  out:
1644         return ERR_PTR(ret);
1645 }
1646
1647 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
1648 {
1649         sector_t size;
1650         struct mmc_blk_data *md;
1651
1652         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
1653                 /*
1654                  * The EXT_CSD sector count is in number or 512 byte
1655                  * sectors.
1656                  */
1657                 size = card->ext_csd.sectors;
1658         } else {
1659                 /*
1660                  * The CSD capacity field is in units of read_blkbits.
1661                  * set_capacity takes units of 512 bytes.
1662                  */
1663                 size = card->csd.capacity << (card->csd.read_blkbits - 9);
1664         }
1665
1666         md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
1667                                         MMC_BLK_DATA_AREA_MAIN);
1668         return md;
1669 }
1670
1671 static int mmc_blk_alloc_part(struct mmc_card *card,
1672                               struct mmc_blk_data *md,
1673                               unsigned int part_type,
1674                               sector_t size,
1675                               bool default_ro,
1676                               const char *subname,
1677                               int area_type)
1678 {
1679         char cap_str[10];
1680         struct mmc_blk_data *part_md;
1681
1682         part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
1683                                     subname, area_type);
1684         if (IS_ERR(part_md))
1685                 return PTR_ERR(part_md);
1686         part_md->part_type = part_type;
1687         list_add(&part_md->part, &md->part);
1688
1689         string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
1690                         cap_str, sizeof(cap_str));
1691         pr_info("%s: %s %s partition %u %s\n",
1692                part_md->disk->disk_name, mmc_card_id(card),
1693                mmc_card_name(card), part_md->part_type, cap_str);
1694         return 0;
1695 }
1696
1697 /* MMC Physical partitions consist of two boot partitions and
1698  * up to four general purpose partitions.
1699  * For each partition enabled in EXT_CSD a block device will be allocatedi
1700  * to provide access to the partition.
1701  */
1702
1703 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
1704 {
1705         int idx, ret = 0;
1706
1707         if (!mmc_card_mmc(card))
1708                 return 0;
1709
1710         for (idx = 0; idx < card->nr_parts; idx++) {
1711                 if (card->part[idx].size) {
1712                         ret = mmc_blk_alloc_part(card, md,
1713                                 card->part[idx].part_cfg,
1714                                 card->part[idx].size >> 9,
1715                                 card->part[idx].force_ro,
1716                                 card->part[idx].name,
1717                                 card->part[idx].area_type);
1718                         if (ret)
1719                                 return ret;
1720                 }
1721         }
1722
1723         return ret;
1724 }
1725
1726 static void mmc_blk_remove_req(struct mmc_blk_data *md)
1727 {
1728         struct mmc_card *card;
1729
1730         if (md) {
1731                 card = md->queue.card;
1732                 if (md->disk->flags & GENHD_FL_UP) {
1733                         device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1734                         if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1735                                         card->ext_csd.boot_ro_lockable)
1736                                 device_remove_file(disk_to_dev(md->disk),
1737                                         &md->power_ro_lock);
1738
1739                         /* Stop new requests from getting into the queue */
1740                         del_gendisk(md->disk);
1741                 }
1742
1743                 /* Then flush out any already in there */
1744                 mmc_cleanup_queue(&md->queue);
1745                 mmc_blk_put(md);
1746         }
1747 }
1748
1749 static void mmc_blk_remove_parts(struct mmc_card *card,
1750                                  struct mmc_blk_data *md)
1751 {
1752         struct list_head *pos, *q;
1753         struct mmc_blk_data *part_md;
1754
1755         __clear_bit(md->name_idx, name_use);
1756         list_for_each_safe(pos, q, &md->part) {
1757                 part_md = list_entry(pos, struct mmc_blk_data, part);
1758                 list_del(pos);
1759                 mmc_blk_remove_req(part_md);
1760         }
1761 }
1762
1763 static int mmc_add_disk(struct mmc_blk_data *md)
1764 {
1765         int ret;
1766         struct mmc_card *card = md->queue.card;
1767
1768         add_disk(md->disk);
1769         md->force_ro.show = force_ro_show;
1770         md->force_ro.store = force_ro_store;
1771         sysfs_attr_init(&md->force_ro.attr);
1772         md->force_ro.attr.name = "force_ro";
1773         md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
1774         ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
1775         if (ret)
1776                 goto force_ro_fail;
1777
1778         if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
1779              card->ext_csd.boot_ro_lockable) {
1780                 umode_t mode;
1781
1782                 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
1783                         mode = S_IRUGO;
1784                 else
1785                         mode = S_IRUGO | S_IWUSR;
1786
1787                 md->power_ro_lock.show = power_ro_lock_show;
1788                 md->power_ro_lock.store = power_ro_lock_store;
1789                 sysfs_attr_init(&md->power_ro_lock.attr);
1790                 md->power_ro_lock.attr.mode = mode;
1791                 md->power_ro_lock.attr.name =
1792                                         "ro_lock_until_next_power_on";
1793                 ret = device_create_file(disk_to_dev(md->disk),
1794                                 &md->power_ro_lock);
1795                 if (ret)
1796                         goto power_ro_lock_fail;
1797         }
1798         return ret;
1799
1800 power_ro_lock_fail:
1801         device_remove_file(disk_to_dev(md->disk), &md->force_ro);
1802 force_ro_fail:
1803         del_gendisk(md->disk);
1804
1805         return ret;
1806 }
1807
1808 #define CID_MANFID_SANDISK      0x2
1809 #define CID_MANFID_TOSHIBA      0x11
1810 #define CID_MANFID_MICRON       0x13
1811 #define CID_MANFID_SAMSUNG      0x15
1812
1813 static const struct mmc_fixup blk_fixups[] =
1814 {
1815         MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
1816                   MMC_QUIRK_INAND_CMD38),
1817         MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
1818                   MMC_QUIRK_INAND_CMD38),
1819         MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
1820                   MMC_QUIRK_INAND_CMD38),
1821         MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
1822                   MMC_QUIRK_INAND_CMD38),
1823         MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
1824                   MMC_QUIRK_INAND_CMD38),
1825
1826         /*
1827          * Some MMC cards experience performance degradation with CMD23
1828          * instead of CMD12-bounded multiblock transfers. For now we'll
1829          * black list what's bad...
1830          * - Certain Toshiba cards.
1831          *
1832          * N.B. This doesn't affect SD cards.
1833          */
1834         MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1835                   MMC_QUIRK_BLK_NO_CMD23),
1836         MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1837                   MMC_QUIRK_BLK_NO_CMD23),
1838         MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
1839                   MMC_QUIRK_BLK_NO_CMD23),
1840
1841         /*
1842          * Some Micron MMC cards needs longer data read timeout than
1843          * indicated in CSD.
1844          */
1845         MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
1846                   MMC_QUIRK_LONG_READ_TIME),
1847
1848         /*
1849          * On these Samsung MoviNAND parts, performing secure erase or
1850          * secure trim can result in unrecoverable corruption due to a
1851          * firmware bug.
1852          */
1853         MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1854                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1855         MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1856                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1857         MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1858                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1859         MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1860                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1861         MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1862                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1863         MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1864                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1865         MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1866                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1867         MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
1868                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
1869
1870         END_FIXUP
1871 };
1872
1873 static int mmc_blk_probe(struct mmc_card *card)
1874 {
1875         struct mmc_blk_data *md, *part_md;
1876         char cap_str[10];
1877
1878         /*
1879          * Check that the card supports the command class(es) we need.
1880          */
1881         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
1882                 return -ENODEV;
1883
1884         md = mmc_blk_alloc(card);
1885         if (IS_ERR(md))
1886                 return PTR_ERR(md);
1887
1888         string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
1889                         cap_str, sizeof(cap_str));
1890         pr_info("%s: %s %s %s %s\n",
1891                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
1892                 cap_str, md->read_only ? "(ro)" : "");
1893
1894         if (mmc_blk_alloc_parts(card, md))
1895                 goto out;
1896
1897         mmc_set_drvdata(card, md);
1898         mmc_fixup_device(card, blk_fixups);
1899
1900         if (mmc_add_disk(md))
1901                 goto out;
1902
1903         list_for_each_entry(part_md, &md->part, part) {
1904                 if (mmc_add_disk(part_md))
1905                         goto out;
1906         }
1907         return 0;
1908
1909  out:
1910         mmc_blk_remove_parts(card, md);
1911         mmc_blk_remove_req(md);
1912         return 0;
1913 }
1914
1915 static void mmc_blk_remove(struct mmc_card *card)
1916 {
1917         struct mmc_blk_data *md = mmc_get_drvdata(card);
1918
1919         mmc_blk_remove_parts(card, md);
1920         mmc_claim_host(card->host);
1921         mmc_blk_part_switch(card, md);
1922         mmc_release_host(card->host);
1923         mmc_blk_remove_req(md);
1924         mmc_set_drvdata(card, NULL);
1925 }
1926
1927 #ifdef CONFIG_PM
1928 static int mmc_blk_suspend(struct mmc_card *card)
1929 {
1930         struct mmc_blk_data *part_md;
1931         struct mmc_blk_data *md = mmc_get_drvdata(card);
1932
1933         if (md) {
1934                 mmc_queue_suspend(&md->queue);
1935                 list_for_each_entry(part_md, &md->part, part) {
1936                         mmc_queue_suspend(&part_md->queue);
1937                 }
1938         }
1939         return 0;
1940 }
1941
1942 static int mmc_blk_resume(struct mmc_card *card)
1943 {
1944         struct mmc_blk_data *part_md;
1945         struct mmc_blk_data *md = mmc_get_drvdata(card);
1946
1947         if (md) {
1948                 /*
1949                  * Resume involves the card going into idle state,
1950                  * so current partition is always the main one.
1951                  */
1952                 md->part_curr = md->part_type;
1953                 mmc_queue_resume(&md->queue);
1954                 list_for_each_entry(part_md, &md->part, part) {
1955                         mmc_queue_resume(&part_md->queue);
1956                 }
1957         }
1958         return 0;
1959 }
1960 #else
1961 #define mmc_blk_suspend NULL
1962 #define mmc_blk_resume  NULL
1963 #endif
1964
1965 static struct mmc_driver mmc_driver = {
1966         .drv            = {
1967                 .name   = "mmcblk",
1968         },
1969         .probe          = mmc_blk_probe,
1970         .remove         = mmc_blk_remove,
1971         .suspend        = mmc_blk_suspend,
1972         .resume         = mmc_blk_resume,
1973 };
1974
1975 static int __init mmc_blk_init(void)
1976 {
1977         int res;
1978
1979         if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
1980                 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
1981
1982         max_devices = 256 / perdev_minors;
1983
1984         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
1985         if (res)
1986                 goto out;
1987
1988         res = mmc_register_driver(&mmc_driver);
1989         if (res)
1990                 goto out2;
1991
1992         return 0;
1993  out2:
1994         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
1995  out:
1996         return res;
1997 }
1998
1999 static void __exit mmc_blk_exit(void)
2000 {
2001         mmc_unregister_driver(&mmc_driver);
2002         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2003 }
2004
2005 module_init(mmc_blk_init);
2006 module_exit(mmc_blk_exit);
2007
2008 MODULE_LICENSE("GPL");
2009 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2010