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