Linux 4.19.133
[linux/fpc-iii.git] / drivers / mmc / core / block.c
blobc723a1e54b188f246c1d7bd49070a041538264e9
1 /*
2 * Block driver for media (i.e., flash cards)
4 * Copyright 2002 Hewlett-Packard Company
5 * Copyright 2005-2008 Pierre Ossman
7 * Use consistent with the GNU GPL is permitted,
8 * provided that this copyright notice is
9 * preserved in its entirety in all copies and derived works.
11 * HEWLETT-PACKARD COMPANY MAKES NO WARRANTIES, EXPRESSED OR IMPLIED,
12 * AS TO THE USEFULNESS OR CORRECTNESS OF THIS CODE OR ITS
13 * FITNESS FOR ANY PARTICULAR PURPOSE.
15 * Many thanks to Alessandro Rubini and Jonathan Corbet!
17 * Author: Andrew Christian
18 * 28 May 2002
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.h>
25 #include <linux/fs.h>
26 #include <linux/slab.h>
27 #include <linux/errno.h>
28 #include <linux/hdreg.h>
29 #include <linux/kdev_t.h>
30 #include <linux/blkdev.h>
31 #include <linux/cdev.h>
32 #include <linux/mutex.h>
33 #include <linux/scatterlist.h>
34 #include <linux/string_helpers.h>
35 #include <linux/delay.h>
36 #include <linux/capability.h>
37 #include <linux/compat.h>
38 #include <linux/pm_runtime.h>
39 #include <linux/idr.h>
40 #include <linux/debugfs.h>
42 #include <linux/mmc/ioctl.h>
43 #include <linux/mmc/card.h>
44 #include <linux/mmc/host.h>
45 #include <linux/mmc/mmc.h>
46 #include <linux/mmc/sd.h>
48 #include <linux/uaccess.h>
50 #include "queue.h"
51 #include "block.h"
52 #include "core.h"
53 #include "card.h"
54 #include "host.h"
55 #include "bus.h"
56 #include "mmc_ops.h"
57 #include "quirks.h"
58 #include "sd_ops.h"
60 MODULE_ALIAS("mmc:block");
61 #ifdef MODULE_PARAM_PREFIX
62 #undef MODULE_PARAM_PREFIX
63 #endif
64 #define MODULE_PARAM_PREFIX "mmcblk."
67 * Set a 10 second timeout for polling write request busy state. Note, mmc core
68 * is setting a 3 second timeout for SD cards, and SDHCI has long had a 10
69 * second software timer to timeout the whole request, so 10 seconds should be
70 * ample.
72 #define MMC_BLK_TIMEOUT_MS (10 * 1000)
73 #define MMC_SANITIZE_REQ_TIMEOUT 240000
74 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
75 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
77 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
78 (rq_data_dir(req) == WRITE))
79 static DEFINE_MUTEX(block_mutex);
82 * The defaults come from config options but can be overriden by module
83 * or bootarg options.
85 static int perdev_minors = CONFIG_MMC_BLOCK_MINORS;
88 * We've only got one major, so number of mmcblk devices is
89 * limited to (1 << 20) / number of minors per device. It is also
90 * limited by the MAX_DEVICES below.
92 static int max_devices;
94 #define MAX_DEVICES 256
96 static DEFINE_IDA(mmc_blk_ida);
97 static DEFINE_IDA(mmc_rpmb_ida);
100 * There is one mmc_blk_data per slot.
102 struct mmc_blk_data {
103 spinlock_t lock;
104 struct device *parent;
105 struct gendisk *disk;
106 struct mmc_queue queue;
107 struct list_head part;
108 struct list_head rpmbs;
110 unsigned int flags;
111 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
112 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
114 unsigned int usage;
115 unsigned int read_only;
116 unsigned int part_type;
117 unsigned int reset_done;
118 #define MMC_BLK_READ BIT(0)
119 #define MMC_BLK_WRITE BIT(1)
120 #define MMC_BLK_DISCARD BIT(2)
121 #define MMC_BLK_SECDISCARD BIT(3)
122 #define MMC_BLK_CQE_RECOVERY BIT(4)
125 * Only set in main mmc_blk_data associated
126 * with mmc_card with dev_set_drvdata, and keeps
127 * track of the current selected device partition.
129 unsigned int part_curr;
130 struct device_attribute force_ro;
131 struct device_attribute power_ro_lock;
132 int area_type;
134 /* debugfs files (only in main mmc_blk_data) */
135 struct dentry *status_dentry;
136 struct dentry *ext_csd_dentry;
139 /* Device type for RPMB character devices */
140 static dev_t mmc_rpmb_devt;
142 /* Bus type for RPMB character devices */
143 static struct bus_type mmc_rpmb_bus_type = {
144 .name = "mmc_rpmb",
148 * struct mmc_rpmb_data - special RPMB device type for these areas
149 * @dev: the device for the RPMB area
150 * @chrdev: character device for the RPMB area
151 * @id: unique device ID number
152 * @part_index: partition index (0 on first)
153 * @md: parent MMC block device
154 * @node: list item, so we can put this device on a list
156 struct mmc_rpmb_data {
157 struct device dev;
158 struct cdev chrdev;
159 int id;
160 unsigned int part_index;
161 struct mmc_blk_data *md;
162 struct list_head node;
165 static DEFINE_MUTEX(open_lock);
167 module_param(perdev_minors, int, 0444);
168 MODULE_PARM_DESC(perdev_minors, "Minors numbers to allocate per device");
170 static inline int mmc_blk_part_switch(struct mmc_card *card,
171 unsigned int part_type);
173 static struct mmc_blk_data *mmc_blk_get(struct gendisk *disk)
175 struct mmc_blk_data *md;
177 mutex_lock(&open_lock);
178 md = disk->private_data;
179 if (md && md->usage == 0)
180 md = NULL;
181 if (md)
182 md->usage++;
183 mutex_unlock(&open_lock);
185 return md;
188 static inline int mmc_get_devidx(struct gendisk *disk)
190 int devidx = disk->first_minor / perdev_minors;
191 return devidx;
194 static void mmc_blk_put(struct mmc_blk_data *md)
196 mutex_lock(&open_lock);
197 md->usage--;
198 if (md->usage == 0) {
199 int devidx = mmc_get_devidx(md->disk);
200 blk_put_queue(md->queue.queue);
201 ida_simple_remove(&mmc_blk_ida, devidx);
202 put_disk(md->disk);
203 kfree(md);
205 mutex_unlock(&open_lock);
208 static ssize_t power_ro_lock_show(struct device *dev,
209 struct device_attribute *attr, char *buf)
211 int ret;
212 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
213 struct mmc_card *card = md->queue.card;
214 int locked = 0;
216 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PERM_WP_EN)
217 locked = 2;
218 else if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_EN)
219 locked = 1;
221 ret = snprintf(buf, PAGE_SIZE, "%d\n", locked);
223 mmc_blk_put(md);
225 return ret;
228 static ssize_t power_ro_lock_store(struct device *dev,
229 struct device_attribute *attr, const char *buf, size_t count)
231 int ret;
232 struct mmc_blk_data *md, *part_md;
233 struct mmc_queue *mq;
234 struct request *req;
235 unsigned long set;
237 if (kstrtoul(buf, 0, &set))
238 return -EINVAL;
240 if (set != 1)
241 return count;
243 md = mmc_blk_get(dev_to_disk(dev));
244 mq = &md->queue;
246 /* Dispatch locking to the block layer */
247 req = blk_get_request(mq->queue, REQ_OP_DRV_OUT, 0);
248 if (IS_ERR(req)) {
249 count = PTR_ERR(req);
250 goto out_put;
252 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_BOOT_WP;
253 blk_execute_rq(mq->queue, NULL, req, 0);
254 ret = req_to_mmc_queue_req(req)->drv_op_result;
255 blk_put_request(req);
257 if (!ret) {
258 pr_info("%s: Locking boot partition ro until next power on\n",
259 md->disk->disk_name);
260 set_disk_ro(md->disk, 1);
262 list_for_each_entry(part_md, &md->part, part)
263 if (part_md->area_type == MMC_BLK_DATA_AREA_BOOT) {
264 pr_info("%s: Locking boot partition ro until next power on\n", part_md->disk->disk_name);
265 set_disk_ro(part_md->disk, 1);
268 out_put:
269 mmc_blk_put(md);
270 return count;
273 static ssize_t force_ro_show(struct device *dev, struct device_attribute *attr,
274 char *buf)
276 int ret;
277 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
279 ret = snprintf(buf, PAGE_SIZE, "%d\n",
280 get_disk_ro(dev_to_disk(dev)) ^
281 md->read_only);
282 mmc_blk_put(md);
283 return ret;
286 static ssize_t force_ro_store(struct device *dev, struct device_attribute *attr,
287 const char *buf, size_t count)
289 int ret;
290 char *end;
291 struct mmc_blk_data *md = mmc_blk_get(dev_to_disk(dev));
292 unsigned long set = simple_strtoul(buf, &end, 0);
293 if (end == buf) {
294 ret = -EINVAL;
295 goto out;
298 set_disk_ro(dev_to_disk(dev), set || md->read_only);
299 ret = count;
300 out:
301 mmc_blk_put(md);
302 return ret;
305 static int mmc_blk_open(struct block_device *bdev, fmode_t mode)
307 struct mmc_blk_data *md = mmc_blk_get(bdev->bd_disk);
308 int ret = -ENXIO;
310 mutex_lock(&block_mutex);
311 if (md) {
312 if (md->usage == 2)
313 check_disk_change(bdev);
314 ret = 0;
316 if ((mode & FMODE_WRITE) && md->read_only) {
317 mmc_blk_put(md);
318 ret = -EROFS;
321 mutex_unlock(&block_mutex);
323 return ret;
326 static void mmc_blk_release(struct gendisk *disk, fmode_t mode)
328 struct mmc_blk_data *md = disk->private_data;
330 mutex_lock(&block_mutex);
331 mmc_blk_put(md);
332 mutex_unlock(&block_mutex);
335 static int
336 mmc_blk_getgeo(struct block_device *bdev, struct hd_geometry *geo)
338 geo->cylinders = get_capacity(bdev->bd_disk) / (4 * 16);
339 geo->heads = 4;
340 geo->sectors = 16;
341 return 0;
344 struct mmc_blk_ioc_data {
345 struct mmc_ioc_cmd ic;
346 unsigned char *buf;
347 u64 buf_bytes;
348 struct mmc_rpmb_data *rpmb;
351 static struct mmc_blk_ioc_data *mmc_blk_ioctl_copy_from_user(
352 struct mmc_ioc_cmd __user *user)
354 struct mmc_blk_ioc_data *idata;
355 int err;
357 idata = kmalloc(sizeof(*idata), GFP_KERNEL);
358 if (!idata) {
359 err = -ENOMEM;
360 goto out;
363 if (copy_from_user(&idata->ic, user, sizeof(idata->ic))) {
364 err = -EFAULT;
365 goto idata_err;
368 idata->buf_bytes = (u64) idata->ic.blksz * idata->ic.blocks;
369 if (idata->buf_bytes > MMC_IOC_MAX_BYTES) {
370 err = -EOVERFLOW;
371 goto idata_err;
374 if (!idata->buf_bytes) {
375 idata->buf = NULL;
376 return idata;
379 idata->buf = memdup_user((void __user *)(unsigned long)
380 idata->ic.data_ptr, idata->buf_bytes);
381 if (IS_ERR(idata->buf)) {
382 err = PTR_ERR(idata->buf);
383 goto idata_err;
386 return idata;
388 idata_err:
389 kfree(idata);
390 out:
391 return ERR_PTR(err);
394 static int mmc_blk_ioctl_copy_to_user(struct mmc_ioc_cmd __user *ic_ptr,
395 struct mmc_blk_ioc_data *idata)
397 struct mmc_ioc_cmd *ic = &idata->ic;
399 if (copy_to_user(&(ic_ptr->response), ic->response,
400 sizeof(ic->response)))
401 return -EFAULT;
403 if (!idata->ic.write_flag) {
404 if (copy_to_user((void __user *)(unsigned long)ic->data_ptr,
405 idata->buf, idata->buf_bytes))
406 return -EFAULT;
409 return 0;
412 static int ioctl_do_sanitize(struct mmc_card *card)
414 int err;
416 if (!mmc_can_sanitize(card)) {
417 pr_warn("%s: %s - SANITIZE is not supported\n",
418 mmc_hostname(card->host), __func__);
419 err = -EOPNOTSUPP;
420 goto out;
423 pr_debug("%s: %s - SANITIZE IN PROGRESS...\n",
424 mmc_hostname(card->host), __func__);
426 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
427 EXT_CSD_SANITIZE_START, 1,
428 MMC_SANITIZE_REQ_TIMEOUT);
430 if (err)
431 pr_err("%s: %s - EXT_CSD_SANITIZE_START failed. err=%d\n",
432 mmc_hostname(card->host), __func__, err);
434 pr_debug("%s: %s - SANITIZE COMPLETED\n", mmc_hostname(card->host),
435 __func__);
436 out:
437 return err;
440 static inline bool mmc_blk_in_tran_state(u32 status)
443 * Some cards mishandle the status bits, so make sure to check both the
444 * busy indication and the card state.
446 return status & R1_READY_FOR_DATA &&
447 (R1_CURRENT_STATE(status) == R1_STATE_TRAN);
450 static int card_busy_detect(struct mmc_card *card, unsigned int timeout_ms,
451 u32 *resp_errs)
453 unsigned long timeout = jiffies + msecs_to_jiffies(timeout_ms);
454 int err = 0;
455 u32 status;
457 do {
458 bool done = time_after(jiffies, timeout);
460 err = __mmc_send_status(card, &status, 5);
461 if (err) {
462 dev_err(mmc_dev(card->host),
463 "error %d requesting status\n", err);
464 return err;
467 /* Accumulate any response error bits seen */
468 if (resp_errs)
469 *resp_errs |= status;
472 * Timeout if the device never becomes ready for data and never
473 * leaves the program state.
475 if (done) {
476 dev_err(mmc_dev(card->host),
477 "Card stuck in wrong state! %s status: %#x\n",
478 __func__, status);
479 return -ETIMEDOUT;
483 * Some cards mishandle the status bits,
484 * so make sure to check both the busy
485 * indication and the card state.
487 } while (!mmc_blk_in_tran_state(status));
489 return err;
492 static int __mmc_blk_ioctl_cmd(struct mmc_card *card, struct mmc_blk_data *md,
493 struct mmc_blk_ioc_data *idata)
495 struct mmc_command cmd = {}, sbc = {};
496 struct mmc_data data = {};
497 struct mmc_request mrq = {};
498 struct scatterlist sg;
499 int err;
500 unsigned int target_part;
502 if (!card || !md || !idata)
503 return -EINVAL;
506 * The RPMB accesses comes in from the character device, so we
507 * need to target these explicitly. Else we just target the
508 * partition type for the block device the ioctl() was issued
509 * on.
511 if (idata->rpmb) {
512 /* Support multiple RPMB partitions */
513 target_part = idata->rpmb->part_index;
514 target_part |= EXT_CSD_PART_CONFIG_ACC_RPMB;
515 } else {
516 target_part = md->part_type;
519 cmd.opcode = idata->ic.opcode;
520 cmd.arg = idata->ic.arg;
521 cmd.flags = idata->ic.flags;
523 if (idata->buf_bytes) {
524 data.sg = &sg;
525 data.sg_len = 1;
526 data.blksz = idata->ic.blksz;
527 data.blocks = idata->ic.blocks;
529 sg_init_one(data.sg, idata->buf, idata->buf_bytes);
531 if (idata->ic.write_flag)
532 data.flags = MMC_DATA_WRITE;
533 else
534 data.flags = MMC_DATA_READ;
536 /* data.flags must already be set before doing this. */
537 mmc_set_data_timeout(&data, card);
539 /* Allow overriding the timeout_ns for empirical tuning. */
540 if (idata->ic.data_timeout_ns)
541 data.timeout_ns = idata->ic.data_timeout_ns;
543 if ((cmd.flags & MMC_RSP_R1B) == MMC_RSP_R1B) {
545 * Pretend this is a data transfer and rely on the
546 * host driver to compute timeout. When all host
547 * drivers support cmd.cmd_timeout for R1B, this
548 * can be changed to:
550 * mrq.data = NULL;
551 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
553 data.timeout_ns = idata->ic.cmd_timeout_ms * 1000000;
556 mrq.data = &data;
559 mrq.cmd = &cmd;
561 err = mmc_blk_part_switch(card, target_part);
562 if (err)
563 return err;
565 if (idata->ic.is_acmd) {
566 err = mmc_app_cmd(card->host, card);
567 if (err)
568 return err;
571 if (idata->rpmb) {
572 sbc.opcode = MMC_SET_BLOCK_COUNT;
574 * We don't do any blockcount validation because the max size
575 * may be increased by a future standard. We just copy the
576 * 'Reliable Write' bit here.
578 sbc.arg = data.blocks | (idata->ic.write_flag & BIT(31));
579 sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
580 mrq.sbc = &sbc;
583 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_SANITIZE_START) &&
584 (cmd.opcode == MMC_SWITCH)) {
585 err = ioctl_do_sanitize(card);
587 if (err)
588 pr_err("%s: ioctl_do_sanitize() failed. err = %d",
589 __func__, err);
591 return err;
594 mmc_wait_for_req(card->host, &mrq);
596 if (cmd.error) {
597 dev_err(mmc_dev(card->host), "%s: cmd error %d\n",
598 __func__, cmd.error);
599 return cmd.error;
601 if (data.error) {
602 dev_err(mmc_dev(card->host), "%s: data error %d\n",
603 __func__, data.error);
604 return data.error;
608 * Make sure the cache of the PARTITION_CONFIG register and
609 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
610 * changed it successfully.
612 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd.arg) == EXT_CSD_PART_CONFIG) &&
613 (cmd.opcode == MMC_SWITCH)) {
614 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
615 u8 value = MMC_EXTRACT_VALUE_FROM_ARG(cmd.arg);
618 * Update cache so the next mmc_blk_part_switch call operates
619 * on up-to-date data.
621 card->ext_csd.part_config = value;
622 main_md->part_curr = value & EXT_CSD_PART_CONFIG_ACC_MASK;
626 * According to the SD specs, some commands require a delay after
627 * issuing the command.
629 if (idata->ic.postsleep_min_us)
630 usleep_range(idata->ic.postsleep_min_us, idata->ic.postsleep_max_us);
632 memcpy(&(idata->ic.response), cmd.resp, sizeof(cmd.resp));
634 if (idata->rpmb || (cmd.flags & MMC_RSP_R1B)) {
636 * Ensure RPMB/R1B command has completed by polling CMD13
637 * "Send Status".
639 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, NULL);
642 return err;
645 static int mmc_blk_ioctl_cmd(struct mmc_blk_data *md,
646 struct mmc_ioc_cmd __user *ic_ptr,
647 struct mmc_rpmb_data *rpmb)
649 struct mmc_blk_ioc_data *idata;
650 struct mmc_blk_ioc_data *idatas[1];
651 struct mmc_queue *mq;
652 struct mmc_card *card;
653 int err = 0, ioc_err = 0;
654 struct request *req;
656 idata = mmc_blk_ioctl_copy_from_user(ic_ptr);
657 if (IS_ERR(idata))
658 return PTR_ERR(idata);
659 /* This will be NULL on non-RPMB ioctl():s */
660 idata->rpmb = rpmb;
662 card = md->queue.card;
663 if (IS_ERR(card)) {
664 err = PTR_ERR(card);
665 goto cmd_done;
669 * Dispatch the ioctl() into the block request queue.
671 mq = &md->queue;
672 req = blk_get_request(mq->queue,
673 idata->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
674 if (IS_ERR(req)) {
675 err = PTR_ERR(req);
676 goto cmd_done;
678 idatas[0] = idata;
679 req_to_mmc_queue_req(req)->drv_op =
680 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
681 req_to_mmc_queue_req(req)->drv_op_data = idatas;
682 req_to_mmc_queue_req(req)->ioc_count = 1;
683 blk_execute_rq(mq->queue, NULL, req, 0);
684 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
685 err = mmc_blk_ioctl_copy_to_user(ic_ptr, idata);
686 blk_put_request(req);
688 cmd_done:
689 kfree(idata->buf);
690 kfree(idata);
691 return ioc_err ? ioc_err : err;
694 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data *md,
695 struct mmc_ioc_multi_cmd __user *user,
696 struct mmc_rpmb_data *rpmb)
698 struct mmc_blk_ioc_data **idata = NULL;
699 struct mmc_ioc_cmd __user *cmds = user->cmds;
700 struct mmc_card *card;
701 struct mmc_queue *mq;
702 int i, err = 0, ioc_err = 0;
703 __u64 num_of_cmds;
704 struct request *req;
706 if (copy_from_user(&num_of_cmds, &user->num_of_cmds,
707 sizeof(num_of_cmds)))
708 return -EFAULT;
710 if (!num_of_cmds)
711 return 0;
713 if (num_of_cmds > MMC_IOC_MAX_CMDS)
714 return -EINVAL;
716 idata = kcalloc(num_of_cmds, sizeof(*idata), GFP_KERNEL);
717 if (!idata)
718 return -ENOMEM;
720 for (i = 0; i < num_of_cmds; i++) {
721 idata[i] = mmc_blk_ioctl_copy_from_user(&cmds[i]);
722 if (IS_ERR(idata[i])) {
723 err = PTR_ERR(idata[i]);
724 num_of_cmds = i;
725 goto cmd_err;
727 /* This will be NULL on non-RPMB ioctl():s */
728 idata[i]->rpmb = rpmb;
731 card = md->queue.card;
732 if (IS_ERR(card)) {
733 err = PTR_ERR(card);
734 goto cmd_err;
739 * Dispatch the ioctl()s into the block request queue.
741 mq = &md->queue;
742 req = blk_get_request(mq->queue,
743 idata[0]->ic.write_flag ? REQ_OP_DRV_OUT : REQ_OP_DRV_IN, 0);
744 if (IS_ERR(req)) {
745 err = PTR_ERR(req);
746 goto cmd_err;
748 req_to_mmc_queue_req(req)->drv_op =
749 rpmb ? MMC_DRV_OP_IOCTL_RPMB : MMC_DRV_OP_IOCTL;
750 req_to_mmc_queue_req(req)->drv_op_data = idata;
751 req_to_mmc_queue_req(req)->ioc_count = num_of_cmds;
752 blk_execute_rq(mq->queue, NULL, req, 0);
753 ioc_err = req_to_mmc_queue_req(req)->drv_op_result;
755 /* copy to user if data and response */
756 for (i = 0; i < num_of_cmds && !err; i++)
757 err = mmc_blk_ioctl_copy_to_user(&cmds[i], idata[i]);
759 blk_put_request(req);
761 cmd_err:
762 for (i = 0; i < num_of_cmds; i++) {
763 kfree(idata[i]->buf);
764 kfree(idata[i]);
766 kfree(idata);
767 return ioc_err ? ioc_err : err;
770 static int mmc_blk_check_blkdev(struct block_device *bdev)
773 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
774 * whole block device, not on a partition. This prevents overspray
775 * between sibling partitions.
777 if ((!capable(CAP_SYS_RAWIO)) || (bdev != bdev->bd_contains))
778 return -EPERM;
779 return 0;
782 static int mmc_blk_ioctl(struct block_device *bdev, fmode_t mode,
783 unsigned int cmd, unsigned long arg)
785 struct mmc_blk_data *md;
786 int ret;
788 switch (cmd) {
789 case MMC_IOC_CMD:
790 ret = mmc_blk_check_blkdev(bdev);
791 if (ret)
792 return ret;
793 md = mmc_blk_get(bdev->bd_disk);
794 if (!md)
795 return -EINVAL;
796 ret = mmc_blk_ioctl_cmd(md,
797 (struct mmc_ioc_cmd __user *)arg,
798 NULL);
799 mmc_blk_put(md);
800 return ret;
801 case MMC_IOC_MULTI_CMD:
802 ret = mmc_blk_check_blkdev(bdev);
803 if (ret)
804 return ret;
805 md = mmc_blk_get(bdev->bd_disk);
806 if (!md)
807 return -EINVAL;
808 ret = mmc_blk_ioctl_multi_cmd(md,
809 (struct mmc_ioc_multi_cmd __user *)arg,
810 NULL);
811 mmc_blk_put(md);
812 return ret;
813 default:
814 return -EINVAL;
818 #ifdef CONFIG_COMPAT
819 static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
820 unsigned int cmd, unsigned long arg)
822 return mmc_blk_ioctl(bdev, mode, cmd, (unsigned long) compat_ptr(arg));
824 #endif
826 static const struct block_device_operations mmc_bdops = {
827 .open = mmc_blk_open,
828 .release = mmc_blk_release,
829 .getgeo = mmc_blk_getgeo,
830 .owner = THIS_MODULE,
831 .ioctl = mmc_blk_ioctl,
832 #ifdef CONFIG_COMPAT
833 .compat_ioctl = mmc_blk_compat_ioctl,
834 #endif
837 static int mmc_blk_part_switch_pre(struct mmc_card *card,
838 unsigned int part_type)
840 int ret = 0;
842 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
843 if (card->ext_csd.cmdq_en) {
844 ret = mmc_cmdq_disable(card);
845 if (ret)
846 return ret;
848 mmc_retune_pause(card->host);
851 return ret;
854 static int mmc_blk_part_switch_post(struct mmc_card *card,
855 unsigned int part_type)
857 int ret = 0;
859 if (part_type == EXT_CSD_PART_CONFIG_ACC_RPMB) {
860 mmc_retune_unpause(card->host);
861 if (card->reenable_cmdq && !card->ext_csd.cmdq_en)
862 ret = mmc_cmdq_enable(card);
865 return ret;
868 static inline int mmc_blk_part_switch(struct mmc_card *card,
869 unsigned int part_type)
871 int ret = 0;
872 struct mmc_blk_data *main_md = dev_get_drvdata(&card->dev);
874 if (main_md->part_curr == part_type)
875 return 0;
877 if (mmc_card_mmc(card)) {
878 u8 part_config = card->ext_csd.part_config;
880 ret = mmc_blk_part_switch_pre(card, part_type);
881 if (ret)
882 return ret;
884 part_config &= ~EXT_CSD_PART_CONFIG_ACC_MASK;
885 part_config |= part_type;
887 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
888 EXT_CSD_PART_CONFIG, part_config,
889 card->ext_csd.part_time);
890 if (ret) {
891 mmc_blk_part_switch_post(card, part_type);
892 return ret;
895 card->ext_csd.part_config = part_config;
897 ret = mmc_blk_part_switch_post(card, main_md->part_curr);
900 main_md->part_curr = part_type;
901 return ret;
904 static int mmc_sd_num_wr_blocks(struct mmc_card *card, u32 *written_blocks)
906 int err;
907 u32 result;
908 __be32 *blocks;
910 struct mmc_request mrq = {};
911 struct mmc_command cmd = {};
912 struct mmc_data data = {};
914 struct scatterlist sg;
916 cmd.opcode = MMC_APP_CMD;
917 cmd.arg = card->rca << 16;
918 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
920 err = mmc_wait_for_cmd(card->host, &cmd, 0);
921 if (err)
922 return err;
923 if (!mmc_host_is_spi(card->host) && !(cmd.resp[0] & R1_APP_CMD))
924 return -EIO;
926 memset(&cmd, 0, sizeof(struct mmc_command));
928 cmd.opcode = SD_APP_SEND_NUM_WR_BLKS;
929 cmd.arg = 0;
930 cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
932 data.blksz = 4;
933 data.blocks = 1;
934 data.flags = MMC_DATA_READ;
935 data.sg = &sg;
936 data.sg_len = 1;
937 mmc_set_data_timeout(&data, card);
939 mrq.cmd = &cmd;
940 mrq.data = &data;
942 blocks = kmalloc(4, GFP_KERNEL);
943 if (!blocks)
944 return -ENOMEM;
946 sg_init_one(&sg, blocks, 4);
948 mmc_wait_for_req(card->host, &mrq);
950 result = ntohl(*blocks);
951 kfree(blocks);
953 if (cmd.error || data.error)
954 return -EIO;
956 *written_blocks = result;
958 return 0;
961 static unsigned int mmc_blk_clock_khz(struct mmc_host *host)
963 if (host->actual_clock)
964 return host->actual_clock / 1000;
966 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
967 if (host->ios.clock)
968 return host->ios.clock / 2000;
970 /* How can there be no clock */
971 WARN_ON_ONCE(1);
972 return 100; /* 100 kHz is minimum possible value */
975 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host *host,
976 struct mmc_data *data)
978 unsigned int ms = DIV_ROUND_UP(data->timeout_ns, 1000000);
979 unsigned int khz;
981 if (data->timeout_clks) {
982 khz = mmc_blk_clock_khz(host);
983 ms += DIV_ROUND_UP(data->timeout_clks, khz);
986 return ms;
989 static int mmc_blk_reset(struct mmc_blk_data *md, struct mmc_host *host,
990 int type)
992 int err;
994 if (md->reset_done & type)
995 return -EEXIST;
997 md->reset_done |= type;
998 err = mmc_hw_reset(host);
999 /* Ensure we switch back to the correct partition */
1000 if (err != -EOPNOTSUPP) {
1001 struct mmc_blk_data *main_md =
1002 dev_get_drvdata(&host->card->dev);
1003 int part_err;
1005 main_md->part_curr = main_md->part_type;
1006 part_err = mmc_blk_part_switch(host->card, md->part_type);
1007 if (part_err) {
1009 * We have failed to get back into the correct
1010 * partition, so we need to abort the whole request.
1012 return -ENODEV;
1015 return err;
1018 static inline void mmc_blk_reset_success(struct mmc_blk_data *md, int type)
1020 md->reset_done &= ~type;
1024 * The non-block commands come back from the block layer after it queued it and
1025 * processed it with all other requests and then they get issued in this
1026 * function.
1028 static void mmc_blk_issue_drv_op(struct mmc_queue *mq, struct request *req)
1030 struct mmc_queue_req *mq_rq;
1031 struct mmc_card *card = mq->card;
1032 struct mmc_blk_data *md = mq->blkdata;
1033 struct mmc_blk_ioc_data **idata;
1034 bool rpmb_ioctl;
1035 u8 **ext_csd;
1036 u32 status;
1037 int ret;
1038 int i;
1040 mq_rq = req_to_mmc_queue_req(req);
1041 rpmb_ioctl = (mq_rq->drv_op == MMC_DRV_OP_IOCTL_RPMB);
1043 switch (mq_rq->drv_op) {
1044 case MMC_DRV_OP_IOCTL:
1045 case MMC_DRV_OP_IOCTL_RPMB:
1046 idata = mq_rq->drv_op_data;
1047 for (i = 0, ret = 0; i < mq_rq->ioc_count; i++) {
1048 ret = __mmc_blk_ioctl_cmd(card, md, idata[i]);
1049 if (ret)
1050 break;
1052 /* Always switch back to main area after RPMB access */
1053 if (rpmb_ioctl)
1054 mmc_blk_part_switch(card, 0);
1055 break;
1056 case MMC_DRV_OP_BOOT_WP:
1057 ret = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL, EXT_CSD_BOOT_WP,
1058 card->ext_csd.boot_ro_lock |
1059 EXT_CSD_BOOT_WP_B_PWR_WP_EN,
1060 card->ext_csd.part_time);
1061 if (ret)
1062 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1063 md->disk->disk_name, ret);
1064 else
1065 card->ext_csd.boot_ro_lock |=
1066 EXT_CSD_BOOT_WP_B_PWR_WP_EN;
1067 break;
1068 case MMC_DRV_OP_GET_CARD_STATUS:
1069 ret = mmc_send_status(card, &status);
1070 if (!ret)
1071 ret = status;
1072 break;
1073 case MMC_DRV_OP_GET_EXT_CSD:
1074 ext_csd = mq_rq->drv_op_data;
1075 ret = mmc_get_ext_csd(card, ext_csd);
1076 break;
1077 default:
1078 pr_err("%s: unknown driver specific operation\n",
1079 md->disk->disk_name);
1080 ret = -EINVAL;
1081 break;
1083 mq_rq->drv_op_result = ret;
1084 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1087 static void mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
1089 struct mmc_blk_data *md = mq->blkdata;
1090 struct mmc_card *card = md->queue.card;
1091 unsigned int from, nr, arg;
1092 int err = 0, type = MMC_BLK_DISCARD;
1093 blk_status_t status = BLK_STS_OK;
1095 if (!mmc_can_erase(card)) {
1096 status = BLK_STS_NOTSUPP;
1097 goto fail;
1100 from = blk_rq_pos(req);
1101 nr = blk_rq_sectors(req);
1103 if (mmc_can_discard(card))
1104 arg = MMC_DISCARD_ARG;
1105 else if (mmc_can_trim(card))
1106 arg = MMC_TRIM_ARG;
1107 else
1108 arg = MMC_ERASE_ARG;
1109 do {
1110 err = 0;
1111 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1112 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1113 INAND_CMD38_ARG_EXT_CSD,
1114 arg == MMC_TRIM_ARG ?
1115 INAND_CMD38_ARG_TRIM :
1116 INAND_CMD38_ARG_ERASE,
1119 if (!err)
1120 err = mmc_erase(card, from, nr, arg);
1121 } while (err == -EIO && !mmc_blk_reset(md, card->host, type));
1122 if (err)
1123 status = BLK_STS_IOERR;
1124 else
1125 mmc_blk_reset_success(md, type);
1126 fail:
1127 blk_mq_end_request(req, status);
1130 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
1131 struct request *req)
1133 struct mmc_blk_data *md = mq->blkdata;
1134 struct mmc_card *card = md->queue.card;
1135 unsigned int from, nr, arg;
1136 int err = 0, type = MMC_BLK_SECDISCARD;
1137 blk_status_t status = BLK_STS_OK;
1139 if (!(mmc_can_secure_erase_trim(card))) {
1140 status = BLK_STS_NOTSUPP;
1141 goto out;
1144 from = blk_rq_pos(req);
1145 nr = blk_rq_sectors(req);
1147 if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
1148 arg = MMC_SECURE_TRIM1_ARG;
1149 else
1150 arg = MMC_SECURE_ERASE_ARG;
1152 retry:
1153 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1154 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1155 INAND_CMD38_ARG_EXT_CSD,
1156 arg == MMC_SECURE_TRIM1_ARG ?
1157 INAND_CMD38_ARG_SECTRIM1 :
1158 INAND_CMD38_ARG_SECERASE,
1160 if (err)
1161 goto out_retry;
1164 err = mmc_erase(card, from, nr, arg);
1165 if (err == -EIO)
1166 goto out_retry;
1167 if (err) {
1168 status = BLK_STS_IOERR;
1169 goto out;
1172 if (arg == MMC_SECURE_TRIM1_ARG) {
1173 if (card->quirks & MMC_QUIRK_INAND_CMD38) {
1174 err = mmc_switch(card, EXT_CSD_CMD_SET_NORMAL,
1175 INAND_CMD38_ARG_EXT_CSD,
1176 INAND_CMD38_ARG_SECTRIM2,
1178 if (err)
1179 goto out_retry;
1182 err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
1183 if (err == -EIO)
1184 goto out_retry;
1185 if (err) {
1186 status = BLK_STS_IOERR;
1187 goto out;
1191 out_retry:
1192 if (err && !mmc_blk_reset(md, card->host, type))
1193 goto retry;
1194 if (!err)
1195 mmc_blk_reset_success(md, type);
1196 out:
1197 blk_mq_end_request(req, status);
1200 static void mmc_blk_issue_flush(struct mmc_queue *mq, struct request *req)
1202 struct mmc_blk_data *md = mq->blkdata;
1203 struct mmc_card *card = md->queue.card;
1204 int ret = 0;
1206 ret = mmc_flush_cache(card);
1207 blk_mq_end_request(req, ret ? BLK_STS_IOERR : BLK_STS_OK);
1211 * Reformat current write as a reliable write, supporting
1212 * both legacy and the enhanced reliable write MMC cards.
1213 * In each transfer we'll handle only as much as a single
1214 * reliable write can handle, thus finish the request in
1215 * partial completions.
1217 static inline void mmc_apply_rel_rw(struct mmc_blk_request *brq,
1218 struct mmc_card *card,
1219 struct request *req)
1221 if (!(card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN)) {
1222 /* Legacy mode imposes restrictions on transfers. */
1223 if (!IS_ALIGNED(blk_rq_pos(req), card->ext_csd.rel_sectors))
1224 brq->data.blocks = 1;
1226 if (brq->data.blocks > card->ext_csd.rel_sectors)
1227 brq->data.blocks = card->ext_csd.rel_sectors;
1228 else if (brq->data.blocks < card->ext_csd.rel_sectors)
1229 brq->data.blocks = 1;
1233 #define CMD_ERRORS_EXCL_OOR \
1234 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1235 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1236 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1237 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1238 R1_CC_ERROR | /* Card controller error */ \
1239 R1_ERROR) /* General/unknown error */
1241 #define CMD_ERRORS \
1242 (CMD_ERRORS_EXCL_OOR | \
1243 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1245 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1247 u32 val;
1250 * Per the SD specification(physical layer version 4.10)[1],
1251 * section 4.3.3, it explicitly states that "When the last
1252 * block of user area is read using CMD18, the host should
1253 * ignore OUT_OF_RANGE error that may occur even the sequence
1254 * is correct". And JESD84-B51 for eMMC also has a similar
1255 * statement on section 6.8.3.
1257 * Multiple block read/write could be done by either predefined
1258 * method, namely CMD23, or open-ending mode. For open-ending mode,
1259 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1261 * However the spec[1] doesn't tell us whether we should also
1262 * ignore that for predefined method. But per the spec[1], section
1263 * 4.15 Set Block Count Command, it says"If illegal block count
1264 * is set, out of range error will be indicated during read/write
1265 * operation (For example, data transfer is stopped at user area
1266 * boundary)." In another word, we could expect a out of range error
1267 * in the response for the following CMD18/25. And if argument of
1268 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1269 * we could also expect to get a -ETIMEDOUT or any error number from
1270 * the host drivers due to missing data response(for write)/data(for
1271 * read), as the cards will stop the data transfer by itself per the
1272 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1275 if (!brq->stop.error) {
1276 bool oor_with_open_end;
1277 /* If there is no error yet, check R1 response */
1279 val = brq->stop.resp[0] & CMD_ERRORS;
1280 oor_with_open_end = val & R1_OUT_OF_RANGE && !brq->mrq.sbc;
1282 if (val && !oor_with_open_end)
1283 brq->stop.error = -EIO;
1287 static void mmc_blk_data_prep(struct mmc_queue *mq, struct mmc_queue_req *mqrq,
1288 int disable_multi, bool *do_rel_wr_p,
1289 bool *do_data_tag_p)
1291 struct mmc_blk_data *md = mq->blkdata;
1292 struct mmc_card *card = md->queue.card;
1293 struct mmc_blk_request *brq = &mqrq->brq;
1294 struct request *req = mmc_queue_req_to_req(mqrq);
1295 bool do_rel_wr, do_data_tag;
1298 * Reliable writes are used to implement Forced Unit Access and
1299 * are supported only on MMCs.
1301 do_rel_wr = (req->cmd_flags & REQ_FUA) &&
1302 rq_data_dir(req) == WRITE &&
1303 (md->flags & MMC_BLK_REL_WR);
1305 memset(brq, 0, sizeof(struct mmc_blk_request));
1307 brq->mrq.data = &brq->data;
1308 brq->mrq.tag = req->tag;
1310 brq->stop.opcode = MMC_STOP_TRANSMISSION;
1311 brq->stop.arg = 0;
1313 if (rq_data_dir(req) == READ) {
1314 brq->data.flags = MMC_DATA_READ;
1315 brq->stop.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
1316 } else {
1317 brq->data.flags = MMC_DATA_WRITE;
1318 brq->stop.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
1321 brq->data.blksz = 512;
1322 brq->data.blocks = blk_rq_sectors(req);
1323 brq->data.blk_addr = blk_rq_pos(req);
1326 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1327 * The eMMC will give "high" priority tasks priority over "simple"
1328 * priority tasks. Here we always set "simple" priority by not setting
1329 * MMC_DATA_PRIO.
1333 * The block layer doesn't support all sector count
1334 * restrictions, so we need to be prepared for too big
1335 * requests.
1337 if (brq->data.blocks > card->host->max_blk_count)
1338 brq->data.blocks = card->host->max_blk_count;
1340 if (brq->data.blocks > 1) {
1342 * Some SD cards in SPI mode return a CRC error or even lock up
1343 * completely when trying to read the last block using a
1344 * multiblock read command.
1346 if (mmc_host_is_spi(card->host) && (rq_data_dir(req) == READ) &&
1347 (blk_rq_pos(req) + blk_rq_sectors(req) ==
1348 get_capacity(md->disk)))
1349 brq->data.blocks--;
1352 * After a read error, we redo the request one sector
1353 * at a time in order to accurately determine which
1354 * sectors can be read successfully.
1356 if (disable_multi)
1357 brq->data.blocks = 1;
1360 * Some controllers have HW issues while operating
1361 * in multiple I/O mode
1363 if (card->host->ops->multi_io_quirk)
1364 brq->data.blocks = card->host->ops->multi_io_quirk(card,
1365 (rq_data_dir(req) == READ) ?
1366 MMC_DATA_READ : MMC_DATA_WRITE,
1367 brq->data.blocks);
1370 if (do_rel_wr) {
1371 mmc_apply_rel_rw(brq, card, req);
1372 brq->data.flags |= MMC_DATA_REL_WR;
1376 * Data tag is used only during writing meta data to speed
1377 * up write and any subsequent read of this meta data
1379 do_data_tag = card->ext_csd.data_tag_unit_size &&
1380 (req->cmd_flags & REQ_META) &&
1381 (rq_data_dir(req) == WRITE) &&
1382 ((brq->data.blocks * brq->data.blksz) >=
1383 card->ext_csd.data_tag_unit_size);
1385 if (do_data_tag)
1386 brq->data.flags |= MMC_DATA_DAT_TAG;
1388 mmc_set_data_timeout(&brq->data, card);
1390 brq->data.sg = mqrq->sg;
1391 brq->data.sg_len = mmc_queue_map_sg(mq, mqrq);
1394 * Adjust the sg list so it is the same size as the
1395 * request.
1397 if (brq->data.blocks != blk_rq_sectors(req)) {
1398 int i, data_size = brq->data.blocks << 9;
1399 struct scatterlist *sg;
1401 for_each_sg(brq->data.sg, sg, brq->data.sg_len, i) {
1402 data_size -= sg->length;
1403 if (data_size <= 0) {
1404 sg->length += data_size;
1405 i++;
1406 break;
1409 brq->data.sg_len = i;
1412 if (do_rel_wr_p)
1413 *do_rel_wr_p = do_rel_wr;
1415 if (do_data_tag_p)
1416 *do_data_tag_p = do_data_tag;
1419 #define MMC_CQE_RETRIES 2
1421 static void mmc_blk_cqe_complete_rq(struct mmc_queue *mq, struct request *req)
1423 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1424 struct mmc_request *mrq = &mqrq->brq.mrq;
1425 struct request_queue *q = req->q;
1426 struct mmc_host *host = mq->card->host;
1427 enum mmc_issue_type issue_type = mmc_issue_type(mq, req);
1428 unsigned long flags;
1429 bool put_card;
1430 int err;
1432 mmc_cqe_post_req(host, mrq);
1434 if (mrq->cmd && mrq->cmd->error)
1435 err = mrq->cmd->error;
1436 else if (mrq->data && mrq->data->error)
1437 err = mrq->data->error;
1438 else
1439 err = 0;
1441 if (err) {
1442 if (mqrq->retries++ < MMC_CQE_RETRIES)
1443 blk_mq_requeue_request(req, true);
1444 else
1445 blk_mq_end_request(req, BLK_STS_IOERR);
1446 } else if (mrq->data) {
1447 if (blk_update_request(req, BLK_STS_OK, mrq->data->bytes_xfered))
1448 blk_mq_requeue_request(req, true);
1449 else
1450 __blk_mq_end_request(req, BLK_STS_OK);
1451 } else {
1452 blk_mq_end_request(req, BLK_STS_OK);
1455 spin_lock_irqsave(q->queue_lock, flags);
1457 mq->in_flight[issue_type] -= 1;
1459 put_card = (mmc_tot_in_flight(mq) == 0);
1461 mmc_cqe_check_busy(mq);
1463 spin_unlock_irqrestore(q->queue_lock, flags);
1465 if (!mq->cqe_busy)
1466 blk_mq_run_hw_queues(q, true);
1468 if (put_card)
1469 mmc_put_card(mq->card, &mq->ctx);
1472 void mmc_blk_cqe_recovery(struct mmc_queue *mq)
1474 struct mmc_card *card = mq->card;
1475 struct mmc_host *host = card->host;
1476 int err;
1478 pr_debug("%s: CQE recovery start\n", mmc_hostname(host));
1480 err = mmc_cqe_recovery(host);
1481 if (err)
1482 mmc_blk_reset(mq->blkdata, host, MMC_BLK_CQE_RECOVERY);
1483 else
1484 mmc_blk_reset_success(mq->blkdata, MMC_BLK_CQE_RECOVERY);
1486 pr_debug("%s: CQE recovery done\n", mmc_hostname(host));
1489 static void mmc_blk_cqe_req_done(struct mmc_request *mrq)
1491 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
1492 brq.mrq);
1493 struct request *req = mmc_queue_req_to_req(mqrq);
1494 struct request_queue *q = req->q;
1495 struct mmc_queue *mq = q->queuedata;
1498 * Block layer timeouts race with completions which means the normal
1499 * completion path cannot be used during recovery.
1501 if (mq->in_recovery)
1502 mmc_blk_cqe_complete_rq(mq, req);
1503 else
1504 blk_mq_complete_request(req);
1507 static int mmc_blk_cqe_start_req(struct mmc_host *host, struct mmc_request *mrq)
1509 mrq->done = mmc_blk_cqe_req_done;
1510 mrq->recovery_notifier = mmc_cqe_recovery_notifier;
1512 return mmc_cqe_start_req(host, mrq);
1515 static struct mmc_request *mmc_blk_cqe_prep_dcmd(struct mmc_queue_req *mqrq,
1516 struct request *req)
1518 struct mmc_blk_request *brq = &mqrq->brq;
1520 memset(brq, 0, sizeof(*brq));
1522 brq->mrq.cmd = &brq->cmd;
1523 brq->mrq.tag = req->tag;
1525 return &brq->mrq;
1528 static int mmc_blk_cqe_issue_flush(struct mmc_queue *mq, struct request *req)
1530 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1531 struct mmc_request *mrq = mmc_blk_cqe_prep_dcmd(mqrq, req);
1533 mrq->cmd->opcode = MMC_SWITCH;
1534 mrq->cmd->arg = (MMC_SWITCH_MODE_WRITE_BYTE << 24) |
1535 (EXT_CSD_FLUSH_CACHE << 16) |
1536 (1 << 8) |
1537 EXT_CSD_CMD_SET_NORMAL;
1538 mrq->cmd->flags = MMC_CMD_AC | MMC_RSP_R1B;
1540 return mmc_blk_cqe_start_req(mq->card->host, mrq);
1543 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue *mq, struct request *req)
1545 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1547 mmc_blk_data_prep(mq, mqrq, 0, NULL, NULL);
1549 return mmc_blk_cqe_start_req(mq->card->host, &mqrq->brq.mrq);
1552 static void mmc_blk_rw_rq_prep(struct mmc_queue_req *mqrq,
1553 struct mmc_card *card,
1554 int disable_multi,
1555 struct mmc_queue *mq)
1557 u32 readcmd, writecmd;
1558 struct mmc_blk_request *brq = &mqrq->brq;
1559 struct request *req = mmc_queue_req_to_req(mqrq);
1560 struct mmc_blk_data *md = mq->blkdata;
1561 bool do_rel_wr, do_data_tag;
1563 mmc_blk_data_prep(mq, mqrq, disable_multi, &do_rel_wr, &do_data_tag);
1565 brq->mrq.cmd = &brq->cmd;
1567 brq->cmd.arg = blk_rq_pos(req);
1568 if (!mmc_card_blockaddr(card))
1569 brq->cmd.arg <<= 9;
1570 brq->cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_ADTC;
1572 if (brq->data.blocks > 1 || do_rel_wr) {
1573 /* SPI multiblock writes terminate using a special
1574 * token, not a STOP_TRANSMISSION request.
1576 if (!mmc_host_is_spi(card->host) ||
1577 rq_data_dir(req) == READ)
1578 brq->mrq.stop = &brq->stop;
1579 readcmd = MMC_READ_MULTIPLE_BLOCK;
1580 writecmd = MMC_WRITE_MULTIPLE_BLOCK;
1581 } else {
1582 brq->mrq.stop = NULL;
1583 readcmd = MMC_READ_SINGLE_BLOCK;
1584 writecmd = MMC_WRITE_BLOCK;
1586 brq->cmd.opcode = rq_data_dir(req) == READ ? readcmd : writecmd;
1589 * Pre-defined multi-block transfers are preferable to
1590 * open ended-ones (and necessary for reliable writes).
1591 * However, it is not sufficient to just send CMD23,
1592 * and avoid the final CMD12, as on an error condition
1593 * CMD12 (stop) needs to be sent anyway. This, coupled
1594 * with Auto-CMD23 enhancements provided by some
1595 * hosts, means that the complexity of dealing
1596 * with this is best left to the host. If CMD23 is
1597 * supported by card and host, we'll fill sbc in and let
1598 * the host deal with handling it correctly. This means
1599 * that for hosts that don't expose MMC_CAP_CMD23, no
1600 * change of behavior will be observed.
1602 * N.B: Some MMC cards experience perf degradation.
1603 * We'll avoid using CMD23-bounded multiblock writes for
1604 * these, while retaining features like reliable writes.
1606 if ((md->flags & MMC_BLK_CMD23) && mmc_op_multi(brq->cmd.opcode) &&
1607 (do_rel_wr || !(card->quirks & MMC_QUIRK_BLK_NO_CMD23) ||
1608 do_data_tag)) {
1609 brq->sbc.opcode = MMC_SET_BLOCK_COUNT;
1610 brq->sbc.arg = brq->data.blocks |
1611 (do_rel_wr ? (1 << 31) : 0) |
1612 (do_data_tag ? (1 << 29) : 0);
1613 brq->sbc.flags = MMC_RSP_R1 | MMC_CMD_AC;
1614 brq->mrq.sbc = &brq->sbc;
1618 #define MMC_MAX_RETRIES 5
1619 #define MMC_DATA_RETRIES 2
1620 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1622 static int mmc_blk_send_stop(struct mmc_card *card, unsigned int timeout)
1624 struct mmc_command cmd = {
1625 .opcode = MMC_STOP_TRANSMISSION,
1626 .flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC,
1627 /* Some hosts wait for busy anyway, so provide a busy timeout */
1628 .busy_timeout = timeout,
1631 return mmc_wait_for_cmd(card->host, &cmd, 5);
1634 static int mmc_blk_fix_state(struct mmc_card *card, struct request *req)
1636 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1637 struct mmc_blk_request *brq = &mqrq->brq;
1638 unsigned int timeout = mmc_blk_data_timeout_ms(card->host, &brq->data);
1639 int err;
1641 mmc_retune_hold_now(card->host);
1643 mmc_blk_send_stop(card, timeout);
1645 err = card_busy_detect(card, timeout, NULL);
1647 mmc_retune_release(card->host);
1649 return err;
1652 #define MMC_READ_SINGLE_RETRIES 2
1654 /* Single sector read during recovery */
1655 static void mmc_blk_read_single(struct mmc_queue *mq, struct request *req)
1657 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1658 struct mmc_request *mrq = &mqrq->brq.mrq;
1659 struct mmc_card *card = mq->card;
1660 struct mmc_host *host = card->host;
1661 blk_status_t error = BLK_STS_OK;
1662 int retries = 0;
1664 do {
1665 u32 status;
1666 int err;
1668 mmc_blk_rw_rq_prep(mqrq, card, 1, mq);
1670 mmc_wait_for_req(host, mrq);
1672 err = mmc_send_status(card, &status);
1673 if (err)
1674 goto error_exit;
1676 if (!mmc_host_is_spi(host) &&
1677 !mmc_blk_in_tran_state(status)) {
1678 err = mmc_blk_fix_state(card, req);
1679 if (err)
1680 goto error_exit;
1683 if (mrq->cmd->error && retries++ < MMC_READ_SINGLE_RETRIES)
1684 continue;
1686 retries = 0;
1688 if (mrq->cmd->error ||
1689 mrq->data->error ||
1690 (!mmc_host_is_spi(host) &&
1691 (mrq->cmd->resp[0] & CMD_ERRORS || status & CMD_ERRORS)))
1692 error = BLK_STS_IOERR;
1693 else
1694 error = BLK_STS_OK;
1696 } while (blk_update_request(req, error, 512));
1698 return;
1700 error_exit:
1701 mrq->data->bytes_xfered = 0;
1702 blk_update_request(req, BLK_STS_IOERR, 512);
1703 /* Let it try the remaining request again */
1704 if (mqrq->retries > MMC_MAX_RETRIES - 1)
1705 mqrq->retries = MMC_MAX_RETRIES - 1;
1708 static inline bool mmc_blk_oor_valid(struct mmc_blk_request *brq)
1710 return !!brq->mrq.sbc;
1713 static inline u32 mmc_blk_stop_err_bits(struct mmc_blk_request *brq)
1715 return mmc_blk_oor_valid(brq) ? CMD_ERRORS : CMD_ERRORS_EXCL_OOR;
1719 * Check for errors the host controller driver might not have seen such as
1720 * response mode errors or invalid card state.
1722 static bool mmc_blk_status_error(struct request *req, u32 status)
1724 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1725 struct mmc_blk_request *brq = &mqrq->brq;
1726 struct mmc_queue *mq = req->q->queuedata;
1727 u32 stop_err_bits;
1729 if (mmc_host_is_spi(mq->card->host))
1730 return false;
1732 stop_err_bits = mmc_blk_stop_err_bits(brq);
1734 return brq->cmd.resp[0] & CMD_ERRORS ||
1735 brq->stop.resp[0] & stop_err_bits ||
1736 status & stop_err_bits ||
1737 (rq_data_dir(req) == WRITE && !mmc_blk_in_tran_state(status));
1740 static inline bool mmc_blk_cmd_started(struct mmc_blk_request *brq)
1742 return !brq->sbc.error && !brq->cmd.error &&
1743 !(brq->cmd.resp[0] & CMD_ERRORS);
1747 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1748 * policy:
1749 * 1. A request that has transferred at least some data is considered
1750 * successful and will be requeued if there is remaining data to
1751 * transfer.
1752 * 2. Otherwise the number of retries is incremented and the request
1753 * will be requeued if there are remaining retries.
1754 * 3. Otherwise the request will be errored out.
1755 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1756 * mqrq->retries. So there are only 4 possible actions here:
1757 * 1. do not accept the bytes_xfered value i.e. set it to zero
1758 * 2. change mqrq->retries to determine the number of retries
1759 * 3. try to reset the card
1760 * 4. read one sector at a time
1762 static void mmc_blk_mq_rw_recovery(struct mmc_queue *mq, struct request *req)
1764 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1765 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1766 struct mmc_blk_request *brq = &mqrq->brq;
1767 struct mmc_blk_data *md = mq->blkdata;
1768 struct mmc_card *card = mq->card;
1769 u32 status;
1770 u32 blocks;
1771 int err;
1774 * Some errors the host driver might not have seen. Set the number of
1775 * bytes transferred to zero in that case.
1777 err = __mmc_send_status(card, &status, 0);
1778 if (err || mmc_blk_status_error(req, status))
1779 brq->data.bytes_xfered = 0;
1781 mmc_retune_release(card->host);
1784 * Try again to get the status. This also provides an opportunity for
1785 * re-tuning.
1787 if (err)
1788 err = __mmc_send_status(card, &status, 0);
1791 * Nothing more to do after the number of bytes transferred has been
1792 * updated and there is no card.
1794 if (err && mmc_detect_card_removed(card->host))
1795 return;
1797 /* Try to get back to "tran" state */
1798 if (!mmc_host_is_spi(mq->card->host) &&
1799 (err || !mmc_blk_in_tran_state(status)))
1800 err = mmc_blk_fix_state(mq->card, req);
1803 * Special case for SD cards where the card might record the number of
1804 * blocks written.
1806 if (!err && mmc_blk_cmd_started(brq) && mmc_card_sd(card) &&
1807 rq_data_dir(req) == WRITE) {
1808 if (mmc_sd_num_wr_blocks(card, &blocks))
1809 brq->data.bytes_xfered = 0;
1810 else
1811 brq->data.bytes_xfered = blocks << 9;
1814 /* Reset if the card is in a bad state */
1815 if (!mmc_host_is_spi(mq->card->host) &&
1816 err && mmc_blk_reset(md, card->host, type)) {
1817 pr_err("%s: recovery failed!\n", req->rq_disk->disk_name);
1818 mqrq->retries = MMC_NO_RETRIES;
1819 return;
1823 * If anything was done, just return and if there is anything remaining
1824 * on the request it will get requeued.
1826 if (brq->data.bytes_xfered)
1827 return;
1829 /* Reset before last retry */
1830 if (mqrq->retries + 1 == MMC_MAX_RETRIES)
1831 mmc_blk_reset(md, card->host, type);
1833 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1834 if (brq->sbc.error || brq->cmd.error)
1835 return;
1837 /* Reduce the remaining retries for data errors */
1838 if (mqrq->retries < MMC_MAX_RETRIES - MMC_DATA_RETRIES) {
1839 mqrq->retries = MMC_MAX_RETRIES - MMC_DATA_RETRIES;
1840 return;
1843 /* FIXME: Missing single sector read for large sector size */
1844 if (!mmc_large_sector(card) && rq_data_dir(req) == READ &&
1845 brq->data.blocks > 1) {
1846 /* Read one sector at a time */
1847 mmc_blk_read_single(mq, req);
1848 return;
1852 static inline bool mmc_blk_rq_error(struct mmc_blk_request *brq)
1854 mmc_blk_eval_resp_error(brq);
1856 return brq->sbc.error || brq->cmd.error || brq->stop.error ||
1857 brq->data.error || brq->cmd.resp[0] & CMD_ERRORS;
1860 static int mmc_blk_card_busy(struct mmc_card *card, struct request *req)
1862 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1863 u32 status = 0;
1864 int err;
1866 if (mmc_host_is_spi(card->host) || rq_data_dir(req) == READ)
1867 return 0;
1869 err = card_busy_detect(card, MMC_BLK_TIMEOUT_MS, &status);
1872 * Do not assume data transferred correctly if there are any error bits
1873 * set.
1875 if (status & mmc_blk_stop_err_bits(&mqrq->brq)) {
1876 mqrq->brq.data.bytes_xfered = 0;
1877 err = err ? err : -EIO;
1880 /* Copy the exception bit so it will be seen later on */
1881 if (mmc_card_mmc(card) && status & R1_EXCEPTION_EVENT)
1882 mqrq->brq.cmd.resp[0] |= R1_EXCEPTION_EVENT;
1884 return err;
1887 static inline void mmc_blk_rw_reset_success(struct mmc_queue *mq,
1888 struct request *req)
1890 int type = rq_data_dir(req) == READ ? MMC_BLK_READ : MMC_BLK_WRITE;
1892 mmc_blk_reset_success(mq->blkdata, type);
1895 static void mmc_blk_mq_complete_rq(struct mmc_queue *mq, struct request *req)
1897 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1898 unsigned int nr_bytes = mqrq->brq.data.bytes_xfered;
1900 if (nr_bytes) {
1901 if (blk_update_request(req, BLK_STS_OK, nr_bytes))
1902 blk_mq_requeue_request(req, true);
1903 else
1904 __blk_mq_end_request(req, BLK_STS_OK);
1905 } else if (!blk_rq_bytes(req)) {
1906 __blk_mq_end_request(req, BLK_STS_IOERR);
1907 } else if (mqrq->retries++ < MMC_MAX_RETRIES) {
1908 blk_mq_requeue_request(req, true);
1909 } else {
1910 if (mmc_card_removed(mq->card))
1911 req->rq_flags |= RQF_QUIET;
1912 blk_mq_end_request(req, BLK_STS_IOERR);
1916 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue *mq,
1917 struct mmc_queue_req *mqrq)
1919 return mmc_card_mmc(mq->card) && !mmc_host_is_spi(mq->card->host) &&
1920 (mqrq->brq.cmd.resp[0] & R1_EXCEPTION_EVENT ||
1921 mqrq->brq.stop.resp[0] & R1_EXCEPTION_EVENT);
1924 static void mmc_blk_urgent_bkops(struct mmc_queue *mq,
1925 struct mmc_queue_req *mqrq)
1927 if (mmc_blk_urgent_bkops_needed(mq, mqrq))
1928 mmc_start_bkops(mq->card, true);
1931 void mmc_blk_mq_complete(struct request *req)
1933 struct mmc_queue *mq = req->q->queuedata;
1935 if (mq->use_cqe)
1936 mmc_blk_cqe_complete_rq(mq, req);
1937 else
1938 mmc_blk_mq_complete_rq(mq, req);
1941 static void mmc_blk_mq_poll_completion(struct mmc_queue *mq,
1942 struct request *req)
1944 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1945 struct mmc_host *host = mq->card->host;
1947 if (mmc_blk_rq_error(&mqrq->brq) ||
1948 mmc_blk_card_busy(mq->card, req)) {
1949 mmc_blk_mq_rw_recovery(mq, req);
1950 } else {
1951 mmc_blk_rw_reset_success(mq, req);
1952 mmc_retune_release(host);
1955 mmc_blk_urgent_bkops(mq, mqrq);
1958 static void mmc_blk_mq_dec_in_flight(struct mmc_queue *mq, struct request *req)
1960 struct request_queue *q = req->q;
1961 unsigned long flags;
1962 bool put_card;
1964 spin_lock_irqsave(q->queue_lock, flags);
1966 mq->in_flight[mmc_issue_type(mq, req)] -= 1;
1968 put_card = (mmc_tot_in_flight(mq) == 0);
1970 spin_unlock_irqrestore(q->queue_lock, flags);
1972 if (put_card)
1973 mmc_put_card(mq->card, &mq->ctx);
1976 static void mmc_blk_mq_post_req(struct mmc_queue *mq, struct request *req)
1978 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
1979 struct mmc_request *mrq = &mqrq->brq.mrq;
1980 struct mmc_host *host = mq->card->host;
1982 mmc_post_req(host, mrq, 0);
1985 * Block layer timeouts race with completions which means the normal
1986 * completion path cannot be used during recovery.
1988 if (mq->in_recovery)
1989 mmc_blk_mq_complete_rq(mq, req);
1990 else
1991 blk_mq_complete_request(req);
1993 mmc_blk_mq_dec_in_flight(mq, req);
1996 void mmc_blk_mq_recovery(struct mmc_queue *mq)
1998 struct request *req = mq->recovery_req;
1999 struct mmc_host *host = mq->card->host;
2000 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2002 mq->recovery_req = NULL;
2003 mq->rw_wait = false;
2005 if (mmc_blk_rq_error(&mqrq->brq)) {
2006 mmc_retune_hold_now(host);
2007 mmc_blk_mq_rw_recovery(mq, req);
2010 mmc_blk_urgent_bkops(mq, mqrq);
2012 mmc_blk_mq_post_req(mq, req);
2015 static void mmc_blk_mq_complete_prev_req(struct mmc_queue *mq,
2016 struct request **prev_req)
2018 if (mmc_host_done_complete(mq->card->host))
2019 return;
2021 mutex_lock(&mq->complete_lock);
2023 if (!mq->complete_req)
2024 goto out_unlock;
2026 mmc_blk_mq_poll_completion(mq, mq->complete_req);
2028 if (prev_req)
2029 *prev_req = mq->complete_req;
2030 else
2031 mmc_blk_mq_post_req(mq, mq->complete_req);
2033 mq->complete_req = NULL;
2035 out_unlock:
2036 mutex_unlock(&mq->complete_lock);
2039 void mmc_blk_mq_complete_work(struct work_struct *work)
2041 struct mmc_queue *mq = container_of(work, struct mmc_queue,
2042 complete_work);
2044 mmc_blk_mq_complete_prev_req(mq, NULL);
2047 static void mmc_blk_mq_req_done(struct mmc_request *mrq)
2049 struct mmc_queue_req *mqrq = container_of(mrq, struct mmc_queue_req,
2050 brq.mrq);
2051 struct request *req = mmc_queue_req_to_req(mqrq);
2052 struct request_queue *q = req->q;
2053 struct mmc_queue *mq = q->queuedata;
2054 struct mmc_host *host = mq->card->host;
2055 unsigned long flags;
2057 if (!mmc_host_done_complete(host)) {
2058 bool waiting;
2061 * We cannot complete the request in this context, so record
2062 * that there is a request to complete, and that a following
2063 * request does not need to wait (although it does need to
2064 * complete complete_req first).
2066 spin_lock_irqsave(q->queue_lock, flags);
2067 mq->complete_req = req;
2068 mq->rw_wait = false;
2069 waiting = mq->waiting;
2070 spin_unlock_irqrestore(q->queue_lock, flags);
2073 * If 'waiting' then the waiting task will complete this
2074 * request, otherwise queue a work to do it. Note that
2075 * complete_work may still race with the dispatch of a following
2076 * request.
2078 if (waiting)
2079 wake_up(&mq->wait);
2080 else
2081 queue_work(mq->card->complete_wq, &mq->complete_work);
2083 return;
2086 /* Take the recovery path for errors or urgent background operations */
2087 if (mmc_blk_rq_error(&mqrq->brq) ||
2088 mmc_blk_urgent_bkops_needed(mq, mqrq)) {
2089 spin_lock_irqsave(q->queue_lock, flags);
2090 mq->recovery_needed = true;
2091 mq->recovery_req = req;
2092 spin_unlock_irqrestore(q->queue_lock, flags);
2093 wake_up(&mq->wait);
2094 schedule_work(&mq->recovery_work);
2095 return;
2098 mmc_blk_rw_reset_success(mq, req);
2100 mq->rw_wait = false;
2101 wake_up(&mq->wait);
2103 mmc_blk_mq_post_req(mq, req);
2106 static bool mmc_blk_rw_wait_cond(struct mmc_queue *mq, int *err)
2108 struct request_queue *q = mq->queue;
2109 unsigned long flags;
2110 bool done;
2113 * Wait while there is another request in progress, but not if recovery
2114 * is needed. Also indicate whether there is a request waiting to start.
2116 spin_lock_irqsave(q->queue_lock, flags);
2117 if (mq->recovery_needed) {
2118 *err = -EBUSY;
2119 done = true;
2120 } else {
2121 done = !mq->rw_wait;
2123 mq->waiting = !done;
2124 spin_unlock_irqrestore(q->queue_lock, flags);
2126 return done;
2129 static int mmc_blk_rw_wait(struct mmc_queue *mq, struct request **prev_req)
2131 int err = 0;
2133 wait_event(mq->wait, mmc_blk_rw_wait_cond(mq, &err));
2135 /* Always complete the previous request if there is one */
2136 mmc_blk_mq_complete_prev_req(mq, prev_req);
2138 return err;
2141 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue *mq,
2142 struct request *req)
2144 struct mmc_queue_req *mqrq = req_to_mmc_queue_req(req);
2145 struct mmc_host *host = mq->card->host;
2146 struct request *prev_req = NULL;
2147 int err = 0;
2149 mmc_blk_rw_rq_prep(mqrq, mq->card, 0, mq);
2151 mqrq->brq.mrq.done = mmc_blk_mq_req_done;
2153 mmc_pre_req(host, &mqrq->brq.mrq);
2155 err = mmc_blk_rw_wait(mq, &prev_req);
2156 if (err)
2157 goto out_post_req;
2159 mq->rw_wait = true;
2161 err = mmc_start_request(host, &mqrq->brq.mrq);
2163 if (prev_req)
2164 mmc_blk_mq_post_req(mq, prev_req);
2166 if (err)
2167 mq->rw_wait = false;
2169 /* Release re-tuning here where there is no synchronization required */
2170 if (err || mmc_host_done_complete(host))
2171 mmc_retune_release(host);
2173 out_post_req:
2174 if (err)
2175 mmc_post_req(host, &mqrq->brq.mrq, err);
2177 return err;
2180 static int mmc_blk_wait_for_idle(struct mmc_queue *mq, struct mmc_host *host)
2182 if (mq->use_cqe)
2183 return host->cqe_ops->cqe_wait_for_idle(host);
2185 return mmc_blk_rw_wait(mq, NULL);
2188 enum mmc_issued mmc_blk_mq_issue_rq(struct mmc_queue *mq, struct request *req)
2190 struct mmc_blk_data *md = mq->blkdata;
2191 struct mmc_card *card = md->queue.card;
2192 struct mmc_host *host = card->host;
2193 int ret;
2195 ret = mmc_blk_part_switch(card, md->part_type);
2196 if (ret)
2197 return MMC_REQ_FAILED_TO_START;
2199 switch (mmc_issue_type(mq, req)) {
2200 case MMC_ISSUE_SYNC:
2201 ret = mmc_blk_wait_for_idle(mq, host);
2202 if (ret)
2203 return MMC_REQ_BUSY;
2204 switch (req_op(req)) {
2205 case REQ_OP_DRV_IN:
2206 case REQ_OP_DRV_OUT:
2207 mmc_blk_issue_drv_op(mq, req);
2208 break;
2209 case REQ_OP_DISCARD:
2210 mmc_blk_issue_discard_rq(mq, req);
2211 break;
2212 case REQ_OP_SECURE_ERASE:
2213 mmc_blk_issue_secdiscard_rq(mq, req);
2214 break;
2215 case REQ_OP_FLUSH:
2216 mmc_blk_issue_flush(mq, req);
2217 break;
2218 default:
2219 WARN_ON_ONCE(1);
2220 return MMC_REQ_FAILED_TO_START;
2222 return MMC_REQ_FINISHED;
2223 case MMC_ISSUE_DCMD:
2224 case MMC_ISSUE_ASYNC:
2225 switch (req_op(req)) {
2226 case REQ_OP_FLUSH:
2227 ret = mmc_blk_cqe_issue_flush(mq, req);
2228 break;
2229 case REQ_OP_READ:
2230 case REQ_OP_WRITE:
2231 if (mq->use_cqe)
2232 ret = mmc_blk_cqe_issue_rw_rq(mq, req);
2233 else
2234 ret = mmc_blk_mq_issue_rw_rq(mq, req);
2235 break;
2236 default:
2237 WARN_ON_ONCE(1);
2238 ret = -EINVAL;
2240 if (!ret)
2241 return MMC_REQ_STARTED;
2242 return ret == -EBUSY ? MMC_REQ_BUSY : MMC_REQ_FAILED_TO_START;
2243 default:
2244 WARN_ON_ONCE(1);
2245 return MMC_REQ_FAILED_TO_START;
2249 static inline int mmc_blk_readonly(struct mmc_card *card)
2251 return mmc_card_readonly(card) ||
2252 !(card->csd.cmdclass & CCC_BLOCK_WRITE);
2255 static struct mmc_blk_data *mmc_blk_alloc_req(struct mmc_card *card,
2256 struct device *parent,
2257 sector_t size,
2258 bool default_ro,
2259 const char *subname,
2260 int area_type)
2262 struct mmc_blk_data *md;
2263 int devidx, ret;
2265 devidx = ida_simple_get(&mmc_blk_ida, 0, max_devices, GFP_KERNEL);
2266 if (devidx < 0) {
2268 * We get -ENOSPC because there are no more any available
2269 * devidx. The reason may be that, either userspace haven't yet
2270 * unmounted the partitions, which postpones mmc_blk_release()
2271 * from being called, or the device has more partitions than
2272 * what we support.
2274 if (devidx == -ENOSPC)
2275 dev_err(mmc_dev(card->host),
2276 "no more device IDs available\n");
2278 return ERR_PTR(devidx);
2281 md = kzalloc(sizeof(struct mmc_blk_data), GFP_KERNEL);
2282 if (!md) {
2283 ret = -ENOMEM;
2284 goto out;
2287 md->area_type = area_type;
2290 * Set the read-only status based on the supported commands
2291 * and the write protect switch.
2293 md->read_only = mmc_blk_readonly(card);
2295 md->disk = alloc_disk(perdev_minors);
2296 if (md->disk == NULL) {
2297 ret = -ENOMEM;
2298 goto err_kfree;
2301 spin_lock_init(&md->lock);
2302 INIT_LIST_HEAD(&md->part);
2303 INIT_LIST_HEAD(&md->rpmbs);
2304 md->usage = 1;
2306 ret = mmc_init_queue(&md->queue, card, &md->lock, subname);
2307 if (ret)
2308 goto err_putdisk;
2310 md->queue.blkdata = md;
2313 * Keep an extra reference to the queue so that we can shutdown the
2314 * queue (i.e. call blk_cleanup_queue()) while there are still
2315 * references to the 'md'. The corresponding blk_put_queue() is in
2316 * mmc_blk_put().
2318 if (!blk_get_queue(md->queue.queue)) {
2319 mmc_cleanup_queue(&md->queue);
2320 ret = -ENODEV;
2321 goto err_putdisk;
2324 md->disk->major = MMC_BLOCK_MAJOR;
2325 md->disk->first_minor = devidx * perdev_minors;
2326 md->disk->fops = &mmc_bdops;
2327 md->disk->private_data = md;
2328 md->disk->queue = md->queue.queue;
2329 md->parent = parent;
2330 set_disk_ro(md->disk, md->read_only || default_ro);
2331 md->disk->flags = GENHD_FL_EXT_DEVT;
2332 if (area_type & (MMC_BLK_DATA_AREA_RPMB | MMC_BLK_DATA_AREA_BOOT))
2333 md->disk->flags |= GENHD_FL_NO_PART_SCAN
2334 | GENHD_FL_SUPPRESS_PARTITION_INFO;
2337 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2339 * - be set for removable media with permanent block devices
2340 * - be unset for removable block devices with permanent media
2342 * Since MMC block devices clearly fall under the second
2343 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2344 * should use the block device creation/destruction hotplug
2345 * messages to tell when the card is present.
2348 snprintf(md->disk->disk_name, sizeof(md->disk->disk_name),
2349 "mmcblk%u%s", card->host->index, subname ? subname : "");
2351 set_capacity(md->disk, size);
2353 if (mmc_host_cmd23(card->host)) {
2354 if ((mmc_card_mmc(card) &&
2355 card->csd.mmca_vsn >= CSD_SPEC_VER_3) ||
2356 (mmc_card_sd(card) &&
2357 card->scr.cmds & SD_SCR_CMD23_SUPPORT))
2358 md->flags |= MMC_BLK_CMD23;
2361 if (mmc_card_mmc(card) &&
2362 md->flags & MMC_BLK_CMD23 &&
2363 ((card->ext_csd.rel_param & EXT_CSD_WR_REL_PARAM_EN) ||
2364 card->ext_csd.rel_sectors)) {
2365 md->flags |= MMC_BLK_REL_WR;
2366 blk_queue_write_cache(md->queue.queue, true, true);
2369 return md;
2371 err_putdisk:
2372 put_disk(md->disk);
2373 err_kfree:
2374 kfree(md);
2375 out:
2376 ida_simple_remove(&mmc_blk_ida, devidx);
2377 return ERR_PTR(ret);
2380 static struct mmc_blk_data *mmc_blk_alloc(struct mmc_card *card)
2382 sector_t size;
2384 if (!mmc_card_sd(card) && mmc_card_blockaddr(card)) {
2386 * The EXT_CSD sector count is in number or 512 byte
2387 * sectors.
2389 size = card->ext_csd.sectors;
2390 } else {
2392 * The CSD capacity field is in units of read_blkbits.
2393 * set_capacity takes units of 512 bytes.
2395 size = (typeof(sector_t))card->csd.capacity
2396 << (card->csd.read_blkbits - 9);
2399 return mmc_blk_alloc_req(card, &card->dev, size, false, NULL,
2400 MMC_BLK_DATA_AREA_MAIN);
2403 static int mmc_blk_alloc_part(struct mmc_card *card,
2404 struct mmc_blk_data *md,
2405 unsigned int part_type,
2406 sector_t size,
2407 bool default_ro,
2408 const char *subname,
2409 int area_type)
2411 char cap_str[10];
2412 struct mmc_blk_data *part_md;
2414 part_md = mmc_blk_alloc_req(card, disk_to_dev(md->disk), size, default_ro,
2415 subname, area_type);
2416 if (IS_ERR(part_md))
2417 return PTR_ERR(part_md);
2418 part_md->part_type = part_type;
2419 list_add(&part_md->part, &md->part);
2421 string_get_size((u64)get_capacity(part_md->disk), 512, STRING_UNITS_2,
2422 cap_str, sizeof(cap_str));
2423 pr_info("%s: %s %s partition %u %s\n",
2424 part_md->disk->disk_name, mmc_card_id(card),
2425 mmc_card_name(card), part_md->part_type, cap_str);
2426 return 0;
2430 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2431 * @filp: the character device file
2432 * @cmd: the ioctl() command
2433 * @arg: the argument from userspace
2435 * This will essentially just redirect the ioctl()s coming in over to
2436 * the main block device spawning the RPMB character device.
2438 static long mmc_rpmb_ioctl(struct file *filp, unsigned int cmd,
2439 unsigned long arg)
2441 struct mmc_rpmb_data *rpmb = filp->private_data;
2442 int ret;
2444 switch (cmd) {
2445 case MMC_IOC_CMD:
2446 ret = mmc_blk_ioctl_cmd(rpmb->md,
2447 (struct mmc_ioc_cmd __user *)arg,
2448 rpmb);
2449 break;
2450 case MMC_IOC_MULTI_CMD:
2451 ret = mmc_blk_ioctl_multi_cmd(rpmb->md,
2452 (struct mmc_ioc_multi_cmd __user *)arg,
2453 rpmb);
2454 break;
2455 default:
2456 ret = -EINVAL;
2457 break;
2460 return ret;
2463 #ifdef CONFIG_COMPAT
2464 static long mmc_rpmb_ioctl_compat(struct file *filp, unsigned int cmd,
2465 unsigned long arg)
2467 return mmc_rpmb_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
2469 #endif
2471 static int mmc_rpmb_chrdev_open(struct inode *inode, struct file *filp)
2473 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2474 struct mmc_rpmb_data, chrdev);
2476 get_device(&rpmb->dev);
2477 filp->private_data = rpmb;
2478 mmc_blk_get(rpmb->md->disk);
2480 return nonseekable_open(inode, filp);
2483 static int mmc_rpmb_chrdev_release(struct inode *inode, struct file *filp)
2485 struct mmc_rpmb_data *rpmb = container_of(inode->i_cdev,
2486 struct mmc_rpmb_data, chrdev);
2488 mmc_blk_put(rpmb->md);
2489 put_device(&rpmb->dev);
2491 return 0;
2494 static const struct file_operations mmc_rpmb_fileops = {
2495 .release = mmc_rpmb_chrdev_release,
2496 .open = mmc_rpmb_chrdev_open,
2497 .owner = THIS_MODULE,
2498 .llseek = no_llseek,
2499 .unlocked_ioctl = mmc_rpmb_ioctl,
2500 #ifdef CONFIG_COMPAT
2501 .compat_ioctl = mmc_rpmb_ioctl_compat,
2502 #endif
2505 static void mmc_blk_rpmb_device_release(struct device *dev)
2507 struct mmc_rpmb_data *rpmb = dev_get_drvdata(dev);
2509 ida_simple_remove(&mmc_rpmb_ida, rpmb->id);
2510 kfree(rpmb);
2513 static int mmc_blk_alloc_rpmb_part(struct mmc_card *card,
2514 struct mmc_blk_data *md,
2515 unsigned int part_index,
2516 sector_t size,
2517 const char *subname)
2519 int devidx, ret;
2520 char rpmb_name[DISK_NAME_LEN];
2521 char cap_str[10];
2522 struct mmc_rpmb_data *rpmb;
2524 /* This creates the minor number for the RPMB char device */
2525 devidx = ida_simple_get(&mmc_rpmb_ida, 0, max_devices, GFP_KERNEL);
2526 if (devidx < 0)
2527 return devidx;
2529 rpmb = kzalloc(sizeof(*rpmb), GFP_KERNEL);
2530 if (!rpmb) {
2531 ida_simple_remove(&mmc_rpmb_ida, devidx);
2532 return -ENOMEM;
2535 snprintf(rpmb_name, sizeof(rpmb_name),
2536 "mmcblk%u%s", card->host->index, subname ? subname : "");
2538 rpmb->id = devidx;
2539 rpmb->part_index = part_index;
2540 rpmb->dev.init_name = rpmb_name;
2541 rpmb->dev.bus = &mmc_rpmb_bus_type;
2542 rpmb->dev.devt = MKDEV(MAJOR(mmc_rpmb_devt), rpmb->id);
2543 rpmb->dev.parent = &card->dev;
2544 rpmb->dev.release = mmc_blk_rpmb_device_release;
2545 device_initialize(&rpmb->dev);
2546 dev_set_drvdata(&rpmb->dev, rpmb);
2547 rpmb->md = md;
2549 cdev_init(&rpmb->chrdev, &mmc_rpmb_fileops);
2550 rpmb->chrdev.owner = THIS_MODULE;
2551 ret = cdev_device_add(&rpmb->chrdev, &rpmb->dev);
2552 if (ret) {
2553 pr_err("%s: could not add character device\n", rpmb_name);
2554 goto out_put_device;
2557 list_add(&rpmb->node, &md->rpmbs);
2559 string_get_size((u64)size, 512, STRING_UNITS_2,
2560 cap_str, sizeof(cap_str));
2562 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2563 rpmb_name, mmc_card_id(card),
2564 mmc_card_name(card), EXT_CSD_PART_CONFIG_ACC_RPMB, cap_str,
2565 MAJOR(mmc_rpmb_devt), rpmb->id);
2567 return 0;
2569 out_put_device:
2570 put_device(&rpmb->dev);
2571 return ret;
2574 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data *rpmb)
2577 cdev_device_del(&rpmb->chrdev, &rpmb->dev);
2578 put_device(&rpmb->dev);
2581 /* MMC Physical partitions consist of two boot partitions and
2582 * up to four general purpose partitions.
2583 * For each partition enabled in EXT_CSD a block device will be allocatedi
2584 * to provide access to the partition.
2587 static int mmc_blk_alloc_parts(struct mmc_card *card, struct mmc_blk_data *md)
2589 int idx, ret;
2591 if (!mmc_card_mmc(card))
2592 return 0;
2594 for (idx = 0; idx < card->nr_parts; idx++) {
2595 if (card->part[idx].area_type & MMC_BLK_DATA_AREA_RPMB) {
2597 * RPMB partitions does not provide block access, they
2598 * are only accessed using ioctl():s. Thus create
2599 * special RPMB block devices that do not have a
2600 * backing block queue for these.
2602 ret = mmc_blk_alloc_rpmb_part(card, md,
2603 card->part[idx].part_cfg,
2604 card->part[idx].size >> 9,
2605 card->part[idx].name);
2606 if (ret)
2607 return ret;
2608 } else if (card->part[idx].size) {
2609 ret = mmc_blk_alloc_part(card, md,
2610 card->part[idx].part_cfg,
2611 card->part[idx].size >> 9,
2612 card->part[idx].force_ro,
2613 card->part[idx].name,
2614 card->part[idx].area_type);
2615 if (ret)
2616 return ret;
2620 return 0;
2623 static void mmc_blk_remove_req(struct mmc_blk_data *md)
2625 struct mmc_card *card;
2627 if (md) {
2629 * Flush remaining requests and free queues. It
2630 * is freeing the queue that stops new requests
2631 * from being accepted.
2633 card = md->queue.card;
2634 if (md->disk->flags & GENHD_FL_UP) {
2635 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2636 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2637 card->ext_csd.boot_ro_lockable)
2638 device_remove_file(disk_to_dev(md->disk),
2639 &md->power_ro_lock);
2641 del_gendisk(md->disk);
2643 mmc_cleanup_queue(&md->queue);
2644 mmc_blk_put(md);
2648 static void mmc_blk_remove_parts(struct mmc_card *card,
2649 struct mmc_blk_data *md)
2651 struct list_head *pos, *q;
2652 struct mmc_blk_data *part_md;
2653 struct mmc_rpmb_data *rpmb;
2655 /* Remove RPMB partitions */
2656 list_for_each_safe(pos, q, &md->rpmbs) {
2657 rpmb = list_entry(pos, struct mmc_rpmb_data, node);
2658 list_del(pos);
2659 mmc_blk_remove_rpmb_part(rpmb);
2661 /* Remove block partitions */
2662 list_for_each_safe(pos, q, &md->part) {
2663 part_md = list_entry(pos, struct mmc_blk_data, part);
2664 list_del(pos);
2665 mmc_blk_remove_req(part_md);
2669 static int mmc_add_disk(struct mmc_blk_data *md)
2671 int ret;
2672 struct mmc_card *card = md->queue.card;
2674 device_add_disk(md->parent, md->disk);
2675 md->force_ro.show = force_ro_show;
2676 md->force_ro.store = force_ro_store;
2677 sysfs_attr_init(&md->force_ro.attr);
2678 md->force_ro.attr.name = "force_ro";
2679 md->force_ro.attr.mode = S_IRUGO | S_IWUSR;
2680 ret = device_create_file(disk_to_dev(md->disk), &md->force_ro);
2681 if (ret)
2682 goto force_ro_fail;
2684 if ((md->area_type & MMC_BLK_DATA_AREA_BOOT) &&
2685 card->ext_csd.boot_ro_lockable) {
2686 umode_t mode;
2688 if (card->ext_csd.boot_ro_lock & EXT_CSD_BOOT_WP_B_PWR_WP_DIS)
2689 mode = S_IRUGO;
2690 else
2691 mode = S_IRUGO | S_IWUSR;
2693 md->power_ro_lock.show = power_ro_lock_show;
2694 md->power_ro_lock.store = power_ro_lock_store;
2695 sysfs_attr_init(&md->power_ro_lock.attr);
2696 md->power_ro_lock.attr.mode = mode;
2697 md->power_ro_lock.attr.name =
2698 "ro_lock_until_next_power_on";
2699 ret = device_create_file(disk_to_dev(md->disk),
2700 &md->power_ro_lock);
2701 if (ret)
2702 goto power_ro_lock_fail;
2704 return ret;
2706 power_ro_lock_fail:
2707 device_remove_file(disk_to_dev(md->disk), &md->force_ro);
2708 force_ro_fail:
2709 del_gendisk(md->disk);
2711 return ret;
2714 #ifdef CONFIG_DEBUG_FS
2716 static int mmc_dbg_card_status_get(void *data, u64 *val)
2718 struct mmc_card *card = data;
2719 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2720 struct mmc_queue *mq = &md->queue;
2721 struct request *req;
2722 int ret;
2724 /* Ask the block layer about the card status */
2725 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2726 if (IS_ERR(req))
2727 return PTR_ERR(req);
2728 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_CARD_STATUS;
2729 blk_execute_rq(mq->queue, NULL, req, 0);
2730 ret = req_to_mmc_queue_req(req)->drv_op_result;
2731 if (ret >= 0) {
2732 *val = ret;
2733 ret = 0;
2735 blk_put_request(req);
2737 return ret;
2739 DEFINE_SIMPLE_ATTRIBUTE(mmc_dbg_card_status_fops, mmc_dbg_card_status_get,
2740 NULL, "%08llx\n");
2742 /* That is two digits * 512 + 1 for newline */
2743 #define EXT_CSD_STR_LEN 1025
2745 static int mmc_ext_csd_open(struct inode *inode, struct file *filp)
2747 struct mmc_card *card = inode->i_private;
2748 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2749 struct mmc_queue *mq = &md->queue;
2750 struct request *req;
2751 char *buf;
2752 ssize_t n = 0;
2753 u8 *ext_csd;
2754 int err, i;
2756 buf = kmalloc(EXT_CSD_STR_LEN + 1, GFP_KERNEL);
2757 if (!buf)
2758 return -ENOMEM;
2760 /* Ask the block layer for the EXT CSD */
2761 req = blk_get_request(mq->queue, REQ_OP_DRV_IN, 0);
2762 if (IS_ERR(req)) {
2763 err = PTR_ERR(req);
2764 goto out_free;
2766 req_to_mmc_queue_req(req)->drv_op = MMC_DRV_OP_GET_EXT_CSD;
2767 req_to_mmc_queue_req(req)->drv_op_data = &ext_csd;
2768 blk_execute_rq(mq->queue, NULL, req, 0);
2769 err = req_to_mmc_queue_req(req)->drv_op_result;
2770 blk_put_request(req);
2771 if (err) {
2772 pr_err("FAILED %d\n", err);
2773 goto out_free;
2776 for (i = 0; i < 512; i++)
2777 n += sprintf(buf + n, "%02x", ext_csd[i]);
2778 n += sprintf(buf + n, "\n");
2780 if (n != EXT_CSD_STR_LEN) {
2781 err = -EINVAL;
2782 kfree(ext_csd);
2783 goto out_free;
2786 filp->private_data = buf;
2787 kfree(ext_csd);
2788 return 0;
2790 out_free:
2791 kfree(buf);
2792 return err;
2795 static ssize_t mmc_ext_csd_read(struct file *filp, char __user *ubuf,
2796 size_t cnt, loff_t *ppos)
2798 char *buf = filp->private_data;
2800 return simple_read_from_buffer(ubuf, cnt, ppos,
2801 buf, EXT_CSD_STR_LEN);
2804 static int mmc_ext_csd_release(struct inode *inode, struct file *file)
2806 kfree(file->private_data);
2807 return 0;
2810 static const struct file_operations mmc_dbg_ext_csd_fops = {
2811 .open = mmc_ext_csd_open,
2812 .read = mmc_ext_csd_read,
2813 .release = mmc_ext_csd_release,
2814 .llseek = default_llseek,
2817 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2819 struct dentry *root;
2821 if (!card->debugfs_root)
2822 return 0;
2824 root = card->debugfs_root;
2826 if (mmc_card_mmc(card) || mmc_card_sd(card)) {
2827 md->status_dentry =
2828 debugfs_create_file("status", S_IRUSR, root, card,
2829 &mmc_dbg_card_status_fops);
2830 if (!md->status_dentry)
2831 return -EIO;
2834 if (mmc_card_mmc(card)) {
2835 md->ext_csd_dentry =
2836 debugfs_create_file("ext_csd", S_IRUSR, root, card,
2837 &mmc_dbg_ext_csd_fops);
2838 if (!md->ext_csd_dentry)
2839 return -EIO;
2842 return 0;
2845 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2846 struct mmc_blk_data *md)
2848 if (!card->debugfs_root)
2849 return;
2851 if (!IS_ERR_OR_NULL(md->status_dentry)) {
2852 debugfs_remove(md->status_dentry);
2853 md->status_dentry = NULL;
2856 if (!IS_ERR_OR_NULL(md->ext_csd_dentry)) {
2857 debugfs_remove(md->ext_csd_dentry);
2858 md->ext_csd_dentry = NULL;
2862 #else
2864 static int mmc_blk_add_debugfs(struct mmc_card *card, struct mmc_blk_data *md)
2866 return 0;
2869 static void mmc_blk_remove_debugfs(struct mmc_card *card,
2870 struct mmc_blk_data *md)
2874 #endif /* CONFIG_DEBUG_FS */
2876 static int mmc_blk_probe(struct mmc_card *card)
2878 struct mmc_blk_data *md, *part_md;
2879 char cap_str[10];
2882 * Check that the card supports the command class(es) we need.
2884 if (!(card->csd.cmdclass & CCC_BLOCK_READ))
2885 return -ENODEV;
2887 mmc_fixup_device(card, mmc_blk_fixups);
2889 card->complete_wq = alloc_workqueue("mmc_complete",
2890 WQ_MEM_RECLAIM | WQ_HIGHPRI, 0);
2891 if (unlikely(!card->complete_wq)) {
2892 pr_err("Failed to create mmc completion workqueue");
2893 return -ENOMEM;
2896 md = mmc_blk_alloc(card);
2897 if (IS_ERR(md))
2898 return PTR_ERR(md);
2900 string_get_size((u64)get_capacity(md->disk), 512, STRING_UNITS_2,
2901 cap_str, sizeof(cap_str));
2902 pr_info("%s: %s %s %s %s\n",
2903 md->disk->disk_name, mmc_card_id(card), mmc_card_name(card),
2904 cap_str, md->read_only ? "(ro)" : "");
2906 if (mmc_blk_alloc_parts(card, md))
2907 goto out;
2909 dev_set_drvdata(&card->dev, md);
2911 if (mmc_add_disk(md))
2912 goto out;
2914 list_for_each_entry(part_md, &md->part, part) {
2915 if (mmc_add_disk(part_md))
2916 goto out;
2919 /* Add two debugfs entries */
2920 mmc_blk_add_debugfs(card, md);
2922 pm_runtime_set_autosuspend_delay(&card->dev, 3000);
2923 pm_runtime_use_autosuspend(&card->dev);
2926 * Don't enable runtime PM for SD-combo cards here. Leave that
2927 * decision to be taken during the SDIO init sequence instead.
2929 if (card->type != MMC_TYPE_SD_COMBO) {
2930 pm_runtime_set_active(&card->dev);
2931 pm_runtime_enable(&card->dev);
2934 return 0;
2936 out:
2937 mmc_blk_remove_parts(card, md);
2938 mmc_blk_remove_req(md);
2939 return 0;
2942 static void mmc_blk_remove(struct mmc_card *card)
2944 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2946 mmc_blk_remove_debugfs(card, md);
2947 mmc_blk_remove_parts(card, md);
2948 pm_runtime_get_sync(&card->dev);
2949 if (md->part_curr != md->part_type) {
2950 mmc_claim_host(card->host);
2951 mmc_blk_part_switch(card, md->part_type);
2952 mmc_release_host(card->host);
2954 if (card->type != MMC_TYPE_SD_COMBO)
2955 pm_runtime_disable(&card->dev);
2956 pm_runtime_put_noidle(&card->dev);
2957 mmc_blk_remove_req(md);
2958 dev_set_drvdata(&card->dev, NULL);
2959 destroy_workqueue(card->complete_wq);
2962 static int _mmc_blk_suspend(struct mmc_card *card)
2964 struct mmc_blk_data *part_md;
2965 struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
2967 if (md) {
2968 mmc_queue_suspend(&md->queue);
2969 list_for_each_entry(part_md, &md->part, part) {
2970 mmc_queue_suspend(&part_md->queue);
2973 return 0;
2976 static void mmc_blk_shutdown(struct mmc_card *card)
2978 _mmc_blk_suspend(card);
2981 #ifdef CONFIG_PM_SLEEP
2982 static int mmc_blk_suspend(struct device *dev)
2984 struct mmc_card *card = mmc_dev_to_card(dev);
2986 return _mmc_blk_suspend(card);
2989 static int mmc_blk_resume(struct device *dev)
2991 struct mmc_blk_data *part_md;
2992 struct mmc_blk_data *md = dev_get_drvdata(dev);
2994 if (md) {
2996 * Resume involves the card going into idle state,
2997 * so current partition is always the main one.
2999 md->part_curr = md->part_type;
3000 mmc_queue_resume(&md->queue);
3001 list_for_each_entry(part_md, &md->part, part) {
3002 mmc_queue_resume(&part_md->queue);
3005 return 0;
3007 #endif
3009 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops, mmc_blk_suspend, mmc_blk_resume);
3011 static struct mmc_driver mmc_driver = {
3012 .drv = {
3013 .name = "mmcblk",
3014 .pm = &mmc_blk_pm_ops,
3016 .probe = mmc_blk_probe,
3017 .remove = mmc_blk_remove,
3018 .shutdown = mmc_blk_shutdown,
3021 static int __init mmc_blk_init(void)
3023 int res;
3025 res = bus_register(&mmc_rpmb_bus_type);
3026 if (res < 0) {
3027 pr_err("mmcblk: could not register RPMB bus type\n");
3028 return res;
3030 res = alloc_chrdev_region(&mmc_rpmb_devt, 0, MAX_DEVICES, "rpmb");
3031 if (res < 0) {
3032 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3033 goto out_bus_unreg;
3036 if (perdev_minors != CONFIG_MMC_BLOCK_MINORS)
3037 pr_info("mmcblk: using %d minors per device\n", perdev_minors);
3039 max_devices = min(MAX_DEVICES, (1 << MINORBITS) / perdev_minors);
3041 res = register_blkdev(MMC_BLOCK_MAJOR, "mmc");
3042 if (res)
3043 goto out_chrdev_unreg;
3045 res = mmc_register_driver(&mmc_driver);
3046 if (res)
3047 goto out_blkdev_unreg;
3049 return 0;
3051 out_blkdev_unreg:
3052 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3053 out_chrdev_unreg:
3054 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3055 out_bus_unreg:
3056 bus_unregister(&mmc_rpmb_bus_type);
3057 return res;
3060 static void __exit mmc_blk_exit(void)
3062 mmc_unregister_driver(&mmc_driver);
3063 unregister_blkdev(MMC_BLOCK_MAJOR, "mmc");
3064 unregister_chrdev_region(mmc_rpmb_devt, MAX_DEVICES);
3065 bus_unregister(&mmc_rpmb_bus_type);
3068 module_init(mmc_blk_init);
3069 module_exit(mmc_blk_exit);
3071 MODULE_LICENSE("GPL");
3072 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");