Linux Audio

Check our new training course

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