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
20 #include <linux/moduleparam.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
24 #include <linux/kernel.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>
60 MODULE_ALIAS("mmc:block");
61 #ifdef MODULE_PARAM_PREFIX
62 #undef MODULE_PARAM_PREFIX
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
72 #define MMC_BLK_TIMEOUT_MS (10 * 1000)
73 #define MMC_EXTRACT_INDEX_FROM_ARG(x) ((x & 0x00FF0000) >> 16)
74 #define MMC_EXTRACT_VALUE_FROM_ARG(x) ((x & 0x0000FF00) >> 8)
76 #define mmc_req_rel_wr(req) ((req->cmd_flags & REQ_FUA) && \
77 (rq_data_dir(req) == WRITE))
78 static DEFINE_MUTEX(block_mutex
);
81 * The defaults come from config options but can be overriden by module
84 static int perdev_minors
= CONFIG_MMC_BLOCK_MINORS
;
87 * We've only got one major, so number of mmcblk devices is
88 * limited to (1 << 20) / number of minors per device. It is also
89 * limited by the MAX_DEVICES below.
91 static int max_devices
;
93 #define MAX_DEVICES 256
95 static DEFINE_IDA(mmc_blk_ida
);
96 static DEFINE_IDA(mmc_rpmb_ida
);
99 * There is one mmc_blk_data per slot.
101 struct mmc_blk_data
{
102 struct device
*parent
;
103 struct gendisk
*disk
;
104 struct mmc_queue queue
;
105 struct list_head part
;
106 struct list_head rpmbs
;
109 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
110 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
113 unsigned int read_only
;
114 unsigned int part_type
;
115 unsigned int reset_done
;
116 #define MMC_BLK_READ BIT(0)
117 #define MMC_BLK_WRITE BIT(1)
118 #define MMC_BLK_DISCARD BIT(2)
119 #define MMC_BLK_SECDISCARD BIT(3)
120 #define MMC_BLK_CQE_RECOVERY BIT(4)
123 * Only set in main mmc_blk_data associated
124 * with mmc_card with dev_set_drvdata, and keeps
125 * track of the current selected device partition.
127 unsigned int part_curr
;
128 struct device_attribute force_ro
;
129 struct device_attribute power_ro_lock
;
132 /* debugfs files (only in main mmc_blk_data) */
133 struct dentry
*status_dentry
;
134 struct dentry
*ext_csd_dentry
;
137 /* Device type for RPMB character devices */
138 static dev_t mmc_rpmb_devt
;
140 /* Bus type for RPMB character devices */
141 static struct bus_type mmc_rpmb_bus_type
= {
146 * struct mmc_rpmb_data - special RPMB device type for these areas
147 * @dev: the device for the RPMB area
148 * @chrdev: character device for the RPMB area
149 * @id: unique device ID number
150 * @part_index: partition index (0 on first)
151 * @md: parent MMC block device
152 * @node: list item, so we can put this device on a list
154 struct mmc_rpmb_data
{
158 unsigned int part_index
;
159 struct mmc_blk_data
*md
;
160 struct list_head node
;
163 static DEFINE_MUTEX(open_lock
);
165 module_param(perdev_minors
, int, 0444);
166 MODULE_PARM_DESC(perdev_minors
, "Minors numbers to allocate per device");
168 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
169 unsigned int part_type
);
170 static void mmc_blk_rw_rq_prep(struct mmc_queue_req
*mqrq
,
171 struct mmc_card
*card
,
173 struct mmc_queue
*mq
);
174 static void mmc_blk_hsq_req_done(struct mmc_request
*mrq
);
176 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
178 struct mmc_blk_data
*md
;
180 mutex_lock(&open_lock
);
181 md
= disk
->private_data
;
182 if (md
&& md
->usage
== 0)
186 mutex_unlock(&open_lock
);
191 static inline int mmc_get_devidx(struct gendisk
*disk
)
193 int devidx
= disk
->first_minor
/ perdev_minors
;
197 static void mmc_blk_put(struct mmc_blk_data
*md
)
199 mutex_lock(&open_lock
);
201 if (md
->usage
== 0) {
202 int devidx
= mmc_get_devidx(md
->disk
);
203 blk_put_queue(md
->queue
.queue
);
204 ida_simple_remove(&mmc_blk_ida
, devidx
);
208 mutex_unlock(&open_lock
);
211 static ssize_t
power_ro_lock_show(struct device
*dev
,
212 struct device_attribute
*attr
, char *buf
)
215 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
216 struct mmc_card
*card
= md
->queue
.card
;
219 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PERM_WP_EN
)
221 else if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_EN
)
224 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", locked
);
231 static ssize_t
power_ro_lock_store(struct device
*dev
,
232 struct device_attribute
*attr
, const char *buf
, size_t count
)
235 struct mmc_blk_data
*md
, *part_md
;
236 struct mmc_queue
*mq
;
240 if (kstrtoul(buf
, 0, &set
))
246 md
= mmc_blk_get(dev_to_disk(dev
));
249 /* Dispatch locking to the block layer */
250 req
= blk_get_request(mq
->queue
, REQ_OP_DRV_OUT
, 0);
252 count
= PTR_ERR(req
);
255 req_to_mmc_queue_req(req
)->drv_op
= MMC_DRV_OP_BOOT_WP
;
256 blk_execute_rq(mq
->queue
, NULL
, req
, 0);
257 ret
= req_to_mmc_queue_req(req
)->drv_op_result
;
258 blk_put_request(req
);
261 pr_info("%s: Locking boot partition ro until next power on\n",
262 md
->disk
->disk_name
);
263 set_disk_ro(md
->disk
, 1);
265 list_for_each_entry(part_md
, &md
->part
, part
)
266 if (part_md
->area_type
== MMC_BLK_DATA_AREA_BOOT
) {
267 pr_info("%s: Locking boot partition ro until next power on\n", part_md
->disk
->disk_name
);
268 set_disk_ro(part_md
->disk
, 1);
276 static ssize_t
force_ro_show(struct device
*dev
, struct device_attribute
*attr
,
280 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
282 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n",
283 get_disk_ro(dev_to_disk(dev
)) ^
289 static ssize_t
force_ro_store(struct device
*dev
, struct device_attribute
*attr
,
290 const char *buf
, size_t count
)
294 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
295 unsigned long set
= simple_strtoul(buf
, &end
, 0);
301 set_disk_ro(dev_to_disk(dev
), set
|| md
->read_only
);
308 static int mmc_blk_open(struct block_device
*bdev
, fmode_t mode
)
310 struct mmc_blk_data
*md
= mmc_blk_get(bdev
->bd_disk
);
313 mutex_lock(&block_mutex
);
316 if ((mode
& FMODE_WRITE
) && md
->read_only
) {
321 mutex_unlock(&block_mutex
);
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
);
332 mutex_unlock(&block_mutex
);
336 mmc_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
338 geo
->cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
344 struct mmc_blk_ioc_data
{
345 struct mmc_ioc_cmd ic
;
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
;
357 idata
= kmalloc(sizeof(*idata
), GFP_KERNEL
);
363 if (copy_from_user(&idata
->ic
, user
, sizeof(idata
->ic
))) {
368 idata
->buf_bytes
= (u64
) idata
->ic
.blksz
* idata
->ic
.blocks
;
369 if (idata
->buf_bytes
> MMC_IOC_MAX_BYTES
) {
374 if (!idata
->buf_bytes
) {
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
);
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
)))
403 if (!idata
->ic
.write_flag
) {
404 if (copy_to_user((void __user
*)(unsigned long)ic
->data_ptr
,
405 idata
->buf
, idata
->buf_bytes
))
412 static int card_busy_detect(struct mmc_card
*card
, unsigned int timeout_ms
,
415 unsigned long timeout
= jiffies
+ msecs_to_jiffies(timeout_ms
);
420 bool done
= time_after(jiffies
, timeout
);
422 err
= __mmc_send_status(card
, &status
, 5);
424 dev_err(mmc_dev(card
->host
),
425 "error %d requesting status\n", err
);
429 /* Accumulate any response error bits seen */
431 *resp_errs
|= status
;
434 * Timeout if the device never becomes ready for data and never
435 * leaves the program state.
438 dev_err(mmc_dev(card
->host
),
439 "Card stuck in wrong state! %s status: %#x\n",
443 } while (!mmc_ready_for_data(status
));
448 static int __mmc_blk_ioctl_cmd(struct mmc_card
*card
, struct mmc_blk_data
*md
,
449 struct mmc_blk_ioc_data
*idata
)
451 struct mmc_command cmd
= {}, sbc
= {};
452 struct mmc_data data
= {};
453 struct mmc_request mrq
= {};
454 struct scatterlist sg
;
456 unsigned int target_part
;
458 if (!card
|| !md
|| !idata
)
462 * The RPMB accesses comes in from the character device, so we
463 * need to target these explicitly. Else we just target the
464 * partition type for the block device the ioctl() was issued
468 /* Support multiple RPMB partitions */
469 target_part
= idata
->rpmb
->part_index
;
470 target_part
|= EXT_CSD_PART_CONFIG_ACC_RPMB
;
472 target_part
= md
->part_type
;
475 cmd
.opcode
= idata
->ic
.opcode
;
476 cmd
.arg
= idata
->ic
.arg
;
477 cmd
.flags
= idata
->ic
.flags
;
479 if (idata
->buf_bytes
) {
482 data
.blksz
= idata
->ic
.blksz
;
483 data
.blocks
= idata
->ic
.blocks
;
485 sg_init_one(data
.sg
, idata
->buf
, idata
->buf_bytes
);
487 if (idata
->ic
.write_flag
)
488 data
.flags
= MMC_DATA_WRITE
;
490 data
.flags
= MMC_DATA_READ
;
492 /* data.flags must already be set before doing this. */
493 mmc_set_data_timeout(&data
, card
);
495 /* Allow overriding the timeout_ns for empirical tuning. */
496 if (idata
->ic
.data_timeout_ns
)
497 data
.timeout_ns
= idata
->ic
.data_timeout_ns
;
499 if ((cmd
.flags
& MMC_RSP_R1B
) == MMC_RSP_R1B
) {
501 * Pretend this is a data transfer and rely on the
502 * host driver to compute timeout. When all host
503 * drivers support cmd.cmd_timeout for R1B, this
507 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
509 data
.timeout_ns
= idata
->ic
.cmd_timeout_ms
* 1000000;
517 err
= mmc_blk_part_switch(card
, target_part
);
521 if (idata
->ic
.is_acmd
) {
522 err
= mmc_app_cmd(card
->host
, card
);
528 sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
530 * We don't do any blockcount validation because the max size
531 * may be increased by a future standard. We just copy the
532 * 'Reliable Write' bit here.
534 sbc
.arg
= data
.blocks
| (idata
->ic
.write_flag
& BIT(31));
535 sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
539 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd
.arg
) == EXT_CSD_SANITIZE_START
) &&
540 (cmd
.opcode
== MMC_SWITCH
))
541 return mmc_sanitize(card
);
543 mmc_wait_for_req(card
->host
, &mrq
);
546 dev_err(mmc_dev(card
->host
), "%s: cmd error %d\n",
547 __func__
, cmd
.error
);
551 dev_err(mmc_dev(card
->host
), "%s: data error %d\n",
552 __func__
, data
.error
);
557 * Make sure the cache of the PARTITION_CONFIG register and
558 * PARTITION_ACCESS bits is updated in case the ioctl ext_csd write
559 * changed it successfully.
561 if ((MMC_EXTRACT_INDEX_FROM_ARG(cmd
.arg
) == EXT_CSD_PART_CONFIG
) &&
562 (cmd
.opcode
== MMC_SWITCH
)) {
563 struct mmc_blk_data
*main_md
= dev_get_drvdata(&card
->dev
);
564 u8 value
= MMC_EXTRACT_VALUE_FROM_ARG(cmd
.arg
);
567 * Update cache so the next mmc_blk_part_switch call operates
568 * on up-to-date data.
570 card
->ext_csd
.part_config
= value
;
571 main_md
->part_curr
= value
& EXT_CSD_PART_CONFIG_ACC_MASK
;
575 * According to the SD specs, some commands require a delay after
576 * issuing the command.
578 if (idata
->ic
.postsleep_min_us
)
579 usleep_range(idata
->ic
.postsleep_min_us
, idata
->ic
.postsleep_max_us
);
581 memcpy(&(idata
->ic
.response
), cmd
.resp
, sizeof(cmd
.resp
));
583 if (idata
->rpmb
|| (cmd
.flags
& MMC_RSP_R1B
) == MMC_RSP_R1B
) {
585 * Ensure RPMB/R1B command has completed by polling CMD13
588 err
= card_busy_detect(card
, MMC_BLK_TIMEOUT_MS
, NULL
);
594 static int mmc_blk_ioctl_cmd(struct mmc_blk_data
*md
,
595 struct mmc_ioc_cmd __user
*ic_ptr
,
596 struct mmc_rpmb_data
*rpmb
)
598 struct mmc_blk_ioc_data
*idata
;
599 struct mmc_blk_ioc_data
*idatas
[1];
600 struct mmc_queue
*mq
;
601 struct mmc_card
*card
;
602 int err
= 0, ioc_err
= 0;
605 idata
= mmc_blk_ioctl_copy_from_user(ic_ptr
);
607 return PTR_ERR(idata
);
608 /* This will be NULL on non-RPMB ioctl():s */
611 card
= md
->queue
.card
;
618 * Dispatch the ioctl() into the block request queue.
621 req
= blk_get_request(mq
->queue
,
622 idata
->ic
.write_flag
? REQ_OP_DRV_OUT
: REQ_OP_DRV_IN
, 0);
628 req_to_mmc_queue_req(req
)->drv_op
=
629 rpmb
? MMC_DRV_OP_IOCTL_RPMB
: MMC_DRV_OP_IOCTL
;
630 req_to_mmc_queue_req(req
)->drv_op_data
= idatas
;
631 req_to_mmc_queue_req(req
)->ioc_count
= 1;
632 blk_execute_rq(mq
->queue
, NULL
, req
, 0);
633 ioc_err
= req_to_mmc_queue_req(req
)->drv_op_result
;
634 err
= mmc_blk_ioctl_copy_to_user(ic_ptr
, idata
);
635 blk_put_request(req
);
640 return ioc_err
? ioc_err
: err
;
643 static int mmc_blk_ioctl_multi_cmd(struct mmc_blk_data
*md
,
644 struct mmc_ioc_multi_cmd __user
*user
,
645 struct mmc_rpmb_data
*rpmb
)
647 struct mmc_blk_ioc_data
**idata
= NULL
;
648 struct mmc_ioc_cmd __user
*cmds
= user
->cmds
;
649 struct mmc_card
*card
;
650 struct mmc_queue
*mq
;
651 int i
, err
= 0, ioc_err
= 0;
655 if (copy_from_user(&num_of_cmds
, &user
->num_of_cmds
,
656 sizeof(num_of_cmds
)))
662 if (num_of_cmds
> MMC_IOC_MAX_CMDS
)
665 idata
= kcalloc(num_of_cmds
, sizeof(*idata
), GFP_KERNEL
);
669 for (i
= 0; i
< num_of_cmds
; i
++) {
670 idata
[i
] = mmc_blk_ioctl_copy_from_user(&cmds
[i
]);
671 if (IS_ERR(idata
[i
])) {
672 err
= PTR_ERR(idata
[i
]);
676 /* This will be NULL on non-RPMB ioctl():s */
677 idata
[i
]->rpmb
= rpmb
;
680 card
= md
->queue
.card
;
688 * Dispatch the ioctl()s into the block request queue.
691 req
= blk_get_request(mq
->queue
,
692 idata
[0]->ic
.write_flag
? REQ_OP_DRV_OUT
: REQ_OP_DRV_IN
, 0);
697 req_to_mmc_queue_req(req
)->drv_op
=
698 rpmb
? MMC_DRV_OP_IOCTL_RPMB
: MMC_DRV_OP_IOCTL
;
699 req_to_mmc_queue_req(req
)->drv_op_data
= idata
;
700 req_to_mmc_queue_req(req
)->ioc_count
= num_of_cmds
;
701 blk_execute_rq(mq
->queue
, NULL
, req
, 0);
702 ioc_err
= req_to_mmc_queue_req(req
)->drv_op_result
;
704 /* copy to user if data and response */
705 for (i
= 0; i
< num_of_cmds
&& !err
; i
++)
706 err
= mmc_blk_ioctl_copy_to_user(&cmds
[i
], idata
[i
]);
708 blk_put_request(req
);
711 for (i
= 0; i
< num_of_cmds
; i
++) {
712 kfree(idata
[i
]->buf
);
716 return ioc_err
? ioc_err
: err
;
719 static int mmc_blk_check_blkdev(struct block_device
*bdev
)
722 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
723 * whole block device, not on a partition. This prevents overspray
724 * between sibling partitions.
726 if (!capable(CAP_SYS_RAWIO
) || bdev_is_partition(bdev
))
731 static int mmc_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
732 unsigned int cmd
, unsigned long arg
)
734 struct mmc_blk_data
*md
;
739 ret
= mmc_blk_check_blkdev(bdev
);
742 md
= mmc_blk_get(bdev
->bd_disk
);
745 ret
= mmc_blk_ioctl_cmd(md
,
746 (struct mmc_ioc_cmd __user
*)arg
,
750 case MMC_IOC_MULTI_CMD
:
751 ret
= mmc_blk_check_blkdev(bdev
);
754 md
= mmc_blk_get(bdev
->bd_disk
);
757 ret
= mmc_blk_ioctl_multi_cmd(md
,
758 (struct mmc_ioc_multi_cmd __user
*)arg
,
768 static int mmc_blk_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
769 unsigned int cmd
, unsigned long arg
)
771 return mmc_blk_ioctl(bdev
, mode
, cmd
, (unsigned long) compat_ptr(arg
));
775 static const struct block_device_operations mmc_bdops
= {
776 .open
= mmc_blk_open
,
777 .release
= mmc_blk_release
,
778 .getgeo
= mmc_blk_getgeo
,
779 .owner
= THIS_MODULE
,
780 .ioctl
= mmc_blk_ioctl
,
782 .compat_ioctl
= mmc_blk_compat_ioctl
,
786 static int mmc_blk_part_switch_pre(struct mmc_card
*card
,
787 unsigned int part_type
)
791 if (part_type
== EXT_CSD_PART_CONFIG_ACC_RPMB
) {
792 if (card
->ext_csd
.cmdq_en
) {
793 ret
= mmc_cmdq_disable(card
);
797 mmc_retune_pause(card
->host
);
803 static int mmc_blk_part_switch_post(struct mmc_card
*card
,
804 unsigned int part_type
)
808 if (part_type
== EXT_CSD_PART_CONFIG_ACC_RPMB
) {
809 mmc_retune_unpause(card
->host
);
810 if (card
->reenable_cmdq
&& !card
->ext_csd
.cmdq_en
)
811 ret
= mmc_cmdq_enable(card
);
817 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
818 unsigned int part_type
)
821 struct mmc_blk_data
*main_md
= dev_get_drvdata(&card
->dev
);
823 if (main_md
->part_curr
== part_type
)
826 if (mmc_card_mmc(card
)) {
827 u8 part_config
= card
->ext_csd
.part_config
;
829 ret
= mmc_blk_part_switch_pre(card
, part_type
);
833 part_config
&= ~EXT_CSD_PART_CONFIG_ACC_MASK
;
834 part_config
|= part_type
;
836 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
837 EXT_CSD_PART_CONFIG
, part_config
,
838 card
->ext_csd
.part_time
);
840 mmc_blk_part_switch_post(card
, part_type
);
844 card
->ext_csd
.part_config
= part_config
;
846 ret
= mmc_blk_part_switch_post(card
, main_md
->part_curr
);
849 main_md
->part_curr
= part_type
;
853 static int mmc_sd_num_wr_blocks(struct mmc_card
*card
, u32
*written_blocks
)
859 struct mmc_request mrq
= {};
860 struct mmc_command cmd
= {};
861 struct mmc_data data
= {};
863 struct scatterlist sg
;
865 cmd
.opcode
= MMC_APP_CMD
;
866 cmd
.arg
= card
->rca
<< 16;
867 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
869 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
872 if (!mmc_host_is_spi(card
->host
) && !(cmd
.resp
[0] & R1_APP_CMD
))
875 memset(&cmd
, 0, sizeof(struct mmc_command
));
877 cmd
.opcode
= SD_APP_SEND_NUM_WR_BLKS
;
879 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
883 data
.flags
= MMC_DATA_READ
;
886 mmc_set_data_timeout(&data
, card
);
891 blocks
= kmalloc(4, GFP_KERNEL
);
895 sg_init_one(&sg
, blocks
, 4);
897 mmc_wait_for_req(card
->host
, &mrq
);
899 result
= ntohl(*blocks
);
902 if (cmd
.error
|| data
.error
)
905 *written_blocks
= result
;
910 static unsigned int mmc_blk_clock_khz(struct mmc_host
*host
)
912 if (host
->actual_clock
)
913 return host
->actual_clock
/ 1000;
915 /* Clock may be subject to a divisor, fudge it by a factor of 2. */
917 return host
->ios
.clock
/ 2000;
919 /* How can there be no clock */
921 return 100; /* 100 kHz is minimum possible value */
924 static unsigned int mmc_blk_data_timeout_ms(struct mmc_host
*host
,
925 struct mmc_data
*data
)
927 unsigned int ms
= DIV_ROUND_UP(data
->timeout_ns
, 1000000);
930 if (data
->timeout_clks
) {
931 khz
= mmc_blk_clock_khz(host
);
932 ms
+= DIV_ROUND_UP(data
->timeout_clks
, khz
);
938 static int mmc_blk_reset(struct mmc_blk_data
*md
, struct mmc_host
*host
,
943 if (md
->reset_done
& type
)
946 md
->reset_done
|= type
;
947 err
= mmc_hw_reset(host
);
948 /* Ensure we switch back to the correct partition */
949 if (err
!= -EOPNOTSUPP
) {
950 struct mmc_blk_data
*main_md
=
951 dev_get_drvdata(&host
->card
->dev
);
954 main_md
->part_curr
= main_md
->part_type
;
955 part_err
= mmc_blk_part_switch(host
->card
, md
->part_type
);
958 * We have failed to get back into the correct
959 * partition, so we need to abort the whole request.
967 static inline void mmc_blk_reset_success(struct mmc_blk_data
*md
, int type
)
969 md
->reset_done
&= ~type
;
973 * The non-block commands come back from the block layer after it queued it and
974 * processed it with all other requests and then they get issued in this
977 static void mmc_blk_issue_drv_op(struct mmc_queue
*mq
, struct request
*req
)
979 struct mmc_queue_req
*mq_rq
;
980 struct mmc_card
*card
= mq
->card
;
981 struct mmc_blk_data
*md
= mq
->blkdata
;
982 struct mmc_blk_ioc_data
**idata
;
989 mq_rq
= req_to_mmc_queue_req(req
);
990 rpmb_ioctl
= (mq_rq
->drv_op
== MMC_DRV_OP_IOCTL_RPMB
);
992 switch (mq_rq
->drv_op
) {
993 case MMC_DRV_OP_IOCTL
:
994 case MMC_DRV_OP_IOCTL_RPMB
:
995 idata
= mq_rq
->drv_op_data
;
996 for (i
= 0, ret
= 0; i
< mq_rq
->ioc_count
; i
++) {
997 ret
= __mmc_blk_ioctl_cmd(card
, md
, idata
[i
]);
1001 /* Always switch back to main area after RPMB access */
1003 mmc_blk_part_switch(card
, 0);
1005 case MMC_DRV_OP_BOOT_WP
:
1006 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BOOT_WP
,
1007 card
->ext_csd
.boot_ro_lock
|
1008 EXT_CSD_BOOT_WP_B_PWR_WP_EN
,
1009 card
->ext_csd
.part_time
);
1011 pr_err("%s: Locking boot partition ro until next power on failed: %d\n",
1012 md
->disk
->disk_name
, ret
);
1014 card
->ext_csd
.boot_ro_lock
|=
1015 EXT_CSD_BOOT_WP_B_PWR_WP_EN
;
1017 case MMC_DRV_OP_GET_CARD_STATUS
:
1018 ret
= mmc_send_status(card
, &status
);
1022 case MMC_DRV_OP_GET_EXT_CSD
:
1023 ext_csd
= mq_rq
->drv_op_data
;
1024 ret
= mmc_get_ext_csd(card
, ext_csd
);
1027 pr_err("%s: unknown driver specific operation\n",
1028 md
->disk
->disk_name
);
1032 mq_rq
->drv_op_result
= ret
;
1033 blk_mq_end_request(req
, ret
? BLK_STS_IOERR
: BLK_STS_OK
);
1036 static void mmc_blk_issue_discard_rq(struct mmc_queue
*mq
, struct request
*req
)
1038 struct mmc_blk_data
*md
= mq
->blkdata
;
1039 struct mmc_card
*card
= md
->queue
.card
;
1040 unsigned int from
, nr
;
1041 int err
= 0, type
= MMC_BLK_DISCARD
;
1042 blk_status_t status
= BLK_STS_OK
;
1044 if (!mmc_can_erase(card
)) {
1045 status
= BLK_STS_NOTSUPP
;
1049 from
= blk_rq_pos(req
);
1050 nr
= blk_rq_sectors(req
);
1054 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1055 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1056 INAND_CMD38_ARG_EXT_CSD
,
1057 card
->erase_arg
== MMC_TRIM_ARG
?
1058 INAND_CMD38_ARG_TRIM
:
1059 INAND_CMD38_ARG_ERASE
,
1060 card
->ext_csd
.generic_cmd6_time
);
1063 err
= mmc_erase(card
, from
, nr
, card
->erase_arg
);
1064 } while (err
== -EIO
&& !mmc_blk_reset(md
, card
->host
, type
));
1066 status
= BLK_STS_IOERR
;
1068 mmc_blk_reset_success(md
, type
);
1070 blk_mq_end_request(req
, status
);
1073 static void mmc_blk_issue_secdiscard_rq(struct mmc_queue
*mq
,
1074 struct request
*req
)
1076 struct mmc_blk_data
*md
= mq
->blkdata
;
1077 struct mmc_card
*card
= md
->queue
.card
;
1078 unsigned int from
, nr
, arg
;
1079 int err
= 0, type
= MMC_BLK_SECDISCARD
;
1080 blk_status_t status
= BLK_STS_OK
;
1082 if (!(mmc_can_secure_erase_trim(card
))) {
1083 status
= BLK_STS_NOTSUPP
;
1087 from
= blk_rq_pos(req
);
1088 nr
= blk_rq_sectors(req
);
1090 if (mmc_can_trim(card
) && !mmc_erase_group_aligned(card
, from
, nr
))
1091 arg
= MMC_SECURE_TRIM1_ARG
;
1093 arg
= MMC_SECURE_ERASE_ARG
;
1096 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1097 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1098 INAND_CMD38_ARG_EXT_CSD
,
1099 arg
== MMC_SECURE_TRIM1_ARG
?
1100 INAND_CMD38_ARG_SECTRIM1
:
1101 INAND_CMD38_ARG_SECERASE
,
1102 card
->ext_csd
.generic_cmd6_time
);
1107 err
= mmc_erase(card
, from
, nr
, arg
);
1111 status
= BLK_STS_IOERR
;
1115 if (arg
== MMC_SECURE_TRIM1_ARG
) {
1116 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1117 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1118 INAND_CMD38_ARG_EXT_CSD
,
1119 INAND_CMD38_ARG_SECTRIM2
,
1120 card
->ext_csd
.generic_cmd6_time
);
1125 err
= mmc_erase(card
, from
, nr
, MMC_SECURE_TRIM2_ARG
);
1129 status
= BLK_STS_IOERR
;
1135 if (err
&& !mmc_blk_reset(md
, card
->host
, type
))
1138 mmc_blk_reset_success(md
, type
);
1140 blk_mq_end_request(req
, status
);
1143 static void mmc_blk_issue_flush(struct mmc_queue
*mq
, struct request
*req
)
1145 struct mmc_blk_data
*md
= mq
->blkdata
;
1146 struct mmc_card
*card
= md
->queue
.card
;
1149 ret
= mmc_flush_cache(card
);
1150 blk_mq_end_request(req
, ret
? BLK_STS_IOERR
: BLK_STS_OK
);
1154 * Reformat current write as a reliable write, supporting
1155 * both legacy and the enhanced reliable write MMC cards.
1156 * In each transfer we'll handle only as much as a single
1157 * reliable write can handle, thus finish the request in
1158 * partial completions.
1160 static inline void mmc_apply_rel_rw(struct mmc_blk_request
*brq
,
1161 struct mmc_card
*card
,
1162 struct request
*req
)
1164 if (!(card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
)) {
1165 /* Legacy mode imposes restrictions on transfers. */
1166 if (!IS_ALIGNED(blk_rq_pos(req
), card
->ext_csd
.rel_sectors
))
1167 brq
->data
.blocks
= 1;
1169 if (brq
->data
.blocks
> card
->ext_csd
.rel_sectors
)
1170 brq
->data
.blocks
= card
->ext_csd
.rel_sectors
;
1171 else if (brq
->data
.blocks
< card
->ext_csd
.rel_sectors
)
1172 brq
->data
.blocks
= 1;
1176 #define CMD_ERRORS_EXCL_OOR \
1177 (R1_ADDRESS_ERROR | /* Misaligned address */ \
1178 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1179 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1180 R1_CARD_ECC_FAILED | /* Card ECC failed */ \
1181 R1_CC_ERROR | /* Card controller error */ \
1182 R1_ERROR) /* General/unknown error */
1184 #define CMD_ERRORS \
1185 (CMD_ERRORS_EXCL_OOR | \
1186 R1_OUT_OF_RANGE) /* Command argument out of range */ \
1188 static void mmc_blk_eval_resp_error(struct mmc_blk_request *brq)
1193 * Per the SD specification(physical layer version 4.10)[1],
1194 * section 4.3.3, it explicitly states that "When the last
1195 * block of user area is read using CMD18, the host should
1196 * ignore OUT_OF_RANGE error that may occur even the sequence
1197 * is correct". And JESD84-B51 for eMMC also has a similar
1198 * statement on section 6.8.3.
1200 * Multiple block read/write could be done by either predefined
1201 * method, namely CMD23, or open-ending mode. For open-ending mode,
1202 * we should ignore the OUT_OF_RANGE error as it's normal behaviour.
1204 * However the spec[1] doesn't tell us whether we should also
1205 * ignore that for predefined method. But per the spec[1], section
1206 * 4.15 Set Block Count Command, it says"If illegal block count
1207 * is set, out of range error will be indicated during read/write
1208 * operation (For example, data transfer is stopped at user area
1209 * boundary)." In another word, we could expect a out of range error
1210 * in the response for the following CMD18/25. And if argument of
1211 * CMD23 + the argument of CMD18/25 exceed the max number of blocks,
1212 * we could also expect to get a -ETIMEDOUT or any error number from
1213 * the host drivers due to missing data response(for write)/data(for
1214 * read), as the cards will stop the data transfer by itself per the
1215 * spec. So we only need to check R1_OUT_OF_RANGE for open-ending mode.
1218 if (!brq
->stop
.error
) {
1219 bool oor_with_open_end
;
1220 /* If there is no error yet, check R1 response */
1222 val
= brq
->stop
.resp
[0] & CMD_ERRORS
;
1223 oor_with_open_end
= val
& R1_OUT_OF_RANGE
&& !brq
->mrq
.sbc
;
1225 if (val
&& !oor_with_open_end
)
1226 brq
->stop
.error
= -EIO
;
1230 static void mmc_blk_data_prep(struct mmc_queue
*mq
, struct mmc_queue_req
*mqrq
,
1231 int disable_multi
, bool *do_rel_wr_p
,
1232 bool *do_data_tag_p
)
1234 struct mmc_blk_data
*md
= mq
->blkdata
;
1235 struct mmc_card
*card
= md
->queue
.card
;
1236 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1237 struct request
*req
= mmc_queue_req_to_req(mqrq
);
1238 bool do_rel_wr
, do_data_tag
;
1241 * Reliable writes are used to implement Forced Unit Access and
1242 * are supported only on MMCs.
1244 do_rel_wr
= (req
->cmd_flags
& REQ_FUA
) &&
1245 rq_data_dir(req
) == WRITE
&&
1246 (md
->flags
& MMC_BLK_REL_WR
);
1248 memset(brq
, 0, sizeof(struct mmc_blk_request
));
1250 brq
->mrq
.data
= &brq
->data
;
1251 brq
->mrq
.tag
= req
->tag
;
1253 brq
->stop
.opcode
= MMC_STOP_TRANSMISSION
;
1256 if (rq_data_dir(req
) == READ
) {
1257 brq
->data
.flags
= MMC_DATA_READ
;
1258 brq
->stop
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
1260 brq
->data
.flags
= MMC_DATA_WRITE
;
1261 brq
->stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1264 brq
->data
.blksz
= 512;
1265 brq
->data
.blocks
= blk_rq_sectors(req
);
1266 brq
->data
.blk_addr
= blk_rq_pos(req
);
1269 * The command queue supports 2 priorities: "high" (1) and "simple" (0).
1270 * The eMMC will give "high" priority tasks priority over "simple"
1271 * priority tasks. Here we always set "simple" priority by not setting
1276 * The block layer doesn't support all sector count
1277 * restrictions, so we need to be prepared for too big
1280 if (brq
->data
.blocks
> card
->host
->max_blk_count
)
1281 brq
->data
.blocks
= card
->host
->max_blk_count
;
1283 if (brq
->data
.blocks
> 1) {
1285 * Some SD cards in SPI mode return a CRC error or even lock up
1286 * completely when trying to read the last block using a
1287 * multiblock read command.
1289 if (mmc_host_is_spi(card
->host
) && (rq_data_dir(req
) == READ
) &&
1290 (blk_rq_pos(req
) + blk_rq_sectors(req
) ==
1291 get_capacity(md
->disk
)))
1295 * After a read error, we redo the request one sector
1296 * at a time in order to accurately determine which
1297 * sectors can be read successfully.
1300 brq
->data
.blocks
= 1;
1303 * Some controllers have HW issues while operating
1304 * in multiple I/O mode
1306 if (card
->host
->ops
->multi_io_quirk
)
1307 brq
->data
.blocks
= card
->host
->ops
->multi_io_quirk(card
,
1308 (rq_data_dir(req
) == READ
) ?
1309 MMC_DATA_READ
: MMC_DATA_WRITE
,
1314 mmc_apply_rel_rw(brq
, card
, req
);
1315 brq
->data
.flags
|= MMC_DATA_REL_WR
;
1319 * Data tag is used only during writing meta data to speed
1320 * up write and any subsequent read of this meta data
1322 do_data_tag
= card
->ext_csd
.data_tag_unit_size
&&
1323 (req
->cmd_flags
& REQ_META
) &&
1324 (rq_data_dir(req
) == WRITE
) &&
1325 ((brq
->data
.blocks
* brq
->data
.blksz
) >=
1326 card
->ext_csd
.data_tag_unit_size
);
1329 brq
->data
.flags
|= MMC_DATA_DAT_TAG
;
1331 mmc_set_data_timeout(&brq
->data
, card
);
1333 brq
->data
.sg
= mqrq
->sg
;
1334 brq
->data
.sg_len
= mmc_queue_map_sg(mq
, mqrq
);
1337 * Adjust the sg list so it is the same size as the
1340 if (brq
->data
.blocks
!= blk_rq_sectors(req
)) {
1341 int i
, data_size
= brq
->data
.blocks
<< 9;
1342 struct scatterlist
*sg
;
1344 for_each_sg(brq
->data
.sg
, sg
, brq
->data
.sg_len
, i
) {
1345 data_size
-= sg
->length
;
1346 if (data_size
<= 0) {
1347 sg
->length
+= data_size
;
1352 brq
->data
.sg_len
= i
;
1356 *do_rel_wr_p
= do_rel_wr
;
1359 *do_data_tag_p
= do_data_tag
;
1362 #define MMC_CQE_RETRIES 2
1364 static void mmc_blk_cqe_complete_rq(struct mmc_queue
*mq
, struct request
*req
)
1366 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1367 struct mmc_request
*mrq
= &mqrq
->brq
.mrq
;
1368 struct request_queue
*q
= req
->q
;
1369 struct mmc_host
*host
= mq
->card
->host
;
1370 enum mmc_issue_type issue_type
= mmc_issue_type(mq
, req
);
1371 unsigned long flags
;
1375 mmc_cqe_post_req(host
, mrq
);
1377 if (mrq
->cmd
&& mrq
->cmd
->error
)
1378 err
= mrq
->cmd
->error
;
1379 else if (mrq
->data
&& mrq
->data
->error
)
1380 err
= mrq
->data
->error
;
1385 if (mqrq
->retries
++ < MMC_CQE_RETRIES
)
1386 blk_mq_requeue_request(req
, true);
1388 blk_mq_end_request(req
, BLK_STS_IOERR
);
1389 } else if (mrq
->data
) {
1390 if (blk_update_request(req
, BLK_STS_OK
, mrq
->data
->bytes_xfered
))
1391 blk_mq_requeue_request(req
, true);
1393 __blk_mq_end_request(req
, BLK_STS_OK
);
1395 blk_mq_end_request(req
, BLK_STS_OK
);
1398 spin_lock_irqsave(&mq
->lock
, flags
);
1400 mq
->in_flight
[issue_type
] -= 1;
1402 put_card
= (mmc_tot_in_flight(mq
) == 0);
1404 mmc_cqe_check_busy(mq
);
1406 spin_unlock_irqrestore(&mq
->lock
, flags
);
1409 blk_mq_run_hw_queues(q
, true);
1412 mmc_put_card(mq
->card
, &mq
->ctx
);
1415 void mmc_blk_cqe_recovery(struct mmc_queue
*mq
)
1417 struct mmc_card
*card
= mq
->card
;
1418 struct mmc_host
*host
= card
->host
;
1421 pr_debug("%s: CQE recovery start\n", mmc_hostname(host
));
1423 err
= mmc_cqe_recovery(host
);
1425 mmc_blk_reset(mq
->blkdata
, host
, MMC_BLK_CQE_RECOVERY
);
1427 mmc_blk_reset_success(mq
->blkdata
, MMC_BLK_CQE_RECOVERY
);
1429 pr_debug("%s: CQE recovery done\n", mmc_hostname(host
));
1432 static void mmc_blk_cqe_req_done(struct mmc_request
*mrq
)
1434 struct mmc_queue_req
*mqrq
= container_of(mrq
, struct mmc_queue_req
,
1436 struct request
*req
= mmc_queue_req_to_req(mqrq
);
1437 struct request_queue
*q
= req
->q
;
1438 struct mmc_queue
*mq
= q
->queuedata
;
1441 * Block layer timeouts race with completions which means the normal
1442 * completion path cannot be used during recovery.
1444 if (mq
->in_recovery
)
1445 mmc_blk_cqe_complete_rq(mq
, req
);
1446 else if (likely(!blk_should_fake_timeout(req
->q
)))
1447 blk_mq_complete_request(req
);
1450 static int mmc_blk_cqe_start_req(struct mmc_host
*host
, struct mmc_request
*mrq
)
1452 mrq
->done
= mmc_blk_cqe_req_done
;
1453 mrq
->recovery_notifier
= mmc_cqe_recovery_notifier
;
1455 return mmc_cqe_start_req(host
, mrq
);
1458 static struct mmc_request
*mmc_blk_cqe_prep_dcmd(struct mmc_queue_req
*mqrq
,
1459 struct request
*req
)
1461 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1463 memset(brq
, 0, sizeof(*brq
));
1465 brq
->mrq
.cmd
= &brq
->cmd
;
1466 brq
->mrq
.tag
= req
->tag
;
1471 static int mmc_blk_cqe_issue_flush(struct mmc_queue
*mq
, struct request
*req
)
1473 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1474 struct mmc_request
*mrq
= mmc_blk_cqe_prep_dcmd(mqrq
, req
);
1476 mrq
->cmd
->opcode
= MMC_SWITCH
;
1477 mrq
->cmd
->arg
= (MMC_SWITCH_MODE_WRITE_BYTE
<< 24) |
1478 (EXT_CSD_FLUSH_CACHE
<< 16) |
1480 EXT_CSD_CMD_SET_NORMAL
;
1481 mrq
->cmd
->flags
= MMC_CMD_AC
| MMC_RSP_R1B
;
1483 return mmc_blk_cqe_start_req(mq
->card
->host
, mrq
);
1486 static int mmc_blk_hsq_issue_rw_rq(struct mmc_queue
*mq
, struct request
*req
)
1488 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1489 struct mmc_host
*host
= mq
->card
->host
;
1492 mmc_blk_rw_rq_prep(mqrq
, mq
->card
, 0, mq
);
1493 mqrq
->brq
.mrq
.done
= mmc_blk_hsq_req_done
;
1494 mmc_pre_req(host
, &mqrq
->brq
.mrq
);
1496 err
= mmc_cqe_start_req(host
, &mqrq
->brq
.mrq
);
1498 mmc_post_req(host
, &mqrq
->brq
.mrq
, err
);
1503 static int mmc_blk_cqe_issue_rw_rq(struct mmc_queue
*mq
, struct request
*req
)
1505 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1506 struct mmc_host
*host
= mq
->card
->host
;
1508 if (host
->hsq_enabled
)
1509 return mmc_blk_hsq_issue_rw_rq(mq
, req
);
1511 mmc_blk_data_prep(mq
, mqrq
, 0, NULL
, NULL
);
1513 return mmc_blk_cqe_start_req(mq
->card
->host
, &mqrq
->brq
.mrq
);
1516 static void mmc_blk_rw_rq_prep(struct mmc_queue_req
*mqrq
,
1517 struct mmc_card
*card
,
1519 struct mmc_queue
*mq
)
1521 u32 readcmd
, writecmd
;
1522 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1523 struct request
*req
= mmc_queue_req_to_req(mqrq
);
1524 struct mmc_blk_data
*md
= mq
->blkdata
;
1525 bool do_rel_wr
, do_data_tag
;
1527 mmc_blk_data_prep(mq
, mqrq
, disable_multi
, &do_rel_wr
, &do_data_tag
);
1529 brq
->mrq
.cmd
= &brq
->cmd
;
1531 brq
->cmd
.arg
= blk_rq_pos(req
);
1532 if (!mmc_card_blockaddr(card
))
1534 brq
->cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
1536 if (brq
->data
.blocks
> 1 || do_rel_wr
) {
1537 /* SPI multiblock writes terminate using a special
1538 * token, not a STOP_TRANSMISSION request.
1540 if (!mmc_host_is_spi(card
->host
) ||
1541 rq_data_dir(req
) == READ
)
1542 brq
->mrq
.stop
= &brq
->stop
;
1543 readcmd
= MMC_READ_MULTIPLE_BLOCK
;
1544 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
1546 brq
->mrq
.stop
= NULL
;
1547 readcmd
= MMC_READ_SINGLE_BLOCK
;
1548 writecmd
= MMC_WRITE_BLOCK
;
1550 brq
->cmd
.opcode
= rq_data_dir(req
) == READ
? readcmd
: writecmd
;
1553 * Pre-defined multi-block transfers are preferable to
1554 * open ended-ones (and necessary for reliable writes).
1555 * However, it is not sufficient to just send CMD23,
1556 * and avoid the final CMD12, as on an error condition
1557 * CMD12 (stop) needs to be sent anyway. This, coupled
1558 * with Auto-CMD23 enhancements provided by some
1559 * hosts, means that the complexity of dealing
1560 * with this is best left to the host. If CMD23 is
1561 * supported by card and host, we'll fill sbc in and let
1562 * the host deal with handling it correctly. This means
1563 * that for hosts that don't expose MMC_CAP_CMD23, no
1564 * change of behavior will be observed.
1566 * N.B: Some MMC cards experience perf degradation.
1567 * We'll avoid using CMD23-bounded multiblock writes for
1568 * these, while retaining features like reliable writes.
1570 if ((md
->flags
& MMC_BLK_CMD23
) && mmc_op_multi(brq
->cmd
.opcode
) &&
1571 (do_rel_wr
|| !(card
->quirks
& MMC_QUIRK_BLK_NO_CMD23
) ||
1573 brq
->sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
1574 brq
->sbc
.arg
= brq
->data
.blocks
|
1575 (do_rel_wr
? (1 << 31) : 0) |
1576 (do_data_tag
? (1 << 29) : 0);
1577 brq
->sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1578 brq
->mrq
.sbc
= &brq
->sbc
;
1582 #define MMC_MAX_RETRIES 5
1583 #define MMC_DATA_RETRIES 2
1584 #define MMC_NO_RETRIES (MMC_MAX_RETRIES + 1)
1586 static int mmc_blk_send_stop(struct mmc_card
*card
, unsigned int timeout
)
1588 struct mmc_command cmd
= {
1589 .opcode
= MMC_STOP_TRANSMISSION
,
1590 .flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
,
1591 /* Some hosts wait for busy anyway, so provide a busy timeout */
1592 .busy_timeout
= timeout
,
1595 return mmc_wait_for_cmd(card
->host
, &cmd
, 5);
1598 static int mmc_blk_fix_state(struct mmc_card
*card
, struct request
*req
)
1600 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1601 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1602 unsigned int timeout
= mmc_blk_data_timeout_ms(card
->host
, &brq
->data
);
1605 mmc_retune_hold_now(card
->host
);
1607 mmc_blk_send_stop(card
, timeout
);
1609 err
= card_busy_detect(card
, timeout
, NULL
);
1611 mmc_retune_release(card
->host
);
1616 #define MMC_READ_SINGLE_RETRIES 2
1618 /* Single sector read during recovery */
1619 static void mmc_blk_read_single(struct mmc_queue
*mq
, struct request
*req
)
1621 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1622 struct mmc_request
*mrq
= &mqrq
->brq
.mrq
;
1623 struct mmc_card
*card
= mq
->card
;
1624 struct mmc_host
*host
= card
->host
;
1625 blk_status_t error
= BLK_STS_OK
;
1632 mmc_blk_rw_rq_prep(mqrq
, card
, 1, mq
);
1634 mmc_wait_for_req(host
, mrq
);
1636 err
= mmc_send_status(card
, &status
);
1640 if (!mmc_host_is_spi(host
) &&
1641 !mmc_ready_for_data(status
)) {
1642 err
= mmc_blk_fix_state(card
, req
);
1647 if (mrq
->cmd
->error
&& retries
++ < MMC_READ_SINGLE_RETRIES
)
1652 if (mrq
->cmd
->error
||
1654 (!mmc_host_is_spi(host
) &&
1655 (mrq
->cmd
->resp
[0] & CMD_ERRORS
|| status
& CMD_ERRORS
)))
1656 error
= BLK_STS_IOERR
;
1660 } while (blk_update_request(req
, error
, 512));
1665 mrq
->data
->bytes_xfered
= 0;
1666 blk_update_request(req
, BLK_STS_IOERR
, 512);
1667 /* Let it try the remaining request again */
1668 if (mqrq
->retries
> MMC_MAX_RETRIES
- 1)
1669 mqrq
->retries
= MMC_MAX_RETRIES
- 1;
1672 static inline bool mmc_blk_oor_valid(struct mmc_blk_request
*brq
)
1674 return !!brq
->mrq
.sbc
;
1677 static inline u32
mmc_blk_stop_err_bits(struct mmc_blk_request
*brq
)
1679 return mmc_blk_oor_valid(brq
) ? CMD_ERRORS
: CMD_ERRORS_EXCL_OOR
;
1683 * Check for errors the host controller driver might not have seen such as
1684 * response mode errors or invalid card state.
1686 static bool mmc_blk_status_error(struct request
*req
, u32 status
)
1688 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1689 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1690 struct mmc_queue
*mq
= req
->q
->queuedata
;
1693 if (mmc_host_is_spi(mq
->card
->host
))
1696 stop_err_bits
= mmc_blk_stop_err_bits(brq
);
1698 return brq
->cmd
.resp
[0] & CMD_ERRORS
||
1699 brq
->stop
.resp
[0] & stop_err_bits
||
1700 status
& stop_err_bits
||
1701 (rq_data_dir(req
) == WRITE
&& !mmc_ready_for_data(status
));
1704 static inline bool mmc_blk_cmd_started(struct mmc_blk_request
*brq
)
1706 return !brq
->sbc
.error
&& !brq
->cmd
.error
&&
1707 !(brq
->cmd
.resp
[0] & CMD_ERRORS
);
1711 * Requests are completed by mmc_blk_mq_complete_rq() which sets simple
1713 * 1. A request that has transferred at least some data is considered
1714 * successful and will be requeued if there is remaining data to
1716 * 2. Otherwise the number of retries is incremented and the request
1717 * will be requeued if there are remaining retries.
1718 * 3. Otherwise the request will be errored out.
1719 * That means mmc_blk_mq_complete_rq() is controlled by bytes_xfered and
1720 * mqrq->retries. So there are only 4 possible actions here:
1721 * 1. do not accept the bytes_xfered value i.e. set it to zero
1722 * 2. change mqrq->retries to determine the number of retries
1723 * 3. try to reset the card
1724 * 4. read one sector at a time
1726 static void mmc_blk_mq_rw_recovery(struct mmc_queue
*mq
, struct request
*req
)
1728 int type
= rq_data_dir(req
) == READ
? MMC_BLK_READ
: MMC_BLK_WRITE
;
1729 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1730 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1731 struct mmc_blk_data
*md
= mq
->blkdata
;
1732 struct mmc_card
*card
= mq
->card
;
1738 * Some errors the host driver might not have seen. Set the number of
1739 * bytes transferred to zero in that case.
1741 err
= __mmc_send_status(card
, &status
, 0);
1742 if (err
|| mmc_blk_status_error(req
, status
))
1743 brq
->data
.bytes_xfered
= 0;
1745 mmc_retune_release(card
->host
);
1748 * Try again to get the status. This also provides an opportunity for
1752 err
= __mmc_send_status(card
, &status
, 0);
1755 * Nothing more to do after the number of bytes transferred has been
1756 * updated and there is no card.
1758 if (err
&& mmc_detect_card_removed(card
->host
))
1761 /* Try to get back to "tran" state */
1762 if (!mmc_host_is_spi(mq
->card
->host
) &&
1763 (err
|| !mmc_ready_for_data(status
)))
1764 err
= mmc_blk_fix_state(mq
->card
, req
);
1767 * Special case for SD cards where the card might record the number of
1770 if (!err
&& mmc_blk_cmd_started(brq
) && mmc_card_sd(card
) &&
1771 rq_data_dir(req
) == WRITE
) {
1772 if (mmc_sd_num_wr_blocks(card
, &blocks
))
1773 brq
->data
.bytes_xfered
= 0;
1775 brq
->data
.bytes_xfered
= blocks
<< 9;
1778 /* Reset if the card is in a bad state */
1779 if (!mmc_host_is_spi(mq
->card
->host
) &&
1780 err
&& mmc_blk_reset(md
, card
->host
, type
)) {
1781 pr_err("%s: recovery failed!\n", req
->rq_disk
->disk_name
);
1782 mqrq
->retries
= MMC_NO_RETRIES
;
1787 * If anything was done, just return and if there is anything remaining
1788 * on the request it will get requeued.
1790 if (brq
->data
.bytes_xfered
)
1793 /* Reset before last retry */
1794 if (mqrq
->retries
+ 1 == MMC_MAX_RETRIES
)
1795 mmc_blk_reset(md
, card
->host
, type
);
1797 /* Command errors fail fast, so use all MMC_MAX_RETRIES */
1798 if (brq
->sbc
.error
|| brq
->cmd
.error
)
1801 /* Reduce the remaining retries for data errors */
1802 if (mqrq
->retries
< MMC_MAX_RETRIES
- MMC_DATA_RETRIES
) {
1803 mqrq
->retries
= MMC_MAX_RETRIES
- MMC_DATA_RETRIES
;
1807 /* FIXME: Missing single sector read for large sector size */
1808 if (!mmc_large_sector(card
) && rq_data_dir(req
) == READ
&&
1809 brq
->data
.blocks
> 1) {
1810 /* Read one sector at a time */
1811 mmc_blk_read_single(mq
, req
);
1816 static inline bool mmc_blk_rq_error(struct mmc_blk_request
*brq
)
1818 mmc_blk_eval_resp_error(brq
);
1820 return brq
->sbc
.error
|| brq
->cmd
.error
|| brq
->stop
.error
||
1821 brq
->data
.error
|| brq
->cmd
.resp
[0] & CMD_ERRORS
;
1824 static int mmc_blk_card_busy(struct mmc_card
*card
, struct request
*req
)
1826 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1830 if (mmc_host_is_spi(card
->host
) || rq_data_dir(req
) == READ
)
1833 err
= card_busy_detect(card
, MMC_BLK_TIMEOUT_MS
, &status
);
1836 * Do not assume data transferred correctly if there are any error bits
1839 if (status
& mmc_blk_stop_err_bits(&mqrq
->brq
)) {
1840 mqrq
->brq
.data
.bytes_xfered
= 0;
1841 err
= err
? err
: -EIO
;
1844 /* Copy the exception bit so it will be seen later on */
1845 if (mmc_card_mmc(card
) && status
& R1_EXCEPTION_EVENT
)
1846 mqrq
->brq
.cmd
.resp
[0] |= R1_EXCEPTION_EVENT
;
1851 static inline void mmc_blk_rw_reset_success(struct mmc_queue
*mq
,
1852 struct request
*req
)
1854 int type
= rq_data_dir(req
) == READ
? MMC_BLK_READ
: MMC_BLK_WRITE
;
1856 mmc_blk_reset_success(mq
->blkdata
, type
);
1859 static void mmc_blk_mq_complete_rq(struct mmc_queue
*mq
, struct request
*req
)
1861 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1862 unsigned int nr_bytes
= mqrq
->brq
.data
.bytes_xfered
;
1865 if (blk_update_request(req
, BLK_STS_OK
, nr_bytes
))
1866 blk_mq_requeue_request(req
, true);
1868 __blk_mq_end_request(req
, BLK_STS_OK
);
1869 } else if (!blk_rq_bytes(req
)) {
1870 __blk_mq_end_request(req
, BLK_STS_IOERR
);
1871 } else if (mqrq
->retries
++ < MMC_MAX_RETRIES
) {
1872 blk_mq_requeue_request(req
, true);
1874 if (mmc_card_removed(mq
->card
))
1875 req
->rq_flags
|= RQF_QUIET
;
1876 blk_mq_end_request(req
, BLK_STS_IOERR
);
1880 static bool mmc_blk_urgent_bkops_needed(struct mmc_queue
*mq
,
1881 struct mmc_queue_req
*mqrq
)
1883 return mmc_card_mmc(mq
->card
) && !mmc_host_is_spi(mq
->card
->host
) &&
1884 (mqrq
->brq
.cmd
.resp
[0] & R1_EXCEPTION_EVENT
||
1885 mqrq
->brq
.stop
.resp
[0] & R1_EXCEPTION_EVENT
);
1888 static void mmc_blk_urgent_bkops(struct mmc_queue
*mq
,
1889 struct mmc_queue_req
*mqrq
)
1891 if (mmc_blk_urgent_bkops_needed(mq
, mqrq
))
1892 mmc_run_bkops(mq
->card
);
1895 static void mmc_blk_hsq_req_done(struct mmc_request
*mrq
)
1897 struct mmc_queue_req
*mqrq
=
1898 container_of(mrq
, struct mmc_queue_req
, brq
.mrq
);
1899 struct request
*req
= mmc_queue_req_to_req(mqrq
);
1900 struct request_queue
*q
= req
->q
;
1901 struct mmc_queue
*mq
= q
->queuedata
;
1902 struct mmc_host
*host
= mq
->card
->host
;
1903 unsigned long flags
;
1905 if (mmc_blk_rq_error(&mqrq
->brq
) ||
1906 mmc_blk_urgent_bkops_needed(mq
, mqrq
)) {
1907 spin_lock_irqsave(&mq
->lock
, flags
);
1908 mq
->recovery_needed
= true;
1909 mq
->recovery_req
= req
;
1910 spin_unlock_irqrestore(&mq
->lock
, flags
);
1912 host
->cqe_ops
->cqe_recovery_start(host
);
1914 schedule_work(&mq
->recovery_work
);
1918 mmc_blk_rw_reset_success(mq
, req
);
1921 * Block layer timeouts race with completions which means the normal
1922 * completion path cannot be used during recovery.
1924 if (mq
->in_recovery
)
1925 mmc_blk_cqe_complete_rq(mq
, req
);
1926 else if (likely(!blk_should_fake_timeout(req
->q
)))
1927 blk_mq_complete_request(req
);
1930 void mmc_blk_mq_complete(struct request
*req
)
1932 struct mmc_queue
*mq
= req
->q
->queuedata
;
1935 mmc_blk_cqe_complete_rq(mq
, req
);
1936 else if (likely(!blk_should_fake_timeout(req
->q
)))
1937 mmc_blk_mq_complete_rq(mq
, req
);
1940 static void mmc_blk_mq_poll_completion(struct mmc_queue
*mq
,
1941 struct request
*req
)
1943 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1944 struct mmc_host
*host
= mq
->card
->host
;
1946 if (mmc_blk_rq_error(&mqrq
->brq
) ||
1947 mmc_blk_card_busy(mq
->card
, req
)) {
1948 mmc_blk_mq_rw_recovery(mq
, req
);
1950 mmc_blk_rw_reset_success(mq
, req
);
1951 mmc_retune_release(host
);
1954 mmc_blk_urgent_bkops(mq
, mqrq
);
1957 static void mmc_blk_mq_dec_in_flight(struct mmc_queue
*mq
, struct request
*req
)
1959 unsigned long flags
;
1962 spin_lock_irqsave(&mq
->lock
, flags
);
1964 mq
->in_flight
[mmc_issue_type(mq
, req
)] -= 1;
1966 put_card
= (mmc_tot_in_flight(mq
) == 0);
1968 spin_unlock_irqrestore(&mq
->lock
, flags
);
1971 mmc_put_card(mq
->card
, &mq
->ctx
);
1974 static void mmc_blk_mq_post_req(struct mmc_queue
*mq
, struct request
*req
)
1976 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
1977 struct mmc_request
*mrq
= &mqrq
->brq
.mrq
;
1978 struct mmc_host
*host
= mq
->card
->host
;
1980 mmc_post_req(host
, mrq
, 0);
1983 * Block layer timeouts race with completions which means the normal
1984 * completion path cannot be used during recovery.
1986 if (mq
->in_recovery
)
1987 mmc_blk_mq_complete_rq(mq
, req
);
1988 else if (likely(!blk_should_fake_timeout(req
->q
)))
1989 blk_mq_complete_request(req
);
1991 mmc_blk_mq_dec_in_flight(mq
, req
);
1994 void mmc_blk_mq_recovery(struct mmc_queue
*mq
)
1996 struct request
*req
= mq
->recovery_req
;
1997 struct mmc_host
*host
= mq
->card
->host
;
1998 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
2000 mq
->recovery_req
= NULL
;
2001 mq
->rw_wait
= false;
2003 if (mmc_blk_rq_error(&mqrq
->brq
)) {
2004 mmc_retune_hold_now(host
);
2005 mmc_blk_mq_rw_recovery(mq
, req
);
2008 mmc_blk_urgent_bkops(mq
, mqrq
);
2010 mmc_blk_mq_post_req(mq
, req
);
2013 static void mmc_blk_mq_complete_prev_req(struct mmc_queue
*mq
,
2014 struct request
**prev_req
)
2016 if (mmc_host_done_complete(mq
->card
->host
))
2019 mutex_lock(&mq
->complete_lock
);
2021 if (!mq
->complete_req
)
2024 mmc_blk_mq_poll_completion(mq
, mq
->complete_req
);
2027 *prev_req
= mq
->complete_req
;
2029 mmc_blk_mq_post_req(mq
, mq
->complete_req
);
2031 mq
->complete_req
= NULL
;
2034 mutex_unlock(&mq
->complete_lock
);
2037 void mmc_blk_mq_complete_work(struct work_struct
*work
)
2039 struct mmc_queue
*mq
= container_of(work
, struct mmc_queue
,
2042 mmc_blk_mq_complete_prev_req(mq
, NULL
);
2045 static void mmc_blk_mq_req_done(struct mmc_request
*mrq
)
2047 struct mmc_queue_req
*mqrq
= container_of(mrq
, struct mmc_queue_req
,
2049 struct request
*req
= mmc_queue_req_to_req(mqrq
);
2050 struct request_queue
*q
= req
->q
;
2051 struct mmc_queue
*mq
= q
->queuedata
;
2052 struct mmc_host
*host
= mq
->card
->host
;
2053 unsigned long flags
;
2055 if (!mmc_host_done_complete(host
)) {
2059 * We cannot complete the request in this context, so record
2060 * that there is a request to complete, and that a following
2061 * request does not need to wait (although it does need to
2062 * complete complete_req first).
2064 spin_lock_irqsave(&mq
->lock
, flags
);
2065 mq
->complete_req
= req
;
2066 mq
->rw_wait
= false;
2067 waiting
= mq
->waiting
;
2068 spin_unlock_irqrestore(&mq
->lock
, flags
);
2071 * If 'waiting' then the waiting task will complete this
2072 * request, otherwise queue a work to do it. Note that
2073 * complete_work may still race with the dispatch of a following
2079 queue_work(mq
->card
->complete_wq
, &mq
->complete_work
);
2084 /* Take the recovery path for errors or urgent background operations */
2085 if (mmc_blk_rq_error(&mqrq
->brq
) ||
2086 mmc_blk_urgent_bkops_needed(mq
, mqrq
)) {
2087 spin_lock_irqsave(&mq
->lock
, flags
);
2088 mq
->recovery_needed
= true;
2089 mq
->recovery_req
= req
;
2090 spin_unlock_irqrestore(&mq
->lock
, flags
);
2092 schedule_work(&mq
->recovery_work
);
2096 mmc_blk_rw_reset_success(mq
, req
);
2098 mq
->rw_wait
= false;
2101 mmc_blk_mq_post_req(mq
, req
);
2104 static bool mmc_blk_rw_wait_cond(struct mmc_queue
*mq
, int *err
)
2106 unsigned long flags
;
2110 * Wait while there is another request in progress, but not if recovery
2111 * is needed. Also indicate whether there is a request waiting to start.
2113 spin_lock_irqsave(&mq
->lock
, flags
);
2114 if (mq
->recovery_needed
) {
2118 done
= !mq
->rw_wait
;
2120 mq
->waiting
= !done
;
2121 spin_unlock_irqrestore(&mq
->lock
, flags
);
2126 static int mmc_blk_rw_wait(struct mmc_queue
*mq
, struct request
**prev_req
)
2130 wait_event(mq
->wait
, mmc_blk_rw_wait_cond(mq
, &err
));
2132 /* Always complete the previous request if there is one */
2133 mmc_blk_mq_complete_prev_req(mq
, prev_req
);
2138 static int mmc_blk_mq_issue_rw_rq(struct mmc_queue
*mq
,
2139 struct request
*req
)
2141 struct mmc_queue_req
*mqrq
= req_to_mmc_queue_req(req
);
2142 struct mmc_host
*host
= mq
->card
->host
;
2143 struct request
*prev_req
= NULL
;
2146 mmc_blk_rw_rq_prep(mqrq
, mq
->card
, 0, mq
);
2148 mqrq
->brq
.mrq
.done
= mmc_blk_mq_req_done
;
2150 mmc_pre_req(host
, &mqrq
->brq
.mrq
);
2152 err
= mmc_blk_rw_wait(mq
, &prev_req
);
2158 err
= mmc_start_request(host
, &mqrq
->brq
.mrq
);
2161 mmc_blk_mq_post_req(mq
, prev_req
);
2164 mq
->rw_wait
= false;
2166 /* Release re-tuning here where there is no synchronization required */
2167 if (err
|| mmc_host_done_complete(host
))
2168 mmc_retune_release(host
);
2172 mmc_post_req(host
, &mqrq
->brq
.mrq
, err
);
2177 static int mmc_blk_wait_for_idle(struct mmc_queue
*mq
, struct mmc_host
*host
)
2180 return host
->cqe_ops
->cqe_wait_for_idle(host
);
2182 return mmc_blk_rw_wait(mq
, NULL
);
2185 enum mmc_issued
mmc_blk_mq_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
2187 struct mmc_blk_data
*md
= mq
->blkdata
;
2188 struct mmc_card
*card
= md
->queue
.card
;
2189 struct mmc_host
*host
= card
->host
;
2192 ret
= mmc_blk_part_switch(card
, md
->part_type
);
2194 return MMC_REQ_FAILED_TO_START
;
2196 switch (mmc_issue_type(mq
, req
)) {
2197 case MMC_ISSUE_SYNC
:
2198 ret
= mmc_blk_wait_for_idle(mq
, host
);
2200 return MMC_REQ_BUSY
;
2201 switch (req_op(req
)) {
2203 case REQ_OP_DRV_OUT
:
2204 mmc_blk_issue_drv_op(mq
, req
);
2206 case REQ_OP_DISCARD
:
2207 mmc_blk_issue_discard_rq(mq
, req
);
2209 case REQ_OP_SECURE_ERASE
:
2210 mmc_blk_issue_secdiscard_rq(mq
, req
);
2213 mmc_blk_issue_flush(mq
, req
);
2217 return MMC_REQ_FAILED_TO_START
;
2219 return MMC_REQ_FINISHED
;
2220 case MMC_ISSUE_DCMD
:
2221 case MMC_ISSUE_ASYNC
:
2222 switch (req_op(req
)) {
2224 ret
= mmc_blk_cqe_issue_flush(mq
, req
);
2229 ret
= mmc_blk_cqe_issue_rw_rq(mq
, req
);
2231 ret
= mmc_blk_mq_issue_rw_rq(mq
, req
);
2238 return MMC_REQ_STARTED
;
2239 return ret
== -EBUSY
? MMC_REQ_BUSY
: MMC_REQ_FAILED_TO_START
;
2242 return MMC_REQ_FAILED_TO_START
;
2246 static inline int mmc_blk_readonly(struct mmc_card
*card
)
2248 return mmc_card_readonly(card
) ||
2249 !(card
->csd
.cmdclass
& CCC_BLOCK_WRITE
);
2252 static struct mmc_blk_data
*mmc_blk_alloc_req(struct mmc_card
*card
,
2253 struct device
*parent
,
2256 const char *subname
,
2259 struct mmc_blk_data
*md
;
2262 devidx
= ida_simple_get(&mmc_blk_ida
, 0, max_devices
, GFP_KERNEL
);
2265 * We get -ENOSPC because there are no more any available
2266 * devidx. The reason may be that, either userspace haven't yet
2267 * unmounted the partitions, which postpones mmc_blk_release()
2268 * from being called, or the device has more partitions than
2271 if (devidx
== -ENOSPC
)
2272 dev_err(mmc_dev(card
->host
),
2273 "no more device IDs available\n");
2275 return ERR_PTR(devidx
);
2278 md
= kzalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
2284 md
->area_type
= area_type
;
2287 * Set the read-only status based on the supported commands
2288 * and the write protect switch.
2290 md
->read_only
= mmc_blk_readonly(card
);
2292 md
->disk
= alloc_disk(perdev_minors
);
2293 if (md
->disk
== NULL
) {
2298 INIT_LIST_HEAD(&md
->part
);
2299 INIT_LIST_HEAD(&md
->rpmbs
);
2302 ret
= mmc_init_queue(&md
->queue
, card
);
2306 md
->queue
.blkdata
= md
;
2309 * Keep an extra reference to the queue so that we can shutdown the
2310 * queue (i.e. call blk_cleanup_queue()) while there are still
2311 * references to the 'md'. The corresponding blk_put_queue() is in
2314 if (!blk_get_queue(md
->queue
.queue
)) {
2315 mmc_cleanup_queue(&md
->queue
);
2320 md
->disk
->major
= MMC_BLOCK_MAJOR
;
2321 md
->disk
->first_minor
= devidx
* perdev_minors
;
2322 md
->disk
->fops
= &mmc_bdops
;
2323 md
->disk
->private_data
= md
;
2324 md
->disk
->queue
= md
->queue
.queue
;
2325 md
->parent
= parent
;
2326 set_disk_ro(md
->disk
, md
->read_only
|| default_ro
);
2327 md
->disk
->flags
= GENHD_FL_EXT_DEVT
;
2328 if (area_type
& (MMC_BLK_DATA_AREA_RPMB
| MMC_BLK_DATA_AREA_BOOT
))
2329 md
->disk
->flags
|= GENHD_FL_NO_PART_SCAN
2330 | GENHD_FL_SUPPRESS_PARTITION_INFO
;
2333 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2335 * - be set for removable media with permanent block devices
2336 * - be unset for removable block devices with permanent media
2338 * Since MMC block devices clearly fall under the second
2339 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2340 * should use the block device creation/destruction hotplug
2341 * messages to tell when the card is present.
2344 snprintf(md
->disk
->disk_name
, sizeof(md
->disk
->disk_name
),
2345 "mmcblk%u%s", card
->host
->index
, subname
? subname
: "");
2347 set_capacity(md
->disk
, size
);
2349 if (mmc_host_cmd23(card
->host
)) {
2350 if ((mmc_card_mmc(card
) &&
2351 card
->csd
.mmca_vsn
>= CSD_SPEC_VER_3
) ||
2352 (mmc_card_sd(card
) &&
2353 card
->scr
.cmds
& SD_SCR_CMD23_SUPPORT
))
2354 md
->flags
|= MMC_BLK_CMD23
;
2357 if (mmc_card_mmc(card
) &&
2358 md
->flags
& MMC_BLK_CMD23
&&
2359 ((card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
) ||
2360 card
->ext_csd
.rel_sectors
)) {
2361 md
->flags
|= MMC_BLK_REL_WR
;
2362 blk_queue_write_cache(md
->queue
.queue
, true, true);
2372 ida_simple_remove(&mmc_blk_ida
, devidx
);
2373 return ERR_PTR(ret
);
2376 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
2380 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
)) {
2382 * The EXT_CSD sector count is in number or 512 byte
2385 size
= card
->ext_csd
.sectors
;
2388 * The CSD capacity field is in units of read_blkbits.
2389 * set_capacity takes units of 512 bytes.
2391 size
= (typeof(sector_t
))card
->csd
.capacity
2392 << (card
->csd
.read_blkbits
- 9);
2395 return mmc_blk_alloc_req(card
, &card
->dev
, size
, false, NULL
,
2396 MMC_BLK_DATA_AREA_MAIN
);
2399 static int mmc_blk_alloc_part(struct mmc_card
*card
,
2400 struct mmc_blk_data
*md
,
2401 unsigned int part_type
,
2404 const char *subname
,
2408 struct mmc_blk_data
*part_md
;
2410 part_md
= mmc_blk_alloc_req(card
, disk_to_dev(md
->disk
), size
, default_ro
,
2411 subname
, area_type
);
2412 if (IS_ERR(part_md
))
2413 return PTR_ERR(part_md
);
2414 part_md
->part_type
= part_type
;
2415 list_add(&part_md
->part
, &md
->part
);
2417 string_get_size((u64
)get_capacity(part_md
->disk
), 512, STRING_UNITS_2
,
2418 cap_str
, sizeof(cap_str
));
2419 pr_info("%s: %s %s partition %u %s\n",
2420 part_md
->disk
->disk_name
, mmc_card_id(card
),
2421 mmc_card_name(card
), part_md
->part_type
, cap_str
);
2426 * mmc_rpmb_ioctl() - ioctl handler for the RPMB chardev
2427 * @filp: the character device file
2428 * @cmd: the ioctl() command
2429 * @arg: the argument from userspace
2431 * This will essentially just redirect the ioctl()s coming in over to
2432 * the main block device spawning the RPMB character device.
2434 static long mmc_rpmb_ioctl(struct file
*filp
, unsigned int cmd
,
2437 struct mmc_rpmb_data
*rpmb
= filp
->private_data
;
2442 ret
= mmc_blk_ioctl_cmd(rpmb
->md
,
2443 (struct mmc_ioc_cmd __user
*)arg
,
2446 case MMC_IOC_MULTI_CMD
:
2447 ret
= mmc_blk_ioctl_multi_cmd(rpmb
->md
,
2448 (struct mmc_ioc_multi_cmd __user
*)arg
,
2459 #ifdef CONFIG_COMPAT
2460 static long mmc_rpmb_ioctl_compat(struct file
*filp
, unsigned int cmd
,
2463 return mmc_rpmb_ioctl(filp
, cmd
, (unsigned long)compat_ptr(arg
));
2467 static int mmc_rpmb_chrdev_open(struct inode
*inode
, struct file
*filp
)
2469 struct mmc_rpmb_data
*rpmb
= container_of(inode
->i_cdev
,
2470 struct mmc_rpmb_data
, chrdev
);
2472 get_device(&rpmb
->dev
);
2473 filp
->private_data
= rpmb
;
2474 mmc_blk_get(rpmb
->md
->disk
);
2476 return nonseekable_open(inode
, filp
);
2479 static int mmc_rpmb_chrdev_release(struct inode
*inode
, struct file
*filp
)
2481 struct mmc_rpmb_data
*rpmb
= container_of(inode
->i_cdev
,
2482 struct mmc_rpmb_data
, chrdev
);
2484 mmc_blk_put(rpmb
->md
);
2485 put_device(&rpmb
->dev
);
2490 static const struct file_operations mmc_rpmb_fileops
= {
2491 .release
= mmc_rpmb_chrdev_release
,
2492 .open
= mmc_rpmb_chrdev_open
,
2493 .owner
= THIS_MODULE
,
2494 .llseek
= no_llseek
,
2495 .unlocked_ioctl
= mmc_rpmb_ioctl
,
2496 #ifdef CONFIG_COMPAT
2497 .compat_ioctl
= mmc_rpmb_ioctl_compat
,
2501 static void mmc_blk_rpmb_device_release(struct device
*dev
)
2503 struct mmc_rpmb_data
*rpmb
= dev_get_drvdata(dev
);
2505 ida_simple_remove(&mmc_rpmb_ida
, rpmb
->id
);
2509 static int mmc_blk_alloc_rpmb_part(struct mmc_card
*card
,
2510 struct mmc_blk_data
*md
,
2511 unsigned int part_index
,
2513 const char *subname
)
2516 char rpmb_name
[DISK_NAME_LEN
];
2518 struct mmc_rpmb_data
*rpmb
;
2520 /* This creates the minor number for the RPMB char device */
2521 devidx
= ida_simple_get(&mmc_rpmb_ida
, 0, max_devices
, GFP_KERNEL
);
2525 rpmb
= kzalloc(sizeof(*rpmb
), GFP_KERNEL
);
2527 ida_simple_remove(&mmc_rpmb_ida
, devidx
);
2531 snprintf(rpmb_name
, sizeof(rpmb_name
),
2532 "mmcblk%u%s", card
->host
->index
, subname
? subname
: "");
2535 rpmb
->part_index
= part_index
;
2536 rpmb
->dev
.init_name
= rpmb_name
;
2537 rpmb
->dev
.bus
= &mmc_rpmb_bus_type
;
2538 rpmb
->dev
.devt
= MKDEV(MAJOR(mmc_rpmb_devt
), rpmb
->id
);
2539 rpmb
->dev
.parent
= &card
->dev
;
2540 rpmb
->dev
.release
= mmc_blk_rpmb_device_release
;
2541 device_initialize(&rpmb
->dev
);
2542 dev_set_drvdata(&rpmb
->dev
, rpmb
);
2545 cdev_init(&rpmb
->chrdev
, &mmc_rpmb_fileops
);
2546 rpmb
->chrdev
.owner
= THIS_MODULE
;
2547 ret
= cdev_device_add(&rpmb
->chrdev
, &rpmb
->dev
);
2549 pr_err("%s: could not add character device\n", rpmb_name
);
2550 goto out_put_device
;
2553 list_add(&rpmb
->node
, &md
->rpmbs
);
2555 string_get_size((u64
)size
, 512, STRING_UNITS_2
,
2556 cap_str
, sizeof(cap_str
));
2558 pr_info("%s: %s %s partition %u %s, chardev (%d:%d)\n",
2559 rpmb_name
, mmc_card_id(card
),
2560 mmc_card_name(card
), EXT_CSD_PART_CONFIG_ACC_RPMB
, cap_str
,
2561 MAJOR(mmc_rpmb_devt
), rpmb
->id
);
2566 put_device(&rpmb
->dev
);
2570 static void mmc_blk_remove_rpmb_part(struct mmc_rpmb_data
*rpmb
)
2573 cdev_device_del(&rpmb
->chrdev
, &rpmb
->dev
);
2574 put_device(&rpmb
->dev
);
2577 /* MMC Physical partitions consist of two boot partitions and
2578 * up to four general purpose partitions.
2579 * For each partition enabled in EXT_CSD a block device will be allocatedi
2580 * to provide access to the partition.
2583 static int mmc_blk_alloc_parts(struct mmc_card
*card
, struct mmc_blk_data
*md
)
2587 if (!mmc_card_mmc(card
))
2590 for (idx
= 0; idx
< card
->nr_parts
; idx
++) {
2591 if (card
->part
[idx
].area_type
& MMC_BLK_DATA_AREA_RPMB
) {
2593 * RPMB partitions does not provide block access, they
2594 * are only accessed using ioctl():s. Thus create
2595 * special RPMB block devices that do not have a
2596 * backing block queue for these.
2598 ret
= mmc_blk_alloc_rpmb_part(card
, md
,
2599 card
->part
[idx
].part_cfg
,
2600 card
->part
[idx
].size
>> 9,
2601 card
->part
[idx
].name
);
2604 } else if (card
->part
[idx
].size
) {
2605 ret
= mmc_blk_alloc_part(card
, md
,
2606 card
->part
[idx
].part_cfg
,
2607 card
->part
[idx
].size
>> 9,
2608 card
->part
[idx
].force_ro
,
2609 card
->part
[idx
].name
,
2610 card
->part
[idx
].area_type
);
2619 static void mmc_blk_remove_req(struct mmc_blk_data
*md
)
2621 struct mmc_card
*card
;
2625 * Flush remaining requests and free queues. It
2626 * is freeing the queue that stops new requests
2627 * from being accepted.
2629 card
= md
->queue
.card
;
2630 if (md
->disk
->flags
& GENHD_FL_UP
) {
2631 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2632 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2633 card
->ext_csd
.boot_ro_lockable
)
2634 device_remove_file(disk_to_dev(md
->disk
),
2635 &md
->power_ro_lock
);
2637 del_gendisk(md
->disk
);
2639 mmc_cleanup_queue(&md
->queue
);
2644 static void mmc_blk_remove_parts(struct mmc_card
*card
,
2645 struct mmc_blk_data
*md
)
2647 struct list_head
*pos
, *q
;
2648 struct mmc_blk_data
*part_md
;
2649 struct mmc_rpmb_data
*rpmb
;
2651 /* Remove RPMB partitions */
2652 list_for_each_safe(pos
, q
, &md
->rpmbs
) {
2653 rpmb
= list_entry(pos
, struct mmc_rpmb_data
, node
);
2655 mmc_blk_remove_rpmb_part(rpmb
);
2657 /* Remove block partitions */
2658 list_for_each_safe(pos
, q
, &md
->part
) {
2659 part_md
= list_entry(pos
, struct mmc_blk_data
, part
);
2661 mmc_blk_remove_req(part_md
);
2665 static int mmc_add_disk(struct mmc_blk_data
*md
)
2668 struct mmc_card
*card
= md
->queue
.card
;
2670 device_add_disk(md
->parent
, md
->disk
, NULL
);
2671 md
->force_ro
.show
= force_ro_show
;
2672 md
->force_ro
.store
= force_ro_store
;
2673 sysfs_attr_init(&md
->force_ro
.attr
);
2674 md
->force_ro
.attr
.name
= "force_ro";
2675 md
->force_ro
.attr
.mode
= S_IRUGO
| S_IWUSR
;
2676 ret
= device_create_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2680 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2681 card
->ext_csd
.boot_ro_lockable
) {
2684 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_DIS
)
2687 mode
= S_IRUGO
| S_IWUSR
;
2689 md
->power_ro_lock
.show
= power_ro_lock_show
;
2690 md
->power_ro_lock
.store
= power_ro_lock_store
;
2691 sysfs_attr_init(&md
->power_ro_lock
.attr
);
2692 md
->power_ro_lock
.attr
.mode
= mode
;
2693 md
->power_ro_lock
.attr
.name
=
2694 "ro_lock_until_next_power_on";
2695 ret
= device_create_file(disk_to_dev(md
->disk
),
2696 &md
->power_ro_lock
);
2698 goto power_ro_lock_fail
;
2703 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2705 del_gendisk(md
->disk
);
2710 #ifdef CONFIG_DEBUG_FS
2712 static int mmc_dbg_card_status_get(void *data
, u64
*val
)
2714 struct mmc_card
*card
= data
;
2715 struct mmc_blk_data
*md
= dev_get_drvdata(&card
->dev
);
2716 struct mmc_queue
*mq
= &md
->queue
;
2717 struct request
*req
;
2720 /* Ask the block layer about the card status */
2721 req
= blk_get_request(mq
->queue
, REQ_OP_DRV_IN
, 0);
2723 return PTR_ERR(req
);
2724 req_to_mmc_queue_req(req
)->drv_op
= MMC_DRV_OP_GET_CARD_STATUS
;
2725 blk_execute_rq(mq
->queue
, NULL
, req
, 0);
2726 ret
= req_to_mmc_queue_req(req
)->drv_op_result
;
2731 blk_put_request(req
);
2735 DEFINE_DEBUGFS_ATTRIBUTE(mmc_dbg_card_status_fops
, mmc_dbg_card_status_get
,
2738 /* That is two digits * 512 + 1 for newline */
2739 #define EXT_CSD_STR_LEN 1025
2741 static int mmc_ext_csd_open(struct inode
*inode
, struct file
*filp
)
2743 struct mmc_card
*card
= inode
->i_private
;
2744 struct mmc_blk_data
*md
= dev_get_drvdata(&card
->dev
);
2745 struct mmc_queue
*mq
= &md
->queue
;
2746 struct request
*req
;
2752 buf
= kmalloc(EXT_CSD_STR_LEN
+ 1, GFP_KERNEL
);
2756 /* Ask the block layer for the EXT CSD */
2757 req
= blk_get_request(mq
->queue
, REQ_OP_DRV_IN
, 0);
2762 req_to_mmc_queue_req(req
)->drv_op
= MMC_DRV_OP_GET_EXT_CSD
;
2763 req_to_mmc_queue_req(req
)->drv_op_data
= &ext_csd
;
2764 blk_execute_rq(mq
->queue
, NULL
, req
, 0);
2765 err
= req_to_mmc_queue_req(req
)->drv_op_result
;
2766 blk_put_request(req
);
2768 pr_err("FAILED %d\n", err
);
2772 for (i
= 0; i
< 512; i
++)
2773 n
+= sprintf(buf
+ n
, "%02x", ext_csd
[i
]);
2774 n
+= sprintf(buf
+ n
, "\n");
2776 if (n
!= EXT_CSD_STR_LEN
) {
2782 filp
->private_data
= buf
;
2791 static ssize_t
mmc_ext_csd_read(struct file
*filp
, char __user
*ubuf
,
2792 size_t cnt
, loff_t
*ppos
)
2794 char *buf
= filp
->private_data
;
2796 return simple_read_from_buffer(ubuf
, cnt
, ppos
,
2797 buf
, EXT_CSD_STR_LEN
);
2800 static int mmc_ext_csd_release(struct inode
*inode
, struct file
*file
)
2802 kfree(file
->private_data
);
2806 static const struct file_operations mmc_dbg_ext_csd_fops
= {
2807 .open
= mmc_ext_csd_open
,
2808 .read
= mmc_ext_csd_read
,
2809 .release
= mmc_ext_csd_release
,
2810 .llseek
= default_llseek
,
2813 static int mmc_blk_add_debugfs(struct mmc_card
*card
, struct mmc_blk_data
*md
)
2815 struct dentry
*root
;
2817 if (!card
->debugfs_root
)
2820 root
= card
->debugfs_root
;
2822 if (mmc_card_mmc(card
) || mmc_card_sd(card
)) {
2824 debugfs_create_file_unsafe("status", 0400, root
,
2826 &mmc_dbg_card_status_fops
);
2827 if (!md
->status_dentry
)
2831 if (mmc_card_mmc(card
)) {
2832 md
->ext_csd_dentry
=
2833 debugfs_create_file("ext_csd", S_IRUSR
, root
, card
,
2834 &mmc_dbg_ext_csd_fops
);
2835 if (!md
->ext_csd_dentry
)
2842 static void mmc_blk_remove_debugfs(struct mmc_card
*card
,
2843 struct mmc_blk_data
*md
)
2845 if (!card
->debugfs_root
)
2848 if (!IS_ERR_OR_NULL(md
->status_dentry
)) {
2849 debugfs_remove(md
->status_dentry
);
2850 md
->status_dentry
= NULL
;
2853 if (!IS_ERR_OR_NULL(md
->ext_csd_dentry
)) {
2854 debugfs_remove(md
->ext_csd_dentry
);
2855 md
->ext_csd_dentry
= NULL
;
2861 static int mmc_blk_add_debugfs(struct mmc_card
*card
, struct mmc_blk_data
*md
)
2866 static void mmc_blk_remove_debugfs(struct mmc_card
*card
,
2867 struct mmc_blk_data
*md
)
2871 #endif /* CONFIG_DEBUG_FS */
2873 static int mmc_blk_probe(struct mmc_card
*card
)
2875 struct mmc_blk_data
*md
, *part_md
;
2879 * Check that the card supports the command class(es) we need.
2881 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
2884 mmc_fixup_device(card
, mmc_blk_fixups
);
2886 card
->complete_wq
= alloc_workqueue("mmc_complete",
2887 WQ_MEM_RECLAIM
| WQ_HIGHPRI
, 0);
2888 if (unlikely(!card
->complete_wq
)) {
2889 pr_err("Failed to create mmc completion workqueue");
2893 md
= mmc_blk_alloc(card
);
2897 string_get_size((u64
)get_capacity(md
->disk
), 512, STRING_UNITS_2
,
2898 cap_str
, sizeof(cap_str
));
2899 pr_info("%s: %s %s %s %s\n",
2900 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
2901 cap_str
, md
->read_only
? "(ro)" : "");
2903 if (mmc_blk_alloc_parts(card
, md
))
2906 dev_set_drvdata(&card
->dev
, md
);
2908 if (mmc_add_disk(md
))
2911 list_for_each_entry(part_md
, &md
->part
, part
) {
2912 if (mmc_add_disk(part_md
))
2916 /* Add two debugfs entries */
2917 mmc_blk_add_debugfs(card
, md
);
2919 pm_runtime_set_autosuspend_delay(&card
->dev
, 3000);
2920 pm_runtime_use_autosuspend(&card
->dev
);
2923 * Don't enable runtime PM for SD-combo cards here. Leave that
2924 * decision to be taken during the SDIO init sequence instead.
2926 if (card
->type
!= MMC_TYPE_SD_COMBO
) {
2927 pm_runtime_set_active(&card
->dev
);
2928 pm_runtime_enable(&card
->dev
);
2934 mmc_blk_remove_parts(card
, md
);
2935 mmc_blk_remove_req(md
);
2939 static void mmc_blk_remove(struct mmc_card
*card
)
2941 struct mmc_blk_data
*md
= dev_get_drvdata(&card
->dev
);
2943 mmc_blk_remove_debugfs(card
, md
);
2944 mmc_blk_remove_parts(card
, md
);
2945 pm_runtime_get_sync(&card
->dev
);
2946 if (md
->part_curr
!= md
->part_type
) {
2947 mmc_claim_host(card
->host
);
2948 mmc_blk_part_switch(card
, md
->part_type
);
2949 mmc_release_host(card
->host
);
2951 if (card
->type
!= MMC_TYPE_SD_COMBO
)
2952 pm_runtime_disable(&card
->dev
);
2953 pm_runtime_put_noidle(&card
->dev
);
2954 mmc_blk_remove_req(md
);
2955 dev_set_drvdata(&card
->dev
, NULL
);
2956 destroy_workqueue(card
->complete_wq
);
2959 static int _mmc_blk_suspend(struct mmc_card
*card
)
2961 struct mmc_blk_data
*part_md
;
2962 struct mmc_blk_data
*md
= dev_get_drvdata(&card
->dev
);
2965 mmc_queue_suspend(&md
->queue
);
2966 list_for_each_entry(part_md
, &md
->part
, part
) {
2967 mmc_queue_suspend(&part_md
->queue
);
2973 static void mmc_blk_shutdown(struct mmc_card
*card
)
2975 _mmc_blk_suspend(card
);
2978 #ifdef CONFIG_PM_SLEEP
2979 static int mmc_blk_suspend(struct device
*dev
)
2981 struct mmc_card
*card
= mmc_dev_to_card(dev
);
2983 return _mmc_blk_suspend(card
);
2986 static int mmc_blk_resume(struct device
*dev
)
2988 struct mmc_blk_data
*part_md
;
2989 struct mmc_blk_data
*md
= dev_get_drvdata(dev
);
2993 * Resume involves the card going into idle state,
2994 * so current partition is always the main one.
2996 md
->part_curr
= md
->part_type
;
2997 mmc_queue_resume(&md
->queue
);
2998 list_for_each_entry(part_md
, &md
->part
, part
) {
2999 mmc_queue_resume(&part_md
->queue
);
3006 static SIMPLE_DEV_PM_OPS(mmc_blk_pm_ops
, mmc_blk_suspend
, mmc_blk_resume
);
3008 static struct mmc_driver mmc_driver
= {
3011 .pm
= &mmc_blk_pm_ops
,
3013 .probe
= mmc_blk_probe
,
3014 .remove
= mmc_blk_remove
,
3015 .shutdown
= mmc_blk_shutdown
,
3018 static int __init
mmc_blk_init(void)
3022 res
= bus_register(&mmc_rpmb_bus_type
);
3024 pr_err("mmcblk: could not register RPMB bus type\n");
3027 res
= alloc_chrdev_region(&mmc_rpmb_devt
, 0, MAX_DEVICES
, "rpmb");
3029 pr_err("mmcblk: failed to allocate rpmb chrdev region\n");
3033 if (perdev_minors
!= CONFIG_MMC_BLOCK_MINORS
)
3034 pr_info("mmcblk: using %d minors per device\n", perdev_minors
);
3036 max_devices
= min(MAX_DEVICES
, (1 << MINORBITS
) / perdev_minors
);
3038 res
= register_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3040 goto out_chrdev_unreg
;
3042 res
= mmc_register_driver(&mmc_driver
);
3044 goto out_blkdev_unreg
;
3049 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3051 unregister_chrdev_region(mmc_rpmb_devt
, MAX_DEVICES
);
3053 bus_unregister(&mmc_rpmb_bus_type
);
3057 static void __exit
mmc_blk_exit(void)
3059 mmc_unregister_driver(&mmc_driver
);
3060 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3061 unregister_chrdev_region(mmc_rpmb_devt
, MAX_DEVICES
);
3062 bus_unregister(&mmc_rpmb_bus_type
);
3065 module_init(mmc_blk_init
);
3066 module_exit(mmc_blk_exit
);
3068 MODULE_LICENSE("GPL");
3069 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");