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