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