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
);
564 static ssize_t
power_ro_lock_store(struct device
*dev
,
565 struct device_attribute
*attr
, const char *buf
, size_t count
)
568 struct mmc_blk_data
*md
, *part_md
;
569 struct mmc_card
*card
;
572 if (kstrtoul(buf
, 0, &set
))
578 md
= mmc_blk_get(dev_to_disk(dev
));
579 card
= md
->queue
.card
;
581 mmc_claim_host(card
->host
);
583 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
, EXT_CSD_BOOT_WP
,
584 card
->ext_csd
.boot_ro_lock
|
585 EXT_CSD_BOOT_WP_B_PWR_WP_EN
,
586 card
->ext_csd
.part_time
);
588 pr_err("%s: Locking boot partition ro until next power on failed: %d\n", md
->disk
->disk_name
, ret
);
590 card
->ext_csd
.boot_ro_lock
|= EXT_CSD_BOOT_WP_B_PWR_WP_EN
;
592 mmc_release_host(card
->host
);
595 pr_info("%s: Locking boot partition ro until next power on\n",
596 md
->disk
->disk_name
);
597 set_disk_ro(md
->disk
, 1);
599 list_for_each_entry(part_md
, &md
->part
, part
)
600 if (part_md
->area_type
== MMC_BLK_DATA_AREA_BOOT
) {
601 pr_info("%s: Locking boot partition ro until next power on\n", part_md
->disk
->disk_name
);
602 set_disk_ro(part_md
->disk
, 1);
610 static ssize_t
force_ro_show(struct device
*dev
, struct device_attribute
*attr
,
614 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
616 ret
= snprintf(buf
, PAGE_SIZE
, "%d\n",
617 get_disk_ro(dev_to_disk(dev
)) ^
623 static ssize_t
force_ro_store(struct device
*dev
, struct device_attribute
*attr
,
624 const char *buf
, size_t count
)
628 struct mmc_blk_data
*md
= mmc_blk_get(dev_to_disk(dev
));
629 unsigned long set
= simple_strtoul(buf
, &end
, 0);
635 set_disk_ro(dev_to_disk(dev
), set
|| md
->read_only
);
642 static int mmc_blk_open(struct block_device
*bdev
, fmode_t mode
)
644 struct mmc_blk_data
*md
= mmc_blk_get(bdev
->bd_disk
);
647 mutex_lock(&block_mutex
);
650 check_disk_change(bdev
);
653 if ((mode
& FMODE_WRITE
) && md
->read_only
) {
658 mutex_unlock(&block_mutex
);
663 static void mmc_blk_release(struct gendisk
*disk
, fmode_t mode
)
665 struct mmc_blk_data
*md
= disk
->private_data
;
667 mutex_lock(&block_mutex
);
669 mutex_unlock(&block_mutex
);
673 mmc_blk_getgeo(struct block_device
*bdev
, struct hd_geometry
*geo
)
675 geo
->cylinders
= get_capacity(bdev
->bd_disk
) / (4 * 16);
681 struct mmc_blk_ioc_data
{
682 struct mmc_ioc_cmd ic
;
687 static struct mmc_blk_ioc_data
*mmc_blk_ioctl_copy_from_user(
688 struct mmc_ioc_cmd __user
*user
)
690 struct mmc_blk_ioc_data
*idata
;
693 idata
= kzalloc(sizeof(*idata
), GFP_KERNEL
);
699 if (copy_from_user(&idata
->ic
, user
, sizeof(idata
->ic
))) {
704 idata
->buf_bytes
= (u64
) idata
->ic
.blksz
* idata
->ic
.blocks
;
705 if (idata
->buf_bytes
> MMC_IOC_MAX_BYTES
) {
710 if (!idata
->buf_bytes
)
713 idata
->buf
= kzalloc(idata
->buf_bytes
, GFP_KERNEL
);
719 if (copy_from_user(idata
->buf
, (void __user
*)(unsigned long)
720 idata
->ic
.data_ptr
, idata
->buf_bytes
)) {
735 static int ioctl_rpmb_card_status_poll(struct mmc_card
*card
, u32
*status
,
741 if (!status
|| !retries_max
)
745 err
= get_card_status(card
, status
, 5);
749 if (!R1_STATUS(*status
) &&
750 (R1_CURRENT_STATE(*status
) != R1_STATE_PRG
))
751 break; /* RPMB programming operation complete */
754 * Rechedule to give the MMC device a chance to continue
755 * processing the previous command without being polled too
758 usleep_range(1000, 5000);
759 } while (++retry_count
< retries_max
);
761 if (retry_count
== retries_max
)
767 static int mmc_blk_ioctl_cmd(struct block_device
*bdev
,
768 struct mmc_ioc_cmd __user
*ic_ptr
)
770 struct mmc_blk_ioc_data
*idata
;
771 struct mmc_blk_data
*md
;
772 struct mmc_card
*card
;
773 struct mmc_command cmd
= {0};
774 struct mmc_data data
= {0};
775 struct mmc_request mrq
= {NULL
};
776 struct scatterlist sg
;
782 * The caller must have CAP_SYS_RAWIO, and must be calling this on the
783 * whole block device, not on a partition. This prevents overspray
784 * between sibling partitions.
786 if ((!capable(CAP_SYS_RAWIO
)) || (bdev
!= bdev
->bd_contains
))
789 idata
= mmc_blk_ioctl_copy_from_user(ic_ptr
);
791 return PTR_ERR(idata
);
793 md
= mmc_blk_get(bdev
->bd_disk
);
799 if (md
->area_type
& MMC_BLK_DATA_AREA_RPMB
)
802 card
= md
->queue
.card
;
808 cmd
.opcode
= idata
->ic
.opcode
;
809 cmd
.arg
= idata
->ic
.arg
;
810 cmd
.flags
= idata
->ic
.flags
;
812 if (idata
->buf_bytes
) {
815 data
.blksz
= idata
->ic
.blksz
;
816 data
.blocks
= idata
->ic
.blocks
;
818 sg_init_one(data
.sg
, idata
->buf
, idata
->buf_bytes
);
820 if (idata
->ic
.write_flag
)
821 data
.flags
= MMC_DATA_WRITE
;
823 data
.flags
= MMC_DATA_READ
;
825 /* data.flags must already be set before doing this. */
826 mmc_set_data_timeout(&data
, card
);
828 /* Allow overriding the timeout_ns for empirical tuning. */
829 if (idata
->ic
.data_timeout_ns
)
830 data
.timeout_ns
= idata
->ic
.data_timeout_ns
;
832 if ((cmd
.flags
& MMC_RSP_R1B
) == MMC_RSP_R1B
) {
834 * Pretend this is a data transfer and rely on the
835 * host driver to compute timeout. When all host
836 * drivers support cmd.cmd_timeout for R1B, this
840 * cmd.cmd_timeout = idata->ic.cmd_timeout_ms;
842 data
.timeout_ns
= idata
->ic
.cmd_timeout_ms
* 1000000;
850 mmc_claim_host(card
->host
);
852 err
= mmc_blk_part_switch(card
, md
);
856 if (idata
->ic
.is_acmd
) {
857 err
= mmc_app_cmd(card
->host
, card
);
863 err
= mmc_set_blockcount(card
, data
.blocks
,
864 idata
->ic
.write_flag
& (1 << 31));
869 mmc_wait_for_req(card
->host
, &mrq
);
872 dev_err(mmc_dev(card
->host
), "%s: cmd error %d\n",
873 __func__
, cmd
.error
);
878 dev_err(mmc_dev(card
->host
), "%s: data error %d\n",
879 __func__
, data
.error
);
885 * According to the SD specs, some commands require a delay after
886 * issuing the command.
888 if (idata
->ic
.postsleep_min_us
)
889 usleep_range(idata
->ic
.postsleep_min_us
, idata
->ic
.postsleep_max_us
);
891 if (copy_to_user(&(ic_ptr
->response
), cmd
.resp
, sizeof(cmd
.resp
))) {
896 if (!idata
->ic
.write_flag
) {
897 if (copy_to_user((void __user
*)(unsigned long) idata
->ic
.data_ptr
,
898 idata
->buf
, idata
->buf_bytes
)) {
906 * Ensure RPMB command has completed by polling CMD13
909 err
= ioctl_rpmb_card_status_poll(card
, &status
, 5);
911 dev_err(mmc_dev(card
->host
),
912 "%s: Card Status=0x%08X, error %d\n",
913 __func__
, status
, err
);
917 mmc_release_host(card
->host
);
927 static int mmc_blk_ioctl(struct block_device
*bdev
, fmode_t mode
,
928 unsigned int cmd
, unsigned long arg
)
931 if (cmd
== MMC_IOC_CMD
)
932 ret
= mmc_blk_ioctl_cmd(bdev
, (struct mmc_ioc_cmd __user
*)arg
);
937 static int mmc_blk_compat_ioctl(struct block_device
*bdev
, fmode_t mode
,
938 unsigned int cmd
, unsigned long arg
)
940 return mmc_blk_ioctl(bdev
, mode
, cmd
, (unsigned long) compat_ptr(arg
));
944 static const struct block_device_operations mmc_bdops
= {
945 .open
= mmc_blk_open
,
946 .release
= mmc_blk_release
,
947 .getgeo
= mmc_blk_getgeo
,
948 .owner
= THIS_MODULE
,
949 .ioctl
= mmc_blk_ioctl
,
951 .compat_ioctl
= mmc_blk_compat_ioctl
,
955 static inline int mmc_blk_part_switch(struct mmc_card
*card
,
956 struct mmc_blk_data
*md
)
959 struct mmc_blk_data
*main_md
= mmc_get_drvdata(card
);
961 if (main_md
->part_curr
== md
->part_type
)
964 if (mmc_card_mmc(card
)) {
965 u8 part_config
= card
->ext_csd
.part_config
;
967 part_config
&= ~EXT_CSD_PART_CONFIG_ACC_MASK
;
968 part_config
|= md
->part_type
;
970 ret
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
971 EXT_CSD_PART_CONFIG
, part_config
,
972 card
->ext_csd
.part_time
);
976 card
->ext_csd
.part_config
= part_config
;
979 main_md
->part_curr
= md
->part_type
;
983 static u32
mmc_sd_num_wr_blocks(struct mmc_card
*card
)
989 struct mmc_request mrq
= {NULL
};
990 struct mmc_command cmd
= {0};
991 struct mmc_data data
= {0};
993 struct scatterlist sg
;
995 cmd
.opcode
= MMC_APP_CMD
;
996 cmd
.arg
= card
->rca
<< 16;
997 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_AC
;
999 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 0);
1002 if (!mmc_host_is_spi(card
->host
) && !(cmd
.resp
[0] & R1_APP_CMD
))
1005 memset(&cmd
, 0, sizeof(struct mmc_command
));
1007 cmd
.opcode
= SD_APP_SEND_NUM_WR_BLKS
;
1009 cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
1013 data
.flags
= MMC_DATA_READ
;
1016 mmc_set_data_timeout(&data
, card
);
1021 blocks
= kmalloc(4, GFP_KERNEL
);
1025 sg_init_one(&sg
, blocks
, 4);
1027 mmc_wait_for_req(card
->host
, &mrq
);
1029 result
= ntohl(*blocks
);
1032 if (cmd
.error
|| data
.error
)
1038 u32
__mmc_sd_num_wr_blocks(struct mmc_card
*card
)
1040 return mmc_sd_num_wr_blocks(card
);
1042 EXPORT_SYMBOL(__mmc_sd_num_wr_blocks
);
1044 static int send_stop(struct mmc_card
*card
, u32
*status
)
1046 struct mmc_command cmd
= {0};
1049 cmd
.opcode
= MMC_STOP_TRANSMISSION
;
1050 cmd
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1051 err
= mmc_wait_for_cmd(card
->host
, &cmd
, 5);
1053 *status
= cmd
.resp
[0];
1057 static int get_card_status(struct mmc_card
*card
, u32
*status
, int retries
)
1059 struct mmc_command cmd
= {0};
1062 cmd
.opcode
= MMC_SEND_STATUS
;
1063 if (!mmc_host_is_spi(card
->host
))
1064 cmd
.arg
= card
->rca
<< 16;
1065 cmd
.flags
= MMC_RSP_SPI_R2
| MMC_RSP_R1
| MMC_CMD_AC
;
1066 err
= mmc_wait_for_cmd(card
->host
, &cmd
, retries
);
1068 *status
= cmd
.resp
[0];
1072 #define ERR_NOMEDIUM 3
1075 #define ERR_CONTINUE 0
1077 static int mmc_blk_cmd_error(struct request
*req
, const char *name
, int error
,
1078 bool status_valid
, u32 status
)
1082 /* response crc error, retry the r/w cmd */
1083 pr_err("%s: %s sending %s command, card status %#x\n",
1084 req
->rq_disk
->disk_name
, "response CRC error",
1089 pr_err("%s: %s sending %s command, card status %#x\n",
1090 req
->rq_disk
->disk_name
, "timed out", name
, status
);
1092 /* If the status cmd initially failed, retry the r/w cmd */
1093 if (!status_valid
) {
1094 pr_err("%s: status not valid, retrying timeout\n", req
->rq_disk
->disk_name
);
1098 * If it was a r/w cmd crc error, or illegal command
1099 * (eg, issued in wrong state) then retry - we should
1100 * have corrected the state problem above.
1102 if (status
& (R1_COM_CRC_ERROR
| R1_ILLEGAL_COMMAND
)) {
1103 pr_err("%s: command error, retrying timeout\n", req
->rq_disk
->disk_name
);
1107 /* Otherwise abort the command */
1108 pr_err("%s: not retrying timeout\n", req
->rq_disk
->disk_name
);
1112 /* We don't understand the error code the driver gave us */
1113 pr_err("%s: unknown error %d sending read/write command, card status %#x\n",
1114 req
->rq_disk
->disk_name
, error
, status
);
1120 * Initial r/w and stop cmd error recovery.
1121 * We don't know whether the card received the r/w cmd or not, so try to
1122 * restore things back to a sane state. Essentially, we do this as follows:
1123 * - Obtain card status. If the first attempt to obtain card status fails,
1124 * the status word will reflect the failed status cmd, not the failed
1125 * r/w cmd. If we fail to obtain card status, it suggests we can no
1126 * longer communicate with the card.
1127 * - Check the card state. If the card received the cmd but there was a
1128 * transient problem with the response, it might still be in a data transfer
1129 * mode. Try to send it a stop command. If this fails, we can't recover.
1130 * - If the r/w cmd failed due to a response CRC error, it was probably
1131 * transient, so retry the cmd.
1132 * - If the r/w cmd timed out, but we didn't get the r/w cmd status, retry.
1133 * - If the r/w cmd timed out, and the r/w cmd failed due to CRC error or
1134 * illegal cmd, retry.
1135 * Otherwise we don't understand what happened, so abort.
1137 static int mmc_blk_cmd_recovery(struct mmc_card
*card
, struct request
*req
,
1138 struct mmc_blk_request
*brq
, int *ecc_err
, int *gen_err
)
1140 bool prev_cmd_status_valid
= true;
1141 u32 status
, stop_status
= 0;
1144 if (mmc_card_removed(card
))
1145 return ERR_NOMEDIUM
;
1148 * Try to get card status which indicates both the card state
1149 * and why there was no response. If the first attempt fails,
1150 * we can't be sure the returned status is for the r/w command.
1152 for (retry
= 2; retry
>= 0; retry
--) {
1153 err
= get_card_status(card
, &status
, 0);
1157 prev_cmd_status_valid
= false;
1158 pr_err("%s: error %d sending status command, %sing\n",
1159 req
->rq_disk
->disk_name
, err
, retry
? "retry" : "abort");
1162 /* We couldn't get a response from the card. Give up. */
1164 /* Check if the card is removed */
1165 if (mmc_detect_card_removed(card
->host
))
1166 return ERR_NOMEDIUM
;
1170 /* Flag ECC errors */
1171 if ((status
& R1_CARD_ECC_FAILED
) ||
1172 (brq
->stop
.resp
[0] & R1_CARD_ECC_FAILED
) ||
1173 (brq
->cmd
.resp
[0] & R1_CARD_ECC_FAILED
))
1176 /* Flag General errors */
1177 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
)
1178 if ((status
& R1_ERROR
) ||
1179 (brq
->stop
.resp
[0] & R1_ERROR
)) {
1180 pr_err("%s: %s: general error sending stop or status command, stop cmd response %#x, card status %#x\n",
1181 req
->rq_disk
->disk_name
, __func__
,
1182 brq
->stop
.resp
[0], status
);
1187 * Check the current card state. If it is in some data transfer
1188 * mode, tell it to stop (and hopefully transition back to TRAN.)
1190 if (R1_CURRENT_STATE(status
) == R1_STATE_DATA
||
1191 R1_CURRENT_STATE(status
) == R1_STATE_RCV
) {
1192 err
= send_stop(card
, &stop_status
);
1195 get_card_status(card
,&status
,0);
1196 if ((R1_CURRENT_STATE(status
) == R1_STATE_TRAN
) ||(R1_CURRENT_STATE(status
) == R1_STATE_PRG
)){
1199 pr_err("b card status %d \n",status
);
1202 pr_err("g card status %d \n",status
);
1205 pr_err("%s: error %d sending stop command\n",
1206 req
->rq_disk
->disk_name
, err
);
1209 * If the stop cmd also timed out, the card is probably
1210 * not present, so abort. Other errors are bad news too.
1214 if (stop_status
& R1_CARD_ECC_FAILED
)
1216 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
)
1217 if (stop_status
& R1_ERROR
) {
1218 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1219 req
->rq_disk
->disk_name
, __func__
,
1225 /* Check for set block count errors */
1227 return mmc_blk_cmd_error(req
, "SET_BLOCK_COUNT", brq
->sbc
.error
,
1228 prev_cmd_status_valid
, status
);
1230 /* Check for r/w command errors */
1232 return mmc_blk_cmd_error(req
, "r/w cmd", brq
->cmd
.error
,
1233 prev_cmd_status_valid
, status
);
1236 if (!brq
->stop
.error
)
1237 return ERR_CONTINUE
;
1239 /* Now for stop errors. These aren't fatal to the transfer. */
1240 pr_err("%s: error %d sending stop command, original cmd response %#x, card status %#x\n",
1241 req
->rq_disk
->disk_name
, brq
->stop
.error
,
1242 brq
->cmd
.resp
[0], status
);
1245 * Subsitute in our own stop status as this will give the error
1246 * state which happened during the execution of the r/w command.
1249 brq
->stop
.resp
[0] = stop_status
;
1250 brq
->stop
.error
= 0;
1252 return ERR_CONTINUE
;
1255 static int mmc_blk_reset(struct mmc_blk_data
*md
, struct mmc_host
*host
,
1260 if (md
->reset_done
& type
)
1263 md
->reset_done
|= type
;
1264 err
= mmc_hw_reset(host
);
1265 /* Ensure we switch back to the correct partition */
1266 if (err
!= -EOPNOTSUPP
) {
1267 struct mmc_blk_data
*main_md
= mmc_get_drvdata(host
->card
);
1270 main_md
->part_curr
= main_md
->part_type
;
1271 part_err
= mmc_blk_part_switch(host
->card
, md
);
1274 * We have failed to get back into the correct
1275 * partition, so we need to abort the whole request.
1283 static inline void mmc_blk_reset_success(struct mmc_blk_data
*md
, int type
)
1285 md
->reset_done
&= ~type
;
1288 int mmc_access_rpmb(struct mmc_queue
*mq
)
1290 struct mmc_blk_data
*md
= mq
->data
;
1292 * If this is a RPMB partition access, return ture
1294 if (md
&& md
->part_type
== EXT_CSD_PART_CONFIG_ACC_RPMB
)
1300 static int mmc_blk_issue_discard_rq(struct mmc_queue
*mq
, struct request
*req
)
1302 struct mmc_blk_data
*md
= mq
->data
;
1303 struct mmc_card
*card
= md
->queue
.card
;
1304 unsigned int from
, nr
, arg
;
1305 int err
= 0, type
= MMC_BLK_DISCARD
;
1307 if (!mmc_can_erase(card
)) {
1312 from
= blk_rq_pos(req
);
1313 nr
= blk_rq_sectors(req
);
1315 if (mmc_can_discard(card
))
1316 arg
= MMC_DISCARD_ARG
;
1317 else if (mmc_can_trim(card
))
1320 arg
= MMC_ERASE_ARG
;
1322 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1323 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1324 INAND_CMD38_ARG_EXT_CSD
,
1325 arg
== MMC_TRIM_ARG
?
1326 INAND_CMD38_ARG_TRIM
:
1327 INAND_CMD38_ARG_ERASE
,
1332 err
= mmc_erase(card
, from
, nr
, arg
);
1334 if (err
== -EIO
&& !mmc_blk_reset(md
, card
->host
, type
))
1337 mmc_blk_reset_success(md
, type
);
1338 blk_end_request(req
, err
, blk_rq_bytes(req
));
1343 static int mmc_blk_issue_secdiscard_rq(struct mmc_queue
*mq
,
1344 struct request
*req
)
1346 struct mmc_blk_data
*md
= mq
->data
;
1347 struct mmc_card
*card
= md
->queue
.card
;
1348 unsigned int from
, nr
, arg
, trim_arg
, erase_arg
;
1349 int err
= 0, type
= MMC_BLK_SECDISCARD
;
1351 if (!(mmc_can_secure_erase_trim(card
) || mmc_can_sanitize(card
))) {
1356 from
= blk_rq_pos(req
);
1357 nr
= blk_rq_sectors(req
);
1359 /* The sanitize operation is supported at v4.5 only */
1360 if (mmc_can_sanitize(card
)) {
1361 erase_arg
= MMC_ERASE_ARG
;
1362 trim_arg
= MMC_TRIM_ARG
;
1364 erase_arg
= MMC_SECURE_ERASE_ARG
;
1365 trim_arg
= MMC_SECURE_TRIM1_ARG
;
1368 if (mmc_erase_group_aligned(card
, from
, nr
))
1370 else if (mmc_can_trim(card
))
1377 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1378 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1379 INAND_CMD38_ARG_EXT_CSD
,
1380 arg
== MMC_SECURE_TRIM1_ARG
?
1381 INAND_CMD38_ARG_SECTRIM1
:
1382 INAND_CMD38_ARG_SECERASE
,
1388 err
= mmc_erase(card
, from
, nr
, arg
);
1394 if (arg
== MMC_SECURE_TRIM1_ARG
) {
1395 if (card
->quirks
& MMC_QUIRK_INAND_CMD38
) {
1396 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1397 INAND_CMD38_ARG_EXT_CSD
,
1398 INAND_CMD38_ARG_SECTRIM2
,
1404 err
= mmc_erase(card
, from
, nr
, MMC_SECURE_TRIM2_ARG
);
1411 if (mmc_can_sanitize(card
)) {
1412 trace_mmc_blk_erase_start(EXT_CSD_SANITIZE_START
, 0, 0);
1413 err
= mmc_switch(card
, EXT_CSD_CMD_SET_NORMAL
,
1414 EXT_CSD_SANITIZE_START
, 1, 0);
1415 trace_mmc_blk_erase_end(EXT_CSD_SANITIZE_START
, 0, 0);
1418 if (err
&& !mmc_blk_reset(md
, card
->host
, type
))
1421 mmc_blk_reset_success(md
, type
);
1423 blk_end_request(req
, err
, blk_rq_bytes(req
));
1428 static int mmc_blk_issue_flush(struct mmc_queue
*mq
, struct request
*req
)
1430 struct mmc_blk_data
*md
= mq
->data
;
1431 struct mmc_card
*card
= md
->queue
.card
;
1434 ret
= mmc_flush_cache(card
);
1438 blk_end_request_all(req
, ret
);
1444 * Reformat current write as a reliable write, supporting
1445 * both legacy and the enhanced reliable write MMC cards.
1446 * In each transfer we'll handle only as much as a single
1447 * reliable write can handle, thus finish the request in
1448 * partial completions.
1450 static inline void mmc_apply_rel_rw(struct mmc_blk_request
*brq
,
1451 struct mmc_card
*card
,
1452 struct request
*req
)
1454 if (!(card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
)) {
1455 /* Legacy mode imposes restrictions on transfers. */
1456 if (!IS_ALIGNED(brq
->cmd
.arg
, card
->ext_csd
.rel_sectors
))
1457 brq
->data
.blocks
= 1;
1459 if (brq
->data
.blocks
> card
->ext_csd
.rel_sectors
)
1460 brq
->data
.blocks
= card
->ext_csd
.rel_sectors
;
1461 else if (brq
->data
.blocks
< card
->ext_csd
.rel_sectors
)
1462 brq
->data
.blocks
= 1;
1466 #define CMD_ERRORS \
1467 (R1_OUT_OF_RANGE | /* Command argument out of range */ \
1468 R1_ADDRESS_ERROR | /* Misaligned address */ \
1469 R1_BLOCK_LEN_ERROR | /* Transferred block length incorrect */\
1470 R1_WP_VIOLATION | /* Tried to write to protected block */ \
1471 R1_CC_ERROR | /* Card controller error */ \
1472 R1_ERROR) /* General/unknown error */
1474 static int mmc_blk_err_check(struct mmc_card
*card
,
1475 struct mmc_async_req
*areq
)
1477 struct mmc_queue_req
*mq_mrq
= container_of(areq
, struct mmc_queue_req
,
1479 struct mmc_blk_request
*brq
= &mq_mrq
->brq
;
1480 struct request
*req
= mq_mrq
->req
;
1481 int ecc_err
= 0, gen_err
= 0;
1484 * sbc.error indicates a problem with the set block count
1485 * command. No data will have been transferred.
1487 * cmd.error indicates a problem with the r/w command. No
1488 * data will have been transferred.
1490 * stop.error indicates a problem with the stop command. Data
1491 * may have been transferred, or may still be transferring.
1493 if (brq
->sbc
.error
|| brq
->cmd
.error
|| brq
->stop
.error
||
1495 switch (mmc_blk_cmd_recovery(card
, req
, brq
, &ecc_err
, &gen_err
)) {
1497 return MMC_BLK_RETRY
;
1499 return MMC_BLK_ABORT
;
1501 return MMC_BLK_NOMEDIUM
;
1508 * Check for errors relating to the execution of the
1509 * initial command - such as address errors. No data
1510 * has been transferred.
1512 if (brq
->cmd
.resp
[0] & CMD_ERRORS
) {
1513 pr_err("%s: r/w command failed, status = %#x\n",
1514 req
->rq_disk
->disk_name
, brq
->cmd
.resp
[0]);
1515 return MMC_BLK_ABORT
;
1519 * Everything else is either success, or a data error of some
1520 * kind. If it was a write, we may have transitioned to
1521 * program mode, which we have to wait for it to complete.
1523 if (!mmc_host_is_spi(card
->host
) && rq_data_dir(req
) != READ
) {
1525 unsigned long timeout
;
1527 /* Check stop command response */
1528 if (brq
->stop
.resp
[0] & R1_ERROR
) {
1529 pr_err("%s: %s: general error sending stop command, stop cmd response %#x\n",
1530 req
->rq_disk
->disk_name
, __func__
,
1535 timeout
= jiffies
+ msecs_to_jiffies(MMC_BLK_TIMEOUT_MS
);
1537 int err
= get_card_status(card
, &status
, 5);
1539 pr_err("%s: error %d requesting status\n",
1540 req
->rq_disk
->disk_name
, err
);
1541 return MMC_BLK_CMD_ERR
;
1544 if (status
& R1_ERROR
) {
1545 pr_err("%s: %s: general error sending status command, card status %#x\n",
1546 req
->rq_disk
->disk_name
, __func__
,
1551 /* Timeout if the device never becomes ready for data
1552 * and never leaves the program state.
1554 if (time_after(jiffies
, timeout
)) {
1555 pr_err("%s: Card stuck in programming state!"\
1556 " %s %s\n", mmc_hostname(card
->host
),
1557 req
->rq_disk
->disk_name
, __func__
);
1559 return MMC_BLK_CMD_ERR
;
1562 * Some cards mishandle the status bits,
1563 * so make sure to check both the busy
1564 * indication and the card state.
1566 } while (!(status
& R1_READY_FOR_DATA
) ||
1567 (R1_CURRENT_STATE(status
) == R1_STATE_PRG
));
1570 /* if general error occurs, retry the write operation. */
1572 pr_warn("%s: retrying write for general error\n",
1573 req
->rq_disk
->disk_name
);
1574 return MMC_BLK_RETRY
;
1577 if (brq
->data
.error
) {
1578 pr_err("%s: error %d transferring data, sector %u, nr %u, cmd response %#x, card status %#x\n",
1579 req
->rq_disk
->disk_name
, brq
->data
.error
,
1580 (unsigned)blk_rq_pos(req
),
1581 (unsigned)blk_rq_sectors(req
),
1582 brq
->cmd
.resp
[0], brq
->stop
.resp
[0]);
1584 if (rq_data_dir(req
) == READ
) {
1586 return MMC_BLK_ECC_ERR
;
1587 return MMC_BLK_DATA_ERR
;
1589 return MMC_BLK_CMD_ERR
;
1593 if (!brq
->data
.bytes_xfered
)
1594 return MMC_BLK_RETRY
;
1596 if (mmc_packed_cmd(mq_mrq
->cmd_type
)) {
1597 if (unlikely(brq
->data
.blocks
<< 9 != brq
->data
.bytes_xfered
))
1598 return MMC_BLK_PARTIAL
;
1600 return MMC_BLK_SUCCESS
;
1603 if (blk_rq_bytes(req
) != brq
->data
.bytes_xfered
)
1604 return MMC_BLK_PARTIAL
;
1606 return MMC_BLK_SUCCESS
;
1609 static int mmc_blk_packed_err_check(struct mmc_card
*card
,
1610 struct mmc_async_req
*areq
)
1612 struct mmc_queue_req
*mq_rq
= container_of(areq
, struct mmc_queue_req
,
1614 struct request
*req
= mq_rq
->req
;
1615 struct mmc_packed
*packed
= mq_rq
->packed
;
1616 int err
, check
, status
;
1622 check
= mmc_blk_err_check(card
, areq
);
1623 err
= get_card_status(card
, &status
, 0);
1625 pr_err("%s: error %d sending status command\n",
1626 req
->rq_disk
->disk_name
, err
);
1627 return MMC_BLK_ABORT
;
1630 if (status
& R1_EXCEPTION_EVENT
) {
1631 ext_csd
= kzalloc(512, GFP_KERNEL
);
1633 pr_err("%s: unable to allocate buffer for ext_csd\n",
1634 req
->rq_disk
->disk_name
);
1638 err
= mmc_send_ext_csd(card
, ext_csd
);
1640 pr_err("%s: error %d sending ext_csd\n",
1641 req
->rq_disk
->disk_name
, err
);
1642 check
= MMC_BLK_ABORT
;
1646 if ((ext_csd
[EXT_CSD_EXP_EVENTS_STATUS
] &
1647 EXT_CSD_PACKED_FAILURE
) &&
1648 (ext_csd
[EXT_CSD_PACKED_CMD_STATUS
] &
1649 EXT_CSD_PACKED_GENERIC_ERROR
)) {
1650 if (ext_csd
[EXT_CSD_PACKED_CMD_STATUS
] &
1651 EXT_CSD_PACKED_INDEXED_ERROR
) {
1652 packed
->idx_failure
=
1653 ext_csd
[EXT_CSD_PACKED_FAILURE_INDEX
] - 1;
1654 check
= MMC_BLK_PARTIAL
;
1656 pr_err("%s: packed cmd failed, nr %u, sectors %u, "
1657 "failure index: %d\n",
1658 req
->rq_disk
->disk_name
, packed
->nr_entries
,
1659 packed
->blocks
, packed
->idx_failure
);
1668 static void mmc_blk_rw_rq_prep(struct mmc_queue_req
*mqrq
,
1669 struct mmc_card
*card
,
1671 struct mmc_queue
*mq
)
1673 u32 readcmd
, writecmd
;
1674 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1675 struct request
*req
= mqrq
->req
;
1676 struct mmc_blk_data
*md
= mq
->data
;
1680 * Reliable writes are used to implement Forced Unit Access and
1681 * REQ_META accesses, and are supported only on MMCs.
1683 * XXX: this really needs a good explanation of why REQ_META
1684 * is treated special.
1686 bool do_rel_wr
= ((req
->cmd_flags
& REQ_FUA
) ||
1687 (req
->cmd_flags
& REQ_META
)) &&
1688 (rq_data_dir(req
) == WRITE
) &&
1689 (md
->flags
& MMC_BLK_REL_WR
);
1691 memset(brq
, 0, sizeof(struct mmc_blk_request
));
1692 brq
->mrq
.cmd
= &brq
->cmd
;
1693 brq
->mrq
.data
= &brq
->data
;
1695 brq
->cmd
.arg
= blk_rq_pos(req
);
1696 if (!mmc_card_blockaddr(card
))
1698 brq
->cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
1699 brq
->data
.blksz
= 512;
1700 brq
->stop
.opcode
= MMC_STOP_TRANSMISSION
;
1702 brq
->stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
1703 brq
->data
.blocks
= blk_rq_sectors(req
);
1706 * The block layer doesn't support all sector count
1707 * restrictions, so we need to be prepared for too big
1710 if (brq
->data
.blocks
> card
->host
->max_blk_count
)
1711 brq
->data
.blocks
= card
->host
->max_blk_count
;
1713 if (brq
->data
.blocks
> 1) {
1715 * After a read error, we redo the request one sector
1716 * at a time in order to accurately determine which
1717 * sectors can be read successfully.
1720 brq
->data
.blocks
= 1;
1722 /* Some controllers can't do multiblock reads due to hw bugs */
1723 if (card
->host
->caps2
& MMC_CAP2_NO_MULTI_READ
&&
1724 rq_data_dir(req
) == READ
)
1725 brq
->data
.blocks
= 1;
1728 if (brq
->data
.blocks
> 1 || do_rel_wr
) {
1729 /* SPI multiblock writes terminate using a special
1730 * token, not a STOP_TRANSMISSION request.
1732 if (!mmc_host_is_spi(card
->host
) ||
1733 rq_data_dir(req
) == READ
)
1734 brq
->mrq
.stop
= &brq
->stop
;
1735 readcmd
= MMC_READ_MULTIPLE_BLOCK
;
1736 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
1738 brq
->mrq
.stop
= NULL
;
1739 readcmd
= MMC_READ_SINGLE_BLOCK
;
1740 writecmd
= MMC_WRITE_BLOCK
;
1742 #ifdef CONFIG_MTK_EMMC_CACHE
1743 /* for non-cacheable system data,
1744 * the implementation of reliable write / force prg write,
1745 * must be applied with mutli write cmd
1747 if (mmc_card_mmc(card
) && (card
->ext_csd
.cache_ctrl
& 0x1)){
1748 writecmd
= MMC_WRITE_MULTIPLE_BLOCK
;
1751 if (rq_data_dir(req
) == READ
) {
1752 brq
->cmd
.opcode
= readcmd
;
1753 brq
->data
.flags
|= MMC_DATA_READ
;
1755 brq
->cmd
.opcode
= writecmd
;
1756 brq
->data
.flags
|= MMC_DATA_WRITE
;
1760 mmc_apply_rel_rw(brq
, card
, req
);
1763 * Data tag is used only during writing meta data to speed
1764 * up write and any subsequent read of this meta data
1766 do_data_tag
= (card
->ext_csd
.data_tag_unit_size
) &&
1767 (req
->cmd_flags
& REQ_META
) &&
1768 (rq_data_dir(req
) == WRITE
) &&
1769 ((brq
->data
.blocks
* brq
->data
.blksz
) >=
1770 card
->ext_csd
.data_tag_unit_size
);
1773 * Pre-defined multi-block transfers are preferable to
1774 * open ended-ones (and necessary for reliable writes).
1775 * However, it is not sufficient to just send CMD23,
1776 * and avoid the final CMD12, as on an error condition
1777 * CMD12 (stop) needs to be sent anyway. This, coupled
1778 * with Auto-CMD23 enhancements provided by some
1779 * hosts, means that the complexity of dealing
1780 * with this is best left to the host. If CMD23 is
1781 * supported by card and host, we'll fill sbc in and let
1782 * the host deal with handling it correctly. This means
1783 * that for hosts that don't expose MMC_CAP_CMD23, no
1784 * change of behavior will be observed.
1786 * N.B: Some MMC cards experience perf degradation.
1787 * We'll avoid using CMD23-bounded multiblock writes for
1788 * these, while retaining features like reliable writes.
1790 if ((md
->flags
& MMC_BLK_CMD23
) && mmc_op_multi(brq
->cmd
.opcode
) &&
1791 (do_rel_wr
|| !(card
->quirks
& MMC_QUIRK_BLK_NO_CMD23
) ||
1793 brq
->sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
1794 brq
->sbc
.arg
= brq
->data
.blocks
|
1795 (do_rel_wr
? (1 << 31) : 0) |
1796 (do_data_tag
? (1 << 29) : 0);
1797 brq
->sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
1798 brq
->mrq
.sbc
= &brq
->sbc
;
1801 mmc_set_data_timeout(&brq
->data
, card
);
1803 brq
->data
.sg
= mqrq
->sg
;
1804 brq
->data
.sg_len
= mmc_queue_map_sg(mq
, mqrq
);
1806 if (brq
->data
.sg_len
> 1024)
1807 pr_err("%s:%d sglen = %x\n", __func__
, __LINE__
, brq
->data
.sg_len
);
1810 * Adjust the sg list so it is the same size as the
1813 if (brq
->data
.blocks
!= blk_rq_sectors(req
)) {
1814 int i
, data_size
= brq
->data
.blocks
<< 9;
1815 struct scatterlist
*sg
;
1817 for_each_sg(brq
->data
.sg
, sg
, brq
->data
.sg_len
, i
) {
1818 data_size
-= sg
->length
;
1819 if (data_size
<= 0) {
1820 sg
->length
+= data_size
;
1825 brq
->data
.sg_len
= i
;
1826 pr_err("%s:%d sglen = %x\n", __func__
, __LINE__
, brq
->data
.sg_len
);
1829 mqrq
->mmc_active
.mrq
= &brq
->mrq
;
1830 mqrq
->mmc_active
.err_check
= mmc_blk_err_check
;
1832 mmc_queue_bounce_pre(mqrq
);
1835 static inline u8
mmc_calc_packed_hdr_segs(struct request_queue
*q
,
1836 struct mmc_card
*card
)
1838 unsigned int hdr_sz
= mmc_large_sector(card
) ? 4096 : 512;
1839 unsigned int max_seg_sz
= queue_max_segment_size(q
);
1840 unsigned int len
, nr_segs
= 0;
1843 len
= min(hdr_sz
, max_seg_sz
);
1851 static u8
mmc_blk_prep_packed_list(struct mmc_queue
*mq
, struct request
*req
)
1853 struct request_queue
*q
= mq
->queue
;
1854 struct mmc_card
*card
= mq
->card
;
1855 struct request
*cur
= req
, *next
= NULL
;
1856 struct mmc_blk_data
*md
= mq
->data
;
1857 struct mmc_queue_req
*mqrq
= mq
->mqrq_cur
;
1858 bool en_rel_wr
= card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
;
1859 unsigned int req_sectors
= 0, phys_segments
= 0;
1860 unsigned int max_blk_count
, max_phys_segs
;
1861 bool put_back
= true;
1862 u8 max_packed_rw
= 0;
1865 if (!(md
->flags
& MMC_BLK_PACKED_CMD
))
1868 if ((rq_data_dir(cur
) == WRITE
) &&
1869 mmc_host_packed_wr(card
->host
))
1870 max_packed_rw
= card
->ext_csd
.max_packed_writes
;
1872 if (max_packed_rw
== 0)
1875 if (mmc_req_rel_wr(cur
) &&
1876 (md
->flags
& MMC_BLK_REL_WR
) && !en_rel_wr
)
1879 if (mmc_large_sector(card
) &&
1880 !IS_ALIGNED(blk_rq_sectors(cur
), 8))
1883 mmc_blk_clear_packed(mqrq
);
1885 max_blk_count
= min(card
->host
->max_blk_count
,
1886 card
->host
->max_req_size
>> 9);
1887 if (unlikely(max_blk_count
> 0xffff))
1888 max_blk_count
= 0xffff;
1890 max_phys_segs
= queue_max_segments(q
);
1891 req_sectors
+= blk_rq_sectors(cur
);
1892 phys_segments
+= cur
->nr_phys_segments
;
1894 if (rq_data_dir(cur
) == WRITE
) {
1895 req_sectors
+= mmc_large_sector(card
) ? 8 : 1;
1896 phys_segments
+= mmc_calc_packed_hdr_segs(q
, card
);
1900 if (reqs
>= max_packed_rw
- 1) {
1905 spin_lock_irq(q
->queue_lock
);
1906 next
= blk_fetch_request(q
);
1907 spin_unlock_irq(q
->queue_lock
);
1913 if (mmc_large_sector(card
) &&
1914 !IS_ALIGNED(blk_rq_sectors(next
), 8))
1917 if (next
->cmd_flags
& REQ_DISCARD
||
1918 next
->cmd_flags
& REQ_FLUSH
)
1921 if (rq_data_dir(cur
) != rq_data_dir(next
))
1924 if (mmc_req_rel_wr(next
) &&
1925 (md
->flags
& MMC_BLK_REL_WR
) && !en_rel_wr
)
1928 req_sectors
+= blk_rq_sectors(next
);
1929 if (req_sectors
> max_blk_count
)
1932 phys_segments
+= next
->nr_phys_segments
;
1933 if (phys_segments
> max_phys_segs
)
1936 list_add_tail(&next
->queuelist
, &mqrq
->packed
->list
);
1942 spin_lock_irq(q
->queue_lock
);
1943 blk_requeue_request(q
, next
);
1944 spin_unlock_irq(q
->queue_lock
);
1948 list_add(&req
->queuelist
, &mqrq
->packed
->list
);
1949 mqrq
->packed
->nr_entries
= ++reqs
;
1950 mqrq
->packed
->retries
= reqs
;
1955 mqrq
->cmd_type
= MMC_PACKED_NONE
;
1959 static void mmc_blk_packed_hdr_wrq_prep(struct mmc_queue_req
*mqrq
,
1960 struct mmc_card
*card
,
1961 struct mmc_queue
*mq
)
1963 struct mmc_blk_request
*brq
= &mqrq
->brq
;
1964 struct request
*req
= mqrq
->req
;
1965 struct request
*prq
;
1966 struct mmc_blk_data
*md
= mq
->data
;
1967 struct mmc_packed
*packed
= mqrq
->packed
;
1968 bool do_rel_wr
, do_data_tag
;
1969 u32
*packed_cmd_hdr
;
1975 mqrq
->cmd_type
= MMC_PACKED_WRITE
;
1977 packed
->idx_failure
= MMC_PACKED_NR_IDX
;
1979 packed_cmd_hdr
= packed
->cmd_hdr
;
1980 memset(packed_cmd_hdr
, 0, sizeof(packed
->cmd_hdr
));
1981 packed_cmd_hdr
[0] = (packed
->nr_entries
<< 16) |
1982 (PACKED_CMD_WR
<< 8) | PACKED_CMD_VER
;
1983 hdr_blocks
= mmc_large_sector(card
) ? 8 : 1;
1986 * Argument for each entry of packed group
1988 list_for_each_entry(prq
, &packed
->list
, queuelist
) {
1989 do_rel_wr
= mmc_req_rel_wr(prq
) && (md
->flags
& MMC_BLK_REL_WR
);
1990 do_data_tag
= (card
->ext_csd
.data_tag_unit_size
) &&
1991 (prq
->cmd_flags
& REQ_META
) &&
1992 (rq_data_dir(prq
) == WRITE
) &&
1993 ((brq
->data
.blocks
* brq
->data
.blksz
) >=
1994 card
->ext_csd
.data_tag_unit_size
);
1995 /* Argument of CMD23 */
1996 packed_cmd_hdr
[(i
* 2)] =
1997 (do_rel_wr
? MMC_CMD23_ARG_REL_WR
: 0) |
1998 (do_data_tag
? MMC_CMD23_ARG_TAG_REQ
: 0) |
1999 blk_rq_sectors(prq
);
2000 /* Argument of CMD18 or CMD25 */
2001 packed_cmd_hdr
[((i
* 2)) + 1] =
2002 mmc_card_blockaddr(card
) ?
2003 blk_rq_pos(prq
) : blk_rq_pos(prq
) << 9;
2004 packed
->blocks
+= blk_rq_sectors(prq
);
2008 memset(brq
, 0, sizeof(struct mmc_blk_request
));
2009 brq
->mrq
.cmd
= &brq
->cmd
;
2010 brq
->mrq
.data
= &brq
->data
;
2011 brq
->mrq
.sbc
= &brq
->sbc
;
2012 brq
->mrq
.stop
= &brq
->stop
;
2014 brq
->sbc
.opcode
= MMC_SET_BLOCK_COUNT
;
2015 brq
->sbc
.arg
= MMC_CMD23_ARG_PACKED
| (packed
->blocks
+ hdr_blocks
);
2016 brq
->sbc
.flags
= MMC_RSP_R1
| MMC_CMD_AC
;
2018 brq
->cmd
.opcode
= MMC_WRITE_MULTIPLE_BLOCK
;
2019 brq
->cmd
.arg
= blk_rq_pos(req
);
2020 if (!mmc_card_blockaddr(card
))
2022 brq
->cmd
.flags
= MMC_RSP_SPI_R1
| MMC_RSP_R1
| MMC_CMD_ADTC
;
2024 brq
->data
.blksz
= 512;
2025 brq
->data
.blocks
= packed
->blocks
+ hdr_blocks
;
2026 brq
->data
.flags
|= MMC_DATA_WRITE
;
2028 brq
->stop
.opcode
= MMC_STOP_TRANSMISSION
;
2030 brq
->stop
.flags
= MMC_RSP_SPI_R1B
| MMC_RSP_R1B
| MMC_CMD_AC
;
2032 mmc_set_data_timeout(&brq
->data
, card
);
2034 brq
->data
.sg
= mqrq
->sg
;
2035 brq
->data
.sg_len
= mmc_queue_map_sg(mq
, mqrq
);
2036 pr_err("%s: sglen = %d\n", __func__
, brq
->data
.sg_len
);
2038 mqrq
->mmc_active
.mrq
= &brq
->mrq
;
2039 mqrq
->mmc_active
.err_check
= mmc_blk_packed_err_check
;
2041 mmc_queue_bounce_pre(mqrq
);
2044 static int mmc_blk_cmd_err(struct mmc_blk_data
*md
, struct mmc_card
*card
,
2045 struct mmc_blk_request
*brq
, struct request
*req
,
2048 struct mmc_queue_req
*mq_rq
;
2049 mq_rq
= container_of(brq
, struct mmc_queue_req
, brq
);
2052 * If this is an SD card and we're writing, we can first
2053 * mark the known good sectors as ok.
2055 * If the card is not SD, we can still ok written sectors
2056 * as reported by the controller (which might be less than
2057 * the real number of written sectors, but never more).
2059 if (mmc_card_sd(card
)) {
2062 blocks
= mmc_sd_num_wr_blocks(card
);
2063 if (blocks
!= (u32
)-1) {
2064 ret
= blk_end_request(req
, 0, blocks
<< 9);
2067 if (!mmc_packed_cmd(mq_rq
->cmd_type
))
2068 ret
= blk_end_request(req
, 0, brq
->data
.bytes_xfered
);
2073 static int mmc_blk_end_packed_req(struct mmc_queue_req
*mq_rq
)
2075 struct request
*prq
;
2076 struct mmc_packed
*packed
= mq_rq
->packed
;
2077 int idx
= packed
->idx_failure
, i
= 0;
2082 while (!list_empty(&packed
->list
)) {
2083 prq
= list_entry_rq(packed
->list
.next
);
2085 /* retry from error index */
2086 packed
->nr_entries
-= idx
;
2090 if (packed
->nr_entries
== MMC_PACKED_NR_SINGLE
) {
2091 list_del_init(&prq
->queuelist
);
2092 mmc_blk_clear_packed(mq_rq
);
2096 list_del_init(&prq
->queuelist
);
2097 blk_end_request(prq
, 0, blk_rq_bytes(prq
));
2101 mmc_blk_clear_packed(mq_rq
);
2105 static void mmc_blk_abort_packed_req(struct mmc_queue_req
*mq_rq
)
2107 struct request
*prq
;
2108 struct mmc_packed
*packed
= mq_rq
->packed
;
2112 while (!list_empty(&packed
->list
)) {
2113 prq
= list_entry_rq(packed
->list
.next
);
2114 list_del_init(&prq
->queuelist
);
2115 blk_end_request(prq
, -EIO
, blk_rq_bytes(prq
));
2118 mmc_blk_clear_packed(mq_rq
);
2121 static void mmc_blk_revert_packed_req(struct mmc_queue
*mq
,
2122 struct mmc_queue_req
*mq_rq
)
2124 struct request
*prq
;
2125 struct request_queue
*q
= mq
->queue
;
2126 struct mmc_packed
*packed
= mq_rq
->packed
;
2130 while (!list_empty(&packed
->list
)) {
2131 prq
= list_entry_rq(packed
->list
.prev
);
2132 if (prq
->queuelist
.prev
!= &packed
->list
) {
2133 list_del_init(&prq
->queuelist
);
2134 spin_lock_irq(q
->queue_lock
);
2135 blk_requeue_request(mq
->queue
, prq
);
2136 spin_unlock_irq(q
->queue_lock
);
2138 list_del_init(&prq
->queuelist
);
2142 mmc_blk_clear_packed(mq_rq
);
2144 #if defined(FEATURE_STORAGE_PERF_INDEX)
2145 #define PRT_TIME_PERIOD 500000000
2146 #define UP_LIMITS_4BYTE 4294967295UL //((4*1024*1024*1024)-1)
2148 pid_t mmcqd
[ID_CNT
]={0};
2149 bool start_async_req
[ID_CNT
] = {0};
2150 unsigned long long start_async_req_time
[ID_CNT
] = {0};
2151 static unsigned long long mmcqd_tag_t1
[ID_CNT
]={0}, mmccid_tag_t1
=0;
2152 unsigned long long mmcqd_t_usage_wr
[ID_CNT
]={0}, mmcqd_t_usage_rd
[ID_CNT
]={0};
2153 unsigned int mmcqd_rq_size_wr
[ID_CNT
]={0}, mmcqd_rq_size_rd
[ID_CNT
]={0};
2154 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};
2155 static unsigned int mmcqd_wr_bit
[ID_CNT
]={0},mmcqd_wr_tract
[ID_CNT
]={0};
2156 static unsigned int mmcqd_rd_bit
[ID_CNT
]={0},mmcqd_rd_tract
[ID_CNT
]={0};
2157 static unsigned int mmcqd_wr_break
[ID_CNT
]={0}, mmcqd_rd_break
[ID_CNT
]={0};
2158 unsigned int mmcqd_rq_count
[ID_CNT
]={0}, mmcqd_wr_rq_count
[ID_CNT
]={0}, mmcqd_rd_rq_count
[ID_CNT
]={0};
2159 extern u32 g_u32_cid
[4];
2160 #ifdef FEATURE_STORAGE_META_LOG
2161 int check_perdev_minors
= CONFIG_MMC_BLOCK_MINORS
;
2162 struct metadata_rwlogger metadata_logger
[10] = {{{0}}};
2165 unsigned int mmcqd_work_percent
[ID_CNT
]={0};
2166 unsigned int mmcqd_w_throughput
[ID_CNT
]={0};
2167 unsigned int mmcqd_r_throughput
[ID_CNT
]={0};
2168 unsigned int mmcqd_read_clear
[ID_CNT
]={0};
2170 static void g_var_clear(unsigned int idx
)
2172 mmcqd_t_usage_wr
[idx
]=0;
2173 mmcqd_t_usage_rd
[idx
]=0;
2174 mmcqd_rq_size_wr
[idx
]=0;
2175 mmcqd_rq_size_rd
[idx
]=0;
2176 mmcqd_rq_count
[idx
]=0;
2177 mmcqd_wr_offset
[idx
]=0;
2178 mmcqd_rd_offset
[idx
]=0;
2179 mmcqd_wr_break
[idx
]=0;
2180 mmcqd_rd_break
[idx
]=0;
2181 mmcqd_wr_tract
[idx
]=0;
2182 mmcqd_wr_bit
[idx
]=0;
2183 mmcqd_rd_tract
[idx
]=0;
2184 mmcqd_rd_bit
[idx
]=0;
2185 mmcqd_wr_rq_count
[idx
]=0;
2186 mmcqd_rd_rq_count
[idx
]=0;
2189 unsigned int find_mmcqd_index(void)
2195 mmcqd_pid
= task_pid_nr(current
);
2198 mmcqd
[0] = mmcqd_pid
;
2199 start_async_req
[0]=0;
2202 for(i
=0;i
<ID_CNT
;i
++)
2204 if(mmcqd_pid
== mmcqd
[i
])
2209 if ((mmcqd
[i
] == 0) ||( i
==ID_CNT
-1))
2212 start_async_req
[i
]=0;
2221 //#undef FEATURE_STORAGE_PID_LOGGER
2222 #if defined(FEATURE_STORAGE_PID_LOGGER)
2224 struct struct_pid_logger g_pid_logger
[PID_ID_CNT
]={{0,0,{0},{0},{0},{0}}};
2228 unsigned char *page_logger
= NULL
;
2229 spinlock_t g_locker
;
2232 static int mmc_blk_issue_rw_rq(struct mmc_queue
*mq
, struct request
*rqc
)
2234 struct mmc_blk_data
*md
= mq
->data
;
2235 struct mmc_card
*card
= md
->queue
.card
;
2236 struct mmc_blk_request
*brq
= &mq
->mqrq_cur
->brq
;
2237 int ret
= 1, disable_multi
= 0, retry
= 0, type
;
2238 enum mmc_blk_status status
;
2239 struct mmc_queue_req
*mq_rq
;
2240 struct request
*req
= rqc
;
2241 struct mmc_async_req
*areq
;
2242 const u8 packed_nr
= 2;
2244 unsigned long long time1
= 0;
2245 #if defined(FEATURE_STORAGE_PERF_INDEX)
2247 unsigned long long t_period
=0, t_usage
=0;
2248 unsigned int t_percent
=0;
2249 unsigned int perf_meter
=0;
2250 unsigned int rq_byte
=0,rq_sector
=0,sect_offset
=0;
2251 unsigned int diversity
=0;
2253 #ifdef FEATURE_STORAGE_META_LOG
2254 unsigned int mmcmetaindex
=0;
2257 #if defined(FEATURE_STORAGE_PID_LOGGER)
2258 unsigned int index
=0;
2261 if (!rqc
&& !mq
->mqrq_prev
->req
)
2263 time1
= sched_clock();
2266 reqs
= mmc_blk_prep_packed_list(mq
, rqc
);
2267 #if defined(FEATURE_STORAGE_PERF_INDEX)
2268 mmcqd_pid
= task_pid_nr(current
);
2270 idx
= find_mmcqd_index();
2272 mmcqd_read_clear
[idx
] = 1;
2273 if(mmccid_tag_t1
==0)
2274 mmccid_tag_t1
= time1
;
2275 t_period
= time1
- mmccid_tag_t1
;
2276 if(t_period
>= (unsigned long long )((PRT_TIME_PERIOD
)*(unsigned long long )10))
2278 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]);
2279 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]);
2280 mmccid_tag_t1
= time1
;
2282 if(mmcqd_tag_t1
[idx
]==0)
2283 mmcqd_tag_t1
[idx
] = time1
;
2284 t_period
= time1
- mmcqd_tag_t1
[idx
];
2286 if(t_period
>= (unsigned long long )PRT_TIME_PERIOD
)
2288 mmcqd_read_clear
[idx
] = 2;
2289 mmcqd_work_percent
[idx
] = 1;
2290 mmcqd_r_throughput
[idx
] = 0;
2291 mmcqd_w_throughput
[idx
] = 0;
2292 t_usage
= mmcqd_t_usage_wr
[idx
] + mmcqd_t_usage_rd
[idx
];
2293 if(t_period
> t_usage
*100)
2294 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
]);
2297 do_div(t_period
, 100); //boundary issue
2298 t_percent
=((unsigned int)t_usage
)/((unsigned int)t_period
);
2299 mmcqd_work_percent
[idx
] = t_percent
;
2300 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
2302 if(mmcqd_wr_rq_count
[idx
] >= 2)
2304 diversity
= mmcqd_wr_offset
[idx
]/(mmcqd_wr_rq_count
[idx
]-1);
2305 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
]);
2307 if(mmcqd_rd_rq_count
[idx
] >= 2)
2309 diversity
= mmcqd_rd_offset
[idx
]/(mmcqd_rd_rq_count
[idx
]-1);
2310 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
]);
2312 if(mmcqd_t_usage_wr
[idx
])
2314 do_div(mmcqd_t_usage_wr
[idx
], 1000000); //boundary issue
2315 if(mmcqd_t_usage_wr
[idx
]) // discard print if duration will <1ms
2317 perf_meter
= (mmcqd_rq_size_wr
[idx
])/((unsigned int)mmcqd_t_usage_wr
[idx
]); //kb/s
2318 mmcqd_w_throughput
[idx
] = perf_meter
;
2319 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
]);
2322 if(mmcqd_t_usage_rd
[idx
])
2324 do_div(mmcqd_t_usage_rd
[idx
], 1000000); //boundary issue
2325 if(mmcqd_t_usage_rd
[idx
]) // discard print if duration will <1ms
2327 perf_meter
= (mmcqd_rq_size_rd
[idx
])/((unsigned int)mmcqd_t_usage_rd
[idx
]); //kb/s
2328 mmcqd_r_throughput
[idx
] = perf_meter
;
2329 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
]);
2332 mmcqd_tag_t1
[idx
]=time1
;
2334 #ifdef FEATURE_STORAGE_META_LOG
2335 mmcmetaindex
= mmc_get_devidx(md
->disk
);
2336 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",
2337 metadata_logger
[mmcmetaindex
].metadata_rw_logger
[0], metadata_logger
[mmcmetaindex
].metadata_rw_logger
[1],
2338 metadata_logger
[mmcmetaindex
].metadata_rw_logger
[2], metadata_logger
[mmcmetaindex
].metadata_rw_logger
[3],
2339 metadata_logger
[mmcmetaindex
].metadata_rw_logger
[4], metadata_logger
[mmcmetaindex
].metadata_rw_logger
[5],
2340 metadata_logger
[mmcmetaindex
].metadata_rw_logger
[6], metadata_logger
[mmcmetaindex
].metadata_rw_logger
[7]);
2341 clear_metadata_rw_status(md
->disk
->first_minor
);
2343 #if defined(FEATURE_STORAGE_PID_LOGGER)
2346 for(index
=0; index
<PID_ID_CNT
; index
++) {
2348 if( g_pid_logger
[index
].current_pid
!=0 && g_pid_logger
[index
].current_pid
== mmcqd_pid
)
2351 if( index
== PID_ID_CNT
)
2353 for( i
=0; i
<PID_LOGGER_COUNT
; i
++) {
2354 //printk(KERN_INFO"hank mmcqd %d %d", g_pid_logger[index].pid_logger[i], mmcqd_pid);
2355 if( g_pid_logger
[index
].pid_logger
[i
] == 0)
2357 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
]);
2361 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "mmcqd pid:%d %s\n", g_pid_logger
[index
].current_pid
, g_pid_logger
[index
].pid_buffer
);
2362 //xlog_printk(ANDROID_LOG_DEBUG, "BLOCK_TAG", "sizeof(&(g_pid_logger[index].pid_logger)):%d\n", sizeof(unsigned short)*PID_LOGGER_COUNT);
2363 //memset( &(g_pid_logger[index].pid_logger), 0, sizeof(struct struct_pid_logger)-(unsigned long)&(((struct struct_pid_logger *)0)->pid_logger));
2364 memset( &(g_pid_logger
[index
].pid_logger
), 0, sizeof(unsigned short)*PID_LOGGER_COUNT
);
2365 memset( &(g_pid_logger
[index
].pid_logger_counter
), 0, sizeof(unsigned short)*PID_LOGGER_COUNT
);
2366 memset( &(g_pid_logger
[index
].pid_logger_length
), 0, sizeof(unsigned int)*PID_LOGGER_COUNT
);
2367 memset( &(g_pid_logger
[index
].pid_logger_r_counter
), 0, sizeof(unsigned short)*PID_LOGGER_COUNT
);
2368 memset( &(g_pid_logger
[index
].pid_logger_r_length
), 0, sizeof(unsigned int)*PID_LOGGER_COUNT
);
2369 memset( &(g_pid_logger
[index
].pid_buffer
), 0, sizeof(char)*1024);
2373 g_pid_logger
[index
].pid_buffer
[0] = '\0';
2378 #if defined(FEATURE_STORAGE_VMSTAT_LOGGER)
2379 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "vmstat (FP:%ld)(FD:%ld)(ND:%ld)(WB:%ld)(NW:%ld)\n",
2380 ((global_page_state(NR_FILE_PAGES
)) << (PAGE_SHIFT
- 10)),
2381 ((global_page_state(NR_FILE_DIRTY
)) << (PAGE_SHIFT
- 10)),
2382 ((global_page_state(NR_DIRTIED
)) << (PAGE_SHIFT
- 10)),
2383 ((global_page_state(NR_WRITEBACK
)) << (PAGE_SHIFT
- 10)),
2384 ((global_page_state(NR_WRITTEN
)) << (PAGE_SHIFT
- 10)));
2390 rq_byte
= blk_rq_bytes(rqc
);
2391 rq_sector
= blk_rq_sectors(rqc
);
2392 if(rq_data_dir(rqc
) == WRITE
)
2394 if(mmcqd_wr_offset_tag
[idx
]>0)
2396 sect_offset
= abs(blk_rq_pos(rqc
) - mmcqd_wr_offset_tag
[idx
]);
2397 mmcqd_wr_offset
[idx
] += sect_offset
;
2398 if(sect_offset
== 1)
2399 mmcqd_wr_break
[idx
]++;
2401 mmcqd_wr_offset_tag
[idx
] = blk_rq_pos(rqc
) + rq_sector
;
2402 if(rq_sector
<= 1) //512 bytes
2403 mmcqd_wr_bit
[idx
] ++;
2404 else if(rq_sector
>= 1016) //508kB
2405 mmcqd_wr_tract
[idx
] ++;
2409 if(mmcqd_rd_offset_tag
[idx
]>0)
2411 sect_offset
= abs(blk_rq_pos(rqc
) - mmcqd_rd_offset_tag
[idx
]);
2412 mmcqd_rd_offset
[idx
] += sect_offset
;
2413 if(sect_offset
== 1)
2414 mmcqd_rd_break
[idx
]++;
2416 mmcqd_rd_offset_tag
[idx
] = blk_rq_pos(rqc
) + rq_sector
;
2417 if(rq_sector
<= 1) //512 bytes
2418 mmcqd_rd_bit
[idx
] ++;
2419 else if(rq_sector
>= 1016) //508kB
2420 mmcqd_rd_tract
[idx
] ++;
2427 * When 4KB native sector is enabled, only 8 blocks
2428 * multiple read or write is allowed
2430 if ((brq
->data
.blocks
& 0x07) &&
2431 (card
->ext_csd
.data_sector_size
== 4096)) {
2432 pr_err("%s: Transfer size is not 4KB sector size aligned\n",
2433 req
->rq_disk
->disk_name
);
2434 mq_rq
= mq
->mqrq_cur
;
2438 if (reqs
>= packed_nr
)
2439 mmc_blk_packed_hdr_wrq_prep(mq
->mqrq_cur
,
2442 mmc_blk_rw_rq_prep(mq
->mqrq_cur
, card
, 0, mq
);
2443 areq
= &mq
->mqrq_cur
->mmc_active
;
2446 areq
= mmc_start_req(card
->host
, areq
, (int *) &status
);
2448 if (status
== MMC_BLK_NEW_REQUEST
)
2449 mq
->flags
|= MMC_QUEUE_NEW_REQUEST
;
2453 mq_rq
= container_of(areq
, struct mmc_queue_req
, mmc_active
);
2456 type
= rq_data_dir(req
) == READ
? MMC_BLK_READ
: MMC_BLK_WRITE
;
2457 mmc_queue_bounce_post(mq_rq
);
2460 case MMC_BLK_SUCCESS
:
2461 case MMC_BLK_PARTIAL
:
2463 * A block was successfully transferred.
2465 mmc_blk_reset_success(md
, type
);
2467 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
2468 ret
= mmc_blk_end_packed_req(mq_rq
);
2471 ret
= blk_end_request(req
, 0,
2472 brq
->data
.bytes_xfered
);
2475 // if (card && card->host && card->host->areq)
2476 // met_mmc_end(card->host, card->host->areq);
2479 * If the blk_end_request function returns non-zero even
2480 * though all data has been transferred and no errors
2481 * were returned by the host controller, it's a bug.
2483 if (status
== MMC_BLK_SUCCESS
&& ret
) {
2484 pr_err("%s BUG rq_tot %d d_xfer %d\n",
2485 __func__
, blk_rq_bytes(req
),
2486 brq
->data
.bytes_xfered
);
2491 case MMC_BLK_CMD_ERR
:
2492 ret
= mmc_blk_cmd_err(md
, card
, brq
, req
, ret
);
2493 if (mmc_blk_reset(md
, card
->host
, type
))
2503 if (!mmc_blk_reset(md
, card
->host
, type
))
2506 case MMC_BLK_DATA_ERR
: {
2509 err
= mmc_blk_reset(md
, card
->host
, type
);
2512 if (err
== -ENODEV
||
2513 mmc_packed_cmd(mq_rq
->cmd_type
))
2517 case MMC_BLK_ECC_ERR
:
2518 if (brq
->data
.blocks
> 1) {
2519 /* Redo read one sector at a time */
2520 pr_warning("%s: retrying using single block read\n",
2521 req
->rq_disk
->disk_name
);
2526 * After an error, we redo I/O one sector at a
2527 * time, so we only reach here after trying to
2528 * read a single sector.
2530 ret
= blk_end_request(req
, -EIO
,
2535 case MMC_BLK_NOMEDIUM
:
2538 pr_err("%s: Unhandled return value (%d)",
2539 req
->rq_disk
->disk_name
, status
);
2544 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
2545 if (!mq_rq
->packed
->retries
)
2547 mmc_blk_packed_hdr_wrq_prep(mq_rq
, card
, mq
);
2548 mmc_start_req(card
->host
,
2549 &mq_rq
->mmc_active
, NULL
);
2553 * In case of a incomplete request
2554 * prepare it again and resend.
2556 mmc_blk_rw_rq_prep(mq_rq
, card
,
2558 mmc_start_req(card
->host
,
2559 &mq_rq
->mmc_active
, NULL
);
2567 if (mmc_packed_cmd(mq_rq
->cmd_type
)) {
2568 mmc_blk_abort_packed_req(mq_rq
);
2570 if (mmc_card_removed(card
))
2571 req
->cmd_flags
|= REQ_QUIET
;
2573 ret
= blk_end_request(req
, -EIO
,
2574 blk_rq_cur_bytes(req
));
2579 if (mmc_card_removed(card
)) {
2580 rqc
->cmd_flags
|= REQ_QUIET
;
2581 blk_end_request_all(rqc
, -EIO
);
2584 * If current request is packed, it needs to put back.
2586 if (mmc_packed_cmd(mq
->mqrq_cur
->cmd_type
))
2587 mmc_blk_revert_packed_req(mq
, mq
->mqrq_cur
);
2589 mmc_blk_rw_rq_prep(mq
->mqrq_cur
, card
, 0, mq
);
2590 mmc_start_req(card
->host
,
2591 &mq
->mqrq_cur
->mmc_active
, NULL
);
2598 static int mmc_blk_issue_rq(struct mmc_queue
*mq
, struct request
*req
)
2601 struct mmc_blk_data
*md
= mq
->data
;
2602 struct mmc_card
*card
= md
->queue
.card
;
2603 struct mmc_host
*host
= card
->host
;
2604 unsigned long flags
;
2605 unsigned int cmd_flags
= req
? req
->cmd_flags
: 0;
2607 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
2608 if (mmc_bus_needs_resume(card
->host
))
2609 mmc_resume_bus(card
->host
);
2612 if (req
&& !mq
->mqrq_prev
->req
)
2613 /* claim host only for the first request */
2614 mmc_claim_host(card
->host
);
2616 ret
= mmc_blk_part_switch(card
, md
);
2619 blk_end_request_all(req
, -EIO
);
2625 mq
->flags
&= ~MMC_QUEUE_NEW_REQUEST
;
2626 if (cmd_flags
& REQ_DISCARD
) {
2627 /* complete ongoing async transfer before issuing discard */
2628 if (card
->host
->areq
)
2629 mmc_blk_issue_rw_rq(mq
, NULL
);
2630 if (req
->cmd_flags
& REQ_SECURE
&&
2631 !(card
->quirks
& MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
))
2632 ret
= mmc_blk_issue_secdiscard_rq(mq
, req
);
2634 ret
= mmc_blk_issue_discard_rq(mq
, req
);
2635 } else if (cmd_flags
& REQ_FLUSH
) {
2636 /* complete ongoing async transfer before issuing flush */
2637 if (card
->host
->areq
)
2638 mmc_blk_issue_rw_rq(mq
, NULL
);
2639 ret
= mmc_blk_issue_flush(mq
, req
);
2641 if (!req
&& host
->areq
) {
2642 spin_lock_irqsave(&host
->context_info
.lock
, flags
);
2643 host
->context_info
.is_waiting_last_req
= true;
2644 spin_unlock_irqrestore(&host
->context_info
.lock
, flags
);
2646 ret
= mmc_blk_issue_rw_rq(mq
, req
);
2650 if ((!req
&& !(mq
->flags
& MMC_QUEUE_NEW_REQUEST
)) ||
2651 (cmd_flags
& MMC_REQ_SPECIAL_MASK
))
2653 * Release host when there are no more requests
2654 * and after special request(discard, flush) is done.
2655 * In case sepecial request, there is no reentry to
2656 * the 'mmc_blk_issue_rq' with 'mqrq_prev->req'.
2658 mmc_release_host(card
->host
);
2662 static inline int mmc_blk_readonly(struct mmc_card
*card
)
2664 return mmc_card_readonly(card
) ||
2665 !(card
->csd
.cmdclass
& CCC_BLOCK_WRITE
);
2668 //#if defined(FEATURE_STORAGE_PID_LOGGER)
2669 //extern unsigned long get_memory_size(void);
2671 #ifdef CONFIG_MTK_EXTMEM
2672 extern void* extmem_malloc_page_align(size_t bytes
);
2674 static struct mmc_blk_data
*mmc_blk_alloc_req(struct mmc_card
*card
,
2675 struct device
*parent
,
2678 const char *subname
,
2681 struct mmc_blk_data
*md
;
2684 devidx
= find_first_zero_bit(dev_use
, max_devices
);
2685 if (devidx
>= max_devices
)
2686 return ERR_PTR(-ENOSPC
);
2687 __set_bit(devidx
, dev_use
);
2689 md
= kzalloc(sizeof(struct mmc_blk_data
), GFP_KERNEL
);
2696 * !subname implies we are creating main mmc_blk_data that will be
2697 * associated with mmc_card with mmc_set_drvdata. Due to device
2698 * partitions, devidx will not coincide with a per-physical card
2699 * index anymore so we keep track of a name index.
2702 md
->name_idx
= find_first_zero_bit(name_use
, max_devices
);
2703 __set_bit(md
->name_idx
, name_use
);
2705 md
->name_idx
= ((struct mmc_blk_data
*)
2706 dev_to_disk(parent
)->private_data
)->name_idx
;
2708 md
->area_type
= area_type
;
2711 * Set the read-only status based on the supported commands
2712 * and the write protect switch.
2714 md
->read_only
= mmc_blk_readonly(card
);
2716 md
->disk
= alloc_disk(perdev_minors
);
2717 if (md
->disk
== NULL
) {
2722 spin_lock_init(&md
->lock
);
2723 INIT_LIST_HEAD(&md
->part
);
2726 ret
= mmc_init_queue(&md
->queue
, card
, &md
->lock
, subname
);
2729 #if defined(FEATURE_STORAGE_PID_LOGGER)
2731 //num_page_logger = sizeof(struct page_pid_logger);
2732 //page_logger = vmalloc(num_physpages*sizeof(struct page_pid_logger));
2733 // solution: use get_memory_size to obtain the size from start pfn to max pfn
2735 //unsigned long count = get_memory_size() >> PAGE_SHIFT;
2736 unsigned long count
= get_max_DRAM_size() >> PAGE_SHIFT
;
2737 #ifdef CONFIG_MTK_EXTMEM
2738 page_logger
= extmem_malloc_page_align(count
* sizeof(struct page_pid_logger
));
2740 page_logger
= vmalloc(count
* sizeof(struct page_pid_logger
));
2743 memset( page_logger
, -1, count
*sizeof( struct page_pid_logger
));
2745 spin_lock_init(&g_locker
);
2748 #if defined(FEATURE_STORAGE_META_LOG)
2749 check_perdev_minors
= perdev_minors
;
2752 md
->queue
.issue_fn
= mmc_blk_issue_rq
;
2753 md
->queue
.data
= md
;
2755 md
->disk
->major
= MMC_BLOCK_MAJOR
;
2756 md
->disk
->first_minor
= devidx
* perdev_minors
;
2757 md
->disk
->fops
= &mmc_bdops
;
2758 md
->disk
->private_data
= md
;
2759 md
->disk
->queue
= md
->queue
.queue
;
2760 md
->disk
->driverfs_dev
= parent
;
2761 set_disk_ro(md
->disk
, md
->read_only
|| default_ro
);
2762 md
->disk
->flags
= GENHD_FL_EXT_DEVT
;
2763 if (area_type
& MMC_BLK_DATA_AREA_RPMB
)
2764 md
->disk
->flags
|= GENHD_FL_NO_PART_SCAN
;
2767 * As discussed on lkml, GENHD_FL_REMOVABLE should:
2769 * - be set for removable media with permanent block devices
2770 * - be unset for removable block devices with permanent media
2772 * Since MMC block devices clearly fall under the second
2773 * case, we do not set GENHD_FL_REMOVABLE. Userspace
2774 * should use the block device creation/destruction hotplug
2775 * messages to tell when the card is present.
2778 snprintf(md
->disk
->disk_name
, sizeof(md
->disk
->disk_name
),
2779 "mmcblk%d%s", md
->name_idx
, subname
? subname
: "");
2781 if (mmc_card_mmc(card
))
2782 blk_queue_logical_block_size(md
->queue
.queue
,
2783 card
->ext_csd
.data_sector_size
);
2785 blk_queue_logical_block_size(md
->queue
.queue
, 512);
2787 set_capacity(md
->disk
, size
);
2789 if (mmc_host_cmd23(card
->host
)) {
2790 if (mmc_card_mmc(card
) ||
2791 (mmc_card_sd(card
) &&
2792 card
->scr
.cmds
& SD_SCR_CMD23_SUPPORT
))
2793 md
->flags
|= MMC_BLK_CMD23
;
2796 if (mmc_card_mmc(card
) &&
2797 md
->flags
& MMC_BLK_CMD23
&&
2798 ((card
->ext_csd
.rel_param
& EXT_CSD_WR_REL_PARAM_EN
) ||
2799 card
->ext_csd
.rel_sectors
)) {
2800 md
->flags
|= MMC_BLK_REL_WR
;
2801 blk_queue_flush(md
->queue
.queue
, REQ_FLUSH
| REQ_FUA
);
2804 if (mmc_card_mmc(card
) &&
2805 (area_type
== MMC_BLK_DATA_AREA_MAIN
) &&
2806 (md
->flags
& MMC_BLK_CMD23
) &&
2807 card
->ext_csd
.packed_event_en
) {
2808 if (!mmc_packed_init(&md
->queue
, card
))
2809 md
->flags
|= MMC_BLK_PACKED_CMD
;
2819 return ERR_PTR(ret
);
2822 static struct mmc_blk_data
*mmc_blk_alloc(struct mmc_card
*card
)
2825 #ifdef CONFIG_MTK_EMMC_SUPPORT
2826 unsigned int l_reserve
;
2827 struct storage_info s_info
= {0};
2829 struct mmc_blk_data
*md
;
2831 if (!mmc_card_sd(card
) && mmc_card_blockaddr(card
)) {
2833 * The EXT_CSD sector count is in number or 512 byte
2836 size
= card
->ext_csd
.sectors
;
2839 * The CSD capacity field is in units of read_blkbits.
2840 * set_capacity takes units of 512 bytes.
2842 size
= card
->csd
.capacity
<< (card
->csd
.read_blkbits
- 9);
2845 if(!mmc_card_sd(card
)){
2846 #ifdef CONFIG_MTK_EMMC_SUPPORT
2847 msdc_get_info(EMMC_CARD_BOOT
, EMMC_RESERVE
, &s_info
);
2848 l_reserve
= s_info
.emmc_reserve
;
2849 printk("l_reserve = 0x%x\n", l_reserve
);
2850 size
-= l_reserve
; /*reserved for 64MB (emmc otp + emmc combo offset + reserved)*/
2853 md
= mmc_blk_alloc_req(card
, &card
->dev
, size
, false, NULL
,
2854 MMC_BLK_DATA_AREA_MAIN
);
2858 static int mmc_blk_alloc_part(struct mmc_card
*card
,
2859 struct mmc_blk_data
*md
,
2860 unsigned int part_type
,
2863 const char *subname
,
2867 struct mmc_blk_data
*part_md
;
2869 part_md
= mmc_blk_alloc_req(card
, disk_to_dev(md
->disk
), size
, default_ro
,
2870 subname
, area_type
);
2871 if (IS_ERR(part_md
))
2872 return PTR_ERR(part_md
);
2873 part_md
->part_type
= part_type
;
2874 list_add(&part_md
->part
, &md
->part
);
2876 string_get_size((u64
)get_capacity(part_md
->disk
) << 9, STRING_UNITS_2
,
2877 cap_str
, sizeof(cap_str
));
2878 pr_info("%s: %s %s partition %u %s\n",
2879 part_md
->disk
->disk_name
, mmc_card_id(card
),
2880 mmc_card_name(card
), part_md
->part_type
, cap_str
);
2884 /* MMC Physical partitions consist of two boot partitions and
2885 * up to four general purpose partitions.
2886 * For each partition enabled in EXT_CSD a block device will be allocatedi
2887 * to provide access to the partition.
2890 static int mmc_blk_alloc_parts(struct mmc_card
*card
, struct mmc_blk_data
*md
)
2894 if (!mmc_card_mmc(card
))
2897 for (idx
= 0; idx
< card
->nr_parts
; idx
++) {
2898 if (card
->part
[idx
].size
) {
2899 ret
= mmc_blk_alloc_part(card
, md
,
2900 card
->part
[idx
].part_cfg
,
2901 card
->part
[idx
].size
>> 9,
2902 card
->part
[idx
].force_ro
,
2903 card
->part
[idx
].name
,
2904 card
->part
[idx
].area_type
);
2913 static void mmc_blk_remove_req(struct mmc_blk_data
*md
)
2915 struct mmc_card
*card
;
2918 card
= md
->queue
.card
;
2919 if (md
->disk
->flags
& GENHD_FL_UP
) {
2920 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2921 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2922 card
->ext_csd
.boot_ro_lockable
)
2923 device_remove_file(disk_to_dev(md
->disk
),
2924 &md
->power_ro_lock
);
2926 /* Stop new requests from getting into the queue */
2927 del_gendisk(md
->disk
);
2930 /* Then flush out any already in there */
2931 mmc_cleanup_queue(&md
->queue
);
2932 if (md
->flags
& MMC_BLK_PACKED_CMD
)
2933 mmc_packed_clean(&md
->queue
);
2938 static void mmc_blk_remove_parts(struct mmc_card
*card
,
2939 struct mmc_blk_data
*md
)
2941 struct list_head
*pos
, *q
;
2942 struct mmc_blk_data
*part_md
;
2944 __clear_bit(md
->name_idx
, name_use
);
2945 list_for_each_safe(pos
, q
, &md
->part
) {
2946 part_md
= list_entry(pos
, struct mmc_blk_data
, part
);
2948 mmc_blk_remove_req(part_md
);
2952 static int mmc_add_disk(struct mmc_blk_data
*md
)
2955 struct mmc_card
*card
= md
->queue
.card
;
2958 md
->force_ro
.show
= force_ro_show
;
2959 md
->force_ro
.store
= force_ro_store
;
2960 sysfs_attr_init(&md
->force_ro
.attr
);
2961 md
->force_ro
.attr
.name
= "force_ro";
2962 md
->force_ro
.attr
.mode
= S_IRUGO
| S_IWUSR
;
2963 ret
= device_create_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2967 if ((md
->area_type
& MMC_BLK_DATA_AREA_BOOT
) &&
2968 card
->ext_csd
.boot_ro_lockable
) {
2971 if (card
->ext_csd
.boot_ro_lock
& EXT_CSD_BOOT_WP_B_PWR_WP_DIS
)
2974 mode
= S_IRUGO
| S_IWUSR
;
2976 md
->power_ro_lock
.show
= power_ro_lock_show
;
2977 md
->power_ro_lock
.store
= power_ro_lock_store
;
2978 sysfs_attr_init(&md
->power_ro_lock
.attr
);
2979 md
->power_ro_lock
.attr
.mode
= mode
;
2980 md
->power_ro_lock
.attr
.name
=
2981 "ro_lock_until_next_power_on";
2982 ret
= device_create_file(disk_to_dev(md
->disk
),
2983 &md
->power_ro_lock
);
2985 goto power_ro_lock_fail
;
2990 device_remove_file(disk_to_dev(md
->disk
), &md
->force_ro
);
2992 del_gendisk(md
->disk
);
2997 #define CID_MANFID_SANDISK 0x2
2998 #define CID_MANFID_TOSHIBA 0x11
2999 #define CID_MANFID_MICRON 0x13
3000 #define CID_MANFID_SAMSUNG 0x15
3001 #define CID_MANFID_SANDISK_NEW 0x45
3002 #define CID_MANFID_HYNIX 0x90
3003 #define CID_MANFID_KSI 0x70
3005 static const struct mmc_fixup blk_fixups
[] =
3007 MMC_FIXUP("SEM02G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3008 MMC_QUIRK_INAND_CMD38
),
3009 MMC_FIXUP("SEM04G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3010 MMC_QUIRK_INAND_CMD38
),
3011 MMC_FIXUP("SEM08G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3012 MMC_QUIRK_INAND_CMD38
),
3013 MMC_FIXUP("SEM16G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3014 MMC_QUIRK_INAND_CMD38
),
3015 MMC_FIXUP("SEM32G", CID_MANFID_SANDISK
, 0x100, add_quirk
,
3016 MMC_QUIRK_INAND_CMD38
),
3017 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_SANDISK_NEW
, CID_OEMID_ANY
, add_quirk
,
3020 * Some MMC cards experience performance degradation with CMD23
3021 * instead of CMD12-bounded multiblock transfers. For now we'll
3022 * black list what's bad...
3023 * - Certain Toshiba cards.
3025 * N.B. This doesn't affect SD cards.
3027 MMC_FIXUP("MMC08G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
3028 MMC_QUIRK_BLK_NO_CMD23
),
3029 MMC_FIXUP("MMC16G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
3030 MMC_QUIRK_BLK_NO_CMD23
),
3031 MMC_FIXUP("MMC32G", CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
3032 MMC_QUIRK_BLK_NO_CMD23
),
3035 * Some Micron MMC cards needs longer data read timeout than
3038 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_MICRON
, 0x200, add_quirk_mmc
,
3039 MMC_QUIRK_LONG_READ_TIME
),
3042 * On these Samsung MoviNAND parts, performing secure erase or
3043 * secure trim can result in unrecoverable corruption due to a
3046 MMC_FIXUP("M8G2FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3047 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3048 MMC_FIXUP("MAG4FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3049 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3050 MMC_FIXUP("MBG8FA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3051 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3052 MMC_FIXUP("MCGAFA", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3053 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3054 MMC_FIXUP("VAL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3055 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3056 MMC_FIXUP("VYL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3057 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3058 MMC_FIXUP("KYL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3059 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3060 MMC_FIXUP("VZL00M", CID_MANFID_SAMSUNG
, CID_OEMID_ANY
, add_quirk_mmc
,
3061 MMC_QUIRK_SEC_ERASE_TRIM_BROKEN
),
3062 #ifdef CONFIG_MTK_EMMC_CACHE
3064 * Some MMC cards cache feature, cannot flush the previous cache data by force programming or reliable write
3065 * which cannot gurrantee the strong order betwee meta data and file data.
3069 * Toshiba eMMC after enable cache feature, write performance drop, because flush operation waste much time
3071 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_TOSHIBA
, CID_OEMID_ANY
, add_quirk_mmc
,
3072 MMC_QUIRK_DISABLE_CACHE
),
3075 /* Hynix 4.41 trim will lead boot up failed. */
3076 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_HYNIX
, CID_OEMID_ANY
, add_quirk_mmc
,
3077 MMC_QUIRK_TRIM_UNSTABLE
),
3079 /* KSI PRV=0x3 trim will lead write performance drop. */
3080 MMC_FIXUP(CID_NAME_ANY
, CID_MANFID_KSI
, CID_OEMID_ANY
, add_quirk_mmc_ksi_v03_skip_trim
,
3081 MMC_QUIRK_KSI_V03_SKIP_TRIM
),
3086 #if defined(CONFIG_MTK_EMMC_SUPPORT) && !defined(CONFIG_MTK_GPT_SCHEME_SUPPORT)
3087 extern void emmc_create_sys_symlink (struct mmc_card
*card
);
3089 static int mmc_blk_probe(struct mmc_card
*card
)
3091 struct mmc_blk_data
*md
, *part_md
;
3095 * Check that the card supports the command class(es) we need.
3097 if (!(card
->csd
.cmdclass
& CCC_BLOCK_READ
))
3100 md
= mmc_blk_alloc(card
);
3104 string_get_size((u64
)get_capacity(md
->disk
) << 9, STRING_UNITS_2
,
3105 cap_str
, sizeof(cap_str
));
3106 pr_info("%s: %s %s %s %s\n",
3107 md
->disk
->disk_name
, mmc_card_id(card
), mmc_card_name(card
),
3108 cap_str
, md
->read_only
? "(ro)" : "");
3110 if (mmc_blk_alloc_parts(card
, md
))
3113 mmc_set_drvdata(card
, md
);
3114 mmc_fixup_device(card
, blk_fixups
);
3116 printk("[%s]: %s by manufacturer settings, quirks=0x%x\n", __func__
, md
->disk
->disk_name
, card
->quirks
);
3118 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3119 mmc_set_bus_resume_policy(card
->host
, 1);
3121 if (mmc_add_disk(md
))
3124 list_for_each_entry(part_md
, &md
->part
, part
) {
3125 if (mmc_add_disk(part_md
))
3128 #if defined(CONFIG_MTK_EMMC_SUPPORT) && !defined(CONFIG_MTK_GPT_SCHEME_SUPPORT)
3129 emmc_create_sys_symlink(card
);
3134 mmc_blk_remove_parts(card
, md
);
3135 mmc_blk_remove_req(md
);
3139 static void mmc_blk_remove(struct mmc_card
*card
)
3141 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
3143 mmc_blk_remove_parts(card
, md
);
3144 mmc_claim_host(card
->host
);
3145 mmc_blk_part_switch(card
, md
);
3146 mmc_release_host(card
->host
);
3147 mmc_blk_remove_req(md
);
3148 mmc_set_drvdata(card
, NULL
);
3149 #ifdef CONFIG_MMC_BLOCK_DEFERRED_RESUME
3150 mmc_set_bus_resume_policy(card
->host
, 0);
3155 static int mmc_blk_suspend(struct mmc_card
*card
)
3157 struct mmc_blk_data
*part_md
;
3158 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
3161 mmc_queue_suspend(&md
->queue
);
3162 list_for_each_entry(part_md
, &md
->part
, part
) {
3163 mmc_queue_suspend(&part_md
->queue
);
3169 static int mmc_blk_resume(struct mmc_card
*card
)
3171 struct mmc_blk_data
*part_md
;
3172 struct mmc_blk_data
*md
= mmc_get_drvdata(card
);
3176 * Resume involves the card going into idle state,
3177 * so current partition is always the main one.
3179 md
->part_curr
= md
->part_type
;
3180 mmc_queue_resume(&md
->queue
);
3181 list_for_each_entry(part_md
, &md
->part
, part
) {
3182 mmc_queue_resume(&part_md
->queue
);
3188 #define mmc_blk_suspend NULL
3189 #define mmc_blk_resume NULL
3192 static struct mmc_driver mmc_driver
= {
3196 .probe
= mmc_blk_probe
,
3197 .remove
= mmc_blk_remove
,
3198 .suspend
= mmc_blk_suspend
,
3199 .resume
= mmc_blk_resume
,
3202 static int __init
mmc_blk_init(void)
3206 if (perdev_minors
!= CONFIG_MMC_BLOCK_MINORS
)
3207 pr_info("mmcblk: using %d minors per device\n", perdev_minors
);
3209 max_devices
= 256 / perdev_minors
;
3211 res
= register_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3215 res
= mmc_register_driver(&mmc_driver
);
3221 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3226 static void __exit
mmc_blk_exit(void)
3228 mmc_unregister_driver(&mmc_driver
);
3229 unregister_blkdev(MMC_BLOCK_MAJOR
, "mmc");
3232 module_init(mmc_blk_init
);
3233 module_exit(mmc_blk_exit
);
3235 MODULE_LICENSE("GPL");
3236 MODULE_DESCRIPTION("Multimedia Card (MMC) block device driver");