Debian Codegraph @

drivers/mmc/core/block.c

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