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