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