xref: /openbmc/linux/drivers/mmc/core/block.c (revision 28efb0046512e8a13ed9f9bdf0d68d10bbfbe9cf)
1 /*
2  * Block driver for media (i.e., flash cards)
3  *
4  * Copyright 2002 Hewlett-Packard Company
5  * Copyright 2005-2008 Pierre Ossman
6  *
7  * Use consistent with the GNU GPL is permitted,
8  * provided that this copyright notice is
9  * preserved in its entirety in all copies and derived works.
10  *
11  * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12  * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13  * FITNESS FOR ANY PARTICULAR PURPOSE.
14  *
15  * Many thanks to Alessandro Rubini and Jonathan Corbet!
16  *
17  * Author:  Andrew Christian
18  *          28 May 2002
19  */
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 
24 #include <linux/kernel.h>
25 #include <linux/fs.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
34 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
37 #include <linux/pm_runtime.h>
38 #include <linux/idr.h>
39 #include <linux/debugfs.h>
40 
41 #include <linux/mmc/ioctl.h>
42 #include <linux/mmc/card.h>
43 #include <linux/mmc/host.h>
44 #include <linux/mmc/mmc.h>
45 #include <linux/mmc/sd.h>
46 
47 #include <linux/uaccess.h>
48 
49 #include "queue.h"
50 #include "block.h"
51 #include "core.h"
52 #include "card.h"
53 #include "host.h"
54 #include "bus.h"
55 #include "mmc_ops.h"
56 #include "quirks.h"
57 #include "sd_ops.h"
58 
59 MODULE_ALIAS("mmc:block");
60 #ifdef MODULE_PARAM_PREFIX
61 #undef MODULE_PARAM_PREFIX
62 #endif
63 #define MODULE_PARAM_PREFIX "mmcblk."
64 
65 #define MMC_BLK_TIMEOUT_MS  (10 * 60 * 1000)        /* 10 minute timeout */
66 #define MMC_SANITIZE_REQ_TIMEOUT 240000
67 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
68 
69 #define mmc_req_rel_wr(req)	((req->cmd_flags & REQ_FUA) && \
70 				  (rq_data_dir(req) == WRITE))
71 static DEFINE_MUTEX(block_mutex);
72 
73 /*
74  * The defaults come from config options but can be overriden by module
75  * or bootarg options.
76  */
77 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
78 
79 /*
80  * We've only got one major, so number of mmcblk devices is
81  * limited to (1 << 20) / number of minors per device.  It is also
82  * limited by the MAX_DEVICES below.
83  */
84 static int max_devices;
85 
86 #define MAX_DEVICES 256
87 
88 static DEFINE_IDA(mmc_blk_ida);
89 
90 /*
91  * There is one mmc_blk_data per slot.
92  */
93 struct mmc_blk_data {
94 	spinlock_t	lock;
95 	struct device	*parent;
96 	struct gendisk	*disk;
97 	struct mmc_queue queue;
98 	struct list_head part;
99 
100 	unsigned int	flags;
101 #define MMC_BLK_CMD23	(1 << 0)	/* Can do SET_BLOCK_COUNT for multiblock */
102 #define MMC_BLK_REL_WR	(1 << 1)	/* MMC Reliable write support */
103 
104 	unsigned int	usage;
105 	unsigned int	read_only;
106 	unsigned int	part_type;
107 	unsigned int	reset_done;
108 #define MMC_BLK_READ		BIT(0)
109 #define MMC_BLK_WRITE		BIT(1)
110 #define MMC_BLK_DISCARD		BIT(2)
111 #define MMC_BLK_SECDISCARD	BIT(3)
112 
113 	/*
114 	 * Only set in main mmc_blk_data associated
115 	 * with mmc_card with dev_set_drvdata, and keeps
116 	 * track of the current selected device partition.
117 	 */
118 	unsigned int	part_curr;
119 	struct device_attribute force_ro;
120 	struct device_attribute power_ro_lock;
121 	int	area_type;
122 };
123 
124 static DEFINE_MUTEX(open_lock);
125 
126 module_param(perdev_minors, int, 0444);
127 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
128 
129 static inline int mmc_blk_part_switch(struct mmc_card *card,
130 				      unsigned int part_type);
131 
132 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
133 {
134 	struct mmc_blk_data *md;
135 
136 	mutex_lock(&open_lock);
137 	md = disk->private_data;
138 	if (md && md->usage == 0)
139 		md = NULL;
140 	if (md)
141 		md->usage++;
142 	mutex_unlock(&open_lock);
143 
144 	return md;
145 }
146 
147 static inline int mmc_get_devidx(struct gendisk *disk)
148 {
149 	int devidx = disk->first_minor / perdev_minors;
150 	return devidx;
151 }
152 
153 static void mmc_blk_put(struct mmc_blk_data *md)
154 {
155 	mutex_lock(&open_lock);
156 	md->usage--;
157 	if (md->usage == 0) {
158 		int devidx = mmc_get_devidx(md->disk);
159 		blk_cleanup_queue(md->queue.queue);
160 		ida_simple_remove(&mmc_blk_ida, devidx);
161 		put_disk(md->disk);
162 		kfree(md);
163 	}
164 	mutex_unlock(&open_lock);
165 }
166 
167 static ssize_t power_ro_lock_show(struct device *dev,
168 		struct device_attribute *attr, char *buf)
169 {
170 	int ret;
171 	struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
172 	struct mmc_card *card = md->queue.card;
173 	int locked = 0;
174 
175 	if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
176 		locked = 2;
177 	else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
178 		locked = 1;
179 
180 	ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
181 
182 	mmc_blk_put(md);
183 
184 	return ret;
185 }
186 
187 static ssize_t power_ro_lock_store(struct device *dev,
188 		struct device_attribute *attr, const char *buf, size_t count)
189 {
190 	int ret;
191 	struct mmc_blk_data *md, *part_md;
192 	struct mmc_queue *mq;
193 	struct request *req;
194 	unsigned long set;
195 
196 	if (kstrtoul(buf, 0, &set))
197 		return -EINVAL;
198 
199 	if (set != 1)
200 		return count;
201 
202 	md = mmc_blk_get(dev_to_disk(dev));
203 	mq = &md->queue;
204 
205 	/* Dispatch locking to the block layer */
206 	req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, __GFP_RECLAIM);
207 	req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
208 	blk_execute_rq(mq->queue, NULL, req, 0);
209 	ret = req_to_mmc_queue_req(req)->drv_op_result;
210 
211 	if (!ret) {
212 		pr_info("%s: Locking boot partition ro until next power on\n",
213 			md->disk->disk_name);
214 		set_disk_ro(md->disk, 1);
215 
216 		list_for_each_entry(part_md, &md->part, part)
217 			if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
218 				pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
219 				set_disk_ro(part_md->disk, 1);
220 			}
221 	}
222 
223 	mmc_blk_put(md);
224 	return count;
225 }
226 
227 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
228 			     char *buf)
229 {
230 	int ret;
231 	struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
232 
233 	ret = snprintf(buf, PAGE_SIZE, "%d\n",
234 		       get_disk_ro(dev_to_disk(dev)) ^
235 		       md->read_only);
236 	mmc_blk_put(md);
237 	return ret;
238 }
239 
240 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
241 			      const char *buf, size_t count)
242 {
243 	int ret;
244 	char *end;
245 	struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
246 	unsigned long set = simple_strtoul(buf, &end, 0);
247 	if (end == buf) {
248 		ret = -EINVAL;
249 		goto out;
250 	}
251 
252 	set_disk_ro(dev_to_disk(dev), set || md->read_only);
253 	ret = count;
254 out:
255 	mmc_blk_put(md);
256 	return ret;
257 }
258 
259 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
260 {
261 	struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
262 	int ret = -ENXIO;
263 
264 	mutex_lock(&block_mutex);
265 	if (md) {
266 		if (md->usage == 2)
267 			check_disk_change(bdev);
268 		ret = 0;
269 
270 		if ((mode & FMODE_WRITE) && md->read_only) {
271 			mmc_blk_put(md);
272 			ret = -EROFS;
273 		}
274 	}
275 	mutex_unlock(&block_mutex);
276 
277 	return ret;
278 }
279 
280 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
281 {
282 	struct mmc_blk_data *md = disk->private_data;
283 
284 	mutex_lock(&block_mutex);
285 	mmc_blk_put(md);
286 	mutex_unlock(&block_mutex);
287 }
288 
289 static int
290 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
291 {
292 	geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
293 	geo->heads = 4;
294 	geo->sectors = 16;
295 	return 0;
296 }
297 
298 struct mmc_blk_ioc_data {
299 	struct mmc_ioc_cmd ic;
300 	unsigned char *buf;
301 	u64 buf_bytes;
302 };
303 
304 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
305 	struct mmc_ioc_cmd __user *user)
306 {
307 	struct mmc_blk_ioc_data *idata;
308 	int err;
309 
310 	idata = kmalloc(sizeof(*idata), GFP_KERNEL);
311 	if (!idata) {
312 		err = -ENOMEM;
313 		goto out;
314 	}
315 
316 	if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
317 		err = -EFAULT;
318 		goto idata_err;
319 	}
320 
321 	idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
322 	if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
323 		err = -EOVERFLOW;
324 		goto idata_err;
325 	}
326 
327 	if (!idata->buf_bytes) {
328 		idata->buf = NULL;
329 		return idata;
330 	}
331 
332 	idata->buf = kmalloc(idata->buf_bytes, GFP_KERNEL);
333 	if (!idata->buf) {
334 		err = -ENOMEM;
335 		goto idata_err;
336 	}
337 
338 	if (copy_from_user(idata->buf, (void __user *)(unsigned long)
339 					idata->ic.data_ptr, idata->buf_bytes)) {
340 		err = -EFAULT;
341 		goto copy_err;
342 	}
343 
344 	return idata;
345 
346 copy_err:
347 	kfree(idata->buf);
348 idata_err:
349 	kfree(idata);
350 out:
351 	return ERR_PTR(err);
352 }
353 
354 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
355 				      struct mmc_blk_ioc_data *idata)
356 {
357 	struct mmc_ioc_cmd *ic = &idata->ic;
358 
359 	if (copy_to_user(&(ic_ptr->response), ic->response,
360 			 sizeof(ic->response)))
361 		return -EFAULT;
362 
363 	if (!idata->ic.write_flag) {
364 		if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
365 				 idata->buf, idata->buf_bytes))
366 			return -EFAULT;
367 	}
368 
369 	return 0;
370 }
371 
372 static int ioctl_rpmb_card_status_poll(struct mmc_card *card, u32 *status,
373 				       u32 retries_max)
374 {
375 	int err;
376 	u32 retry_count = 0;
377 
378 	if (!status || !retries_max)
379 		return -EINVAL;
380 
381 	do {
382 		err = __mmc_send_status(card, status, 5);
383 		if (err)
384 			break;
385 
386 		if (!R1_STATUS(*status) &&
387 				(R1_CURRENT_STATE(*status) != R1_STATE_PRG))
388 			break; /* RPMB programming operation complete */
389 
390 		/*
391 		 * Rechedule to give the MMC device a chance to continue
392 		 * processing the previous command without being polled too
393 		 * frequently.
394 		 */
395 		usleep_range(1000, 5000);
396 	} while (++retry_count < retries_max);
397 
398 	if (retry_count == retries_max)
399 		err = -EPERM;
400 
401 	return err;
402 }
403 
404 static int ioctl_do_sanitize(struct mmc_card *card)
405 {
406 	int err;
407 
408 	if (!mmc_can_sanitize(card)) {
409 			pr_warn("%s: %s - SANITIZE is not supported\n",
410 				mmc_hostname(card->host), __func__);
411 			err = -EOPNOTSUPP;
412 			goto out;
413 	}
414 
415 	pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
416 		mmc_hostname(card->host), __func__);
417 
418 	err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
419 					EXT_CSD_SANITIZE_START, 1,
420 					MMC_SANITIZE_REQ_TIMEOUT);
421 
422 	if (err)
423 		pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
424 		       mmc_hostname(card->host), __func__, err);
425 
426 	pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
427 					     __func__);
428 out:
429 	return err;
430 }
431 
432 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
433 			       struct mmc_blk_ioc_data *idata)
434 {
435 	struct mmc_command cmd = {};
436 	struct mmc_data data = {};
437 	struct mmc_request mrq = {};
438 	struct scatterlist sg;
439 	int err;
440 	bool is_rpmb = false;
441 	u32 status = 0;
442 
443 	if (!card || !md || !idata)
444 		return -EINVAL;
445 
446 	if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
447 		is_rpmb = true;
448 
449 	cmd.opcode = idata->ic.opcode;
450 	cmd.arg = idata->ic.arg;
451 	cmd.flags = idata->ic.flags;
452 
453 	if (idata->buf_bytes) {
454 		data.sg = &sg;
455 		data.sg_len = 1;
456 		data.blksz = idata->ic.blksz;
457 		data.blocks = idata->ic.blocks;
458 
459 		sg_init_one(data.sg, idata->buf, idata->buf_bytes);
460 
461 		if (idata->ic.write_flag)
462 			data.flags = MMC_DATA_WRITE;
463 		else
464 			data.flags = MMC_DATA_READ;
465 
466 		/* data.flags must already be set before doing this. */
467 		mmc_set_data_timeout(&data, card);
468 
469 		/* Allow overriding the timeout_ns for empirical tuning. */
470 		if (idata->ic.data_timeout_ns)
471 			data.timeout_ns = idata->ic.data_timeout_ns;
472 
473 		if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
474 			/*
475 			 * Pretend this is a data transfer and rely on the
476 			 * host driver to compute timeout.  When all host
477 			 * drivers support cmd.cmd_timeout for R1B, this
478 			 * can be changed to:
479 			 *
480 			 *     mrq.data = NULL;
481 			 *     cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
482 			 */
483 			data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
484 		}
485 
486 		mrq.data = &data;
487 	}
488 
489 	mrq.cmd = &cmd;
490 
491 	err = mmc_blk_part_switch(card, md->part_type);
492 	if (err)
493 		return err;
494 
495 	if (idata->ic.is_acmd) {
496 		err = mmc_app_cmd(card->host, card);
497 		if (err)
498 			return err;
499 	}
500 
501 	if (is_rpmb) {
502 		err = mmc_set_blockcount(card, data.blocks,
503 			idata->ic.write_flag & (1 << 31));
504 		if (err)
505 			return err;
506 	}
507 
508 	if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
509 	    (cmd.opcode == MMC_SWITCH)) {
510 		err = ioctl_do_sanitize(card);
511 
512 		if (err)
513 			pr_err("%s: ioctl_do_sanitize() failed. err = %d",
514 			       __func__, err);
515 
516 		return err;
517 	}
518 
519 	mmc_wait_for_req(card->host, &mrq);
520 
521 	if (cmd.error) {
522 		dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
523 						__func__, cmd.error);
524 		return cmd.error;
525 	}
526 	if (data.error) {
527 		dev_err(mmc_dev(card->host), "%s: data error %d\n",
528 						__func__, data.error);
529 		return data.error;
530 	}
531 
532 	/*
533 	 * According to the SD specs, some commands require a delay after
534 	 * issuing the command.
535 	 */
536 	if (idata->ic.postsleep_min_us)
537 		usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
538 
539 	memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
540 
541 	if (is_rpmb) {
542 		/*
543 		 * Ensure RPMB command has completed by polling CMD13
544 		 * "Send Status".
545 		 */
546 		err = ioctl_rpmb_card_status_poll(card, &status, 5);
547 		if (err)
548 			dev_err(mmc_dev(card->host),
549 					"%s: Card Status=0x%08X, error %d\n",
550 					__func__, status, err);
551 	}
552 
553 	return err;
554 }
555 
556 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
557 			     struct mmc_ioc_cmd __user *ic_ptr)
558 {
559 	struct mmc_blk_ioc_data *idata;
560 	struct mmc_blk_ioc_data *idatas[1];
561 	struct mmc_queue *mq;
562 	struct mmc_card *card;
563 	int err = 0, ioc_err = 0;
564 	struct request *req;
565 
566 	idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
567 	if (IS_ERR(idata))
568 		return PTR_ERR(idata);
569 
570 	card = md->queue.card;
571 	if (IS_ERR(card)) {
572 		err = PTR_ERR(card);
573 		goto cmd_done;
574 	}
575 
576 	/*
577 	 * Dispatch the ioctl() into the block request queue.
578 	 */
579 	mq = &md->queue;
580 	req = blk_get_request(mq->queue,
581 		idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
582 		__GFP_RECLAIM);
583 	idatas[0] = idata;
584 	req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL;
585 	req_to_mmc_queue_req(req)->drv_op_data = idatas;
586 	req_to_mmc_queue_req(req)->ioc_count = 1;
587 	blk_execute_rq(mq->queue, NULL, req, 0);
588 	ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
589 	err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
590 	blk_put_request(req);
591 
592 cmd_done:
593 	kfree(idata->buf);
594 	kfree(idata);
595 	return ioc_err ? ioc_err : err;
596 }
597 
598 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
599 				   struct mmc_ioc_multi_cmd __user *user)
600 {
601 	struct mmc_blk_ioc_data **idata = NULL;
602 	struct mmc_ioc_cmd __user *cmds = user->cmds;
603 	struct mmc_card *card;
604 	struct mmc_queue *mq;
605 	int i, err = 0, ioc_err = 0;
606 	__u64 num_of_cmds;
607 	struct request *req;
608 
609 	if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
610 			   sizeof(num_of_cmds)))
611 		return -EFAULT;
612 
613 	if (!num_of_cmds)
614 		return 0;
615 
616 	if (num_of_cmds > MMC_IOC_MAX_CMDS)
617 		return -EINVAL;
618 
619 	idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
620 	if (!idata)
621 		return -ENOMEM;
622 
623 	for (i = 0; i < num_of_cmds; i++) {
624 		idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
625 		if (IS_ERR(idata[i])) {
626 			err = PTR_ERR(idata[i]);
627 			num_of_cmds = i;
628 			goto cmd_err;
629 		}
630 	}
631 
632 	card = md->queue.card;
633 	if (IS_ERR(card)) {
634 		err = PTR_ERR(card);
635 		goto cmd_err;
636 	}
637 
638 
639 	/*
640 	 * Dispatch the ioctl()s into the block request queue.
641 	 */
642 	mq = &md->queue;
643 	req = blk_get_request(mq->queue,
644 		idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN,
645 		__GFP_RECLAIM);
646 	req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_IOCTL;
647 	req_to_mmc_queue_req(req)->drv_op_data = idata;
648 	req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
649 	blk_execute_rq(mq->queue, NULL, req, 0);
650 	ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
651 
652 	/* copy to user if data and response */
653 	for (i = 0; i < num_of_cmds && !err; i++)
654 		err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
655 
656 	blk_put_request(req);
657 
658 cmd_err:
659 	for (i = 0; i < num_of_cmds; i++) {
660 		kfree(idata[i]->buf);
661 		kfree(idata[i]);
662 	}
663 	kfree(idata);
664 	return ioc_err ? ioc_err : err;
665 }
666 
667 static int mmc_blk_check_blkdev(struct block_device *bdev)
668 {
669 	/*
670 	 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
671 	 * whole block device, not on a partition.  This prevents overspray
672 	 * between sibling partitions.
673 	 */
674 	if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
675 		return -EPERM;
676 	return 0;
677 }
678 
679 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
680 	unsigned int cmd, unsigned long arg)
681 {
682 	struct mmc_blk_data *md;
683 	int ret;
684 
685 	switch (cmd) {
686 	case MMC_IOC_CMD:
687 		ret = mmc_blk_check_blkdev(bdev);
688 		if (ret)
689 			return ret;
690 		md = mmc_blk_get(bdev->bd_disk);
691 		if (!md)
692 			return -EINVAL;
693 		ret = mmc_blk_ioctl_cmd(md,
694 					(struct mmc_ioc_cmd __user *)arg);
695 		mmc_blk_put(md);
696 		return ret;
697 	case MMC_IOC_MULTI_CMD:
698 		ret = mmc_blk_check_blkdev(bdev);
699 		if (ret)
700 			return ret;
701 		md = mmc_blk_get(bdev->bd_disk);
702 		if (!md)
703 			return -EINVAL;
704 		ret = mmc_blk_ioctl_multi_cmd(md,
705 					(struct mmc_ioc_multi_cmd __user *)arg);
706 		mmc_blk_put(md);
707 		return ret;
708 	default:
709 		return -EINVAL;
710 	}
711 }
712 
713 #ifdef CONFIG_COMPAT
714 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
715 	unsigned int cmd, unsigned long arg)
716 {
717 	return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
718 }
719 #endif
720 
721 static const struct block_device_operations mmc_bdops = {
722 	.open			= mmc_blk_open,
723 	.release		= mmc_blk_release,
724 	.getgeo			= mmc_blk_getgeo,
725 	.owner			= THIS_MODULE,
726 	.ioctl			= mmc_blk_ioctl,
727 #ifdef CONFIG_COMPAT
728 	.compat_ioctl		= mmc_blk_compat_ioctl,
729 #endif
730 };
731 
732 static int mmc_blk_part_switch_pre(struct mmc_card *card,
733 				   unsigned int part_type)
734 {
735 	int ret = 0;
736 
737 	if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
738 		if (card->ext_csd.cmdq_en) {
739 			ret = mmc_cmdq_disable(card);
740 			if (ret)
741 				return ret;
742 		}
743 		mmc_retune_pause(card->host);
744 	}
745 
746 	return ret;
747 }
748 
749 static int mmc_blk_part_switch_post(struct mmc_card *card,
750 				    unsigned int part_type)
751 {
752 	int ret = 0;
753 
754 	if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
755 		mmc_retune_unpause(card->host);
756 		if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
757 			ret = mmc_cmdq_enable(card);
758 	}
759 
760 	return ret;
761 }
762 
763 static inline int mmc_blk_part_switch(struct mmc_card *card,
764 				      unsigned int part_type)
765 {
766 	int ret = 0;
767 	struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
768 
769 	if (main_md->part_curr == part_type)
770 		return 0;
771 
772 	if (mmc_card_mmc(card)) {
773 		u8 part_config = card->ext_csd.part_config;
774 
775 		ret = mmc_blk_part_switch_pre(card, part_type);
776 		if (ret)
777 			return ret;
778 
779 		part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
780 		part_config |= part_type;
781 
782 		ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
783 				 EXT_CSD_PART_CONFIG, part_config,
784 				 card->ext_csd.part_time);
785 		if (ret) {
786 			mmc_blk_part_switch_post(card, part_type);
787 			return ret;
788 		}
789 
790 		card->ext_csd.part_config = part_config;
791 
792 		ret = mmc_blk_part_switch_post(card, main_md->part_curr);
793 	}
794 
795 	main_md->part_curr = part_type;
796 	return ret;
797 }
798 
799 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
800 {
801 	int err;
802 	u32 result;
803 	__be32 *blocks;
804 
805 	struct mmc_request mrq = {};
806 	struct mmc_command cmd = {};
807 	struct mmc_data data = {};
808 
809 	struct scatterlist sg;
810 
811 	cmd.opcode = MMC_APP_CMD;
812 	cmd.arg = card->rca << 16;
813 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
814 
815 	err = mmc_wait_for_cmd(card->host, &cmd, 0);
816 	if (err)
817 		return err;
818 	if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
819 		return -EIO;
820 
821 	memset(&cmd, 0, sizeof(struct mmc_command));
822 
823 	cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
824 	cmd.arg = 0;
825 	cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
826 
827 	data.blksz = 4;
828 	data.blocks = 1;
829 	data.flags = MMC_DATA_READ;
830 	data.sg = &sg;
831 	data.sg_len = 1;
832 	mmc_set_data_timeout(&data, card);
833 
834 	mrq.cmd = &cmd;
835 	mrq.data = &data;
836 
837 	blocks = kmalloc(4, GFP_KERNEL);
838 	if (!blocks)
839 		return -ENOMEM;
840 
841 	sg_init_one(&sg, blocks, 4);
842 
843 	mmc_wait_for_req(card->host, &mrq);
844 
845 	result = ntohl(*blocks);
846 	kfree(blocks);
847 
848 	if (cmd.error || data.error)
849 		return -EIO;
850 
851 	*written_blocks = result;
852 
853 	return 0;
854 }
855 
856 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
857 		bool hw_busy_detect, struct request *req, bool *gen_err)
858 {
859 	unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
860 	int err = 0;
861 	u32 status;
862 
863 	do {
864 		err = __mmc_send_status(card, &status, 5);
865 		if (err) {
866 			pr_err("%s: error %d requesting status\n",
867 			       req->rq_disk->disk_name, err);
868 			return err;
869 		}
870 
871 		if (status & R1_ERROR) {
872 			pr_err("%s: %s: error sending status cmd, status %#x\n",
873 				req->rq_disk->disk_name, __func__, status);
874 			*gen_err = true;
875 		}
876 
877 		/* We may rely on the host hw to handle busy detection.*/
878 		if ((card->host->caps & MMC_CAP_WAIT_WHILE_BUSY) &&
879 			hw_busy_detect)
880 			break;
881 
882 		/*
883 		 * Timeout if the device never becomes ready for data and never
884 		 * leaves the program state.
885 		 */
886 		if (time_after(jiffies, timeout)) {
887 			pr_err("%s: Card stuck in programming state! %s %s\n",
888 				mmc_hostname(card->host),
889 				req->rq_disk->disk_name, __func__);
890 			return -ETIMEDOUT;
891 		}
892 
893 		/*
894 		 * Some cards mishandle the status bits,
895 		 * so make sure to check both the busy
896 		 * indication and the card state.
897 		 */
898 	} while (!(status & R1_READY_FOR_DATA) ||
899 		 (R1_CURRENT_STATE(status) == R1_STATE_PRG));
900 
901 	return err;
902 }
903 
904 static int send_stop(struct mmc_card *card, unsigned int timeout_ms,
905 		struct request *req, bool *gen_err, u32 *stop_status)
906 {
907 	struct mmc_host *host = card->host;
908 	struct mmc_command cmd = {};
909 	int err;
910 	bool use_r1b_resp = rq_data_dir(req) == WRITE;
911 
912 	/*
913 	 * Normally we use R1B responses for WRITE, but in cases where the host
914 	 * has specified a max_busy_timeout we need to validate it. A failure
915 	 * means we need to prevent the host from doing hw busy detection, which
916 	 * is done by converting to a R1 response instead.
917 	 */
918 	if (host->max_busy_timeout && (timeout_ms > host->max_busy_timeout))
919 		use_r1b_resp = false;
920 
921 	cmd.opcode = MMC_STOP_TRANSMISSION;
922 	if (use_r1b_resp) {
923 		cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
924 		cmd.busy_timeout = timeout_ms;
925 	} else {
926 		cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
927 	}
928 
929 	err = mmc_wait_for_cmd(host, &cmd, 5);
930 	if (err)
931 		return err;
932 
933 	*stop_status = cmd.resp[0];
934 
935 	/* No need to check card status in case of READ. */
936 	if (rq_data_dir(req) == READ)
937 		return 0;
938 
939 	if (!mmc_host_is_spi(host) &&
940 		(*stop_status & R1_ERROR)) {
941 		pr_err("%s: %s: general error sending stop command, resp %#x\n",
942 			req->rq_disk->disk_name, __func__, *stop_status);
943 		*gen_err = true;
944 	}
945 
946 	return card_busy_detect(card, timeout_ms, use_r1b_resp, req, gen_err);
947 }
948 
949 #define ERR_NOMEDIUM	3
950 #define ERR_RETRY	2
951 #define ERR_ABORT	1
952 #define ERR_CONTINUE	0
953 
954 static int mmc_blk_cmd_error(struct request *req, const char *name, int error,
955 	bool status_valid, u32 status)
956 {
957 	switch (error) {
958 	case -EILSEQ:
959 		/* response crc error, retry the r/w cmd */
960 		pr_err("%s: %s sending %s command, card status %#x\n",
961 			req->rq_disk->disk_name, "response CRC error",
962 			name, status);
963 		return ERR_RETRY;
964 
965 	case -ETIMEDOUT:
966 		pr_err("%s: %s sending %s command, card status %#x\n",
967 			req->rq_disk->disk_name, "timed out", name, status);
968 
969 		/* If the status cmd initially failed, retry the r/w cmd */
970 		if (!status_valid) {
971 			pr_err("%s: status not valid, retrying timeout\n",
972 				req->rq_disk->disk_name);
973 			return ERR_RETRY;
974 		}
975 
976 		/*
977 		 * If it was a r/w cmd crc error, or illegal command
978 		 * (eg, issued in wrong state) then retry - we should
979 		 * have corrected the state problem above.
980 		 */
981 		if (status & (R1_COM_CRC_ERROR | R1_ILLEGAL_COMMAND)) {
982 			pr_err("%s: command error, retrying timeout\n",
983 				req->rq_disk->disk_name);
984 			return ERR_RETRY;
985 		}
986 
987 		/* Otherwise abort the command */
988 		return ERR_ABORT;
989 
990 	default:
991 		/* We don't understand the error code the driver gave us */
992 		pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
993 		       req->rq_disk->disk_name, error, status);
994 		return ERR_ABORT;
995 	}
996 }
997 
998 /*
999  * Initial r/w and stop cmd error recovery.
1000  * We don't know whether the card received the r/w cmd or not, so try to
1001  * restore things back to a sane state.  Essentially, we do this as follows:
1002  * - Obtain card status.  If the first attempt to obtain card status fails,
1003  *   the status word will reflect the failed status cmd, not the failed
1004  *   r/w cmd.  If we fail to obtain card status, it suggests we can no
1005  *   longer communicate with the card.
1006  * - Check the card state.  If the card received the cmd but there was a
1007  *   transient problem with the response, it might still be in a data transfer
1008  *   mode.  Try to send it a stop command.  If this fails, we can't recover.
1009  * - If the r/w cmd failed due to a response CRC error, it was probably
1010  *   transient, so retry the cmd.
1011  * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1012  * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1013  *   illegal cmd, retry.
1014  * Otherwise we don't understand what happened, so abort.
1015  */
1016 static int mmc_blk_cmd_recovery(struct mmc_card *card, struct request *req,
1017 	struct mmc_blk_request *brq, bool *ecc_err, bool *gen_err)
1018 {
1019 	bool prev_cmd_status_valid = true;
1020 	u32 status, stop_status = 0;
1021 	int err, retry;
1022 
1023 	if (mmc_card_removed(card))
1024 		return ERR_NOMEDIUM;
1025 
1026 	/*
1027 	 * Try to get card status which indicates both the card state
1028 	 * and why there was no response.  If the first attempt fails,
1029 	 * we can't be sure the returned status is for the r/w command.
1030 	 */
1031 	for (retry = 2; retry >= 0; retry--) {
1032 		err = __mmc_send_status(card, &status, 0);
1033 		if (!err)
1034 			break;
1035 
1036 		/* Re-tune if needed */
1037 		mmc_retune_recheck(card->host);
1038 
1039 		prev_cmd_status_valid = false;
1040 		pr_err("%s: error %d sending status command, %sing\n",
1041 		       req->rq_disk->disk_name, err, retry ? "retry" : "abort");
1042 	}
1043 
1044 	/* We couldn't get a response from the card.  Give up. */
1045 	if (err) {
1046 		/* Check if the card is removed */
1047 		if (mmc_detect_card_removed(card->host))
1048 			return ERR_NOMEDIUM;
1049 		return ERR_ABORT;
1050 	}
1051 
1052 	/* Flag ECC errors */
1053 	if ((status & R1_CARD_ECC_FAILED) ||
1054 	    (brq->stop.resp[0] & R1_CARD_ECC_FAILED) ||
1055 	    (brq->cmd.resp[0] & R1_CARD_ECC_FAILED))
1056 		*ecc_err = true;
1057 
1058 	/* Flag General errors */
1059 	if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ)
1060 		if ((status & R1_ERROR) ||
1061 			(brq->stop.resp[0] & R1_ERROR)) {
1062 			pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1063 			       req->rq_disk->disk_name, __func__,
1064 			       brq->stop.resp[0], status);
1065 			*gen_err = true;
1066 		}
1067 
1068 	/*
1069 	 * Check the current card state.  If it is in some data transfer
1070 	 * mode, tell it to stop (and hopefully transition back to TRAN.)
1071 	 */
1072 	if (R1_CURRENT_STATE(status) == R1_STATE_DATA ||
1073 	    R1_CURRENT_STATE(status) == R1_STATE_RCV) {
1074 		err = send_stop(card,
1075 			DIV_ROUND_UP(brq->data.timeout_ns, 1000000),
1076 			req, gen_err, &stop_status);
1077 		if (err) {
1078 			pr_err("%s: error %d sending stop command\n",
1079 			       req->rq_disk->disk_name, err);
1080 			/*
1081 			 * If the stop cmd also timed out, the card is probably
1082 			 * not present, so abort. Other errors are bad news too.
1083 			 */
1084 			return ERR_ABORT;
1085 		}
1086 
1087 		if (stop_status & R1_CARD_ECC_FAILED)
1088 			*ecc_err = true;
1089 	}
1090 
1091 	/* Check for set block count errors */
1092 	if (brq->sbc.error)
1093 		return mmc_blk_cmd_error(req, "SET_BLOCK_COUNT", brq->sbc.error,
1094 				prev_cmd_status_valid, status);
1095 
1096 	/* Check for r/w command errors */
1097 	if (brq->cmd.error)
1098 		return mmc_blk_cmd_error(req, "r/w cmd", brq->cmd.error,
1099 				prev_cmd_status_valid, status);
1100 
1101 	/* Data errors */
1102 	if (!brq->stop.error)
1103 		return ERR_CONTINUE;
1104 
1105 	/* Now for stop errors.  These aren't fatal to the transfer. */
1106 	pr_info("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1107 	       req->rq_disk->disk_name, brq->stop.error,
1108 	       brq->cmd.resp[0], status);
1109 
1110 	/*
1111 	 * Subsitute in our own stop status as this will give the error
1112 	 * state which happened during the execution of the r/w command.
1113 	 */
1114 	if (stop_status) {
1115 		brq->stop.resp[0] = stop_status;
1116 		brq->stop.error = 0;
1117 	}
1118 	return ERR_CONTINUE;
1119 }
1120 
1121 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
1122 			 int type)
1123 {
1124 	int err;
1125 
1126 	if (md->reset_done & type)
1127 		return -EEXIST;
1128 
1129 	md->reset_done |= type;
1130 	err = mmc_hw_reset(host);
1131 	/* Ensure we switch back to the correct partition */
1132 	if (err != -EOPNOTSUPP) {
1133 		struct mmc_blk_data *main_md =
1134 			dev_get_drvdata(&host->card->dev);
1135 		int part_err;
1136 
1137 		main_md->part_curr = main_md->part_type;
1138 		part_err = mmc_blk_part_switch(host->card, md->part_type);
1139 		if (part_err) {
1140 			/*
1141 			 * We have failed to get back into the correct
1142 			 * partition, so we need to abort the whole request.
1143 			 */
1144 			return -ENODEV;
1145 		}
1146 	}
1147 	return err;
1148 }
1149 
1150 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1151 {
1152 	md->reset_done &= ~type;
1153 }
1154 
1155 int mmc_access_rpmb(struct mmc_queue *mq)
1156 {
1157 	struct mmc_blk_data *md = mq->blkdata;
1158 	/*
1159 	 * If this is a RPMB partition access, return ture
1160 	 */
1161 	if (md && md->part_type == EXT_CSD_PART_CONFIG_ACC_RPMB)
1162 		return true;
1163 
1164 	return false;
1165 }
1166 
1167 /*
1168  * The non-block commands come back from the block layer after it queued it and
1169  * processed it with all other requests and then they get issued in this
1170  * function.
1171  */
1172 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1173 {
1174 	struct mmc_queue_req *mq_rq;
1175 	struct mmc_card *card = mq->card;
1176 	struct mmc_blk_data *md = mq->blkdata;
1177 	struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
1178 	struct mmc_blk_ioc_data **idata;
1179 	u8 **ext_csd;
1180 	u32 status;
1181 	int ret;
1182 	int i;
1183 
1184 	mq_rq = req_to_mmc_queue_req(req);
1185 
1186 	switch (mq_rq->drv_op) {
1187 	case MMC_DRV_OP_IOCTL:
1188 		idata = mq_rq->drv_op_data;
1189 		for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1190 			ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1191 			if (ret)
1192 				break;
1193 		}
1194 		/* Always switch back to main area after RPMB access */
1195 		if (md->area_type & MMC_BLK_DATA_AREA_RPMB)
1196 			mmc_blk_part_switch(card, main_md->part_type);
1197 		break;
1198 	case MMC_DRV_OP_BOOT_WP:
1199 		ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1200 				 card->ext_csd.boot_ro_lock |
1201 				 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1202 				 card->ext_csd.part_time);
1203 		if (ret)
1204 			pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1205 			       md->disk->disk_name, ret);
1206 		else
1207 			card->ext_csd.boot_ro_lock |=
1208 				EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1209 		break;
1210 	case MMC_DRV_OP_GET_CARD_STATUS:
1211 		ret = mmc_send_status(card, &status);
1212 		if (!ret)
1213 			ret = status;
1214 		break;
1215 	case MMC_DRV_OP_GET_EXT_CSD:
1216 		ext_csd = mq_rq->drv_op_data;
1217 		ret = mmc_get_ext_csd(card, ext_csd);
1218 		break;
1219 	default:
1220 		pr_err("%s: unknown driver specific operation\n",
1221 		       md->disk->disk_name);
1222 		ret = -EINVAL;
1223 		break;
1224 	}
1225 	mq_rq->drv_op_result = ret;
1226 	blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1227 }
1228 
1229 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1230 {
1231 	struct mmc_blk_data *md = mq->blkdata;
1232 	struct mmc_card *card = md->queue.card;
1233 	unsigned int from, nr, arg;
1234 	int err = 0, type = MMC_BLK_DISCARD;
1235 	blk_status_t status = BLK_STS_OK;
1236 
1237 	if (!mmc_can_erase(card)) {
1238 		status = BLK_STS_NOTSUPP;
1239 		goto fail;
1240 	}
1241 
1242 	from = blk_rq_pos(req);
1243 	nr = blk_rq_sectors(req);
1244 
1245 	if (mmc_can_discard(card))
1246 		arg = MMC_DISCARD_ARG;
1247 	else if (mmc_can_trim(card))
1248 		arg = MMC_TRIM_ARG;
1249 	else
1250 		arg = MMC_ERASE_ARG;
1251 	do {
1252 		err = 0;
1253 		if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1254 			err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1255 					 INAND_CMD38_ARG_EXT_CSD,
1256 					 arg == MMC_TRIM_ARG ?
1257 					 INAND_CMD38_ARG_TRIM :
1258 					 INAND_CMD38_ARG_ERASE,
1259 					 0);
1260 		}
1261 		if (!err)
1262 			err = mmc_erase(card, from, nr, arg);
1263 	} while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1264 	if (err)
1265 		status = BLK_STS_IOERR;
1266 	else
1267 		mmc_blk_reset_success(md, type);
1268 fail:
1269 	blk_end_request(req, status, blk_rq_bytes(req));
1270 }
1271 
1272 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1273 				       struct request *req)
1274 {
1275 	struct mmc_blk_data *md = mq->blkdata;
1276 	struct mmc_card *card = md->queue.card;
1277 	unsigned int from, nr, arg;
1278 	int err = 0, type = MMC_BLK_SECDISCARD;
1279 	blk_status_t status = BLK_STS_OK;
1280 
1281 	if (!(mmc_can_secure_erase_trim(card))) {
1282 		status = BLK_STS_NOTSUPP;
1283 		goto out;
1284 	}
1285 
1286 	from = blk_rq_pos(req);
1287 	nr = blk_rq_sectors(req);
1288 
1289 	if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1290 		arg = MMC_SECURE_TRIM1_ARG;
1291 	else
1292 		arg = MMC_SECURE_ERASE_ARG;
1293 
1294 retry:
1295 	if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1296 		err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1297 				 INAND_CMD38_ARG_EXT_CSD,
1298 				 arg == MMC_SECURE_TRIM1_ARG ?
1299 				 INAND_CMD38_ARG_SECTRIM1 :
1300 				 INAND_CMD38_ARG_SECERASE,
1301 				 0);
1302 		if (err)
1303 			goto out_retry;
1304 	}
1305 
1306 	err = mmc_erase(card, from, nr, arg);
1307 	if (err == -EIO)
1308 		goto out_retry;
1309 	if (err) {
1310 		status = BLK_STS_IOERR;
1311 		goto out;
1312 	}
1313 
1314 	if (arg == MMC_SECURE_TRIM1_ARG) {
1315 		if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1316 			err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1317 					 INAND_CMD38_ARG_EXT_CSD,
1318 					 INAND_CMD38_ARG_SECTRIM2,
1319 					 0);
1320 			if (err)
1321 				goto out_retry;
1322 		}
1323 
1324 		err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1325 		if (err == -EIO)
1326 			goto out_retry;
1327 		if (err) {
1328 			status = BLK_STS_IOERR;
1329 			goto out;
1330 		}
1331 	}
1332 
1333 out_retry:
1334 	if (err && !mmc_blk_reset(md, card->host, type))
1335 		goto retry;
1336 	if (!err)
1337 		mmc_blk_reset_success(md, type);
1338 out:
1339 	blk_end_request(req, status, blk_rq_bytes(req));
1340 }
1341 
1342 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1343 {
1344 	struct mmc_blk_data *md = mq->blkdata;
1345 	struct mmc_card *card = md->queue.card;
1346 	int ret = 0;
1347 
1348 	ret = mmc_flush_cache(card);
1349 	blk_end_request_all(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1350 }
1351 
1352 /*
1353  * Reformat current write as a reliable write, supporting
1354  * both legacy and the enhanced reliable write MMC cards.
1355  * In each transfer we'll handle only as much as a single
1356  * reliable write can handle, thus finish the request in
1357  * partial completions.
1358  */
1359 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1360 				    struct mmc_card *card,
1361 				    struct request *req)
1362 {
1363 	if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1364 		/* Legacy mode imposes restrictions on transfers. */
1365 		if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1366 			brq->data.blocks = 1;
1367 
1368 		if (brq->data.blocks > card->ext_csd.rel_sectors)
1369 			brq->data.blocks = card->ext_csd.rel_sectors;
1370 		else if (brq->data.blocks < card->ext_csd.rel_sectors)
1371 			brq->data.blocks = 1;
1372 	}
1373 }
1374 
1375 #define CMD_ERRORS							\
1376 	(R1_OUT_OF_RANGE |	/* Command argument out of range */	\
1377 	 R1_ADDRESS_ERROR |	/* Misaligned address */		\
1378 	 R1_BLOCK_LEN_ERROR |	/* Transferred block length incorrect */\
1379 	 R1_WP_VIOLATION |	/* Tried to write to protected block */	\
1380 	 R1_CARD_ECC_FAILED |	/* Card ECC failed */			\
1381 	 R1_CC_ERROR |		/* Card controller error */		\
1382 	 R1_ERROR)		/* General/unknown error */
1383 
1384 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1385 {
1386 	u32 val;
1387 
1388 	/*
1389 	 * Per the SD specification(physical layer version 4.10)[1],
1390 	 * section 4.3.3, it explicitly states that "When the last
1391 	 * block of user area is read using CMD18, the host should
1392 	 * ignore OUT_OF_RANGE error that may occur even the sequence
1393 	 * is correct". And JESD84-B51 for eMMC also has a similar
1394 	 * statement on section 6.8.3.
1395 	 *
1396 	 * Multiple block read/write could be done by either predefined
1397 	 * method, namely CMD23, or open-ending mode. For open-ending mode,
1398 	 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1399 	 *
1400 	 * However the spec[1] doesn't tell us whether we should also
1401 	 * ignore that for predefined method. But per the spec[1], section
1402 	 * 4.15 Set Block Count Command, it says"If illegal block count
1403 	 * is set, out of range error will be indicated during read/write
1404 	 * operation (For example, data transfer is stopped at user area
1405 	 * boundary)." In another word, we could expect a out of range error
1406 	 * in the response for the following CMD18/25. And if argument of
1407 	 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1408 	 * we could also expect to get a -ETIMEDOUT or any error number from
1409 	 * the host drivers due to missing data response(for write)/data(for
1410 	 * read), as the cards will stop the data transfer by itself per the
1411 	 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1412 	 */
1413 
1414 	if (!brq->stop.error) {
1415 		bool oor_with_open_end;
1416 		/* If there is no error yet, check R1 response */
1417 
1418 		val = brq->stop.resp[0] & CMD_ERRORS;
1419 		oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1420 
1421 		if (val && !oor_with_open_end)
1422 			brq->stop.error = -EIO;
1423 	}
1424 }
1425 
1426 static enum mmc_blk_status mmc_blk_err_check(struct mmc_card *card,
1427 					     struct mmc_async_req *areq)
1428 {
1429 	struct mmc_queue_req *mq_mrq = container_of(areq, struct mmc_queue_req,
1430 						    areq);
1431 	struct mmc_blk_request *brq = &mq_mrq->brq;
1432 	struct request *req = mmc_queue_req_to_req(mq_mrq);
1433 	int need_retune = card->host->need_retune;
1434 	bool ecc_err = false;
1435 	bool gen_err = false;
1436 
1437 	/*
1438 	 * sbc.error indicates a problem with the set block count
1439 	 * command.  No data will have been transferred.
1440 	 *
1441 	 * cmd.error indicates a problem with the r/w command.  No
1442 	 * data will have been transferred.
1443 	 *
1444 	 * stop.error indicates a problem with the stop command.  Data
1445 	 * may have been transferred, or may still be transferring.
1446 	 */
1447 
1448 	mmc_blk_eval_resp_error(brq);
1449 
1450 	if (brq->sbc.error || brq->cmd.error ||
1451 	    brq->stop.error || brq->data.error) {
1452 		switch (mmc_blk_cmd_recovery(card, req, brq, &ecc_err, &gen_err)) {
1453 		case ERR_RETRY:
1454 			return MMC_BLK_RETRY;
1455 		case ERR_ABORT:
1456 			return MMC_BLK_ABORT;
1457 		case ERR_NOMEDIUM:
1458 			return MMC_BLK_NOMEDIUM;
1459 		case ERR_CONTINUE:
1460 			break;
1461 		}
1462 	}
1463 
1464 	/*
1465 	 * Check for errors relating to the execution of the
1466 	 * initial command - such as address errors.  No data
1467 	 * has been transferred.
1468 	 */
1469 	if (brq->cmd.resp[0] & CMD_ERRORS) {
1470 		pr_err("%s: r/w command failed, status = %#x\n",
1471 		       req->rq_disk->disk_name, brq->cmd.resp[0]);
1472 		return MMC_BLK_ABORT;
1473 	}
1474 
1475 	/*
1476 	 * Everything else is either success, or a data error of some
1477 	 * kind.  If it was a write, we may have transitioned to
1478 	 * program mode, which we have to wait for it to complete.
1479 	 */
1480 	if (!mmc_host_is_spi(card->host) && rq_data_dir(req) != READ) {
1481 		int err;
1482 
1483 		/* Check stop command response */
1484 		if (brq->stop.resp[0] & R1_ERROR) {
1485 			pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1486 			       req->rq_disk->disk_name, __func__,
1487 			       brq->stop.resp[0]);
1488 			gen_err = true;
1489 		}
1490 
1491 		err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, false, req,
1492 					&gen_err);
1493 		if (err)
1494 			return MMC_BLK_CMD_ERR;
1495 	}
1496 
1497 	/* if general error occurs, retry the write operation. */
1498 	if (gen_err) {
1499 		pr_warn("%s: retrying write for general error\n",
1500 				req->rq_disk->disk_name);
1501 		return MMC_BLK_RETRY;
1502 	}
1503 
1504 	/* Some errors (ECC) are flagged on the next commmand, so check stop, too */
1505 	if (brq->data.error || brq->stop.error) {
1506 		if (need_retune && !brq->retune_retry_done) {
1507 			pr_debug("%s: retrying because a re-tune was needed\n",
1508 				 req->rq_disk->disk_name);
1509 			brq->retune_retry_done = 1;
1510 			return MMC_BLK_RETRY;
1511 		}
1512 		pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1513 		       req->rq_disk->disk_name, brq->data.error ?: brq->stop.error,
1514 		       (unsigned)blk_rq_pos(req),
1515 		       (unsigned)blk_rq_sectors(req),
1516 		       brq->cmd.resp[0], brq->stop.resp[0]);
1517 
1518 		if (rq_data_dir(req) == READ) {
1519 			if (ecc_err)
1520 				return MMC_BLK_ECC_ERR;
1521 			return MMC_BLK_DATA_ERR;
1522 		} else {
1523 			return MMC_BLK_CMD_ERR;
1524 		}
1525 	}
1526 
1527 	if (!brq->data.bytes_xfered)
1528 		return MMC_BLK_RETRY;
1529 
1530 	if (blk_rq_bytes(req) != brq->data.bytes_xfered)
1531 		return MMC_BLK_PARTIAL;
1532 
1533 	return MMC_BLK_SUCCESS;
1534 }
1535 
1536 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1537 			      int disable_multi, bool *do_rel_wr,
1538 			      bool *do_data_tag)
1539 {
1540 	struct mmc_blk_data *md = mq->blkdata;
1541 	struct mmc_card *card = md->queue.card;
1542 	struct mmc_blk_request *brq = &mqrq->brq;
1543 	struct request *req = mmc_queue_req_to_req(mqrq);
1544 
1545 	/*
1546 	 * Reliable writes are used to implement Forced Unit Access and
1547 	 * are supported only on MMCs.
1548 	 */
1549 	*do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1550 		     rq_data_dir(req) == WRITE &&
1551 		     (md->flags & MMC_BLK_REL_WR);
1552 
1553 	memset(brq, 0, sizeof(struct mmc_blk_request));
1554 
1555 	brq->mrq.data = &brq->data;
1556 
1557 	brq->stop.opcode = MMC_STOP_TRANSMISSION;
1558 	brq->stop.arg = 0;
1559 
1560 	if (rq_data_dir(req) == READ) {
1561 		brq->data.flags = MMC_DATA_READ;
1562 		brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1563 	} else {
1564 		brq->data.flags = MMC_DATA_WRITE;
1565 		brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1566 	}
1567 
1568 	brq->data.blksz = 512;
1569 	brq->data.blocks = blk_rq_sectors(req);
1570 
1571 	/*
1572 	 * The block layer doesn't support all sector count
1573 	 * restrictions, so we need to be prepared for too big
1574 	 * requests.
1575 	 */
1576 	if (brq->data.blocks > card->host->max_blk_count)
1577 		brq->data.blocks = card->host->max_blk_count;
1578 
1579 	if (brq->data.blocks > 1) {
1580 		/*
1581 		 * After a read error, we redo the request one sector
1582 		 * at a time in order to accurately determine which
1583 		 * sectors can be read successfully.
1584 		 */
1585 		if (disable_multi)
1586 			brq->data.blocks = 1;
1587 
1588 		/*
1589 		 * Some controllers have HW issues while operating
1590 		 * in multiple I/O mode
1591 		 */
1592 		if (card->host->ops->multi_io_quirk)
1593 			brq->data.blocks = card->host->ops->multi_io_quirk(card,
1594 						(rq_data_dir(req) == READ) ?
1595 						MMC_DATA_READ : MMC_DATA_WRITE,
1596 						brq->data.blocks);
1597 	}
1598 
1599 	if (*do_rel_wr)
1600 		mmc_apply_rel_rw(brq, card, req);
1601 
1602 	/*
1603 	 * Data tag is used only during writing meta data to speed
1604 	 * up write and any subsequent read of this meta data
1605 	 */
1606 	*do_data_tag = card->ext_csd.data_tag_unit_size &&
1607 		       (req->cmd_flags & REQ_META) &&
1608 		       (rq_data_dir(req) == WRITE) &&
1609 		       ((brq->data.blocks * brq->data.blksz) >=
1610 			card->ext_csd.data_tag_unit_size);
1611 
1612 	mmc_set_data_timeout(&brq->data, card);
1613 
1614 	brq->data.sg = mqrq->sg;
1615 	brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1616 
1617 	/*
1618 	 * Adjust the sg list so it is the same size as the
1619 	 * request.
1620 	 */
1621 	if (brq->data.blocks != blk_rq_sectors(req)) {
1622 		int i, data_size = brq->data.blocks << 9;
1623 		struct scatterlist *sg;
1624 
1625 		for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1626 			data_size -= sg->length;
1627 			if (data_size <= 0) {
1628 				sg->length += data_size;
1629 				i++;
1630 				break;
1631 			}
1632 		}
1633 		brq->data.sg_len = i;
1634 	}
1635 
1636 	mqrq->areq.mrq = &brq->mrq;
1637 
1638 	mmc_queue_bounce_pre(mqrq);
1639 }
1640 
1641 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1642 			       struct mmc_card *card,
1643 			       int disable_multi,
1644 			       struct mmc_queue *mq)
1645 {
1646 	u32 readcmd, writecmd;
1647 	struct mmc_blk_request *brq = &mqrq->brq;
1648 	struct request *req = mmc_queue_req_to_req(mqrq);
1649 	struct mmc_blk_data *md = mq->blkdata;
1650 	bool do_rel_wr, do_data_tag;
1651 
1652 	mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1653 
1654 	brq->mrq.cmd = &brq->cmd;
1655 
1656 	brq->cmd.arg = blk_rq_pos(req);
1657 	if (!mmc_card_blockaddr(card))
1658 		brq->cmd.arg <<= 9;
1659 	brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1660 
1661 	if (brq->data.blocks > 1 || do_rel_wr) {
1662 		/* SPI multiblock writes terminate using a special
1663 		 * token, not a STOP_TRANSMISSION request.
1664 		 */
1665 		if (!mmc_host_is_spi(card->host) ||
1666 		    rq_data_dir(req) == READ)
1667 			brq->mrq.stop = &brq->stop;
1668 		readcmd = MMC_READ_MULTIPLE_BLOCK;
1669 		writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1670 	} else {
1671 		brq->mrq.stop = NULL;
1672 		readcmd = MMC_READ_SINGLE_BLOCK;
1673 		writecmd = MMC_WRITE_BLOCK;
1674 	}
1675 	brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1676 
1677 	/*
1678 	 * Pre-defined multi-block transfers are preferable to
1679 	 * open ended-ones (and necessary for reliable writes).
1680 	 * However, it is not sufficient to just send CMD23,
1681 	 * and avoid the final CMD12, as on an error condition
1682 	 * CMD12 (stop) needs to be sent anyway. This, coupled
1683 	 * with Auto-CMD23 enhancements provided by some
1684 	 * hosts, means that the complexity of dealing
1685 	 * with this is best left to the host. If CMD23 is
1686 	 * supported by card and host, we'll fill sbc in and let
1687 	 * the host deal with handling it correctly. This means
1688 	 * that for hosts that don't expose MMC_CAP_CMD23, no
1689 	 * change of behavior will be observed.
1690 	 *
1691 	 * N.B: Some MMC cards experience perf degradation.
1692 	 * We'll avoid using CMD23-bounded multiblock writes for
1693 	 * these, while retaining features like reliable writes.
1694 	 */
1695 	if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1696 	    (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1697 	     do_data_tag)) {
1698 		brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1699 		brq->sbc.arg = brq->data.blocks |
1700 			(do_rel_wr ? (1 << 31) : 0) |
1701 			(do_data_tag ? (1 << 29) : 0);
1702 		brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1703 		brq->mrq.sbc = &brq->sbc;
1704 	}
1705 
1706 	mqrq->areq.err_check = mmc_blk_err_check;
1707 }
1708 
1709 static bool mmc_blk_rw_cmd_err(struct mmc_blk_data *md, struct mmc_card *card,
1710 			       struct mmc_blk_request *brq, struct request *req,
1711 			       bool old_req_pending)
1712 {
1713 	bool req_pending;
1714 
1715 	/*
1716 	 * If this is an SD card and we're writing, we can first
1717 	 * mark the known good sectors as ok.
1718 	 *
1719 	 * If the card is not SD, we can still ok written sectors
1720 	 * as reported by the controller (which might be less than
1721 	 * the real number of written sectors, but never more).
1722 	 */
1723 	if (mmc_card_sd(card)) {
1724 		u32 blocks;
1725 		int err;
1726 
1727 		err = mmc_sd_num_wr_blocks(card, &blocks);
1728 		if (err)
1729 			req_pending = old_req_pending;
1730 		else
1731 			req_pending = blk_end_request(req, BLK_STS_OK, blocks << 9);
1732 	} else {
1733 		req_pending = blk_end_request(req, BLK_STS_OK, brq->data.bytes_xfered);
1734 	}
1735 	return req_pending;
1736 }
1737 
1738 static void mmc_blk_rw_cmd_abort(struct mmc_queue *mq, struct mmc_card *card,
1739 				 struct request *req,
1740 				 struct mmc_queue_req *mqrq)
1741 {
1742 	if (mmc_card_removed(card))
1743 		req->rq_flags |= RQF_QUIET;
1744 	while (blk_end_request(req, BLK_STS_IOERR, blk_rq_cur_bytes(req)));
1745 	mq->qcnt--;
1746 }
1747 
1748 /**
1749  * mmc_blk_rw_try_restart() - tries to restart the current async request
1750  * @mq: the queue with the card and host to restart
1751  * @req: a new request that want to be started after the current one
1752  */
1753 static void mmc_blk_rw_try_restart(struct mmc_queue *mq, struct request *req,
1754 				   struct mmc_queue_req *mqrq)
1755 {
1756 	if (!req)
1757 		return;
1758 
1759 	/*
1760 	 * If the card was removed, just cancel everything and return.
1761 	 */
1762 	if (mmc_card_removed(mq->card)) {
1763 		req->rq_flags |= RQF_QUIET;
1764 		blk_end_request_all(req, BLK_STS_IOERR);
1765 		mq->qcnt--; /* FIXME: just set to 0? */
1766 		return;
1767 	}
1768 	/* Else proceed and try to restart the current async request */
1769 	mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
1770 	mmc_start_areq(mq->card->host, &mqrq->areq, NULL);
1771 }
1772 
1773 static void mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *new_req)
1774 {
1775 	struct mmc_blk_data *md = mq->blkdata;
1776 	struct mmc_card *card = md->queue.card;
1777 	struct mmc_blk_request *brq;
1778 	int disable_multi = 0, retry = 0, type, retune_retry_done = 0;
1779 	enum mmc_blk_status status;
1780 	struct mmc_queue_req *mqrq_cur = NULL;
1781 	struct mmc_queue_req *mq_rq;
1782 	struct request *old_req;
1783 	struct mmc_async_req *new_areq;
1784 	struct mmc_async_req *old_areq;
1785 	bool req_pending = true;
1786 
1787 	if (new_req) {
1788 		mqrq_cur = req_to_mmc_queue_req(new_req);
1789 		mq->qcnt++;
1790 	}
1791 
1792 	if (!mq->qcnt)
1793 		return;
1794 
1795 	do {
1796 		if (new_req) {
1797 			/*
1798 			 * When 4KB native sector is enabled, only 8 blocks
1799 			 * multiple read or write is allowed
1800 			 */
1801 			if (mmc_large_sector(card) &&
1802 				!IS_ALIGNED(blk_rq_sectors(new_req), 8)) {
1803 				pr_err("%s: Transfer size is not 4KB sector size aligned\n",
1804 					new_req->rq_disk->disk_name);
1805 				mmc_blk_rw_cmd_abort(mq, card, new_req, mqrq_cur);
1806 				return;
1807 			}
1808 
1809 			mmc_blk_rw_rq_prep(mqrq_cur, card, 0, mq);
1810 			new_areq = &mqrq_cur->areq;
1811 		} else
1812 			new_areq = NULL;
1813 
1814 		old_areq = mmc_start_areq(card->host, new_areq, &status);
1815 		if (!old_areq) {
1816 			/*
1817 			 * We have just put the first request into the pipeline
1818 			 * and there is nothing more to do until it is
1819 			 * complete.
1820 			 */
1821 			return;
1822 		}
1823 
1824 		/*
1825 		 * An asynchronous request has been completed and we proceed
1826 		 * to handle the result of it.
1827 		 */
1828 		mq_rq =	container_of(old_areq, struct mmc_queue_req, areq);
1829 		brq = &mq_rq->brq;
1830 		old_req = mmc_queue_req_to_req(mq_rq);
1831 		type = rq_data_dir(old_req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1832 		mmc_queue_bounce_post(mq_rq);
1833 
1834 		switch (status) {
1835 		case MMC_BLK_SUCCESS:
1836 		case MMC_BLK_PARTIAL:
1837 			/*
1838 			 * A block was successfully transferred.
1839 			 */
1840 			mmc_blk_reset_success(md, type);
1841 
1842 			req_pending = blk_end_request(old_req, BLK_STS_OK,
1843 						      brq->data.bytes_xfered);
1844 			/*
1845 			 * If the blk_end_request function returns non-zero even
1846 			 * though all data has been transferred and no errors
1847 			 * were returned by the host controller, it's a bug.
1848 			 */
1849 			if (status == MMC_BLK_SUCCESS && req_pending) {
1850 				pr_err("%s BUG rq_tot %d d_xfer %d\n",
1851 				       __func__, blk_rq_bytes(old_req),
1852 				       brq->data.bytes_xfered);
1853 				mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1854 				return;
1855 			}
1856 			break;
1857 		case MMC_BLK_CMD_ERR:
1858 			req_pending = mmc_blk_rw_cmd_err(md, card, brq, old_req, req_pending);
1859 			if (mmc_blk_reset(md, card->host, type)) {
1860 				if (req_pending)
1861 					mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1862 				else
1863 					mq->qcnt--;
1864 				mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1865 				return;
1866 			}
1867 			if (!req_pending) {
1868 				mq->qcnt--;
1869 				mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1870 				return;
1871 			}
1872 			break;
1873 		case MMC_BLK_RETRY:
1874 			retune_retry_done = brq->retune_retry_done;
1875 			if (retry++ < 5)
1876 				break;
1877 			/* Fall through */
1878 		case MMC_BLK_ABORT:
1879 			if (!mmc_blk_reset(md, card->host, type))
1880 				break;
1881 			mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1882 			mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1883 			return;
1884 		case MMC_BLK_DATA_ERR: {
1885 			int err;
1886 
1887 			err = mmc_blk_reset(md, card->host, type);
1888 			if (!err)
1889 				break;
1890 			if (err == -ENODEV) {
1891 				mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1892 				mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1893 				return;
1894 			}
1895 			/* Fall through */
1896 		}
1897 		case MMC_BLK_ECC_ERR:
1898 			if (brq->data.blocks > 1) {
1899 				/* Redo read one sector at a time */
1900 				pr_warn("%s: retrying using single block read\n",
1901 					old_req->rq_disk->disk_name);
1902 				disable_multi = 1;
1903 				break;
1904 			}
1905 			/*
1906 			 * After an error, we redo I/O one sector at a
1907 			 * time, so we only reach here after trying to
1908 			 * read a single sector.
1909 			 */
1910 			req_pending = blk_end_request(old_req, BLK_STS_IOERR,
1911 						      brq->data.blksz);
1912 			if (!req_pending) {
1913 				mq->qcnt--;
1914 				mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1915 				return;
1916 			}
1917 			break;
1918 		case MMC_BLK_NOMEDIUM:
1919 			mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1920 			mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1921 			return;
1922 		default:
1923 			pr_err("%s: Unhandled return value (%d)",
1924 					old_req->rq_disk->disk_name, status);
1925 			mmc_blk_rw_cmd_abort(mq, card, old_req, mq_rq);
1926 			mmc_blk_rw_try_restart(mq, new_req, mqrq_cur);
1927 			return;
1928 		}
1929 
1930 		if (req_pending) {
1931 			/*
1932 			 * In case of a incomplete request
1933 			 * prepare it again and resend.
1934 			 */
1935 			mmc_blk_rw_rq_prep(mq_rq, card,
1936 					disable_multi, mq);
1937 			mmc_start_areq(card->host,
1938 					&mq_rq->areq, NULL);
1939 			mq_rq->brq.retune_retry_done = retune_retry_done;
1940 		}
1941 	} while (req_pending);
1942 
1943 	mq->qcnt--;
1944 }
1945 
1946 void mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
1947 {
1948 	int ret;
1949 	struct mmc_blk_data *md = mq->blkdata;
1950 	struct mmc_card *card = md->queue.card;
1951 
1952 	if (req && !mq->qcnt)
1953 		/* claim host only for the first request */
1954 		mmc_get_card(card);
1955 
1956 	ret = mmc_blk_part_switch(card, md->part_type);
1957 	if (ret) {
1958 		if (req) {
1959 			blk_end_request_all(req, BLK_STS_IOERR);
1960 		}
1961 		goto out;
1962 	}
1963 
1964 	if (req) {
1965 		switch (req_op(req)) {
1966 		case REQ_OP_DRV_IN:
1967 		case REQ_OP_DRV_OUT:
1968 			/*
1969 			 * Complete ongoing async transfer before issuing
1970 			 * ioctl()s
1971 			 */
1972 			if (mq->qcnt)
1973 				mmc_blk_issue_rw_rq(mq, NULL);
1974 			mmc_blk_issue_drv_op(mq, req);
1975 			break;
1976 		case REQ_OP_DISCARD:
1977 			/*
1978 			 * Complete ongoing async transfer before issuing
1979 			 * discard.
1980 			 */
1981 			if (mq->qcnt)
1982 				mmc_blk_issue_rw_rq(mq, NULL);
1983 			mmc_blk_issue_discard_rq(mq, req);
1984 			break;
1985 		case REQ_OP_SECURE_ERASE:
1986 			/*
1987 			 * Complete ongoing async transfer before issuing
1988 			 * secure erase.
1989 			 */
1990 			if (mq->qcnt)
1991 				mmc_blk_issue_rw_rq(mq, NULL);
1992 			mmc_blk_issue_secdiscard_rq(mq, req);
1993 			break;
1994 		case REQ_OP_FLUSH:
1995 			/*
1996 			 * Complete ongoing async transfer before issuing
1997 			 * flush.
1998 			 */
1999 			if (mq->qcnt)
2000 				mmc_blk_issue_rw_rq(mq, NULL);
2001 			mmc_blk_issue_flush(mq, req);
2002 			break;
2003 		default:
2004 			/* Normal request, just issue it */
2005 			mmc_blk_issue_rw_rq(mq, req);
2006 			card->host->context_info.is_waiting_last_req = false;
2007 			break;
2008 		}
2009 	} else {
2010 		/* No request, flushing the pipeline with NULL */
2011 		mmc_blk_issue_rw_rq(mq, NULL);
2012 		card->host->context_info.is_waiting_last_req = false;
2013 	}
2014 
2015 out:
2016 	if (!mq->qcnt)
2017 		mmc_put_card(card);
2018 }
2019 
2020 static inline int mmc_blk_readonly(struct mmc_card *card)
2021 {
2022 	return mmc_card_readonly(card) ||
2023 	       !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2024 }
2025 
2026 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2027 					      struct device *parent,
2028 					      sector_t size,
2029 					      bool default_ro,
2030 					      const char *subname,
2031 					      int area_type)
2032 {
2033 	struct mmc_blk_data *md;
2034 	int devidx, ret;
2035 
2036 	devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2037 	if (devidx < 0) {
2038 		/*
2039 		 * We get -ENOSPC because there are no more any available
2040 		 * devidx. The reason may be that, either userspace haven't yet
2041 		 * unmounted the partitions, which postpones mmc_blk_release()
2042 		 * from being called, or the device has more partitions than
2043 		 * what we support.
2044 		 */
2045 		if (devidx == -ENOSPC)
2046 			dev_err(mmc_dev(card->host),
2047 				"no more device IDs available\n");
2048 
2049 		return ERR_PTR(devidx);
2050 	}
2051 
2052 	md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2053 	if (!md) {
2054 		ret = -ENOMEM;
2055 		goto out;
2056 	}
2057 
2058 	md->area_type = area_type;
2059 
2060 	/*
2061 	 * Set the read-only status based on the supported commands
2062 	 * and the write protect switch.
2063 	 */
2064 	md->read_only = mmc_blk_readonly(card);
2065 
2066 	md->disk = alloc_disk(perdev_minors);
2067 	if (md->disk == NULL) {
2068 		ret = -ENOMEM;
2069 		goto err_kfree;
2070 	}
2071 
2072 	spin_lock_init(&md->lock);
2073 	INIT_LIST_HEAD(&md->part);
2074 	md->usage = 1;
2075 
2076 	ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2077 	if (ret)
2078 		goto err_putdisk;
2079 
2080 	md->queue.blkdata = md;
2081 
2082 	md->disk->major	= MMC_BLOCK_MAJOR;
2083 	md->disk->first_minor = devidx * perdev_minors;
2084 	md->disk->fops = &mmc_bdops;
2085 	md->disk->private_data = md;
2086 	md->disk->queue = md->queue.queue;
2087 	md->parent = parent;
2088 	set_disk_ro(md->disk, md->read_only || default_ro);
2089 	md->disk->flags = GENHD_FL_EXT_DEVT;
2090 	if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2091 		md->disk->flags |= GENHD_FL_NO_PART_SCAN;
2092 
2093 	/*
2094 	 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2095 	 *
2096 	 * - be set for removable media with permanent block devices
2097 	 * - be unset for removable block devices with permanent media
2098 	 *
2099 	 * Since MMC block devices clearly fall under the second
2100 	 * case, we do not set GENHD_FL_REMOVABLE.  Userspace
2101 	 * should use the block device creation/destruction hotplug
2102 	 * messages to tell when the card is present.
2103 	 */
2104 
2105 	snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2106 		 "mmcblk%u%s", card->host->index, subname ? subname : "");
2107 
2108 	if (mmc_card_mmc(card))
2109 		blk_queue_logical_block_size(md->queue.queue,
2110 					     card->ext_csd.data_sector_size);
2111 	else
2112 		blk_queue_logical_block_size(md->queue.queue, 512);
2113 
2114 	set_capacity(md->disk, size);
2115 
2116 	if (mmc_host_cmd23(card->host)) {
2117 		if ((mmc_card_mmc(card) &&
2118 		     card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2119 		    (mmc_card_sd(card) &&
2120 		     card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2121 			md->flags |= MMC_BLK_CMD23;
2122 	}
2123 
2124 	if (mmc_card_mmc(card) &&
2125 	    md->flags & MMC_BLK_CMD23 &&
2126 	    ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2127 	     card->ext_csd.rel_sectors)) {
2128 		md->flags |= MMC_BLK_REL_WR;
2129 		blk_queue_write_cache(md->queue.queue, true, true);
2130 	}
2131 
2132 	return md;
2133 
2134  err_putdisk:
2135 	put_disk(md->disk);
2136  err_kfree:
2137 	kfree(md);
2138  out:
2139 	ida_simple_remove(&mmc_blk_ida, devidx);
2140 	return ERR_PTR(ret);
2141 }
2142 
2143 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2144 {
2145 	sector_t size;
2146 
2147 	if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2148 		/*
2149 		 * The EXT_CSD sector count is in number or 512 byte
2150 		 * sectors.
2151 		 */
2152 		size = card->ext_csd.sectors;
2153 	} else {
2154 		/*
2155 		 * The CSD capacity field is in units of read_blkbits.
2156 		 * set_capacity takes units of 512 bytes.
2157 		 */
2158 		size = (typeof(sector_t))card->csd.capacity
2159 			<< (card->csd.read_blkbits - 9);
2160 	}
2161 
2162 	return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2163 					MMC_BLK_DATA_AREA_MAIN);
2164 }
2165 
2166 static int mmc_blk_alloc_part(struct mmc_card *card,
2167 			      struct mmc_blk_data *md,
2168 			      unsigned int part_type,
2169 			      sector_t size,
2170 			      bool default_ro,
2171 			      const char *subname,
2172 			      int area_type)
2173 {
2174 	char cap_str[10];
2175 	struct mmc_blk_data *part_md;
2176 
2177 	part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2178 				    subname, area_type);
2179 	if (IS_ERR(part_md))
2180 		return PTR_ERR(part_md);
2181 	part_md->part_type = part_type;
2182 	list_add(&part_md->part, &md->part);
2183 
2184 	string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2185 			cap_str, sizeof(cap_str));
2186 	pr_info("%s: %s %s partition %u %s\n",
2187 	       part_md->disk->disk_name, mmc_card_id(card),
2188 	       mmc_card_name(card), part_md->part_type, cap_str);
2189 	return 0;
2190 }
2191 
2192 /* MMC Physical partitions consist of two boot partitions and
2193  * up to four general purpose partitions.
2194  * For each partition enabled in EXT_CSD a block device will be allocatedi
2195  * to provide access to the partition.
2196  */
2197 
2198 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2199 {
2200 	int idx, ret = 0;
2201 
2202 	if (!mmc_card_mmc(card))
2203 		return 0;
2204 
2205 	for (idx = 0; idx < card->nr_parts; idx++) {
2206 		if (card->part[idx].size) {
2207 			ret = mmc_blk_alloc_part(card, md,
2208 				card->part[idx].part_cfg,
2209 				card->part[idx].size >> 9,
2210 				card->part[idx].force_ro,
2211 				card->part[idx].name,
2212 				card->part[idx].area_type);
2213 			if (ret)
2214 				return ret;
2215 		}
2216 	}
2217 
2218 	return ret;
2219 }
2220 
2221 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2222 {
2223 	struct mmc_card *card;
2224 
2225 	if (md) {
2226 		/*
2227 		 * Flush remaining requests and free queues. It
2228 		 * is freeing the queue that stops new requests
2229 		 * from being accepted.
2230 		 */
2231 		card = md->queue.card;
2232 		spin_lock_irq(md->queue.queue->queue_lock);
2233 		queue_flag_set(QUEUE_FLAG_BYPASS, md->queue.queue);
2234 		spin_unlock_irq(md->queue.queue->queue_lock);
2235 		blk_set_queue_dying(md->queue.queue);
2236 		mmc_cleanup_queue(&md->queue);
2237 		if (md->disk->flags & GENHD_FL_UP) {
2238 			device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2239 			if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2240 					card->ext_csd.boot_ro_lockable)
2241 				device_remove_file(disk_to_dev(md->disk),
2242 					&md->power_ro_lock);
2243 
2244 			del_gendisk(md->disk);
2245 		}
2246 		mmc_blk_put(md);
2247 	}
2248 }
2249 
2250 static void mmc_blk_remove_parts(struct mmc_card *card,
2251 				 struct mmc_blk_data *md)
2252 {
2253 	struct list_head *pos, *q;
2254 	struct mmc_blk_data *part_md;
2255 
2256 	list_for_each_safe(pos, q, &md->part) {
2257 		part_md = list_entry(pos, struct mmc_blk_data, part);
2258 		list_del(pos);
2259 		mmc_blk_remove_req(part_md);
2260 	}
2261 }
2262 
2263 static int mmc_add_disk(struct mmc_blk_data *md)
2264 {
2265 	int ret;
2266 	struct mmc_card *card = md->queue.card;
2267 
2268 	device_add_disk(md->parent, md->disk);
2269 	md->force_ro.show = force_ro_show;
2270 	md->force_ro.store = force_ro_store;
2271 	sysfs_attr_init(&md->force_ro.attr);
2272 	md->force_ro.attr.name = "force_ro";
2273 	md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2274 	ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2275 	if (ret)
2276 		goto force_ro_fail;
2277 
2278 	if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2279 	     card->ext_csd.boot_ro_lockable) {
2280 		umode_t mode;
2281 
2282 		if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2283 			mode = S_IRUGO;
2284 		else
2285 			mode = S_IRUGO | S_IWUSR;
2286 
2287 		md->power_ro_lock.show = power_ro_lock_show;
2288 		md->power_ro_lock.store = power_ro_lock_store;
2289 		sysfs_attr_init(&md->power_ro_lock.attr);
2290 		md->power_ro_lock.attr.mode = mode;
2291 		md->power_ro_lock.attr.name =
2292 					"ro_lock_until_next_power_on";
2293 		ret = device_create_file(disk_to_dev(md->disk),
2294 				&md->power_ro_lock);
2295 		if (ret)
2296 			goto power_ro_lock_fail;
2297 	}
2298 	return ret;
2299 
2300 power_ro_lock_fail:
2301 	device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2302 force_ro_fail:
2303 	del_gendisk(md->disk);
2304 
2305 	return ret;
2306 }
2307 
2308 #ifdef CONFIG_DEBUG_FS
2309 
2310 static int mmc_dbg_card_status_get(void *data, u64 *val)
2311 {
2312 	struct mmc_card *card = data;
2313 	struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2314 	struct mmc_queue *mq = &md->queue;
2315 	struct request *req;
2316 	int ret;
2317 
2318 	/* Ask the block layer about the card status */
2319 	req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2320 	req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2321 	blk_execute_rq(mq->queue, NULL, req, 0);
2322 	ret = req_to_mmc_queue_req(req)->drv_op_result;
2323 	if (ret >= 0) {
2324 		*val = ret;
2325 		ret = 0;
2326 	}
2327 
2328 	return ret;
2329 }
2330 DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2331 		NULL, "%08llx\n");
2332 
2333 /* That is two digits * 512 + 1 for newline */
2334 #define EXT_CSD_STR_LEN 1025
2335 
2336 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2337 {
2338 	struct mmc_card *card = inode->i_private;
2339 	struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2340 	struct mmc_queue *mq = &md->queue;
2341 	struct request *req;
2342 	char *buf;
2343 	ssize_t n = 0;
2344 	u8 *ext_csd;
2345 	int err, i;
2346 
2347 	buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2348 	if (!buf)
2349 		return -ENOMEM;
2350 
2351 	/* Ask the block layer for the EXT CSD */
2352 	req = blk_get_request(mq->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
2353 	req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2354 	req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2355 	blk_execute_rq(mq->queue, NULL, req, 0);
2356 	err = req_to_mmc_queue_req(req)->drv_op_result;
2357 	if (err) {
2358 		pr_err("FAILED %d\n", err);
2359 		goto out_free;
2360 	}
2361 
2362 	for (i = 0; i < 512; i++)
2363 		n += sprintf(buf + n, "%02x", ext_csd[i]);
2364 	n += sprintf(buf + n, "\n");
2365 
2366 	if (n != EXT_CSD_STR_LEN) {
2367 		err = -EINVAL;
2368 		goto out_free;
2369 	}
2370 
2371 	filp->private_data = buf;
2372 	kfree(ext_csd);
2373 	return 0;
2374 
2375 out_free:
2376 	kfree(buf);
2377 	return err;
2378 }
2379 
2380 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2381 				size_t cnt, loff_t *ppos)
2382 {
2383 	char *buf = filp->private_data;
2384 
2385 	return simple_read_from_buffer(ubuf, cnt, ppos,
2386 				       buf, EXT_CSD_STR_LEN);
2387 }
2388 
2389 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2390 {
2391 	kfree(file->private_data);
2392 	return 0;
2393 }
2394 
2395 static const struct file_operations mmc_dbg_ext_csd_fops = {
2396 	.open		= mmc_ext_csd_open,
2397 	.read		= mmc_ext_csd_read,
2398 	.release	= mmc_ext_csd_release,
2399 	.llseek		= default_llseek,
2400 };
2401 
2402 static int mmc_blk_add_debugfs(struct mmc_card *card)
2403 {
2404 	struct dentry *root;
2405 
2406 	if (!card->debugfs_root)
2407 		return 0;
2408 
2409 	root = card->debugfs_root;
2410 
2411 	if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2412 		if (!debugfs_create_file("status", S_IRUSR, root, card,
2413 					 &mmc_dbg_card_status_fops))
2414 			return -EIO;
2415 	}
2416 
2417 	if (mmc_card_mmc(card)) {
2418 		if (!debugfs_create_file("ext_csd", S_IRUSR, root, card,
2419 					 &mmc_dbg_ext_csd_fops))
2420 			return -EIO;
2421 	}
2422 
2423 	return 0;
2424 }
2425 
2426 
2427 #else
2428 
2429 static int mmc_blk_add_debugfs(struct mmc_card *card)
2430 {
2431 	return 0;
2432 }
2433 
2434 #endif /* CONFIG_DEBUG_FS */
2435 
2436 static int mmc_blk_probe(struct mmc_card *card)
2437 {
2438 	struct mmc_blk_data *md, *part_md;
2439 	char cap_str[10];
2440 
2441 	/*
2442 	 * Check that the card supports the command class(es) we need.
2443 	 */
2444 	if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2445 		return -ENODEV;
2446 
2447 	mmc_fixup_device(card, mmc_blk_fixups);
2448 
2449 	md = mmc_blk_alloc(card);
2450 	if (IS_ERR(md))
2451 		return PTR_ERR(md);
2452 
2453 	string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2454 			cap_str, sizeof(cap_str));
2455 	pr_info("%s: %s %s %s %s\n",
2456 		md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2457 		cap_str, md->read_only ? "(ro)" : "");
2458 
2459 	if (mmc_blk_alloc_parts(card, md))
2460 		goto out;
2461 
2462 	dev_set_drvdata(&card->dev, md);
2463 
2464 	if (mmc_add_disk(md))
2465 		goto out;
2466 
2467 	list_for_each_entry(part_md, &md->part, part) {
2468 		if (mmc_add_disk(part_md))
2469 			goto out;
2470 	}
2471 
2472 	/* Add two debugfs entries */
2473 	mmc_blk_add_debugfs(card);
2474 
2475 	pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2476 	pm_runtime_use_autosuspend(&card->dev);
2477 
2478 	/*
2479 	 * Don't enable runtime PM for SD-combo cards here. Leave that
2480 	 * decision to be taken during the SDIO init sequence instead.
2481 	 */
2482 	if (card->type != MMC_TYPE_SD_COMBO) {
2483 		pm_runtime_set_active(&card->dev);
2484 		pm_runtime_enable(&card->dev);
2485 	}
2486 
2487 	return 0;
2488 
2489  out:
2490 	mmc_blk_remove_parts(card, md);
2491 	mmc_blk_remove_req(md);
2492 	return 0;
2493 }
2494 
2495 static void mmc_blk_remove(struct mmc_card *card)
2496 {
2497 	struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2498 
2499 	mmc_blk_remove_parts(card, md);
2500 	pm_runtime_get_sync(&card->dev);
2501 	mmc_claim_host(card->host);
2502 	mmc_blk_part_switch(card, md->part_type);
2503 	mmc_release_host(card->host);
2504 	if (card->type != MMC_TYPE_SD_COMBO)
2505 		pm_runtime_disable(&card->dev);
2506 	pm_runtime_put_noidle(&card->dev);
2507 	mmc_blk_remove_req(md);
2508 	dev_set_drvdata(&card->dev, NULL);
2509 }
2510 
2511 static int _mmc_blk_suspend(struct mmc_card *card)
2512 {
2513 	struct mmc_blk_data *part_md;
2514 	struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2515 
2516 	if (md) {
2517 		mmc_queue_suspend(&md->queue);
2518 		list_for_each_entry(part_md, &md->part, part) {
2519 			mmc_queue_suspend(&part_md->queue);
2520 		}
2521 	}
2522 	return 0;
2523 }
2524 
2525 static void mmc_blk_shutdown(struct mmc_card *card)
2526 {
2527 	_mmc_blk_suspend(card);
2528 }
2529 
2530 #ifdef CONFIG_PM_SLEEP
2531 static int mmc_blk_suspend(struct device *dev)
2532 {
2533 	struct mmc_card *card = mmc_dev_to_card(dev);
2534 
2535 	return _mmc_blk_suspend(card);
2536 }
2537 
2538 static int mmc_blk_resume(struct device *dev)
2539 {
2540 	struct mmc_blk_data *part_md;
2541 	struct mmc_blk_data *md = dev_get_drvdata(dev);
2542 
2543 	if (md) {
2544 		/*
2545 		 * Resume involves the card going into idle state,
2546 		 * so current partition is always the main one.
2547 		 */
2548 		md->part_curr = md->part_type;
2549 		mmc_queue_resume(&md->queue);
2550 		list_for_each_entry(part_md, &md->part, part) {
2551 			mmc_queue_resume(&part_md->queue);
2552 		}
2553 	}
2554 	return 0;
2555 }
2556 #endif
2557 
2558 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
2559 
2560 static struct mmc_driver mmc_driver = {
2561 	.drv		= {
2562 		.name	= "mmcblk",
2563 		.pm	= &mmc_blk_pm_ops,
2564 	},
2565 	.probe		= mmc_blk_probe,
2566 	.remove		= mmc_blk_remove,
2567 	.shutdown	= mmc_blk_shutdown,
2568 };
2569 
2570 static int __init mmc_blk_init(void)
2571 {
2572 	int res;
2573 
2574 	if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
2575 		pr_info("mmcblk: using %d minors per device\n", perdev_minors);
2576 
2577 	max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
2578 
2579 	res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
2580 	if (res)
2581 		goto out;
2582 
2583 	res = mmc_register_driver(&mmc_driver);
2584 	if (res)
2585 		goto out2;
2586 
2587 	return 0;
2588  out2:
2589 	unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2590  out:
2591 	return res;
2592 }
2593 
2594 static void __exit mmc_blk_exit(void)
2595 {
2596 	mmc_unregister_driver(&mmc_driver);
2597 	unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
2598 }
2599 
2600 module_init(mmc_blk_init);
2601 module_exit(mmc_blk_exit);
2602 
2603 MODULE_LICENSE("GPL");
2604 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");
2605 
2606