Linux 5.15.57
[platform/kernel/linux-rpi.git] / drivers / mmc / core / block.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Block driver for media (i.e., flash cards)
4  *
5  * Copyright 2002 Hewlett-Packard Company
6  * Copyright 2005-2008 Pierre Ossman
7  *
8  * Use consistent with the GNU GPL is permitted,
9  * provided that this copyright notice is
10  * preserved in its entirety in all copies and derived works.
11  *
12  * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
13  * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
14  * FITNESS FOR ANY PARTICULAR PURPOSE.
15  *
16  * Many thanks to Alessandro Rubini and Jonathan Corbet!
17  *
18  * Author:  Andrew Christian
19  *          28 May 2002
20  */
21 #include <linux/moduleparam.h>
22 #include <linux/module.h>
23 #include <linux/init.h>
24
25 #include <linux/kernel.h>
26 #include <linux/fs.h>
27 #include <linux/slab.h>
28 #include <linux/errno.h>
29 #include <linux/hdreg.h>
30 #include <linux/kdev_t.h>
31 #include <linux/kref.h>
32 #include <linux/blkdev.h>
33 #include <linux/cdev.h>
34 #include <linux/mutex.h>
35 #include <linux/scatterlist.h>
36 #include <linux/string_helpers.h>
37 #include <linux/delay.h>
38 #include <linux/capability.h>
39 #include <linux/compat.h>
40 #include <linux/pm_runtime.h>
41 #include <linux/idr.h>
42 #include <linux/debugfs.h>
43
44 #include <linux/mmc/ioctl.h>
45 #include <linux/mmc/card.h>
46 #include <linux/mmc/host.h>
47 #include <linux/mmc/mmc.h>
48 #include <linux/mmc/sd.h>
49
50 #include <linux/uaccess.h>
51
52 #include "queue.h"
53 #include "block.h"
54 #include "core.h"
55 #include "card.h"
56 #include "crypto.h"
57 #include "host.h"
58 #include "bus.h"
59 #include "mmc_ops.h"
60 #include "quirks.h"
61 #include "sd_ops.h"
62
63 MODULE_ALIAS("mmc:block");
64 #ifdef MODULE_PARAM_PREFIX
65 #undef MODULE_PARAM_PREFIX
66 #endif
67 #define MODULE_PARAM_PREFIX "mmcblk."
68
69 /*
70  * Set a 10 second timeout for polling write request busy state. Note, mmc core
71  * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
72  * second software timer to timeout the whole request, so 10 seconds should be
73  * ample.
74  */
75 #define MMC_BLK_TIMEOUT_MS  (10 * 1000)
76 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
77 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
78
79 #define mmc_req_rel_wr(req)     ((req->cmd_flags & REQ_FUA) && \
80                                   (rq_data_dir(req) == WRITE))
81 static DEFINE_MUTEX(block_mutex);
82
83 /*
84  * The defaults come from config options but can be overriden by module
85  * or bootarg options.
86  */
87 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
88
89 /*
90  * We've only got one major, so number of mmcblk devices is
91  * limited to (1 << 20) / number of minors per device.  It is also
92  * limited by the MAX_DEVICES below.
93  */
94 static int max_devices;
95
96 #define MAX_DEVICES 256
97
98 static DEFINE_IDA(mmc_blk_ida);
99 static DEFINE_IDA(mmc_rpmb_ida);
100
101 struct mmc_blk_busy_data {
102         struct mmc_card *card;
103         u32 status;
104 };
105
106 /*
107  * There is one mmc_blk_data per slot.
108  */
109 struct mmc_blk_data {
110         struct device   *parent;
111         struct gendisk  *disk;
112         struct mmc_queue queue;
113         struct list_head part;
114         struct list_head rpmbs;
115
116         unsigned int    flags;
117 #define MMC_BLK_CMD23   (1 << 0)        /* Can do SET_BLOCK_COUNT for multiblock */
118 #define MMC_BLK_REL_WR  (1 << 1)        /* MMC Reliable write support */
119
120         struct kref     kref;
121         unsigned int    read_only;
122         unsigned int    part_type;
123         unsigned int    reset_done;
124 #define MMC_BLK_READ            BIT(0)
125 #define MMC_BLK_WRITE           BIT(1)
126 #define MMC_BLK_DISCARD         BIT(2)
127 #define MMC_BLK_SECDISCARD      BIT(3)
128 #define MMC_BLK_CQE_RECOVERY    BIT(4)
129
130         /*
131          * Only set in main mmc_blk_data associated
132          * with mmc_card with dev_set_drvdata, and keeps
133          * track of the current selected device partition.
134          */
135         unsigned int    part_curr;
136         int     area_type;
137
138         /* debugfs files (only in main mmc_blk_data) */
139         struct dentry *status_dentry;
140         struct dentry *ext_csd_dentry;
141 };
142
143 /* Device type for RPMB character devices */
144 static dev_t mmc_rpmb_devt;
145
146 /* Bus type for RPMB character devices */
147 static struct bus_type mmc_rpmb_bus_type = {
148         .name = "mmc_rpmb",
149 };
150
151 /**
152  * struct mmc_rpmb_data - special RPMB device type for these areas
153  * @dev: the device for the RPMB area
154  * @chrdev: character device for the RPMB area
155  * @id: unique device ID number
156  * @part_index: partition index (0 on first)
157  * @md: parent MMC block device
158  * @node: list item, so we can put this device on a list
159  */
160 struct mmc_rpmb_data {
161         struct device dev;
162         struct cdev chrdev;
163         int id;
164         unsigned int part_index;
165         struct mmc_blk_data *md;
166         struct list_head node;
167 };
168
169 static DEFINE_MUTEX(open_lock);
170
171 module_param(perdev_minors, int, 0444);
172 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
173
174 static inline int mmc_blk_part_switch(struct mmc_card *card,
175                                       unsigned int part_type);
176 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
177                                struct mmc_card *card,
178                                int disable_multi,
179                                struct mmc_queue *mq);
180 static void mmc_blk_hsq_req_done(struct mmc_request *mrq);
181
182 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
183 {
184         struct mmc_blk_data *md;
185
186         mutex_lock(&open_lock);
187         md = disk->private_data;
188         if (md && !kref_get_unless_zero(&md->kref))
189                 md = NULL;
190         mutex_unlock(&open_lock);
191
192         return md;
193 }
194
195 static inline int mmc_get_devidx(struct gendisk *disk)
196 {
197         int devidx = disk->first_minor / perdev_minors;
198         return devidx;
199 }
200
201 static void mmc_blk_kref_release(struct kref *ref)
202 {
203         struct mmc_blk_data *md = container_of(ref, struct mmc_blk_data, kref);
204         int devidx;
205
206         devidx = mmc_get_devidx(md->disk);
207         ida_simple_remove(&mmc_blk_ida, devidx);
208
209         mutex_lock(&open_lock);
210         md->disk->private_data = NULL;
211         mutex_unlock(&open_lock);
212
213         put_disk(md->disk);
214         kfree(md);
215 }
216
217 static void mmc_blk_put(struct mmc_blk_data *md)
218 {
219         kref_put(&md->kref, mmc_blk_kref_release);
220 }
221
222 static ssize_t power_ro_lock_show(struct device *dev,
223                 struct device_attribute *attr, char *buf)
224 {
225         int ret;
226         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
227         struct mmc_card *card = md->queue.card;
228         int locked = 0;
229
230         if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
231                 locked = 2;
232         else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
233                 locked = 1;
234
235         ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
236
237         mmc_blk_put(md);
238
239         return ret;
240 }
241
242 static ssize_t power_ro_lock_store(struct device *dev,
243                 struct device_attribute *attr, const char *buf, size_t count)
244 {
245         int ret;
246         struct mmc_blk_data *md, *part_md;
247         struct mmc_queue *mq;
248         struct request *req;
249         unsigned long set;
250
251         if (kstrtoul(buf, 0, &set))
252                 return -EINVAL;
253
254         if (set != 1)
255                 return count;
256
257         md = mmc_blk_get(dev_to_disk(dev));
258         mq = &md->queue;
259
260         /* Dispatch locking to the block layer */
261         req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0);
262         if (IS_ERR(req)) {
263                 count = PTR_ERR(req);
264                 goto out_put;
265         }
266         req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
267         blk_execute_rq(NULL, req, 0);
268         ret = req_to_mmc_queue_req(req)->drv_op_result;
269         blk_put_request(req);
270
271         if (!ret) {
272                 pr_info("%s: Locking boot partition ro until next power on\n",
273                         md->disk->disk_name);
274                 set_disk_ro(md->disk, 1);
275
276                 list_for_each_entry(part_md, &md->part, part)
277                         if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
278                                 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
279                                 set_disk_ro(part_md->disk, 1);
280                         }
281         }
282 out_put:
283         mmc_blk_put(md);
284         return count;
285 }
286
287 static DEVICE_ATTR(ro_lock_until_next_power_on, 0,
288                 power_ro_lock_show, power_ro_lock_store);
289
290 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
291                              char *buf)
292 {
293         int ret;
294         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
295
296         ret = snprintf(buf, PAGE_SIZE, "%d\n",
297                        get_disk_ro(dev_to_disk(dev)) ^
298                        md->read_only);
299         mmc_blk_put(md);
300         return ret;
301 }
302
303 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
304                               const char *buf, size_t count)
305 {
306         int ret;
307         char *end;
308         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
309         unsigned long set = simple_strtoul(buf, &end, 0);
310         if (end == buf) {
311                 ret = -EINVAL;
312                 goto out;
313         }
314
315         set_disk_ro(dev_to_disk(dev), set || md->read_only);
316         ret = count;
317 out:
318         mmc_blk_put(md);
319         return ret;
320 }
321
322 static DEVICE_ATTR(force_ro, 0644, force_ro_show, force_ro_store);
323
324 static struct attribute *mmc_disk_attrs[] = {
325         &dev_attr_force_ro.attr,
326         &dev_attr_ro_lock_until_next_power_on.attr,
327         NULL,
328 };
329
330 static umode_t mmc_disk_attrs_is_visible(struct kobject *kobj,
331                 struct attribute *a, int n)
332 {
333         struct device *dev = container_of(kobj, struct device, kobj);
334         struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
335         umode_t mode = a->mode;
336
337         if (a == &dev_attr_ro_lock_until_next_power_on.attr &&
338             (md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
339             md->queue.card->ext_csd.boot_ro_lockable) {
340                 mode = S_IRUGO;
341                 if (!(md->queue.card->ext_csd.boot_ro_lock &
342                                 EXT_CSD_BOOT_WP_B_PWR_WP_DIS))
343                         mode |= S_IWUSR;
344         }
345
346         mmc_blk_put(md);
347         return mode;
348 }
349
350 static const struct attribute_group mmc_disk_attr_group = {
351         .is_visible     = mmc_disk_attrs_is_visible,
352         .attrs          = mmc_disk_attrs,
353 };
354
355 static const struct attribute_group *mmc_disk_attr_groups[] = {
356         &mmc_disk_attr_group,
357         NULL,
358 };
359
360 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
361 {
362         struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
363         int ret = -ENXIO;
364
365         mutex_lock(&block_mutex);
366         if (md) {
367                 ret = 0;
368                 if ((mode & FMODE_WRITE) && md->read_only) {
369                         mmc_blk_put(md);
370                         ret = -EROFS;
371                 }
372         }
373         mutex_unlock(&block_mutex);
374
375         return ret;
376 }
377
378 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
379 {
380         struct mmc_blk_data *md = disk->private_data;
381
382         mutex_lock(&block_mutex);
383         mmc_blk_put(md);
384         mutex_unlock(&block_mutex);
385 }
386
387 static int
388 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
389 {
390         geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
391         geo->heads = 4;
392         geo->sectors = 16;
393         return 0;
394 }
395
396 struct mmc_blk_ioc_data {
397         struct mmc_ioc_cmd ic;
398         unsigned char *buf;
399         u64 buf_bytes;
400         struct mmc_rpmb_data *rpmb;
401 };
402
403 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
404         struct mmc_ioc_cmd __user *user)
405 {
406         struct mmc_blk_ioc_data *idata;
407         int err;
408
409         idata = kmalloc(sizeof(*idata), GFP_KERNEL);
410         if (!idata) {
411                 err = -ENOMEM;
412                 goto out;
413         }
414
415         if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
416                 err = -EFAULT;
417                 goto idata_err;
418         }
419
420         idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
421         if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
422                 err = -EOVERFLOW;
423                 goto idata_err;
424         }
425
426         if (!idata->buf_bytes) {
427                 idata->buf = NULL;
428                 return idata;
429         }
430
431         idata->buf = memdup_user((void __user *)(unsigned long)
432                                  idata->ic.data_ptr, idata->buf_bytes);
433         if (IS_ERR(idata->buf)) {
434                 err = PTR_ERR(idata->buf);
435                 goto idata_err;
436         }
437
438         return idata;
439
440 idata_err:
441         kfree(idata);
442 out:
443         return ERR_PTR(err);
444 }
445
446 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
447                                       struct mmc_blk_ioc_data *idata)
448 {
449         struct mmc_ioc_cmd *ic = &idata->ic;
450
451         if (copy_to_user(&(ic_ptr->response), ic->response,
452                          sizeof(ic->response)))
453                 return -EFAULT;
454
455         if (!idata->ic.write_flag) {
456                 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
457                                  idata->buf, idata->buf_bytes))
458                         return -EFAULT;
459         }
460
461         return 0;
462 }
463
464 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
465                                struct mmc_blk_ioc_data *idata)
466 {
467         struct mmc_command cmd = {}, sbc = {};
468         struct mmc_data data = {};
469         struct mmc_request mrq = {};
470         struct scatterlist sg;
471         int err;
472         unsigned int target_part;
473
474         if (!card || !md || !idata)
475                 return -EINVAL;
476
477         /*
478          * The RPMB accesses comes in from the character device, so we
479          * need to target these explicitly. Else we just target the
480          * partition type for the block device the ioctl() was issued
481          * on.
482          */
483         if (idata->rpmb) {
484                 /* Support multiple RPMB partitions */
485                 target_part = idata->rpmb->part_index;
486                 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
487         } else {
488                 target_part = md->part_type;
489         }
490
491         cmd.opcode = idata->ic.opcode;
492         cmd.arg = idata->ic.arg;
493         cmd.flags = idata->ic.flags;
494
495         if (idata->buf_bytes) {
496                 data.sg = &sg;
497                 data.sg_len = 1;
498                 data.blksz = idata->ic.blksz;
499                 data.blocks = idata->ic.blocks;
500
501                 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
502
503                 if (idata->ic.write_flag)
504                         data.flags = MMC_DATA_WRITE;
505                 else
506                         data.flags = MMC_DATA_READ;
507
508                 /* data.flags must already be set before doing this. */
509                 mmc_set_data_timeout(&data, card);
510
511                 /* Allow overriding the timeout_ns for empirical tuning. */
512                 if (idata->ic.data_timeout_ns)
513                         data.timeout_ns = idata->ic.data_timeout_ns;
514
515                 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
516                         /*
517                          * Pretend this is a data transfer and rely on the
518                          * host driver to compute timeout.  When all host
519                          * drivers support cmd.cmd_timeout for R1B, this
520                          * can be changed to:
521                          *
522                          *     mrq.data = NULL;
523                          *     cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
524                          */
525                         data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
526                 }
527
528                 mrq.data = &data;
529         }
530
531         mrq.cmd = &cmd;
532
533         err = mmc_blk_part_switch(card, target_part);
534         if (err)
535                 return err;
536
537         if (idata->ic.is_acmd) {
538                 err = mmc_app_cmd(card->host, card);
539                 if (err)
540                         return err;
541         }
542
543         if (idata->rpmb) {
544                 sbc.opcode = MMC_SET_BLOCK_COUNT;
545                 /*
546                  * We don't do any blockcount validation because the max size
547                  * may be increased by a future standard. We just copy the
548                  * 'Reliable Write' bit here.
549                  */
550                 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
551                 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
552                 mrq.sbc = &sbc;
553         }
554
555         if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
556             (cmd.opcode == MMC_SWITCH))
557                 return mmc_sanitize(card, idata->ic.cmd_timeout_ms);
558
559         mmc_wait_for_req(card->host, &mrq);
560         memcpy(&idata->ic.response, cmd.resp, sizeof(cmd.resp));
561
562         if (cmd.error) {
563                 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
564                                                 __func__, cmd.error);
565                 return cmd.error;
566         }
567         if (data.error) {
568                 dev_err(mmc_dev(card->host), "%s: data error %d\n",
569                                                 __func__, data.error);
570                 return data.error;
571         }
572
573         /*
574          * Make sure the cache of the PARTITION_CONFIG register and
575          * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
576          * changed it successfully.
577          */
578         if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
579             (cmd.opcode == MMC_SWITCH)) {
580                 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
581                 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
582
583                 /*
584                  * Update cache so the next mmc_blk_part_switch call operates
585                  * on up-to-date data.
586                  */
587                 card->ext_csd.part_config = value;
588                 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
589         }
590
591         /*
592          * Make sure to update CACHE_CTRL in case it was changed. The cache
593          * will get turned back on if the card is re-initialized, e.g.
594          * suspend/resume or hw reset in recovery.
595          */
596         if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_CACHE_CTRL) &&
597             (cmd.opcode == MMC_SWITCH)) {
598                 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg) & 1;
599
600                 card->ext_csd.cache_ctrl = value;
601         }
602
603         /*
604          * According to the SD specs, some commands require a delay after
605          * issuing the command.
606          */
607         if (idata->ic.postsleep_min_us)
608                 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
609
610         if (idata->rpmb || (cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
611                 /*
612                  * Ensure RPMB/R1B command has completed by polling CMD13 "Send Status". Here we
613                  * allow to override the default timeout value if a custom timeout is specified.
614                  */
615                 err = mmc_poll_for_busy(card, idata->ic.cmd_timeout_ms ? : MMC_BLK_TIMEOUT_MS,
616                                         false, MMC_BUSY_IO);
617         }
618
619         return err;
620 }
621
622 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
623                              struct mmc_ioc_cmd __user *ic_ptr,
624                              struct mmc_rpmb_data *rpmb)
625 {
626         struct mmc_blk_ioc_data *idata;
627         struct mmc_blk_ioc_data *idatas[1];
628         struct mmc_queue *mq;
629         struct mmc_card *card;
630         int err = 0, ioc_err = 0;
631         struct request *req;
632
633         idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
634         if (IS_ERR(idata))
635                 return PTR_ERR(idata);
636         /* This will be NULL on non-RPMB ioctl():s */
637         idata->rpmb = rpmb;
638
639         card = md->queue.card;
640         if (IS_ERR(card)) {
641                 err = PTR_ERR(card);
642                 goto cmd_done;
643         }
644
645         /*
646          * Dispatch the ioctl() into the block request queue.
647          */
648         mq = &md->queue;
649         req = blk_get_request(mq->queue,
650                 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
651         if (IS_ERR(req)) {
652                 err = PTR_ERR(req);
653                 goto cmd_done;
654         }
655         idatas[0] = idata;
656         req_to_mmc_queue_req(req)->drv_op =
657                 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
658         req_to_mmc_queue_req(req)->drv_op_data = idatas;
659         req_to_mmc_queue_req(req)->ioc_count = 1;
660         blk_execute_rq(NULL, req, 0);
661         ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
662         err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
663         blk_put_request(req);
664
665 cmd_done:
666         kfree(idata->buf);
667         kfree(idata);
668         return ioc_err ? ioc_err : err;
669 }
670
671 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
672                                    struct mmc_ioc_multi_cmd __user *user,
673                                    struct mmc_rpmb_data *rpmb)
674 {
675         struct mmc_blk_ioc_data **idata = NULL;
676         struct mmc_ioc_cmd __user *cmds = user->cmds;
677         struct mmc_card *card;
678         struct mmc_queue *mq;
679         int i, err = 0, ioc_err = 0;
680         __u64 num_of_cmds;
681         struct request *req;
682
683         if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
684                            sizeof(num_of_cmds)))
685                 return -EFAULT;
686
687         if (!num_of_cmds)
688                 return 0;
689
690         if (num_of_cmds > MMC_IOC_MAX_CMDS)
691                 return -EINVAL;
692
693         idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
694         if (!idata)
695                 return -ENOMEM;
696
697         for (i = 0; i < num_of_cmds; i++) {
698                 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
699                 if (IS_ERR(idata[i])) {
700                         err = PTR_ERR(idata[i]);
701                         num_of_cmds = i;
702                         goto cmd_err;
703                 }
704                 /* This will be NULL on non-RPMB ioctl():s */
705                 idata[i]->rpmb = rpmb;
706         }
707
708         card = md->queue.card;
709         if (IS_ERR(card)) {
710                 err = PTR_ERR(card);
711                 goto cmd_err;
712         }
713
714
715         /*
716          * Dispatch the ioctl()s into the block request queue.
717          */
718         mq = &md->queue;
719         req = blk_get_request(mq->queue,
720                 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
721         if (IS_ERR(req)) {
722                 err = PTR_ERR(req);
723                 goto cmd_err;
724         }
725         req_to_mmc_queue_req(req)->drv_op =
726                 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
727         req_to_mmc_queue_req(req)->drv_op_data = idata;
728         req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
729         blk_execute_rq(NULL, req, 0);
730         ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
731
732         /* copy to user if data and response */
733         for (i = 0; i < num_of_cmds && !err; i++)
734                 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
735
736         blk_put_request(req);
737
738 cmd_err:
739         for (i = 0; i < num_of_cmds; i++) {
740                 kfree(idata[i]->buf);
741                 kfree(idata[i]);
742         }
743         kfree(idata);
744         return ioc_err ? ioc_err : err;
745 }
746
747 static int mmc_blk_check_blkdev(struct block_device *bdev)
748 {
749         /*
750          * The caller must have CAP_SYS_RAWIO, and must be calling this on the
751          * whole block device, not on a partition.  This prevents overspray
752          * between sibling partitions.
753          */
754         if (!capable(CAP_SYS_RAWIO) || bdev_is_partition(bdev))
755                 return -EPERM;
756         return 0;
757 }
758
759 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
760         unsigned int cmd, unsigned long arg)
761 {
762         struct mmc_blk_data *md;
763         int ret;
764
765         switch (cmd) {
766         case MMC_IOC_CMD:
767                 ret = mmc_blk_check_blkdev(bdev);
768                 if (ret)
769                         return ret;
770                 md = mmc_blk_get(bdev->bd_disk);
771                 if (!md)
772                         return -EINVAL;
773                 ret = mmc_blk_ioctl_cmd(md,
774                                         (struct mmc_ioc_cmd __user *)arg,
775                                         NULL);
776                 mmc_blk_put(md);
777                 return ret;
778         case MMC_IOC_MULTI_CMD:
779                 ret = mmc_blk_check_blkdev(bdev);
780                 if (ret)
781                         return ret;
782                 md = mmc_blk_get(bdev->bd_disk);
783                 if (!md)
784                         return -EINVAL;
785                 ret = mmc_blk_ioctl_multi_cmd(md,
786                                         (struct mmc_ioc_multi_cmd __user *)arg,
787                                         NULL);
788                 mmc_blk_put(md);
789                 return ret;
790         default:
791                 return -EINVAL;
792         }
793 }
794
795 #ifdef CONFIG_COMPAT
796 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
797         unsigned int cmd, unsigned long arg)
798 {
799         return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
800 }
801 #endif
802
803 static int mmc_blk_alternative_gpt_sector(struct gendisk *disk,
804                                           sector_t *sector)
805 {
806         struct mmc_blk_data *md;
807         int ret;
808
809         md = mmc_blk_get(disk);
810         if (!md)
811                 return -EINVAL;
812
813         if (md->queue.card)
814                 ret = mmc_card_alternative_gpt_sector(md->queue.card, sector);
815         else
816                 ret = -ENODEV;
817
818         mmc_blk_put(md);
819
820         return ret;
821 }
822
823 static const struct block_device_operations mmc_bdops = {
824         .open                   = mmc_blk_open,
825         .release                = mmc_blk_release,
826         .getgeo                 = mmc_blk_getgeo,
827         .owner                  = THIS_MODULE,
828         .ioctl                  = mmc_blk_ioctl,
829 #ifdef CONFIG_COMPAT
830         .compat_ioctl           = mmc_blk_compat_ioctl,
831 #endif
832         .alternative_gpt_sector = mmc_blk_alternative_gpt_sector,
833 };
834
835 static int mmc_blk_part_switch_pre(struct mmc_card *card,
836                                    unsigned int part_type)
837 {
838         int ret = 0;
839
840         if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
841                 if (card->ext_csd.cmdq_en) {
842                         ret = mmc_cmdq_disable(card);
843                         if (ret)
844                                 return ret;
845                 }
846                 mmc_retune_pause(card->host);
847         }
848
849         return ret;
850 }
851
852 static int mmc_blk_part_switch_post(struct mmc_card *card,
853                                     unsigned int part_type)
854 {
855         int ret = 0;
856
857         if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
858                 mmc_retune_unpause(card->host);
859                 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
860                         ret = mmc_cmdq_enable(card);
861         }
862
863         return ret;
864 }
865
866 static inline int mmc_blk_part_switch(struct mmc_card *card,
867                                       unsigned int part_type)
868 {
869         int ret = 0;
870         struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
871
872         if (main_md->part_curr == part_type)
873                 return 0;
874
875         if (mmc_card_mmc(card)) {
876                 u8 part_config = card->ext_csd.part_config;
877
878                 ret = mmc_blk_part_switch_pre(card, part_type);
879                 if (ret)
880                         return ret;
881
882                 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
883                 part_config |= part_type;
884
885                 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
886                                  EXT_CSD_PART_CONFIG, part_config,
887                                  card->ext_csd.part_time);
888                 if (ret) {
889                         mmc_blk_part_switch_post(card, part_type);
890                         return ret;
891                 }
892
893                 card->ext_csd.part_config = part_config;
894
895                 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
896         }
897
898         main_md->part_curr = part_type;
899         return ret;
900 }
901
902 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
903 {
904         int err;
905         u32 result;
906         __be32 *blocks;
907
908         struct mmc_request mrq = {};
909         struct mmc_command cmd = {};
910         struct mmc_data data = {};
911
912         struct scatterlist sg;
913
914         cmd.opcode = MMC_APP_CMD;
915         cmd.arg = card->rca << 16;
916         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
917
918         err = mmc_wait_for_cmd(card->host, &cmd, 0);
919         if (err)
920                 return err;
921         if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
922                 return -EIO;
923
924         memset(&cmd, 0, sizeof(struct mmc_command));
925
926         cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
927         cmd.arg = 0;
928         cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
929
930         data.blksz = 4;
931         data.blocks = 1;
932         data.flags = MMC_DATA_READ;
933         data.sg = &sg;
934         data.sg_len = 1;
935         mmc_set_data_timeout(&data, card);
936
937         mrq.cmd = &cmd;
938         mrq.data = &data;
939
940         blocks = kmalloc(4, GFP_KERNEL);
941         if (!blocks)
942                 return -ENOMEM;
943
944         sg_init_one(&sg, blocks, 4);
945
946         mmc_wait_for_req(card->host, &mrq);
947
948         result = ntohl(*blocks);
949         kfree(blocks);
950
951         if (cmd.error || data.error)
952                 return -EIO;
953
954         *written_blocks = result;
955
956         return 0;
957 }
958
959 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
960 {
961         if (host->actual_clock)
962                 return host->actual_clock / 1000;
963
964         /* Clock may be subject to a divisor, fudge it by a factor of 2. */
965         if (host->ios.clock)
966                 return host->ios.clock / 2000;
967
968         /* How can there be no clock */
969         WARN_ON_ONCE(1);
970         return 100; /* 100 kHz is minimum possible value */
971 }
972
973 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
974                                             struct mmc_data *data)
975 {
976         unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
977         unsigned int khz;
978
979         if (data->timeout_clks) {
980                 khz = mmc_blk_clock_khz(host);
981                 ms += DIV_ROUND_UP(data->timeout_clks, khz);
982         }
983
984         return ms;
985 }
986
987 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
988                          int type)
989 {
990         int err;
991
992         if (md->reset_done & type)
993                 return -EEXIST;
994
995         md->reset_done |= type;
996         err = mmc_hw_reset(host);
997         /* Ensure we switch back to the correct partition */
998         if (err) {
999                 struct mmc_blk_data *main_md =
1000                         dev_get_drvdata(&host->card->dev);
1001                 int part_err;
1002
1003                 main_md->part_curr = main_md->part_type;
1004                 part_err = mmc_blk_part_switch(host->card, md->part_type);
1005                 if (part_err) {
1006                         /*
1007                          * We have failed to get back into the correct
1008                          * partition, so we need to abort the whole request.
1009                          */
1010                         return -ENODEV;
1011                 }
1012         }
1013         return err;
1014 }
1015
1016 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1017 {
1018         md->reset_done &= ~type;
1019 }
1020
1021 /*
1022  * The non-block commands come back from the block layer after it queued it and
1023  * processed it with all other requests and then they get issued in this
1024  * function.
1025  */
1026 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1027 {
1028         struct mmc_queue_req *mq_rq;
1029         struct mmc_card *card = mq->card;
1030         struct mmc_blk_data *md = mq->blkdata;
1031         struct mmc_blk_ioc_data **idata;
1032         bool rpmb_ioctl;
1033         u8 **ext_csd;
1034         u32 status;
1035         int ret;
1036         int i;
1037
1038         mq_rq = req_to_mmc_queue_req(req);
1039         rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1040
1041         switch (mq_rq->drv_op) {
1042         case MMC_DRV_OP_IOCTL:
1043                 if (card->ext_csd.cmdq_en) {
1044                         ret = mmc_cmdq_disable(card);
1045                         if (ret)
1046                                 break;
1047                 }
1048                 fallthrough;
1049         case MMC_DRV_OP_IOCTL_RPMB:
1050                 idata = mq_rq->drv_op_data;
1051                 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1052                         ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1053                         if (ret)
1054                                 break;
1055                 }
1056                 /* Always switch back to main area after RPMB access */
1057                 if (rpmb_ioctl)
1058                         mmc_blk_part_switch(card, 0);
1059                 else if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
1060                         mmc_cmdq_enable(card);
1061                 break;
1062         case MMC_DRV_OP_BOOT_WP:
1063                 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1064                                  card->ext_csd.boot_ro_lock |
1065                                  EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1066                                  card->ext_csd.part_time);
1067                 if (ret)
1068                         pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1069                                md->disk->disk_name, ret);
1070                 else
1071                         card->ext_csd.boot_ro_lock |=
1072                                 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1073                 break;
1074         case MMC_DRV_OP_GET_CARD_STATUS:
1075                 ret = mmc_send_status(card, &status);
1076                 if (!ret)
1077                         ret = status;
1078                 break;
1079         case MMC_DRV_OP_GET_EXT_CSD:
1080                 ext_csd = mq_rq->drv_op_data;
1081                 ret = mmc_get_ext_csd(card, ext_csd);
1082                 break;
1083         default:
1084                 pr_err("%s: unknown driver specific operation\n",
1085                        md->disk->disk_name);
1086                 ret = -EINVAL;
1087                 break;
1088         }
1089         mq_rq->drv_op_result = ret;
1090         blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1091 }
1092
1093 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1094 {
1095         struct mmc_blk_data *md = mq->blkdata;
1096         struct mmc_card *card = md->queue.card;
1097         unsigned int from, nr;
1098         int err = 0, type = MMC_BLK_DISCARD;
1099         blk_status_t status = BLK_STS_OK;
1100
1101         if (!mmc_can_erase(card)) {
1102                 status = BLK_STS_NOTSUPP;
1103                 goto fail;
1104         }
1105
1106         from = blk_rq_pos(req);
1107         nr = blk_rq_sectors(req);
1108
1109         do {
1110                 err = 0;
1111                 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1112                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1113                                          INAND_CMD38_ARG_EXT_CSD,
1114                                          card->erase_arg == MMC_TRIM_ARG ?
1115                                          INAND_CMD38_ARG_TRIM :
1116                                          INAND_CMD38_ARG_ERASE,
1117                                          card->ext_csd.generic_cmd6_time);
1118                 }
1119                 if (!err)
1120                         err = mmc_erase(card, from, nr, card->erase_arg);
1121         } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1122         if (err)
1123                 status = BLK_STS_IOERR;
1124         else
1125                 mmc_blk_reset_success(md, type);
1126 fail:
1127         blk_mq_end_request(req, status);
1128 }
1129
1130 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1131                                        struct request *req)
1132 {
1133         struct mmc_blk_data *md = mq->blkdata;
1134         struct mmc_card *card = md->queue.card;
1135         unsigned int from, nr, arg;
1136         int err = 0, type = MMC_BLK_SECDISCARD;
1137         blk_status_t status = BLK_STS_OK;
1138
1139         if (!(mmc_can_secure_erase_trim(card))) {
1140                 status = BLK_STS_NOTSUPP;
1141                 goto out;
1142         }
1143
1144         from = blk_rq_pos(req);
1145         nr = blk_rq_sectors(req);
1146
1147         if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1148                 arg = MMC_SECURE_TRIM1_ARG;
1149         else
1150                 arg = MMC_SECURE_ERASE_ARG;
1151
1152 retry:
1153         if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1154                 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1155                                  INAND_CMD38_ARG_EXT_CSD,
1156                                  arg == MMC_SECURE_TRIM1_ARG ?
1157                                  INAND_CMD38_ARG_SECTRIM1 :
1158                                  INAND_CMD38_ARG_SECERASE,
1159                                  card->ext_csd.generic_cmd6_time);
1160                 if (err)
1161                         goto out_retry;
1162         }
1163
1164         err = mmc_erase(card, from, nr, arg);
1165         if (err == -EIO)
1166                 goto out_retry;
1167         if (err) {
1168                 status = BLK_STS_IOERR;
1169                 goto out;
1170         }
1171
1172         if (arg == MMC_SECURE_TRIM1_ARG) {
1173                 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1174                         err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1175                                          INAND_CMD38_ARG_EXT_CSD,
1176                                          INAND_CMD38_ARG_SECTRIM2,
1177                                          card->ext_csd.generic_cmd6_time);
1178                         if (err)
1179                                 goto out_retry;
1180                 }
1181
1182                 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1183                 if (err == -EIO)
1184                         goto out_retry;
1185                 if (err) {
1186                         status = BLK_STS_IOERR;
1187                         goto out;
1188                 }
1189         }
1190
1191 out_retry:
1192         if (err && !mmc_blk_reset(md, card->host, type))
1193                 goto retry;
1194         if (!err)
1195                 mmc_blk_reset_success(md, type);
1196 out:
1197         blk_mq_end_request(req, status);
1198 }
1199
1200 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1201 {
1202         struct mmc_blk_data *md = mq->blkdata;
1203         struct mmc_card *card = md->queue.card;
1204         int ret = 0;
1205
1206         ret = mmc_flush_cache(card->host);
1207         blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1208 }
1209
1210 /*
1211  * Reformat current write as a reliable write, supporting
1212  * both legacy and the enhanced reliable write MMC cards.
1213  * In each transfer we'll handle only as much as a single
1214  * reliable write can handle, thus finish the request in
1215  * partial completions.
1216  */
1217 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1218                                     struct mmc_card *card,
1219                                     struct request *req)
1220 {
1221         if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1222                 /* Legacy mode imposes restrictions on transfers. */
1223                 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1224                         brq->data.blocks = 1;
1225
1226                 if (brq->data.blocks > card->ext_csd.rel_sectors)
1227                         brq->data.blocks = card->ext_csd.rel_sectors;
1228                 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1229                         brq->data.blocks = 1;
1230         }
1231 }
1232
1233 #define CMD_ERRORS_EXCL_OOR                                             \
1234         (R1_ADDRESS_ERROR |     /* Misaligned address */                \
1235          R1_BLOCK_LEN_ERROR |   /* Transferred block length incorrect */\
1236          R1_WP_VIOLATION |      /* Tried to write to protected block */ \
1237          R1_CARD_ECC_FAILED |   /* Card ECC failed */                   \
1238          R1_CC_ERROR |          /* Card controller error */             \
1239          R1_ERROR)              /* General/unknown error */
1240
1241 #define CMD_ERRORS                                                      \
1242         (CMD_ERRORS_EXCL_OOR |                                          \
1243          R1_OUT_OF_RANGE)       /* Command argument out of range */     \
1244
1245 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1246 {
1247         u32 val;
1248
1249         /*
1250          * Per the SD specification(physical layer version 4.10)[1],
1251          * section 4.3.3, it explicitly states that "When the last
1252          * block of user area is read using CMD18, the host should
1253          * ignore OUT_OF_RANGE error that may occur even the sequence
1254          * is correct". And JESD84-B51 for eMMC also has a similar
1255          * statement on section 6.8.3.
1256          *
1257          * Multiple block read/write could be done by either predefined
1258          * method, namely CMD23, or open-ending mode. For open-ending mode,
1259          * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1260          *
1261          * However the spec[1] doesn't tell us whether we should also
1262          * ignore that for predefined method. But per the spec[1], section
1263          * 4.15 Set Block Count Command, it says"If illegal block count
1264          * is set, out of range error will be indicated during read/write
1265          * operation (For example, data transfer is stopped at user area
1266          * boundary)." In another word, we could expect a out of range error
1267          * in the response for the following CMD18/25. And if argument of
1268          * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1269          * we could also expect to get a -ETIMEDOUT or any error number from
1270          * the host drivers due to missing data response(for write)/data(for
1271          * read), as the cards will stop the data transfer by itself per the
1272          * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1273          */
1274
1275         if (!brq->stop.error) {
1276                 bool oor_with_open_end;
1277                 /* If there is no error yet, check R1 response */
1278
1279                 val = brq->stop.resp[0] & CMD_ERRORS;
1280                 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1281
1282                 if (val && !oor_with_open_end)
1283                         brq->stop.error = -EIO;
1284         }
1285 }
1286
1287 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1288                               int disable_multi, bool *do_rel_wr_p,
1289                               bool *do_data_tag_p)
1290 {
1291         struct mmc_blk_data *md = mq->blkdata;
1292         struct mmc_card *card = md->queue.card;
1293         struct mmc_blk_request *brq = &mqrq->brq;
1294         struct request *req = mmc_queue_req_to_req(mqrq);
1295         bool do_rel_wr, do_data_tag;
1296
1297         /*
1298          * Reliable writes are used to implement Forced Unit Access and
1299          * are supported only on MMCs.
1300          */
1301         do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1302                     rq_data_dir(req) == WRITE &&
1303                     (md->flags & MMC_BLK_REL_WR);
1304
1305         memset(brq, 0, sizeof(struct mmc_blk_request));
1306
1307         mmc_crypto_prepare_req(mqrq);
1308
1309         brq->mrq.data = &brq->data;
1310         brq->mrq.tag = req->tag;
1311
1312         brq->stop.opcode = MMC_STOP_TRANSMISSION;
1313         brq->stop.arg = 0;
1314
1315         if (rq_data_dir(req) == READ) {
1316                 brq->data.flags = MMC_DATA_READ;
1317                 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1318         } else {
1319                 brq->data.flags = MMC_DATA_WRITE;
1320                 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1321         }
1322
1323         brq->data.blksz = 512;
1324         brq->data.blocks = blk_rq_sectors(req);
1325         brq->data.blk_addr = blk_rq_pos(req);
1326
1327         /*
1328          * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1329          * The eMMC will give "high" priority tasks priority over "simple"
1330          * priority tasks. Here we always set "simple" priority by not setting
1331          * MMC_DATA_PRIO.
1332          */
1333
1334         /*
1335          * The block layer doesn't support all sector count
1336          * restrictions, so we need to be prepared for too big
1337          * requests.
1338          */
1339         if (brq->data.blocks > card->host->max_blk_count)
1340                 brq->data.blocks = card->host->max_blk_count;
1341
1342         if (brq->data.blocks > 1) {
1343                 /*
1344                  * Some SD cards in SPI mode return a CRC error or even lock up
1345                  * completely when trying to read the last block using a
1346                  * multiblock read command.
1347                  */
1348                 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1349                     (blk_rq_pos(req) + blk_rq_sectors(req) ==
1350                      get_capacity(md->disk)))
1351                         brq->data.blocks--;
1352
1353                 /*
1354                  * After a read error, we redo the request one sector
1355                  * at a time in order to accurately determine which
1356                  * sectors can be read successfully.
1357                  */
1358                 if (disable_multi)
1359                         brq->data.blocks = 1;
1360
1361                 /*
1362                  * Some controllers have HW issues while operating
1363                  * in multiple I/O mode
1364                  */
1365                 if (card->host->ops->multi_io_quirk)
1366                         brq->data.blocks = card->host->ops->multi_io_quirk(card,
1367                                                 (rq_data_dir(req) == READ) ?
1368                                                 MMC_DATA_READ : MMC_DATA_WRITE,
1369                                                 brq->data.blocks);
1370         }
1371
1372         if (do_rel_wr) {
1373                 mmc_apply_rel_rw(brq, card, req);
1374                 brq->data.flags |= MMC_DATA_REL_WR;
1375         }
1376
1377         /*
1378          * Data tag is used only during writing meta data to speed
1379          * up write and any subsequent read of this meta data
1380          */
1381         do_data_tag = card->ext_csd.data_tag_unit_size &&
1382                       (req->cmd_flags & REQ_META) &&
1383                       (rq_data_dir(req) == WRITE) &&
1384                       ((brq->data.blocks * brq->data.blksz) >=
1385                        card->ext_csd.data_tag_unit_size);
1386
1387         if (do_data_tag)
1388                 brq->data.flags |= MMC_DATA_DAT_TAG;
1389
1390         mmc_set_data_timeout(&brq->data, card);
1391
1392         brq->data.sg = mqrq->sg;
1393         brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1394
1395         /*
1396          * Adjust the sg list so it is the same size as the
1397          * request.
1398          */
1399         if (brq->data.blocks != blk_rq_sectors(req)) {
1400                 int i, data_size = brq->data.blocks << 9;
1401                 struct scatterlist *sg;
1402
1403                 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1404                         data_size -= sg->length;
1405                         if (data_size <= 0) {
1406                                 sg->length += data_size;
1407                                 i++;
1408                                 break;
1409                         }
1410                 }
1411                 brq->data.sg_len = i;
1412         }
1413
1414         if (do_rel_wr_p)
1415                 *do_rel_wr_p = do_rel_wr;
1416
1417         if (do_data_tag_p)
1418                 *do_data_tag_p = do_data_tag;
1419 }
1420
1421 #define MMC_CQE_RETRIES 2
1422
1423 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1424 {
1425         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1426         struct mmc_request *mrq = &mqrq->brq.mrq;
1427         struct request_queue *q = req->q;
1428         struct mmc_host *host = mq->card->host;
1429         enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1430         unsigned long flags;
1431         bool put_card;
1432         int err;
1433
1434         mmc_cqe_post_req(host, mrq);
1435
1436         if (mrq->cmd && mrq->cmd->error)
1437                 err = mrq->cmd->error;
1438         else if (mrq->data && mrq->data->error)
1439                 err = mrq->data->error;
1440         else
1441                 err = 0;
1442
1443         if (err) {
1444                 if (mqrq->retries++ < MMC_CQE_RETRIES)
1445                         blk_mq_requeue_request(req, true);
1446                 else
1447                         blk_mq_end_request(req, BLK_STS_IOERR);
1448         } else if (mrq->data) {
1449                 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1450                         blk_mq_requeue_request(req, true);
1451                 else
1452                         __blk_mq_end_request(req, BLK_STS_OK);
1453         } else {
1454                 blk_mq_end_request(req, BLK_STS_OK);
1455         }
1456
1457         spin_lock_irqsave(&mq->lock, flags);
1458
1459         mq->in_flight[issue_type] -= 1;
1460
1461         put_card = (mmc_tot_in_flight(mq) == 0);
1462
1463         mmc_cqe_check_busy(mq);
1464
1465         spin_unlock_irqrestore(&mq->lock, flags);
1466
1467         if (!mq->cqe_busy)
1468                 blk_mq_run_hw_queues(q, true);
1469
1470         if (put_card)
1471                 mmc_put_card(mq->card, &mq->ctx);
1472 }
1473
1474 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1475 {
1476         struct mmc_card *card = mq->card;
1477         struct mmc_host *host = card->host;
1478         int err;
1479
1480         pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1481
1482         err = mmc_cqe_recovery(host);
1483         if (err)
1484                 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1485         mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1486
1487         pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1488 }
1489
1490 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1491 {
1492         struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1493                                                   brq.mrq);
1494         struct request *req = mmc_queue_req_to_req(mqrq);
1495         struct request_queue *q = req->q;
1496         struct mmc_queue *mq = q->queuedata;
1497
1498         /*
1499          * Block layer timeouts race with completions which means the normal
1500          * completion path cannot be used during recovery.
1501          */
1502         if (mq->in_recovery)
1503                 mmc_blk_cqe_complete_rq(mq, req);
1504         else if (likely(!blk_should_fake_timeout(req->q)))
1505                 blk_mq_complete_request(req);
1506 }
1507
1508 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1509 {
1510         mrq->done               = mmc_blk_cqe_req_done;
1511         mrq->recovery_notifier  = mmc_cqe_recovery_notifier;
1512
1513         return mmc_cqe_start_req(host, mrq);
1514 }
1515
1516 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1517                                                  struct request *req)
1518 {
1519         struct mmc_blk_request *brq = &mqrq->brq;
1520
1521         memset(brq, 0, sizeof(*brq));
1522
1523         brq->mrq.cmd = &brq->cmd;
1524         brq->mrq.tag = req->tag;
1525
1526         return &brq->mrq;
1527 }
1528
1529 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1530 {
1531         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1532         struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1533
1534         mrq->cmd->opcode = MMC_SWITCH;
1535         mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1536                         (EXT_CSD_FLUSH_CACHE << 16) |
1537                         (1 << 8) |
1538                         EXT_CSD_CMD_SET_NORMAL;
1539         mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1540
1541         return mmc_blk_cqe_start_req(mq->card->host, mrq);
1542 }
1543
1544 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1545 {
1546         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1547         struct mmc_host *host = mq->card->host;
1548         int err;
1549
1550         mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1551         mqrq->brq.mrq.done = mmc_blk_hsq_req_done;
1552         mmc_pre_req(host, &mqrq->brq.mrq);
1553
1554         err = mmc_cqe_start_req(host, &mqrq->brq.mrq);
1555         if (err)
1556                 mmc_post_req(host, &mqrq->brq.mrq, err);
1557
1558         return err;
1559 }
1560
1561 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1562 {
1563         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1564         struct mmc_host *host = mq->card->host;
1565
1566         if (host->hsq_enabled)
1567                 return mmc_blk_hsq_issue_rw_rq(mq, req);
1568
1569         mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1570
1571         return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1572 }
1573
1574 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1575                                struct mmc_card *card,
1576                                int disable_multi,
1577                                struct mmc_queue *mq)
1578 {
1579         u32 readcmd, writecmd;
1580         struct mmc_blk_request *brq = &mqrq->brq;
1581         struct request *req = mmc_queue_req_to_req(mqrq);
1582         struct mmc_blk_data *md = mq->blkdata;
1583         bool do_rel_wr, do_data_tag;
1584
1585         mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1586
1587         brq->mrq.cmd = &brq->cmd;
1588
1589         brq->cmd.arg = blk_rq_pos(req);
1590         if (!mmc_card_blockaddr(card))
1591                 brq->cmd.arg <<= 9;
1592         brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1593
1594         if (brq->data.blocks > 1 || do_rel_wr) {
1595                 /* SPI multiblock writes terminate using a special
1596                  * token, not a STOP_TRANSMISSION request.
1597                  */
1598                 if (!mmc_host_is_spi(card->host) ||
1599                     rq_data_dir(req) == READ)
1600                         brq->mrq.stop = &brq->stop;
1601                 readcmd = MMC_READ_MULTIPLE_BLOCK;
1602                 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1603         } else {
1604                 brq->mrq.stop = NULL;
1605                 readcmd = MMC_READ_SINGLE_BLOCK;
1606                 writecmd = MMC_WRITE_BLOCK;
1607         }
1608         brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1609
1610         /*
1611          * Pre-defined multi-block transfers are preferable to
1612          * open ended-ones (and necessary for reliable writes).
1613          * However, it is not sufficient to just send CMD23,
1614          * and avoid the final CMD12, as on an error condition
1615          * CMD12 (stop) needs to be sent anyway. This, coupled
1616          * with Auto-CMD23 enhancements provided by some
1617          * hosts, means that the complexity of dealing
1618          * with this is best left to the host. If CMD23 is
1619          * supported by card and host, we'll fill sbc in and let
1620          * the host deal with handling it correctly. This means
1621          * that for hosts that don't expose MMC_CAP_CMD23, no
1622          * change of behavior will be observed.
1623          *
1624          * N.B: Some MMC cards experience perf degradation.
1625          * We'll avoid using CMD23-bounded multiblock writes for
1626          * these, while retaining features like reliable writes.
1627          */
1628         if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1629             (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1630              do_data_tag)) {
1631                 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1632                 brq->sbc.arg = brq->data.blocks |
1633                         (do_rel_wr ? (1 << 31) : 0) |
1634                         (do_data_tag ? (1 << 29) : 0);
1635                 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1636                 brq->mrq.sbc = &brq->sbc;
1637         }
1638 }
1639
1640 #define MMC_MAX_RETRIES         5
1641 #define MMC_DATA_RETRIES        2
1642 #define MMC_NO_RETRIES          (MMC_MAX_RETRIES + 1)
1643
1644 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1645 {
1646         struct mmc_command cmd = {
1647                 .opcode = MMC_STOP_TRANSMISSION,
1648                 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1649                 /* Some hosts wait for busy anyway, so provide a busy timeout */
1650                 .busy_timeout = timeout,
1651         };
1652
1653         return mmc_wait_for_cmd(card->host, &cmd, 5);
1654 }
1655
1656 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1657 {
1658         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1659         struct mmc_blk_request *brq = &mqrq->brq;
1660         unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1661         int err;
1662
1663         mmc_retune_hold_now(card->host);
1664
1665         mmc_blk_send_stop(card, timeout);
1666
1667         err = mmc_poll_for_busy(card, timeout, false, MMC_BUSY_IO);
1668
1669         mmc_retune_release(card->host);
1670
1671         return err;
1672 }
1673
1674 #define MMC_READ_SINGLE_RETRIES 2
1675
1676 /* Single sector read during recovery */
1677 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1678 {
1679         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1680         struct mmc_request *mrq = &mqrq->brq.mrq;
1681         struct mmc_card *card = mq->card;
1682         struct mmc_host *host = card->host;
1683         blk_status_t error = BLK_STS_OK;
1684
1685         do {
1686                 u32 status;
1687                 int err;
1688                 int retries = 0;
1689
1690                 while (retries++ <= MMC_READ_SINGLE_RETRIES) {
1691                         mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1692
1693                         mmc_wait_for_req(host, mrq);
1694
1695                         err = mmc_send_status(card, &status);
1696                         if (err)
1697                                 goto error_exit;
1698
1699                         if (!mmc_host_is_spi(host) &&
1700                             !mmc_ready_for_data(status)) {
1701                                 err = mmc_blk_fix_state(card, req);
1702                                 if (err)
1703                                         goto error_exit;
1704                         }
1705
1706                         if (!mrq->cmd->error)
1707                                 break;
1708                 }
1709
1710                 if (mrq->cmd->error ||
1711                     mrq->data->error ||
1712                     (!mmc_host_is_spi(host) &&
1713                      (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1714                         error = BLK_STS_IOERR;
1715                 else
1716                         error = BLK_STS_OK;
1717
1718         } while (blk_update_request(req, error, 512));
1719
1720         return;
1721
1722 error_exit:
1723         mrq->data->bytes_xfered = 0;
1724         blk_update_request(req, BLK_STS_IOERR, 512);
1725         /* Let it try the remaining request again */
1726         if (mqrq->retries > MMC_MAX_RETRIES - 1)
1727                 mqrq->retries = MMC_MAX_RETRIES - 1;
1728 }
1729
1730 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1731 {
1732         return !!brq->mrq.sbc;
1733 }
1734
1735 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1736 {
1737         return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1738 }
1739
1740 /*
1741  * Check for errors the host controller driver might not have seen such as
1742  * response mode errors or invalid card state.
1743  */
1744 static bool mmc_blk_status_error(struct request *req, u32 status)
1745 {
1746         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1747         struct mmc_blk_request *brq = &mqrq->brq;
1748         struct mmc_queue *mq = req->q->queuedata;
1749         u32 stop_err_bits;
1750
1751         if (mmc_host_is_spi(mq->card->host))
1752                 return false;
1753
1754         stop_err_bits = mmc_blk_stop_err_bits(brq);
1755
1756         return brq->cmd.resp[0]  & CMD_ERRORS    ||
1757                brq->stop.resp[0] & stop_err_bits ||
1758                status            & stop_err_bits ||
1759                (rq_data_dir(req) == WRITE && !mmc_ready_for_data(status));
1760 }
1761
1762 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1763 {
1764         return !brq->sbc.error && !brq->cmd.error &&
1765                !(brq->cmd.resp[0] & CMD_ERRORS);
1766 }
1767
1768 /*
1769  * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1770  * policy:
1771  * 1. A request that has transferred at least some data is considered
1772  * successful and will be requeued if there is remaining data to
1773  * transfer.
1774  * 2. Otherwise the number of retries is incremented and the request
1775  * will be requeued if there are remaining retries.
1776  * 3. Otherwise the request will be errored out.
1777  * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1778  * mqrq->retries. So there are only 4 possible actions here:
1779  *      1. do not accept the bytes_xfered value i.e. set it to zero
1780  *      2. change mqrq->retries to determine the number of retries
1781  *      3. try to reset the card
1782  *      4. read one sector at a time
1783  */
1784 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1785 {
1786         int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1787         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1788         struct mmc_blk_request *brq = &mqrq->brq;
1789         struct mmc_blk_data *md = mq->blkdata;
1790         struct mmc_card *card = mq->card;
1791         u32 status;
1792         u32 blocks;
1793         int err;
1794
1795         /*
1796          * Some errors the host driver might not have seen. Set the number of
1797          * bytes transferred to zero in that case.
1798          */
1799         err = __mmc_send_status(card, &status, 0);
1800         if (err || mmc_blk_status_error(req, status))
1801                 brq->data.bytes_xfered = 0;
1802
1803         mmc_retune_release(card->host);
1804
1805         /*
1806          * Try again to get the status. This also provides an opportunity for
1807          * re-tuning.
1808          */
1809         if (err)
1810                 err = __mmc_send_status(card, &status, 0);
1811
1812         /*
1813          * Nothing more to do after the number of bytes transferred has been
1814          * updated and there is no card.
1815          */
1816         if (err && mmc_detect_card_removed(card->host))
1817                 return;
1818
1819         /* Try to get back to "tran" state */
1820         if (!mmc_host_is_spi(mq->card->host) &&
1821             (err || !mmc_ready_for_data(status)))
1822                 err = mmc_blk_fix_state(mq->card, req);
1823
1824         /*
1825          * Special case for SD cards where the card might record the number of
1826          * blocks written.
1827          */
1828         if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1829             rq_data_dir(req) == WRITE) {
1830                 if (mmc_sd_num_wr_blocks(card, &blocks))
1831                         brq->data.bytes_xfered = 0;
1832                 else
1833                         brq->data.bytes_xfered = blocks << 9;
1834         }
1835
1836         /* Reset if the card is in a bad state */
1837         if (!mmc_host_is_spi(mq->card->host) &&
1838             err && mmc_blk_reset(md, card->host, type)) {
1839                 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1840                 mqrq->retries = MMC_NO_RETRIES;
1841                 return;
1842         }
1843
1844         /*
1845          * If anything was done, just return and if there is anything remaining
1846          * on the request it will get requeued.
1847          */
1848         if (brq->data.bytes_xfered)
1849                 return;
1850
1851         /* Reset before last retry */
1852         if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1853                 mmc_blk_reset(md, card->host, type);
1854
1855         /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1856         if (brq->sbc.error || brq->cmd.error)
1857                 return;
1858
1859         /* Reduce the remaining retries for data errors */
1860         if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1861                 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1862                 return;
1863         }
1864
1865         /* FIXME: Missing single sector read for large sector size */
1866         if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1867             brq->data.blocks > 1) {
1868                 /* Read one sector at a time */
1869                 mmc_blk_read_single(mq, req);
1870                 return;
1871         }
1872 }
1873
1874 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1875 {
1876         mmc_blk_eval_resp_error(brq);
1877
1878         return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1879                brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1880 }
1881
1882 static int mmc_spi_err_check(struct mmc_card *card)
1883 {
1884         u32 status = 0;
1885         int err;
1886
1887         /*
1888          * SPI does not have a TRAN state we have to wait on, instead the
1889          * card is ready again when it no longer holds the line LOW.
1890          * We still have to ensure two things here before we know the write
1891          * was successful:
1892          * 1. The card has not disconnected during busy and we actually read our
1893          * own pull-up, thinking it was still connected, so ensure it
1894          * still responds.
1895          * 2. Check for any error bits, in particular R1_SPI_IDLE to catch a
1896          * just reconnected card after being disconnected during busy.
1897          */
1898         err = __mmc_send_status(card, &status, 0);
1899         if (err)
1900                 return err;
1901         /* All R1 and R2 bits of SPI are errors in our case */
1902         if (status)
1903                 return -EIO;
1904         return 0;
1905 }
1906
1907 static int mmc_blk_busy_cb(void *cb_data, bool *busy)
1908 {
1909         struct mmc_blk_busy_data *data = cb_data;
1910         u32 status = 0;
1911         int err;
1912
1913         err = mmc_send_status(data->card, &status);
1914         if (err)
1915                 return err;
1916
1917         /* Accumulate response error bits. */
1918         data->status |= status;
1919
1920         *busy = !mmc_ready_for_data(status);
1921         return 0;
1922 }
1923
1924 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1925 {
1926         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1927         struct mmc_blk_busy_data cb_data;
1928         int err;
1929
1930         if (rq_data_dir(req) == READ)
1931                 return 0;
1932
1933         if (mmc_host_is_spi(card->host)) {
1934                 err = mmc_spi_err_check(card);
1935                 if (err)
1936                         mqrq->brq.data.bytes_xfered = 0;
1937                 return err;
1938         }
1939
1940         cb_data.card = card;
1941         cb_data.status = 0;
1942         err = __mmc_poll_for_busy(card, MMC_BLK_TIMEOUT_MS, &mmc_blk_busy_cb,
1943                                   &cb_data);
1944
1945         /*
1946          * Do not assume data transferred correctly if there are any error bits
1947          * set.
1948          */
1949         if (cb_data.status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1950                 mqrq->brq.data.bytes_xfered = 0;
1951                 err = err ? err : -EIO;
1952         }
1953
1954         /* Copy the exception bit so it will be seen later on */
1955         if (mmc_card_mmc(card) && cb_data.status & R1_EXCEPTION_EVENT)
1956                 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1957
1958         return err;
1959 }
1960
1961 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1962                                             struct request *req)
1963 {
1964         int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1965
1966         mmc_blk_reset_success(mq->blkdata, type);
1967 }
1968
1969 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1970 {
1971         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1972         unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1973
1974         if (nr_bytes) {
1975                 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1976                         blk_mq_requeue_request(req, true);
1977                 else
1978                         __blk_mq_end_request(req, BLK_STS_OK);
1979         } else if (!blk_rq_bytes(req)) {
1980                 __blk_mq_end_request(req, BLK_STS_IOERR);
1981         } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1982                 blk_mq_requeue_request(req, true);
1983         } else {
1984                 if (mmc_card_removed(mq->card))
1985                         req->rq_flags |= RQF_QUIET;
1986                 blk_mq_end_request(req, BLK_STS_IOERR);
1987         }
1988 }
1989
1990 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1991                                         struct mmc_queue_req *mqrq)
1992 {
1993         return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1994                (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1995                 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1996 }
1997
1998 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1999                                  struct mmc_queue_req *mqrq)
2000 {
2001         if (mmc_blk_urgent_bkops_needed(mq, mqrq))
2002                 mmc_run_bkops(mq->card);
2003 }
2004
2005 static void mmc_blk_hsq_req_done(struct mmc_request *mrq)
2006 {
2007         struct mmc_queue_req *mqrq =
2008                 container_of(mrq, struct mmc_queue_req, brq.mrq);
2009         struct request *req = mmc_queue_req_to_req(mqrq);
2010         struct request_queue *q = req->q;
2011         struct mmc_queue *mq = q->queuedata;
2012         struct mmc_host *host = mq->card->host;
2013         unsigned long flags;
2014
2015         if (mmc_blk_rq_error(&mqrq->brq) ||
2016             mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2017                 spin_lock_irqsave(&mq->lock, flags);
2018                 mq->recovery_needed = true;
2019                 mq->recovery_req = req;
2020                 spin_unlock_irqrestore(&mq->lock, flags);
2021
2022                 host->cqe_ops->cqe_recovery_start(host);
2023
2024                 schedule_work(&mq->recovery_work);
2025                 return;
2026         }
2027
2028         mmc_blk_rw_reset_success(mq, req);
2029
2030         /*
2031          * Block layer timeouts race with completions which means the normal
2032          * completion path cannot be used during recovery.
2033          */
2034         if (mq->in_recovery)
2035                 mmc_blk_cqe_complete_rq(mq, req);
2036         else if (likely(!blk_should_fake_timeout(req->q)))
2037                 blk_mq_complete_request(req);
2038 }
2039
2040 void mmc_blk_mq_complete(struct request *req)
2041 {
2042         struct mmc_queue *mq = req->q->queuedata;
2043         struct mmc_host *host = mq->card->host;
2044
2045         if (host->cqe_enabled)
2046                 mmc_blk_cqe_complete_rq(mq, req);
2047         else if (likely(!blk_should_fake_timeout(req->q)))
2048                 mmc_blk_mq_complete_rq(mq, req);
2049 }
2050
2051 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
2052                                        struct request *req)
2053 {
2054         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2055         struct mmc_host *host = mq->card->host;
2056
2057         if (mmc_blk_rq_error(&mqrq->brq) ||
2058             mmc_blk_card_busy(mq->card, req)) {
2059                 mmc_blk_mq_rw_recovery(mq, req);
2060         } else {
2061                 mmc_blk_rw_reset_success(mq, req);
2062                 mmc_retune_release(host);
2063         }
2064
2065         mmc_blk_urgent_bkops(mq, mqrq);
2066 }
2067
2068 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
2069 {
2070         unsigned long flags;
2071         bool put_card;
2072
2073         spin_lock_irqsave(&mq->lock, flags);
2074
2075         mq->in_flight[mmc_issue_type(mq, req)] -= 1;
2076
2077         put_card = (mmc_tot_in_flight(mq) == 0);
2078
2079         spin_unlock_irqrestore(&mq->lock, flags);
2080
2081         if (put_card)
2082                 mmc_put_card(mq->card, &mq->ctx);
2083 }
2084
2085 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
2086 {
2087         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2088         struct mmc_request *mrq = &mqrq->brq.mrq;
2089         struct mmc_host *host = mq->card->host;
2090
2091         mmc_post_req(host, mrq, 0);
2092
2093         /*
2094          * Block layer timeouts race with completions which means the normal
2095          * completion path cannot be used during recovery.
2096          */
2097         if (mq->in_recovery)
2098                 mmc_blk_mq_complete_rq(mq, req);
2099         else if (likely(!blk_should_fake_timeout(req->q)))
2100                 blk_mq_complete_request(req);
2101
2102         mmc_blk_mq_dec_in_flight(mq, req);
2103 }
2104
2105 void mmc_blk_mq_recovery(struct mmc_queue *mq)
2106 {
2107         struct request *req = mq->recovery_req;
2108         struct mmc_host *host = mq->card->host;
2109         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2110
2111         mq->recovery_req = NULL;
2112         mq->rw_wait = false;
2113
2114         if (mmc_blk_rq_error(&mqrq->brq)) {
2115                 mmc_retune_hold_now(host);
2116                 mmc_blk_mq_rw_recovery(mq, req);
2117         }
2118
2119         mmc_blk_urgent_bkops(mq, mqrq);
2120
2121         mmc_blk_mq_post_req(mq, req);
2122 }
2123
2124 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2125                                          struct request **prev_req)
2126 {
2127         if (mmc_host_done_complete(mq->card->host))
2128                 return;
2129
2130         mutex_lock(&mq->complete_lock);
2131
2132         if (!mq->complete_req)
2133                 goto out_unlock;
2134
2135         mmc_blk_mq_poll_completion(mq, mq->complete_req);
2136
2137         if (prev_req)
2138                 *prev_req = mq->complete_req;
2139         else
2140                 mmc_blk_mq_post_req(mq, mq->complete_req);
2141
2142         mq->complete_req = NULL;
2143
2144 out_unlock:
2145         mutex_unlock(&mq->complete_lock);
2146 }
2147
2148 void mmc_blk_mq_complete_work(struct work_struct *work)
2149 {
2150         struct mmc_queue *mq = container_of(work, struct mmc_queue,
2151                                             complete_work);
2152
2153         mmc_blk_mq_complete_prev_req(mq, NULL);
2154 }
2155
2156 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2157 {
2158         struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2159                                                   brq.mrq);
2160         struct request *req = mmc_queue_req_to_req(mqrq);
2161         struct request_queue *q = req->q;
2162         struct mmc_queue *mq = q->queuedata;
2163         struct mmc_host *host = mq->card->host;
2164         unsigned long flags;
2165
2166         if (!mmc_host_done_complete(host)) {
2167                 bool waiting;
2168
2169                 /*
2170                  * We cannot complete the request in this context, so record
2171                  * that there is a request to complete, and that a following
2172                  * request does not need to wait (although it does need to
2173                  * complete complete_req first).
2174                  */
2175                 spin_lock_irqsave(&mq->lock, flags);
2176                 mq->complete_req = req;
2177                 mq->rw_wait = false;
2178                 waiting = mq->waiting;
2179                 spin_unlock_irqrestore(&mq->lock, flags);
2180
2181                 /*
2182                  * If 'waiting' then the waiting task will complete this
2183                  * request, otherwise queue a work to do it. Note that
2184                  * complete_work may still race with the dispatch of a following
2185                  * request.
2186                  */
2187                 if (waiting)
2188                         wake_up(&mq->wait);
2189                 else
2190                         queue_work(mq->card->complete_wq, &mq->complete_work);
2191
2192                 return;
2193         }
2194
2195         /* Take the recovery path for errors or urgent background operations */
2196         if (mmc_blk_rq_error(&mqrq->brq) ||
2197             mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2198                 spin_lock_irqsave(&mq->lock, flags);
2199                 mq->recovery_needed = true;
2200                 mq->recovery_req = req;
2201                 spin_unlock_irqrestore(&mq->lock, flags);
2202                 wake_up(&mq->wait);
2203                 schedule_work(&mq->recovery_work);
2204                 return;
2205         }
2206
2207         mmc_blk_rw_reset_success(mq, req);
2208
2209         mq->rw_wait = false;
2210         wake_up(&mq->wait);
2211
2212         mmc_blk_mq_post_req(mq, req);
2213 }
2214
2215 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2216 {
2217         unsigned long flags;
2218         bool done;
2219
2220         /*
2221          * Wait while there is another request in progress, but not if recovery
2222          * is needed. Also indicate whether there is a request waiting to start.
2223          */
2224         spin_lock_irqsave(&mq->lock, flags);
2225         if (mq->recovery_needed) {
2226                 *err = -EBUSY;
2227                 done = true;
2228         } else {
2229                 done = !mq->rw_wait;
2230         }
2231         mq->waiting = !done;
2232         spin_unlock_irqrestore(&mq->lock, flags);
2233
2234         return done;
2235 }
2236
2237 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2238 {
2239         int err = 0;
2240
2241         wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2242
2243         /* Always complete the previous request if there is one */
2244         mmc_blk_mq_complete_prev_req(mq, prev_req);
2245
2246         return err;
2247 }
2248
2249 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2250                                   struct request *req)
2251 {
2252         struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2253         struct mmc_host *host = mq->card->host;
2254         struct request *prev_req = NULL;
2255         int err = 0;
2256
2257         mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2258
2259         mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2260
2261         mmc_pre_req(host, &mqrq->brq.mrq);
2262
2263         err = mmc_blk_rw_wait(mq, &prev_req);
2264         if (err)
2265                 goto out_post_req;
2266
2267         mq->rw_wait = true;
2268
2269         err = mmc_start_request(host, &mqrq->brq.mrq);
2270
2271         if (prev_req)
2272                 mmc_blk_mq_post_req(mq, prev_req);
2273
2274         if (err)
2275                 mq->rw_wait = false;
2276
2277         /* Release re-tuning here where there is no synchronization required */
2278         if (err || mmc_host_done_complete(host))
2279                 mmc_retune_release(host);
2280
2281 out_post_req:
2282         if (err)
2283                 mmc_post_req(host, &mqrq->brq.mrq, err);
2284
2285         return err;
2286 }
2287
2288 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2289 {
2290         if (host->cqe_enabled)
2291                 return host->cqe_ops->cqe_wait_for_idle(host);
2292
2293         return mmc_blk_rw_wait(mq, NULL);
2294 }
2295
2296 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2297 {
2298         struct mmc_blk_data *md = mq->blkdata;
2299         struct mmc_card *card = md->queue.card;
2300         struct mmc_host *host = card->host;
2301         int ret;
2302
2303         ret = mmc_blk_part_switch(card, md->part_type);
2304         if (ret)
2305                 return MMC_REQ_FAILED_TO_START;
2306
2307         switch (mmc_issue_type(mq, req)) {
2308         case MMC_ISSUE_SYNC:
2309                 ret = mmc_blk_wait_for_idle(mq, host);
2310                 if (ret)
2311                         return MMC_REQ_BUSY;
2312                 switch (req_op(req)) {
2313                 case REQ_OP_DRV_IN:
2314                 case REQ_OP_DRV_OUT:
2315                         mmc_blk_issue_drv_op(mq, req);
2316                         break;
2317                 case REQ_OP_DISCARD:
2318                         mmc_blk_issue_discard_rq(mq, req);
2319                         break;
2320                 case REQ_OP_SECURE_ERASE:
2321                         mmc_blk_issue_secdiscard_rq(mq, req);
2322                         break;
2323                 case REQ_OP_FLUSH:
2324                         mmc_blk_issue_flush(mq, req);
2325                         break;
2326                 default:
2327                         WARN_ON_ONCE(1);
2328                         return MMC_REQ_FAILED_TO_START;
2329                 }
2330                 return MMC_REQ_FINISHED;
2331         case MMC_ISSUE_DCMD:
2332         case MMC_ISSUE_ASYNC:
2333                 switch (req_op(req)) {
2334                 case REQ_OP_FLUSH:
2335                         if (!mmc_cache_enabled(host)) {
2336                                 blk_mq_end_request(req, BLK_STS_OK);
2337                                 return MMC_REQ_FINISHED;
2338                         }
2339                         ret = mmc_blk_cqe_issue_flush(mq, req);
2340                         break;
2341                 case REQ_OP_READ:
2342                 case REQ_OP_WRITE:
2343                         if (host->cqe_enabled)
2344                                 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2345                         else
2346                                 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2347                         break;
2348                 default:
2349                         WARN_ON_ONCE(1);
2350                         ret = -EINVAL;
2351                 }
2352                 if (!ret)
2353                         return MMC_REQ_STARTED;
2354                 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2355         default:
2356                 WARN_ON_ONCE(1);
2357                 return MMC_REQ_FAILED_TO_START;
2358         }
2359 }
2360
2361 static inline int mmc_blk_readonly(struct mmc_card *card)
2362 {
2363         return mmc_card_readonly(card) ||
2364                !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2365 }
2366
2367 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2368                                               struct device *parent,
2369                                               sector_t size,
2370                                               bool default_ro,
2371                                               const char *subname,
2372                                               int area_type,
2373                                               unsigned int part_type)
2374 {
2375         struct mmc_blk_data *md;
2376         int devidx, ret;
2377         char cap_str[10];
2378         bool cache_enabled = false;
2379         bool fua_enabled = false;
2380
2381         devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2382         if (devidx < 0) {
2383                 /*
2384                  * We get -ENOSPC because there are no more any available
2385                  * devidx. The reason may be that, either userspace haven't yet
2386                  * unmounted the partitions, which postpones mmc_blk_release()
2387                  * from being called, or the device has more partitions than
2388                  * what we support.
2389                  */
2390                 if (devidx == -ENOSPC)
2391                         dev_err(mmc_dev(card->host),
2392                                 "no more device IDs available\n");
2393
2394                 return ERR_PTR(devidx);
2395         }
2396
2397         md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2398         if (!md) {
2399                 ret = -ENOMEM;
2400                 goto out;
2401         }
2402
2403         md->area_type = area_type;
2404
2405         /*
2406          * Set the read-only status based on the supported commands
2407          * and the write protect switch.
2408          */
2409         md->read_only = mmc_blk_readonly(card);
2410
2411         md->disk = mmc_init_queue(&md->queue, card);
2412         if (IS_ERR(md->disk)) {
2413                 ret = PTR_ERR(md->disk);
2414                 goto err_kfree;
2415         }
2416
2417         INIT_LIST_HEAD(&md->part);
2418         INIT_LIST_HEAD(&md->rpmbs);
2419         kref_init(&md->kref);
2420
2421         md->queue.blkdata = md;
2422         md->part_type = part_type;
2423
2424         md->disk->major = MMC_BLOCK_MAJOR;
2425         md->disk->minors = perdev_minors;
2426         md->disk->first_minor = devidx * perdev_minors;
2427         md->disk->fops = &mmc_bdops;
2428         md->disk->private_data = md;
2429         md->parent = parent;
2430         set_disk_ro(md->disk, md->read_only || default_ro);
2431         md->disk->flags = GENHD_FL_EXT_DEVT;
2432         if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2433                 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2434                                    | GENHD_FL_SUPPRESS_PARTITION_INFO;
2435
2436         /*
2437          * As discussed on lkml, GENHD_FL_REMOVABLE should:
2438          *
2439          * - be set for removable media with permanent block devices
2440          * - be unset for removable block devices with permanent media
2441          *
2442          * Since MMC block devices clearly fall under the second
2443          * case, we do not set GENHD_FL_REMOVABLE.  Userspace
2444          * should use the block device creation/destruction hotplug
2445          * messages to tell when the card is present.
2446          */
2447
2448         snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2449                  "mmcblk%u%s", card->host->index, subname ? subname : "");
2450
2451         set_capacity(md->disk, size);
2452
2453         if (mmc_host_cmd23(card->host)) {
2454                 if ((mmc_card_mmc(card) &&
2455                      card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2456                     (mmc_card_sd(card) &&
2457                      card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2458                         md->flags |= MMC_BLK_CMD23;
2459         }
2460
2461         if (md->flags & MMC_BLK_CMD23 &&
2462             ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2463              card->ext_csd.rel_sectors)) {
2464                 md->flags |= MMC_BLK_REL_WR;
2465                 fua_enabled = true;
2466                 cache_enabled = true;
2467         }
2468         if (mmc_cache_enabled(card->host))
2469                 cache_enabled  = true;
2470
2471         blk_queue_write_cache(md->queue.queue, cache_enabled, fua_enabled);
2472
2473         string_get_size((u64)size, 512, STRING_UNITS_2,
2474                         cap_str, sizeof(cap_str));
2475         pr_info("%s: %s %s %s %s\n",
2476                 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2477                 cap_str, md->read_only ? "(ro)" : "");
2478
2479         /* used in ->open, must be set before add_disk: */
2480         if (area_type == MMC_BLK_DATA_AREA_MAIN)
2481                 dev_set_drvdata(&card->dev, md);
2482         device_add_disk(md->parent, md->disk, mmc_disk_attr_groups);
2483         return md;
2484
2485  err_kfree:
2486         kfree(md);
2487  out:
2488         ida_simple_remove(&mmc_blk_ida, devidx);
2489         return ERR_PTR(ret);
2490 }
2491
2492 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2493 {
2494         sector_t size;
2495
2496         if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2497                 /*
2498                  * The EXT_CSD sector count is in number or 512 byte
2499                  * sectors.
2500                  */
2501                 size = card->ext_csd.sectors;
2502         } else {
2503                 /*
2504                  * The CSD capacity field is in units of read_blkbits.
2505                  * set_capacity takes units of 512 bytes.
2506                  */
2507                 size = (typeof(sector_t))card->csd.capacity
2508                         << (card->csd.read_blkbits - 9);
2509         }
2510
2511         return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2512                                         MMC_BLK_DATA_AREA_MAIN, 0);
2513 }
2514
2515 static int mmc_blk_alloc_part(struct mmc_card *card,
2516                               struct mmc_blk_data *md,
2517                               unsigned int part_type,
2518                               sector_t size,
2519                               bool default_ro,
2520                               const char *subname,
2521                               int area_type)
2522 {
2523         struct mmc_blk_data *part_md;
2524
2525         part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2526                                     subname, area_type, part_type);
2527         if (IS_ERR(part_md))
2528                 return PTR_ERR(part_md);
2529         list_add(&part_md->part, &md->part);
2530
2531         return 0;
2532 }
2533
2534 /**
2535  * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2536  * @filp: the character device file
2537  * @cmd: the ioctl() command
2538  * @arg: the argument from userspace
2539  *
2540  * This will essentially just redirect the ioctl()s coming in over to
2541  * the main block device spawning the RPMB character device.
2542  */
2543 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2544                            unsigned long arg)
2545 {
2546         struct mmc_rpmb_data *rpmb = filp->private_data;
2547         int ret;
2548
2549         switch (cmd) {
2550         case MMC_IOC_CMD:
2551                 ret = mmc_blk_ioctl_cmd(rpmb->md,
2552                                         (struct mmc_ioc_cmd __user *)arg,
2553                                         rpmb);
2554                 break;
2555         case MMC_IOC_MULTI_CMD:
2556                 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2557                                         (struct mmc_ioc_multi_cmd __user *)arg,
2558                                         rpmb);
2559                 break;
2560         default:
2561                 ret = -EINVAL;
2562                 break;
2563         }
2564
2565         return ret;
2566 }
2567
2568 #ifdef CONFIG_COMPAT
2569 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2570                               unsigned long arg)
2571 {
2572         return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2573 }
2574 #endif
2575
2576 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2577 {
2578         struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2579                                                   struct mmc_rpmb_data, chrdev);
2580
2581         get_device(&rpmb->dev);
2582         filp->private_data = rpmb;
2583         mmc_blk_get(rpmb->md->disk);
2584
2585         return nonseekable_open(inode, filp);
2586 }
2587
2588 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2589 {
2590         struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2591                                                   struct mmc_rpmb_data, chrdev);
2592
2593         mmc_blk_put(rpmb->md);
2594         put_device(&rpmb->dev);
2595
2596         return 0;
2597 }
2598
2599 static const struct file_operations mmc_rpmb_fileops = {
2600         .release = mmc_rpmb_chrdev_release,
2601         .open = mmc_rpmb_chrdev_open,
2602         .owner = THIS_MODULE,
2603         .llseek = no_llseek,
2604         .unlocked_ioctl = mmc_rpmb_ioctl,
2605 #ifdef CONFIG_COMPAT
2606         .compat_ioctl = mmc_rpmb_ioctl_compat,
2607 #endif
2608 };
2609
2610 static void mmc_blk_rpmb_device_release(struct device *dev)
2611 {
2612         struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2613
2614         ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2615         kfree(rpmb);
2616 }
2617
2618 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2619                                    struct mmc_blk_data *md,
2620                                    unsigned int part_index,
2621                                    sector_t size,
2622                                    const char *subname)
2623 {
2624         int devidx, ret;
2625         char rpmb_name[DISK_NAME_LEN];
2626         char cap_str[10];
2627         struct mmc_rpmb_data *rpmb;
2628
2629         /* This creates the minor number for the RPMB char device */
2630         devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2631         if (devidx < 0)
2632                 return devidx;
2633
2634         rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2635         if (!rpmb) {
2636                 ida_simple_remove(&mmc_rpmb_ida, devidx);
2637                 return -ENOMEM;
2638         }
2639
2640         snprintf(rpmb_name, sizeof(rpmb_name),
2641                  "mmcblk%u%s", card->host->index, subname ? subname : "");
2642
2643         rpmb->id = devidx;
2644         rpmb->part_index = part_index;
2645         rpmb->dev.init_name = rpmb_name;
2646         rpmb->dev.bus = &mmc_rpmb_bus_type;
2647         rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2648         rpmb->dev.parent = &card->dev;
2649         rpmb->dev.release = mmc_blk_rpmb_device_release;
2650         device_initialize(&rpmb->dev);
2651         dev_set_drvdata(&rpmb->dev, rpmb);
2652         rpmb->md = md;
2653
2654         cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2655         rpmb->chrdev.owner = THIS_MODULE;
2656         ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2657         if (ret) {
2658                 pr_err("%s: could not add character device\n", rpmb_name);
2659                 goto out_put_device;
2660         }
2661
2662         list_add(&rpmb->node, &md->rpmbs);
2663
2664         string_get_size((u64)size, 512, STRING_UNITS_2,
2665                         cap_str, sizeof(cap_str));
2666
2667         pr_info("%s: %s %s %s, chardev (%d:%d)\n",
2668                 rpmb_name, mmc_card_id(card), mmc_card_name(card), cap_str,
2669                 MAJOR(mmc_rpmb_devt), rpmb->id);
2670
2671         return 0;
2672
2673 out_put_device:
2674         put_device(&rpmb->dev);
2675         return ret;
2676 }
2677
2678 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2679
2680 {
2681         cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2682         put_device(&rpmb->dev);
2683 }
2684
2685 /* MMC Physical partitions consist of two boot partitions and
2686  * up to four general purpose partitions.
2687  * For each partition enabled in EXT_CSD a block device will be allocatedi
2688  * to provide access to the partition.
2689  */
2690
2691 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2692 {
2693         int idx, ret;
2694
2695         if (!mmc_card_mmc(card))
2696                 return 0;
2697
2698         for (idx = 0; idx < card->nr_parts; idx++) {
2699                 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2700                         /*
2701                          * RPMB partitions does not provide block access, they
2702                          * are only accessed using ioctl():s. Thus create
2703                          * special RPMB block devices that do not have a
2704                          * backing block queue for these.
2705                          */
2706                         ret = mmc_blk_alloc_rpmb_part(card, md,
2707                                 card->part[idx].part_cfg,
2708                                 card->part[idx].size >> 9,
2709                                 card->part[idx].name);
2710                         if (ret)
2711                                 return ret;
2712                 } else if (card->part[idx].size) {
2713                         ret = mmc_blk_alloc_part(card, md,
2714                                 card->part[idx].part_cfg,
2715                                 card->part[idx].size >> 9,
2716                                 card->part[idx].force_ro,
2717                                 card->part[idx].name,
2718                                 card->part[idx].area_type);
2719                         if (ret)
2720                                 return ret;
2721                 }
2722         }
2723
2724         return 0;
2725 }
2726
2727 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2728 {
2729         /*
2730          * Flush remaining requests and free queues. It is freeing the queue
2731          * that stops new requests from being accepted.
2732          */
2733         del_gendisk(md->disk);
2734         mmc_cleanup_queue(&md->queue);
2735         mmc_blk_put(md);
2736 }
2737
2738 static void mmc_blk_remove_parts(struct mmc_card *card,
2739                                  struct mmc_blk_data *md)
2740 {
2741         struct list_head *pos, *q;
2742         struct mmc_blk_data *part_md;
2743         struct mmc_rpmb_data *rpmb;
2744
2745         /* Remove RPMB partitions */
2746         list_for_each_safe(pos, q, &md->rpmbs) {
2747                 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2748                 list_del(pos);
2749                 mmc_blk_remove_rpmb_part(rpmb);
2750         }
2751         /* Remove block partitions */
2752         list_for_each_safe(pos, q, &md->part) {
2753                 part_md = list_entry(pos, struct mmc_blk_data, part);
2754                 list_del(pos);
2755                 mmc_blk_remove_req(part_md);
2756         }
2757 }
2758
2759 #ifdef CONFIG_DEBUG_FS
2760
2761 static int mmc_dbg_card_status_get(void *data, u64 *val)
2762 {
2763         struct mmc_card *card = data;
2764         struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2765         struct mmc_queue *mq = &md->queue;
2766         struct request *req;
2767         int ret;
2768
2769         /* Ask the block layer about the card status */
2770         req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2771         if (IS_ERR(req))
2772                 return PTR_ERR(req);
2773         req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2774         blk_execute_rq(NULL, req, 0);
2775         ret = req_to_mmc_queue_req(req)->drv_op_result;
2776         if (ret >= 0) {
2777                 *val = ret;
2778                 ret = 0;
2779         }
2780         blk_put_request(req);
2781
2782         return ret;
2783 }
2784 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2785                          NULL, "%08llx\n");
2786
2787 /* That is two digits * 512 + 1 for newline */
2788 #define EXT_CSD_STR_LEN 1025
2789
2790 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2791 {
2792         struct mmc_card *card = inode->i_private;
2793         struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2794         struct mmc_queue *mq = &md->queue;
2795         struct request *req;
2796         char *buf;
2797         ssize_t n = 0;
2798         u8 *ext_csd;
2799         int err, i;
2800
2801         buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2802         if (!buf)
2803                 return -ENOMEM;
2804
2805         /* Ask the block layer for the EXT CSD */
2806         req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2807         if (IS_ERR(req)) {
2808                 err = PTR_ERR(req);
2809                 goto out_free;
2810         }
2811         req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2812         req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2813         blk_execute_rq(NULL, req, 0);
2814         err = req_to_mmc_queue_req(req)->drv_op_result;
2815         blk_put_request(req);
2816         if (err) {
2817                 pr_err("FAILED %d\n", err);
2818                 goto out_free;
2819         }
2820
2821         for (i = 0; i < 512; i++)
2822                 n += sprintf(buf + n, "%02x", ext_csd[i]);
2823         n += sprintf(buf + n, "\n");
2824
2825         if (n != EXT_CSD_STR_LEN) {
2826                 err = -EINVAL;
2827                 kfree(ext_csd);
2828                 goto out_free;
2829         }
2830
2831         filp->private_data = buf;
2832         kfree(ext_csd);
2833         return 0;
2834
2835 out_free:
2836         kfree(buf);
2837         return err;
2838 }
2839
2840 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2841                                 size_t cnt, loff_t *ppos)
2842 {
2843         char *buf = filp->private_data;
2844
2845         return simple_read_from_buffer(ubuf, cnt, ppos,
2846                                        buf, EXT_CSD_STR_LEN);
2847 }
2848
2849 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2850 {
2851         kfree(file->private_data);
2852         return 0;
2853 }
2854
2855 static const struct file_operations mmc_dbg_ext_csd_fops = {
2856         .open           = mmc_ext_csd_open,
2857         .read           = mmc_ext_csd_read,
2858         .release        = mmc_ext_csd_release,
2859         .llseek         = default_llseek,
2860 };
2861
2862 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2863 {
2864         struct dentry *root;
2865
2866         if (!card->debugfs_root)
2867                 return 0;
2868
2869         root = card->debugfs_root;
2870
2871         if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2872                 md->status_dentry =
2873                         debugfs_create_file_unsafe("status", 0400, root,
2874                                                    card,
2875                                                    &mmc_dbg_card_status_fops);
2876                 if (!md->status_dentry)
2877                         return -EIO;
2878         }
2879
2880         if (mmc_card_mmc(card)) {
2881                 md->ext_csd_dentry =
2882                         debugfs_create_file("ext_csd", S_IRUSR, root, card,
2883                                             &mmc_dbg_ext_csd_fops);
2884                 if (!md->ext_csd_dentry)
2885                         return -EIO;
2886         }
2887
2888         return 0;
2889 }
2890
2891 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2892                                    struct mmc_blk_data *md)
2893 {
2894         if (!card->debugfs_root)
2895                 return;
2896
2897         if (!IS_ERR_OR_NULL(md->status_dentry)) {
2898                 debugfs_remove(md->status_dentry);
2899                 md->status_dentry = NULL;
2900         }
2901
2902         if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2903                 debugfs_remove(md->ext_csd_dentry);
2904                 md->ext_csd_dentry = NULL;
2905         }
2906 }
2907
2908 #else
2909
2910 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2911 {
2912         return 0;
2913 }
2914
2915 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2916                                    struct mmc_blk_data *md)
2917 {
2918 }
2919
2920 #endif /* CONFIG_DEBUG_FS */
2921
2922 static int mmc_blk_probe(struct mmc_card *card)
2923 {
2924         struct mmc_blk_data *md;
2925         int ret = 0;
2926
2927         /*
2928          * Check that the card supports the command class(es) we need.
2929          */
2930         if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2931                 return -ENODEV;
2932
2933         mmc_fixup_device(card, mmc_blk_fixups);
2934
2935         card->complete_wq = alloc_workqueue("mmc_complete",
2936                                         WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2937         if (!card->complete_wq) {
2938                 pr_err("Failed to create mmc completion workqueue");
2939                 return -ENOMEM;
2940         }
2941
2942         md = mmc_blk_alloc(card);
2943         if (IS_ERR(md)) {
2944                 ret = PTR_ERR(md);
2945                 goto out_free;
2946         }
2947
2948         ret = mmc_blk_alloc_parts(card, md);
2949         if (ret)
2950                 goto out;
2951
2952         /* Add two debugfs entries */
2953         mmc_blk_add_debugfs(card, md);
2954
2955         pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2956         pm_runtime_use_autosuspend(&card->dev);
2957
2958         /*
2959          * Don't enable runtime PM for SD-combo cards here. Leave that
2960          * decision to be taken during the SDIO init sequence instead.
2961          */
2962         if (card->type != MMC_TYPE_SD_COMBO) {
2963                 pm_runtime_set_active(&card->dev);
2964                 pm_runtime_enable(&card->dev);
2965         }
2966
2967         return 0;
2968
2969 out:
2970         mmc_blk_remove_parts(card, md);
2971         mmc_blk_remove_req(md);
2972 out_free:
2973         destroy_workqueue(card->complete_wq);
2974         return ret;
2975 }
2976
2977 static void mmc_blk_remove(struct mmc_card *card)
2978 {
2979         struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2980
2981         mmc_blk_remove_debugfs(card, md);
2982         mmc_blk_remove_parts(card, md);
2983         pm_runtime_get_sync(&card->dev);
2984         if (md->part_curr != md->part_type) {
2985                 mmc_claim_host(card->host);
2986                 mmc_blk_part_switch(card, md->part_type);
2987                 mmc_release_host(card->host);
2988         }
2989         if (card->type != MMC_TYPE_SD_COMBO)
2990                 pm_runtime_disable(&card->dev);
2991         pm_runtime_put_noidle(&card->dev);
2992         mmc_blk_remove_req(md);
2993         dev_set_drvdata(&card->dev, NULL);
2994         destroy_workqueue(card->complete_wq);
2995 }
2996
2997 static int _mmc_blk_suspend(struct mmc_card *card)
2998 {
2999         struct mmc_blk_data *part_md;
3000         struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
3001
3002         if (md) {
3003                 mmc_queue_suspend(&md->queue);
3004                 list_for_each_entry(part_md, &md->part, part) {
3005                         mmc_queue_suspend(&part_md->queue);
3006                 }
3007         }
3008         return 0;
3009 }
3010
3011 static void mmc_blk_shutdown(struct mmc_card *card)
3012 {
3013         _mmc_blk_suspend(card);
3014 }
3015
3016 #ifdef CONFIG_PM_SLEEP
3017 static int mmc_blk_suspend(struct device *dev)
3018 {
3019         struct mmc_card *card = mmc_dev_to_card(dev);
3020
3021         return _mmc_blk_suspend(card);
3022 }
3023
3024 static int mmc_blk_resume(struct device *dev)
3025 {
3026         struct mmc_blk_data *part_md;
3027         struct mmc_blk_data *md = dev_get_drvdata(dev);
3028
3029         if (md) {
3030                 /*
3031                  * Resume involves the card going into idle state,
3032                  * so current partition is always the main one.
3033                  */
3034                 md->part_curr = md->part_type;
3035                 mmc_queue_resume(&md->queue);
3036                 list_for_each_entry(part_md, &md->part, part) {
3037                         mmc_queue_resume(&part_md->queue);
3038                 }
3039         }
3040         return 0;
3041 }
3042 #endif
3043
3044 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3045
3046 static struct mmc_driver mmc_driver = {
3047         .drv            = {
3048                 .name   = "mmcblk",
3049                 .pm     = &mmc_blk_pm_ops,
3050         },
3051         .probe          = mmc_blk_probe,
3052         .remove         = mmc_blk_remove,
3053         .shutdown       = mmc_blk_shutdown,
3054 };
3055
3056 static int __init mmc_blk_init(void)
3057 {
3058         int res;
3059
3060         res  = bus_register(&mmc_rpmb_bus_type);
3061         if (res < 0) {
3062                 pr_err("mmcblk: could not register RPMB bus type\n");
3063                 return res;
3064         }
3065         res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3066         if (res < 0) {
3067                 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3068                 goto out_bus_unreg;
3069         }
3070
3071         if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3072                 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3073
3074         max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3075
3076         res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3077         if (res)
3078                 goto out_chrdev_unreg;
3079
3080         res = mmc_register_driver(&mmc_driver);
3081         if (res)
3082                 goto out_blkdev_unreg;
3083
3084         return 0;
3085
3086 out_blkdev_unreg:
3087         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3088 out_chrdev_unreg:
3089         unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3090 out_bus_unreg:
3091         bus_unregister(&mmc_rpmb_bus_type);
3092         return res;
3093 }
3094
3095 static void __exit mmc_blk_exit(void)
3096 {
3097         mmc_unregister_driver(&mmc_driver);
3098         unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3099         unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3100         bus_unregister(&mmc_rpmb_bus_type);
3101 }
3102
3103 module_init(mmc_blk_init);
3104 module_exit(mmc_blk_exit);
3105
3106 MODULE_LICENSE("GPL");
3107 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
3108