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