Skip to content

Commit dc91338

Browse files
digetxaxboe
authored andcommitted
mmc: block: Support alternative_gpt_sector() operation
Support generic alternative_gpt_sector() block device operation. It calculates location of GPT entry for eMMC of NVIDIA Tegra Android devices. Add new MMC_CAP2_ALT_GPT_TEGRA flag that enables scanning of alternative GPT sector and add raw_boot_mult field to mmc_ext_csd which allows to get size of the boot partitions that is needed for the calculation. Signed-off-by: Dmitry Osipenko <[email protected]> Reviewed-by: Ulf Hansson <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 466d9c4 commit dc91338

File tree

6 files changed

+62
-0
lines changed

6 files changed

+62
-0
lines changed

drivers/mmc/core/block.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,26 @@ static int mmc_blk_compat_ioctl(struct block_device *bdev, fmode_t mode,
831831
}
832832
#endif
833833

834+
static int mmc_blk_alternative_gpt_sector(struct gendisk *disk,
835+
sector_t *sector)
836+
{
837+
struct mmc_blk_data *md;
838+
int ret;
839+
840+
md = mmc_blk_get(disk);
841+
if (!md)
842+
return -EINVAL;
843+
844+
if (md->queue.card)
845+
ret = mmc_card_alternative_gpt_sector(md->queue.card, sector);
846+
else
847+
ret = -ENODEV;
848+
849+
mmc_blk_put(md);
850+
851+
return ret;
852+
}
853+
834854
static const struct block_device_operations mmc_bdops = {
835855
.open = mmc_blk_open,
836856
.release = mmc_blk_release,
@@ -840,6 +860,7 @@ static const struct block_device_operations mmc_bdops = {
840860
#ifdef CONFIG_COMPAT
841861
.compat_ioctl = mmc_blk_compat_ioctl,
842862
#endif
863+
.alternative_gpt_sector = mmc_blk_alternative_gpt_sector,
843864
};
844865

845866
static int mmc_blk_part_switch_pre(struct mmc_card *card,

drivers/mmc/core/core.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,6 +2149,41 @@ int mmc_detect_card_removed(struct mmc_host *host)
21492149
}
21502150
EXPORT_SYMBOL(mmc_detect_card_removed);
21512151

2152+
int mmc_card_alternative_gpt_sector(struct mmc_card *card, sector_t *gpt_sector)
2153+
{
2154+
unsigned int boot_sectors_num;
2155+
2156+
if ((!(card->host->caps2 & MMC_CAP2_ALT_GPT_TEGRA)))
2157+
return -EOPNOTSUPP;
2158+
2159+
/* filter out unrelated cards */
2160+
if (card->ext_csd.rev < 3 ||
2161+
!mmc_card_mmc(card) ||
2162+
!mmc_card_is_blockaddr(card) ||
2163+
mmc_card_is_removable(card->host))
2164+
return -ENOENT;
2165+
2166+
/*
2167+
* eMMC storage has two special boot partitions in addition to the
2168+
* main one. NVIDIA's bootloader linearizes eMMC boot0->boot1->main
2169+
* accesses, this means that the partition table addresses are shifted
2170+
* by the size of boot partitions. In accordance with the eMMC
2171+
* specification, the boot partition size is calculated as follows:
2172+
*
2173+
* boot partition size = 128K byte x BOOT_SIZE_MULT
2174+
*
2175+
* Calculate number of sectors occupied by the both boot partitions.
2176+
*/
2177+
boot_sectors_num = card->ext_csd.raw_boot_mult * SZ_128K /
2178+
SZ_512 * MMC_NUM_BOOT_PARTITION;
2179+
2180+
/* Defined by NVIDIA and used by Android devices. */
2181+
*gpt_sector = card->ext_csd.sectors - boot_sectors_num - 1;
2182+
2183+
return 0;
2184+
}
2185+
EXPORT_SYMBOL(mmc_card_alternative_gpt_sector);
2186+
21522187
void mmc_rescan(struct work_struct *work)
21532188
{
21542189
struct mmc_host *host =

drivers/mmc/core/core.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ void mmc_release_host(struct mmc_host *host);
119119
void mmc_get_card(struct mmc_card *card, struct mmc_ctx *ctx);
120120
void mmc_put_card(struct mmc_card *card, struct mmc_ctx *ctx);
121121

122+
int mmc_card_alternative_gpt_sector(struct mmc_card *card, sector_t *sector);
123+
122124
/**
123125
* mmc_claim_host - exclusively claim a host
124126
* @host: mmc host to claim

drivers/mmc/core/mmc.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ static int mmc_decode_ext_csd(struct mmc_card *card, u8 *ext_csd)
418418
ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
419419
card->ext_csd.raw_hc_erase_grp_size =
420420
ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE];
421+
card->ext_csd.raw_boot_mult =
422+
ext_csd[EXT_CSD_BOOT_MULT];
421423
if (card->ext_csd.rev >= 3) {
422424
u8 sa_shift = ext_csd[EXT_CSD_S_A_TIMEOUT];
423425
card->ext_csd.part_config = ext_csd[EXT_CSD_PART_CONFIG];

include/linux/mmc/card.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ struct mmc_ext_csd {
109109
u8 raw_hc_erase_gap_size; /* 221 */
110110
u8 raw_erase_timeout_mult; /* 223 */
111111
u8 raw_hc_erase_grp_size; /* 224 */
112+
u8 raw_boot_mult; /* 226 */
112113
u8 raw_sec_trim_mult; /* 229 */
113114
u8 raw_sec_erase_mult; /* 230 */
114115
u8 raw_sec_feature_support;/* 231 */

include/linux/mmc/host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ struct mmc_host {
398398
#else
399399
#define MMC_CAP2_CRYPTO 0
400400
#endif
401+
#define MMC_CAP2_ALT_GPT_TEGRA (1 << 28) /* Host with eMMC that has GPT entry at a non-standard location */
401402

402403
int fixed_drv_type; /* fixed driver type for non-removable media */
403404

0 commit comments

Comments
 (0)