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