8d4d8837fb00a52bce02a9a2419803aaf555c8b7
[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",
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 static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
956 {
957         struct mmc_blk_data *md = mq->data;
958         struct mmc_card *card = md->queue.card;
959         unsigned int from, nr, arg;
960         int err = 0, type = MMC_BLK_DISCARD;
961
962         if (!mmc_can_erase(card)) {
963                 err = -EOPNOTSUPP;
964                 goto out;
965         }
966
967         from = blk_rq_pos(req);
968         nr = blk_rq_sectors(req);
969
970         if (mmc_can_discard(card))
971                 arg = MMC_DISCARD_ARG;
972         else if (mmc_can_trim(card))
973                 arg = MMC_TRIM_ARG;
974         else
975                 arg = MMC_ERASE_ARG;
976 retry:
977         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
978                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
979                                  INAND_CMD38_ARG_EXT_CSD,
980                                  arg == MMC_TRIM_ARG ?
981                                  INAND_CMD38_ARG_TRIM :
982                                  INAND_CMD38_ARG_ERASE,
983                                  0);
984                 if (err)
985                         goto out;
986         }
987         err = mmc_erase(card, from, nr, arg);
988 out:
989         if (err == -EIO && !mmc_blk_reset(md, card->host, type))
990                 goto retry;
991         if (!err)
992                 mmc_blk_reset_success(md, type);
993         blk_end_request(req, err, blk_rq_bytes(req));
994
995         return err ? 0 : 1;
996 }
997
998 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
999                                        struct request *req)
1000 {
1001         struct mmc_blk_data *md = mq->data;
1002         struct mmc_card *card = md->queue.card;
1003         unsigned int from, nr, arg;
1004         int err = 0, type = MMC_BLK_SECDISCARD;
1005
1006         if (!(mmc_can_secure_erase_trim(card))) {
1007                 err = -EOPNOTSUPP;
1008                 goto out;
1009         }
1010
1011         from = blk_rq_pos(req);
1012         nr = blk_rq_sectors(req);
1013
1014         if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1015                 arg = MMC_SECURE_TRIM1_ARG;
1016         else
1017                 arg = MMC_SECURE_ERASE_ARG;
1018
1019 retry:
1020         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1021                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1022                                  INAND_CMD38_ARG_EXT_CSD,
1023                                  arg == MMC_SECURE_TRIM1_ARG ?
1024                                  INAND_CMD38_ARG_SECTRIM1 :
1025                                  INAND_CMD38_ARG_SECERASE,
1026                                  0);
1027                 if (err)
1028                         goto out_retry;
1029         }
1030
1031         err = mmc_erase(card, from, nr, arg);
1032         if (err == -EIO)
1033                 goto out_retry;
1034         if (err)
1035                 goto out;
1036
1037         if (arg == MMC_SECURE_TRIM1_ARG) {
1038                 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1039                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1040                                          INAND_CMD38_ARG_EXT_CSD,
1041                                          INAND_CMD38_ARG_SECTRIM2,
1042                                          0);
1043                         if (err)
1044                                 goto out_retry;
1045                 }
1046
1047                 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1048                 if (err == -EIO)
1049                         goto out_retry;
1050                 if (err)
1051                         goto out;
1052         }
1053
1054 out_retry:
1055         if (err && !mmc_blk_reset(md, card->host, type))
1056                 goto retry;
1057         if (!err)
1058                 mmc_blk_reset_success(md, type);
1059 out:
1060         blk_end_request(req, err, blk_rq_bytes(req));
1061
1062         return err ? 0 : 1;
1063 }
1064
1065 static int mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1066 {
1067         struct mmc_blk_data *md = mq->data;
1068         struct mmc_card *card = md->queue.card;
1069         int ret = 0;
1070
1071         ret = mmc_flush_cache(card);
1072         if (ret)
1073                 ret = -EIO;
1074
1075         blk_end_request_all(req, ret);
1076
1077         return ret ? 0 : 1;
1078 }
1079
1080 /*
1081  * Reformat current write as a reliable write, supporting
1082  * both legacy and the enhanced reliable write MMC cards.
1083  * In each transfer we'll handle only as much as a single
1084  * reliable write can handle, thus finish the request in
1085  * partial completions.
1086  */
1087 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1088                                     struct mmc_card *card,
1089                                     struct request *req)
1090 {
1091         if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1092                 /* Legacy mode imposes restrictions on transfers. */
1093                 if (!IS_ALIGNED(brq->cmd.arg, card->ext_csd.rel_sectors))
1094                         brq->data.blocks = 1;
1095
1096                 if (brq->data.blocks > card->ext_csd.rel_sectors)
1097                         brq->data.blocks = card->ext_csd.rel_sectors;
1098                 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1099                         brq->data.blocks = 1;
1100         }
1101 }
1102
1103 #define CMD_ERRORS                                                      \
1104         (R1_OUT_OF_RANGE |      /* Command argument out of range */     \
1105          R1_ADDRESS_ERROR |     /* Misaligned address */                \
1106          R1_BLOCK_LEN_ERROR |   /* Transferred block length incorrect */\
1107          R1_WP_VIOLATION |      /* Tried to write to protected block */ \
1108          R1_CC_ERROR |          /* Card controller error */             \
1109          R1_ERROR)              /* General/unknown error */
1110
1111 static int mmc_blk_err_check(struct mmc_card *card,
1112                              struct mmc_async_req *areq)
1113 {
1114         struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1115                                                     mmc_active);
1116         struct mmc_blk_request *brq = &mq_mrq->brq;
1117         struct request *req = mq_mrq->req;
1118         int ecc_err = 0, gen_err = 0;
1119
1120         /*
1121          * sbc.error indicates a problem with the set block count
1122          * command.  No data will have been transferred.
1123          *
1124          * cmd.error indicates a problem with the r/w command.  No
1125          * data will have been transferred.
1126          *
1127          * stop.error indicates a problem with the stop command.  Data
1128          * may have been transferred, or may still be transferring.
1129          */
1130         if (brq->sbc.error || brq->cmd.error || brq->stop.error ||
1131             brq->data.error) {
1132                 switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1133                 case ERR_RETRY:
1134                         return MMC_BLK_RETRY;
1135                 case ERR_ABORT:
1136                         return MMC_BLK_ABORT;
1137                 case ERR_NOMEDIUM:
1138                         return MMC_BLK_NOMEDIUM;
1139                 case ERR_CONTINUE:
1140                         break;
1141                 }
1142         }
1143
1144         /*
1145          * Check for errors relating to the execution of the
1146          * initial command - such as address errors.  No data
1147          * has been transferred.
1148          */
1149         if (brq->cmd.resp[0] & CMD_ERRORS) {
1150                 pr_err("%s: r/w command failed, status = %#x\n",
1151                        req->rq_disk->disk_name, brq->cmd.resp[0]);
1152                 return MMC_BLK_ABORT;
1153         }
1154
1155         /*
1156          * Everything else is either success, or a data error of some
1157          * kind.  If it was a write, we may have transitioned to
1158          * program mode, which we have to wait for it to complete.
1159          */
1160         if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1161                 u32 status;
1162                 unsigned long timeout;
1163
1164                 /* Check stop command response */
1165                 if (brq->stop.resp[0] & R1_ERROR) {
1166                         pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1167                                req->rq_disk->disk_name, __func__,
1168                                brq->stop.resp[0]);
1169                         gen_err = 1;
1170                 }
1171
1172                 timeout = jiffies + msecs_to_jiffies(MMC_BLK_TIMEOUT_MS);
1173                 do {
1174                         int err = get_card_status(card, &status, 5);
1175                         if (err) {
1176                                 pr_err("%s: error %d requesting status\n",
1177                                        req->rq_disk->disk_name, err);
1178                                 return MMC_BLK_CMD_ERR;
1179                         }
1180
1181                         if (status & R1_ERROR) {
1182                                 pr_err("%s: %s: general error sending status command, card status %#x\n",
1183                                        req->rq_disk->disk_name, __func__,
1184                                        status);
1185                                 gen_err = 1;
1186                         }
1187
1188                         /* Timeout if the device never becomes ready for data
1189                          * and never leaves the program state.
1190                          */
1191                         if (time_after(jiffies, timeout)) {
1192                                 pr_err("%s: Card stuck in programming state!"\
1193                                         " %s %s\n", mmc_hostname(card->host),
1194                                         req->rq_disk->disk_name, __func__);
1195
1196                                 return MMC_BLK_CMD_ERR;
1197                         }
1198                         /*
1199                          * Some cards mishandle the status bits,
1200                          * so make sure to check both the busy
1201                          * indication and the card state.
1202                          */
1203                 } while (!(status & R1_READY_FOR_DATA) ||
1204                          (R1_CURRENT_STATE(status) == R1_STATE_PRG));
1205         }
1206
1207         /* if general error occurs, retry the write operation. */
1208         if (gen_err) {
1209                 pr_warn("%s: retrying write for general error\n",
1210                                 req->rq_disk->disk_name);
1211                 return MMC_BLK_RETRY;
1212         }
1213
1214         if (brq->data.error) {
1215                 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1216                        req->rq_disk->disk_name, brq->data.error,
1217                        (unsigned)blk_rq_pos(req),
1218                        (unsigned)blk_rq_sectors(req),
1219                        brq->cmd.resp[0], brq->stop.resp[0]);
1220
1221                 if (rq_data_dir(req) == READ) {
1222                         if (ecc_err)
1223                                 return MMC_BLK_ECC_ERR;
1224                         return MMC_BLK_DATA_ERR;
1225                 } else {
1226                         return MMC_BLK_CMD_ERR;
1227                 }
1228         }
1229
1230         if (!brq->data.bytes_xfered)
1231                 return MMC_BLK_RETRY;
1232
1233         if (mmc_packed_cmd(mq_mrq->cmd_type)) {
1234                 if (unlikely(brq->data.blocks << 9 != brq->data.bytes_xfered))
1235                         return MMC_BLK_PARTIAL;
1236                 else
1237                         return MMC_BLK_SUCCESS;
1238         }
1239
1240         if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1241                 return MMC_BLK_PARTIAL;
1242
1243         return MMC_BLK_SUCCESS;
1244 }
1245
1246 static int mmc_blk_packed_err_check(struct mmc_card *card,
1247                                     struct mmc_async_req *areq)
1248 {
1249         struct mmc_queue_req *mq_rq = container_of(areq, struct mmc_queue_req,
1250                         mmc_active);
1251         struct request *req = mq_rq->req;
1252         struct mmc_packed *packed = mq_rq->packed;
1253         int err, check, status;
1254         u8 *ext_csd;
1255
1256         BUG_ON(!packed);
1257
1258         packed->retries--;
1259         check = mmc_blk_err_check(card, areq);
1260         err = get_card_status(card, &status, 0);
1261         if (err) {
1262                 pr_err("%s: error %d sending status command\n",
1263                        req->rq_disk->disk_name, err);
1264                 return MMC_BLK_ABORT;
1265         }
1266
1267         if (status & R1_EXCEPTION_EVENT) {
1268                 ext_csd = kzalloc(512, GFP_KERNEL);
1269                 if (!ext_csd) {
1270                         pr_err("%s: unable to allocate buffer for ext_csd\n",
1271                                req->rq_disk->disk_name);
1272                         return -ENOMEM;
1273                 }
1274
1275                 err = mmc_send_ext_csd(card, ext_csd);
1276                 if (err) {
1277                         pr_err("%s: error %d sending ext_csd\n",
1278                                req->rq_disk->disk_name, err);
1279                         check = MMC_BLK_ABORT;
1280                         goto free;
1281                 }
1282
1283                 if ((ext_csd[EXT_CSD_EXP_EVENTS_STATUS] &
1284                      EXT_CSD_PACKED_FAILURE) &&
1285                     (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1286                      EXT_CSD_PACKED_GENERIC_ERROR)) {
1287                         if (ext_csd[EXT_CSD_PACKED_CMD_STATUS] &
1288                             EXT_CSD_PACKED_INDEXED_ERROR) {
1289                                 packed->idx_failure =
1290                                   ext_csd[EXT_CSD_PACKED_FAILURE_INDEX] - 1;
1291                                 check = MMC_BLK_PARTIAL;
1292                         }
1293                         pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1294                                "failure index: %d\n",
1295                                req->rq_disk->disk_name, packed->nr_entries,
1296                                packed->blocks, packed->idx_failure);
1297                 }
1298 free:
1299                 kfree(ext_csd);
1300         }
1301
1302         return check;
1303 }
1304
1305 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1306                                struct mmc_card *card,
1307                                int disable_multi,
1308                                struct mmc_queue *mq)
1309 {
1310         u32 readcmd, writecmd;
1311         struct mmc_blk_request *brq = &mqrq->brq;
1312         struct request *req = mqrq->req;
1313         struct mmc_blk_data *md = mq->data;
1314         bool do_data_tag;
1315
1316         /*
1317          * Reliable writes are used to implement Forced Unit Access and
1318          * REQ_META accesses, and are supported only on MMCs.
1319          *
1320          * XXX: this really needs a good explanation of why REQ_META
1321          * is treated special.
1322          */
1323         bool do_rel_wr = ((req->cmd_flags & REQ_FUA) ||
1324                           (req->cmd_flags & REQ_META)) &&
1325                 (rq_data_dir(req) == WRITE) &&
1326                 (md->flags & MMC_BLK_REL_WR);
1327
1328         memset(brq, 0, sizeof(struct mmc_blk_request));
1329         brq->mrq.cmd = &brq->cmd;
1330         brq->mrq.data = &brq->data;
1331
1332         brq->cmd.arg = blk_rq_pos(req);
1333         if (!mmc_card_blockaddr(card))
1334                 brq->cmd.arg <<= 9;
1335         brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1336         brq->data.blksz = 512;
1337         brq->stop.opcode = MMC_STOP_TRANSMISSION;
1338         brq->stop.arg = 0;
1339         brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1340         brq->data.blocks = blk_rq_sectors(req);
1341
1342         /*
1343          * The block layer doesn't support all sector count
1344          * restrictions, so we need to be prepared for too big
1345          * requests.
1346          */
1347         if (brq->data.blocks > card->host->max_blk_count)
1348                 brq->data.blocks = card->host->max_blk_count;
1349
1350         if (brq->data.blocks > 1) {
1351                 /*
1352                  * After a read error, we redo the request one sector
1353                  * at a time in order to accurately determine which
1354                  * sectors can be read successfully.
1355                  */
1356                 if (disable_multi)
1357                         brq->data.blocks = 1;
1358
1359                 /* Some controllers can't do multiblock reads due to hw bugs */
1360                 if (card->host->caps2 & MMC_CAP2_NO_MULTI_READ &&
1361                     rq_data_dir(req) == READ)
1362                         brq->data.blocks = 1;
1363         }
1364
1365         if (brq->data.blocks > 1 || do_rel_wr) {
1366                 /* SPI multiblock writes terminate using a special
1367                  * token, not a STOP_TRANSMISSION request.
1368                  */
1369                 if (!mmc_host_is_spi(card->host) ||
1370                     rq_data_dir(req) == READ)
1371                         brq->mrq.stop = &brq->stop;
1372                 readcmd = MMC_READ_MULTIPLE_BLOCK;
1373                 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1374         } else {
1375                 brq->mrq.stop = NULL;
1376                 readcmd = MMC_READ_SINGLE_BLOCK;
1377                 writecmd = MMC_WRITE_BLOCK;
1378         }
1379         if (rq_data_dir(req) == READ) {
1380                 brq->cmd.opcode = readcmd;
1381                 brq->data.flags |= MMC_DATA_READ;
1382         } else {
1383                 brq->cmd.opcode = writecmd;
1384                 brq->data.flags |= MMC_DATA_WRITE;
1385         }
1386
1387         if (do_rel_wr)
1388                 mmc_apply_rel_rw(brq, card, req);
1389
1390         /*
1391          * Data tag is used only during writing meta data to speed
1392          * up write and any subsequent read of this meta data
1393          */
1394         do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1395                 (req->cmd_flags & REQ_META) &&
1396                 (rq_data_dir(req) == WRITE) &&
1397                 ((brq->data.blocks * brq->data.blksz) >=
1398                  card->ext_csd.data_tag_unit_size);
1399
1400         /*
1401          * Pre-defined multi-block transfers are preferable to
1402          * open ended-ones (and necessary for reliable writes).
1403          * However, it is not sufficient to just send CMD23,
1404          * and avoid the final CMD12, as on an error condition
1405          * CMD12 (stop) needs to be sent anyway. This, coupled
1406          * with Auto-CMD23 enhancements provided by some
1407          * hosts, means that the complexity of dealing
1408          * with this is best left to the host. If CMD23 is
1409          * supported by card and host, we'll fill sbc in and let
1410          * the host deal with handling it correctly. This means
1411          * that for hosts that don't expose MMC_CAP_CMD23, no
1412          * change of behavior will be observed.
1413          *
1414          * N.B: Some MMC cards experience perf degradation.
1415          * We'll avoid using CMD23-bounded multiblock writes for
1416          * these, while retaining features like reliable writes.
1417          */
1418         if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1419             (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1420              do_data_tag)) {
1421                 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1422                 brq->sbc.arg = brq->data.blocks |
1423                         (do_rel_wr ? (1 << 31) : 0) |
1424                         (do_data_tag ? (1 << 29) : 0);
1425                 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1426                 brq->mrq.sbc = &brq->sbc;
1427         }
1428
1429         mmc_set_data_timeout(&brq->data, card);
1430
1431         brq->data.sg = mqrq->sg;
1432         brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1433
1434         /*
1435          * Adjust the sg list so it is the same size as the
1436          * request.
1437          */
1438         if (brq->data.blocks != blk_rq_sectors(req)) {
1439                 int i, data_size = brq->data.blocks << 9;
1440                 struct scatterlist *sg;
1441
1442                 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1443                         data_size -= sg->length;
1444                         if (data_size <= 0) {
1445                                 sg->length += data_size;
1446                                 i++;
1447                                 break;
1448                         }
1449                 }
1450                 brq->data.sg_len = i;
1451         }
1452
1453         mqrq->mmc_active.mrq = &brq->mrq;
1454         mqrq->mmc_active.err_check = mmc_blk_err_check;
1455
1456         mmc_queue_bounce_pre(mqrq);
1457 }
1458
1459 static inline u8 mmc_calc_packed_hdr_segs(struct request_queue *q,
1460                                           struct mmc_card *card)
1461 {
1462         unsigned int hdr_sz = mmc_large_sector(card) ? 4096 : 512;
1463         unsigned int max_seg_sz = queue_max_segment_size(q);
1464         unsigned int len, nr_segs = 0;
1465
1466         do {
1467                 len = min(hdr_sz, max_seg_sz);
1468                 hdr_sz -= len;
1469                 nr_segs++;
1470         } while (hdr_sz);
1471
1472         return nr_segs;
1473 }
1474
1475 static u8 mmc_blk_prep_packed_list(struct mmc_queue *mq, struct request *req)
1476 {
1477         struct request_queue *q = mq->queue;
1478         struct mmc_card *card = mq->card;
1479         struct request *cur = req, *next = NULL;
1480         struct mmc_blk_data *md = mq->data;
1481         struct mmc_queue_req *mqrq = mq->mqrq_cur;
1482         bool en_rel_wr = card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN;
1483         unsigned int req_sectors = 0, phys_segments = 0;
1484         unsigned int max_blk_count, max_phys_segs;
1485         bool put_back = true;
1486         u8 max_packed_rw = 0;
1487         u8 reqs = 0;
1488
1489         if (!(md->flags & MMC_BLK_PACKED_CMD))
1490                 goto no_packed;
1491
1492         if ((rq_data_dir(cur) == WRITE) &&
1493             mmc_host_packed_wr(card->host))
1494                 max_packed_rw = card->ext_csd.max_packed_writes;
1495
1496         if (max_packed_rw == 0)
1497                 goto no_packed;
1498
1499         if (mmc_req_rel_wr(cur) &&
1500             (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1501                 goto no_packed;
1502
1503         if (mmc_large_sector(card) &&
1504             !IS_ALIGNED(blk_rq_sectors(cur), 8))
1505                 goto no_packed;
1506
1507         mmc_blk_clear_packed(mqrq);
1508
1509         max_blk_count = min(card->host->max_blk_count,
1510                             card->host->max_req_size >> 9);
1511         if (unlikely(max_blk_count > 0xffff))
1512                 max_blk_count = 0xffff;
1513
1514         max_phys_segs = queue_max_segments(q);
1515         req_sectors += blk_rq_sectors(cur);
1516         phys_segments += cur->nr_phys_segments;
1517
1518         if (rq_data_dir(cur) == WRITE) {
1519                 req_sectors += mmc_large_sector(card) ? 8 : 1;
1520                 phys_segments += mmc_calc_packed_hdr_segs(q, card);
1521         }
1522
1523         do {
1524                 if (reqs >= max_packed_rw - 1) {
1525                         put_back = false;
1526                         break;
1527                 }
1528
1529                 spin_lock_irq(q->queue_lock);
1530                 next = blk_fetch_request(q);
1531                 spin_unlock_irq(q->queue_lock);
1532                 if (!next) {
1533                         put_back = false;
1534                         break;
1535                 }
1536
1537                 if (mmc_large_sector(card) &&
1538                     !IS_ALIGNED(blk_rq_sectors(next), 8))
1539                         break;
1540
1541                 if (next->cmd_flags & REQ_DISCARD ||
1542                     next->cmd_flags & REQ_FLUSH)
1543                         break;
1544
1545                 if (rq_data_dir(cur) != rq_data_dir(next))
1546                         break;
1547
1548                 if (mmc_req_rel_wr(next) &&
1549                     (md->flags & MMC_BLK_REL_WR) && !en_rel_wr)
1550                         break;
1551
1552                 req_sectors += blk_rq_sectors(next);
1553                 if (req_sectors > max_blk_count)
1554                         break;
1555
1556                 phys_segments +=  next->nr_phys_segments;
1557                 if (phys_segments > max_phys_segs)
1558                         break;
1559
1560                 list_add_tail(&next->queuelist, &mqrq->packed->list);
1561                 cur = next;
1562                 reqs++;
1563         } while (1);
1564
1565         if (put_back) {
1566                 spin_lock_irq(q->queue_lock);
1567                 blk_requeue_request(q, next);
1568                 spin_unlock_irq(q->queue_lock);
1569         }
1570
1571         if (reqs > 0) {
1572                 list_add(&req->queuelist, &mqrq->packed->list);
1573                 mqrq->packed->nr_entries = ++reqs;
1574                 mqrq->packed->retries = reqs;
1575                 return reqs;
1576         }
1577
1578 no_packed:
1579         mqrq->cmd_type = MMC_PACKED_NONE;
1580         return 0;
1581 }
1582
1583 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req *mqrq,
1584                                         struct mmc_card *card,
1585                                         struct mmc_queue *mq)
1586 {
1587         struct mmc_blk_request *brq = &mqrq->brq;
1588         struct request *req = mqrq->req;
1589         struct request *prq;
1590         struct mmc_blk_data *md = mq->data;
1591         struct mmc_packed *packed = mqrq->packed;
1592         bool do_rel_wr, do_data_tag;
1593         u32 *packed_cmd_hdr;
1594         u8 hdr_blocks;
1595         u8 i = 1;
1596
1597         BUG_ON(!packed);
1598
1599         mqrq->cmd_type = MMC_PACKED_WRITE;
1600         packed->blocks = 0;
1601         packed->idx_failure = MMC_PACKED_NR_IDX;
1602
1603         packed_cmd_hdr = packed->cmd_hdr;
1604         memset(packed_cmd_hdr, 0, sizeof(packed->cmd_hdr));
1605         packed_cmd_hdr[0] = (packed->nr_entries << 16) |
1606                 (PACKED_CMD_WR << 8) | PACKED_CMD_VER;
1607         hdr_blocks = mmc_large_sector(card) ? 8 : 1;
1608
1609         /*
1610          * Argument for each entry of packed group
1611          */
1612         list_for_each_entry(prq, &packed->list, queuelist) {
1613                 do_rel_wr = mmc_req_rel_wr(prq) && (md->flags & MMC_BLK_REL_WR);
1614                 do_data_tag = (card->ext_csd.data_tag_unit_size) &&
1615                         (prq->cmd_flags & REQ_META) &&
1616                         (rq_data_dir(prq) == WRITE) &&
1617                         ((brq->data.blocks * brq->data.blksz) >=
1618                          card->ext_csd.data_tag_unit_size);
1619                 /* Argument of CMD23 */
1620                 packed_cmd_hdr[(i * 2)] =
1621                         (do_rel_wr ? MMC_CMD23_ARG_REL_WR : 0) |
1622                         (do_data_tag ? MMC_CMD23_ARG_TAG_REQ : 0) |
1623                         blk_rq_sectors(prq);
1624                 /* Argument of CMD18 or CMD25 */
1625                 packed_cmd_hdr[((i * 2)) + 1] =
1626                         mmc_card_blockaddr(card) ?
1627                         blk_rq_pos(prq) : blk_rq_pos(prq) << 9;
1628                 packed->blocks += blk_rq_sectors(prq);
1629                 i++;
1630         }
1631
1632         memset(brq, 0, sizeof(struct mmc_blk_request));
1633         brq->mrq.cmd = &brq->cmd;
1634         brq->mrq.data = &brq->data;
1635         brq->mrq.sbc = &brq->sbc;
1636         brq->mrq.stop = &brq->stop;
1637
1638         brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1639         brq->sbc.arg = MMC_CMD23_ARG_PACKED | (packed->blocks + hdr_blocks);
1640         brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1641
1642         brq->cmd.opcode = MMC_WRITE_MULTIPLE_BLOCK;
1643         brq->cmd.arg = blk_rq_pos(req);
1644         if (!mmc_card_blockaddr(card))
1645                 brq->cmd.arg <<= 9;
1646         brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1647
1648         brq->data.blksz = 512;
1649         brq->data.blocks = packed->blocks + hdr_blocks;
1650         brq->data.flags |= MMC_DATA_WRITE;
1651
1652         brq->stop.opcode = MMC_STOP_TRANSMISSION;
1653         brq->stop.arg = 0;
1654         brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1655
1656         mmc_set_data_timeout(&brq->data, card);
1657
1658         brq->data.sg = mqrq->sg;
1659         brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1660
1661         mqrq->mmc_active.mrq = &brq->mrq;
1662         mqrq->mmc_active.err_check = mmc_blk_packed_err_check;
1663
1664         mmc_queue_bounce_pre(mqrq);
1665 }
1666
1667 static int mmc_blk_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1668                            struct mmc_blk_request *brq, struct request *req,
1669                            int ret)
1670 {
1671         struct mmc_queue_req *mq_rq;
1672         mq_rq = container_of(brq, struct mmc_queue_req, brq);
1673
1674         /*
1675          * If this is an SD card and we're writing, we can first
1676          * mark the known good sectors as ok.
1677          *
1678          * If the card is not SD, we can still ok written sectors
1679          * as reported by the controller (which might be less than
1680          * the real number of written sectors, but never more).
1681          */
1682         if (mmc_card_sd(card)) {
1683                 u32 blocks;
1684
1685                 blocks = mmc_sd_num_wr_blocks(card);
1686                 if (blocks != (u32)-1) {
1687                         ret = blk_end_request(req, 0, blocks << 9);
1688                 }
1689         } else {
1690                 if (!mmc_packed_cmd(mq_rq->cmd_type))
1691                         ret = blk_end_request(req, 0, brq->data.bytes_xfered);
1692         }
1693         return ret;
1694 }
1695
1696 static int mmc_blk_end_packed_req(struct mmc_queue_req *mq_rq)
1697 {
1698         struct request *prq;
1699         struct mmc_packed *packed = mq_rq->packed;
1700         int idx = packed->idx_failure, i = 0;
1701         int ret = 0;
1702
1703         BUG_ON(!packed);
1704
1705         while (!list_empty(&packed->list)) {
1706                 prq = list_entry_rq(packed->list.next);
1707                 if (idx == i) {
1708                         /* retry from error index */
1709                         packed->nr_entries -= idx;
1710                         mq_rq->req = prq;
1711                         ret = 1;
1712
1713                         if (packed->nr_entries == MMC_PACKED_NR_SINGLE) {
1714                                 list_del_init(&prq->queuelist);
1715                                 mmc_blk_clear_packed(mq_rq);
1716                         }
1717                         return ret;
1718                 }
1719                 list_del_init(&prq->queuelist);
1720                 blk_end_request(prq, 0, blk_rq_bytes(prq));
1721                 i++;
1722         }
1723
1724         mmc_blk_clear_packed(mq_rq);
1725         return ret;
1726 }
1727
1728 static void mmc_blk_abort_packed_req(struct mmc_queue_req *mq_rq)
1729 {
1730         struct request *prq;
1731         struct mmc_packed *packed = mq_rq->packed;
1732
1733         BUG_ON(!packed);
1734
1735         while (!list_empty(&packed->list)) {
1736                 prq = list_entry_rq(packed->list.next);
1737                 list_del_init(&prq->queuelist);
1738                 blk_end_request(prq, -EIO, blk_rq_bytes(prq));
1739         }
1740
1741         mmc_blk_clear_packed(mq_rq);
1742 }
1743
1744 static void mmc_blk_revert_packed_req(struct mmc_queue *mq,
1745                                       struct mmc_queue_req *mq_rq)
1746 {
1747         struct request *prq;
1748         struct request_queue *q = mq->queue;
1749         struct mmc_packed *packed = mq_rq->packed;
1750
1751         BUG_ON(!packed);
1752
1753         while (!list_empty(&packed->list)) {
1754                 prq = list_entry_rq(packed->list.prev);
1755                 if (prq->queuelist.prev != &packed->list) {
1756                         list_del_init(&prq->queuelist);
1757                         spin_lock_irq(q->queue_lock);
1758                         blk_requeue_request(mq->queue, prq);
1759                         spin_unlock_irq(q->queue_lock);
1760                 } else {
1761                         list_del_init(&prq->queuelist);
1762                 }
1763         }
1764
1765         mmc_blk_clear_packed(mq_rq);
1766 }
1767
1768 static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *rqc)
1769 {
1770         struct mmc_blk_data *md = mq->data;
1771         struct mmc_card *card = md->queue.card;
1772         struct mmc_blk_request *brq = &mq->mqrq_cur->brq;
1773         int ret = 1, disable_multi = 0, retry = 0, type;
1774         enum mmc_blk_status status;
1775         struct mmc_queue_req *mq_rq;
1776         struct request *req = rqc;
1777         struct mmc_async_req *areq;
1778         const u8 packed_nr = 2;
1779         u8 reqs = 0;
1780
1781         if (!rqc && !mq->mqrq_prev->req)
1782                 return 0;
1783
1784         if (rqc)
1785                 reqs = mmc_blk_prep_packed_list(mq, rqc);
1786
1787         do {
1788                 if (rqc) {
1789                         /*
1790                          * When 4KB native sector is enabled, only 8 blocks
1791                          * multiple read or write is allowed
1792                          */
1793                         if ((brq->data.blocks & 0x07) &&
1794                             (card->ext_csd.data_sector_size == 4096)) {
1795                                 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1796                                         req->rq_disk->disk_name);
1797                                 mq_rq = mq->mqrq_cur;
1798                                 goto cmd_abort;
1799                         }
1800
1801                         if (reqs >= packed_nr)
1802                                 mmc_blk_packed_hdr_wrq_prep(mq->mqrq_cur,
1803                                                             card, mq);
1804                         else
1805                                 mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1806                         areq = &mq->mqrq_cur->mmc_active;
1807                 } else
1808                         areq = NULL;
1809                 areq = mmc_start_req(card->host, areq, (int *) &status);
1810                 if (!areq) {
1811                         if (status == MMC_BLK_NEW_REQUEST)
1812                                 mq->flags |= MMC_QUEUE_NEW_REQUEST;
1813                         return 0;
1814                 }
1815
1816                 mq_rq = container_of(areq, struct mmc_queue_req, mmc_active);
1817                 brq = &mq_rq->brq;
1818                 req = mq_rq->req;
1819                 type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1820                 mmc_queue_bounce_post(mq_rq);
1821
1822                 switch (status) {
1823                 case MMC_BLK_SUCCESS:
1824                 case MMC_BLK_PARTIAL:
1825                         /*
1826                          * A block was successfully transferred.
1827                          */
1828                         mmc_blk_reset_success(md, type);
1829
1830                         if (mmc_packed_cmd(mq_rq->cmd_type)) {
1831                                 ret = mmc_blk_end_packed_req(mq_rq);
1832                                 break;
1833                         } else {
1834                                 ret = blk_end_request(req, 0,
1835                                                 brq->data.bytes_xfered);
1836                         }
1837
1838                         /*
1839                          * If the blk_end_request function returns non-zero even
1840                          * though all data has been transferred and no errors
1841                          * were returned by the host controller, it's a bug.
1842                          */
1843                         if (status == MMC_BLK_SUCCESS && ret) {
1844                                 pr_err("%s BUG rq_tot %d d_xfer %d\n",
1845                                        __func__, blk_rq_bytes(req),
1846                                        brq->data.bytes_xfered);
1847                                 rqc = NULL;
1848                                 goto cmd_abort;
1849                         }
1850                         break;
1851                 case MMC_BLK_CMD_ERR:
1852                         ret = mmc_blk_cmd_err(md, card, brq, req, ret);
1853                         if (!mmc_blk_reset(md, card->host, type))
1854                                 break;
1855                         goto cmd_abort;
1856                 case MMC_BLK_RETRY:
1857                         if (retry++ < 5)
1858                                 break;
1859                         /* Fall through */
1860                 case MMC_BLK_ABORT:
1861                         if (!mmc_blk_reset(md, card->host, type))
1862                                 break;
1863                         goto cmd_abort;
1864                 case MMC_BLK_DATA_ERR: {
1865                         int err;
1866
1867                         err = mmc_blk_reset(md, card->host, type);
1868                         if (!err)
1869                                 break;
1870                         if (err == -ENODEV ||
1871                                 mmc_packed_cmd(mq_rq->cmd_type))
1872                                 goto cmd_abort;
1873                         /* Fall through */
1874                 }
1875                 case MMC_BLK_ECC_ERR:
1876                         if (brq->data.blocks > 1) {
1877                                 /* Redo read one sector at a time */
1878                                 pr_warning("%s: retrying using single block read\n",
1879                                            req->rq_disk->disk_name);
1880                                 disable_multi = 1;
1881                                 break;
1882                         }
1883                         /*
1884                          * After an error, we redo I/O one sector at a
1885                          * time, so we only reach here after trying to
1886                          * read a single sector.
1887                          */
1888                         ret = blk_end_request(req, -EIO,
1889                                                 brq->data.blksz);
1890                         if (!ret)
1891                                 goto start_new_req;
1892                         break;
1893                 case MMC_BLK_NOMEDIUM:
1894                         goto cmd_abort;
1895                 default:
1896                         pr_err("%s: Unhandled return value (%d)",
1897                                         req->rq_disk->disk_name, status);
1898                         goto cmd_abort;
1899                 }
1900
1901                 if (ret) {
1902                         if (mmc_packed_cmd(mq_rq->cmd_type)) {
1903                                 if (!mq_rq->packed->retries)
1904                                         goto cmd_abort;
1905                                 mmc_blk_packed_hdr_wrq_prep(mq_rq, card, mq);
1906                                 mmc_start_req(card->host,
1907                                               &mq_rq->mmc_active, NULL);
1908                         } else {
1909
1910                                 /*
1911                                  * In case of a incomplete request
1912                                  * prepare it again and resend.
1913                                  */
1914                                 mmc_blk_rw_rq_prep(mq_rq, card,
1915                                                 disable_multi, mq);
1916                                 mmc_start_req(card->host,
1917                                                 &mq_rq->mmc_active, NULL);
1918                         }
1919                 }
1920         } while (ret);
1921
1922         return 1;
1923
1924  cmd_abort:
1925         if (mmc_packed_cmd(mq_rq->cmd_type)) {
1926                 mmc_blk_abort_packed_req(mq_rq);
1927         } else {
1928                 if (mmc_card_removed(card))
1929                         req->cmd_flags |= REQ_QUIET;
1930                 while (ret)
1931                         ret = blk_end_request(req, -EIO,
1932                                         blk_rq_cur_bytes(req));
1933         }
1934
1935  start_new_req:
1936         if (rqc) {
1937                 if (mmc_card_removed(card)) {
1938                         rqc->cmd_flags |= REQ_QUIET;
1939                         blk_end_request_all(rqc, -EIO);
1940                 } else {
1941                         /*
1942                          * If current request is packed, it needs to put back.
1943                          */
1944                         if (mmc_packed_cmd(mq->mqrq_cur->cmd_type))
1945                                 mmc_blk_revert_packed_req(mq, mq->mqrq_cur);
1946
1947                         mmc_blk_rw_rq_prep(mq->mqrq_cur, card, 0, mq);
1948                         mmc_start_req(card->host,
1949                                       &mq->mqrq_cur->mmc_active, NULL);
1950                 }
1951         }
1952
1953         return 0;
1954 }
1955
1956 static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1957 {
1958         int ret;
1959         struct mmc_blk_data *md = mq->data;
1960         struct mmc_card *card = md->queue.card;
1961         struct mmc_host *host = card->host;
1962         unsigned long flags;
1963         unsigned int cmd_flags = req ? req->cmd_flags : 0;
1964
1965 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
1966         if (mmc_bus_needs_resume(card->host))
1967                 mmc_resume_bus(card->host);
1968 #endif
1969
1970         if (req && !mq->mqrq_prev->req)
1971                 /* claim host only for the first request */
1972                 mmc_get_card(card);
1973
1974         ret = mmc_blk_part_switch(card, md);
1975         if (ret) {
1976                 if (req) {
1977                         blk_end_request_all(req, -EIO);
1978                 }
1979                 ret = 0;
1980                 goto out;
1981         }
1982
1983         mq->flags &= ~MMC_QUEUE_NEW_REQUEST;
1984         if (cmd_flags & REQ_DISCARD) {
1985                 /* complete ongoing async transfer before issuing discard */
1986                 if (card->host->areq)
1987                         mmc_blk_issue_rw_rq(mq, NULL);
1988                 if (req->cmd_flags & REQ_SECURE &&
1989                         !(card->quirks & MMC_QUIRK_SEC_ERASE_TRIM_BROKEN))
1990                         ret = mmc_blk_issue_secdiscard_rq(mq, req);
1991                 else
1992                         ret = mmc_blk_issue_discard_rq(mq, req);
1993         } else if (cmd_flags & REQ_FLUSH) {
1994                 /* complete ongoing async transfer before issuing flush */
1995                 if (card->host->areq)
1996                         mmc_blk_issue_rw_rq(mq, NULL);
1997                 ret = mmc_blk_issue_flush(mq, req);
1998         } else {
1999                 if (!req && host->areq) {
2000                         spin_lock_irqsave(&host->context_info.lock, flags);
2001                         host->context_info.is_waiting_last_req = true;
2002                         spin_unlock_irqrestore(&host->context_info.lock, flags);
2003                 }
2004                 ret = mmc_blk_issue_rw_rq(mq, req);
2005         }
2006
2007 out:
2008         if ((!req && !(mq->flags & MMC_QUEUE_NEW_REQUEST)) ||
2009              (cmd_flags & MMC_REQ_SPECIAL_MASK))
2010                 /*
2011                  * Release host when there are no more requests
2012                  * and after special request(discard, flush) is done.
2013                  * In case sepecial request, there is no reentry to
2014                  * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2015                  */
2016                 mmc_put_card(card);
2017         return ret;
2018 }
2019
2020 static inline int mmc_blk_readonly(struct mmc_card *card)
2021 {
2022         return mmc_card_readonly(card) ||
2023                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2024 }
2025
2026 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2027                                               struct device *parent,
2028                                               sector_t size,
2029                                               bool default_ro,
2030                                               const char *subname,
2031                                               int area_type)
2032 {
2033         struct mmc_blk_data *md;
2034         int devidx, ret;
2035
2036         devidx = find_first_zero_bit(dev_use, max_devices);
2037         if (devidx >= max_devices)
2038                 return ERR_PTR(-ENOSPC);
2039         __set_bit(devidx, dev_use);
2040
2041         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2042         if (!md) {
2043                 ret = -ENOMEM;
2044                 goto out;
2045         }
2046
2047         /*
2048          * !subname implies we are creating main mmc_blk_data that will be
2049          * associated with mmc_card with mmc_set_drvdata. Due to device
2050          * partitions, devidx will not coincide with a per-physical card
2051          * index anymore so we keep track of a name index.
2052          */
2053         if (!subname) {
2054                 md->name_idx = find_first_zero_bit(name_use, max_devices);
2055                 __set_bit(md->name_idx, name_use);
2056         } else
2057                 md->name_idx = ((struct mmc_blk_data *)
2058                                 dev_to_disk(parent)->private_data)->name_idx;
2059
2060         md->area_type = area_type;
2061
2062         /*
2063          * Set the read-only status based on the supported commands
2064          * and the write protect switch.
2065          */
2066         md->read_only = mmc_blk_readonly(card);
2067
2068         md->disk = alloc_disk(perdev_minors);
2069         if (md->disk == NULL) {
2070                 ret = -ENOMEM;
2071                 goto err_kfree;
2072         }
2073
2074         spin_lock_init(&md->lock);
2075         INIT_LIST_HEAD(&md->part);
2076         md->usage = 1;
2077
2078         ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2079         if (ret)
2080                 goto err_putdisk;
2081
2082         md->queue.issue_fn = mmc_blk_issue_rq;
2083         md->queue.data = md;
2084
2085         md->disk->major = MMC_BLOCK_MAJOR;
2086         md->disk->first_minor = devidx * perdev_minors;
2087         md->disk->fops = &mmc_bdops;
2088         md->disk->private_data = md;
2089         md->disk->queue = md->queue.queue;
2090         md->disk->driverfs_dev = parent;
2091         set_disk_ro(md->disk, md->read_only || default_ro);
2092         md->disk->flags = GENHD_FL_EXT_DEVT;
2093         if (area_type & MMC_BLK_DATA_AREA_RPMB)
2094                 md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2095
2096         /*
2097          * As discussed on lkml, GENHD_FL_REMOVABLE should:
2098          *
2099          * - be set for removable media with permanent block devices
2100          * - be unset for removable block devices with permanent media
2101          *
2102          * Since MMC block devices clearly fall under the second
2103          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
2104          * should use the block device creation/destruction hotplug
2105          * messages to tell when the card is present.
2106          */
2107
2108         snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2109                  "mmcblk%d%s", md->name_idx, subname ? subname : "");
2110
2111         if (mmc_card_mmc(card))
2112                 blk_queue_logical_block_size(md->queue.queue,
2113                                              card->ext_csd.data_sector_size);
2114         else
2115                 blk_queue_logical_block_size(md->queue.queue, 512);
2116
2117         set_capacity(md->disk, size);
2118
2119         if (mmc_host_cmd23(card->host)) {
2120                 if (mmc_card_mmc(card) ||
2121                     (mmc_card_sd(card) &&
2122                      card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2123                         md->flags |= MMC_BLK_CMD23;
2124         }
2125
2126         if (mmc_card_mmc(card) &&
2127             md->flags & MMC_BLK_CMD23 &&
2128             ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2129              card->ext_csd.rel_sectors)) {
2130                 md->flags |= MMC_BLK_REL_WR;
2131                 blk_queue_flush(md->queue.queue, REQ_FLUSH | REQ_FUA);
2132         }
2133
2134         if (mmc_card_mmc(card) &&
2135             (area_type == MMC_BLK_DATA_AREA_MAIN) &&
2136             (md->flags & MMC_BLK_CMD23) &&
2137             card->ext_csd.packed_event_en) {
2138                 if (!mmc_packed_init(&md->queue, card))
2139                         md->flags |= MMC_BLK_PACKED_CMD;
2140         }
2141
2142         return md;
2143
2144  err_putdisk:
2145         put_disk(md->disk);
2146  err_kfree:
2147         kfree(md);
2148  out:
2149         return ERR_PTR(ret);
2150 }
2151
2152 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2153 {
2154         sector_t size;
2155         struct mmc_blk_data *md;
2156
2157         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2158                 /*
2159                  * The EXT_CSD sector count is in number or 512 byte
2160                  * sectors.
2161                  */
2162                 size = card->ext_csd.sectors;
2163         } else {
2164                 /*
2165                  * The CSD capacity field is in units of read_blkbits.
2166                  * set_capacity takes units of 512 bytes.
2167                  */
2168                 size = card->csd.capacity << (card->csd.read_blkbits - 9);
2169         }
2170
2171         md = mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2172                                         MMC_BLK_DATA_AREA_MAIN);
2173         return md;
2174 }
2175
2176 static int mmc_blk_alloc_part(struct mmc_card *card,
2177                               struct mmc_blk_data *md,
2178                               unsigned int part_type,
2179                               sector_t size,
2180                               bool default_ro,
2181                               const char *subname,
2182                               int area_type)
2183 {
2184         char cap_str[10];
2185         struct mmc_blk_data *part_md;
2186
2187         part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2188                                     subname, area_type);
2189         if (IS_ERR(part_md))
2190                 return PTR_ERR(part_md);
2191         part_md->part_type = part_type;
2192         list_add(&part_md->part, &md->part);
2193
2194         string_get_size((u64)get_capacity(part_md->disk) << 9, STRING_UNITS_2,
2195                         cap_str, sizeof(cap_str));
2196         pr_info("%s: %s %s partition %u %s\n",
2197                part_md->disk->disk_name, mmc_card_id(card),
2198                mmc_card_name(card), part_md->part_type, cap_str);
2199         return 0;
2200 }
2201
2202 /* MMC Physical partitions consist of two boot partitions and
2203  * up to four general purpose partitions.
2204  * For each partition enabled in EXT_CSD a block device will be allocatedi
2205  * to provide access to the partition.
2206  */
2207
2208 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2209 {
2210         int idx, ret = 0;
2211
2212         if (!mmc_card_mmc(card))
2213                 return 0;
2214
2215         for (idx = 0; idx < card->nr_parts; idx++) {
2216                 if (card->part[idx].size) {
2217                         ret = mmc_blk_alloc_part(card, md,
2218                                 card->part[idx].part_cfg,
2219                                 card->part[idx].size >> 9,
2220                                 card->part[idx].force_ro,
2221                                 card->part[idx].name,
2222                                 card->part[idx].area_type);
2223                         if (ret)
2224                                 return ret;
2225                 }
2226         }
2227
2228         return ret;
2229 }
2230
2231 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2232 {
2233         struct mmc_card *card;
2234
2235         if (md) {
2236                 /*
2237                  * Flush remaining requests and free queues. It
2238                  * is freeing the queue that stops new requests
2239                  * from being accepted.
2240                  */
2241                 card = md->queue.card;
2242                 mmc_cleanup_queue(&md->queue);
2243                 if (md->flags & MMC_BLK_PACKED_CMD)
2244                         mmc_packed_clean(&md->queue);
2245                 if (md->disk->flags & GENHD_FL_UP) {
2246                         device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2247                         if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2248                                         card->ext_csd.boot_ro_lockable)
2249                                 device_remove_file(disk_to_dev(md->disk),
2250                                         &md->power_ro_lock);
2251
2252                         del_gendisk(md->disk);
2253                 }
2254                 mmc_blk_put(md);
2255         }
2256 }
2257
2258 static void mmc_blk_remove_parts(struct mmc_card *card,
2259                                  struct mmc_blk_data *md)
2260 {
2261         struct list_head *pos, *q;
2262         struct mmc_blk_data *part_md;
2263
2264         __clear_bit(md->name_idx, name_use);
2265         list_for_each_safe(pos, q, &md->part) {
2266                 part_md = list_entry(pos, struct mmc_blk_data, part);
2267                 list_del(pos);
2268                 mmc_blk_remove_req(part_md);
2269         }
2270 }
2271
2272 static int mmc_add_disk(struct mmc_blk_data *md)
2273 {
2274         int ret;
2275         struct mmc_card *card = md->queue.card;
2276
2277         add_disk(md->disk);
2278         md->force_ro.show = force_ro_show;
2279         md->force_ro.store = force_ro_store;
2280         sysfs_attr_init(&md->force_ro.attr);
2281         md->force_ro.attr.name = "force_ro";
2282         md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2283         ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2284         if (ret)
2285                 goto force_ro_fail;
2286
2287         if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2288              card->ext_csd.boot_ro_lockable) {
2289                 umode_t mode;
2290
2291                 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2292                         mode = S_IRUGO;
2293                 else
2294                         mode = S_IRUGO | S_IWUSR;
2295
2296                 md->power_ro_lock.show = power_ro_lock_show;
2297                 md->power_ro_lock.store = power_ro_lock_store;
2298                 sysfs_attr_init(&md->power_ro_lock.attr);
2299                 md->power_ro_lock.attr.mode = mode;
2300                 md->power_ro_lock.attr.name =
2301                                         "ro_lock_until_next_power_on";
2302                 ret = device_create_file(disk_to_dev(md->disk),
2303                                 &md->power_ro_lock);
2304                 if (ret)
2305                         goto power_ro_lock_fail;
2306         }
2307         return ret;
2308
2309 power_ro_lock_fail:
2310         device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2311 force_ro_fail:
2312         del_gendisk(md->disk);
2313
2314         return ret;
2315 }
2316
2317 #define CID_MANFID_SANDISK      0x2
2318 #define CID_MANFID_TOSHIBA      0x11
2319 #define CID_MANFID_MICRON       0x13
2320 #define CID_MANFID_SAMSUNG      0x15
2321
2322 static const struct mmc_fixup blk_fixups[] =
2323 {
2324         MMC_FIXUP("SEM02G", CID_MANFID_SANDISK, 0x100, add_quirk,
2325                   MMC_QUIRK_INAND_CMD38),
2326         MMC_FIXUP("SEM04G", CID_MANFID_SANDISK, 0x100, add_quirk,
2327                   MMC_QUIRK_INAND_CMD38),
2328         MMC_FIXUP("SEM08G", CID_MANFID_SANDISK, 0x100, add_quirk,
2329                   MMC_QUIRK_INAND_CMD38),
2330         MMC_FIXUP("SEM16G", CID_MANFID_SANDISK, 0x100, add_quirk,
2331                   MMC_QUIRK_INAND_CMD38),
2332         MMC_FIXUP("SEM32G", CID_MANFID_SANDISK, 0x100, add_quirk,
2333                   MMC_QUIRK_INAND_CMD38),
2334
2335         /*
2336          * Some MMC cards experience performance degradation with CMD23
2337          * instead of CMD12-bounded multiblock transfers. For now we'll
2338          * black list what's bad...
2339          * - Certain Toshiba cards.
2340          *
2341          * N.B. This doesn't affect SD cards.
2342          */
2343         MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2344                   MMC_QUIRK_BLK_NO_CMD23),
2345         MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2346                   MMC_QUIRK_BLK_NO_CMD23),
2347         MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA, CID_OEMID_ANY, add_quirk_mmc,
2348                   MMC_QUIRK_BLK_NO_CMD23),
2349
2350         /*
2351          * Some Micron MMC cards needs longer data read timeout than
2352          * indicated in CSD.
2353          */
2354         MMC_FIXUP(CID_NAME_ANY, CID_MANFID_MICRON, 0x200, add_quirk_mmc,
2355                   MMC_QUIRK_LONG_READ_TIME),
2356
2357         /*
2358          * On these Samsung MoviNAND parts, performing secure erase or
2359          * secure trim can result in unrecoverable corruption due to a
2360          * firmware bug.
2361          */
2362         MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2363                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2364         MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2365                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2366         MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2367                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2368         MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2369                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2370         MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2371                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2372         MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2373                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2374         MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2375                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2376         MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG, CID_OEMID_ANY, add_quirk_mmc,
2377                   MMC_QUIRK_SEC_ERASE_TRIM_BROKEN),
2378
2379         END_FIXUP
2380 };
2381
2382 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2383 extern struct mmc_card *this_card;
2384 #endif
2385
2386 static int mmc_blk_probe(struct mmc_card *card)
2387 {
2388         struct mmc_blk_data *md, *part_md;
2389         char cap_str[10];
2390
2391         /*
2392          * Check that the card supports the command class(es) we need.
2393          */
2394         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2395                 return -ENODEV;
2396
2397         md = mmc_blk_alloc(card);
2398         if (IS_ERR(md))
2399                 return PTR_ERR(md);
2400
2401         string_get_size((u64)get_capacity(md->disk) << 9, STRING_UNITS_2,
2402                         cap_str, sizeof(cap_str));
2403         pr_info("%s: %s %s %s %s\n",
2404                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2405                 cap_str, md->read_only ? "(ro)" : "");
2406
2407         if (mmc_blk_alloc_parts(card, md))
2408                 goto out;
2409
2410         mmc_set_drvdata(card, md);
2411         mmc_fixup_device(card, blk_fixups);
2412
2413 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2414         mmc_set_bus_resume_policy(card->host, 1);
2415 #endif
2416 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2417     if(card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC){
2418         this_card = card;
2419         md->disk->emmc_disk = 1;
2420     }else {
2421         md->disk->emmc_disk = 0;
2422     }
2423 #endif
2424         if (mmc_add_disk(md))
2425                 goto out;
2426
2427         list_for_each_entry(part_md, &md->part, part) {
2428                 if (mmc_add_disk(part_md))
2429                         goto out;
2430         }
2431
2432         pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2433         pm_runtime_use_autosuspend(&card->dev);
2434
2435         /*
2436          * Don't enable runtime PM for SD-combo cards here. Leave that
2437          * decision to be taken during the SDIO init sequence instead.
2438          */
2439         if (card->type != MMC_TYPE_SD_COMBO) {
2440                 pm_runtime_set_active(&card->dev);
2441                 pm_runtime_enable(&card->dev);
2442         }
2443
2444         return 0;
2445
2446  out:
2447         mmc_blk_remove_parts(card, md);
2448         mmc_blk_remove_req(md);
2449         return 0;
2450 }
2451
2452 static void mmc_blk_remove(struct mmc_card *card)
2453 {
2454         struct mmc_blk_data *md = mmc_get_drvdata(card);
2455         
2456 #if defined(CONFIG_MMC_DW_ROCKCHIP)
2457     if(card->host->restrict_caps & RESTRICT_CARD_TYPE_EMMC)
2458         this_card = NULL;
2459 #endif
2460         mmc_blk_remove_parts(card, md);
2461         pm_runtime_get_sync(&card->dev);
2462         mmc_claim_host(card->host);
2463         mmc_blk_part_switch(card, md);
2464         mmc_release_host(card->host);
2465         if (card->type != MMC_TYPE_SD_COMBO)
2466                 pm_runtime_disable(&card->dev);
2467         pm_runtime_put_noidle(&card->dev);
2468         mmc_blk_remove_req(md);
2469         mmc_set_drvdata(card, NULL);
2470 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2471         mmc_set_bus_resume_policy(card->host, 0);
2472 #endif
2473 }
2474
2475 static int _mmc_blk_suspend(struct mmc_card *card)
2476 {
2477         struct mmc_blk_data *part_md;
2478         struct mmc_blk_data *md = mmc_get_drvdata(card);
2479
2480         if (md) {
2481                 mmc_queue_suspend(&md->queue);
2482                 list_for_each_entry(part_md, &md->part, part) {
2483                         mmc_queue_suspend(&part_md->queue);
2484                 }
2485         }
2486         return 0;
2487 }
2488
2489 static void mmc_blk_shutdown(struct mmc_card *card)
2490 {
2491         _mmc_blk_suspend(card);
2492 }
2493
2494 #ifdef CONFIG_PM
2495 static int mmc_blk_suspend(struct mmc_card *card)
2496 {
2497         return _mmc_blk_suspend(card);
2498 }
2499
2500 static int mmc_blk_resume(struct mmc_card *card)
2501 {
2502         struct mmc_blk_data *part_md;
2503         struct mmc_blk_data *md = mmc_get_drvdata(card);
2504
2505         if (md) {
2506                 /*
2507                  * Resume involves the card going into idle state,
2508                  * so current partition is always the main one.
2509                  */
2510                 md->part_curr = md->part_type;
2511                 mmc_queue_resume(&md->queue);
2512                 list_for_each_entry(part_md, &md->part, part) {
2513                         mmc_queue_resume(&part_md->queue);
2514                 }
2515         }
2516         return 0;
2517 }
2518 #else
2519 #define mmc_blk_suspend NULL
2520 #define mmc_blk_resume  NULL
2521 #endif
2522
2523 static struct mmc_driver mmc_driver = {
2524         .drv            = {
2525                 .name   = "mmcblk",
2526         },
2527         .probe          = mmc_blk_probe,
2528         .remove         = mmc_blk_remove,
2529         .suspend        = mmc_blk_suspend,
2530         .resume         = mmc_blk_resume,
2531         .shutdown       = mmc_blk_shutdown,
2532 };
2533
2534 static int __init mmc_blk_init(void)
2535 {
2536         int res;
2537
2538         if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2539                 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2540
2541         max_devices = 256 / perdev_minors;
2542
2543         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2544         if (res)
2545                 goto out;
2546
2547         res = mmc_register_driver(&mmc_driver);
2548         if (res)
2549                 goto out2;
2550
2551         return 0;
2552  out2:
2553         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2554  out:
2555         return res;
2556 }
2557
2558 static void __exit mmc_blk_exit(void)
2559 {
2560         mmc_unregister_driver(&mmc_driver);
2561         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2562 }
2563
2564 module_init(mmc_blk_init);
2565 module_exit(mmc_blk_exit);
2566
2567 MODULE_LICENSE("GPL");
2568 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2569