diff --git a/drivers/misc/apple-h2fmi-ro.c b/drivers/misc/apple-h2fmi-ro.c index e578ec70160361..913159e1ef69bd 100644 --- a/drivers/misc/apple-h2fmi-ro.c +++ b/drivers/misc/apple-h2fmi-ro.c @@ -98,6 +98,12 @@ static const u8 ro_ops[][2] = { { 0xff, 0x00 }, /* NAND reset (SDR bring-up) */ { 0xef, 0x00 }, /* set feature: only via sdr_set_power() */ { 0xe7, 0x00 }, /* set feature confirm */ +#if IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE) + { 0x8a, 0x00 }, /* PPN program: only via ro_lane_program() */ + { 0x17, 0x00 }, /* PPN program confirm */ + { 0x6a, 0xd7 }, /* PPN block erase: only via ro_lane_erase() */ + { 0x79, 0x00 }, /* PPN mutation status */ +#endif }; enum ro_mode { RO_UNKNOWN, RO_NONE, RO_DDR, RO_SDR }; @@ -108,12 +114,29 @@ 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; + bool program_ok; + bool erase_ok; + bool failed; struct mutex lock; u8 *res; size_t res_len; @@ -139,6 +162,12 @@ static int ro_cmd_write(struct ro *ro, struct ro_bus *b, u8 op, u8 op2) if (op == 0xef && !ro->set_power_ok) goto refuse; + if ((op == 0x8a || op == 0x17) && !ro->program_ok) + goto refuse; + if (op == 0x6a && !ro->erase_ok) + goto refuse; + if (op == 0x79 && !ro->program_ok && !ro->erase_ok) + goto refuse; for (i = 0; i < ARRAY_SIZE(ro_ops); i++) if (ro_ops[i][0] == op && ro_ops[i][1] == op2) { writel(op | op2 << 8, b->fmc + FMC_CMD); @@ -437,7 +466,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; @@ -449,7 +478,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); } @@ -641,7 +670,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; @@ -655,18 +686,14 @@ 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 @@ -674,26 +701,44 @@ static int ro_do_get(struct ro *ro, unsigned int bus, unsigned int ce, u8 op, u8 #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; } @@ -708,18 +753,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) @@ -743,7 +802,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; @@ -830,6 +890,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; @@ -843,33 +904,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) @@ -879,7 +950,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; @@ -895,8 +966,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) @@ -908,9 +979,10 @@ 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) @@ -918,7 +990,7 @@ static int ro_ppn_read(void *priv, unsigned int bus, unsigned int cau, unsigned 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); @@ -935,22 +1007,339 @@ static int ro_ppn_read(void *priv, unsigned int bus, unsigned int cau, unsigned return ret; } +#if IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE) +static void ro_join(u8 *raw, const u8 *data, const u8 *oob) +{ + int c; + + for (c = 0; c < J2A_CHUNKS; c++) { + u8 *dst = raw + c * J2A_CHUNK_RAW; + const u8 *src = data + c * 4096; + + memcpy(dst, src, J2A_CHUNK_LO); + memcpy(dst + J2A_CHUNK_LO, oob + c * J2A_CHUNK_META, J2A_CHUNK_META); + memcpy(dst + J2A_CHUNK_LO + J2A_CHUNK_META, src + J2A_CHUNK_LO, + 4096 - J2A_CHUNK_LO); + } +} + +static int ro_sdr_transmit(struct ro *ro, struct ro_bus *b, const u8 *raw, u32 len) +{ + unsigned int sector, word; + int ret; + + for (sector = 0; sector < len / RO_SDR_SECTOR; sector++) { + const u8 *p = raw + sector * RO_SDR_SECTOR; + + if (!(sector % 4)) { + ro_sdr_clear(b); + writel(5, b->fmi + FMI_CONFIG); + writel(RO_SDR_SECTOR << 8 | 4, b->fmi + FMI_PIO_CONFIG); + writel(5, b->fmi + FMI_CONTROL); + } + ret = ro_sdr_fifo(ro, b); + if (ret) + return ret; + for (word = 0; word < RO_SDR_SECTOR; word += 4) + writel(get_unaligned_le32(p + word), b->fmi + FMI_DATA); + if (sector % 4 == 3) { + ret = ro_wait(ro, b->fmi, FMI_STATUS, 2, 2); + if (ret) + return ret; + } + } + return 0; +} + +static int ro_ddr_data_in(struct ro *ro, struct ro_bus *b, const u8 *buf, u32 len) +{ + u32 off, i, v; + int ret; + + if (!len || len & 3) + return -EINVAL; + for (off = 0; off < len; ) { + u32 bytes = min_t(u32, len - off, 0x400); + + writel(5, b->fmi + FMI_CONFIG); + writel((bytes << 8 & 0x7fc00) | 1, b->fmi + FMI_PIO_CONFIG); + writel(5, b->fmi + FMI_CONTROL); + ret = readl_poll_timeout(b->fmi + FMI_DMA_STATUS, v, v & 0x18, 0, 100000); + if (ret) + return ret; + for (i = 0; i < bytes; i += 4) + writel(get_unaligned_le32(buf + off + i), b->fmi + FMI_DATA); + ret = ro_wait(ro, b->fmi, FMI_STATUS, 2, 2); + if (ret) + return ret; + off += bytes; + } + return 0; +} + +static int ro_program_status(u8 st) +{ + if (!(st & BIT(6)) || (st & (BIT(0) | BIT(4)))) + return -EIO; + return 0; +} + +/* One 64 GB PPN 1.3 page program; caller holds ro->lock. */ +static int ro_lane_program(struct ro *ro, unsigned int lane, u32 block, u32 page, + const u8 *expected) +{ + 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, on, if_ctrl; + u8 st = 0; + int ret; + + if (ro->failed) + return -EIO; + if (g != &ro_geoms[1] || lane >= g->lanes) + return -EINVAL; + ce = idx / g->caus; + cau = idx % g->caus; + /* Protect block 0 and the top 32 bootstrap/VFL/factory blocks (2032..2063). */ + if (!block || block >= g->blocks - 32) + return -EPERM; + if (page >= g->pages || !ro_ce_ok(ro, bus, ce)) + return -EINVAL; + + /* Verify target page is truly erased before putting 0x8a on the bus. */ + ret = ro_lane_page(ro, lane, false, block, page, ro_mtd_raw, &st); + if (ret) + return ret; + if (st != PPN_ST_ERASED || memchr_inv(ro_mtd_raw, 0xff, J2A_RAW)) + return -EEXIST; + + row = page | block << g->page_bits | cau << (g->page_bits + g->block_bits); + ro->program_ok = true; + if (b->mode == RO_SDR) { + ro_sdr_reset(b); + writel(1u << ce, b->fmc + FMC_CE_CTRL); + ro_sdr_clear(b); + ret = ro_cmd_addr(ro, b, 0x8a, row, g->row_bytes); + if (!ret) + ret = ro_sdr_transmit(ro, b, expected, J2A_RAW); + if (!ret) + ret = ro_cmd1(ro, b, 0x17); + if (!ret) + ret = ro_cmd1(ro, b, 0x79); + if (!ret) + ret = ro_sdr_poll(ro, b, &st); + if (!ret) + ret = ro_program_status(st); + writel(0, b->fmc + FMC_CE_CTRL); + ro_sdr_reset(b); + } else if (b->mode == RO_DDR) { + on = readl(b->fmc + FMC_ON); + if_ctrl = readl(b->fmc + FMC_IF_CTRL); + if (on != FMC_ON_DDR) { + ret = -EIO; + } else { + writel(1u << ce, b->fmc + FMC_CE_CTRL); + ro_clear_irqs(b); + ret = ro_cmd_addr(ro, b, 0x8a, row, g->row_bytes); + if (!ret) + ret = ro_ddr_data_in(ro, b, expected, J2A_RAW); + if (!ret) + ret = ro_cmd1(ro, b, 0x17); + if (!ret) + ret = ro_cmd1(ro, b, 0x79); + if (!ret) + ret = ro_ddr_poll(ro, b, &st); + if (!ret) + ret = ro_program_status(st); + writel(0, b->fmc + FMC_CE_CTRL); + writel(6, b->fmi + FMI_CONTROL); + writel(on, b->fmc + FMC_ON); + writel(if_ctrl, b->fmc + FMC_IF_CTRL); + } + } else { + ret = -ENODEV; + } + ro->program_ok = false; + if (ret) { + ro->failed = true; + dev_err(ro->dev, "PPN program lane %u blk %u pg %u row 0x%06x: st 0x%02x err %d\n", + lane, block, page, row, st, ret); + return ret; + } + + /* Mandatory byte-for-byte readback verification. */ + ret = ro_lane_page(ro, lane, false, block, page, ro_mtd_raw, &st); + if (ret || st != PPN_ST_GOOD || memcmp(ro_mtd_raw, expected, J2A_RAW)) { + ro->failed = true; + ret = ret ? ret : -EIO; + dev_err(ro->dev, "PPN readback lane %u blk %u pg %u: st 0x%02x err %d\n", + lane, block, page, st, ret); + return ret; + } + return 0; +} + +static int ro_ppn_program(void *priv, unsigned int bus, unsigned int cau, + unsigned int block, unsigned int page, + const u8 *data, const u8 *oob) +{ + struct ro *ro = priv; + u8 *expected; + int ret; + + if (!ro->geom || ro->geom != &ro_geoms[1] || bus > 1 || + cau >= ro->geom->lanes / 2 || !data || !oob) + return -EINVAL; + expected = kmalloc(J2A_RAW, GFP_KERNEL); + if (!expected) + return -ENOMEM; + ro_join(expected, data, oob); + mutex_lock(&ro->lock); + ret = ro_lane_program(ro, bus + 2 * cau, block, page, expected); + mutex_unlock(&ro->lock); + kfree(expected); + return ret; +} + +/* One 64 GB PPN 1.3 block erase; caller holds ro->lock. */ +static int ro_lane_erase(struct ro *ro, unsigned int lane, u32 block) +{ + const struct ro_geom *g = ro->geom; + unsigned int bus = lane & 1, idx = lane >> 1; + unsigned int ce, cau, page; + struct ro_bus *b = &ro->bus[bus]; + u32 row, on, if_ctrl; + u8 queued = 0, completed = 0, st = 0; + int ret; + + if (ro->failed) + return -EIO; + if (g != &ro_geoms[1] || lane >= g->lanes) + return -EINVAL; + ce = idx / g->caus; + cau = idx % g->caus; + /* Protect block 0 and the top 32 bootstrap/VFL/factory blocks (2032..2063). */ + if (!block || block >= g->blocks - 32) + return -EPERM; + if (!ro_ce_ok(ro, bus, ce)) + return -EINVAL; + + row = block << g->page_bits | cau << (g->page_bits + g->block_bits); + ro->erase_ok = true; + if (b->mode == RO_SDR) { + ro_sdr_reset(b); + writel(1u << ce, b->fmc + FMC_CE_CTRL); + ro_sdr_clear(b); + ret = ro_cmd_addr_cmd(ro, b, 0x6a, 0xd7, row, g->row_bytes); + if (!ret) + ret = ro_cmd1(ro, b, 0x79); + if (!ret) + ret = ro_sdr_poll(ro, b, &queued); + if (!ret) + ret = ro_sdr_status(ro, b, &completed); + if (!ret) + ret = ro_cmd1(ro, b, 0x77); + writel(0, b->fmc + FMC_CE_CTRL); + ro_sdr_reset(b); + } else if (b->mode == RO_DDR) { + on = readl(b->fmc + FMC_ON); + if_ctrl = readl(b->fmc + FMC_IF_CTRL); + if (on != FMC_ON_DDR) { + ret = -EIO; + } else { + writel(1u << ce, b->fmc + FMC_CE_CTRL); + ro_clear_irqs(b); + ret = ro_cmd_addr_cmd(ro, b, 0x6a, 0xd7, row, g->row_bytes); + if (!ret) + ret = ro_cmd1(ro, b, 0x79); + if (!ret) + ret = ro_ddr_poll(ro, b, &queued); + if (!ret) + ret = ro_ddr_status(ro, b, &completed); + if (!ret) + ret = ro_cmd1(ro, b, 0x77); + writel(0, b->fmc + FMC_CE_CTRL); + writel(6, b->fmi + FMI_CONTROL); + writel(on, b->fmc + FMC_ON); + writel(if_ctrl, b->fmc + FMC_IF_CTRL); + } + } else { + ret = -ENODEV; + } + ro->erase_ok = false; + /* + * CMD 79 returns NAND mutation/queue status: on PPN 1.3 (64 GB j2a), + * erase queue status returns 0x64 (BIT(6) ready, BIT(0)|BIT(4) clear), + * followed by 0x77 0x7d completion status 0x40 (PPN_ST_GOOD). + */ + if (!ret && (ro_program_status(queued) || completed != PPN_ST_GOOD)) + ret = -EIO; + if (ret) { + ro->failed = true; + dev_err(ro->dev, "PPN erase lane %u blk %u row 0x%06x: q 0x%02x c 0x%02x err %d\n", + lane, block, row, queued, completed, ret); + return ret; + } + + /* Verify every page of the erased block reads back erased (0x49 + all 0xff). */ + for (page = 0; page < g->pages; page++) { + ret = ro_lane_page(ro, lane, false, block, page, ro_mtd_raw, &st); + if (ret || st != PPN_ST_ERASED || memchr_inv(ro_mtd_raw, 0xff, J2A_RAW)) { + ro->failed = true; + ret = ret ? ret : -EIO; + dev_err(ro->dev, "PPN erase readback lane %u blk %u pg %u: st 0x%02x err %d\n", + lane, block, page, st, ret); + return ret; + } + } + return 0; +} + +static int ro_ppn_erase(void *priv, unsigned int bus, unsigned int cau, + unsigned int block) +{ + struct ro *ro = priv; + int ret; + + if (!ro->geom || ro->geom != &ro_geoms[1] || bus > 1 || + cau >= ro->geom->lanes / 2) + return -EINVAL; + mutex_lock(&ro->lock); + ret = ro_lane_erase(ro, bus + 2 * cau, block); + mutex_unlock(&ro->lock); + return ret; +} +#endif + 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; apple_ppn_access_init(&ro_ppn_access); - ro_ppn_ops.read_page = ro_ppn_read; /* and nothing else: the disk stays read-only */ + ro_ppn_ops.read_page = ro_ppn_read; + ro_ppn_ops.program_page = NULL; + ro_ppn_ops.erase_block = NULL; +#if IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE) + if (ro->geom == &ro_geoms[1]) { + ro_ppn_ops.program_page = ro_ppn_program; + ro_ppn_ops.erase_block = ro_ppn_erase; + } +#endif pdev = platform_device_register_data(ro->dev, "apple-sftl", PLATFORM_DEVID_AUTO, &provider, sizeof(provider)); if (IS_ERR(pdev)) return PTR_ERR(pdev); - dev_info(ro->dev, "read-only PPN provider registered for the SFTL\n"); + dev_info(ro->dev, "%s PPN provider registered for the SFTL\n", + ro_ppn_ops.program_page ? + (ro_ppn_ops.erase_block ? "fenced 64 GB write+erase" : "fenced 64 GB write-capable") : + "read-only"); return 0; } #else diff --git a/drivers/mtd/Kconfig b/drivers/mtd/Kconfig index 4db4ec217d9ef7..42f1a683e55fb2 100644 --- a/drivers/mtd/Kconfig +++ b/drivers/mtd/Kconfig @@ -230,6 +230,17 @@ config MTD_APPLE_SFTL read-only as partitions of a second disk, applesftlNlwvm, with their LwVM number, PARTUUID and name. +config MTD_APPLE_SFTL_J2A_WRITE + bool "Fenced writes to iPad 3 64 GB PPN 1.3 Linux LwVM partitions" + depends on MTD_APPLE_SFTL + default n + help + Allow the SFTL writer on the iPad 3 (j2a) 64 GB PPN 1.3 (8 KiB, + lpp=2) geometry. Block writes are strictly fenced to validated + contiguous Linux or XBOOTLDR LwVM partition chunks; iOS System and + Data partitions, free chunks and the LwVM header remain read-only. + Raw MTD writes stay disabled. Say N for a read-only kernel. + config MTD_APPLE_SFTL_KUNIT_TEST tristate "KUnit tests for Apple VFL/SwissPPNFTL" if !KUNIT_ALL_TESTS depends on KUNIT && MTD_APPLE_SFTL diff --git a/drivers/mtd/apple-sftl-account-test.c b/drivers/mtd/apple-sftl-account-test.c index bba306251e6b7e..85ed3a14ea65d0 100644 --- a/drivers/mtd/apple-sftl-account-test.c +++ b/drivers/mtd/apple-sftl-account-test.c @@ -265,6 +265,80 @@ static void account_native_free_allocation(struct kunit *test) KUNIT_EXPECT_EQ(test, plan.landing, 7); } +static void account_64gb_8k_lpp2(struct kunit *test) +{ + struct apple_sftl_layout layout; + struct apple_sftl_extent runs[] = { + { 0, 2, 100 * APPLE_SFTL_STRIDE }, + { 2, APPLE_SFTL_LOGICAL_PAGES - 3, APPLE_SFTL_NO_ADDRESS }, + { APPLE_SFTL_LOGICAL_PAGES - 1, 1, 2056 * APPLE_SFTL_STRIDE + 1 }, + }; + struct apple_sftl_snapshot map = { + .runs = runs, .nr_runs = ARRAY_SIZE(runs), + .subblocks = APPLE_SFTL_64GB_8K_SUBBLOCKS, .lpp = 2, + }; + struct apple_sftl_checkpoint image = {}; + struct apple_sftl_allocation plan = {}; + u8 footer_2[APPLE_SFTL_META] = { 0x1c, 4 }; /* 2 chunks (1 x 8 KiB page) */ + u8 *stats, *sb0, *sb100, *sb2056, *reads; + u32 blocks[] = { 802, 830 }; + u32 i; + + KUNIT_ASSERT_TRUE(test, apple_sftl_layout_for(APPLE_SFTL_64GB_8K_SUBBLOCKS, 2, &layout)); + KUNIT_EXPECT_EQ(test, layout.lpp, (u8)2); + KUNIT_EXPECT_EQ(test, layout.stats_start, 2U); + KUNIT_EXPECT_EQ(test, layout.sb_start, 4U); + KUNIT_EXPECT_EQ(test, layout.user_seq_start, 10U); + KUNIT_EXPECT_EQ(test, layout.read_count_start, 20U); + KUNIT_EXPECT_EQ(test, layout.opaque_records, APPLE_SFTL_64GB_8K_ACCOUNT_RECORDS); + + /* 24 opaque + 32 map = 56 records (matches measured live 64 GB iPad3,3). */ + image.count = APPLE_SFTL_64GB_8K_ACCOUNT_RECORDS + 32; + image.records = kunit_kcalloc(test, image.count, sizeof(*image.records), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, image.records); + apple_sftl_test_native_prefix_64gb_8k(image.records); + for (i = APPLE_SFTL_64GB_8K_ACCOUNT_RECORDS; i < image.count; i++) { + image.records[i].meta[0] = 0x1f; + image.records[i].meta[1] = 4; + put_unaligned_le32(U32_MAX, image.records[i].meta + 12); + } + stats = image.records[2].data; + sb0 = image.records[4].data; + sb100 = sb0 + 100 * 8; + sb2056 = image.records[8].data + 8 * 8; /* 2056 - 2048 = 8 */ + + /* Verify valid LBA recomputation across 2057 subblocks. */ + KUNIT_ASSERT_EQ(test, apple_sftl_account_map(&image, &map), 0); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(sb100 + 2), 2); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(sb2056 + 2), 1); + KUNIT_EXPECT_EQ(test, get_unaligned_le64(stats + TEST_VALID_STAT_OFFSET), 3ULL); + + /* Verify 2-chunk BTOC footer (lpp=2) retire and release. */ + sb100[0] = 3; + sb100[1] = 4; /* chunks=2 << 1 */ + put_unaligned_le16(0, sb100 + 2); + put_unaligned_le16(71, sb100 + 6); + reads = image.records[20].data + 100 * 4; + put_unaligned_le32(42, reads); + put_unaligned_le16(2, footer_2 + 12); + put_unaligned_le16(0xffff, footer_2 + 14); + KUNIT_ASSERT_EQ(test, apple_sftl_account_retire(&image, 100, footer_2), 0); + KUNIT_EXPECT_EQ(test, sb100[0], (u8)9); + KUNIT_EXPECT_EQ(test, get_unaligned_le32(reads), 0U); + KUNIT_ASSERT_EQ(test, apple_sftl_account_release(&image, 100, APPLE_SFTL_ERASED_DONOR), 0); + KUNIT_EXPECT_EQ(test, sb100[0], (u8)1); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(sb100 + 6), 72); + + /* Verify context pair and free-block allocation on 24-record prefix. */ + image.records[4 + 802 / 512].data[(802 % 512) * 8] = 7; + image.records[4 + 830 / 512].data[(830 % 512) * 8] = 7; + map.opaque = image.records; + map.nr_opaque = APPLE_SFTL_64GB_8K_ACCOUNT_RECORDS; + KUNIT_EXPECT_TRUE(test, apple_sftl_account_context_pair(&map, blocks)); + KUNIT_ASSERT_EQ(test, apple_sftl_account_plan_allocation(&map, &plan), 0); + KUNIT_EXPECT_EQ(test, plan.landing, 100); +} + static struct kunit_case account_cases[] = { KUNIT_CASE(account_recompute_valid_lbas), KUNIT_CASE(account_invalid_layout_or_map), @@ -272,6 +346,7 @@ static struct kunit_case account_cases[] = { KUNIT_CASE(account_context_and_retired_donor), KUNIT_CASE(account_native_free_allocation), KUNIT_CASE(account_saved_counts_before_refresh), + KUNIT_CASE(account_64gb_8k_lpp2), {} }; diff --git a/drivers/mtd/apple-sftl-account.c b/drivers/mtd/apple-sftl-account.c index d572b16e695e5e..db7f28fc46a025 100644 --- a/drivers/mtd/apple-sftl-account.c +++ b/drivers/mtd/apple-sftl-account.c @@ -14,10 +14,6 @@ #include "apple-sftl-account.h" #include "apple-sftl-reclaim.h" -#define SFTL_STATS_START 4 /* 64 GB writer accounting: 16 KiB pages */ -#define SFTL_SB_START 8 -#define SFTL_USER_SEQ_START 16 -#define SFTL_READ_COUNT_START 28 #define SFTL_SB_ENTRY_BYTES 8 #define SFTL_SB_PER_RECORD (APPLE_SFTL_CHUNK / SFTL_SB_ENTRY_BYTES) #define SFTL_MAX_WEAR 0xffff @@ -28,6 +24,93 @@ static u32 sftl_section_records(u32 subblocks, u32 per_record, u32 lpp) return round_up(DIV_ROUND_UP(subblocks, per_record), lpp); } +bool apple_sftl_layout_for(u32 subblocks, u8 lpp, struct apple_sftl_layout *out) +{ + struct apple_sftl_layout layout = {}; + u32 sb_records, user_records, read_records; + + if (!out) + return false; + if (subblocks == APPLE_SFTL_SUBBLOCKS && (!lpp || lpp == 4)) { + layout.subblocks = subblocks; + layout.lpp = 4; + layout.stride = APPLE_SFTL_STRIDE; + layout.lane_slots = 512; + layout.logical_pages = APPLE_SFTL_LOGICAL_PAGES; + layout.unmapped_sentinel = 0x1ffffffU; + layout.map_batch = APPLE_SFTL_NATIVE_MAP_BATCH; + layout.map_min = APPLE_SFTL_NATIVE_MAP_BATCH; + layout.footer_max = 32; + } else if (subblocks == APPLE_SFTL_64GB_8K_SUBBLOCKS && (!lpp || lpp == 2)) { + layout.subblocks = subblocks; + layout.lpp = 2; + layout.stride = APPLE_SFTL_STRIDE; + layout.lane_slots = 512; + layout.logical_pages = APPLE_SFTL_LOGICAL_PAGES; + layout.unmapped_sentinel = 0x1ffffffU; + layout.map_batch = APPLE_SFTL_64GB_8K_MAP_BATCH; + layout.map_min = APPLE_SFTL_64GB_8K_MAP_MIN; + layout.footer_max = 32; + } else { + return false; + } + sb_records = sftl_section_records(subblocks, SFTL_SB_PER_RECORD, layout.lpp); + user_records = sftl_section_records(subblocks, APPLE_SFTL_CHUNK / 16, layout.lpp); + read_records = sftl_section_records(subblocks, APPLE_SFTL_CHUNK / 4, layout.lpp); + layout.provider_limit = layout.subblocks * layout.stride; + layout.stats_start = layout.lpp; + layout.sb_start = 2U * layout.lpp; + layout.user_seq_start = layout.sb_start + sb_records; + layout.read_count_start = layout.user_seq_start + user_records; + layout.opaque_records = layout.read_count_start + read_records; + *out = layout; + return true; +} +EXPORT_SYMBOL_GPL(apple_sftl_layout_for); + +bool apple_sftl_records_layout(const struct apple_sftl_context_record *records, + u32 count, struct apple_sftl_layout *out) +{ + struct apple_sftl_layout layout; + u32 tail; + + if (!records || !out) + return false; + if (count >= 4 && records[3].meta[1] == 1) { + tail = get_unaligned_le32(records[3].data + APPLE_SFTL_CHUNK - 4); + if (apple_sftl_layout_for(tail, 4, &layout)) { + *out = layout; + return true; + } + } + if (count >= 2 && records[1].meta[1] == 1 && (count < 3 || records[2].meta[1] != 1)) { + tail = get_unaligned_le32(records[1].data + APPLE_SFTL_CHUNK - 4); + if (apple_sftl_layout_for(tail, 2, &layout)) { + *out = layout; + return true; + } + } + return false; +} +EXPORT_SYMBOL_GPL(apple_sftl_records_layout); + +bool apple_sftl_snapshot_layout(const struct apple_sftl_snapshot *snapshot, + struct apple_sftl_layout *out) +{ + if (!snapshot || !out) + return false; + if (snapshot->subblocks || snapshot->lpp) { + u32 subblocks = snapshot->subblocks ? snapshot->subblocks : APPLE_SFTL_SUBBLOCKS; + + return apple_sftl_layout_for(subblocks, snapshot->lpp, out); + } + if (snapshot->opaque && apple_sftl_records_layout(snapshot->opaque, + snapshot->nr_opaque, out)) + return true; + return apple_sftl_layout_for(APPLE_SFTL_SUBBLOCKS, 4, out); +} +EXPORT_SYMBOL_GPL(apple_sftl_snapshot_layout); + /* * The fixed native records are, in order: base (one page), statistics (one * page), block table, closed-block user sequences and read counts, each sized @@ -110,22 +193,27 @@ void apple_sftl_summarize_user_seq(struct apple_sftl_user_seq *out, const u8 *ru } EXPORT_SYMBOL_GPL(apple_sftl_summarize_user_seq); -static bool sftl_native_prefix(const struct apple_sftl_context_record *records, u32 count) +static bool sftl_native_prefix_layout(const struct apple_sftl_context_record *records, + u32 count, struct apple_sftl_layout *out) { + struct apple_sftl_layout layout; u32 i; - if (!records || count < APPLE_SFTL_ACCOUNT_RECORDS || - get_unaligned_le32(records[3].data + APPLE_SFTL_CHUNK - 4) != APPLE_SFTL_SUBBLOCKS) + if (!apple_sftl_records_layout(records, count, &layout) || + count < layout.opaque_records) return false; - for (i = 0; i < APPLE_SFTL_ACCOUNT_RECORDS; i++) - if (!apple_sftl_native_context_tag(i, records[i].meta)) + for (i = 0; i < layout.opaque_records; i++) + if (!apple_sftl_native_context_tag_lpp(layout.subblocks, layout.lpp, + i, records[i].meta)) return false; + *out = layout; return true; } -static u8 *sftl_sb_entry(struct apple_sftl_context_record *records, u32 block) +static u8 *sftl_sb_entry(struct apple_sftl_context_record *records, + const struct apple_sftl_layout *layout, u32 block) { - u32 record = SFTL_SB_START + block / SFTL_SB_PER_RECORD; + u32 record = layout->sb_start + block / SFTL_SB_PER_RECORD; u32 offset = block % SFTL_SB_PER_RECORD * SFTL_SB_ENTRY_BYTES; return records[record].data + offset; @@ -166,16 +254,20 @@ int apple_sftl_account_plan_allocation(const struct apple_sftl_snapshot *runtime struct sftl_free_block pool[APPLE_SFTL_CLEAN_POOL]; struct sftl_free_block clean = { U32_MAX, U32_MAX }, least = clean; struct apple_sftl_allocation result = {}; + struct apple_sftl_layout layout; u32 i, nr_clean = 0; bool erased = false; - if (!plan || !runtime || runtime->nr_opaque != APPLE_SFTL_ACCOUNT_RECORDS || - !sftl_native_prefix(runtime->opaque, runtime->nr_opaque)) + if (!plan || !runtime || + !sftl_native_prefix_layout(runtime->opaque, runtime->nr_opaque, &layout) || + runtime->nr_opaque != layout.opaque_records || + (runtime->subblocks && runtime->subblocks != layout.subblocks) || + (runtime->lpp && runtime->lpp != layout.lpp)) return -EINVAL; for (i = 0; i < ARRAY_SIZE(pool); i++) pool[i] = (struct sftl_free_block) { U32_MAX, SFTL_MAX_WEAR }; - for (i = 0; i < APPLE_SFTL_SUBBLOCKS; i++) { - const u8 *entry = sftl_sb_entry(runtime->opaque, i); + for (i = 0; i < layout.subblocks; i++) { + const u8 *entry = sftl_sb_entry(runtime->opaque, &layout, i); u32 wear = get_unaligned_le16(entry + 6); if (entry[0] != 1 && entry[0] != 2 && entry[0] != 9) @@ -202,7 +294,7 @@ int apple_sftl_account_plan_allocation(const struct apple_sftl_snapshot *runtime erased |= pool[i].block == result.landing; nr_clean++; } - if (!erased && sftl_sb_entry(runtime->opaque, result.landing)[0] != 1) { + if (!erased && sftl_sb_entry(runtime->opaque, &layout, result.landing)[0] != 1) { /* The native halfword wraps. Leave that policy unqualified. */ if (least.wear == SFTL_MAX_WEAR) return -EOVERFLOW; @@ -214,32 +306,40 @@ int apple_sftl_account_plan_allocation(const struct apple_sftl_snapshot *runtime EXPORT_SYMBOL_GPL(apple_sftl_account_plan_allocation); static bool sftl_account_context_pair(struct apple_sftl_context_record *records, u32 count, - const u32 blocks[2]) + const u32 blocks[2], struct apple_sftl_layout *out) { + struct apple_sftl_layout layout; u32 i; - if (!blocks || blocks[0] == blocks[1] || !sftl_native_prefix(records, count)) + if (!blocks || blocks[0] == blocks[1] || + !sftl_native_prefix_layout(records, count, &layout)) return false; for (i = 0; i < 2; i++) { const u8 *entry; - if (!blocks[i] || blocks[i] >= APPLE_SFTL_SUBBLOCKS) + if (!blocks[i] || blocks[i] >= layout.subblocks) return false; - entry = sftl_sb_entry(records, blocks[i]); + entry = sftl_sb_entry(records, &layout, blocks[i]); /* Recovery assigns class 7 to context (0x1f) at 0x8108d762. * The context allocator independently assigns it at 0x810937a4. */ if (entry[0] != 7 || get_unaligned_le16(entry + 2)) return false; } + if (out) + *out = layout; return true; } bool apple_sftl_account_context_pair(const struct apple_sftl_snapshot *snapshot, const u32 blocks[2]) { - return snapshot && snapshot->nr_opaque == APPLE_SFTL_ACCOUNT_RECORDS && - sftl_account_context_pair(snapshot->opaque, snapshot->nr_opaque, blocks); + struct apple_sftl_layout layout; + + return snapshot && + sftl_account_context_pair(snapshot->opaque, snapshot->nr_opaque, + blocks, &layout) && + snapshot->nr_opaque == layout.opaque_records; } EXPORT_SYMBOL_GPL(apple_sftl_account_context_pair); @@ -251,38 +351,43 @@ bool apple_sftl_account_gc_image(const struct apple_sftl_snapshot *before, const struct apple_sftl_checkpoint *image, const u32 blocks[2], u32 donor, u32 landing, u32 live, u16 footer_chunks) { + struct apple_sftl_layout layout, img_layout; u32 block, old, i, seen = 0, reads_per_record = APPLE_SFTL_CHUNK / sizeof(u32); const u8 *old_source, *source, *destination, *reads; - if (!sftl_native_prefix(before->opaque, before->nr_opaque) || - image->count <= APPLE_SFTL_ACCOUNT_RECORDS || - !sftl_account_context_pair(image->records, image->count, blocks) || - !donor || donor >= APPLE_SFTL_SUBBLOCKS || !landing || landing >= APPLE_SFTL_SUBBLOCKS || - !live || footer_chunks < 4 || footer_chunks > 32 || footer_chunks % 4) + if (!sftl_native_prefix_layout(before->opaque, before->nr_opaque, &layout) || + before->nr_opaque != layout.opaque_records || + !sftl_account_context_pair(image->records, image->count, blocks, &img_layout) || + img_layout.subblocks != layout.subblocks || img_layout.lpp != layout.lpp || + image->count <= layout.opaque_records || + !donor || donor >= layout.subblocks || !landing || landing >= layout.subblocks || + !live || footer_chunks < layout.lpp || footer_chunks > layout.footer_max || + footer_chunks % layout.lpp) return false; /* This bounded path retires one previously occupied context block. * The save names freshly allocated sites and still serializes the old * role/wear before any retirement, including when the old site is full. */ old = get_unaligned_le32(before->opaque[0].data + 4); - if (!old || old >= APPLE_SFTL_SUBBLOCKS || old == blocks[0] || old == blocks[1] || - sftl_sb_entry(before->opaque, old)[0] != 7 || - get_unaligned_le16(sftl_sb_entry(before->opaque, old) + 6) == SFTL_MAX_WEAR || - memcmp(sftl_sb_entry(before->opaque, old), sftl_sb_entry(image->records, old), 8)) + if (!old || old >= layout.subblocks || old == blocks[0] || old == blocks[1] || + sftl_sb_entry(before->opaque, &layout, old)[0] != 7 || + get_unaligned_le16(sftl_sb_entry(before->opaque, &layout, old) + 6) == SFTL_MAX_WEAR || + memcmp(sftl_sb_entry(before->opaque, &layout, old), + sftl_sb_entry(image->records, &layout, old), 8)) return false; for (i = 0; i < 2; i++) { - const u8 *prior = sftl_sb_entry(before->opaque, blocks[i]); - const u8 *next = sftl_sb_entry(image->records, blocks[i]); + const u8 *prior = sftl_sb_entry(before->opaque, &layout, blocks[i]); + const u8 *next = sftl_sb_entry(image->records, &layout, blocks[i]); if (prior[0] != 1 || get_unaligned_le16(prior + 2) || (i == 1 && get_unaligned_le16(prior + 6) == SFTL_MAX_WEAR) || get_unaligned_le16(prior + 6) != get_unaligned_le16(next + 6)) return false; } - old_source = sftl_sb_entry(before->opaque, donor); - source = sftl_sb_entry(image->records, donor); - destination = sftl_sb_entry(image->records, landing); - reads = image->records[SFTL_READ_COUNT_START + donor / reads_per_record].data + + old_source = sftl_sb_entry(before->opaque, &layout, donor); + source = sftl_sb_entry(image->records, &layout, donor); + destination = sftl_sb_entry(image->records, &layout, landing); + reads = image->records[layout.read_count_start + donor / reads_per_record].data + (donor % reads_per_record) * sizeof(u32); if (old_source[0] != 3 || source[0] != 9 || source[1] != old_source[1] || get_unaligned_le16(source + 6) == SFTL_MAX_WEAR || @@ -291,8 +396,8 @@ bool apple_sftl_account_gc_image(const struct apple_sftl_snapshot *before, get_unaligned_le16(source + 2) || get_unaligned_le32(reads) || destination[0] != 4 || destination[1] || get_unaligned_le16(destination + 2) != live) return false; - for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) { - const u8 *entry = sftl_sb_entry(image->records, block); + for (block = 0; block < layout.subblocks; block++) { + const u8 *entry = sftl_sb_entry(image->records, &layout, block); if (entry[0] != 4) continue; @@ -306,13 +411,14 @@ bool apple_sftl_account_gc_image(const struct apple_sftl_snapshot *before, int apple_sftl_account_user_seq(struct apple_sftl_snapshot *snapshot, const struct apple_sftl_user_seq *updates) { + struct apple_sftl_layout layout; u32 block, per_record = APPLE_SFTL_CHUNK / sizeof(*updates); - if (snapshot->nr_opaque != APPLE_SFTL_ACCOUNT_RECORDS || - !sftl_native_prefix(snapshot->opaque, snapshot->nr_opaque)) + if (!sftl_native_prefix_layout(snapshot->opaque, snapshot->nr_opaque, &layout) || + snapshot->nr_opaque != layout.opaque_records) return -EBADMSG; - for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) { - u32 record = SFTL_USER_SEQ_START + block / per_record; + for (block = 0; block < layout.subblocks; block++) { + u32 record = layout.user_seq_start + block / per_record; u32 offset = block % per_record * sizeof(*updates); /* A native close always has a nonzero sample count. */ @@ -323,22 +429,27 @@ int apple_sftl_account_user_seq(struct apple_sftl_snapshot *snapshot, return 0; } -static int sftl_account_layout(const struct apple_sftl_checkpoint *image) +static int sftl_account_layout(const struct apple_sftl_checkpoint *image, + struct apple_sftl_layout *out) { + struct apple_sftl_layout layout; u32 i; - if (image->count <= APPLE_SFTL_ACCOUNT_RECORDS || image->count % 4 || - !sftl_native_prefix(image->records, image->count)) + if (!sftl_native_prefix_layout(image->records, image->count, &layout) || + image->count <= layout.opaque_records || image->count % layout.lpp) return -EOPNOTSUPP; - for (i = APPLE_SFTL_ACCOUNT_RECORDS; i < image->count; i++) - if (!apple_sftl_native_context_tag(i, image->records[i].meta)) + for (i = layout.opaque_records; i < image->count; i++) + if (!apple_sftl_native_context_tag_lpp(layout.subblocks, layout.lpp, + i, image->records[i].meta)) return -EOPNOTSUPP; + *out = layout; return 0; } -static u8 *sftl_stats(struct apple_sftl_checkpoint *image, u8 **provider_stats) +static u8 *sftl_stats(struct apple_sftl_checkpoint *image, + const struct apple_sftl_layout *layout, u8 **provider_stats) { - u8 *payload = image->records[SFTL_STATS_START].data; + u8 *payload = image->records[layout->stats_start].data; u32 i, offset = sizeof(u32); /* The measured stats payload is a word-counted TLV list. IDs 1..43 @@ -372,10 +483,12 @@ static u8 *sftl_stats(struct apple_sftl_checkpoint *image, u8 **provider_stats) int apple_sftl_account_read_stats(struct apple_sftl_checkpoint *image, const struct apple_sftl_gc_io *io, void *priv) { + struct apple_sftl_layout layout; u8 *data[2]; int ret; - if (!sftl_stats(image, data)) + if (!image || !image->records || sftl_account_layout(image, &layout) || + !sftl_stats(image, &layout, data)) return -EBADMSG; ret = io->read_native_stats(priv, 255, data[0], 88); if (ret != 88) @@ -395,24 +508,28 @@ static u8 *sftl_stat(u8 *stats, u32 id) */ int apple_sftl_account_retire(struct apple_sftl_checkpoint *image, u32 block, const u8 *footer_meta) { + struct apple_sftl_layout layout; u32 reads_per_record = APPLE_SFTL_CHUNK / sizeof(u32); u8 *entry, *reads; u16 chunks; int ret; - if (!image || !image->records || !footer_meta || !block || block >= APPLE_SFTL_SUBBLOCKS) + if (!image || !image->records || !footer_meta || !block) return -EINVAL; - ret = sftl_account_layout(image); + ret = sftl_account_layout(image, &layout); if (ret) return ret; + if (block >= layout.subblocks) + return -EINVAL; chunks = get_unaligned_le16(footer_meta + 12); - if (footer_meta[0] != 0x1c || chunks < 4 || chunks > 32 || chunks % 4 || - footer_meta[1] >> 1 != chunks || get_unaligned_le16(footer_meta + 14) != 0xffff) + if (footer_meta[0] != 0x1c || chunks < layout.lpp || chunks > layout.footer_max || + chunks % layout.lpp || footer_meta[1] >> 1 != chunks || + get_unaligned_le16(footer_meta + 14) != 0xffff) return -EOPNOTSUPP; - entry = sftl_sb_entry(image->records, block); + entry = sftl_sb_entry(image->records, &layout, block); if (entry[0] != 3 || entry[1] != footer_meta[1] || get_unaligned_le16(entry + 2)) return -EOPNOTSUPP; - reads = image->records[SFTL_READ_COUNT_START + block / reads_per_record].data + + reads = image->records[layout.read_count_start + block / reads_per_record].data + (block % reads_per_record) * sizeof(u32); /* At 0x81090ade final invalidation only updates the entry and runtime * totals. A save can see the previous cached free and data statistics. @@ -426,17 +543,20 @@ EXPORT_SYMBOL_GPL(apple_sftl_account_retire); int apple_sftl_account_release(struct apple_sftl_checkpoint *image, u32 block, enum apple_sftl_release event) { + struct apple_sftl_layout layout; u8 *entry; u16 wear; int ret; - if (!image || !image->records || !block || block >= APPLE_SFTL_SUBBLOCKS || + if (!image || !image->records || !block || (event != APPLE_SFTL_ERASED_DONOR && event != APPLE_SFTL_RELEASED_CONTEXT)) return -EINVAL; - ret = sftl_account_layout(image); + ret = sftl_account_layout(image, &layout); if (ret) return ret; - entry = sftl_sb_entry(image->records, block); + if (block >= layout.subblocks) + return -EINVAL; + entry = sftl_sb_entry(image->records, &layout, block); if (entry[0] != (event == APPLE_SFTL_ERASED_DONOR ? 9 : 7) || get_unaligned_le16(entry + 2)) return -EOPNOTSUPP; @@ -458,20 +578,21 @@ EXPORT_SYMBOL_GPL(apple_sftl_account_release); int apple_sftl_account_refresh(struct apple_sftl_checkpoint *image, const struct apple_sftl_runtime_stats *runtime) { + struct apple_sftl_layout layout; u8 *stats; int ret; if (!image || !image->records || !runtime) return -EINVAL; - ret = sftl_account_layout(image); + ret = sftl_account_layout(image, &layout); if (ret) return ret; - stats = sftl_stats(image, NULL); + stats = sftl_stats(image, &layout, NULL); if (!stats) return -EOPNOTSUPP; - if (runtime->valid > APPLE_SFTL_LOGICAL_PAGES || + if (runtime->valid > layout.logical_pages || (u64)runtime->free + runtime->data + runtime->context + runtime->reserved > - APPLE_SFTL_SUBBLOCKS) + layout.subblocks) return -EBADMSG; put_unaligned_le64(runtime->valid, sftl_stat(stats, 9)); put_unaligned_le64(runtime->free, sftl_stat(stats, 10)); @@ -484,7 +605,9 @@ int apple_sftl_account_refresh(struct apple_sftl_checkpoint *image, } EXPORT_SYMBOL_GPL(apple_sftl_account_refresh); -static int sftl_account_runs(const struct apple_sftl_snapshot *map, u16 *counts, u32 *total) +static int sftl_account_runs(const struct apple_sftl_snapshot *map, + const struct apple_sftl_layout *layout, + u16 *counts, u32 *total) { u32 cursor = 0, i; @@ -492,73 +615,76 @@ static int sftl_account_runs(const struct apple_sftl_snapshot *map, u16 *counts, const struct apple_sftl_extent *run = &map->runs[i]; u32 length = run->length, addr = run->provider; - if (run->logical != cursor || !length || length > APPLE_SFTL_LOGICAL_PAGES - cursor) + if (run->logical != cursor || !length || length > layout->logical_pages - cursor) return -EBADMSG; cursor += length; if (addr == APPLE_SFTL_NO_ADDRESS) continue; - if (addr >= APPLE_SFTL_SUBBLOCKS * APPLE_SFTL_STRIDE || - length > APPLE_SFTL_SUBBLOCKS * APPLE_SFTL_STRIDE - addr) + if (addr >= layout->provider_limit || + length > layout->provider_limit - addr) return -EBADMSG; *total += length; while (length) { - u32 block = addr / APPLE_SFTL_STRIDE; - u32 n = min(length, APPLE_SFTL_STRIDE - addr % APPLE_SFTL_STRIDE); + u32 block = addr / layout->stride; + u32 n = min(length, layout->stride - addr % layout->stride); - if (counts[block] > APPLE_SFTL_STRIDE - n) + if (counts[block] > layout->stride - n) return -EBADMSG; counts[block] += n; addr += n; length -= n; } } - return cursor == APPLE_SFTL_LOGICAL_PAGES ? 0 : -EBADMSG; + return cursor == layout->logical_pages ? 0 : -EBADMSG; } static int sftl_update_map_counts(struct apple_sftl_checkpoint *image, const struct apple_sftl_snapshot *map, bool preserve_cache) { + struct apple_sftl_layout layout; u32 old_total = 0, total = 0, block; u8 *valid, *stats; u16 *counts; int ret; - if (!image || !image->records || !map || !map->runs || !map->nr_runs || - map->nr_runs > APPLE_SFTL_LOGICAL_PAGES) + if (!image || !image->records || !map || !map->runs || !map->nr_runs) return -EINVAL; - ret = sftl_account_layout(image); + ret = sftl_account_layout(image, &layout); if (ret) return ret; - stats = sftl_stats(image, NULL); + if (map->nr_runs > layout.logical_pages) + return -EINVAL; + stats = sftl_stats(image, &layout, NULL); if (!stats) return -EOPNOTSUPP; valid = sftl_stat(stats, 9); - for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) { - u16 n = get_unaligned_le16(sftl_sb_entry(image->records, block) + 2); + for (block = 0; block < layout.subblocks; block++) { + u16 n = get_unaligned_le16(sftl_sb_entry(image->records, &layout, block) + 2); - if (n > APPLE_SFTL_STRIDE) + if (n > layout.stride) return -EBADMSG; old_total += n; } if (preserve_cache) { u32 id; - if (get_unaligned_le64(valid) > APPLE_SFTL_LOGICAL_PAGES) + if (get_unaligned_le64(valid) > layout.logical_pages) return -EBADMSG; for (id = 10; id <= 13; id++) - if (get_unaligned_le64(sftl_stat(stats, id)) > APPLE_SFTL_SUBBLOCKS) + if (get_unaligned_le64(sftl_stat(stats, id)) > layout.subblocks) return -EBADMSG; } else if (get_unaligned_le64(valid) != old_total) { return -EBADMSG; } - counts = kcalloc(APPLE_SFTL_SUBBLOCKS, sizeof(*counts), GFP_NOIO); + counts = kcalloc(layout.subblocks, sizeof(*counts), GFP_NOIO); if (!counts) return -ENOMEM; - ret = sftl_account_runs(map, counts, &total); + ret = sftl_account_runs(map, &layout, counts, &total); if (!ret) { - for (block = 0; block < APPLE_SFTL_SUBBLOCKS; block++) - put_unaligned_le16(counts[block], sftl_sb_entry(image->records, block) + 2); + for (block = 0; block < layout.subblocks; block++) + put_unaligned_le16(counts[block], + sftl_sb_entry(image->records, &layout, block) + 2); if (!preserve_cache) put_unaligned_le64(total, valid); } diff --git a/drivers/mtd/apple-sftl-account.h b/drivers/mtd/apple-sftl-account.h index c0cdb976f6b932..97ad8b34163888 100644 --- a/drivers/mtd/apple-sftl-account.h +++ b/drivers/mtd/apple-sftl-account.h @@ -5,16 +5,42 @@ #include #define APPLE_SFTL_ACCOUNT_RECORDS 32U +#define APPLE_SFTL_64GB_8K_ACCOUNT_RECORDS 24U + +struct apple_sftl_checkpoint; +struct apple_sftl_context_record; +struct apple_sftl_gc_io; +struct apple_sftl_snapshot; + +struct apple_sftl_layout { + u32 subblocks; + u32 stride; + u32 lane_slots; + u32 logical_pages; + u32 provider_limit; + u32 unmapped_sentinel; + u32 map_batch; + u32 map_min; + u32 footer_max; + u32 stats_start; + u32 sb_start; + u32 user_seq_start; + u32 read_count_start; + u32 opaque_records; + u8 lpp; +}; + +bool apple_sftl_layout_for(u32 subblocks, u8 lpp, struct apple_sftl_layout *out); +bool apple_sftl_snapshot_layout(const struct apple_sftl_snapshot *snapshot, + struct apple_sftl_layout *out); +bool apple_sftl_records_layout(const struct apple_sftl_context_record *records, + u32 count, struct apple_sftl_layout *out); /* Native subtype 5 keeps one little-endian tuple per closed block. */ struct apple_sftl_user_seq { u8 data[16]; }; -struct apple_sftl_checkpoint; -struct apple_sftl_gc_io; -struct apple_sftl_snapshot; - #define APPLE_SFTL_CLEAN_POOL 6 struct apple_sftl_allocation { diff --git a/drivers/mtd/apple-sftl-block.c b/drivers/mtd/apple-sftl-block.c index 11b028b286af16..8cbbfe6bdf69c3 100644 --- a/drivers/mtd/apple-sftl-block.c +++ b/drivers/mtd/apple-sftl-block.c @@ -2,6 +2,7 @@ /* Block frontend for Apple's VFL/SwissPPNFTL logical medium. */ #include #include +#include #include #include #include @@ -28,9 +29,14 @@ struct sftl_disk { struct mutex io_lock; /* serializes requests using buffer */ u8 *buffer; unsigned int id; + unsigned int lpp; bool dead; bool writable; + bool fenced; + struct apple_sftl_lwvm_fence fence; struct sftl_view *view; + struct dentry *debugfs_dir; + struct device *dev; }; /* @@ -105,7 +111,7 @@ static int sftl_read_request(struct sftl_disk *sftl, struct request *request, return 0; } -/* Preserve untouched sectors at both ends and collect up to four 4 KiB +/* Preserve untouched sectors at both ends and collect up to lpp 4 KiB * pages per physical program. Request segments can split a logical page. */ static int sftl_write_request(struct sftl_disk *sftl, struct request *request) @@ -117,6 +123,19 @@ static int sftl_write_request(struct sftl_disk *sftl, struct request *request) unsigned int count = 0; int ret; + if (sftl->fenced) { + u64 bytes = blk_rq_bytes(request); + + if (bytes) { + u32 start_lba = position >> 12; + u32 end_lba = (position + bytes - 1) >> 12; + u32 lba; + + for (lba = start_lba; lba <= end_lba; lba++) + if (!apple_sftl_lwvm_lba_writable(&sftl->fence, lba)) + return -EROFS; + } + } if (request->cmd_flags & REQ_PREFLUSH) { ret = apple_sftl_flush(sftl->ftl); if (ret) @@ -133,7 +152,7 @@ static int sftl_write_request(struct sftl_disk *sftl, struct request *request) void *mapped; if (logical != cached) { - if (count == 4) { + if (count == sftl->lpp) { ret = apple_sftl_write(sftl->ftl, first, sftl->buffer, count); if (ret) @@ -379,6 +398,244 @@ static void sftl_remove_view(struct sftl_disk *sftl) sftl->view = NULL; } +static int sftl_init_fence(struct platform_device *pdev, struct sftl_disk *sftl, u64 media) +{ + unsigned int shift = apple_sftl_lwvm_chunk_shift(media); + unsigned int noio_flags; + u8 *primary, *backup; + u32 copy_lba; + int ret; + + if (!shift || shift <= 12) + return -EINVAL; + copy_lba = (1U << (shift - 12)) - 1; + primary = kmalloc(SFTL_LOGICAL_BYTES, GFP_KERNEL); + backup = kmalloc(SFTL_LOGICAL_BYTES, GFP_KERNEL); + if (!primary || !backup) { + kfree(primary); + kfree(backup); + return -ENOMEM; + } + mutex_lock(&sftl->io_lock); + noio_flags = memalloc_noio_save(); + ret = apple_sftl_read(sftl->ftl, 0, primary); + if (!ret) + ret = apple_sftl_read(sftl->ftl, copy_lba, backup); + memalloc_noio_restore(noio_flags); + mutex_unlock(&sftl->io_lock); + if (!ret) + ret = apple_sftl_lwvm_build_fence(primary, backup, media, &sftl->fence); + kfree(primary); + kfree(backup); + if (ret) { + dev_warn(&pdev->dev, "LwVM write fence rejected header (%d), read-only\n", ret); + return ret; + } + if (!sftl->fence.writable_count) { + dev_info(&pdev->dev, "no contiguous Linux LwVM partitions found, read-only\n"); + return -EROFS; + } + sftl->fenced = true; + dev_info(&pdev->dev, "LwVM write fence active: %u writable Linux chunks\n", + sftl->fence.writable_count); + return 0; +} + +#if IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE) && IS_ENABLED(CONFIG_DEBUG_FS) +static ssize_t j2a_status_read(struct file *file, char __user *ubuf, + size_t count, loff_t *ppos) +{ + struct sftl_disk *sftl = file->private_data; + struct apple_sftl_info info; + char buf[256]; + int len; + + apple_sftl_get_info(sftl->ftl, &info); + len = scnprintf(buf, sizeof(buf), + "writable=%u fenced=%u writable_chunks=%u disk_pages=%u seq=%llu journal=%llu ckpt=%llu\n", + sftl->writable, sftl->fenced, sftl->fence.writable_count, + info.disk_pages, info.sequence, info.journal_sequence, + info.checkpoint_sequence); + return simple_read_from_buffer(ubuf, count, ppos, buf, len); +} + +static const struct file_operations j2a_status_fops = { + .owner = THIS_MODULE, + .open = simple_open, + .read = j2a_status_read, + .llseek = default_llseek, +}; + +static ssize_t j2a_lwvm_install_write(struct file *file, const char __user *ubuf, + size_t count, loff_t *ppos) +{ + struct sftl_disk *sftl = file->private_data; + struct apple_sftl_info info; + struct apple_sftl_lwvm *old_lwvm = NULL, *new_lwvm = NULL; + struct apple_sftl_lwvm_fence new_fence; + u8 *old_pri = NULL, *old_bak = NULL, *new_hdr = NULL, *verify = NULL; + unsigned int shift, noio_flags, k; + u64 media; + u32 copy_lba; + int ret; + + if (*ppos != 0 || count != SFTL_LOGICAL_BYTES) + return -EINVAL; + apple_sftl_get_info(sftl->ftl, &info); + if (info.read_only || !info.fenced_write || + info.sequence != info.journal_sequence) + return -EROFS; + media = (u64)info.disk_pages * SFTL_LOGICAL_BYTES; + shift = apple_sftl_lwvm_chunk_shift(media); + if (!shift || shift <= 12) + return -EINVAL; + copy_lba = (1U << (shift - 12)) - 1; + + old_lwvm = kzalloc(sizeof(*old_lwvm), GFP_KERNEL); + new_lwvm = kzalloc(sizeof(*new_lwvm), GFP_KERNEL); + old_pri = kmalloc(SFTL_LOGICAL_BYTES, GFP_KERNEL); + old_bak = kmalloc(SFTL_LOGICAL_BYTES, GFP_KERNEL); + new_hdr = kmalloc(SFTL_LOGICAL_BYTES, GFP_KERNEL); + verify = kmalloc(SFTL_LOGICAL_BYTES, GFP_KERNEL); + if (!old_lwvm || !new_lwvm || !old_pri || !old_bak || !new_hdr || !verify) { + ret = -ENOMEM; + goto out_free; + } + if (copy_from_user(new_hdr, ubuf, SFTL_LOGICAL_BYTES)) { + ret = -EFAULT; + goto out_free; + } + + mutex_lock(&sftl->io_lock); + noio_flags = memalloc_noio_save(); + if (sftl->dead) { + ret = -ENODEV; + goto out_unlock; + } + ret = apple_sftl_read(sftl->ftl, 0, old_pri); + if (!ret) + ret = apple_sftl_read(sftl->ftl, copy_lba, old_bak); + if (ret) + goto out_unlock; + if (memcmp(old_pri, old_bak, SFTL_LOGICAL_BYTES)) { + ret = -EUCLEAN; + goto out_unlock; + } + ret = apple_sftl_lwvm_decode(old_pri, media, old_lwvm); + if (ret) + goto out_unlock; + ret = apple_sftl_lwvm_build_fence(new_hdr, new_hdr, media, &new_fence); + if (ret) + goto out_unlock; + if (!new_fence.writable_count) { + ret = -EINVAL; + goto out_unlock; + } + ret = apple_sftl_lwvm_decode(new_hdr, media, new_lwvm); + if (ret) + goto out_unlock; + + /* Strictly allow adding Linux p3 to a stock 2-partition iOS table. */ + if (old_lwvm->nr_parts != 2 || new_lwvm->nr_parts != 3) { + ret = -EINVAL; + goto out_unlock; + } + /* p1 System descriptor and every p1 chunk must be unchanged. */ + if (memcmp(old_pri + 0x200, new_hdr + 0x200, 0x80) || + old_lwvm->parts[0].nr_chunks != new_lwvm->parts[0].nr_chunks) { + ret = -EPERM; + goto out_unlock; + } + for (k = 0; k < old_lwvm->parts[0].nr_chunks; k++) { + if (old_lwvm->parts[0].chunk[k] != new_lwvm->parts[0].chunk[k]) { + ret = -EPERM; + goto out_unlock; + } + } + /* p2 Data identity/begin/attr/name unchanged, shrunk to >= 8 GiB, retained chunks unchanged. */ + if (memcmp(old_pri + 0x280, new_hdr + 0x280, 40) || + memcmp(old_pri + 0x280 + 48, new_hdr + 0x280 + 48, 80) || + new_lwvm->parts[1].end > old_lwvm->parts[1].end || + new_lwvm->parts[1].end - new_lwvm->parts[1].begin < (8ULL << 30)) { + ret = -EPERM; + goto out_unlock; + } + for (k = 0; k < new_lwvm->parts[1].nr_chunks; k++) { + if (old_lwvm->parts[1].chunk[k] != new_lwvm->parts[1].chunk[k]) { + ret = -EPERM; + goto out_unlock; + } + } + /* p3 must be contiguous and sit after shrunk p2. */ + if (new_lwvm->parts[2].pieced || + new_lwvm->parts[2].begin < new_lwvm->parts[1].end) { + ret = -EINVAL; + goto out_unlock; + } + + /* 1. Write backup LwVM header first and verify readback. */ + ret = apple_sftl_write(sftl->ftl, copy_lba, new_hdr, 1); + if (!ret) + ret = apple_sftl_read(sftl->ftl, copy_lba, verify); + if (!ret && memcmp(new_hdr, verify, SFTL_LOGICAL_BYTES)) + ret = -EIO; + if (ret) { + dev_err(sftl->dev, "LwVM backup write/verify failed: %d\n", ret); + goto out_unlock; + } + dev_info(sftl->dev, "LwVM backup header at LBA %u written and verified\n", copy_lba); + + /* 2. Write primary LwVM header and verify readback. */ + ret = apple_sftl_write(sftl->ftl, 0, new_hdr, 1); + if (!ret) + ret = apple_sftl_read(sftl->ftl, 0, verify); + if (!ret && memcmp(new_hdr, verify, SFTL_LOGICAL_BYTES)) + ret = -EIO; + if (ret) { + dev_err(sftl->dev, "LwVM primary write/verify failed: %d\n", ret); + goto out_unlock; + } + ret = apple_sftl_flush(sftl->ftl); + apple_sftl_get_info(sftl->ftl, &info); + dev_info(sftl->dev, "LwVM primary header at LBA 0 written and verified (seq %llu, %u Linux chunks)\n", + info.sequence, new_fence.writable_count); + +out_unlock: + memalloc_noio_restore(noio_flags); + mutex_unlock(&sftl->io_lock); +out_free: + kfree(verify); + kfree(new_hdr); + kfree(old_bak); + kfree(old_pri); + kfree(new_lwvm); + kfree(old_lwvm); + return ret ? ret : count; +} + +static const struct file_operations j2a_lwvm_install_fops = { + .owner = THIS_MODULE, + .open = simple_open, + .write = j2a_lwvm_install_write, + .llseek = noop_llseek, +}; + +static void sftl_j2a_debugfs_init(struct sftl_disk *sftl) +{ + sftl->debugfs_dir = debugfs_create_dir("apple_sftl_j2a", NULL); + if (IS_ERR_OR_NULL(sftl->debugfs_dir)) { + sftl->debugfs_dir = NULL; + return; + } + debugfs_create_file("status", 0400, sftl->debugfs_dir, sftl, &j2a_status_fops); + debugfs_create_file("lwvm_install", 0200, sftl->debugfs_dir, sftl, &j2a_lwvm_install_fops); +} +#else +static void sftl_j2a_debugfs_init(struct sftl_disk *sftl) +{ +} +#endif + static int sftl_disk_probe(struct platform_device *pdev) { struct queue_limits limits = { @@ -407,6 +664,7 @@ static int sftl_disk_probe(struct platform_device *pdev) sftl = kzalloc(sizeof(*sftl), GFP_KERNEL); if (!sftl) return -ENOMEM; + sftl->dev = &pdev->dev; mutex_init(&sftl->io_lock); sftl->buffer = kmalloc(APPLE_PPN_PAGE_SIZE, GFP_KERNEL); if (!sftl->buffer) { @@ -421,12 +679,19 @@ static int sftl_disk_probe(struct platform_device *pdev) ret = -EINVAL; goto free_ftl; } + sftl->lpp = info.lpp ? info.lpp : 4; /* Appending continues the journal; it must end where the map does. */ sftl->writable = provider->ops->program_page && !info.read_only && info.sequence == info.journal_sequence; + if (sftl->writable && info.fenced_write) { + u64 media = (u64)info.disk_pages * SFTL_LOGICAL_BYTES; + + if (sftl_init_fence(pdev, sftl, media)) + sftl->writable = false; + } if (info.read_only) dev_warn(&pdev->dev, "incomplete metadata publication, read-only\n"); - else if (provider->ops->program_page && !sftl->writable) + else if (provider->ops->program_page && info.sequence != info.journal_sequence) dev_warn(&pdev->dev, "journal ends before the allocation floor, read-only\n"); if (sftl->writable) limits.features |= BLK_FEAT_FUA; @@ -467,6 +732,8 @@ static int sftl_disk_probe(struct platform_device *pdev) dev_info(&pdev->dev, "%s: %s, %u logical pages, sequence %llu\n", disk->disk_name, sftl->writable ? "read-write" : "read-only", info.disk_pages, info.sequence); + if (provider->ops->program_page && info.fenced_write) + sftl_j2a_debugfs_init(sftl); sftl_add_view(pdev, sftl, &info); return 0; @@ -493,6 +760,7 @@ static void sftl_disk_remove(struct platform_device *pdev) mutex_lock(&sftl->io_lock); sftl->dead = true; mutex_unlock(&sftl->io_lock); + debugfs_remove_recursive(sftl->debugfs_dir); sftl_remove_view(sftl); /* del_gendisk freezes and drains the queue before the provider is freed. */ del_gendisk(sftl->disk); diff --git a/drivers/mtd/apple-sftl-core.c b/drivers/mtd/apple-sftl-core.c index d4877223288dbe..5c27ba2e98c581 100644 --- a/drivers/mtd/apple-sftl-core.c +++ b/drivers/mtd/apple-sftl-core.c @@ -75,6 +75,7 @@ struct sftl_geometry { u32 disk_pages; /* logical pages exported as the disk */ u32 lpages; /* logical pages including the hidden tail */ bool writer; /* append, reclamation and checkpoints are modelled */ + bool fenced_write; /* block writes require a validated Linux LwVM partition fence */ bool summary_sequences; /* a summary may consume one sequence per chunk */ bool degrade_remnants; /* a context block with an unknown base only blocks writes */ bool hidden_tail_type2; /* iOS 9 writes the hidden tail as type 2, flag 1 */ @@ -140,10 +141,34 @@ 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 = APPLE_SFTL_64GB_8K_MAP_BATCH, + .disk_pages = 15625000, .lpages = 15625016, + .writer = IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE), + .fenced_write = true, + .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 { @@ -889,6 +914,9 @@ int apple_sftl_rotate_vfl(struct apple_sftl *sftl, const struct apple_sftl_vfl_o goto out; cold->ppn = sftl->ppn; cold->geo = sftl->geo; + cold->lpp = sftl->lpp; + cold->psize = sftl->psize; + cold->osize = sftl->osize; cold->page = sftl->page; ret = sftl_vfl(cold); if (ret) @@ -1531,9 +1559,12 @@ static int sftl_read_open(struct apple_sftl *sftl, u32 address, unsigned int *ch static int sftl_remember_user_seq(struct apple_sftl *sftl, u32 block, const u8 *footer, u32 runs) { - /* Four base chunks identify the layout validated by sftl_decode_generation. */ - if (sftl->context.count != APPLE_SFTL_ACCOUNT_RECORDS || - sftl_flags(&sftl->context.items[3]) != 1) + struct apple_sftl_layout layout; + + if (!sftl->native_context || + !apple_sftl_layout_for(sftl->geo->subblocks, sftl->lpp, &layout) || + sftl->context.count != layout.opaque_records || + sftl_flags(&sftl->context.items[sftl->lpp - 1]) != 1) return 0; if (!sftl->closed_user_seq) { sftl->closed_user_seq = kvzalloc(SFTL_MAX_SUBBLOCKS * sizeof(*sftl->closed_user_seq), @@ -2101,6 +2132,8 @@ static void sftl_copy_map(struct apple_sftl *sftl, struct apple_sftl_snapshot *s }; } snapshot->nr_runs = sftl->nr_runs; + snapshot->subblocks = sftl->geo->subblocks; + snapshot->lpp = sftl->lpp; snapshot->sequence = sftl->sequence; snapshot->journal_sequence = sftl->journal_sequence; snapshot->checkpoint_sequence = sftl->checkpoint_sequence; @@ -2120,27 +2153,27 @@ static int sftl_verify_closed(struct apple_sftl *sftl, struct apple_sftl_gc_bloc u64 base; int ret; - ret = sftl_read_address(sftl, first + capacity - 4, &chunk); + ret = sftl_read_address(sftl, first + capacity - sftl->lpp, &chunk); if (ret) return ret; memcpy(trailer, sftl->oob, SFTL_META); chunks = get_unaligned_le16(trailer + 12); span = get_unaligned_le32(trailer + 8); base = sftl_sequence(trailer); - if (trailer[0] != 0x1c || chunks < 4 || chunks > SFTL_FOOTER_CHUNKS || - chunks % 4 || trailer[1] >> 1 != chunks || + if (trailer[0] != 0x1c || chunks < sftl->lpp || chunks > SFTL_FOOTER_CHUNKS || + chunks % sftl->lpp || trailer[1] >> 1 != chunks || (sftl->native_context && ((trailer[1] & 1) || sftl->block_flags[victim->block] != trailer[1])) || get_unaligned_le16(trailer + 14) != 0xffff || !base || !span || base + span - 1 >= sftl->checkpoint_start) return -EOPNOTSUPP; - for (i = 0; i < chunks; i += 4) { + for (i = 0; i < chunks; i += sftl->lpp) { ret = sftl_read_address(sftl, first + capacity - chunks + i, &chunk); if (ret) return ret; - for (chunk = 0; chunk < 4; chunk++) + for (chunk = 0; chunk < sftl->lpp; chunk++) if (memcmp(sftl->oob + chunk * SFTL_META, trailer, SFTL_META)) return -EBADMSG; - memcpy(sftl->footer + i * SFTL_CHUNK, sftl->page, APPLE_PPN_PAGE_SIZE); + memcpy(sftl->footer + i * SFTL_CHUNK, sftl->page, sftl->psize); } for (i = 0; i < chunks * SFTL_CHUNK; i += SFTL_META) { const u8 *entry = sftl->footer + i; @@ -2164,14 +2197,14 @@ static int sftl_verify_closed(struct apple_sftl *sftl, struct apple_sftl_gc_bloc u64 seq = base + relative + j; const u8 *meta; - if (!(position % 4)) { + if (!(position % sftl->lpp)) { if (time_after_eq(jiffies, deadline)) return -ETIMEDOUT; ret = sftl_read_address(sftl, first + position, &chunk); if (ret) return ret; } - meta = sftl->oob + (position % 4) * SFTL_META; + meta = sftl->oob + (position % sftl->lpp) * SFTL_META; /* Recognized native classes outside this operation are ineligible. */ if ((meta[0] == 2 && !meta[1]) || (relocate && meta[0] == 1 && meta[1] == 1)) @@ -2199,14 +2232,64 @@ static int sftl_block_blank(struct apple_sftl *sftl, u32 block, unsigned long de int ret; *blank = false; - for (pos = 0; pos < capacity; pos += 4) { + for (pos = 0; pos < capacity; pos += sftl->lpp) { if (time_after_eq(jiffies, deadline)) return -ETIMEDOUT; ret = sftl_read_address(sftl, block * sftl_stride(sftl) + pos, &chunk); if (ret) return ret; - if (!sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE) || - !sftl_erased(sftl->oob, APPLE_PPN_OOB_SIZE)) + if (!sftl_erased(sftl->page, sftl->psize) || + !sftl_erased(sftl->oob, sftl->osize)) + return 0; + } + *blank = true; + return 0; +} + +static bool sftl_block_mapped(struct apple_sftl *sftl, u32 block) +{ + u32 stride = sftl_stride(sftl); + struct rb_node *node; + + for (node = rb_first(&sftl->runs); node; node = rb_next(node)) { + const struct sftl_run *run = rb_entry(node, struct sftl_run, node); + u32 first, last; + + if (run->provider == SFTL_UNMAPPED) + continue; + first = run->provider / stride; + last = (run->provider + run->end - run->start - 1) / stride; + if (block >= first && block <= last) + return true; + } + return false; +} + +static int sftl_blank_ends(struct apple_sftl *sftl, u32 block, bool *blank) +{ + u32 capacity = sftl_slots(sftl, sftl->lanes[block]); + u32 ring = hweight16(sftl->lanes[block]) * sftl->lpp; + u32 pos; + unsigned int chunk; + int ret; + + *blank = false; + if (!ring || capacity < ring) + return 0; + for (pos = 0; pos < ring; pos += sftl->lpp) { + ret = sftl_read_address(sftl, block * sftl_stride(sftl) + pos, &chunk); + if (ret) + return ret; + if (!sftl_erased(sftl->page, sftl->psize) || + !sftl_erased(sftl->oob, sftl->osize)) + return 0; + } + for (pos = capacity - ring; pos < capacity; pos += sftl->lpp) { + ret = sftl_read_address(sftl, block * sftl_stride(sftl) + pos, &chunk); + if (ret) + return ret; + if (!sftl_erased(sftl->page, sftl->psize) || + !sftl_erased(sftl->oob, sftl->osize)) return 0; } *blank = true; @@ -2248,6 +2331,63 @@ static int sftl_erase_verified(struct apple_sftl *sftl, u32 block, unsigned long return ret ? ret : (blank ? 0 : -EBADMSG); } +/* + * On 12H321 native media, blank DIRTY_FREE (role 9) superblocks hold no live + * pages and are erased before reuse, leaving iOS's native CLEAN_FREE pool alone. + */ +static int sftl_reuse_blank_dirty(struct apple_sftl *sftl, u32 *claimed, + unsigned long deadline) +{ + u32 block, best; + bool blank; + int ret; + + if (!sftl->geo->fenced_write || !sftl->native_context || + sftl->vfl_degraded || sftl->context_degraded || + !sftl->ppn->ops->erase_block) + return -ENOSPC; + for (;;) { + best = U32_MAX; + for (block = 1; block < sftl->geo->vfl_first; block++) { + if (sftl->roles[block] != SFTL_DIRTY_FREE || + !sftl->lanes[block] || sftl->wear[block] == 0xffff || + test_bit(block, sftl->used) || + test_bit(block, sftl->reserved) || + test_bit(block, sftl->vfl_reserved) || + test_bit(block, sftl->unknown_lane_flags) || + test_bit(block, sftl->checkpoint_used) || + test_bit(block, sftl->old_closed)) + continue; + if (best != U32_MAX && sftl->wear[block] >= sftl->wear[best]) + continue; + if (sftl_block_mapped(sftl, block)) + continue; + best = block; + } + if (best == U32_MAX) + return -ENOSPC; + __set_bit(best, sftl->used); + ret = sftl_blank_ends(sftl, best, &blank); + if (ret) { + sftl->failed = true; + return ret; + } + if (!blank) + continue; + ret = sftl_erase_verified(sftl, best, deadline); + if (ret) { + sftl->failed = true; + return ret; + } + sftl->roles[best] = SFTL_CLEAN_FREE; + sftl->wear[best]++; + dev_info(sftl->ppn->dev, "reclaimed blank dirty-free block %u (wear %u)\n", + best, sftl->wear[best]); + *claimed = best; + return 0; + } +} + static int sftl_reclaim_retired(struct apple_sftl *sftl, u32 *claimed, unsigned long deadline) { @@ -2467,8 +2607,9 @@ static int sftl_move_read(void *priv, u32 address, struct apple_sftl_gc_page *pa ret = sftl_read_address(sftl, address, &chunk); if (ret) return ret; - memcpy(page->data, sftl->page, sizeof(page->data)); - memcpy(page->oob, sftl->oob, sizeof(page->oob)); + memset(page, 0xff, sizeof(*page)); + memcpy(page->data, sftl->page, sftl->psize); + memcpy(page->oob, sftl->oob, sftl->osize); return 0; } @@ -2482,14 +2623,14 @@ static int sftl_move_program(void *priv, u32 address, const struct apple_sftl_gc if (time_after_eq(jiffies, move->deadline)) return -ETIMEDOUT; if (address != sftl->append_block * sftl_stride(sftl) + sftl->append_position || - sftl->append_position + 4 > sftl->append_capacity) + sftl->append_position + sftl->lpp > sftl->append_capacity) return -ERANGE; ret = sftl_program_address(sftl, address, page->data, page->oob); if (ret) return ret; - for (chunk = 0; chunk < 4; chunk++) + for (chunk = 0; chunk < sftl->lpp; chunk++) sftl_append_summary(sftl, page->oob + chunk * SFTL_META); - sftl->append_position += 4; + sftl->append_position += sftl->lpp; return 0; } @@ -2583,21 +2724,21 @@ static int sftl_native_sites(struct apple_sftl *sftl, const struct apple_sftl_sn } length = sftl->checkpoint_sequence - sftl->checkpoint_start + 1; slots = sftl_slots(sftl, sftl->lanes[prior[0]]); - if (!length || length % 4 || length > slots) + if (!length || length > slots || (u32)length % sftl->lpp) return -EOPNOTSUPP; - for (i = 0; i < slots; i += 4) { + for (i = 0; i < slots; i += sftl->lpp) { if (time_after_eq(jiffies, deadline)) return -ETIMEDOUT; ret = sftl_read_address(sftl, prior[0] * sftl_stride(sftl) + i, &chunk); if (ret) return ret; if (i >= length) { - if (!sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE) || - !sftl_erased(sftl->oob, APPLE_PPN_OOB_SIZE)) + if (!sftl_erased(sftl->page, sftl->psize) || + !sftl_erased(sftl->oob, sftl->osize)) return -EOPNOTSUPP; continue; } - for (j = 0; j < 4; j++) { + for (j = 0; j < sftl->lpp; j++) { const u8 *meta = sftl->oob + j * SFTL_META; if (meta[0] != 0x1f || !meta[1] || meta[1] > 6 || @@ -2710,8 +2851,8 @@ static int sftl_compact(struct apple_sftl *sftl, u32 landing, unsigned long dead goto out; slots = apple_sftl_gc_copy_chunks(gc); /* Keep at least a page for this request, and gain usable capacity. */ - if (!slots || !apple_sftl_gc_has_sequence_room(gc, 4) || - slots + 4 > sftl_slots(sftl, destination.lanes) - SFTL_FOOTER_CHUNKS || + if (!slots || !apple_sftl_gc_has_sequence_room(gc, sftl->lpp) || + slots + sftl->lpp > sftl_slots(sftl, destination.lanes) - SFTL_FOOTER_CHUNKS || slots >= sftl_slots(sftl, victim.lanes) - SFTL_FOOTER_CHUNKS) { apple_sftl_gc_free(gc); gc = NULL; @@ -2880,7 +3021,14 @@ static int sftl_claim_data_block(struct apple_sftl *sftl, bool compact, const u3 return ret; } } else { - ret = sftl_take_blank(sftl, &block, deadline); + if (!compact && sftl->geo->fenced_write && sftl->native_context && + sftl->ppn->ops->erase_block) { + ret = sftl_reuse_blank_dirty(sftl, &block, deadline); + if (ret == -ENOSPC && !sftl->failed) + ret = sftl_take_blank(sftl, &block, deadline); + } else { + ret = sftl_take_blank(sftl, &block, deadline); + } if (ret == -ENOSPC && !sftl->failed) ret = sftl_reclaim_retired(sftl, &block, deadline); if (ret) @@ -3004,12 +3152,12 @@ static int sftl_close_block(struct apple_sftl *sftl) put_unaligned_le32(span, meta + 8); put_unaligned_le16(SFTL_FOOTER_CHUNKS, meta + 12); put_unaligned_le16(0xffff, meta + 14); - for (chunk = 0; chunk < 4; chunk++) + for (chunk = 0; chunk < sftl->lpp; chunk++) memcpy(sftl->stage_oob + chunk * SFTL_META, meta, SFTL_META); - for (index = 0; index < SFTL_FOOTER_CHUNKS / 4; index++) { + for (index = 0; index < SFTL_FOOTER_CHUNKS / sftl->lpp; index++) { u32 address = sftl->append_block * sftl_stride(sftl) + - sftl->append_capacity + index * 4; - const u8 *data = sftl->footer + index * APPLE_PPN_PAGE_SIZE; + sftl->append_capacity + index * sftl->lpp; + const u8 *data = sftl->footer + index * sftl->psize; ret = sftl_program_address(sftl, address, data, sftl->stage_oob); /* A summary consumes no sequence. A given-up block moves its data. */ @@ -3048,18 +3196,18 @@ static int sftl_give_up(struct apple_sftl *sftl, u32 address, const u8 *data, if (err == -EINVAL || err == -ERANGE || ++sftl->failures > SFTL_FAIL_MAX) goto stop; ret = sftl_read_address(sftl, address, &chunk); - blank = !ret && sftl_erased(sftl->oob, sizeof(sftl->oob)) && - sftl_erased(sftl->page, APPLE_PPN_PAGE_SIZE); - landed = !ret && !memcmp(sftl->oob, oob, sizeof(sftl->oob)) && - !memcmp(sftl->page, data, APPLE_PPN_PAGE_SIZE); + blank = !ret && sftl_erased(sftl->oob, sftl->osize) && + sftl_erased(sftl->page, sftl->psize); + landed = !ret && !memcmp(sftl->oob, oob, sftl->osize) && + !memcmp(sftl->page, data, sftl->psize); /* A torn summary is not recognised at load. */ if (!blank && !landed && oob[0] == 0x1c) goto stop; /* A torn data page is the block's last programmed page: a fresh load - * skips it and accounts its four sequences, which are not reused. + * skips it and accounts its lpp sequences, which are not reused. */ if (!blank && oob[0] != 0x1c) { - sftl->sequence += 4; + sftl->sequence += sftl->lpp; sftl->journal_sequence = sftl->sequence; } dev_warn(sftl->ppn->dev, "program at %#x failed (%d): block %u given up\n", @@ -3081,7 +3229,7 @@ static void sftl_stamp_page(struct apple_sftl *sftl, u8 *oob, bool relocated) { unsigned int chunk; - for (chunk = 0; chunk < 4; chunk++) { + for (chunk = 0; chunk < sftl->lpp; chunk++) { u8 *meta = oob + chunk * SFTL_META; u64 sequence = sftl->sequence + 1 + chunk; bool keep = relocated && meta[0] == 1 && !meta[1] && @@ -3108,14 +3256,14 @@ static int sftl_append_once(struct apple_sftl *sftl, const u8 *page, u8 *oob, bo if (ret) return ret; } - if (sftl->sequence > SFTL_SEQUENCE - 4) + if (sftl->sequence > SFTL_SEQUENCE - sftl->lpp) return -EOVERFLOW; sftl_stamp_page(sftl, oob, relocated); address = sftl->append_block * sftl_stride(sftl) + sftl->append_position; ret = sftl_program_address(sftl, address, page, oob); if (ret) return sftl_give_up(sftl, address, page, oob, ret); - for (chunk = 0; chunk < 4; chunk++) { + for (chunk = 0; chunk < sftl->lpp; chunk++) { ret = sftl_replace(sftl, get_unaligned_le32(oob + chunk * SFTL_META + 8), 1, address + chunk); if (ret) { @@ -3124,9 +3272,9 @@ static int sftl_append_once(struct apple_sftl *sftl, const u8 *page, u8 *oob, bo } sftl_append_summary(sftl, oob + chunk * SFTL_META); } - sftl->sequence += 4; + sftl->sequence += sftl->lpp; sftl->journal_sequence = sftl->sequence; - sftl->append_position += 4; + sftl->append_position += sftl->lpp; if (sftl->append_position == sftl->append_capacity) { ret = sftl_close_block(sftl); if (ret) @@ -3175,13 +3323,13 @@ static int sftl_collect_abandoned(struct apple_sftl *sftl, u32 **out, u32 *count return 0; } -/* Read up to four live pages for relocation; a short tail repeats the last. */ +/* Read up to lpp live pages for relocation; a short tail repeats the last. */ static int sftl_load_move(struct apple_sftl *sftl, const u32 *keys, u32 count, u32 first) { unsigned int c, chunk; int ret; - for (c = 0; c < 4; c++) { + for (c = 0; c < sftl->lpp; c++) { u32 key = keys[min(first + c, count - 1)]; const struct sftl_run *run = sftl_find(sftl, key); const u8 *meta; @@ -3212,7 +3360,7 @@ static int sftl_evacuate(struct apple_sftl *sftl) sftl->evacuate = false; kvfree(keys); ret = sftl_collect_abandoned(sftl, &keys, &count); - for (i = 0; !ret && i < count && !sftl->evacuate; i += 4) { + for (i = 0; !ret && i < count && !sftl->evacuate; i += sftl->lpp) { ret = sftl_load_move(sftl, keys, count, i); if (ret) { sftl->failed = true; @@ -3250,25 +3398,25 @@ int apple_sftl_write(struct apple_sftl *sftl, u32 logical_page, unsigned int chunk; int ret; - if (!sftl || !data || !count || count > 4 || logical_page >= sftl->geo->disk_pages || + if (!sftl || !data || !count || count > sftl->lpp || + logical_page >= sftl->geo->disk_pages || count > sftl->geo->disk_pages - logical_page) return -EINVAL; - /* The append path is written for four chunks a page. */ - if (sftl->lpp != 4 || !sftl->geo->writer) + if ((sftl->lpp != 4 && sftl->lpp != 2) || !sftl->geo->writer) return -EROFS; mutex_lock(&sftl->lock); if (sftl->failed) { ret = -EIO; goto out; } - if (sftl->sequence > SFTL_SEQUENCE - 4) { + if (sftl->sequence > SFTL_SEQUENCE - sftl->lpp) { ret = -EOVERFLOW; goto out; } ret = sftl_writer_prepare(sftl); if (ret) goto out; - for (chunk = 0; chunk < 4; chunk++) { + for (chunk = 0; chunk < sftl->lpp; chunk++) { unsigned int index = min(chunk, count - 1); memcpy(sftl->stage + chunk * SFTL_CHUNK, data + index * SFTL_CHUNK, SFTL_CHUNK); @@ -3298,6 +3446,8 @@ EXPORT_SYMBOL_GPL(apple_sftl_flush); void apple_sftl_get_info(struct apple_sftl *sftl, struct apple_sftl_info *info) { info->read_only = sftl->vfl_degraded || sftl->context_degraded || !sftl->geo->writer; + info->fenced_write = sftl->geo->fenced_write; + info->lpp = sftl->lpp; info->logical_pages = sftl->geo->lpages; info->disk_pages = sftl->geo->disk_pages; info->nr_runs = sftl->nr_runs; diff --git a/drivers/mtd/apple-sftl-lwvm-test.c b/drivers/mtd/apple-sftl-lwvm-test.c index c66d1564f74479..b8a3c5bf09899a 100644 --- a/drivers/mtd/apple-sftl-lwvm-test.c +++ b/drivers/mtd/apple-sftl-lwvm-test.c @@ -459,6 +459,103 @@ static void lwvm_p105_16gb(struct kunit *test) KUNIT_EXPECT_EQ(test, view_map(test, f->view, 89 * chunk), 870 * chunk); } +static void lwvm_j2a_64gb_write_fence(struct kunit *test) +{ + static const u8 type_hfs[16] = { + 0x48, 0x46, 0x53, 0x00, 0x00, 0x00, 0x11, 0xaa, + 0xaa, 0x11, 0x00, 0x30, 0x65, 0x43, 0xec, 0xac, + }; + static const u8 type_linux[16] = { + 0x0f, 0xc6, 0x3d, 0xaf, 0x84, 0x83, 0x47, 0x72, + 0x8e, 0x79, 0x3d, 0x69, 0xd8, 0x47, 0x7d, 0xe4, + }; + static const u8 g_sys[16] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + static const u8 g_dat[16] = { 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + static const u8 g_lin[16] = { 3, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }; + u64 media = 15625000ULL * 4096ULL; /* 64 GB iPad3,3 */ + u64 chunk = 1ULL << 26; /* 64 MiB chunks */ + u32 chunks = media >> 26; /* 953 chunks */ + u8 *primary = kunit_kzalloc(test, APPLE_SFTL_LWVM_HEADER_BYTES, GFP_KERNEL); + u8 *backup = kunit_kzalloc(test, APPLE_SFTL_LWVM_HEADER_BYTES, GFP_KERNEL); + struct apple_sftl_lwvm_fence fence; + u32 k; + + KUNIT_ASSERT_NOT_NULL(test, primary); + KUNIT_ASSERT_NOT_NULL(test, backup); + + /* 1. Stock 64 GB iPad 3 layout: System (chunks 1..44) + pieced Data (chunks 45..946). */ + header_init(primary, media, 2); + primary[0x10] = 0xa1; /* non-zero media UUID */ + for (k = 1; k < chunks; k++) + put_unaligned_le16(0xf111, primary + 0x800 + 2 * k); + for (k = chunks; k < APPLE_SFTL_LWVM_CHUNKS; k++) + put_unaligned_le16(0xf3ff, primary + 0x800 + 2 * k); + header_part(primary, 0, 0x6000, 0x00afdfa000ULL, "System"); + memcpy(primary + 0x200, type_hfs, 16); + memcpy(primary + 0x210, g_sys, 16); + header_part(primary, 1, 0x00afdfa000ULL, 0x0ec4006000ULL, "Data"); + memcpy(primary + 0x280, type_hfs, 16); + memcpy(primary + 0x290, g_dat, 16); + header_run(primary, 0, 0, 1, 44); + header_run(primary, 1, 892, 45, 10); + header_run(primary, 1, 0, 55, 892); + header_seal(primary); + memcpy(backup, primary, APPLE_SFTL_LWVM_HEADER_BYTES); + + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_build_fence(primary, backup, media, &fence), 0); + KUNIT_EXPECT_EQ(test, fence.writable_count, 0U); + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 0)); + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 16384)); + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 55 * 16384)); + + /* 2. Backup mismatch is rejected with -EUCLEAN. */ + backup[0x30] ^= 1; + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_build_fence(primary, backup, media, &fence), -EUCLEAN); + backup[0x30] ^= 1; + + /* 3. Add contiguous Linux p3 over chunks 900..939 (shrinking Data to chunks 55..899). */ + header_init(primary, media, 3); + primary[0x10] = 0xa1; + for (k = 1; k < chunks; k++) + put_unaligned_le16(0xf111, primary + 0x800 + 2 * k); + for (k = chunks; k < APPLE_SFTL_LWVM_CHUNKS; k++) + put_unaligned_le16(0xf3ff, primary + 0x800 + 2 * k); + header_part(primary, 0, 0x6000, 44 * chunk + 0x6000, "System"); + memcpy(primary + 0x200, type_hfs, 16); + memcpy(primary + 0x210, g_sys, 16); + header_part(primary, 1, 44 * chunk + 0x6000, (44 + 845) * chunk + 0x6000, "Data"); + memcpy(primary + 0x280, type_hfs, 16); + memcpy(primary + 0x290, g_dat, 16); + header_part(primary, 2, (44 + 845) * chunk + 0x6000, + (44 + 845 + 40) * chunk + 0x6000, "Linux"); + memcpy(primary + 0x300, type_linux, 16); + memcpy(primary + 0x310, g_lin, 16); + header_run(primary, 0, 0, 1, 44); + header_run(primary, 1, 0, 55, 845); + header_run(primary, 2, 0, 900, 40); + header_seal(primary); + memcpy(backup, primary, APPLE_SFTL_LWVM_HEADER_BYTES); + + KUNIT_ASSERT_EQ(test, apple_sftl_lwvm_build_fence(primary, backup, media, &fence), 0); + KUNIT_EXPECT_EQ(test, fence.writable_count, 40U); + /* Chunk 0 (header), System (1..44), free (45..54), Data (55..899), tail (940+) stay RO! */ + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 0)); + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 16383)); + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 16384)); + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 45 * 16384)); + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 899 * 16384 + 16383)); + KUNIT_EXPECT_FALSE(test, apple_sftl_lwvm_lba_writable(&fence, 940 * 16384)); + /* Contiguous Linux p3 (chunks 900..939) is writable! */ + KUNIT_EXPECT_TRUE(test, apple_sftl_lwvm_lba_writable(&fence, 900 * 16384)); + KUNIT_EXPECT_TRUE(test, apple_sftl_lwvm_lba_writable(&fence, 939 * 16384 + 16383)); + + /* 4. Partition ending past iOS 8.4.1 ios_end is rejected. */ + put_unaligned_le64(apple_sftl_lwvm_ios_end(media, 26) + 4096, primary + 0x300 + 40); + header_seal(primary); + memcpy(backup, primary, APPLE_SFTL_LWVM_HEADER_BYTES); + KUNIT_EXPECT_EQ(test, apple_sftl_lwvm_build_fence(primary, backup, media, &fence), -EINVAL); +} + static struct kunit_case lwvm_cases[] = { KUNIT_CASE(lwvm_chunk_size), KUNIT_CASE(lwvm_contiguous), @@ -469,6 +566,7 @@ static struct kunit_case lwvm_cases[] = { KUNIT_CASE(lwvm_view), KUNIT_CASE(lwvm_view_empty), KUNIT_CASE(lwvm_p105_16gb), + KUNIT_CASE(lwvm_j2a_64gb_write_fence), {} }; diff --git a/drivers/mtd/apple-sftl-lwvm.c b/drivers/mtd/apple-sftl-lwvm.c index 712c741c309d80..5351da4e9de808 100644 --- a/drivers/mtd/apple-sftl-lwvm.c +++ b/drivers/mtd/apple-sftl-lwvm.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -236,3 +237,136 @@ enum apple_sftl_lwvm_page apple_sftl_lwvm_view_map(const struct apple_sftl_lwvm_ return APPLE_SFTL_LWVM_PAGE_DISK; } EXPORT_SYMBOL_GPL(apple_sftl_lwvm_view_map); + +/* 0fc63daf-8483-4772-8e79-3d69d8477de4 (Linux filesystem data), LwVM network byte order. */ +static const u8 lwvm_type_linux[16] = { + 0x0f, 0xc6, 0x3d, 0xaf, 0x84, 0x83, 0x47, 0x72, + 0x8e, 0x79, 0x3d, 0x69, 0xd8, 0x47, 0x7d, 0xe4, +}; + +/* bc13c2ff-59e6-4262-a352-b275fd6f7172 (XBOOTLDR), LwVM network byte order. */ +static const u8 lwvm_type_xbootldr[16] = { + 0xbc, 0x13, 0xc2, 0xff, 0x59, 0xe6, 0x42, 0x62, + 0xa3, 0x52, 0xb2, 0x75, 0xfd, 0x6f, 0x71, 0x72, +}; + +#define LWVM_ID_OFFSET 0x10 +#define LWVM_SENTINEL_HEAD 0xf000U +#define LWVM_SENTINEL_FREE 0xf111U +#define LWVM_SENTINEL_PAST 0xf3ffU + +/* + * iOS 8.4.1 LwVM virtual end bound: usable space is (chunks - 8) chunks past + * the header reserve of two 4 KiB pages + 16 KiB. + */ +u64 apple_sftl_lwvm_ios_end(u64 media_bytes, unsigned int chunk_shift) +{ + u64 chunks; + + if (!chunk_shift || chunk_shift >= 64) + return 0; + chunks = media_bytes >> chunk_shift; + return chunks > 8 ? ((chunks - 8) << chunk_shift) + + 2 * APPLE_SFTL_LWVM_HEADER_BYTES + 0x4000 : 0; +} +EXPORT_SYMBOL_GPL(apple_sftl_lwvm_ios_end); + +static bool lwvm_writable_type(const u8 *type_guid) +{ + return !memcmp(type_guid, lwvm_type_linux, sizeof(lwvm_type_linux)) || + !memcmp(type_guid, lwvm_type_xbootldr, sizeof(lwvm_type_xbootldr)); +} + +int apple_sftl_lwvm_build_fence(const u8 *primary, const u8 *backup, + u64 media_bytes, struct apple_sftl_lwvm_fence *fence) +{ + struct apple_sftl_lwvm *lwvm; + u64 ios_end; + u32 i, j, k; + int ret; + + if (!primary || !backup || !fence) + return -EINVAL; + memset(fence, 0, sizeof(*fence)); + if (memcmp(primary, backup, APPLE_SFTL_LWVM_HEADER_BYTES)) + return -EUCLEAN; + lwvm = kzalloc(sizeof(*lwvm), GFP_KERNEL); + if (!lwvm) + return -ENOMEM; + ret = apple_sftl_lwvm_decode(primary, media_bytes, lwvm); + if (ret) + goto out; + if (!memchr_inv(primary + LWVM_ID_OFFSET, 0, 16)) { + ret = -EINVAL; + goto out; + } + ios_end = apple_sftl_lwvm_ios_end(media_bytes, lwvm->chunk_shift); + for (i = 0; i < lwvm->nr_parts; i++) { + const struct apple_sftl_lwvm_part *p = &lwvm->parts[i]; + const u8 *e = primary + LWVM_PARTS_OFFSET + i * LWVM_PART_BYTES; + + if (p->error || p->mapped != p->nr_chunks || + !memchr_inv(e, 0, 16) || !memchr_inv(p->guid, 0, 16) || + p->begin < APPLE_SFTL_LWVM_HEADER_BYTES || + (p->begin | p->end) & (APPLE_SFTL_LWVM_HEADER_BYTES - 1) || + p->end > media_bytes || (ios_end && p->end > ios_end)) { + ret = -EINVAL; + goto out; + } + for (j = 0; j < i; j++) { + if (!memcmp(p->guid, lwvm->parts[j].guid, 16) || + (p->begin < lwvm->parts[j].end && p->end > lwvm->parts[j].begin)) { + ret = -EINVAL; + goto out; + } + } + } + for (k = 0; k < APPLE_SFTL_LWVM_CHUNKS; k++) { + u16 w = get_unaligned_le16(primary + LWVM_MAP_OFFSET + 2 * k); + + if (w >> 12 != LWVM_CHUNK_FREE) + continue; + if (!k ? w != LWVM_SENTINEL_HEAD : + k < lwvm->chunks ? w != LWVM_SENTINEL_FREE : w != LWVM_SENTINEL_PAST) { + ret = -EINVAL; + goto out; + } + } + fence->chunk_shift = lwvm->chunk_shift; + fence->chunks = lwvm->chunks; + for (i = 0; i < lwvm->nr_parts; i++) { + const struct apple_sftl_lwvm_part *p = &lwvm->parts[i]; + const u8 *e = primary + LWVM_PARTS_OFFSET + i * LWVM_PART_BYTES; + + /* Only contiguous Linux/XBOOTLDR partitions are writable on applesftlN. */ + if (p->pieced || !lwvm_writable_type(e)) + continue; + for (k = 0; k < p->nr_chunks; k++) { + u16 disk_chunk = p->chunk[k]; + + if (disk_chunk && disk_chunk < lwvm->chunks && + !test_and_set_bit(disk_chunk, fence->writable_chunks)) + fence->writable_count++; + } + } + ret = 0; +out: + kfree(lwvm); + return ret; +} +EXPORT_SYMBOL_GPL(apple_sftl_lwvm_build_fence); + +bool apple_sftl_lwvm_lba_writable(const struct apple_sftl_lwvm_fence *fence, + u32 logical_page) +{ + u64 byte_offset, chunk; + + if (!fence || !fence->chunk_shift || !fence->writable_count) + return false; + byte_offset = (u64)logical_page << 12; + chunk = byte_offset >> fence->chunk_shift; + return chunk > 0 && chunk < fence->chunks && + chunk < APPLE_SFTL_LWVM_CHUNKS && + test_bit(chunk, fence->writable_chunks); +} +EXPORT_SYMBOL_GPL(apple_sftl_lwvm_lba_writable); diff --git a/drivers/mtd/apple-sftl-lwvm.h b/drivers/mtd/apple-sftl-lwvm.h index 11604305ee20dd..0749a350b013b4 100644 --- a/drivers/mtd/apple-sftl-lwvm.h +++ b/drivers/mtd/apple-sftl-lwvm.h @@ -3,6 +3,7 @@ #define _APPLE_SFTL_LWVM_H #include +#include #define APPLE_SFTL_LWVM_HEADER_BYTES 4096U #define APPLE_SFTL_LWVM_CHUNKS 1024U @@ -68,4 +69,23 @@ u32 apple_sftl_lwvm_view(const u8 *header, const struct apple_sftl_lwvm *lwvm, enum apple_sftl_lwvm_page apple_sftl_lwvm_view_map(const struct apple_sftl_lwvm_view *view, u64 offset, u64 *disk_offset); +/* + * Immutable write allowlist derived from a verified primary/backup LwVM header + * pair. Only chunks owned by contiguous Linux or XBOOTLDR partitions may be + * written; chunk 0, HFS+ iOS partitions, free chunks and past-end chunks stay + * read-only. + */ +struct apple_sftl_lwvm_fence { + u8 chunk_shift; + u32 chunks; + u32 writable_count; + DECLARE_BITMAP(writable_chunks, APPLE_SFTL_LWVM_CHUNKS); +}; + +u64 apple_sftl_lwvm_ios_end(u64 media_bytes, unsigned int chunk_shift); +int apple_sftl_lwvm_build_fence(const u8 *primary, const u8 *backup, + u64 media_bytes, struct apple_sftl_lwvm_fence *fence); +bool apple_sftl_lwvm_lba_writable(const struct apple_sftl_lwvm_fence *fence, + u32 logical_page); + #endif diff --git a/drivers/mtd/apple-sftl-reclaim-test.c b/drivers/mtd/apple-sftl-reclaim-test.c index 969af6857e8a29..1486675e8bf5d2 100644 --- a/drivers/mtd/apple-sftl-reclaim-test.c +++ b/drivers/mtd/apple-sftl-reclaim-test.c @@ -6,7 +6,9 @@ #include #include +#include "apple-sftl-account.h" #include "apple-sftl-reclaim.h" +#include "apple-sftl-test-fixture.h" static void reclaim_source(struct apple_sftl_snapshot *snapshot, struct apple_sftl_context_record *opaque, @@ -456,6 +458,74 @@ static void reclaim_copy_live_pages(struct kunit *test) } } +static void reclaim_64gb_8k_lpp2(struct kunit *test) +{ + struct apple_sftl_context_record *opaque; + struct apple_sftl_extent runs[2], recovered_runs[2]; + struct apple_sftl_snapshot snapshot = {}, recovered; + struct apple_sftl_gc_block victim = { + .block = 4, .lanes = 3, .footer_chunks = 2, + .closed = true, .last_sequence = 199, + }; + struct apple_sftl_gc_block destination = { + .block = 7, .lanes = 3, .blank = true, + }; + const struct apple_sftl_gc_copy *copy; + struct apple_sftl_checkpoint *image = NULL; + struct apple_sftl_gc *gc = NULL; + u32 blocks[] = { 802, 830 }; + + opaque = kunit_kcalloc(test, APPLE_SFTL_64GB_8K_ACCOUNT_RECORDS, + sizeof(*opaque), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, opaque); + apple_sftl_test_native_prefix_64gb_8k(opaque); + /* 3 live chunks in victim -> lpp=2 pads with 1 chunk (total 4 chunks = 2 x 8 KiB pages). */ + runs[0] = (struct apple_sftl_extent) { + .logical = 0, .length = 3, .provider = 4 * APPLE_SFTL_STRIDE, + }; + runs[1] = (struct apple_sftl_extent) { + .logical = 3, .length = APPLE_SFTL_LOGICAL_PAGES - 3, + .provider = APPLE_SFTL_NO_ADDRESS, + }; + /* Seed valid count so apple_sftl_account_map() validates the prior state. */ + put_unaligned_le16(3, opaque[4].data + 4 * 8 + 2); + put_unaligned_le64(3, opaque[2].data + 4 + 8 * 16 + 8); + snapshot = (struct apple_sftl_snapshot) { + .runs = runs, .nr_runs = ARRAY_SIZE(runs), + .opaque = opaque, .nr_opaque = APPLE_SFTL_64GB_8K_ACCOUNT_RECORDS, + .subblocks = APPLE_SFTL_64GB_8K_SUBBLOCKS, .lpp = 2, + .sequence = 200, .checkpoint_sequence = 101, .journal_sequence = 200, + }; + + KUNIT_ASSERT_EQ(test, apple_sftl_gc_plan(&snapshot, &victim, &destination, + 64, 131072, &gc), 0); + KUNIT_EXPECT_EQ(test, apple_sftl_gc_copy_chunks(gc), 4U); + copy = apple_sftl_gc_next_copy(gc); + KUNIT_ASSERT_NOT_NULL(test, copy); + KUNIT_EXPECT_EQ(test, copy->logical, 0U); + KUNIT_EXPECT_EQ(test, copy->length, 3U); + /* 1 leading pad chunk for lpp=2 alignment (not 1..3 for lpp=4!). */ + KUNIT_EXPECT_EQ(test, copy->destination, 7 * APPLE_SFTL_STRIDE + 1); + KUNIT_ASSERT_EQ(test, apple_sftl_gc_copy_done(gc, copy, 204), 0); + KUNIT_EXPECT_EQ(test, apple_sftl_gc_stage(gc), APPLE_SFTL_GC_CHECKPOINT); + KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint(gc, blocks, 2, &image), 0); + /* 24 opaque + 32 map_min = 56 records (matches live 64 GB iPad3,3!). */ + KUNIT_EXPECT_EQ(test, image->count, 56U); + KUNIT_EXPECT_EQ(test, image->first_sequence, 205ULL); + KUNIT_EXPECT_EQ(test, image->sequence, 260ULL); + KUNIT_ASSERT_EQ(test, apple_sftl_gc_checkpoint_done(gc, image), 0); + memcpy(recovered_runs, runs, sizeof(runs)); + recovered_runs[0].provider = 7 * APPLE_SFTL_STRIDE + 1; + recovered = snapshot; + recovered.runs = recovered_runs; + recovered.sequence = image->sequence; + recovered.journal_sequence = image->sequence; + recovered.checkpoint_sequence = image->sequence; + KUNIT_ASSERT_EQ(test, apple_sftl_gc_recovered(gc, &recovered), 0); + apple_sftl_checkpoint_free(image); + apple_sftl_gc_free(gc); +} + static struct kunit_case reclaim_cases[] = { KUNIT_CASE(reclaim_copy_live_pages), KUNIT_CASE(reclaim_metadata_vector), @@ -466,6 +536,7 @@ static struct kunit_case reclaim_cases[] = { KUNIT_CASE(reclaim_gc_transaction), KUNIT_CASE(reclaim_gc_fail_stop), KUNIT_CASE(reclaim_obsolete_history_needs_checkpoint), + KUNIT_CASE(reclaim_64gb_8k_lpp2), {} }; diff --git a/drivers/mtd/apple-sftl-reclaim.c b/drivers/mtd/apple-sftl-reclaim.c index 70ebba4e4873ad..c70bd50135e893 100644 --- a/drivers/mtd/apple-sftl-reclaim.c +++ b/drivers/mtd/apple-sftl-reclaim.c @@ -27,6 +27,7 @@ struct apple_sftl_gc { struct apple_sftl_snapshot snapshot; + struct apple_sftl_layout layout; struct apple_sftl_gc_copy *copies; struct apple_sftl_checkpoint *checkpoint; bool issued; @@ -50,12 +51,15 @@ int apple_sftl_context_meta(u8 *meta, u8 subtype, u64 sequence, } EXPORT_SYMBOL_GPL(apple_sftl_context_meta); -static int sftl_check_map(const struct apple_sftl_snapshot *snapshot) +static int sftl_check_map(const struct apple_sftl_snapshot *snapshot, + struct apple_sftl_layout *out) { + struct apple_sftl_layout layout; u32 cursor = 0, i; if (!snapshot || !snapshot->runs || !snapshot->nr_runs || - snapshot->nr_runs > APPLE_SFTL_LOGICAL_PAGES || + !apple_sftl_snapshot_layout(snapshot, &layout) || + snapshot->nr_runs > layout.logical_pages || snapshot->checkpoint_sequence > snapshot->sequence || snapshot->journal_sequence > snapshot->sequence || snapshot->sequence > SFTL_SEQUENCE) @@ -64,17 +68,39 @@ static int sftl_check_map(const struct apple_sftl_snapshot *snapshot) const struct apple_sftl_extent *run = &snapshot->runs[i]; if (run->logical != cursor || !run->length || - run->length > APPLE_SFTL_LOGICAL_PAGES - cursor || + run->length > layout.logical_pages - cursor || (run->provider != APPLE_SFTL_NO_ADDRESS && - (run->provider >= SFTL_PROVIDER || - run->length > SFTL_PROVIDER - run->provider))) + (run->provider >= layout.provider_limit || + run->length > layout.provider_limit - run->provider))) return -EBADMSG; cursor += run->length; } - return cursor == APPLE_SFTL_LOGICAL_PAGES ? 0 : -EBADMSG; + if (cursor != layout.logical_pages) + return -EBADMSG; + if (out) + *out = layout; + return 0; } -static int sftl_check_context(const struct apple_sftl_snapshot *snapshot) +static bool sftl_native_context(const struct apple_sftl_snapshot *snapshot, + struct apple_sftl_layout *out) +{ + struct apple_sftl_layout layout; + u32 i; + + if (!snapshot || !snapshot->opaque || + !apple_sftl_records_layout(snapshot->opaque, snapshot->nr_opaque, &layout)) + return false; + for (i = 0; i < layout.lpp; i++) + if (snapshot->opaque[i].meta[1] != 1) + return false; + if (out) + *out = layout; + return true; +} + +static int sftl_check_context(const struct apple_sftl_snapshot *snapshot, + const struct apple_sftl_layout *layout) { u32 i, previous = 0, names; @@ -88,13 +114,14 @@ static int sftl_check_context(const struct apple_sftl_snapshot *snapshot) return -EBADMSG; previous = order; } - /* A four-chunk native base cannot be downgraded to the compact form + /* A multi-chunk native base cannot be downgraded to the compact form * when its geometry or accounting prefix is damaged. */ - if (snapshot->nr_opaque >= 4 && snapshot->opaque[3].meta[1] == 1 && - (snapshot->nr_opaque != APPLE_SFTL_ACCOUNT_RECORDS || - get_unaligned_le32(snapshot->opaque[3].data + APPLE_SFTL_CHUNK - 4) != - APPLE_SFTL_SUBBLOCKS)) + if (snapshot->nr_opaque >= layout->lpp && + snapshot->opaque[layout->lpp - 1].meta[1] == 1 && + (snapshot->nr_opaque != layout->opaque_records || + get_unaligned_le32(snapshot->opaque[layout->lpp - 1].data + + APPLE_SFTL_CHUNK - 4) != layout->subblocks)) return -EOPNOTSUPP; names = get_unaligned_le32(snapshot->opaque[0].data); if (!names || names > 16) @@ -102,20 +129,13 @@ static int sftl_check_context(const struct apple_sftl_snapshot *snapshot) return 0; } -static bool sftl_native_context(const struct apple_sftl_snapshot *snapshot) -{ - const struct apple_sftl_context_record *base = snapshot->opaque; - - return snapshot->nr_opaque >= 4 && base[0].meta[1] == 1 && - base[1].meta[1] == 1 && base[2].meta[1] == 1 && base[3].meta[1] == 1 && - get_unaligned_le32(base[3].data + APPLE_SFTL_CHUNK - 4) == APPLE_SFTL_SUBBLOCKS; -} - static int sftl_checkpoint_count(const struct apple_sftl_snapshot *snapshot, - u32 max_records, u32 *count) + u32 max_records, u32 *count, + struct apple_sftl_layout *out) { + struct apple_sftl_layout layout; u32 records; - int ret = sftl_check_map(snapshot); + int ret = sftl_check_map(snapshot, &layout); if (ret) return ret; @@ -123,21 +143,24 @@ static int sftl_checkpoint_count(const struct apple_sftl_snapshot *snapshot, snapshot->nr_opaque > SFTL_MAX_RECORDS || !max_records || max_records > SFTL_MAX_RECORDS) return -EINVAL; - ret = sftl_check_context(snapshot); + ret = sftl_check_context(snapshot, &layout); if (ret) return ret; records = (snapshot->nr_runs + SFTL_PAIRS - 1) / SFTL_PAIRS; - /* cxtNextBatch pads the measured 64 GB range stream to whole batches. */ - if (sftl_native_context(snapshot)) - records = (records + APPLE_SFTL_NATIVE_MAP_BATCH - 1) & - ~(APPLE_SFTL_NATIVE_MAP_BATCH - 1); + /* cxtNextBatch pads native range streams to whole batches (with floor map_min). */ + if (sftl_native_context(snapshot, NULL)) { + records = max(records, layout.map_min); + records = round_up(records, layout.map_batch); + } records += snapshot->nr_opaque; - records = (records + 3) & ~3U; + records = round_up(records, layout.lpp); if (records > max_records) return -ENOSPC; if (records > SFTL_SEQUENCE - snapshot->sequence) return -EOVERFLOW; *count = records; + if (out) + *out = layout; return 0; } @@ -165,19 +188,20 @@ static int sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, bool preserve_cached_stats, struct apple_sftl_checkpoint **out) { struct apple_sftl_checkpoint *checkpoint; + struct apple_sftl_layout layout; u32 count, i, j, cursor = 0, run = 0, old_names; int ret; if (!out) return -EINVAL; *out = NULL; - ret = sftl_checkpoint_count(snapshot, max_records, &count); + ret = sftl_checkpoint_count(snapshot, max_records, &count, &layout); if (ret) return ret; if (!blocks || !nr_blocks || nr_blocks > 16) return -EINVAL; for (i = 0; i < nr_blocks; i++) { - if (!blocks[i] || blocks[i] >= APPLE_SFTL_SUBBLOCKS) + if (!blocks[i] || blocks[i] >= layout.subblocks) return -EINVAL; for (j = 0; j < i; j++) if (blocks[j] == blocks[i]) @@ -224,13 +248,13 @@ static int sftl_checkpoint_build(const struct apple_sftl_snapshot *snapshot, u32 address = extent->provider; if (address == APPLE_SFTL_NO_ADDRESS) - address = SFTL_UNMAPPED; + address = layout.unmapped_sentinel; put_unaligned_le32(address, record->data + 8 + j * 8); put_unaligned_le32(extent->length, record->data + 12 + j * 8); cursor += extent->length; } } - if (sftl_native_context(snapshot)) { + if (sftl_native_context(snapshot, NULL)) { ret = preserve_cached_stats ? apple_sftl_account_saved_map(checkpoint, snapshot) : apple_sftl_account_map(checkpoint, snapshot); if (ret) { @@ -265,9 +289,10 @@ static void sftl_gc_run(struct apple_sftl_snapshot *map, u32 logical, }; } -static bool sftl_gc_block_valid(const struct apple_sftl_gc_block *block) +static bool sftl_gc_block_valid(const struct apple_sftl_gc_block *block, + const struct apple_sftl_layout *layout) { - return block && block->block && block->block < APPLE_SFTL_SUBBLOCKS && + return block && block->block && block->block < layout->subblocks && block->lanes && !block->reserved && block->last_sequence <= SFTL_SEQUENCE; } @@ -277,6 +302,7 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, u32 max_records, size_t max_bytes, struct apple_sftl_gc **out) { struct apple_sftl_gc *gc; + struct apple_sftl_layout layout; u32 start, end, dest, capacity, copied = 0, i, count, footer; size_t map_bytes, copy_bytes, opaque_bytes; int ret; @@ -284,29 +310,29 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, if (!out) return -EINVAL; *out = NULL; - ret = sftl_checkpoint_count(snapshot, max_records, &count); + ret = sftl_checkpoint_count(snapshot, max_records, &count, &layout); if (ret) return ret; if (snapshot->sequence != snapshot->journal_sequence) return -EOPNOTSUPP; - if (!sftl_gc_block_valid(victim) || !sftl_gc_block_valid(destination) || + if (!sftl_gc_block_valid(victim, &layout) || !sftl_gc_block_valid(destination, &layout) || victim->blank || !victim->closed || !destination->blank || destination->last_sequence || victim->block == destination->block || victim->last_sequence > snapshot->sequence) return -EINVAL; - footer = victim->footer_chunks ? victim->footer_chunks : 32; - if (footer < 4 || footer > 32 || footer % 4) + footer = victim->footer_chunks ? victim->footer_chunks : layout.footer_max; + if (footer < layout.lpp || footer > layout.footer_max || footer % layout.lpp) return -EINVAL; - start = victim->block * APPLE_SFTL_STRIDE; - end = start + hweight16(victim->lanes) * 512; - dest = destination->block * APPLE_SFTL_STRIDE; - /* Eight native footer pages remain available when the destination closes. */ - capacity = hweight16(destination->lanes) * 512 - 32; + start = victim->block * layout.stride; + end = start + hweight16(victim->lanes) * layout.lane_slots; + dest = destination->block * layout.stride; + /* Reserve native footer pages when the destination closes. */ + capacity = hweight16(destination->lanes) * layout.lane_slots - layout.footer_max; for (i = 0; i < snapshot->nr_runs; i++) { const struct apple_sftl_extent *run = &snapshot->runs[i]; if (run->provider != APPLE_SFTL_NO_ADDRESS && - sftl_overlap(run->provider, run->length, dest, dest + APPLE_SFTL_STRIDE)) + sftl_overlap(run->provider, run->length, dest, dest + layout.stride)) return -EBADMSG; } map_bytes = array_size(snapshot->nr_runs * 3, sizeof(*gc->snapshot.runs)); @@ -318,6 +344,9 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, gc = kzalloc(sizeof(*gc), GFP_KERNEL); if (!gc) return -ENOMEM; + gc->layout = layout; + gc->snapshot.subblocks = layout.subblocks; + gc->snapshot.lpp = layout.lpp; gc->snapshot.sequence = snapshot->sequence; gc->snapshot.checkpoint_sequence = snapshot->checkpoint_sequence; gc->snapshot.journal_sequence = snapshot->journal_sequence; @@ -343,8 +372,8 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, u32 before, live, offset, address = run->provider; if (address != APPLE_SFTL_NO_ADDRESS && - sftl_overlap(address, run->length, start, start + APPLE_SFTL_STRIDE) && - min(address + run->length, start + APPLE_SFTL_STRIDE) > end - footer) { + sftl_overlap(address, run->length, start, start + layout.stride) && + min(address + run->length, start + layout.stride) > end - footer) { ret = -EBADMSG; goto failed; } @@ -375,7 +404,7 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, /* Program complete pages. Repeat the first key in the leading slots: * its last copy then begins an uninterrupted run of live mappings. */ - gc->padding = (4 - copied % 4) % 4; + gc->padding = (layout.lpp - copied % layout.lpp) % layout.lpp; if (gc->padding) { for (i = 0; i < gc->nr_copies; i++) gc->copies[i].destination += gc->padding; @@ -394,7 +423,7 @@ int apple_sftl_gc_plan(const struct apple_sftl_snapshot *snapshot, } gc->snapshot.sequence += copied; gc->snapshot.journal_sequence += copied; - ret = sftl_checkpoint_count(&gc->snapshot, max_records, &count); + ret = sftl_checkpoint_count(&gc->snapshot, max_records, &count, NULL); if (ret) goto failed; gc->stage = gc->nr_copies ? APPLE_SFTL_GC_COPY : APPLE_SFTL_GC_CHECKPOINT; @@ -435,7 +464,7 @@ bool apple_sftl_gc_has_sequence_room(const struct apple_sftl_gc *gc, u32 pending { u32 count; - if (!gc || sftl_checkpoint_count(&gc->snapshot, gc->max_records, &count)) + if (!gc || sftl_checkpoint_count(&gc->snapshot, gc->max_records, &count, NULL)) return false; return pending <= SFTL_SEQUENCE - gc->snapshot.sequence - count; } @@ -470,14 +499,18 @@ EXPORT_SYMBOL_GPL(apple_sftl_gc_copy_done); static int sftl_gc_program(struct apple_sftl_gc_page *buffer, const struct apple_sftl_gc_page *page, - const struct apple_sftl_gc_io *io, void *priv, u32 address) + const struct apple_sftl_gc_io *io, void *priv, + u32 address, u8 lpp) { + size_t data_bytes = lpp * APPLE_SFTL_CHUNK; + size_t oob_bytes = lpp * APPLE_SFTL_META; int ret; ret = io->read(priv, address, buffer); if (ret) return ret; - if (memchr_inv(buffer, 0xff, sizeof(*buffer))) + if (memchr_inv(buffer->data, 0xff, data_bytes) || + memchr_inv(buffer->oob, 0xff, oob_bytes)) return -EBADMSG; ret = io->program(priv, address, page); if (ret) @@ -485,7 +518,8 @@ static int sftl_gc_program(struct apple_sftl_gc_page *buffer, ret = io->read(priv, address, buffer); if (ret) return ret; - return memcmp(page, buffer, sizeof(*page)) ? -EBADMSG : 0; + return (memcmp(page->data, buffer->data, data_bytes) || + memcmp(page->oob, buffer->oob, oob_bytes)) ? -EBADMSG : 0; } static int sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, @@ -495,20 +529,30 @@ static int sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, { struct apple_sftl_checkpoint *copy; struct apple_sftl_gc_page *pages; - u32 capacity = hweight16(lanes) * 512, start = block * APPLE_SFTL_STRIDE; - u32 i, j, order = 0; + struct apple_sftl_layout layout; + u32 capacity, start, i, j, order = 0; bool native; int ret; if (!readback) return -EINVAL; *readback = NULL; - if (!image || !image->records || !image->count || image->count % 4 || - !block || block >= APPLE_SFTL_SUBBLOCKS || image->count > capacity || + if (!image || !image->records || !image->count || !io || !io->read || !io->program) + return -EINVAL; + native = apple_sftl_records_layout(image->records, image->count, &layout); + if (!native) { + if (gc) + layout = gc->layout; + else + apple_sftl_layout_for(APPLE_SFTL_SUBBLOCKS, 4, &layout); + } + capacity = hweight16(lanes) * layout.lane_slots; + start = block * layout.stride; + if (image->count % layout.lpp || + !block || block >= layout.subblocks || image->count > capacity || !image->first_sequence || image->sequence > SFTL_SEQUENCE || image->first_sequence > image->sequence || - image->sequence - image->first_sequence != image->count - 1 || - !io || !io->read || !io->program) + image->sequence - image->first_sequence != image->count - 1) return -EINVAL; if (image->records[0].meta[1] != 1 || image->records[image->count - 1].meta[1] != 4 || @@ -516,17 +560,18 @@ static int sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, get_unaligned_le32(image->records[0].data) > 16 || get_unaligned_le32(image->records[0].data + 4) != block) return -EBADMSG; - native = image->records[3].meta[1] == 1; - if (native && (image->count <= APPLE_SFTL_ACCOUNT_RECORDS || - (image->count - APPLE_SFTL_ACCOUNT_RECORDS) % APPLE_SFTL_NATIVE_MAP_BATCH || - get_unaligned_le32(image->records[3].data + APPLE_SFTL_CHUNK - 4) != - APPLE_SFTL_SUBBLOCKS)) + if (native && (image->count <= layout.opaque_records || + (image->count - layout.opaque_records) % layout.map_batch || + get_unaligned_le32(image->records[layout.lpp - 1].data + + APPLE_SFTL_CHUNK - 4) != layout.subblocks)) return -EBADMSG; for (i = 0; i < image->count; i++) { const u8 *meta = image->records[i].meta; u32 group = meta[1] == 4 ? 7 : meta[1]; - if (group < order || (native && !apple_sftl_native_context_tag(i, meta))) + if (group < order || + (native && !apple_sftl_native_context_tag_lpp(layout.subblocks, layout.lpp, + i, meta))) return -EBADMSG; order = group; if (meta[0] != 0x1f || !meta[1] || meta[1] > 6 || @@ -546,35 +591,37 @@ static int sftl_checkpoint_stage(const struct apple_sftl_checkpoint *image, /* Check the whole block before the first program, including the space * past the new generation where a loader might find older records. */ - for (i = 0; i < capacity; i += 4) { + for (i = 0; i < capacity; i += layout.lpp) { ret = io->read(priv, start + i, &pages[0]); if (ret) goto out; - if (memchr_inv(&pages[0], 0xff, sizeof(*pages))) { + if (memchr_inv(pages[0].data, 0xff, layout.lpp * APPLE_SFTL_CHUNK) || + memchr_inv(pages[0].oob, 0xff, layout.lpp * APPLE_SFTL_META)) { ret = -EBADMSG; goto out; } } - for (i = 0; i < image->count; i += 4) { - /* 13G36 samples both providers after the one-page base, before - * formatting the statistics page. Keep these samples in the GC's - * expected image for the independent readback comparison. + for (i = 0; i < image->count; i += layout.lpp) { + /* Native cxtSave samples both providers after the one-page base, + * before formatting the statistics page. Keep these samples in the + * GC's expected image for the independent readback comparison. */ - if (gc && i == 4) { + if (gc && i == layout.lpp) { ret = apple_sftl_account_read_stats(gc->checkpoint, io, priv); if (ret) goto out; } - for (j = 0; j < 4; j++) { + memset(&pages[1], 0xff, sizeof(pages[1])); + for (j = 0; j < layout.lpp; j++) { memcpy(pages[1].data + j * APPLE_SFTL_CHUNK, image->records[i + j].data, APPLE_SFTL_CHUNK); memcpy(pages[1].oob + j * APPLE_SFTL_META, image->records[i + j].meta, APPLE_SFTL_META); } - ret = sftl_gc_program(&pages[0], &pages[1], io, priv, start + i); + ret = sftl_gc_program(&pages[0], &pages[1], io, priv, start + i, layout.lpp); if (ret) goto out; - for (j = 0; j < 4; j++) { + for (j = 0; j < layout.lpp; j++) { memcpy(copy->records[i + j].data, pages[0].data + j * APPLE_SFTL_CHUNK, APPLE_SFTL_CHUNK); memcpy(copy->records[i + j].meta, @@ -627,18 +674,22 @@ int apple_sftl_gc_relocate(struct apple_sftl_gc *gc, { struct apple_sftl_gc_page *pages, *src, *dst; u32 i, offset, slot = 0, cached = U32_MAX, repeat, address; + u32 lpp, mask; u64 sequence; int ret = 0; if (!gc || gc->stage != APPLE_SFTL_GC_COPY || gc->copied || !io || !io->read || !io->program) return -EINVAL; + lpp = gc->layout.lpp; + mask = lpp - 1; pages = kvmalloc(2 * sizeof(*pages), GFP_NOIO); if (!pages) return -ENOMEM; src = &pages[0]; dst = &pages[1]; - address = gc->destination * APPLE_SFTL_STRIDE; + memset(dst, 0xff, sizeof(*dst)); + address = gc->destination * gc->layout.stride; sequence = gc->copied_sequence; repeat = 1 + gc->padding; for (i = 0; i < gc->nr_copies; i++) { @@ -650,13 +701,13 @@ int apple_sftl_gc_relocate(struct apple_sftl_gc *gc, u64 stamp; u32 user_epoch; - if (cached != (from & ~3U)) { - cached = from & ~3U; + if (cached != (from & ~mask)) { + cached = from & ~mask; ret = io->read(priv, cached, src); if (ret) goto failed; } - meta = src->oob + (from & 3) * APPLE_SFTL_META; + meta = src->oob + (from & mask) * APPLE_SFTL_META; stamp = get_unaligned_le64(meta) >> 16; user_epoch = get_unaligned_le32(meta + 12); /* Only rewrite ordinary unflagged data in its native class. */ @@ -671,19 +722,19 @@ int apple_sftl_gc_relocate(struct apple_sftl_gc *gc, u8 *out = dst->oob + slot * APPLE_SFTL_META; memcpy(dst->data + slot * APPLE_SFTL_CHUNK, - src->data + (from & 3) * APPLE_SFTL_CHUNK, + src->data + (from & mask) * APPLE_SFTL_CHUNK, APPLE_SFTL_CHUNK); sequence++; put_unaligned_le64(sequence << 16 | 1, out); put_unaligned_le32(key, out + 8); put_unaligned_le32(user_epoch, out + 12); - if (++slot == 4) { - ret = sftl_gc_program(src, dst, io, priv, address); + if (++slot == lpp) { + ret = sftl_gc_program(src, dst, io, priv, address, lpp); if (ret) goto failed; cached = U32_MAX; slot = 0; - address += 4; + address += lpp; } } while (--repeat); repeat = 1; @@ -810,7 +861,8 @@ int apple_sftl_gc_checkpoint_done(struct apple_sftl_gc *gc, EXPORT_SYMBOL_GPL(apple_sftl_gc_checkpoint_done); static bool sftl_maps_equal(const struct apple_sftl_snapshot *expected, - const struct apple_sftl_snapshot *recovered) + const struct apple_sftl_snapshot *recovered, + u32 logical_pages) { u32 wanted = 0, found = 0, offset = 0, length; @@ -834,7 +886,7 @@ static bool sftl_maps_equal(const struct apple_sftl_snapshot *expected, if (offset == b->logical + b->length) found++; } - return offset == APPLE_SFTL_LOGICAL_PAGES; + return offset == logical_pages; } /* @@ -847,16 +899,20 @@ int apple_sftl_gc_plan_retired(const struct apple_sftl_snapshot *active, const struct apple_sftl_gc_block *victim, u64 checkpoint_start) { + struct apple_sftl_layout active_layout, cold_layout; u32 i, first; int ret; - ret = sftl_check_map(active); + ret = sftl_check_map(active, &active_layout); if (ret) return ret; - ret = sftl_check_map(cold); + ret = sftl_check_map(cold, &cold_layout); if (ret) return ret; - if (!sftl_gc_block_valid(victim) || !victim->closed || victim->blank || + if (active_layout.subblocks != cold_layout.subblocks || + active_layout.lpp != cold_layout.lpp) + return -EBADMSG; + if (!sftl_gc_block_valid(victim, &cold_layout) || !victim->closed || victim->blank || !checkpoint_start || checkpoint_start > cold->checkpoint_sequence || victim->last_sequence >= checkpoint_start) return -EOPNOTSUPP; @@ -864,14 +920,14 @@ int apple_sftl_gc_plan_retired(const struct apple_sftl_snapshot *active, active->checkpoint_sequence != cold->checkpoint_sequence || active->journal_sequence != active->sequence || cold->journal_sequence != cold->sequence || - !sftl_maps_equal(active, cold)) + !sftl_maps_equal(active, cold, cold_layout.logical_pages)) return -EBADMSG; - first = victim->block * APPLE_SFTL_STRIDE; + first = victim->block * cold_layout.stride; for (i = 0; i < cold->nr_runs; i++) { const struct apple_sftl_extent *run = &cold->runs[i]; if (run->provider != APPLE_SFTL_NO_ADDRESS && - sftl_overlap(run->provider, run->length, first, first + APPLE_SFTL_STRIDE)) + sftl_overlap(run->provider, run->length, first, first + cold_layout.stride)) return -EOPNOTSUPP; } return 0; @@ -880,13 +936,18 @@ int apple_sftl_gc_plan_retired(const struct apple_sftl_snapshot *active, int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, const struct apple_sftl_snapshot *recovered) { + struct apple_sftl_layout layout; int ret; if (!gc || (gc->stage != APPLE_SFTL_GC_RECOVERY && gc->stage != APPLE_SFTL_GC_VERIFIED)) return -EINVAL; - ret = sftl_check_map(recovered); + ret = sftl_check_map(recovered, &layout); if (ret) goto failed; + if (layout.subblocks != gc->layout.subblocks || layout.lpp != gc->layout.lpp) { + ret = -EBADMSG; + goto failed; + } if (recovered->checkpoint_sequence != gc->checkpoint->sequence || recovered->sequence != gc->checkpoint->sequence || recovered->journal_sequence != recovered->sequence || @@ -894,7 +955,7 @@ int apple_sftl_gc_recovered(struct apple_sftl_gc *gc, ret = -EBADMSG; goto failed; } - if (!sftl_maps_equal(&gc->snapshot, recovered)) { + if (!sftl_maps_equal(&gc->snapshot, recovered, gc->layout.logical_pages)) { ret = -EBADMSG; goto failed; } diff --git a/drivers/mtd/apple-sftl-reclaim.h b/drivers/mtd/apple-sftl-reclaim.h index a13ff96b9755b6..b2e3dbdc47d8d7 100644 --- a/drivers/mtd/apple-sftl-reclaim.h +++ b/drivers/mtd/apple-sftl-reclaim.h @@ -7,8 +7,11 @@ #define APPLE_SFTL_CHUNK 4096U #define APPLE_SFTL_META 16U #define APPLE_SFTL_NATIVE_MAP_BATCH 64U +#define APPLE_SFTL_64GB_8K_MAP_BATCH 16U +#define APPLE_SFTL_64GB_8K_MAP_MIN 32U #define APPLE_SFTL_STRIDE 8192U #define APPLE_SFTL_SUBBLOCKS 2086U +#define APPLE_SFTL_64GB_8K_SUBBLOCKS 2057U #define APPLE_SFTL_LOGICAL_PAGES 15625016U #define APPLE_SFTL_NO_ADDRESS ((u32)~0U) @@ -28,6 +31,8 @@ struct apple_sftl_snapshot { struct apple_sftl_extent *runs; struct apple_sftl_context_record *opaque; u32 nr_runs, nr_opaque; + u32 subblocks; /* 0 defaults to auto-detect / APPLE_SFTL_SUBBLOCKS */ + u8 lpp; /* 0 defaults to auto-detect / 4 */ u64 sequence, checkpoint_sequence, journal_sequence; }; diff --git a/drivers/mtd/apple-sftl-test-fixture.h b/drivers/mtd/apple-sftl-test-fixture.h index 16d65b6a2db7fa..ce932cf7a3fe70 100644 --- a/drivers/mtd/apple-sftl-test-fixture.h +++ b/drivers/mtd/apple-sftl-test-fixture.h @@ -47,4 +47,49 @@ static inline void apple_sftl_test_native_prefix(struct apple_sftl_context_recor pos += 8 + words * 4; } } + +/* Populate the measured 64 GB PPN 1.3 (8 KiB, lpp=2, 2057 subblocks) native + * layout in 24 zeroed synthetic records. + */ +static inline void apple_sftl_test_native_prefix_64gb_8k(struct apple_sftl_context_record *records) +{ + static const struct { + u8 subtype, chunks; + u16 entries; + } groups[] = { { 1, 2, 0 }, { 2, 2, 0 }, { 3, 6, 512 }, + { 5, 10, 256 }, { 6, 4, 1024 } }; + u32 i, pos, j, cursor = 0; + u8 *stats; + + for (i = 0; i < ARRAY_SIZE(groups); i++) { + for (j = 0; j < groups[i].chunks; j++) { + u8 *meta = records[cursor++].meta; + u32 start = j * groups[i].entries; + u16 n = start >= APPLE_SFTL_64GB_8K_SUBBLOCKS ? 0 : + min_t(u32, groups[i].entries, + APPLE_SFTL_64GB_8K_SUBBLOCKS - start); + + meta[0] = 0x1f; + meta[1] = groups[i].subtype; + put_unaligned_le16(start, meta + 8); + put_unaligned_le16(n, meta + 10); + put_unaligned_le32(U32_MAX, meta + 12); + } + } + put_unaligned_le32(2, records[0].data); + put_unaligned_le32(802, records[0].data + 4); + put_unaligned_le32(830, records[0].data + 8); + put_unaligned_le32(APPLE_SFTL_64GB_8K_SUBBLOCKS, + records[1].data + APPLE_SFTL_CHUNK - 4); + stats = records[2].data; + put_unaligned_le32(45, stats); + pos = 4; + for (i = 0; i < 45; i++) { + u32 words = i < 43 ? 2 : (i == 43 ? 22 : 116); + + put_unaligned_le32(i < 43 ? i + 1 : (i == 43 ? 255 : 254), stats + pos); + put_unaligned_le32(words, stats + pos + 4); + pos += 8 + words * 4; + } +} #endif diff --git a/drivers/mtd/apple-sftl-test.c b/drivers/mtd/apple-sftl-test.c index a266329fe5a417..811a5dfd9c6d48 100644 --- a/drivers/mtd/apple-sftl-test.c +++ b/drivers/mtd/apple-sftl-test.c @@ -4379,6 +4379,383 @@ static void sftl_test_16gb_context_rules(struct kunit *test) apple_sftl_free(sftl); } +/* ---- Synthetic 64 GB PPN 1.3 (j2a, 8 KiB, lpp=2, 2057 subblocks) tests ---- */ + +#define J2A_PAGE_SIZE 8192U +#define J2A_OOB_SIZE 32U +#define J2A_LPP 2U +#define J2A_SUBBLOCKS 2057U +#define J2A_VFL_BLOCK 2061U +#define J2A_CTX_BLOCK 802U +#define J2A_CTX_SPARE 830U +#define J2A_CAND_A 10U +#define J2A_CAND_B 11U +#define J2A_CTX_RECORDS 56U /* 24 opaque + 32 map_min */ +#define J2A_CTX_FIRST_SEQ 1000ULL +#define J2A_CTX_LAST_SEQ (J2A_CTX_FIRST_SEQ + J2A_CTX_RECORDS - 1) + +struct j2a_page { + u8 data[J2A_PAGE_SIZE]; + u8 oob[J2A_OOB_SIZE]; +}; + +struct j2a_fixture { + struct kunit *test; + struct apple_ppn provider; + struct apple_ppn_ops ops; + struct apple_sftl_context_record ctx[J2A_CTX_RECORDS]; + struct j2a_page *written[2][256]; /* candidate blocks 10 and 11, lane 0 only */ + unsigned int programs; + unsigned int fail_program; + unsigned int erases; + unsigned int last_erased_block; +}; + +static int j2a_vfl_read(unsigned int lane, unsigned int page, + u8 *data, u8 *oob, u8 *status) +{ + unsigned int i, group = lane / 4; + u32 origin = group ? 0xffffff00U : 0xfffffe00U; + + if (lane >= 8) + return 0; + if (!page || page == 17) { + memset(data, 0, J2A_PAGE_SIZE); + put_unaligned_le16(16, data + 10); + put_unaligned_le16(J2A_SUBBLOCKS, data + 12); + put_unaligned_le16(J2A_SUBBLOCKS, data + 14); + for (i = 0; i < 8; i++) { + put_unaligned_le16(J2A_VFL_BLOCK, data + 0x100 + i * 4); + data[0x102 + i * 4] = i; + data[0x103 + i * 4] = 0; + } + for (i = 0; i < 16; i++) + put_unaligned_le16((group << 15) | (i + 1), data + 0x200 + i * 32); + memset(oob, 0, J2A_OOB_SIZE); + oob[0] = 0x20; + oob[1] = 0xff; + oob[2] = 0xff; + put_unaligned_le32(origin - page, oob + 4); + *status = 0x40; + return 0; + } + if (page <= 16) { + unsigned int tbl_lane = page - 1; + + memset(data, 0xff, J2A_PAGE_SIZE); + /* All 16 lanes active on context blocks; lane 0 only on candidate blocks 10 & 11. */ + put_unaligned_le16(0, data + J2A_CTX_BLOCK * 2); + put_unaligned_le16(0, data + J2A_CTX_SPARE * 2); + if (!tbl_lane) { + put_unaligned_le16(0, data + J2A_CAND_A * 2); + put_unaligned_le16(0, data + J2A_CAND_B * 2); + } + memset(oob, 0, J2A_OOB_SIZE); + oob[0] = 0x20; + oob[1] = tbl_lane; + put_unaligned_le32(origin - page, oob + 4); + *status = 0x40; + } + return 0; +} + +static int j2a_read_page(void *priv, unsigned int bus, unsigned int cau, + unsigned int block, unsigned int page, bool context, + u8 *data, u8 *oob, u8 *status) +{ + struct j2a_fixture *f = priv; + unsigned int lane = cau * 2 + bus; + + memset(data, 0xff, J2A_PAGE_SIZE); + memset(oob, 0xff, J2A_OOB_SIZE); + *status = 0x49; + if (context) { + if (block == J2A_VFL_BLOCK) + return j2a_vfl_read(lane, page, data, oob, status); + return 0; + } + if (block == J2A_CTX_BLOCK) { + u32 pos = page * 16 + lane; + u32 rec = pos * J2A_LPP; + + if (rec < J2A_CTX_RECORDS) { + u32 c; + + for (c = 0; c < J2A_LPP; c++) { + memcpy(data + c * 4096, f->ctx[rec + c].data, 4096); + memcpy(oob + c * 16, f->ctx[rec + c].meta, 16); + } + *status = 0x40; + } + return 0; + } + if ((block == J2A_CAND_A || block == J2A_CAND_B) && !lane && page < 256) { + struct j2a_page *p = f->written[block - J2A_CAND_A][page]; + + if (p) { + memcpy(data, p->data, J2A_PAGE_SIZE); + memcpy(oob, p->oob, J2A_OOB_SIZE); + *status = 0x40; + } + } + return 0; +} + +static int j2a_program_page(void *priv, unsigned int bus, unsigned int cau, + unsigned int block, unsigned int page, + const u8 *data, const u8 *oob) +{ + struct j2a_fixture *f = priv; + unsigned int lane = cau * 2 + bus; + struct j2a_page *p; + + if ((block != J2A_CAND_A && block != J2A_CAND_B) || lane || page >= 256) + return -EINVAL; + if (f->written[block - J2A_CAND_A][page]) + return -EEXIST; + f->programs++; + if (f->fail_program && f->programs == f->fail_program) + return -EIO; + p = kunit_kmalloc(f->test, sizeof(*p), GFP_KERNEL); + if (!p) + return -ENOMEM; + memcpy(p->data, data, J2A_PAGE_SIZE); + memcpy(p->oob, oob, J2A_OOB_SIZE); + f->written[block - J2A_CAND_A][page] = p; + return 0; +} + +static int j2a_erase_block(void *priv, unsigned int bus, unsigned int cau, + unsigned int block) +{ + struct j2a_fixture *f = priv; + unsigned int lane = cau * 2 + bus; + + if ((block != J2A_CAND_A && block != J2A_CAND_B) || lane) + return -EINVAL; + memset(f->written[block - J2A_CAND_A], 0, sizeof(f->written[0])); + f->erases++; + f->last_erased_block = block; + return 0; +} + +static struct j2a_fixture *j2a_fixture_init(struct kunit *test) +{ + struct j2a_fixture *f = kunit_kzalloc(test, sizeof(*f), GFP_KERNEL); + u32 i; + + if (!f) + return NULL; + f->test = test; + apple_sftl_test_native_prefix_64gb_8k(f->ctx); + /* Mark context blocks 802, 830 as SFTL_CONTEXT (7), candidates 10 & 11 as CLEAN_FREE (1). */ + f->ctx[4 + J2A_CTX_BLOCK / 512].data[(J2A_CTX_BLOCK % 512) * 8] = 7; + f->ctx[4 + J2A_CTX_SPARE / 512].data[(J2A_CTX_SPARE % 512) * 8] = 7; + f->ctx[4].data[J2A_CAND_A * 8] = 1; + f->ctx[4].data[J2A_CAND_B * 8] = 1; + for (i = 0; i < 24; i++) { + u64 seq = J2A_CTX_FIRST_SEQ + i; + u8 *m = f->ctx[i].meta; + + put_unaligned_le64(seq << 16 | (u16)m[1] << 8 | 0x1f, m); + } + for (i = 24; i < J2A_CTX_RECORDS; i++) { + u64 seq = J2A_CTX_FIRST_SEQ + i; + + memset(f->ctx[i].data, 0xff, 4096); + apple_sftl_context_meta(f->ctx[i].meta, 4, seq, 0, 0); + } + /* Single range covering all 15,625,016 logical pages as unmapped (0x1ffffff). */ + put_unaligned_le32(0, f->ctx[24].data); + put_unaligned_le32(0xfffffff0U, f->ctx[24].data + 4); + put_unaligned_le32(0x1ffffffU, f->ctx[24].data + 8); + put_unaligned_le32(TEST_LOGICAL_PAGES, f->ctx[24].data + 12); + + f->ops.read_page = j2a_read_page; + f->ops.program_page = j2a_program_page; + f->provider = (struct apple_ppn) { + .ops = &f->ops, .priv = f, + .buses = 2, .caus = 8, .blocks = 2064, .pages = 256, + .page_size = J2A_PAGE_SIZE, .oob_size = J2A_OOB_SIZE, + }; + return f; +} + +static void sftl_test_64gb_8k_load_and_write(struct kunit *test) +{ + struct j2a_fixture *f = j2a_fixture_init(test); + struct apple_sftl *sftl = NULL; + struct apple_sftl_info info; + u8 *wbuf = kunit_kmalloc(test, J2A_PAGE_SIZE, GFP_KERNEL); + u8 *rbuf = kunit_kmalloc(test, 4096, GFP_KERNEL); + + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, wbuf); + KUNIT_ASSERT_NOT_NULL(test, rbuf); + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.lpp, (u8)2); + KUNIT_EXPECT_TRUE(test, info.fenced_write); + KUNIT_EXPECT_EQ(test, info.disk_pages, 15625000U); + KUNIT_EXPECT_EQ(test, info.sequence, J2A_CTX_LAST_SEQ); + KUNIT_EXPECT_EQ(test, info.read_only, !IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE)); + + memset(wbuf, 0x3c, 4096); + memset(wbuf + 4096, 0x5a, 4096); + /* Reject > lpp chunks regardless of write config. */ + KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, 100, wbuf, 3), -EINVAL); + + if (!IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE)) { + KUNIT_EXPECT_EQ(test, apple_sftl_write(sftl, 100, wbuf, 1), -EROFS); + KUNIT_EXPECT_EQ(test, f->programs, 0U); + apple_sftl_free(sftl); + return; + } + /* Write 1 chunk (repeated into 2nd slot) and 2 chunks, verify live + cold reload! */ + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 100, wbuf, 1), 0); + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 101, wbuf, 2), 0); + KUNIT_EXPECT_EQ(test, f->programs, 2U); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 100, rbuf), 0); + KUNIT_EXPECT_MEMEQ(test, rbuf, wbuf, 4096); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 102, rbuf), 0); + KUNIT_EXPECT_MEMEQ(test, rbuf, wbuf + 4096, 4096); + apple_sftl_free(sftl); + + sftl = NULL; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + apple_sftl_get_info(sftl, &info); + KUNIT_EXPECT_EQ(test, info.sequence, J2A_CTX_LAST_SEQ + 4); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 100, rbuf), 0); + KUNIT_EXPECT_MEMEQ(test, rbuf, wbuf, 4096); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 101, rbuf), 0); + KUNIT_EXPECT_MEMEQ(test, rbuf, wbuf, 4096); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 102, rbuf), 0); + KUNIT_EXPECT_MEMEQ(test, rbuf, wbuf + 4096, 4096); + apple_sftl_free(sftl); +} + +static void sftl_test_64gb_8k_close_and_reload(struct kunit *test) +{ + struct j2a_fixture *f; + struct apple_sftl *sftl = NULL; + u8 *wbuf, *rbuf; + u32 p; + + if (!IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE)) + kunit_skip(test, "requires CONFIG_MTD_APPLE_SFTL_J2A_WRITE"); + f = j2a_fixture_init(test); + wbuf = kunit_kmalloc(test, J2A_PAGE_SIZE, GFP_KERNEL); + rbuf = kunit_kmalloc(test, 4096, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, wbuf); + KUNIT_ASSERT_NOT_NULL(test, rbuf); + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + + /* 1 active lane = 256 pages = 512 chunks; 480 data chunks (240 pages) + 32 BTOC chunks (16 pages). */ + for (p = 0; p < 240; p++) { + memset(wbuf, (u8)(p & 0xff), 4096); + memset(wbuf + 4096, (u8)((p ^ 0x55) & 0xff), 4096); + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 200 + p * 2, wbuf, 2), 0); + } + /* Exactly 240 data programs + 16 BTOC footer programs = 256 pages in block 10! */ + KUNIT_EXPECT_EQ(test, f->programs, 256U); + KUNIT_ASSERT_NOT_NULL(test, f->written[0][255]); + KUNIT_EXPECT_EQ(test, f->written[0][255]->oob[0], (u8)0x1c); + KUNIT_EXPECT_EQ(test, get_unaligned_le16(f->written[0][255]->oob + 12), 32); + apple_sftl_free(sftl); + + /* Cold reload parses the 32-chunk BTOC footer and recovers all 480 logical pages! */ + sftl = NULL; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + for (p = 0; p < 240; p += 37) { + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 200 + p * 2, rbuf), 0); + KUNIT_EXPECT_EQ(test, rbuf[0], (u8)(p & 0xff)); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 200 + p * 2 + 1, rbuf), 0); + KUNIT_EXPECT_EQ(test, rbuf[0], (u8)((p ^ 0x55) & 0xff)); + } + apple_sftl_free(sftl); +} + +static void sftl_test_64gb_8k_program_failure_evacuate(struct kunit *test) +{ + struct j2a_fixture *f; + struct apple_sftl *sftl = NULL; + u8 *wbuf, *rbuf; + + if (!IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE)) + kunit_skip(test, "requires CONFIG_MTD_APPLE_SFTL_J2A_WRITE"); + f = j2a_fixture_init(test); + wbuf = kunit_kmalloc(test, J2A_PAGE_SIZE, GFP_KERNEL); + rbuf = kunit_kmalloc(test, 4096, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, wbuf); + KUNIT_ASSERT_NOT_NULL(test, rbuf); + f->fail_program = 2; /* Fail 2nd program on block 10 -> give up & evacuate to block 11! */ + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + + memset(wbuf, 0x11, 4096); + memset(wbuf + 4096, 0x22, 4096); + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 500, wbuf, 2), 0); + memset(wbuf, 0x33, 4096); + memset(wbuf + 4096, 0x44, 4096); + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 502, wbuf, 2), 0); + /* Block 11 now holds relocated LBAs 500..501 and newly written 502..503! */ + KUNIT_ASSERT_NOT_NULL(test, f->written[1][0]); + KUNIT_ASSERT_NOT_NULL(test, f->written[1][1]); + apple_sftl_free(sftl); + + sftl = NULL; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 500, rbuf), 0); + KUNIT_EXPECT_EQ(test, rbuf[0], (u8)0x11); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 501, rbuf), 0); + KUNIT_EXPECT_EQ(test, rbuf[0], (u8)0x22); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 502, rbuf), 0); + KUNIT_EXPECT_EQ(test, rbuf[0], (u8)0x33); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 503, rbuf), 0); + KUNIT_EXPECT_EQ(test, rbuf[0], (u8)0x44); + apple_sftl_free(sftl); +} + +static void sftl_test_64gb_8k_reuse_blank_dirty(struct kunit *test) +{ + struct j2a_fixture *f; + struct apple_sftl *sftl = NULL; + u8 *wbuf, *rbuf; + + if (!IS_ENABLED(CONFIG_MTD_APPLE_SFTL_J2A_WRITE)) + kunit_skip(test, "requires CONFIG_MTD_APPLE_SFTL_J2A_WRITE"); + f = j2a_fixture_init(test); + wbuf = kunit_kmalloc(test, J2A_PAGE_SIZE, GFP_KERNEL); + rbuf = kunit_kmalloc(test, 4096, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, f); + KUNIT_ASSERT_NOT_NULL(test, wbuf); + KUNIT_ASSERT_NOT_NULL(test, rbuf); + /* Keep block 10 CLEAN_FREE (1) and mark block 11 blank DIRTY_FREE (9) with wear 5. */ + f->ctx[4].data[J2A_CAND_B * 8] = 9; + put_unaligned_le16(5, f->ctx[4].data + J2A_CAND_B * 8 + 6); + f->ops.erase_block = j2a_erase_block; + + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + memset(wbuf, 0xa5, 4096); + memset(wbuf + 4096, 0x5a, 4096); + KUNIT_ASSERT_EQ(test, apple_sftl_write(sftl, 700, wbuf, 2), 0); + /* Blank DIRTY_FREE block 11 must be erased & used, leaving CLEAN_FREE block 10 untouched. */ + KUNIT_EXPECT_EQ(test, f->erases, 1U); + KUNIT_EXPECT_EQ(test, f->last_erased_block, J2A_CAND_B); + KUNIT_EXPECT_NULL(test, f->written[0][0]); + KUNIT_ASSERT_NOT_NULL(test, f->written[1][0]); + apple_sftl_free(sftl); + + sftl = NULL; + KUNIT_ASSERT_EQ(test, apple_sftl_load(&f->provider, &sftl), 0); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 700, rbuf), 0); + KUNIT_EXPECT_EQ(test, rbuf[0], (u8)0xa5); + KUNIT_ASSERT_EQ(test, apple_sftl_read(sftl, 701, rbuf), 0); + KUNIT_EXPECT_EQ(test, rbuf[0], (u8)0x5a); + apple_sftl_free(sftl); +} + static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_rotate_mounted), KUNIT_CASE(sftl_test_vfl_rotation_cuts), @@ -4454,6 +4831,10 @@ static struct kunit_case apple_sftl_cases[] = { KUNIT_CASE(sftl_test_16gb_trim_flag1), KUNIT_CASE(sftl_test_16gb_closed_trim_flag1), KUNIT_CASE(sftl_test_16gb_hidden_tail), + KUNIT_CASE(sftl_test_64gb_8k_load_and_write), + KUNIT_CASE(sftl_test_64gb_8k_close_and_reload), + KUNIT_CASE(sftl_test_64gb_8k_program_failure_evacuate), + KUNIT_CASE(sftl_test_64gb_8k_reuse_blank_dirty), {} }; diff --git a/drivers/mtd/apple-sftl.h b/drivers/mtd/apple-sftl.h index 6c16a6ae6fbdb8..03a02c22ff92de 100644 --- a/drivers/mtd/apple-sftl.h +++ b/drivers/mtd/apple-sftl.h @@ -54,6 +54,8 @@ void apple_sftl_set_checkpoint_ops(struct apple_sftl *sftl, struct apple_sftl_info { bool read_only; + bool fenced_write; + u8 lpp; u32 logical_pages; u32 disk_pages; u32 nr_runs; diff --git a/drivers/mtd/devices/mtd-apple-h2fmi-test.c b/drivers/mtd/devices/mtd-apple-h2fmi-test.c index a8434b08c10af0..ddc3f647c08e5e 100644 --- a/drivers/mtd/devices/mtd-apple-h2fmi-test.c +++ b/drivers/mtd/devices/mtd-apple-h2fmi-test.c @@ -287,6 +287,72 @@ static void h2fmi_sdr_facts_test(struct kunit *test) KUNIT_EXPECT_EQ(test, H2FMI_CHUNKS * H2FMI_CHUNK_RAW, H2FMI_PAGE_RAW); } +static void h2fmi_ppn13_64gb_test(struct kunit *test) +{ + static const u32 ppn13_64gb[] = { + 4, 2, 2064, 12, 256, 128, 8, 1, 2, 8224, + }; + static const u8 id13[H2FMI_ID_SIZE] = { 'P', 'P', 'N', 1, 3, 0, 0, 0 }; + static const u8 id15[H2FMI_ID_SIZE] = { 'P', 'P', 'N', 1, 5, 5, 0, 0 }; + u8 params[H2FMI_PARAMS_SIZE]; + struct h2fmi_geometry g; + u8 *raw, *data_in, *data_out, oob_in[H2FMI_J2A_PAGE_OOB], oob_out[H2FMI_J2A_PAGE_OOB]; + u32 row, addr0, addr1, i; + + KUNIT_EXPECT_TRUE(test, h2fmi_ppn13_id(id13)); + KUNIT_EXPECT_FALSE(test, h2fmi_ppn13_id(id15)); + + h2fmi_test_params(params, ppn13_64gb); + KUNIT_ASSERT_EQ(test, h2fmi_parse_ppn13_64gb_geometry(params, sizeof(params), &g), 0); + KUNIT_EXPECT_EQ(test, g.caus, 4U); + KUNIT_EXPECT_EQ(test, g.blocks, 2064U); + KUNIT_EXPECT_EQ(test, g.pages, 256U); + KUNIT_EXPECT_EQ(test, g.slc_pages, 128U); + KUNIT_EXPECT_TRUE(test, h2fmi_erase_geometry_valid(&g)); + + /* Row fits in 3 bytes (22 bits = 8 page + 12 block + 2 cau, bit 22 = SLC). */ + KUNIT_ASSERT_EQ(test, h2fmi_page_row(&g, 3, 2063, 255, &row), 0); + KUNIT_EXPECT_EQ(test, row, 0x380fffU); + KUNIT_EXPECT_LT(test, row, 1U << (8 * H2FMI_PPN13_ROW_BYTES)); + KUNIT_ASSERT_EQ(test, h2fmi_slc_row(&g, 0, 2061, 0, &row), 0); + KUNIT_EXPECT_EQ(test, row, 0x480d00U); + KUNIT_EXPECT_LT(test, row, 1U << (8 * H2FMI_PPN13_ROW_BYTES)); + + /* Erase protects block 0 and bootstrap window 2032..2063 (incl. VFL block 2061!). */ + KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 0, 0, &row), -EPERM); + KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 0, 2032, &row), -EPERM); + KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 0, 2061, &row), -EPERM); + KUNIT_EXPECT_EQ(test, h2fmi_erase_row(&g, 3, 2063, &row), -EPERM); + KUNIT_ASSERT_EQ(test, h2fmi_erase_row(&g, 0, 1, &row), 0); + KUNIT_EXPECT_EQ(test, row, 0x100U); + KUNIT_ASSERT_EQ(test, h2fmi_erase_row(&g, 3, 2031, &row), 0); + KUNIT_EXPECT_EQ(test, row, 0x37ef00U); + + /* 7-byte full address: 8224 (0x2020) + 3-byte row. */ + h2fmi_full_address_raw(H2FMI_J2A_PAGE_RAW, 0x380fffU, &addr0, &addr1); + KUNIT_EXPECT_EQ(test, addr0, 0x2020U); + KUNIT_EXPECT_EQ(test, addr1, 0x380fffU); + + /* 2-chunk 8224 B raw split/join round-trip. */ + raw = kunit_kmalloc(test, H2FMI_J2A_PAGE_RAW, GFP_KERNEL); + data_in = kunit_kmalloc(test, H2FMI_J2A_PAGE_DATA, GFP_KERNEL); + data_out = kunit_kmalloc(test, H2FMI_J2A_PAGE_DATA, GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, raw); + KUNIT_ASSERT_NOT_NULL(test, data_in); + KUNIT_ASSERT_NOT_NULL(test, data_out); + for (i = 0; i < H2FMI_J2A_PAGE_DATA; i++) + data_in[i] = (u8)((i * 37 + 13) & 0xff); + for (i = 0; i < H2FMI_J2A_PAGE_OOB; i++) + oob_in[i] = (u8)(0xa0 + i); + h2fmi_join_chunks(raw, data_in, oob_in, H2FMI_J2A_CHUNKS); + KUNIT_EXPECT_MEMEQ(test, raw + H2FMI_CHUNK_LO, oob_in, H2FMI_CHUNK_META); + KUNIT_EXPECT_MEMEQ(test, raw + H2FMI_CHUNK_RAW + H2FMI_CHUNK_LO, + oob_in + H2FMI_CHUNK_META, H2FMI_CHUNK_META); + h2fmi_split_chunks(raw, data_out, oob_out, H2FMI_J2A_CHUNKS); + KUNIT_EXPECT_MEMEQ(test, data_out, data_in, H2FMI_J2A_PAGE_DATA); + KUNIT_EXPECT_MEMEQ(test, oob_out, oob_in, H2FMI_J2A_PAGE_OOB); +} + static struct kunit_case h2fmi_test_cases[] = { KUNIT_CASE(h2fmi_raw_write_ownership_test), KUNIT_CASE(h2fmi_geometry_test), @@ -298,6 +364,7 @@ static struct kunit_case h2fmi_test_cases[] = { KUNIT_CASE(h2fmi_slc_row_test), KUNIT_CASE(h2fmi_program_status_test), KUNIT_CASE(h2fmi_sdr_facts_test), + KUNIT_CASE(h2fmi_ppn13_64gb_test), {} }; diff --git a/drivers/mtd/devices/mtd-apple-h2fmi.h b/drivers/mtd/devices/mtd-apple-h2fmi.h index 0639a2b0d95ac0..5ab7a46db0d55a 100644 --- a/drivers/mtd/devices/mtd-apple-h2fmi.h +++ b/drivers/mtd/devices/mtd-apple-h2fmi.h @@ -11,6 +11,7 @@ #define H2FMI_PARAMS_SIZE 512 #define H2FMI_CHUNKS 4 +#define H2FMI_J2A_CHUNKS 2 #define H2FMI_CHUNK_LO 1024 #define H2FMI_CHUNK_META 16 #define H2FMI_CHUNK_HI 3072 @@ -19,6 +20,11 @@ #define H2FMI_PAGE_DATA 16384 #define H2FMI_PAGE_OOB 64 #define H2FMI_PAGE_SHIFT 14 +#define H2FMI_J2A_PAGE_RAW 8224 +#define H2FMI_J2A_PAGE_DATA 8192 +#define H2FMI_J2A_PAGE_OOB 32 +#define H2FMI_PPN13_ROW_BYTES 3 +#define H2FMI_PPN13_FULL_BYTES 7 #define PPN_ST_GOOD 0x40 #define PPN_ST_REFRESH 0x42 @@ -182,6 +188,91 @@ static inline bool h2fmi_ppn_id(const u8 *id) return !memcmp(id, "PPN", 3) && id[3] == 1 && id[4] == 5; } +/* iPad 3 (j2a) 64 GB packages report PPN 1.3 (3-byte row, 7-byte full addr). */ +static inline bool h2fmi_ppn13_id(const u8 *id) +{ + return !memcmp(id, "PPN", 3) && id[3] == 1 && id[4] == 3; +} + +/* Measured 64 GB PPN 1.3 package on iPad3,3 (j2a): 4 CAUs x 2064 blocks x + * 256 MLC / 128 SLC pages of 8224 raw bytes (2 x 4112 B chunks). + */ +static inline int h2fmi_parse_ppn13_64gb_geometry(const u8 *params, size_t len, + struct h2fmi_geometry *geometry) +{ + struct h2fmi_geometry g; + u32 cell_bits, cells, raw_bytes; + + if (len != H2FMI_PARAMS_SIZE || + memcmp(params, "PPN Device Info", sizeof("PPN Device Info") - 1)) + return -EINVAL; + g.caus = get_unaligned_le32(params + 0x10); + g.cau_bits = get_unaligned_le32(params + 0x14); + g.blocks = get_unaligned_le32(params + 0x18); + g.block_bits = get_unaligned_le32(params + 0x1c); + g.pages = get_unaligned_le32(params + 0x20); + g.slc_pages = get_unaligned_le32(params + 0x24); + g.page_bits = get_unaligned_le32(params + 0x28); + cell_bits = get_unaligned_le32(params + 0x2c); + cells = get_unaligned_le32(params + 0x30); + raw_bytes = get_unaligned_le32(params + 0x34); + if (raw_bytes != H2FMI_J2A_PAGE_RAW || cell_bits != 1 || cells != 2) + return -EOPNOTSUPP; + if (g.caus == 4 && g.cau_bits == 2 && g.blocks == 2064 && + g.block_bits == 12 && g.pages == 256 && g.slc_pages == 128 && + g.page_bits == 8) { + *geometry = g; + return 0; + } + return -EOPNOTSUPP; +} + +static inline void h2fmi_full_address_raw(u32 raw_bytes, u32 row, + u32 *addr0, u32 *addr1) +{ + *addr0 = raw_bytes & 0xffff; + *addr1 = row; +} + +static inline void h2fmi_split_chunks(const u8 *raw, u8 *data, u8 *oob, + unsigned int chunks) +{ + unsigned int c; + + for (c = 0; c < chunks; c++) { + const u8 *src = raw + c * H2FMI_CHUNK_RAW; + + if (data) { + u8 *dst = data + c * (H2FMI_CHUNK_LO + H2FMI_CHUNK_HI); + + memcpy(dst, src, H2FMI_CHUNK_LO); + memcpy(dst + H2FMI_CHUNK_LO, + src + H2FMI_CHUNK_LO + H2FMI_CHUNK_META, + H2FMI_CHUNK_HI); + } + if (oob) + memcpy(oob + c * H2FMI_CHUNK_META, + src + H2FMI_CHUNK_LO, H2FMI_CHUNK_META); + } +} + +static inline void h2fmi_join_chunks(u8 *raw, const u8 *data, const u8 *oob, + unsigned int chunks) +{ + unsigned int c; + + for (c = 0; c < chunks; c++) { + u8 *dst = raw + c * H2FMI_CHUNK_RAW; + const u8 *src = data + c * (H2FMI_CHUNK_LO + H2FMI_CHUNK_HI); + + memcpy(dst, src, H2FMI_CHUNK_LO); + memcpy(dst + H2FMI_CHUNK_LO, oob + c * H2FMI_CHUNK_META, + H2FMI_CHUNK_META); + memcpy(dst + H2FMI_CHUNK_LO + H2FMI_CHUNK_META, + src + H2FMI_CHUNK_LO, H2FMI_CHUNK_HI); + } +} + /* Feature 0x84 | cau << 8 returns one bit per block of that CAU. */ static inline u32 h2fmi_bad_block_bytes(const struct h2fmi_geometry *g) {