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