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