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