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/mutex.h>
32 #include <linux/scatterlist.h>
33 #include <linux/string_helpers.h>
34 #include <linux/delay.h>
35 #include <linux/capability.h>
36 #include <linux/compat.h>
38 #define CREATE_TRACE_POINTS
39 #include <trace/events/mmc.h>
41 #include <linux/mmc/ioctl.h>
42 #include <linux/mmc/card.h>
43 #include <linux/mmc/host.h>
44 #include <linux/mmc/mmc.h>
45 #include <linux/mmc/sd.h>
47 #include <asm/uaccess.h>
50 #include <mach/mtk_meminfo.h>
52 //add vmstat info with block tag log
53 #include <linux/vmstat.h>
54 #define FEATURE_STORAGE_VMSTAT_LOGGER
57 #include <linux/xlog.h>
58 #include <asm/div64.h>
59 #include <linux/vmalloc.h>
61 #include <linux/mmc/sd_misc.h>
63 #define MET_USER_EVENT_SUPPORT
64 #include <linux/met_drv.h>
66 #define FEATURE_STORAGE_PERF_INDEX
67 //enable storage log in user load
69 #ifdef USER_BUILD_KERNEL
70 #undef FEATURE_STORAGE_PERF_INDEX
74 MODULE_ALIAS("mmc:block");
75 #ifdef MODULE_PARAM_PREFIX
76 #undef MODULE_PARAM_PREFIX
78 #define MODULE_PARAM_PREFIX "mmcblk."
80 #define INAND_CMD38_ARG_EXT_CSD 113
81 #define INAND_CMD38_ARG_ERASE 0x00
82 #define INAND_CMD38_ARG_TRIM 0x01
83 #define INAND_CMD38_ARG_SECERASE 0x80
84 #define INAND_CMD38_ARG_SECTRIM1 0x81
85 #define INAND_CMD38_ARG_SECTRIM2 0x88
86 #define MMC_BLK_TIMEOUT_MS (10 * 60 * 1000) /* 10 minute timeout */
88 #define mmc_req_rel_wr(req) (((req->cmd_flags & REQ_FUA) || \
89 (req->cmd_flags & REQ_META)) && \
90 (rq_data_dir(req) == WRITE))
91 #define PACKED_CMD_VER 0x01
92 #define PACKED_CMD_WR 0x02
94 static DEFINE_MUTEX(block_mutex
);
97 * The defaults come from config options but can be overriden by module
100 static int perdev_minors
= CONFIG_MMC_BLOCK_MINORS
;
103 * We've only got one major, so number of mmcblk devices is
104 * limited to 256 / number of minors per device.
106 static int max_devices
;
108 /* 256 minors, so at most 256 separate devices */
109 static DECLARE_BITMAP(dev_use
, 256);
110 static DECLARE_BITMAP(name_use
, 256);
113 * There is one mmc_blk_data per slot.
115 struct mmc_blk_data
{
117 struct gendisk
*disk
;
118 struct mmc_queue queue
;
119 struct list_head part
;
122 #define MMC_BLK_CMD23 (1 << 0) /* Can do SET_BLOCK_COUNT for multiblock */
123 #define MMC_BLK_REL_WR (1 << 1) /* MMC Reliable write support */
124 #define MMC_BLK_PACKED_CMD (1 << 2) /* MMC packed command support */
127 unsigned int read_only
;
128 unsigned int part_type
;
129 unsigned int name_idx
;
130 unsigned int reset_done
;
131 #define MMC_BLK_READ BIT(0)
132 #define MMC_BLK_WRITE BIT(1)
133 #define MMC_BLK_DISCARD BIT(2)
134 #define MMC_BLK_SECDISCARD BIT(3)
137 * Only set in main mmc_blk_data associated
138 * with mmc_card with mmc_set_drvdata, and keeps
139 * track of the current selected device partition.
141 unsigned int part_curr
;
142 struct device_attribute force_ro
;
143 struct device_attribute power_ro_lock
;
147 static DEFINE_MUTEX(open_lock
);
150 MMC_PACKED_NR_IDX
= -1,
152 MMC_PACKED_NR_SINGLE
,
155 module_param(perdev_minors
, int, 0444);
156 MODULE_PARM_DESC(perdev_minors
, "Minors numbers to allocate per device");
158 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
159 struct mmc_blk_data
*md
);
160 static int get_card_status(struct mmc_card
*card
, u32
*status
, int retries
);
162 #ifndef CONFIG_MTK_FPGA
163 #include <linux/met_ftrace_bio.h>
166 char mmc_get_rw_type(u32 opcode
)
170 case MMC_READ_SINGLE_BLOCK
:
171 case MMC_READ_MULTIPLE_BLOCK
:
173 case MMC_WRITE_BLOCK
:
174 case MMC_WRITE_MULTIPLE_BLOCK
:
182 inline int check_met_mmc_async_req_legal(struct mmc_host
*host
, struct mmc_async_req
*areq
)
186 if (!((host
== NULL
) || (areq
== NULL
) || (areq
->mrq
== NULL
)
187 || (areq
->mrq
->cmd
== NULL
) || (areq
->mrq
->data
== NULL
)
188 || (host
->card
== NULL
))) {
195 inline int check_met_mmc_blk_data_legal(struct mmc_blk_data
*md
)
199 if (!((md
== NULL
) || (md
->disk
== NULL
))) {
206 inline int check_met_mmc_req_legal(struct mmc_host
*host
, struct mmc_request
*req
)
210 if (!((host
== NULL
) || (req
== NULL
) || (req
->cmd
== NULL
)
211 || (req
->data
== NULL
) || (host
->card
== NULL
))) {
218 void met_mmc_insert(struct mmc_host
*host
, struct mmc_async_req
*areq
)
220 struct mmc_blk_data
*md
;
223 if (!check_met_mmc_async_req_legal(host
, areq
))
226 md
= mmc_get_drvdata(host
->card
);
227 if (!check_met_mmc_blk_data_legal(md
))
230 type
= mmc_get_rw_type(areq
->mrq
->cmd
->opcode
);
234 #ifndef CONFIG_MTK_FPGA
235 MET_FTRACE_PRINTK(met_mmc_insert
, md
, areq
, type
);
239 void met_mmc_dma_map(struct mmc_host
*host
, struct mmc_async_req
*areq
)
241 struct mmc_blk_data
*md
;
244 if (!check_met_mmc_async_req_legal(host
, areq
))
247 md
= mmc_get_drvdata(host
->card
);
248 if (!check_met_mmc_blk_data_legal(md
))
251 type
= mmc_get_rw_type(areq
->mrq
->cmd
->opcode
);
254 #ifndef CONFIG_MTK_FPGA
255 MET_FTRACE_PRINTK(met_mmc_dma_map
, md
, areq
, type
);
259 //void met_mmc_issue(struct mmc_host *host, struct mmc_async_req *areq)
261 // struct mmc_blk_data *md;
264 // if (!check_met_mmc_async_req_legal(host, areq))
267 // md = mmc_get_drvdata(host->card);
269 // type = mmc_get_rw_type(areq->mrq->cmd->opcode);
273 // MET_FTRACE_PRINTK(met_mmc_issue, md, areq, type);
276 void met_mmc_issue(struct mmc_host
*host
, struct mmc_request
*req
)
278 struct mmc_blk_data
*md
;
281 if (!check_met_mmc_req_legal(host
, req
))
284 md
= mmc_get_drvdata(host
->card
);
285 if (!check_met_mmc_blk_data_legal(md
))
288 type
= mmc_get_rw_type(req
->cmd
->opcode
);
291 #ifndef CONFIG_MTK_FPGA
292 MET_FTRACE_PRINTK(met_mmc_issue
, md
, req
, type
);
296 void met_mmc_send_cmd(struct mmc_host
*host
, struct mmc_command
*cmd
)
298 struct mmc_blk_data
*md
= mmc_get_drvdata(host
->card
);
301 type
= mmc_get_rw_type(cmd
->opcode
);
305 trace_printk("%d,%d %c %d + %d [%s]\n",
306 md
->disk
->major
, md
->disk
->first_minor
, type
,
307 cmd
->arg
, cmd
->data
->blocks
,
311 void met_mmc_xfr_done(struct mmc_host
*host
, struct mmc_command
*cmd
)
313 struct mmc_blk_data
*md
=mmc_get_drvdata(host
->card
);
316 type
= mmc_get_rw_type(cmd
->opcode
);
320 trace_printk("%d,%d %c %d + %d [%s]\n",
321 md
->disk
->major
, md
->disk
->first_minor
, type
,
322 cmd
->arg
, cmd
->data
->blocks
,
326 void met_mmc_wait_xfr(struct mmc_host
*host
, struct mmc_async_req
*areq
)
328 struct mmc_blk_data
*md
= mmc_get_drvdata(host
->card
);
331 type
= mmc_get_rw_type(areq
->mrq
->cmd
->opcode
);
335 trace_printk("%d,%d %c %d + %d [%s]\n",
336 md
->disk
->major
, md
->disk
->first_minor
, type
,
337 areq
->mrq
->cmd
->arg
, areq
->mrq
->data
->blocks
,
342 void met_mmc_tuning_start(struct mmc_host
*host
, struct mmc_command
*cmd
)
344 struct mmc_blk_data
*md
= mmc_get_drvdata(host
->card
);
347 type
= mmc_get_rw_type(cmd
->opcode
);
351 trace_printk("%d,%d %c %d + %d [%s]\n",
352 md
->disk
->major
, md
->disk
->first_minor
, type
,
353 cmd
->arg
, cmd
->data
->blocks
,
357 void met_mmc_tuning_end(struct mmc_host
*host
, struct mmc_command
*cmd
)
359 struct mmc_blk_data
*md
= mmc_get_drvdata(host
->card
);
362 type
= mmc_get_rw_type(cmd
->opcode
);
366 trace_printk("%d,%d %c %d + %d [%s]\n",
367 md
->disk
->major
, md
->disk
->first_minor
, type
,
368 cmd
->arg
, cmd
->data
->blocks
,
372 void met_mmc_complete(struct mmc_host
*host
, struct mmc_async_req
*areq
)
374 struct mmc_blk_data
*md
;
377 if (!check_met_mmc_async_req_legal(host
, areq
))
380 md
= mmc_get_drvdata(host
->card
);
381 if (!check_met_mmc_blk_data_legal(md
))
384 type
= mmc_get_rw_type(areq
->mrq
->cmd
->opcode
);
387 #ifndef CONFIG_MTK_FPGA
388 MET_FTRACE_PRINTK(met_mmc_complete
, md
, areq
, type
);
392 void met_mmc_dma_unmap_start(struct mmc_host
*host
, struct mmc_async_req
*areq
)
394 struct mmc_blk_data
*md
;
397 if (!check_met_mmc_async_req_legal(host
, areq
))
400 md
= mmc_get_drvdata(host
->card
);
401 if (!check_met_mmc_blk_data_legal(md
))
404 type
= mmc_get_rw_type(areq
->mrq
->cmd
->opcode
);
407 #ifndef CONFIG_MTK_FPGA
408 MET_FTRACE_PRINTK(met_mmc_dma_unmap_start
, md
, areq
, type
);
412 void met_mmc_dma_unmap_stop(struct mmc_host
*host
, struct mmc_async_req
*areq
)
414 struct mmc_blk_data
*md
;
417 if (!check_met_mmc_async_req_legal(host
, areq
))
420 md
= mmc_get_drvdata(host
->card
);
421 if (!check_met_mmc_blk_data_legal(md
))
424 type
= mmc_get_rw_type(areq
->mrq
->cmd
->opcode
);
427 #ifndef CONFIG_MTK_FPGA
428 MET_FTRACE_PRINTK(met_mmc_dma_unmap_stop
, md
, areq
, type
);
432 void met_mmc_continue_req_end(struct mmc_host
*host
, struct mmc_async_req
*areq
)
434 struct mmc_blk_data
*md
;
437 if (!check_met_mmc_async_req_legal(host
, areq
))
440 md
= mmc_get_drvdata(host
->card
);
441 if (!check_met_mmc_blk_data_legal(md
))
444 type
= mmc_get_rw_type(areq
->mrq
->cmd
->opcode
);
447 #ifndef CONFIG_MTK_FPGA
448 MET_FTRACE_PRINTK(met_mmc_continue_req_end
, md
, areq
, type
);
452 void met_mmc_dma_stop(struct mmc_host
*host
, struct mmc_async_req
*areq
, unsigned int bd_num
)
454 struct mmc_blk_data
*md
;
457 if (!check_met_mmc_async_req_legal(host
, areq
))
460 md
= mmc_get_drvdata(host
->card
);
461 if (!check_met_mmc_blk_data_legal(md
))
464 type
= mmc_get_rw_type(areq
->mrq
->cmd
->opcode
);
467 #ifndef CONFIG_MTK_FPGA
468 MET_FTRACE_PRINTK(met_mmc_dma_stop
, md
, areq
, type
, bd_num
);
472 //void met_mmc_end(struct mmc_host *host, struct mmc_async_req *areq)
474 // struct mmc_blk_data *md;
477 // if (areq && areq->mrq && host && host->card) {
478 // type = mmc_get_rw_type(areq->mrq->cmd->opcode);
482 // md = mmc_get_drvdata(host->card);
484 // if (areq && areq->mrq)
486 // trace_printk("%d,%d %c %d + %d [%s]\n",
487 // md->disk->major, md->disk->first_minor, type,
488 // areq->mrq->cmd->arg, areq->mrq->data->blocks,
494 static inline void mmc_blk_clear_packed(struct mmc_queue_req
*mqrq
)
496 struct mmc_packed
*packed
= mqrq
->packed
;
500 mqrq
->cmd_type
= MMC_PACKED_NONE
;
501 packed
->nr_entries
= MMC_PACKED_NR_ZERO
;
502 packed
->idx_failure
= MMC_PACKED_NR_IDX
;
507 static struct mmc_blk_data
*mmc_blk_get(struct gendisk
*disk
)
509 struct mmc_blk_data
*md
;
511 mutex_lock(&open_lock
);
512 md
= disk
->private_data
;
513 if (md
&& md
->usage
== 0)
517 mutex_unlock(&open_lock
);
522 static inline int mmc_get_devidx(struct gendisk
*disk
)
524 int devidx
= disk
->first_minor
/ perdev_minors
;
528 static void mmc_blk_put(struct mmc_blk_data
*md
)
530 mutex_lock(&open_lock
);
532 if (md
->usage
== 0) {
533 int devidx
= mmc_get_devidx(md
->disk
);
534 blk_cleanup_queue(md
->queue
.queue
);
536 __clear_bit(devidx
, dev_use
);
541 mutex_unlock(&open_lock
);
544 static ssize_t
power_ro_lock_show(struct device
*dev
,
545 struct device_attribute
*attr
, char *buf
)
548 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
549 struct mmc_card
*card
= md
->queue
.card
;
552 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PERM_WP_EN
)
554 else if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_EN
)
557 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n", locked
);
562 static ssize_t
power_ro_lock_store(struct device
*dev
,
563 struct device_attribute
*attr
, const char *buf
, size_t count
)
566 struct mmc_blk_data
*md
, *part_md
;
567 struct mmc_card
*card
;
570 if (kstrtoul(buf
, 0, &set
))
576 md
= mmc_blk_get(dev_to_disk(dev
));
577 card
= md
->queue
.card
;
579 mmc_claim_host(card
->host
);
581 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BOOT_WP
,
582 card
->ext_csd
.boot_ro_lock
|
583 EXT_CSD_BOOT_WP_B_PWR_WP_EN
,
584 card
->ext_csd
.part_time
);
586 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md
->disk
->disk_name
, ret
);
588 card
->ext_csd
.boot_ro_lock
|= EXT_CSD_BOOT_WP_B_PWR_WP_EN
;
590 mmc_release_host(card
->host
);
593 pr_info("%s: Locking boot partition ro until next power on\n",
594 md
->disk
->disk_name
);
595 set_disk_ro(md
->disk
, 1);
597 list_for_each_entry(part_md
, &md
->part
, part
)
598 if (part_md
->area_type
== MMC_BLK_DATA_AREA_BOOT
) {
599 pr_info("%s: Locking boot partition ro until next power on\n", part_md
->disk
->disk_name
);
600 set_disk_ro(part_md
->disk
, 1);
608 static ssize_t
force_ro_show(struct device
*dev
, struct device_attribute
*attr
,
612 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
614 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n",
615 get_disk_ro(dev_to_disk(dev
)) ^
621 static ssize_t
force_ro_store(struct device
*dev
, struct device_attribute
*attr
,
622 const char *buf
, size_t count
)
626 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
627 unsigned long set
= simple_strtoul(buf
, &end
, 0);
633 set_disk_ro(dev_to_disk(dev
), set
|| md
->read_only
);
640 static int mmc_blk_open(struct block_device
*bdev
, fmode_t mode
)
642 struct mmc_blk_data
*md
= mmc_blk_get(bdev
->bd_disk
);
645 mutex_lock(&block_mutex
);
648 check_disk_change(bdev
);
651 if ((mode
& FMODE_WRITE
) && md
->read_only
) {
656 mutex_unlock(&block_mutex
);
661 static void mmc_blk_release(struct gendisk
*disk
, fmode_t mode
)
663 struct mmc_blk_data
*md
= disk
->private_data
;
665 mutex_lock(&block_mutex
);
667 mutex_unlock(&block_mutex
);
671 mmc_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
673 geo
->cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
679 struct mmc_blk_ioc_data
{
680 struct mmc_ioc_cmd ic
;
685 static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
686 struct mmc_ioc_cmd __user
*user
)
688 struct mmc_blk_ioc_data
*idata
;
691 idata
= kzalloc(sizeof(*idata
), GFP_KERNEL
);
697 if (copy_from_user(&idata
->ic
, user
, sizeof(idata
->ic
))) {
702 idata
->buf_bytes
= (u64
) idata
->ic
.blksz
* idata
->ic
.blocks
;
703 if (idata
->buf_bytes
> MMC_IOC_MAX_BYTES
) {
708 if (!idata
->buf_bytes
)
711 idata
->buf
= kzalloc(idata
->buf_bytes
, GFP_KERNEL
);
717 if (copy_from_user(idata
->buf
, (void __user
*)(unsigned long)
718 idata
->ic
.data_ptr
, idata
->buf_bytes
)) {
733 static int ioctl_rpmb_card_status_poll(struct mmc_card
*card
, u32
*status
,
739 if (!status
|| !retries_max
)
743 err
= get_card_status(card
, status
, 5);
747 if (!R1_STATUS(*status
) &&
748 (R1_CURRENT_STATE(*status
) != R1_STATE_PRG
))
749 break; /* RPMB programming operation complete */
752 * Rechedule to give the MMC device a chance to continue
753 * processing the previous command without being polled too
756 usleep_range(1000, 5000);
757 } while (++retry_count
< retries_max
);
759 if (retry_count
== retries_max
)
765 static int mmc_blk_ioctl_cmd(struct block_device
*bdev
,
766 struct mmc_ioc_cmd __user
*ic_ptr
)
768 struct mmc_blk_ioc_data
*idata
;
769 struct mmc_blk_data
*md
;
770 struct mmc_card
*card
;
771 struct mmc_command cmd
= {0};
772 struct mmc_data data
= {0};
773 struct mmc_request mrq
= {NULL
};
774 struct scatterlist sg
;
780 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
781 * whole block device, not on a partition. This prevents overspray
782 * between sibling partitions.
784 if ((!capable(CAP_SYS_RAWIO
)) || (bdev
!= bdev
->bd_contains
))
787 idata
= mmc_blk_ioctl_copy_from_user(ic_ptr
);
789 return PTR_ERR(idata
);
791 md
= mmc_blk_get(bdev
->bd_disk
);
797 if (md
->area_type
& MMC_BLK_DATA_AREA_RPMB
)
800 card
= md
->queue
.card
;
806 cmd
.opcode
= idata
->ic
.opcode
;
807 cmd
.arg
= idata
->ic
.arg
;
808 cmd
.flags
= idata
->ic
.flags
;
810 if (idata
->buf_bytes
) {
813 data
.blksz
= idata
->ic
.blksz
;
814 data
.blocks
= idata
->ic
.blocks
;
816 sg_init_one(data
.sg
, idata
->buf
, idata
->buf_bytes
);
818 if (idata
->ic
.write_flag
)
819 data
.flags
= MMC_DATA_WRITE
;
821 data
.flags
= MMC_DATA_READ
;
823 /* data.flags must already be set before doing this. */
824 mmc_set_data_timeout(&data
, card
);
826 /* Allow overriding the timeout_ns for empirical tuning. */
827 if (idata
->ic
.data_timeout_ns
)
828 data
.timeout_ns
= idata
->ic
.data_timeout_ns
;
830 if ((cmd
.flags
& MMC_RSP_R1B
) == MMC_RSP_R1B
) {
832 * Pretend this is a data transfer and rely on the
833 * host driver to compute timeout. When all host
834 * drivers support cmd.cmd_timeout for R1B, this
838 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
840 data
.timeout_ns
= idata
->ic
.cmd_timeout_ms
* 1000000;
848 mmc_claim_host(card
->host
);
850 err
= mmc_blk_part_switch(card
, md
);
854 if (idata
->ic
.is_acmd
) {
855 err
= mmc_app_cmd(card
->host
, card
);
861 err
= mmc_set_blockcount(card
, data
.blocks
,
862 idata
->ic
.write_flag
& (1 << 31));
867 mmc_wait_for_req(card
->host
, &mrq
);
870 dev_err(mmc_dev(card
->host
), "%s: cmd error %d\n",
871 __func__
, cmd
.error
);
876 dev_err(mmc_dev(card
->host
), "%s: data error %d\n",
877 __func__
, data
.error
);
883 * According to the SD specs, some commands require a delay after
884 * issuing the command.
886 if (idata
->ic
.postsleep_min_us
)
887 usleep_range(idata
->ic
.postsleep_min_us
, idata
->ic
.postsleep_max_us
);
889 if (copy_to_user(&(ic_ptr
->response
), cmd
.resp
, sizeof(cmd
.resp
))) {
894 if (!idata
->ic
.write_flag
) {
895 if (copy_to_user((void __user
*)(unsigned long) idata
->ic
.data_ptr
,
896 idata
->buf
, idata
->buf_bytes
)) {
904 * Ensure RPMB command has completed by polling CMD13
907 err
= ioctl_rpmb_card_status_poll(card
, &status
, 5);
909 dev_err(mmc_dev(card
->host
),
910 "%s: Card Status=0x%08X, error %d\n",
911 __func__
, status
, err
);
915 mmc_release_host(card
->host
);
925 static int mmc_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
926 unsigned int cmd
, unsigned long arg
)
929 if (cmd
== MMC_IOC_CMD
)
930 ret
= mmc_blk_ioctl_cmd(bdev
, (struct mmc_ioc_cmd __user
*)arg
);
935 static int mmc_blk_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
936 unsigned int cmd
, unsigned long arg
)
938 return mmc_blk_ioctl(bdev
, mode
, cmd
, (unsigned long) compat_ptr(arg
));
942 static const struct block_device_operations mmc_bdops
= {
943 .open
= mmc_blk_open
,
944 .release
= mmc_blk_release
,
945 .getgeo
= mmc_blk_getgeo
,
946 .owner
= THIS_MODULE
,
947 .ioctl
= mmc_blk_ioctl
,
949 .compat_ioctl
= mmc_blk_compat_ioctl
,
953 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
954 struct mmc_blk_data
*md
)
957 struct mmc_blk_data
*main_md
= mmc_get_drvdata(card
);
959 if (main_md
->part_curr
== md
->part_type
)
962 if (mmc_card_mmc(card
)) {
963 u8 part_config
= card
->ext_csd
.part_config
;
965 part_config
&= ~EXT_CSD_PART_CONFIG_ACC_MASK
;
966 part_config
|= md
->part_type
;
968 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
969 EXT_CSD_PART_CONFIG
, part_config
,
970 card
->ext_csd
.part_time
);
974 card
->ext_csd
.part_config
= part_config
;
977 main_md
->part_curr
= md
->part_type
;
981 static u32
mmc_sd_num_wr_blocks(struct mmc_card
*card
)
987 struct mmc_request mrq
= {NULL
};
988 struct mmc_command cmd
= {0};
989 struct mmc_data data
= {0};
991 struct scatterlist sg
;
993 cmd
.opcode
= MMC_APP_CMD
;
994 cmd
.arg
= card
->rca
<< 16;
995 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
997 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
1000 if (!mmc_host_is_spi(card
->host
) && !(cmd
.resp
[0] & R1_APP_CMD
))
1003 memset(&cmd
, 0, sizeof(struct mmc_command
));
1005 cmd
.opcode
= SD_APP_SEND_NUM_WR_BLKS
;
1007 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
1011 data
.flags
= MMC_DATA_READ
;
1014 mmc_set_data_timeout(&data
, card
);
1019 blocks
= kmalloc(4, GFP_KERNEL
);
1023 sg_init_one(&sg
, blocks
, 4);
1025 mmc_wait_for_req(card
->host
, &mrq
);
1027 result
= ntohl(*blocks
);
1030 if (cmd
.error
|| data
.error
)
1036 u32
__mmc_sd_num_wr_blocks(struct mmc_card
*card
)
1038 return mmc_sd_num_wr_blocks(card
);
1040 EXPORT_SYMBOL(__mmc_sd_num_wr_blocks
);
1042 static int send_stop(struct mmc_card
*card
, u32
*status
)
1044 struct mmc_command cmd
= {0};
1047 cmd
.opcode
= MMC_STOP_TRANSMISSION
;
1048 cmd
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1049 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
1051 *status
= cmd
.resp
[0];
1055 static int get_card_status(struct mmc_card
*card
, u32
*status
, int retries
)
1057 struct mmc_command cmd
= {0};
1060 cmd
.opcode
= MMC_SEND_STATUS
;
1061 if (!mmc_host_is_spi(card
->host
))
1062 cmd
.arg
= card
->rca
<< 16;
1063 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
1064 err
= mmc_wait_for_cmd(card
->host
, &cmd
, retries
);
1066 *status
= cmd
.resp
[0];
1070 #define ERR_NOMEDIUM 3
1073 #define ERR_CONTINUE 0
1075 static int mmc_blk_cmd_error(struct request
*req
, const char *name
, int error
,
1076 bool status_valid
, u32 status
)
1080 /* response crc error, retry the r/w cmd */
1081 pr_err("%s: %s sending %s command, card status %#x\n",
1082 req
->rq_disk
->disk_name
, "response CRC error",
1087 pr_err("%s: %s sending %s command, card status %#x\n",
1088 req
->rq_disk
->disk_name
, "timed out", name
, status
);
1090 /* If the status cmd initially failed, retry the r/w cmd */
1091 if (!status_valid
) {
1092 pr_err("%s: status not valid, retrying timeout\n", req
->rq_disk
->disk_name
);
1096 * If it was a r/w cmd crc error, or illegal command
1097 * (eg, issued in wrong state) then retry - we should
1098 * have corrected the state problem above.
1100 if (status
& (R1_COM_CRC_ERROR
| R1_ILLEGAL_COMMAND
)) {
1101 pr_err("%s: command error, retrying timeout\n", req
->rq_disk
->disk_name
);
1105 /* Otherwise abort the command */
1106 pr_err("%s: not retrying timeout\n", req
->rq_disk
->disk_name
);
1110 /* We don't understand the error code the driver gave us */
1111 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1112 req
->rq_disk
->disk_name
, error
, status
);
1118 * Initial r/w and stop cmd error recovery.
1119 * We don't know whether the card received the r/w cmd or not, so try to
1120 * restore things back to a sane state. Essentially, we do this as follows:
1121 * - Obtain card status. If the first attempt to obtain card status fails,
1122 * the status word will reflect the failed status cmd, not the failed
1123 * r/w cmd. If we fail to obtain card status, it suggests we can no
1124 * longer communicate with the card.
1125 * - Check the card state. If the card received the cmd but there was a
1126 * transient problem with the response, it might still be in a data transfer
1127 * mode. Try to send it a stop command. If this fails, we can't recover.
1128 * - If the r/w cmd failed due to a response CRC error, it was probably
1129 * transient, so retry the cmd.
1130 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1131 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1132 * illegal cmd, retry.
1133 * Otherwise we don't understand what happened, so abort.
1135 static int mmc_blk_cmd_recovery(struct mmc_card
*card
, struct request
*req
,
1136 struct mmc_blk_request
*brq
, int *ecc_err
, int *gen_err
)
1138 bool prev_cmd_status_valid
= true;
1139 u32 status
, stop_status
= 0;
1142 if (mmc_card_removed(card
))
1143 return ERR_NOMEDIUM
;
1146 * Try to get card status which indicates both the card state
1147 * and why there was no response. If the first attempt fails,
1148 * we can't be sure the returned status is for the r/w command.
1150 for (retry
= 2; retry
>= 0; retry
--) {
1151 err
= get_card_status(card
, &status
, 0);
1155 prev_cmd_status_valid
= false;
1156 pr_err("%s: error %d sending status command, %sing\n",
1157 req
->rq_disk
->disk_name
, err
, retry
? "retry" : "abort");
1160 /* We couldn't get a response from the card. Give up. */
1162 /* Check if the card is removed */
1163 if (mmc_detect_card_removed(card
->host
))
1164 return ERR_NOMEDIUM
;
1168 /* Flag ECC errors */
1169 if ((status
& R1_CARD_ECC_FAILED
) ||
1170 (brq
->stop
.resp
[0] & R1_CARD_ECC_FAILED
) ||
1171 (brq
->cmd
.resp
[0] & R1_CARD_ECC_FAILED
))
1174 /* Flag General errors */
1175 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
)
1176 if ((status
& R1_ERROR
) ||
1177 (brq
->stop
.resp
[0] & R1_ERROR
)) {
1178 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1179 req
->rq_disk
->disk_name
, __func__
,
1180 brq
->stop
.resp
[0], status
);
1185 * Check the current card state. If it is in some data transfer
1186 * mode, tell it to stop (and hopefully transition back to TRAN.)
1188 if (R1_CURRENT_STATE(status
) == R1_STATE_DATA
||
1189 R1_CURRENT_STATE(status
) == R1_STATE_RCV
) {
1190 err
= send_stop(card
, &stop_status
);
1193 get_card_status(card
,&status
,0);
1194 if ((R1_CURRENT_STATE(status
) == R1_STATE_TRAN
) ||(R1_CURRENT_STATE(status
) == R1_STATE_PRG
)){
1197 pr_err("b card status %d \n",status
);
1200 pr_err("g card status %d \n",status
);
1203 pr_err("%s: error %d sending stop command\n",
1204 req
->rq_disk
->disk_name
, err
);
1207 * If the stop cmd also timed out, the card is probably
1208 * not present, so abort. Other errors are bad news too.
1212 if (stop_status
& R1_CARD_ECC_FAILED
)
1214 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
)
1215 if (stop_status
& R1_ERROR
) {
1216 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1217 req
->rq_disk
->disk_name
, __func__
,
1223 /* Check for set block count errors */
1225 return mmc_blk_cmd_error(req
, "SET_BLOCK_COUNT", brq
->sbc
.error
,
1226 prev_cmd_status_valid
, status
);
1228 /* Check for r/w command errors */
1230 return mmc_blk_cmd_error(req
, "r/w cmd", brq
->cmd
.error
,
1231 prev_cmd_status_valid
, status
);
1234 if (!brq
->stop
.error
)
1235 return ERR_CONTINUE
;
1237 /* Now for stop errors. These aren't fatal to the transfer. */
1238 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1239 req
->rq_disk
->disk_name
, brq
->stop
.error
,
1240 brq
->cmd
.resp
[0], status
);
1243 * Subsitute in our own stop status as this will give the error
1244 * state which happened during the execution of the r/w command.
1247 brq
->stop
.resp
[0] = stop_status
;
1248 brq
->stop
.error
= 0;
1250 return ERR_CONTINUE
;
1253 static int mmc_blk_reset(struct mmc_blk_data
*md
, struct mmc_host
*host
,
1258 if (md
->reset_done
& type
)
1261 md
->reset_done
|= type
;
1262 err
= mmc_hw_reset(host
);
1263 /* Ensure we switch back to the correct partition */
1264 if (err
!= -EOPNOTSUPP
) {
1265 struct mmc_blk_data
*main_md
= mmc_get_drvdata(host
->card
);
1268 main_md
->part_curr
= main_md
->part_type
;
1269 part_err
= mmc_blk_part_switch(host
->card
, md
);
1272 * We have failed to get back into the correct
1273 * partition, so we need to abort the whole request.
1281 static inline void mmc_blk_reset_success(struct mmc_blk_data
*md
, int type
)
1283 md
->reset_done
&= ~type
;
1286 int mmc_access_rpmb(struct mmc_queue
*mq
)
1288 struct mmc_blk_data
*md
= mq
->data
;
1290 * If this is a RPMB partition access, return ture
1292 if (md
&& md
->part_type
== EXT_CSD_PART_CONFIG_ACC_RPMB
)
1298 static int mmc_blk_issue_discard_rq(struct mmc_queue
*mq
, struct request
*req
)
1300 struct mmc_blk_data
*md
= mq
->data
;
1301 struct mmc_card
*card
= md
->queue
.card
;
1302 unsigned int from
, nr
, arg
;
1303 int err
= 0, type
= MMC_BLK_DISCARD
;
1305 if (!mmc_can_erase(card
)) {
1310 from
= blk_rq_pos(req
);
1311 nr
= blk_rq_sectors(req
);
1313 if (mmc_can_discard(card
))
1314 arg
= MMC_DISCARD_ARG
;
1315 else if (mmc_can_trim(card
))
1318 arg
= MMC_ERASE_ARG
;
1320 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1321 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1322 INAND_CMD38_ARG_EXT_CSD
,
1323 arg
== MMC_TRIM_ARG
?
1324 INAND_CMD38_ARG_TRIM
:
1325 INAND_CMD38_ARG_ERASE
,
1330 err
= mmc_erase(card
, from
, nr
, arg
);
1332 if (err
== -EIO
&& !mmc_blk_reset(md
, card
->host
, type
))
1335 mmc_blk_reset_success(md
, type
);
1336 blk_end_request(req
, err
, blk_rq_bytes(req
));
1341 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue
*mq
,
1342 struct request
*req
)
1344 struct mmc_blk_data
*md
= mq
->data
;
1345 struct mmc_card
*card
= md
->queue
.card
;
1346 unsigned int from
, nr
, arg
, trim_arg
, erase_arg
;
1347 int err
= 0, type
= MMC_BLK_SECDISCARD
;
1349 if (!(mmc_can_secure_erase_trim(card
) || mmc_can_sanitize(card
))) {
1354 from
= blk_rq_pos(req
);
1355 nr
= blk_rq_sectors(req
);
1357 /* The sanitize operation is supported at v4.5 only */
1358 if (mmc_can_sanitize(card
)) {
1359 erase_arg
= MMC_ERASE_ARG
;
1360 trim_arg
= MMC_TRIM_ARG
;
1362 erase_arg
= MMC_SECURE_ERASE_ARG
;
1363 trim_arg
= MMC_SECURE_TRIM1_ARG
;
1366 if (mmc_erase_group_aligned(card
, from
, nr
))
1368 else if (mmc_can_trim(card
))
1375 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1376 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1377 INAND_CMD38_ARG_EXT_CSD
,
1378 arg
== MMC_SECURE_TRIM1_ARG
?
1379 INAND_CMD38_ARG_SECTRIM1
:
1380 INAND_CMD38_ARG_SECERASE
,
1386 err
= mmc_erase(card
, from
, nr
, arg
);
1392 if (arg
== MMC_SECURE_TRIM1_ARG
) {
1393 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1394 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1395 INAND_CMD38_ARG_EXT_CSD
,
1396 INAND_CMD38_ARG_SECTRIM2
,
1402 err
= mmc_erase(card
, from
, nr
, MMC_SECURE_TRIM2_ARG
);
1409 if (mmc_can_sanitize(card
)) {
1410 trace_mmc_blk_erase_start(EXT_CSD_SANITIZE_START
, 0, 0);
1411 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1412 EXT_CSD_SANITIZE_START
, 1, 0);
1413 trace_mmc_blk_erase_end(EXT_CSD_SANITIZE_START
, 0, 0);
1416 if (err
&& !mmc_blk_reset(md
, card
->host
, type
))
1419 mmc_blk_reset_success(md
, type
);
1421 blk_end_request(req
, err
, blk_rq_bytes(req
));
1426 static int mmc_blk_issue_flush(struct mmc_queue
*mq
, struct request
*req
)
1428 struct mmc_blk_data
*md
= mq
->data
;
1429 struct mmc_card
*card
= md
->queue
.card
;
1432 ret
= mmc_flush_cache(card
);
1436 blk_end_request_all(req
, ret
);
1442 * Reformat current write as a reliable write, supporting
1443 * both legacy and the enhanced reliable write MMC cards.
1444 * In each transfer we'll handle only as much as a single
1445 * reliable write can handle, thus finish the request in
1446 * partial completions.
1448 static inline void mmc_apply_rel_rw(struct mmc_blk_request
*brq
,
1449 struct mmc_card
*card
,
1450 struct request
*req
)
1452 if (!(card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
)) {
1453 /* Legacy mode imposes restrictions on transfers. */
1454 if (!IS_ALIGNED(brq
->cmd
.arg
, card
->ext_csd
.rel_sectors
))
1455 brq
->data
.blocks
= 1;
1457 if (brq
->data
.blocks
> card
->ext_csd
.rel_sectors
)
1458 brq
->data
.blocks
= card
->ext_csd
.rel_sectors
;
1459 else if (brq
->data
.blocks
< card
->ext_csd
.rel_sectors
)
1460 brq
->data
.blocks
= 1;
1464 #define CMD_ERRORS \
1465 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1466 R1_ADDRESS_ERROR | /* Misaligned address */ \
1467 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1468 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1469 R1_CC_ERROR | /* Card controller error */ \
1470 R1_ERROR) /* General/unknown error */
1472 static int mmc_blk_err_check(struct mmc_card
*card
,
1473 struct mmc_async_req
*areq
)
1475 struct mmc_queue_req
*mq_mrq
= container_of(areq
, struct mmc_queue_req
,
1477 struct mmc_blk_request
*brq
= &mq_mrq
->brq
;
1478 struct request
*req
= mq_mrq
->req
;
1479 int ecc_err
= 0, gen_err
= 0;
1482 * sbc.error indicates a problem with the set block count
1483 * command. No data will have been transferred.
1485 * cmd.error indicates a problem with the r/w command. No
1486 * data will have been transferred.
1488 * stop.error indicates a problem with the stop command. Data
1489 * may have been transferred, or may still be transferring.
1491 if (brq
->sbc
.error
|| brq
->cmd
.error
|| brq
->stop
.error
||
1493 switch (mmc_blk_cmd_recovery(card
, req
, brq
, &ecc_err
, &gen_err
)) {
1495 return MMC_BLK_RETRY
;
1497 return MMC_BLK_ABORT
;
1499 return MMC_BLK_NOMEDIUM
;
1506 * Check for errors relating to the execution of the
1507 * initial command - such as address errors. No data
1508 * has been transferred.
1510 if (brq
->cmd
.resp
[0] & CMD_ERRORS
) {
1511 pr_err("%s: r/w command failed, status = %#x\n",
1512 req
->rq_disk
->disk_name
, brq
->cmd
.resp
[0]);
1513 return MMC_BLK_ABORT
;
1517 * Everything else is either success, or a data error of some
1518 * kind. If it was a write, we may have transitioned to
1519 * program mode, which we have to wait for it to complete.
1521 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
) {
1523 unsigned long timeout
;
1525 /* Check stop command response */
1526 if (brq
->stop
.resp
[0] & R1_ERROR
) {
1527 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1528 req
->rq_disk
->disk_name
, __func__
,
1533 timeout
= jiffies
+ msecs_to_jiffies(MMC_BLK_TIMEOUT_MS
);
1535 int err
= get_card_status(card
, &status
, 5);
1537 pr_err("%s: error %d requesting status\n",
1538 req
->rq_disk
->disk_name
, err
);
1539 return MMC_BLK_CMD_ERR
;
1542 if (status
& R1_ERROR
) {
1543 pr_err("%s: %s: general error sending status command, card status %#x\n",
1544 req
->rq_disk
->disk_name
, __func__
,
1549 /* Timeout if the device never becomes ready for data
1550 * and never leaves the program state.
1552 if (time_after(jiffies
, timeout
)) {
1553 pr_err("%s: Card stuck in programming state!"\
1554 " %s %s\n", mmc_hostname(card
->host
),
1555 req
->rq_disk
->disk_name
, __func__
);
1557 return MMC_BLK_CMD_ERR
;
1560 * Some cards mishandle the status bits,
1561 * so make sure to check both the busy
1562 * indication and the card state.
1564 } while (!(status
& R1_READY_FOR_DATA
) ||
1565 (R1_CURRENT_STATE(status
) == R1_STATE_PRG
));
1568 /* if general error occurs, retry the write operation. */
1570 pr_warn("%s: retrying write for general error\n",
1571 req
->rq_disk
->disk_name
);
1572 return MMC_BLK_RETRY
;
1575 if (brq
->data
.error
) {
1576 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1577 req
->rq_disk
->disk_name
, brq
->data
.error
,
1578 (unsigned)blk_rq_pos(req
),
1579 (unsigned)blk_rq_sectors(req
),
1580 brq
->cmd
.resp
[0], brq
->stop
.resp
[0]);
1582 if (rq_data_dir(req
) == READ
) {
1584 return MMC_BLK_ECC_ERR
;
1585 return MMC_BLK_DATA_ERR
;
1587 return MMC_BLK_CMD_ERR
;
1591 if (!brq
->data
.bytes_xfered
)
1592 return MMC_BLK_RETRY
;
1594 if (mmc_packed_cmd(mq_mrq
->cmd_type
)) {
1595 if (unlikely(brq
->data
.blocks
<< 9 != brq
->data
.bytes_xfered
))
1596 return MMC_BLK_PARTIAL
;
1598 return MMC_BLK_SUCCESS
;
1601 if (blk_rq_bytes(req
) != brq
->data
.bytes_xfered
)
1602 return MMC_BLK_PARTIAL
;
1604 return MMC_BLK_SUCCESS
;
1607 static int mmc_blk_packed_err_check(struct mmc_card
*card
,
1608 struct mmc_async_req
*areq
)
1610 struct mmc_queue_req
*mq_rq
= container_of(areq
, struct mmc_queue_req
,
1612 struct request
*req
= mq_rq
->req
;
1613 struct mmc_packed
*packed
= mq_rq
->packed
;
1614 int err
, check
, status
;
1620 check
= mmc_blk_err_check(card
, areq
);
1621 err
= get_card_status(card
, &status
, 0);
1623 pr_err("%s: error %d sending status command\n",
1624 req
->rq_disk
->disk_name
, err
);
1625 return MMC_BLK_ABORT
;
1628 if (status
& R1_EXCEPTION_EVENT
) {
1629 ext_csd
= kzalloc(512, GFP_KERNEL
);
1631 pr_err("%s: unable to allocate buffer for ext_csd\n",
1632 req
->rq_disk
->disk_name
);
1636 err
= mmc_send_ext_csd(card
, ext_csd
);
1638 pr_err("%s: error %d sending ext_csd\n",
1639 req
->rq_disk
->disk_name
, err
);
1640 check
= MMC_BLK_ABORT
;
1644 if ((ext_csd
[EXT_CSD_EXP_EVENTS_STATUS
] &
1645 EXT_CSD_PACKED_FAILURE
) &&
1646 (ext_csd
[EXT_CSD_PACKED_CMD_STATUS
] &
1647 EXT_CSD_PACKED_GENERIC_ERROR
)) {
1648 if (ext_csd
[EXT_CSD_PACKED_CMD_STATUS
] &
1649 EXT_CSD_PACKED_INDEXED_ERROR
) {
1650 packed
->idx_failure
=
1651 ext_csd
[EXT_CSD_PACKED_FAILURE_INDEX
] - 1;
1652 check
= MMC_BLK_PARTIAL
;
1654 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1655 "failure index: %d\n",
1656 req
->rq_disk
->disk_name
, packed
->nr_entries
,
1657 packed
->blocks
, packed
->idx_failure
);
1666 static void mmc_blk_rw_rq_prep(struct mmc_queue_req
*mqrq
,
1667 struct mmc_card
*card
,
1669 struct mmc_queue
*mq
)
1671 u32 readcmd
, writecmd
;
1672 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1673 struct request
*req
= mqrq
->req
;
1674 struct mmc_blk_data
*md
= mq
->data
;
1678 * Reliable writes are used to implement Forced Unit Access and
1679 * REQ_META accesses, and are supported only on MMCs.
1681 * XXX: this really needs a good explanation of why REQ_META
1682 * is treated special.
1684 bool do_rel_wr
= ((req
->cmd_flags
& REQ_FUA
) ||
1685 (req
->cmd_flags
& REQ_META
)) &&
1686 (rq_data_dir(req
) == WRITE
) &&
1687 (md
->flags
& MMC_BLK_REL_WR
);
1689 memset(brq
, 0, sizeof(struct mmc_blk_request
));
1690 brq
->mrq
.cmd
= &brq
->cmd
;
1691 brq
->mrq
.data
= &brq
->data
;
1693 brq
->cmd
.arg
= blk_rq_pos(req
);
1694 if (!mmc_card_blockaddr(card
))
1696 brq
->cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
1697 brq
->data
.blksz
= 512;
1698 brq
->stop
.opcode
= MMC_STOP_TRANSMISSION
;
1700 brq
->stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1701 brq
->data
.blocks
= blk_rq_sectors(req
);
1704 * The block layer doesn't support all sector count
1705 * restrictions, so we need to be prepared for too big
1708 if (brq
->data
.blocks
> card
->host
->max_blk_count
)
1709 brq
->data
.blocks
= card
->host
->max_blk_count
;
1711 if (brq
->data
.blocks
> 1) {
1713 * After a read error, we redo the request one sector
1714 * at a time in order to accurately determine which
1715 * sectors can be read successfully.
1718 brq
->data
.blocks
= 1;
1720 /* Some controllers can't do multiblock reads due to hw bugs */
1721 if (card
->host
->caps2
& MMC_CAP2_NO_MULTI_READ
&&
1722 rq_data_dir(req
) == READ
)
1723 brq
->data
.blocks
= 1;
1726 if (brq
->data
.blocks
> 1 || do_rel_wr
) {
1727 /* SPI multiblock writes terminate using a special
1728 * token, not a STOP_TRANSMISSION request.
1730 if (!mmc_host_is_spi(card
->host
) ||
1731 rq_data_dir(req
) == READ
)
1732 brq
->mrq
.stop
= &brq
->stop
;
1733 readcmd
= MMC_READ_MULTIPLE_BLOCK
;
1734 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
1736 brq
->mrq
.stop
= NULL
;
1737 readcmd
= MMC_READ_SINGLE_BLOCK
;
1738 writecmd
= MMC_WRITE_BLOCK
;
1740 #ifdef CONFIG_MTK_EMMC_CACHE
1741 /* for non-cacheable system data,
1742 * the implementation of reliable write / force prg write,
1743 * must be applied with mutli write cmd
1745 if (mmc_card_mmc(card
) && (card
->ext_csd
.cache_ctrl
& 0x1)){
1746 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
1749 if (rq_data_dir(req
) == READ
) {
1750 brq
->cmd
.opcode
= readcmd
;
1751 brq
->data
.flags
|= MMC_DATA_READ
;
1753 brq
->cmd
.opcode
= writecmd
;
1754 brq
->data
.flags
|= MMC_DATA_WRITE
;
1758 mmc_apply_rel_rw(brq
, card
, req
);
1761 * Data tag is used only during writing meta data to speed
1762 * up write and any subsequent read of this meta data
1764 do_data_tag
= (card
->ext_csd
.data_tag_unit_size
) &&
1765 (req
->cmd_flags
& REQ_META
) &&
1766 (rq_data_dir(req
) == WRITE
) &&
1767 ((brq
->data
.blocks
* brq
->data
.blksz
) >=
1768 card
->ext_csd
.data_tag_unit_size
);
1771 * Pre-defined multi-block transfers are preferable to
1772 * open ended-ones (and necessary for reliable writes).
1773 * However, it is not sufficient to just send CMD23,
1774 * and avoid the final CMD12, as on an error condition
1775 * CMD12 (stop) needs to be sent anyway. This, coupled
1776 * with Auto-CMD23 enhancements provided by some
1777 * hosts, means that the complexity of dealing
1778 * with this is best left to the host. If CMD23 is
1779 * supported by card and host, we'll fill sbc in and let
1780 * the host deal with handling it correctly. This means
1781 * that for hosts that don't expose MMC_CAP_CMD23, no
1782 * change of behavior will be observed.
1784 * N.B: Some MMC cards experience perf degradation.
1785 * We'll avoid using CMD23-bounded multiblock writes for
1786 * these, while retaining features like reliable writes.
1788 if ((md
->flags
& MMC_BLK_CMD23
) && mmc_op_multi(brq
->cmd
.opcode
) &&
1789 (do_rel_wr
|| !(card
->quirks
& MMC_QUIRK_BLK_NO_CMD23
) ||
1791 brq
->sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
1792 brq
->sbc
.arg
= brq
->data
.blocks
|
1793 (do_rel_wr
? (1 << 31) : 0) |
1794 (do_data_tag
? (1 << 29) : 0);
1795 brq
->sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1796 brq
->mrq
.sbc
= &brq
->sbc
;
1799 mmc_set_data_timeout(&brq
->data
, card
);
1801 brq
->data
.sg
= mqrq
->sg
;
1802 brq
->data
.sg_len
= mmc_queue_map_sg(mq
, mqrq
);
1804 if (brq
->data
.sg_len
> 1024)
1805 pr_err("%s:%d sglen = %x\n", __func__
, __LINE__
, brq
->data
.sg_len
);
1808 * Adjust the sg list so it is the same size as the
1811 if (brq
->data
.blocks
!= blk_rq_sectors(req
)) {
1812 int i
, data_size
= brq
->data
.blocks
<< 9;
1813 struct scatterlist
*sg
;
1815 for_each_sg(brq
->data
.sg
, sg
, brq
->data
.sg_len
, i
) {
1816 data_size
-= sg
->length
;
1817 if (data_size
<= 0) {
1818 sg
->length
+= data_size
;
1823 brq
->data
.sg_len
= i
;
1824 pr_err("%s:%d sglen = %x\n", __func__
, __LINE__
, brq
->data
.sg_len
);
1827 mqrq
->mmc_active
.mrq
= &brq
->mrq
;
1828 mqrq
->mmc_active
.err_check
= mmc_blk_err_check
;
1830 mmc_queue_bounce_pre(mqrq
);
1833 static inline u8
mmc_calc_packed_hdr_segs(struct request_queue
*q
,
1834 struct mmc_card
*card
)
1836 unsigned int hdr_sz
= mmc_large_sector(card
) ? 4096 : 512;
1837 unsigned int max_seg_sz
= queue_max_segment_size(q
);
1838 unsigned int len
, nr_segs
= 0;
1841 len
= min(hdr_sz
, max_seg_sz
);
1849 static u8
mmc_blk_prep_packed_list(struct mmc_queue
*mq
, struct request
*req
)
1851 struct request_queue
*q
= mq
->queue
;
1852 struct mmc_card
*card
= mq
->card
;
1853 struct request
*cur
= req
, *next
= NULL
;
1854 struct mmc_blk_data
*md
= mq
->data
;
1855 struct mmc_queue_req
*mqrq
= mq
->mqrq_cur
;
1856 bool en_rel_wr
= card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
;
1857 unsigned int req_sectors
= 0, phys_segments
= 0;
1858 unsigned int max_blk_count
, max_phys_segs
;
1859 bool put_back
= true;
1860 u8 max_packed_rw
= 0;
1863 if (!(md
->flags
& MMC_BLK_PACKED_CMD
))
1866 if ((rq_data_dir(cur
) == WRITE
) &&
1867 mmc_host_packed_wr(card
->host
))
1868 max_packed_rw
= card
->ext_csd
.max_packed_writes
;
1870 if (max_packed_rw
== 0)
1873 if (mmc_req_rel_wr(cur
) &&
1874 (md
->flags
& MMC_BLK_REL_WR
) && !en_rel_wr
)
1877 if (mmc_large_sector(card
) &&
1878 !IS_ALIGNED(blk_rq_sectors(cur
), 8))
1881 mmc_blk_clear_packed(mqrq
);
1883 max_blk_count
= min(card
->host
->max_blk_count
,
1884 card
->host
->max_req_size
>> 9);
1885 if (unlikely(max_blk_count
> 0xffff))
1886 max_blk_count
= 0xffff;
1888 max_phys_segs
= queue_max_segments(q
);
1889 req_sectors
+= blk_rq_sectors(cur
);
1890 phys_segments
+= cur
->nr_phys_segments
;
1892 if (rq_data_dir(cur
) == WRITE
) {
1893 req_sectors
+= mmc_large_sector(card
) ? 8 : 1;
1894 phys_segments
+= mmc_calc_packed_hdr_segs(q
, card
);
1898 if (reqs
>= max_packed_rw
- 1) {
1903 spin_lock_irq(q
->queue_lock
);
1904 next
= blk_fetch_request(q
);
1905 spin_unlock_irq(q
->queue_lock
);
1911 if (mmc_large_sector(card
) &&
1912 !IS_ALIGNED(blk_rq_sectors(next
), 8))
1915 if (next
->cmd_flags
& REQ_DISCARD
||
1916 next
->cmd_flags
& REQ_FLUSH
)
1919 if (rq_data_dir(cur
) != rq_data_dir(next
))
1922 if (mmc_req_rel_wr(next
) &&
1923 (md
->flags
& MMC_BLK_REL_WR
) && !en_rel_wr
)
1926 req_sectors
+= blk_rq_sectors(next
);
1927 if (req_sectors
> max_blk_count
)
1930 phys_segments
+= next
->nr_phys_segments
;
1931 if (phys_segments
> max_phys_segs
)
1934 list_add_tail(&next
->queuelist
, &mqrq
->packed
->list
);
1940 spin_lock_irq(q
->queue_lock
);
1941 blk_requeue_request(q
, next
);
1942 spin_unlock_irq(q
->queue_lock
);
1946 list_add(&req
->queuelist
, &mqrq
->packed
->list
);
1947 mqrq
->packed
->nr_entries
= ++reqs
;
1948 mqrq
->packed
->retries
= reqs
;
1953 mqrq
->cmd_type
= MMC_PACKED_NONE
;
1957 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req
*mqrq
,
1958 struct mmc_card
*card
,
1959 struct mmc_queue
*mq
)
1961 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1962 struct request
*req
= mqrq
->req
;
1963 struct request
*prq
;
1964 struct mmc_blk_data
*md
= mq
->data
;
1965 struct mmc_packed
*packed
= mqrq
->packed
;
1966 bool do_rel_wr
, do_data_tag
;
1967 u32
*packed_cmd_hdr
;
1973 mqrq
->cmd_type
= MMC_PACKED_WRITE
;
1975 packed
->idx_failure
= MMC_PACKED_NR_IDX
;
1977 packed_cmd_hdr
= packed
->cmd_hdr
;
1978 memset(packed_cmd_hdr
, 0, sizeof(packed
->cmd_hdr
));
1979 packed_cmd_hdr
[0] = (packed
->nr_entries
<< 16) |
1980 (PACKED_CMD_WR
<< 8) | PACKED_CMD_VER
;
1981 hdr_blocks
= mmc_large_sector(card
) ? 8 : 1;
1984 * Argument for each entry of packed group
1986 list_for_each_entry(prq
, &packed
->list
, queuelist
) {
1987 do_rel_wr
= mmc_req_rel_wr(prq
) && (md
->flags
& MMC_BLK_REL_WR
);
1988 do_data_tag
= (card
->ext_csd
.data_tag_unit_size
) &&
1989 (prq
->cmd_flags
& REQ_META
) &&
1990 (rq_data_dir(prq
) == WRITE
) &&
1991 ((brq
->data
.blocks
* brq
->data
.blksz
) >=
1992 card
->ext_csd
.data_tag_unit_size
);
1993 /* Argument of CMD23 */
1994 packed_cmd_hdr
[(i
* 2)] =
1995 (do_rel_wr
? MMC_CMD23_ARG_REL_WR
: 0) |
1996 (do_data_tag
? MMC_CMD23_ARG_TAG_REQ
: 0) |
1997 blk_rq_sectors(prq
);
1998 /* Argument of CMD18 or CMD25 */
1999 packed_cmd_hdr
[((i
* 2)) + 1] =
2000 mmc_card_blockaddr(card
) ?
2001 blk_rq_pos(prq
) : blk_rq_pos(prq
) << 9;
2002 packed
->blocks
+= blk_rq_sectors(prq
);
2006 memset(brq
, 0, sizeof(struct mmc_blk_request
));
2007 brq
->mrq
.cmd
= &brq
->cmd
;
2008 brq
->mrq
.data
= &brq
->data
;
2009 brq
->mrq
.sbc
= &brq
->sbc
;
2010 brq
->mrq
.stop
= &brq
->stop
;
2012 brq
->sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
2013 brq
->sbc
.arg
= MMC_CMD23_ARG_PACKED
| (packed
->blocks
+ hdr_blocks
);
2014 brq
->sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
2016 brq
->cmd
.opcode
= MMC_WRITE_MULTIPLE_BLOCK
;
2017 brq
->cmd
.arg
= blk_rq_pos(req
);
2018 if (!mmc_card_blockaddr(card
))
2020 brq
->cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
2022 brq
->data
.blksz
= 512;
2023 brq
->data
.blocks
= packed
->blocks
+ hdr_blocks
;
2024 brq
->data
.flags
|= MMC_DATA_WRITE
;
2026 brq
->stop
.opcode
= MMC_STOP_TRANSMISSION
;
2028 brq
->stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
2030 mmc_set_data_timeout(&brq
->data
, card
);
2032 brq
->data
.sg
= mqrq
->sg
;
2033 brq
->data
.sg_len
= mmc_queue_map_sg(mq
, mqrq
);
2034 pr_err("%s: sglen = %d\n", __func__
, brq
->data
.sg_len
);
2036 mqrq
->mmc_active
.mrq
= &brq
->mrq
;
2037 mqrq
->mmc_active
.err_check
= mmc_blk_packed_err_check
;
2039 mmc_queue_bounce_pre(mqrq
);
2042 static int mmc_blk_cmd_err(struct mmc_blk_data
*md
, struct mmc_card
*card
,
2043 struct mmc_blk_request
*brq
, struct request
*req
,
2046 struct mmc_queue_req
*mq_rq
;
2047 mq_rq
= container_of(brq
, struct mmc_queue_req
, brq
);
2050 * If this is an SD card and we're writing, we can first
2051 * mark the known good sectors as ok.
2053 * If the card is not SD, we can still ok written sectors
2054 * as reported by the controller (which might be less than
2055 * the real number of written sectors, but never more).
2057 if (mmc_card_sd(card
)) {
2060 blocks
= mmc_sd_num_wr_blocks(card
);
2061 if (blocks
!= (u32
)-1) {
2062 ret
= blk_end_request(req
, 0, blocks
<< 9);
2065 if (!mmc_packed_cmd(mq_rq
->cmd_type
))
2066 ret
= blk_end_request(req
, 0, brq
->data
.bytes_xfered
);
2071 static int mmc_blk_end_packed_req(struct mmc_queue_req
*mq_rq
)
2073 struct request
*prq
;
2074 struct mmc_packed
*packed
= mq_rq
->packed
;
2075 int idx
= packed
->idx_failure
, i
= 0;
2080 while (!list_empty(&packed
->list
)) {
2081 prq
= list_entry_rq(packed
->list
.next
);
2083 /* retry from error index */
2084 packed
->nr_entries
-= idx
;
2088 if (packed
->nr_entries
== MMC_PACKED_NR_SINGLE
) {
2089 list_del_init(&prq
->queuelist
);
2090 mmc_blk_clear_packed(mq_rq
);
2094 list_del_init(&prq
->queuelist
);
2095 blk_end_request(prq
, 0, blk_rq_bytes(prq
));
2099 mmc_blk_clear_packed(mq_rq
);
2103 static void mmc_blk_abort_packed_req(struct mmc_queue_req
*mq_rq
)
2105 struct request
*prq
;
2106 struct mmc_packed
*packed
= mq_rq
->packed
;
2110 while (!list_empty(&packed
->list
)) {
2111 prq
= list_entry_rq(packed
->list
.next
);
2112 list_del_init(&prq
->queuelist
);
2113 blk_end_request(prq
, -EIO
, blk_rq_bytes(prq
));
2116 mmc_blk_clear_packed(mq_rq
);
2119 static void mmc_blk_revert_packed_req(struct mmc_queue
*mq
,
2120 struct mmc_queue_req
*mq_rq
)
2122 struct request
*prq
;
2123 struct request_queue
*q
= mq
->queue
;
2124 struct mmc_packed
*packed
= mq_rq
->packed
;
2128 while (!list_empty(&packed
->list
)) {
2129 prq
= list_entry_rq(packed
->list
.prev
);
2130 if (prq
->queuelist
.prev
!= &packed
->list
) {
2131 list_del_init(&prq
->queuelist
);
2132 spin_lock_irq(q
->queue_lock
);
2133 blk_requeue_request(mq
->queue
, prq
);
2134 spin_unlock_irq(q
->queue_lock
);
2136 list_del_init(&prq
->queuelist
);
2140 mmc_blk_clear_packed(mq_rq
);
2142 #if defined(FEATURE_STORAGE_PERF_INDEX)
2143 #define PRT_TIME_PERIOD 500000000
2144 #define UP_LIMITS_4BYTE 4294967295UL //((4*1024*1024*1024)-1)
2146 pid_t mmcqd
[ID_CNT
]={0};
2147 bool start_async_req
[ID_CNT
] = {0};
2148 unsigned long long start_async_req_time
[ID_CNT
] = {0};
2149 static unsigned long long mmcqd_tag_t1
[ID_CNT
]={0}, mmccid_tag_t1
=0;
2150 unsigned long long mmcqd_t_usage_wr
[ID_CNT
]={0}, mmcqd_t_usage_rd
[ID_CNT
]={0};
2151 unsigned int mmcqd_rq_size_wr
[ID_CNT
]={0}, mmcqd_rq_size_rd
[ID_CNT
]={0};
2152 static unsigned int mmcqd_wr_offset_tag
[ID_CNT
]={0}, mmcqd_rd_offset_tag
[ID_CNT
]={0}, mmcqd_wr_offset
[ID_CNT
]={0}, mmcqd_rd_offset
[ID_CNT
]={0};
2153 static unsigned int mmcqd_wr_bit
[ID_CNT
]={0},mmcqd_wr_tract
[ID_CNT
]={0};
2154 static unsigned int mmcqd_rd_bit
[ID_CNT
]={0},mmcqd_rd_tract
[ID_CNT
]={0};
2155 static unsigned int mmcqd_wr_break
[ID_CNT
]={0}, mmcqd_rd_break
[ID_CNT
]={0};
2156 unsigned int mmcqd_rq_count
[ID_CNT
]={0}, mmcqd_wr_rq_count
[ID_CNT
]={0}, mmcqd_rd_rq_count
[ID_CNT
]={0};
2157 extern u32 g_u32_cid
[4];
2158 #ifdef FEATURE_STORAGE_META_LOG
2159 int check_perdev_minors
= CONFIG_MMC_BLOCK_MINORS
;
2160 struct metadata_rwlogger metadata_logger
[10] = {{{0}}};
2163 unsigned int mmcqd_work_percent
[ID_CNT
]={0};
2164 unsigned int mmcqd_w_throughput
[ID_CNT
]={0};
2165 unsigned int mmcqd_r_throughput
[ID_CNT
]={0};
2166 unsigned int mmcqd_read_clear
[ID_CNT
]={0};
2168 static void g_var_clear(unsigned int idx
)
2170 mmcqd_t_usage_wr
[idx
]=0;
2171 mmcqd_t_usage_rd
[idx
]=0;
2172 mmcqd_rq_size_wr
[idx
]=0;
2173 mmcqd_rq_size_rd
[idx
]=0;
2174 mmcqd_rq_count
[idx
]=0;
2175 mmcqd_wr_offset
[idx
]=0;
2176 mmcqd_rd_offset
[idx
]=0;
2177 mmcqd_wr_break
[idx
]=0;
2178 mmcqd_rd_break
[idx
]=0;
2179 mmcqd_wr_tract
[idx
]=0;
2180 mmcqd_wr_bit
[idx
]=0;
2181 mmcqd_rd_tract
[idx
]=0;
2182 mmcqd_rd_bit
[idx
]=0;
2183 mmcqd_wr_rq_count
[idx
]=0;
2184 mmcqd_rd_rq_count
[idx
]=0;
2187 unsigned int find_mmcqd_index(void)
2193 mmcqd_pid
= task_pid_nr(current
);
2196 mmcqd
[0] = mmcqd_pid
;
2197 start_async_req
[0]=0;
2200 for(i
=0;i
<ID_CNT
;i
++)
2202 if(mmcqd_pid
== mmcqd
[i
])
2207 if ((mmcqd
[i
] == 0) ||( i
==ID_CNT
-1))
2210 start_async_req
[i
]=0;
2219 //#undef FEATURE_STORAGE_PID_LOGGER
2220 #if defined(FEATURE_STORAGE_PID_LOGGER)
2222 struct struct_pid_logger g_pid_logger
[PID_ID_CNT
]={{0,0,{0},{0},{0},{0}}};
2226 unsigned char *page_logger
= NULL
;
2227 spinlock_t g_locker
;
2230 static int mmc_blk_issue_rw_rq(struct mmc_queue
*mq
, struct request
*rqc
)
2232 struct mmc_blk_data
*md
= mq
->data
;
2233 struct mmc_card
*card
= md
->queue
.card
;
2234 struct mmc_blk_request
*brq
= &mq
->mqrq_cur
->brq
;
2235 int ret
= 1, disable_multi
= 0, retry
= 0, type
;
2236 enum mmc_blk_status status
;
2237 struct mmc_queue_req
*mq_rq
;
2238 struct request
*req
= rqc
;
2239 struct mmc_async_req
*areq
;
2240 const u8 packed_nr
= 2;
2242 unsigned long long time1
= 0;
2243 #if defined(FEATURE_STORAGE_PERF_INDEX)
2245 unsigned long long t_period
=0, t_usage
=0;
2246 unsigned int t_percent
=0;
2247 unsigned int perf_meter
=0;
2248 unsigned int rq_byte
=0,rq_sector
=0,sect_offset
=0;
2249 unsigned int diversity
=0;
2251 #ifdef FEATURE_STORAGE_META_LOG
2252 unsigned int mmcmetaindex
=0;
2255 #if defined(FEATURE_STORAGE_PID_LOGGER)
2256 unsigned int index
=0;
2259 if (!rqc
&& !mq
->mqrq_prev
->req
)
2261 time1
= sched_clock();
2264 reqs
= mmc_blk_prep_packed_list(mq
, rqc
);
2265 #if defined(FEATURE_STORAGE_PERF_INDEX)
2266 mmcqd_pid
= task_pid_nr(current
);
2268 idx
= find_mmcqd_index();
2270 mmcqd_read_clear
[idx
] = 1;
2271 if(mmccid_tag_t1
==0)
2272 mmccid_tag_t1
= time1
;
2273 t_period
= time1
- mmccid_tag_t1
;
2274 if(t_period
>= (unsigned long long )((PRT_TIME_PERIOD
)*(unsigned long long )10))
2276 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "MMC Queue Thread:%d, %d, %d, %d, %d \n", mmcqd
[0], mmcqd
[1], mmcqd
[2], mmcqd
[3], mmcqd
[4]);
2277 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "MMC CID: %lx %lx %lx %lx \n", g_u32_cid
[0], g_u32_cid
[1], g_u32_cid
[2], g_u32_cid
[3]);
2278 mmccid_tag_t1
= time1
;
2280 if(mmcqd_tag_t1
[idx
]==0)
2281 mmcqd_tag_t1
[idx
] = time1
;
2282 t_period
= time1
- mmcqd_tag_t1
[idx
];
2284 if(t_period
>= (unsigned long long )PRT_TIME_PERIOD
)
2286 mmcqd_read_clear
[idx
] = 2;
2287 mmcqd_work_percent
[idx
] = 1;
2288 mmcqd_r_throughput
[idx
] = 0;
2289 mmcqd_w_throughput
[idx
] = 0;
2290 t_usage
= mmcqd_t_usage_wr
[idx
] + mmcqd_t_usage_rd
[idx
];
2291 if(t_period
> t_usage
*100)
2292 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd:%d Workload < 1%%, duty %lld, period %lld, req_cnt=%d \n", mmcqd
[idx
], t_usage
, t_period
, mmcqd_rq_count
[idx
]);
2295 do_div(t_period
, 100); //boundary issue
2296 t_percent
=((unsigned int)t_usage
)/((unsigned int)t_period
);
2297 mmcqd_work_percent
[idx
] = t_percent
;
2298 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd:%d Workload=%d%%, duty %lld, period %lld00, req_cnt=%d \n", mmcqd
[idx
], t_percent
, t_usage
, t_period
, mmcqd_rq_count
[idx
]); //period %lld00 == period %lld x100
2300 if(mmcqd_wr_rq_count
[idx
] >= 2)
2302 diversity
= mmcqd_wr_offset
[idx
]/(mmcqd_wr_rq_count
[idx
]-1);
2303 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd:%d Write Diversity=%d sectors offset, req_cnt=%d, break_cnt=%d, tract_cnt=%d, bit_cnt=%d\n", mmcqd
[idx
], diversity
, mmcqd_wr_rq_count
[idx
], mmcqd_wr_break
[idx
], mmcqd_wr_tract
[idx
], mmcqd_wr_bit
[idx
]);
2305 if(mmcqd_rd_rq_count
[idx
] >= 2)
2307 diversity
= mmcqd_rd_offset
[idx
]/(mmcqd_rd_rq_count
[idx
]-1);
2308 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd:%d Read Diversity=%d sectors offset, req_cnt=%d, break_cnt=%d, tract_cnt=%d, bit_cnt=%d\n", mmcqd
[idx
], diversity
, mmcqd_rd_rq_count
[idx
], mmcqd_rd_break
[idx
], mmcqd_rd_tract
[idx
], mmcqd_rd_bit
[idx
]);
2310 if(mmcqd_t_usage_wr
[idx
])
2312 do_div(mmcqd_t_usage_wr
[idx
], 1000000); //boundary issue
2313 if(mmcqd_t_usage_wr
[idx
]) // discard print if duration will <1ms
2315 perf_meter
= (mmcqd_rq_size_wr
[idx
])/((unsigned int)mmcqd_t_usage_wr
[idx
]); //kb/s
2316 mmcqd_w_throughput
[idx
] = perf_meter
;
2317 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd:%d Write Throughput=%d kB/s, size: %d bytes, time:%lld ms\n", mmcqd
[idx
], perf_meter
, mmcqd_rq_size_wr
[idx
], mmcqd_t_usage_wr
[idx
]);
2320 if(mmcqd_t_usage_rd
[idx
])
2322 do_div(mmcqd_t_usage_rd
[idx
], 1000000); //boundary issue
2323 if(mmcqd_t_usage_rd
[idx
]) // discard print if duration will <1ms
2325 perf_meter
= (mmcqd_rq_size_rd
[idx
])/((unsigned int)mmcqd_t_usage_rd
[idx
]); //kb/s
2326 mmcqd_r_throughput
[idx
] = perf_meter
;
2327 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd:%d Read Throughput=%d kB/s, size: %d bytes, time:%lld ms\n", mmcqd
[idx
], perf_meter
, mmcqd_rq_size_rd
[idx
], mmcqd_t_usage_rd
[idx
]);
2330 mmcqd_tag_t1
[idx
]=time1
;
2332 #ifdef FEATURE_STORAGE_META_LOG
2333 mmcmetaindex
= mmc_get_devidx(md
->disk
);
2334 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd metarw WR:%d NWR:%d HR:%d WDR:%d HDR:%d WW:%d NWW:%d HW:%d\n",
2335 metadata_logger
[mmcmetaindex
].metadata_rw_logger
[0], metadata_logger
[mmcmetaindex
].metadata_rw_logger
[1],
2336 metadata_logger
[mmcmetaindex
].metadata_rw_logger
[2], metadata_logger
[mmcmetaindex
].metadata_rw_logger
[3],
2337 metadata_logger
[mmcmetaindex
].metadata_rw_logger
[4], metadata_logger
[mmcmetaindex
].metadata_rw_logger
[5],
2338 metadata_logger
[mmcmetaindex
].metadata_rw_logger
[6], metadata_logger
[mmcmetaindex
].metadata_rw_logger
[7]);
2339 clear_metadata_rw_status(md
->disk
->first_minor
);
2341 #if defined(FEATURE_STORAGE_PID_LOGGER)
2344 for(index
=0; index
<PID_ID_CNT
; index
++) {
2346 if( g_pid_logger
[index
].current_pid
!=0 && g_pid_logger
[index
].current_pid
== mmcqd_pid
)
2349 if( index
== PID_ID_CNT
)
2351 for( i
=0; i
<PID_LOGGER_COUNT
; i
++) {
2352 //printk(KERN_INFO"hank mmcqd %d %d", g_pid_logger[index].pid_logger[i], mmcqd_pid);
2353 if( g_pid_logger
[index
].pid_logger
[i
] == 0)
2355 sprintf (g_pid_logger
[index
].pid_buffer
+i
*37, "{%05d:%05d:%08d:%05d:%08d}", g_pid_logger
[index
].pid_logger
[i
], g_pid_logger
[index
].pid_logger_counter
[i
], g_pid_logger
[index
].pid_logger_length
[i
], g_pid_logger
[index
].pid_logger_r_counter
[i
], g_pid_logger
[index
].pid_logger_r_length
[i
]);
2359 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd pid:%d %s\n", g_pid_logger
[index
].current_pid
, g_pid_logger
[index
].pid_buffer
);
2360 //xlog_printk(ANDROID_LOG_DEBUG, "BLOCK_TAG", "sizeof(&(g_pid_logger[index].pid_logger)):%d\n", sizeof(unsigned short)*PID_LOGGER_COUNT);
2361 //memset( &(g_pid_logger[index].pid_logger), 0, sizeof(struct struct_pid_logger)-(unsigned long)&(((struct struct_pid_logger *)0)->pid_logger));
2362 memset( &(g_pid_logger
[index
].pid_logger
), 0, sizeof(unsigned short)*PID_LOGGER_COUNT
);
2363 memset( &(g_pid_logger
[index
].pid_logger_counter
), 0, sizeof(unsigned short)*PID_LOGGER_COUNT
);
2364 memset( &(g_pid_logger
[index
].pid_logger_length
), 0, sizeof(unsigned int)*PID_LOGGER_COUNT
);
2365 memset( &(g_pid_logger
[index
].pid_logger_r_counter
), 0, sizeof(unsigned short)*PID_LOGGER_COUNT
);
2366 memset( &(g_pid_logger
[index
].pid_logger_r_length
), 0, sizeof(unsigned int)*PID_LOGGER_COUNT
);
2367 memset( &(g_pid_logger
[index
].pid_buffer
), 0, sizeof(char)*1024);
2371 g_pid_logger
[index
].pid_buffer
[0] = '\0';
2376 #if defined(FEATURE_STORAGE_VMSTAT_LOGGER)
2377 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "vmstat (FP:%ld)(FD:%ld)(ND:%ld)(WB:%ld)(NW:%ld)\n",
2378 ((global_page_state(NR_FILE_PAGES
)) << (PAGE_SHIFT
- 10)),
2379 ((global_page_state(NR_FILE_DIRTY
)) << (PAGE_SHIFT
- 10)),
2380 ((global_page_state(NR_DIRTIED
)) << (PAGE_SHIFT
- 10)),
2381 ((global_page_state(NR_WRITEBACK
)) << (PAGE_SHIFT
- 10)),
2382 ((global_page_state(NR_WRITTEN
)) << (PAGE_SHIFT
- 10)));
2388 rq_byte
= blk_rq_bytes(rqc
);
2389 rq_sector
= blk_rq_sectors(rqc
);
2390 if(rq_data_dir(rqc
) == WRITE
)
2392 if(mmcqd_wr_offset_tag
[idx
]>0)
2394 sect_offset
= abs(blk_rq_pos(rqc
) - mmcqd_wr_offset_tag
[idx
]);
2395 mmcqd_wr_offset
[idx
] += sect_offset
;
2396 if(sect_offset
== 1)
2397 mmcqd_wr_break
[idx
]++;
2399 mmcqd_wr_offset_tag
[idx
] = blk_rq_pos(rqc
) + rq_sector
;
2400 if(rq_sector
<= 1) //512 bytes
2401 mmcqd_wr_bit
[idx
] ++;
2402 else if(rq_sector
>= 1016) //508kB
2403 mmcqd_wr_tract
[idx
] ++;
2407 if(mmcqd_rd_offset_tag
[idx
]>0)
2409 sect_offset
= abs(blk_rq_pos(rqc
) - mmcqd_rd_offset_tag
[idx
]);
2410 mmcqd_rd_offset
[idx
] += sect_offset
;
2411 if(sect_offset
== 1)
2412 mmcqd_rd_break
[idx
]++;
2414 mmcqd_rd_offset_tag
[idx
] = blk_rq_pos(rqc
) + rq_sector
;
2415 if(rq_sector
<= 1) //512 bytes
2416 mmcqd_rd_bit
[idx
] ++;
2417 else if(rq_sector
>= 1016) //508kB
2418 mmcqd_rd_tract
[idx
] ++;
2425 * When 4KB native sector is enabled, only 8 blocks
2426 * multiple read or write is allowed
2428 if ((brq
->data
.blocks
& 0x07) &&
2429 (card
->ext_csd
.data_sector_size
== 4096)) {
2430 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2431 req
->rq_disk
->disk_name
);
2432 mq_rq
= mq
->mqrq_cur
;
2436 if (reqs
>= packed_nr
)
2437 mmc_blk_packed_hdr_wrq_prep(mq
->mqrq_cur
,
2440 mmc_blk_rw_rq_prep(mq
->mqrq_cur
, card
, 0, mq
);
2441 areq
= &mq
->mqrq_cur
->mmc_active
;
2444 areq
= mmc_start_req(card
->host
, areq
, (int *) &status
);
2446 if (status
== MMC_BLK_NEW_REQUEST
)
2447 mq
->flags
|= MMC_QUEUE_NEW_REQUEST
;
2451 mq_rq
= container_of(areq
, struct mmc_queue_req
, mmc_active
);
2454 type
= rq_data_dir(req
) == READ
? MMC_BLK_READ
: MMC_BLK_WRITE
;
2455 mmc_queue_bounce_post(mq_rq
);
2458 case MMC_BLK_SUCCESS
:
2459 case MMC_BLK_PARTIAL
:
2461 * A block was successfully transferred.
2463 mmc_blk_reset_success(md
, type
);
2465 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
2466 ret
= mmc_blk_end_packed_req(mq_rq
);
2469 ret
= blk_end_request(req
, 0,
2470 brq
->data
.bytes_xfered
);
2473 // if (card && card->host && card->host->areq)
2474 // met_mmc_end(card->host, card->host->areq);
2477 * If the blk_end_request function returns non-zero even
2478 * though all data has been transferred and no errors
2479 * were returned by the host controller, it's a bug.
2481 if (status
== MMC_BLK_SUCCESS
&& ret
) {
2482 pr_err("%s BUG rq_tot %d d_xfer %d\n",
2483 __func__
, blk_rq_bytes(req
),
2484 brq
->data
.bytes_xfered
);
2489 case MMC_BLK_CMD_ERR
:
2490 ret
= mmc_blk_cmd_err(md
, card
, brq
, req
, ret
);
2491 if (!mmc_blk_reset(md
, card
->host
, type
))
2499 if (!mmc_blk_reset(md
, card
->host
, type
))
2502 case MMC_BLK_DATA_ERR
: {
2505 err
= mmc_blk_reset(md
, card
->host
, type
);
2508 if (err
== -ENODEV
||
2509 mmc_packed_cmd(mq_rq
->cmd_type
))
2513 case MMC_BLK_ECC_ERR
:
2514 if (brq
->data
.blocks
> 1) {
2515 /* Redo read one sector at a time */
2516 pr_warning("%s: retrying using single block read\n",
2517 req
->rq_disk
->disk_name
);
2522 * After an error, we redo I/O one sector at a
2523 * time, so we only reach here after trying to
2524 * read a single sector.
2526 ret
= blk_end_request(req
, -EIO
,
2531 case MMC_BLK_NOMEDIUM
:
2534 pr_err("%s: Unhandled return value (%d)",
2535 req
->rq_disk
->disk_name
, status
);
2540 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
2541 if (!mq_rq
->packed
->retries
)
2543 mmc_blk_packed_hdr_wrq_prep(mq_rq
, card
, mq
);
2544 mmc_start_req(card
->host
,
2545 &mq_rq
->mmc_active
, NULL
);
2549 * In case of a incomplete request
2550 * prepare it again and resend.
2552 mmc_blk_rw_rq_prep(mq_rq
, card
,
2554 mmc_start_req(card
->host
,
2555 &mq_rq
->mmc_active
, NULL
);
2563 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
2564 mmc_blk_abort_packed_req(mq_rq
);
2566 if (mmc_card_removed(card
))
2567 req
->cmd_flags
|= REQ_QUIET
;
2569 ret
= blk_end_request(req
, -EIO
,
2570 blk_rq_cur_bytes(req
));
2575 if (mmc_card_removed(card
)) {
2576 rqc
->cmd_flags
|= REQ_QUIET
;
2577 blk_end_request_all(rqc
, -EIO
);
2580 * If current request is packed, it needs to put back.
2582 if (mmc_packed_cmd(mq
->mqrq_cur
->cmd_type
))
2583 mmc_blk_revert_packed_req(mq
, mq
->mqrq_cur
);
2585 mmc_blk_rw_rq_prep(mq
->mqrq_cur
, card
, 0, mq
);
2586 mmc_start_req(card
->host
,
2587 &mq
->mqrq_cur
->mmc_active
, NULL
);
2594 static int mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
2597 struct mmc_blk_data
*md
= mq
->data
;
2598 struct mmc_card
*card
= md
->queue
.card
;
2599 struct mmc_host
*host
= card
->host
;
2600 unsigned long flags
;
2601 unsigned int cmd_flags
= req
? req
->cmd_flags
: 0;
2603 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2604 if (mmc_bus_needs_resume(card
->host
))
2605 mmc_resume_bus(card
->host
);
2608 if (req
&& !mq
->mqrq_prev
->req
)
2609 /* claim host only for the first request */
2610 mmc_claim_host(card
->host
);
2612 ret
= mmc_blk_part_switch(card
, md
);
2615 blk_end_request_all(req
, -EIO
);
2621 mq
->flags
&= ~MMC_QUEUE_NEW_REQUEST
;
2622 if (cmd_flags
& REQ_DISCARD
) {
2623 /* complete ongoing async transfer before issuing discard */
2624 if (card
->host
->areq
)
2625 mmc_blk_issue_rw_rq(mq
, NULL
);
2626 if (req
->cmd_flags
& REQ_SECURE
&&
2627 !(card
->quirks
& MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
))
2628 ret
= mmc_blk_issue_secdiscard_rq(mq
, req
);
2630 ret
= mmc_blk_issue_discard_rq(mq
, req
);
2631 } else if (cmd_flags
& REQ_FLUSH
) {
2632 /* complete ongoing async transfer before issuing flush */
2633 if (card
->host
->areq
)
2634 mmc_blk_issue_rw_rq(mq
, NULL
);
2635 ret
= mmc_blk_issue_flush(mq
, req
);
2637 if (!req
&& host
->areq
) {
2638 spin_lock_irqsave(&host
->context_info
.lock
, flags
);
2639 host
->context_info
.is_waiting_last_req
= true;
2640 spin_unlock_irqrestore(&host
->context_info
.lock
, flags
);
2642 ret
= mmc_blk_issue_rw_rq(mq
, req
);
2646 if ((!req
&& !(mq
->flags
& MMC_QUEUE_NEW_REQUEST
)) ||
2647 (cmd_flags
& MMC_REQ_SPECIAL_MASK
))
2649 * Release host when there are no more requests
2650 * and after special request(discard, flush) is done.
2651 * In case sepecial request, there is no reentry to
2652 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2654 mmc_release_host(card
->host
);
2658 static inline int mmc_blk_readonly(struct mmc_card
*card
)
2660 return mmc_card_readonly(card
) ||
2661 !(card
->csd
.cmdclass
& CCC_BLOCK_WRITE
);
2664 //#if defined(FEATURE_STORAGE_PID_LOGGER)
2665 //extern unsigned long get_memory_size(void);
2667 #ifdef CONFIG_MTK_EXTMEM
2668 extern void* extmem_malloc_page_align(size_t bytes
);
2670 static struct mmc_blk_data
*mmc_blk_alloc_req(struct mmc_card
*card
,
2671 struct device
*parent
,
2674 const char *subname
,
2677 struct mmc_blk_data
*md
;
2680 devidx
= find_first_zero_bit(dev_use
, max_devices
);
2681 if (devidx
>= max_devices
)
2682 return ERR_PTR(-ENOSPC
);
2683 __set_bit(devidx
, dev_use
);
2685 md
= kzalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
2692 * !subname implies we are creating main mmc_blk_data that will be
2693 * associated with mmc_card with mmc_set_drvdata. Due to device
2694 * partitions, devidx will not coincide with a per-physical card
2695 * index anymore so we keep track of a name index.
2698 md
->name_idx
= find_first_zero_bit(name_use
, max_devices
);
2699 __set_bit(md
->name_idx
, name_use
);
2701 md
->name_idx
= ((struct mmc_blk_data
*)
2702 dev_to_disk(parent
)->private_data
)->name_idx
;
2704 md
->area_type
= area_type
;
2707 * Set the read-only status based on the supported commands
2708 * and the write protect switch.
2710 md
->read_only
= mmc_blk_readonly(card
);
2712 md
->disk
= alloc_disk(perdev_minors
);
2713 if (md
->disk
== NULL
) {
2718 spin_lock_init(&md
->lock
);
2719 INIT_LIST_HEAD(&md
->part
);
2722 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
, subname
);
2725 #if defined(FEATURE_STORAGE_PID_LOGGER)
2727 //num_page_logger = sizeof(struct page_pid_logger);
2728 //page_logger = vmalloc(num_physpages*sizeof(struct page_pid_logger));
2729 // solution: use get_memory_size to obtain the size from start pfn to max pfn
2731 //unsigned long count = get_memory_size() >> PAGE_SHIFT;
2732 unsigned long count
= get_max_DRAM_size() >> PAGE_SHIFT
;
2733 #ifdef CONFIG_MTK_EXTMEM
2734 page_logger
= extmem_malloc_page_align(count
* sizeof(struct page_pid_logger
));
2736 page_logger
= vmalloc(count
* sizeof(struct page_pid_logger
));
2739 memset( page_logger
, -1, count
*sizeof( struct page_pid_logger
));
2741 spin_lock_init(&g_locker
);
2744 #if defined(FEATURE_STORAGE_META_LOG)
2745 check_perdev_minors
= perdev_minors
;
2748 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
2749 md
->queue
.data
= md
;
2751 md
->disk
->major
= MMC_BLOCK_MAJOR
;
2752 md
->disk
->first_minor
= devidx
* perdev_minors
;
2753 md
->disk
->fops
= &mmc_bdops
;
2754 md
->disk
->private_data
= md
;
2755 md
->disk
->queue
= md
->queue
.queue
;
2756 md
->disk
->driverfs_dev
= parent
;
2757 set_disk_ro(md
->disk
, md
->read_only
|| default_ro
);
2758 md
->disk
->flags
= GENHD_FL_EXT_DEVT
;
2759 if (area_type
& MMC_BLK_DATA_AREA_RPMB
)
2760 md
->disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
2763 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2765 * - be set for removable media with permanent block devices
2766 * - be unset for removable block devices with permanent media
2768 * Since MMC block devices clearly fall under the second
2769 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2770 * should use the block device creation/destruction hotplug
2771 * messages to tell when the card is present.
2774 snprintf(md
->disk
->disk_name
, sizeof(md
->disk
->disk_name
),
2775 "mmcblk%d%s", md
->name_idx
, subname
? subname
: "");
2777 if (mmc_card_mmc(card
))
2778 blk_queue_logical_block_size(md
->queue
.queue
,
2779 card
->ext_csd
.data_sector_size
);
2781 blk_queue_logical_block_size(md
->queue
.queue
, 512);
2783 set_capacity(md
->disk
, size
);
2785 if (mmc_host_cmd23(card
->host
)) {
2786 if (mmc_card_mmc(card
) ||
2787 (mmc_card_sd(card
) &&
2788 card
->scr
.cmds
& SD_SCR_CMD23_SUPPORT
))
2789 md
->flags
|= MMC_BLK_CMD23
;
2792 if (mmc_card_mmc(card
) &&
2793 md
->flags
& MMC_BLK_CMD23
&&
2794 ((card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
) ||
2795 card
->ext_csd
.rel_sectors
)) {
2796 md
->flags
|= MMC_BLK_REL_WR
;
2797 blk_queue_flush(md
->queue
.queue
, REQ_FLUSH
| REQ_FUA
);
2800 if (mmc_card_mmc(card
) &&
2801 (area_type
== MMC_BLK_DATA_AREA_MAIN
) &&
2802 (md
->flags
& MMC_BLK_CMD23
) &&
2803 card
->ext_csd
.packed_event_en
) {
2804 if (!mmc_packed_init(&md
->queue
, card
))
2805 md
->flags
|= MMC_BLK_PACKED_CMD
;
2815 return ERR_PTR(ret
);
2818 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
2821 #ifdef CONFIG_MTK_EMMC_SUPPORT
2822 unsigned int l_reserve
;
2823 struct storage_info s_info
= {0};
2825 struct mmc_blk_data
*md
;
2827 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
)) {
2829 * The EXT_CSD sector count is in number or 512 byte
2832 size
= card
->ext_csd
.sectors
;
2835 * The CSD capacity field is in units of read_blkbits.
2836 * set_capacity takes units of 512 bytes.
2838 size
= card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9);
2841 if(!mmc_card_sd(card
)){
2842 #ifdef CONFIG_MTK_EMMC_SUPPORT
2843 msdc_get_info(EMMC_CARD_BOOT
, EMMC_RESERVE
, &s_info
);
2844 l_reserve
= s_info
.emmc_reserve
;
2845 printk("l_reserve = 0x%x\n", l_reserve
);
2846 size
-= l_reserve
; /*reserved for 64MB (emmc otp + emmc combo offset + reserved)*/
2849 md
= mmc_blk_alloc_req(card
, &card
->dev
, size
, false, NULL
,
2850 MMC_BLK_DATA_AREA_MAIN
);
2854 static int mmc_blk_alloc_part(struct mmc_card
*card
,
2855 struct mmc_blk_data
*md
,
2856 unsigned int part_type
,
2859 const char *subname
,
2863 struct mmc_blk_data
*part_md
;
2865 part_md
= mmc_blk_alloc_req(card
, disk_to_dev(md
->disk
), size
, default_ro
,
2866 subname
, area_type
);
2867 if (IS_ERR(part_md
))
2868 return PTR_ERR(part_md
);
2869 part_md
->part_type
= part_type
;
2870 list_add(&part_md
->part
, &md
->part
);
2872 string_get_size((u64
)get_capacity(part_md
->disk
) << 9, STRING_UNITS_2
,
2873 cap_str
, sizeof(cap_str
));
2874 pr_info("%s: %s %s partition %u %s\n",
2875 part_md
->disk
->disk_name
, mmc_card_id(card
),
2876 mmc_card_name(card
), part_md
->part_type
, cap_str
);
2880 /* MMC Physical partitions consist of two boot partitions and
2881 * up to four general purpose partitions.
2882 * For each partition enabled in EXT_CSD a block device will be allocatedi
2883 * to provide access to the partition.
2886 static int mmc_blk_alloc_parts(struct mmc_card
*card
, struct mmc_blk_data
*md
)
2890 if (!mmc_card_mmc(card
))
2893 for (idx
= 0; idx
< card
->nr_parts
; idx
++) {
2894 if (card
->part
[idx
].size
) {
2895 ret
= mmc_blk_alloc_part(card
, md
,
2896 card
->part
[idx
].part_cfg
,
2897 card
->part
[idx
].size
>> 9,
2898 card
->part
[idx
].force_ro
,
2899 card
->part
[idx
].name
,
2900 card
->part
[idx
].area_type
);
2909 static void mmc_blk_remove_req(struct mmc_blk_data
*md
)
2911 struct mmc_card
*card
;
2914 card
= md
->queue
.card
;
2915 if (md
->disk
->flags
& GENHD_FL_UP
) {
2916 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2917 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2918 card
->ext_csd
.boot_ro_lockable
)
2919 device_remove_file(disk_to_dev(md
->disk
),
2920 &md
->power_ro_lock
);
2922 /* Stop new requests from getting into the queue */
2923 del_gendisk(md
->disk
);
2926 /* Then flush out any already in there */
2927 mmc_cleanup_queue(&md
->queue
);
2928 if (md
->flags
& MMC_BLK_PACKED_CMD
)
2929 mmc_packed_clean(&md
->queue
);
2934 static void mmc_blk_remove_parts(struct mmc_card
*card
,
2935 struct mmc_blk_data
*md
)
2937 struct list_head
*pos
, *q
;
2938 struct mmc_blk_data
*part_md
;
2940 __clear_bit(md
->name_idx
, name_use
);
2941 list_for_each_safe(pos
, q
, &md
->part
) {
2942 part_md
= list_entry(pos
, struct mmc_blk_data
, part
);
2944 mmc_blk_remove_req(part_md
);
2948 static int mmc_add_disk(struct mmc_blk_data
*md
)
2951 struct mmc_card
*card
= md
->queue
.card
;
2954 md
->force_ro
.show
= force_ro_show
;
2955 md
->force_ro
.store
= force_ro_store
;
2956 sysfs_attr_init(&md
->force_ro
.attr
);
2957 md
->force_ro
.attr
.name
= "force_ro";
2958 md
->force_ro
.attr
.mode
= S_IRUGO
| S_IWUSR
;
2959 ret
= device_create_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2963 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2964 card
->ext_csd
.boot_ro_lockable
) {
2967 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_DIS
)
2970 mode
= S_IRUGO
| S_IWUSR
;
2972 md
->power_ro_lock
.show
= power_ro_lock_show
;
2973 md
->power_ro_lock
.store
= power_ro_lock_store
;
2974 sysfs_attr_init(&md
->power_ro_lock
.attr
);
2975 md
->power_ro_lock
.attr
.mode
= mode
;
2976 md
->power_ro_lock
.attr
.name
=
2977 "ro_lock_until_next_power_on";
2978 ret
= device_create_file(disk_to_dev(md
->disk
),
2979 &md
->power_ro_lock
);
2981 goto power_ro_lock_fail
;
2986 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2988 del_gendisk(md
->disk
);
2993 #define CID_MANFID_SANDISK 0x2
2994 #define CID_MANFID_TOSHIBA 0x11
2995 #define CID_MANFID_MICRON 0x13
2996 #define CID_MANFID_SAMSUNG 0x15
2997 #define CID_MANFID_SANDISK_NEW 0x45
2998 #define CID_MANFID_HYNIX 0x90
2999 #define CID_MANFID_KSI 0x70
3001 static const struct mmc_fixup blk_fixups
[] =
3003 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3004 MMC_QUIRK_INAND_CMD38
),
3005 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3006 MMC_QUIRK_INAND_CMD38
),
3007 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3008 MMC_QUIRK_INAND_CMD38
),
3009 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3010 MMC_QUIRK_INAND_CMD38
),
3011 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3012 MMC_QUIRK_INAND_CMD38
),
3013 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_SANDISK_NEW
, CID_OEMID_ANY
, add_quirk
,
3016 * Some MMC cards experience performance degradation with CMD23
3017 * instead of CMD12-bounded multiblock transfers. For now we'll
3018 * black list what's bad...
3019 * - Certain Toshiba cards.
3021 * N.B. This doesn't affect SD cards.
3023 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
3024 MMC_QUIRK_BLK_NO_CMD23
),
3025 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
3026 MMC_QUIRK_BLK_NO_CMD23
),
3027 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
3028 MMC_QUIRK_BLK_NO_CMD23
),
3031 * Some Micron MMC cards needs longer data read timeout than
3034 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_MICRON
, 0x200, add_quirk_mmc
,
3035 MMC_QUIRK_LONG_READ_TIME
),
3038 * On these Samsung MoviNAND parts, performing secure erase or
3039 * secure trim can result in unrecoverable corruption due to a
3042 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3043 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3044 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3045 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3046 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3047 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3048 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3049 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3050 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3051 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3052 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3053 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3054 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3055 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3056 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3057 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3058 #ifdef CONFIG_MTK_EMMC_CACHE
3060 * Some MMC cards cache feature, cannot flush the previous cache data by force programming or reliable write
3061 * which cannot gurrantee the strong order betwee meta data and file data.
3065 * Toshiba eMMC after enable cache feature, write performance drop, because flush operation waste much time
3067 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
3068 MMC_QUIRK_DISABLE_CACHE
),
3071 /* Hynix 4.41 trim will lead boot up failed. */
3072 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_HYNIX
, CID_OEMID_ANY
, add_quirk_mmc
,
3073 MMC_QUIRK_TRIM_UNSTABLE
),
3075 /* KSI PRV=0x3 trim will lead write performance drop. */
3076 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_KSI
, CID_OEMID_ANY
, add_quirk_mmc_ksi_v03_skip_trim
,
3077 MMC_QUIRK_KSI_V03_SKIP_TRIM
),
3082 #if defined(CONFIG_MTK_EMMC_SUPPORT) && !defined(CONFIG_MTK_GPT_SCHEME_SUPPORT)
3083 extern void emmc_create_sys_symlink (struct mmc_card
*card
);
3085 static int mmc_blk_probe(struct mmc_card
*card
)
3087 struct mmc_blk_data
*md
, *part_md
;
3091 * Check that the card supports the command class(es) we need.
3093 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
3096 md
= mmc_blk_alloc(card
);
3100 string_get_size((u64
)get_capacity(md
->disk
) << 9, STRING_UNITS_2
,
3101 cap_str
, sizeof(cap_str
));
3102 pr_info("%s: %s %s %s %s\n",
3103 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
3104 cap_str
, md
->read_only
? "(ro)" : "");
3106 if (mmc_blk_alloc_parts(card
, md
))
3109 mmc_set_drvdata(card
, md
);
3110 mmc_fixup_device(card
, blk_fixups
);
3112 printk("[%s]: %s by manufacturer settings, quirks=0x%x\n", __func__
, md
->disk
->disk_name
, card
->quirks
);
3114 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3115 mmc_set_bus_resume_policy(card
->host
, 1);
3117 if (mmc_add_disk(md
))
3120 list_for_each_entry(part_md
, &md
->part
, part
) {
3121 if (mmc_add_disk(part_md
))
3124 #if defined(CONFIG_MTK_EMMC_SUPPORT) && !defined(CONFIG_MTK_GPT_SCHEME_SUPPORT)
3125 emmc_create_sys_symlink(card
);
3130 mmc_blk_remove_parts(card
, md
);
3131 mmc_blk_remove_req(md
);
3135 static void mmc_blk_remove(struct mmc_card
*card
)
3137 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
3139 mmc_blk_remove_parts(card
, md
);
3140 mmc_claim_host(card
->host
);
3141 mmc_blk_part_switch(card
, md
);
3142 mmc_release_host(card
->host
);
3143 mmc_blk_remove_req(md
);
3144 mmc_set_drvdata(card
, NULL
);
3145 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3146 mmc_set_bus_resume_policy(card
->host
, 0);
3151 static int mmc_blk_suspend(struct mmc_card
*card
)
3153 struct mmc_blk_data
*part_md
;
3154 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
3157 mmc_queue_suspend(&md
->queue
);
3158 list_for_each_entry(part_md
, &md
->part
, part
) {
3159 mmc_queue_suspend(&part_md
->queue
);
3165 static int mmc_blk_resume(struct mmc_card
*card
)
3167 struct mmc_blk_data
*part_md
;
3168 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
3172 * Resume involves the card going into idle state,
3173 * so current partition is always the main one.
3175 md
->part_curr
= md
->part_type
;
3176 mmc_queue_resume(&md
->queue
);
3177 list_for_each_entry(part_md
, &md
->part
, part
) {
3178 mmc_queue_resume(&part_md
->queue
);
3184 #define mmc_blk_suspend NULL
3185 #define mmc_blk_resume NULL
3188 static struct mmc_driver mmc_driver
= {
3192 .probe
= mmc_blk_probe
,
3193 .remove
= mmc_blk_remove
,
3194 .suspend
= mmc_blk_suspend
,
3195 .resume
= mmc_blk_resume
,
3198 static int __init
mmc_blk_init(void)
3202 if (perdev_minors
!= CONFIG_MMC_BLOCK_MINORS
)
3203 pr_info("mmcblk: using %d minors per device\n", perdev_minors
);
3205 max_devices
= 256 / perdev_minors
;
3207 res
= register_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3211 res
= mmc_register_driver(&mmc_driver
);
3217 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3222 static void __exit
mmc_blk_exit(void)
3224 mmc_unregister_driver(&mmc_driver
);
3225 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3228 module_init(mmc_blk_init
);
3229 module_exit(mmc_blk_exit
);
3231 MODULE_LICENSE("GPL");
3232 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");