Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 107 additions & 50 deletions drivers/misc/apple-h2fmi-ro.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,25 @@ struct ro_bus {
u32 if_ctrl; /* SDR timing */
};

struct ro_geom {
u32 caus;
u32 cau_bits;
u32 blocks;
u32 block_bits;
u32 pages;
u32 slc_pages;
u32 page_bits;
u32 lanes;
u8 row_bytes;
u8 full_bytes;
};

struct ro {
struct device *dev;
struct ro_bus bus[RO_BUSES];
void __iomem *ps; /* four PMGR power-state registers */
u32 ce_bitmap;
const struct ro_geom *geom;
bool set_power_ok;
struct mutex lock;
u8 *res;
Expand Down Expand Up @@ -437,7 +451,7 @@ static int ro_sdr_get(struct ro *ro, struct ro_bus *b, unsigned int ce, u8 op, u

/* Page read in SDR: the full address is a u16 byte count, u16 column, row. */
static int ro_sdr_page(struct ro *ro, struct ro_bus *b, unsigned int ce, u32 row,
u8 *buf, u32 len, u8 *st)
unsigned int full_bytes, u8 *buf, u32 len, u8 *st)
{
int ret, end_ret = 0;

Expand All @@ -449,7 +463,7 @@ static int ro_sdr_page(struct ro *ro, struct ro_bus *b, unsigned int ce, u32 row
if (!ret) {
writel(len & 0xffff, b->fmc + FMC_ADDR0);
writel(row, b->fmc + FMC_ADDR1);
writel((8 + 7) & 7, b->fmc + FMC_ADDRNUM);
writel((full_bytes + 7) & 7, b->fmc + FMC_ADDRNUM);
writel(0xb, b->fmc + FMC_RW_CTRL);
ret = ro_wait(ro, b->fmc, FMC_STATUS, 0xb, 0xb);
}
Expand Down Expand Up @@ -641,7 +655,9 @@ static int ro_do_get(struct ro *ro, unsigned int bus, unsigned int ce, u8 op, u8
} else if (b->mode == RO_SDR) {
if (page && len % RO_SDR_SECTOR)
return -EINVAL;
ret = page ? ro_sdr_page(ro, b, ce, addr, ro->res, len, &st) :
ret = page ? ro_sdr_page(ro, b, ce, addr,
ro->geom ? ro->geom->full_bytes : 8,
ro->res, len, &st) :
ro_sdr_get(ro, b, ce, op, op2, addr, naddr, ro->res, len, &st);
} else {
return -ENODEV;
Expand All @@ -655,45 +671,59 @@ static int ro_do_get(struct ro *ro, unsigned int bus, unsigned int ce, u8 op, u8

/* ---- read-only MTD view (needs CONFIG_MTD) ----
*
* The iPad3,3 geometry measured on 2026-12-02 (four PPN 1.6 packages, every
* one identical). Each of the 8 lanes is a read-only MTD twice: the normal
* (MLC) view and the SLC view the VFL and FTL context use. lane = bus +
* 2 * (2 * ce + cau), from the FTL's own striping counters.
* Measured iPad3,3 geometries (four packages, CE0 and CE1 on both buses,
* 8 KiB data + 32 B OOB pages):
* - 32 GB PPN 1.6: 2 CAUs/CE (cau_bits 3), 2048 blocks (11 bits), 8 lanes
* - 64 GB PPN 1.3: 4 CAUs/CE (cau_bits 2), 2064 blocks (12 bits), 16 lanes
*
* Each lane is a read-only MTD twice: the normal (MLC) view and the SLC view
* the VFL and FTL context use. lane = bus + 2 * (caus * ce + cau).
*/
#define J2A_CAUS 2
#define J2A_CAU_BITS 3
#define J2A_BLOCKS 2048
#define J2A_BLOCK_BITS 11
#define J2A_PAGES 256
#define J2A_SLC_PAGES 128
#define J2A_PAGE_BITS 8
#define J2A_RAW 8224
#define J2A_CHUNKS 2
#define J2A_CHUNK_LO 1024
#define J2A_CHUNK_META 16
#define J2A_CHUNK_RAW 4112
#define J2A_DATA 8192
#define J2A_OOB 32
#define J2A_LANES 8
static_assert(J2A_DATA == 1 << 13 && J2A_RAW % RO_SDR_SECTOR == 0);

static const struct ro_geom ro_geoms[] = {
{ /* 32 GB PPN 1.6 */
.caus = 2, .cau_bits = 3, .blocks = 2048, .block_bits = 11,
.pages = 256, .slc_pages = 128, .page_bits = 8, .lanes = 8,
.row_bytes = 4, .full_bytes = 8,
},
{ /* 64 GB PPN 1.3 */
.caus = 4, .cau_bits = 2, .blocks = 2064, .block_bits = 12,
.pages = 256, .slc_pages = 128, .page_bits = 8, .lanes = 16,
.row_bytes = 3, .full_bytes = 7,
},
};

/* One raw page of one lane; caller holds ro->lock. */
static int ro_lane_page(struct ro *ro, unsigned int lane, bool slc, u32 block, u32 page,
u8 *raw, u8 *st)
{
unsigned int bus = lane & 1, idx = lane >> 1, ce = idx >> 1, cau = idx & 1;
const struct ro_geom *g = ro->geom;
unsigned int bus = lane & 1, idx = lane >> 1;
unsigned int ce, cau;
struct ro_bus *b = &ro->bus[bus];
u32 row;

if (lane >= J2A_LANES || block >= J2A_BLOCKS ||
page >= (slc ? J2A_SLC_PAGES : J2A_PAGES) || !ro_ce_ok(ro, bus, ce))
if (!g || lane >= g->lanes)
return -EINVAL;
row = page | block << J2A_PAGE_BITS | cau << (J2A_PAGE_BITS + J2A_BLOCK_BITS) |
(slc ? 1u << (J2A_PAGE_BITS + J2A_BLOCK_BITS + J2A_CAU_BITS) : 0);
ce = idx / g->caus;
cau = idx % g->caus;
if (block >= g->blocks || page >= (slc ? g->slc_pages : g->pages) ||
!ro_ce_ok(ro, bus, ce))
return -EINVAL;
row = page | block << g->page_bits | cau << (g->page_bits + g->block_bits) |
(slc ? 1u << (g->page_bits + g->block_bits + g->cau_bits) : 0);
if (b->mode == RO_SDR)
return ro_sdr_page(ro, b, ce, row, raw, J2A_RAW, st);
return ro_sdr_page(ro, b, ce, row, g->full_bytes, raw, J2A_RAW, st);
if (b->mode == RO_DDR)
return ro_ddr_get(ro, b, ce, 0x0a, 0x37, row, 4, raw, J2A_RAW, true, st);
return ro_ddr_get(ro, b, ce, 0x0a, 0x37, row, g->row_bytes, raw, J2A_RAW, true, st);
return -ENODEV;
}

Expand All @@ -708,18 +738,32 @@ struct ro_lane_mtd {
static struct ro_lane_mtd *ro_mtds;
static u8 *ro_mtd_raw;

static int ro_check_params(const u8 *p)
static const struct ro_geom *ro_match_params(const u8 *p, int *err)
{
static const u32 want[] = { J2A_CAUS, J2A_CAU_BITS, J2A_BLOCKS, J2A_BLOCK_BITS,
J2A_PAGES, J2A_SLC_PAGES, J2A_PAGE_BITS, 1, 2, J2A_RAW };
int i;
unsigned int g, i;

if (memcmp(p, "PPN Device Info", 15))
return -ENODEV;
for (i = 0; i < ARRAY_SIZE(want); i++)
if (get_unaligned_le32(p + 0x10 + 4 * i) != want[i])
return -EOPNOTSUPP;
return 0;
if (memcmp(p, "PPN Device Info", 15)) {
*err = -ENODEV;
return NULL;
}
for (g = 0; g < ARRAY_SIZE(ro_geoms); g++) {
const struct ro_geom *cand = &ro_geoms[g];
const u32 want[] = {
cand->caus, cand->cau_bits, cand->blocks, cand->block_bits,
cand->pages, cand->slc_pages, cand->page_bits,
1, J2A_CHUNKS, J2A_RAW,
};

for (i = 0; i < ARRAY_SIZE(want); i++)
if (get_unaligned_le32(p + 0x10 + 4 * i) != want[i])
break;
if (i == ARRAY_SIZE(want)) {
*err = 0;
return cand;
}
}
*err = -EOPNOTSUPP;
return NULL;
}

static void ro_split(const u8 *raw, u8 *data, u8 *oob)
Expand All @@ -743,7 +787,8 @@ static void ro_split(const u8 *raw, u8 *data, u8 *oob)
* anything else (0x45 retired, errors) is an I/O error with no data returned. */
static int ro_mtd_read_page(struct ro_lane_mtd *m, loff_t ofs, u8 *data, u8 *oob)
{
u32 pages = m->slc ? J2A_SLC_PAGES : J2A_PAGES;
const struct ro_geom *g = m->ro->geom;
u32 pages = m->slc ? g->slc_pages : g->pages;
u32 index = (u32)(ofs >> 13); /* J2A_DATA = 8192 */
u8 st = 0;
int ret;
Expand Down Expand Up @@ -830,6 +875,7 @@ static int ro_mtd_isbad(struct mtd_info *mtd, loff_t ofs)
/* Bring the bus up (SDR unless iBoot left DDR), check every package, register. */
static int ro_mtd_init(struct ro *ro)
{
const struct ro_geom *geom = NULL;
unsigned int bus, ce, i;
int ret;

Expand All @@ -843,33 +889,43 @@ static int ro_mtd_init(struct ro *ro)
dev_err(ro->dev, "SDR bring-up: %d\n%.*s", ret, (int)ro->res_len, ro->res);
for (bus = 0; !ret && bus < RO_BUSES; bus++)
for (ce = 0; !ret && ce < 8; ce++) {
const struct ro_geom *cand;

if (!ro_ce_ok(ro, bus, ce))
continue;
ret = ro_do_get(ro, bus, ce, 0x92, 0x97, 0, 1, RO_PARAMS_SIZE, false);
if (!ret)
ret = ro_check_params(ro->res);
if (!ret) {
cand = ro_match_params(ro->res, &ret);
if (!ret && geom && cand != geom)
ret = -EOPNOTSUPP;
else if (!ret)
geom = cand;
}
if (ret)
dev_err(ro->dev, "bus %u ce %u: parameters %d: not the measured J2A geometry\n",
dev_err(ro->dev, "bus %u ce %u: parameters %d: unsupported J2A geometry\n",
bus, ce, ret);
}
ro->res_len = 0;
mutex_unlock(&ro->lock);
if (ret)
return ret;
if (!geom)
return -ENODEV;
ro->geom = geom;

ro_mtd_raw = devm_kmalloc(ro->dev, J2A_RAW, GFP_KERNEL);
ro_mtds = devm_kcalloc(ro->dev, 2 * J2A_LANES, sizeof(*ro_mtds), GFP_KERNEL);
ro_mtds = devm_kcalloc(ro->dev, 2 * geom->lanes, sizeof(*ro_mtds), GFP_KERNEL);
if (!ro_mtd_raw || !ro_mtds)
return -ENOMEM;
for (i = 0; i < 2 * J2A_LANES; i++) {
for (i = 0; i < 2 * geom->lanes; i++) {
struct ro_lane_mtd *m = &ro_mtds[i];
struct mtd_info *mtd = &m->mtd;
u32 pages;

m->ro = ro;
m->lane = i % J2A_LANES;
m->slc = i >= J2A_LANES;
pages = m->slc ? J2A_SLC_PAGES : J2A_PAGES;
m->lane = i % geom->lanes;
m->slc = i >= geom->lanes;
pages = m->slc ? geom->slc_pages : geom->pages;
mtd->name = devm_kasprintf(ro->dev, GFP_KERNEL, "j2a-lane%u%s", m->lane,
m->slc ? "-slc" : "");
if (!mtd->name)
Expand All @@ -879,7 +935,7 @@ static int ro_mtd_init(struct ro *ro)
mtd->type = MTD_NANDFLASH;
mtd->flags = MTD_NO_ERASE; /* and no MTD_WRITEABLE: the core returns -EROFS */
mtd->erasesize = pages * J2A_DATA;
mtd->size = (u64)J2A_BLOCKS * mtd->erasesize;
mtd->size = (u64)geom->blocks * mtd->erasesize;
mtd->writesize = J2A_DATA;
mtd->writebufsize = J2A_DATA;
mtd->oobsize = J2A_OOB;
Expand All @@ -895,8 +951,8 @@ static int ro_mtd_init(struct ro *ro)
return ret;
}
}
dev_info(ro->dev, "%u read-only MTDs: lanes 0-7 normal (%u pages) and SLC (%u pages), %u blocks\n",
2 * J2A_LANES, J2A_PAGES, J2A_SLC_PAGES, J2A_BLOCKS);
dev_info(ro->dev, "%u read-only MTDs: lanes 0-%u normal (%u pages) and SLC (%u pages), %u blocks\n",
2 * geom->lanes, geom->lanes - 1, geom->pages, geom->slc_pages, geom->blocks);
return 0;
}
#if IS_REACHABLE(CONFIG_MTD_APPLE_SFTL)
Expand All @@ -908,17 +964,18 @@ static struct apple_ppn_ops ro_ppn_ops;

/*
* The SFTL's (bus, cau) is a lane: lane = bus + 2 * cau, cau counting the
* bus's four CAUs. Status as Orchard's h2fmi_ppn_read: GOOD, REFRESH and a
* truly blank ERASED page return data; RETIRED returns zeros and its status;
* anything else is an I/O error. There is no program or erase operation.
* bus's CAUs across its populated CEs. Status as Orchard's h2fmi_ppn_read:
* GOOD, REFRESH and a truly blank ERASED page return data; RETIRED returns
* zeros and its status; anything else is an I/O error. There is no program
* or erase operation.
*/
static int ro_ppn_read(void *priv, unsigned int bus, unsigned int cau, unsigned int block,
unsigned int page, bool context, u8 *data, u8 *oob, u8 *status)
{
struct ro *ro = priv;
int ret;

if (bus > 1 || cau > 3 || !data || !oob || !status)
if (!ro->geom || bus > 1 || cau >= ro->geom->lanes / 2 || !data || !oob || !status)
return -EINVAL;
memset(data, 0, J2A_DATA);
memset(oob, 0, J2A_OOB);
Expand All @@ -939,8 +996,8 @@ static int ro_sftl_init(struct ro *ro)
{
struct apple_ppn provider = {
.dev = ro->dev, .ops = &ro_ppn_ops, .priv = ro, .access = &ro_ppn_access,
.buses = 2, .caus = 4, .blocks = J2A_BLOCKS, .pages = J2A_PAGES,
.page_size = J2A_DATA, .oob_size = J2A_OOB,
.buses = 2, .caus = ro->geom->lanes / 2, .blocks = ro->geom->blocks,
.pages = ro->geom->pages, .page_size = J2A_DATA, .oob_size = J2A_OOB,
};
struct platform_device *pdev;

Expand Down
22 changes: 22 additions & 0 deletions drivers/mtd/apple-sftl-core.c
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,32 @@ static const struct sftl_geometry sftl_32gb = {
.degrade_remnants = true, .lpp = 2, .reused_names = true, .moved_epochs = true,
};

/*
* 64 GB PPN 1.3 with 12H321 (iOS 8.4.1) on the media, an iPad3,3, measured
* read-only: four packages of four CAUs (CE0 and CE1 on both buses), 16 lanes
* (lane = bus + 2 * (4 * CE + CAU)), 2064 blocks of 256 pages of 8 KiB (two
* 4 KiB logical pages per physical page). 2057 superblocks; the VFL in block
* 2061 of lanes 0-7 (group 0 lanes 0-3, group 1 lanes 4-7, 128 SLC pages);
* 15625000 exported and 16 hidden logical pages. Reads only: no writer,
* reclamation or VFL rotation.
*/
static const struct sftl_geometry sftl_64gb_8k = {
.buses = 2, .caus = 8, .lanes = 16, .blocks = 2064, .pages = 256,
.subblocks = 2057, .vfl_pages = 128, .vfl_first = 2057,
.bootstrap = { 2061 }, .nr_bootstrap = 1,
.map_batch = 16,
.disk_pages = 15625000, .lpages = 15625016,
.summary_sequences = true, .degrade_remnants = true,
.hidden_tail_type2 = true, .lpp = 2,
.reused_names = true, .moved_epochs = true,
.older_epoch_subtype1 = true,
};

static const struct sftl_geometry *const sftl_geometries[] = {
&sftl_64gb,
&sftl_16gb,
&sftl_32gb,
&sftl_64gb_8k,
};

struct sftl_run {
Expand Down
Loading