DiskOnChip G3 has eight free bytes in its 16-byte spare area. An OOB-only AUTO write of all eight bytes fails. A 16-byte request succeeds, but the driver stages and reports only the first eight bytes. Require one page's worth of spare bytes in the selected mode for a staged write. MEMWRITE supplies the data and OOB lengths separately: two data pages can have spare bytes for only the first. Require a data page for each supplied spare area, not a spare area for every data page. Reject partial spare areas and nonzero spare offsets instead of using the offset to index the caller's buffer. Use staged spare bytes only when no OOB buffer was supplied. MEMWRITE keeps its OOB buffer across chunks, so later data pages with no spare bytes use the normal zero-filled spare area and the requested ECC mode. Fixes: fb50b58e488d ("mtd: docg3: add write functions") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard --- drivers/mtd/devices/docg3.c | 119 ++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 83 deletions(-) diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c index 1f0b1b0f4a83..b72b2eab5a9a 100644 --- a/drivers/mtd/devices/docg3.c +++ b/drivers/mtd/devices/docg3.c @@ -1288,30 +1288,6 @@ static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf, return ret; } -/** - * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops - * @ops: the oob operations - * - * Returns 0 or 1 if success, -EINVAL if invalid oob mode - */ -static int doc_guess_autoecc(struct mtd_oob_ops *ops) -{ - int autoecc; - - switch (ops->mode) { - case MTD_OPS_PLACE_OOB: - case MTD_OPS_AUTO_OOB: - autoecc = 1; - break; - case MTD_OPS_RAW: - autoecc = 0; - break; - default: - autoecc = -EINVAL; - } - return autoecc; -} - /** * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes * @dst: the target 16 bytes OOB buffer @@ -1324,9 +1300,26 @@ static void doc_fill_autooob(u8 *dst, u8 *oobsrc) dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ]; } +static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops *ops) +{ + u32 avail = mtd_oobavail(mtd, ops); + + if (ops->mode != MTD_OPS_PLACE_OOB && ops->mode != MTD_OPS_AUTO_OOB && + ops->mode != MTD_OPS_RAW) + return false; + if (to % DOC_LAYOUT_PAGE_SIZE || ops->len % DOC_LAYOUT_PAGE_SIZE || + (ops->ooblen && (ops->ooboffs || ops->ooblen % avail))) + return false; + + if (!ops->len) + return ops->ooblen == avail; + + return ops->ooblen / avail <= ops->len / DOC_LAYOUT_PAGE_SIZE; +} + /** * doc_backup_oob - Backup OOB into docg3 structure - * @docg3: the device + * @mtd: flash device * @to: the page offset in the chip * @ops: the OOB size and buffer * @@ -1338,30 +1331,18 @@ static void doc_fill_autooob(u8 *dst, u8 *oobsrc) * * The only reliable way would be for userland to call doc_write_oob() with both * the page data _and_ the OOB area. - * - * Returns 0 if success, -EINVAL if ops content invalid */ -static int doc_backup_oob(struct docg3 *docg3, loff_t to, - struct mtd_oob_ops *ops) +static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops) { - int ooblen = ops->ooblen, autoecc; - - if (ooblen != DOC_LAYOUT_OOB_SIZE) - return -EINVAL; - autoecc = doc_guess_autoecc(ops); - if (autoecc < 0) - return autoecc; + struct docg3 *docg3 = mtd->priv; docg3->oob_write_ofs = to; - docg3->oob_autoecc = autoecc; - if (ops->mode == MTD_OPS_AUTO_OOB) { + docg3->oob_autoecc = ops->mode != MTD_OPS_RAW; + if (ops->mode == MTD_OPS_AUTO_OOB) doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf); - ops->oobretlen = 8; - } else { + else memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE); - ops->oobretlen = DOC_LAYOUT_OOB_SIZE; - } - return 0; + ops->oobretlen = ops->ooblen; } /** @@ -1372,12 +1353,12 @@ static int doc_backup_oob(struct docg3 *docg3, loff_t to, * @ops: the mtd oob structure * * Either write OOB data into a temporary buffer, for the subsequent write - * page. The provided OOB should be 16 bytes long. If a data buffer is provided - * as well, issue the page write. + * page. An OOB-only write supplies one page in the selected mode. If a data + * buffer is provided as well, issue the page write. * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will * still be filled in if asked for). * - * Returns 0 is successful, EINVAL if length is not 14 bytes + * Returns 0 on success or a negative error code. */ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, struct mtd_oob_ops *ops) @@ -1386,57 +1367,29 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, int ret, autoecc, oobdelta; u8 *oobbuf = ops->oobbuf; u8 *buf = ops->datbuf; - size_t len, ooblen; + size_t len = ops->len, ooblen = ops->ooblen; u8 oob[DOC_LAYOUT_OOB_SIZE]; - if (buf) - len = ops->len; - else - len = 0; - if (oobbuf) - ooblen = ops->ooblen; - else - ooblen = 0; - - if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB) - oobbuf += ops->ooboffs; - doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n", ofs, ops->mode, buf, len, oobbuf, ooblen); - switch (ops->mode) { - case MTD_OPS_PLACE_OOB: - case MTD_OPS_RAW: - oobdelta = mtd->oobsize; - break; - case MTD_OPS_AUTO_OOB: - oobdelta = mtd->oobavail; - break; - default: - return -EINVAL; - } - if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) || - (ofs % DOC_LAYOUT_PAGE_SIZE)) - return -EINVAL; - if (len && ooblen && - (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta)) + if (!doc_oob_write_valid(mtd, ofs, ops)) return -EINVAL; ops->oobretlen = 0; ops->retlen = 0; ret = 0; - if (len == 0 && ooblen == 0) - return -EINVAL; - if (len == 0 && ooblen > 0) - return doc_backup_oob(docg3, ofs, ops); + if (!len) { + doc_backup_oob(mtd, ofs, ops); + return 0; + } - autoecc = doc_guess_autoecc(ops); - if (autoecc < 0) - return autoecc; + oobdelta = mtd_oobavail(mtd, ops); + autoecc = ops->mode != MTD_OPS_RAW; mutex_lock(&docg3->cascade->lock); while (!ret && len > 0) { memset(oob, 0, sizeof(oob)); - if (ofs == docg3->oob_write_ofs) + if (!ops->oobbuf && ofs == docg3->oob_write_ofs) memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE); else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB) doc_fill_autooob(oob, oobbuf); -- 2.53.0 A data-only write can reuse spare bytes from a page's previous program, even after its block pair has been erased. The slot also starts out looking valid for page zero, and a concurrent spare-only write can replace its contents while the page is being programmed. Start with an empty slot. Hold the cascade lock while staging or using the spare bytes and their ECC mode. Retire the slot after a successful program of the matching page and count only pages actually programmed. Invalidate the slot when issuing the erase command for its block pair. A timeout or a failure on one plane does not establish that both blocks are unchanged. If the reset fails before the command, or the erase never reaches that pair, keep the staged bytes. Fixes: fb50b58e488d ("mtd: docg3: add write functions") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard --- drivers/mtd/devices/docg3.c | 65 ++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 24 deletions(-) diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c index b72b2eab5a9a..06de6d43fe32 100644 --- a/drivers/mtd/devices/docg3.c +++ b/drivers/mtd/devices/docg3.c @@ -1133,13 +1133,15 @@ static int doc_write_erase_wait_status(struct docg3 *docg3) * @docg3: the device * @block0: the first block to erase (leftmost plane) * @block1: the second block to erase (rightmost plane) + * @clear_oob: whether the staged spare bytes belong to this block pair * * Erase both blocks, and return operation status * * Returns 0 if erase successful, -EIO if erase issue, -ETIMEOUT if chip not * ready for too long */ -static int doc_erase_block(struct docg3 *docg3, int block0, int block1) +static int doc_erase_block(struct docg3 *docg3, int block0, int block1, + bool clear_oob) { int ret, sector; @@ -1159,6 +1161,8 @@ static int doc_erase_block(struct docg3 *docg3, int block0, int block1) doc_setup_addr_sector(docg3, sector); doc_delay(docg3, 1); + if (clear_oob) + docg3->oob_write_ofs = -1; doc_flash_command(docg3, DOC_CMD_ERASECYCLE2); doc_delay(docg3, 2); @@ -1186,6 +1190,7 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info) struct docg3 *docg3 = mtd->priv; uint64_t len; int block0, block1, page, ret = 0, ofs = 0; + int staged_block = -1; doc_dbg("doc_erase(from=%lld, len=%lld\n", info->addr, info->len); @@ -1194,13 +1199,19 @@ static int doc_erase(struct mtd_info *mtd, struct erase_info *info) if (info->addr + info->len > mtd->size || page || ofs) return -EINVAL; + mutex_lock(&docg3->cascade->lock); + if (docg3->oob_write_ofs >= 0) + calc_block_sector(docg3->oob_write_ofs, &staged_block, &block1, + &page, &ofs, docg3->reliable); calc_block_sector(info->addr, &block0, &block1, &page, &ofs, docg3->reliable); - mutex_lock(&docg3->cascade->lock); doc_set_device_id(docg3, docg3->device_id); doc_set_reliable_mode(docg3); - for (len = info->len; !ret && len > 0; len -= mtd->erasesize) { - ret = doc_erase_block(docg3, block0, block1); + for (len = info->len; len > 0; len -= mtd->erasesize) { + ret = doc_erase_block(docg3, block0, block1, + block0 == staged_block); + if (ret) + break; block0 += 2; block1 += 2; } @@ -1323,25 +1334,23 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops * @to: the page offset in the chip * @ops: the OOB size and buffer * - * As the docg3 should write a page with its OOB in one pass, and some userland - * applications do write_oob() to setup the OOB and then write(), store the OOB - * into a temporary storage. This is very dangerous, as 2 concurrent - * applications could store an OOB, and then write their pages (which will - * result into one having its OOB corrupted). - * - * The only reliable way would be for userland to call doc_write_oob() with both - * the page data _and_ the OOB area. + * Some applications supply spare bytes before writing the page data. + * Another spare-only write can replace those bytes between the two calls. + * Callers that need the data and OOB to stay together can supply both in + * one write. */ static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops) { struct docg3 *docg3 = mtd->priv; + mutex_lock(&docg3->cascade->lock); docg3->oob_write_ofs = to; docg3->oob_autoecc = ops->mode != MTD_OPS_RAW; if (ops->mode == MTD_OPS_AUTO_OOB) doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf); else memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE); + mutex_unlock(&docg3->cascade->lock); ops->oobretlen = ops->ooblen; } @@ -1352,11 +1361,10 @@ static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops * * size * @ops: the mtd oob structure * - * Either write OOB data into a temporary buffer, for the subsequent write - * page. An OOB-only write supplies one page in the selected mode. If a data - * buffer is provided as well, issue the page write. - * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will - * still be filled in if asked for). + * An OOB-only write stages one page's spare bytes. Use them only for a + * matching write without an OOB buffer. MEMWRITE retains its OOB buffer + * in later chunks after the last spare byte; those pages use a zero-filled + * spare area and the requested ECC mode. * * Returns 0 on success or a negative error code. */ @@ -1387,15 +1395,23 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, autoecc = ops->mode != MTD_OPS_RAW; mutex_lock(&docg3->cascade->lock); - while (!ret && len > 0) { + while (len > 0) { + int page_autoecc = autoecc; + memset(oob, 0, sizeof(oob)); - if (!ops->oobbuf && ofs == docg3->oob_write_ofs) - memcpy(oob, docg3->oob_write_buf, DOC_LAYOUT_OOB_SIZE); - else if (ooblen > 0 && ops->mode == MTD_OPS_AUTO_OOB) + if (!ops->oobbuf && ofs == docg3->oob_write_ofs) { + memcpy(oob, docg3->oob_write_buf, sizeof(oob)); + page_autoecc = docg3->oob_autoecc; + } else if (ooblen && ops->mode == MTD_OPS_AUTO_OOB) { doc_fill_autooob(oob, oobbuf); - else if (ooblen > 0) - memcpy(oob, oobbuf, DOC_LAYOUT_OOB_SIZE); - ret = doc_write_page(docg3, ofs, buf, oob, autoecc); + } else if (ooblen) { + memcpy(oob, oobbuf, sizeof(oob)); + } + ret = doc_write_page(docg3, ofs, buf, oob, page_autoecc); + if (ret) + break; + if (ofs == docg3->oob_write_ofs) + docg3->oob_write_ofs = -1; ofs += DOC_LAYOUT_PAGE_SIZE; len -= DOC_LAYOUT_PAGE_SIZE; @@ -1730,6 +1746,7 @@ static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd) if (docg3->reliable == 2) mtd->erasesize /= 2; mtd->writebufsize = mtd->writesize = DOC_LAYOUT_PAGE_SIZE; + docg3->oob_write_ofs = -1; mtd->oobsize = DOC_LAYOUT_OOB_SIZE; mtd->_erase = doc_erase; mtd->_read_oob = doc_read_oob; -- 2.53.0 A full-page read can report a corrected error in the spare area while returning the wrong byte. The BCH codeword covers 512 data bytes and eight spare bytes, but all corrections are applied relative to the separate data buffer. A spare correction therefore writes past the page data instead of fixing the spare byte. Apply each correction to the appropriate buffer before copying the requested spare bytes. Leave the stored BCH parity outside the data correction range. Fixes: d13d19ece39f ("mtd: docg3: add ECC correction code") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard --- drivers/mtd/devices/docg3.c | 48 ++++++++++++++++++++++++--------------------- 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c index 06de6d43fe32..c9fdd84353d7 100644 --- a/drivers/mtd/devices/docg3.c +++ b/drivers/mtd/devices/docg3.c @@ -620,16 +620,14 @@ static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes) /** * doc_ecc_bch_fix_data - Fix if need be read data from flash * @docg3: the device - * @buf: the buffer of read data (512 + 7 + 1 bytes) + * @buf: the main data of the page + * @oob: the physical spare area of the page * @hwecc: the hardware calculated ECC. * It's in fact recv_ecc ^ calc_ecc, where recv_ecc was read from OOB * area data, and calc_ecc the ECC calculated by the hardware generator. * - * Checks if the received data matches the ECC, and if an error is detected, - * tries to fix the bit flips (at most 4) in the buffer buf. As the docg3 - * understands the (data, ecc, syndroms) in an inverted order in comparison to - * the BCH library, the function reverses the order of bits (ie. bit7 and bit0, - * bit6 and bit 1, ...) for all ECC data. + * BCH protects the main data and the first eight spare bytes. Apply each + * correction to the buffer containing the affected byte. * * The hardware ecc unit produces oob_ecc ^ calc_ecc. The kernel's bch * algorithm is used to decode this. However the hw operates on page @@ -637,10 +635,10 @@ static void doc_hamming_ecc_init(struct docg3 *docg3, int nb_bytes) * requiring that the bits be reversed on the result. Thanks to Ivan * Djelic for his analysis. * - * Returns number of fixed bits (0, 1, 2, 3, 4) or -EBADMSG if too many bit - * errors were detected and cannot be fixed. + * Return: The number of bit errors reported by the decoder, or -EBADMSG + * if the errors cannot be corrected. */ -static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc) +static int doc_ecc_bch_fix_data(struct docg3 *docg3, u8 *buf, u8 *oob, u8 *hwecc) { u8 ecc[DOC_ECC_BCH_SIZE]; int errorpos[DOC_ECC_BCH_T], i, numerrs; @@ -654,12 +652,15 @@ static int doc_ecc_bch_fix_data(struct docg3 *docg3, void *buf, u8 *hwecc) if (numerrs < 0) goto out; - for (i = 0; i < numerrs; i++) - errorpos[i] = (errorpos[i] & ~7) | (7 - (errorpos[i] & 7)); - for (i = 0; i < numerrs; i++) - if (errorpos[i] < DOC_ECC_BCH_COVERED_BYTES*8) - /* error is located in data, correct it */ - change_bit(errorpos[i], buf); + for (i = 0; i < numerrs; i++) { + unsigned int byte = errorpos[i] / 8; + u8 mask = BIT(7 - (errorpos[i] % 8)); + + if (byte < DOC_LAYOUT_PAGE_SIZE) + buf[byte] ^= mask; + else if (byte < DOC_ECC_BCH_COVERED_BYTES) + oob[byte - DOC_LAYOUT_PAGE_SIZE] ^= mask; + } out: doc_dbg("doc_ecc_bch_fix_data: flipped %d bits\n", numerrs); return numerrs; @@ -870,6 +871,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, u8 *oobbuf = ops->oobbuf; u8 *buf = ops->datbuf; size_t len, ooblen, nbdata, nboob; + u8 oob[DOC_LAYOUT_OOB_SIZE] __aligned(2); u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1; struct mtd_ecc_stats old_stats; int max_bitflips = 0; @@ -917,20 +919,20 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata - skip, NULL, 0, (skip + nbdata) % 2); - ret = doc_read_page_getbytes(docg3, nboob, oobbuf, 0, 0); + ret = doc_read_page_getbytes(docg3, nboob, oob, 0, 0); if (ret < nboob) goto err_in_read; doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob, - NULL, 0, nboob % 2); + oob + nboob, 0, nboob % 2); doc_get_bch_hw_ecc(docg3, hwecc); eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1); if (nboob >= DOC_LAYOUT_OOB_SIZE) { - doc_dbg("OOB - INFO: %*phC\n", 7, oobbuf); - doc_dbg("OOB - HAMMING: %02x\n", oobbuf[7]); - doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oobbuf + 8); - doc_dbg("OOB - UNUSED: %02x\n", oobbuf[15]); + doc_dbg("OOB - INFO: %*phC\n", 7, oob); + doc_dbg("OOB - HAMMING: %02x\n", oob[7]); + doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oob + 8); + doc_dbg("OOB - UNUSED: %02x\n", oob[15]); } doc_dbg("ECC checks: ECCConf1=%x\n", eccconf1); doc_dbg("ECC HW_ECC: %*phC\n", 7, hwecc); @@ -944,7 +946,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, (eccconf1 & DOC_ECCCONF1_PAGE_IS_WRITTEN) && (ops->mode != MTD_OPS_RAW) && (nbdata == DOC_LAYOUT_PAGE_SIZE)) { - ret = doc_ecc_bch_fix_data(docg3, buf, hwecc); + ret = doc_ecc_bch_fix_data(docg3, buf, oob, hwecc); if (ret < 0) { mtd->ecc_stats.failed++; ret = -EBADMSG; @@ -956,6 +958,8 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, } } + if (nboob) + memcpy(oobbuf, oob, nboob); doc_read_page_finish(docg3); ops->retlen += nbdata; ops->oobretlen += nboob; -- 2.53.0 An AUTO read of eight bytes should return the seven page-information bytes and the final free byte. DiskOnChip G3 instead requires reads in multiples of 16 and copies physical spare bytes, including ECC. PLACE reads also apply the spare offset to the destination instead of the page. Read the physical spare area into a local buffer. Keep the chip's read and drain phases, including the NOPs issued by an empty phase. Copy AUTO bytes from the free regions and PLACE or RAW bytes from the requested physical offset. Apply the offset only to the first page and reject unknown modes before accessing the chip. Fixes: efa2ca73a7bc ("mtd: Add DiskOnChip G3 support") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard --- drivers/mtd/devices/docg3.c | 52 ++++++++++++++++++++++++++++++--------------- 1 file changed, 35 insertions(+), 17 deletions(-) diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c index c9fdd84353d7..aa43f4b9862b 100644 --- a/drivers/mtd/devices/docg3.c +++ b/drivers/mtd/devices/docg3.c @@ -852,6 +852,12 @@ static void calc_block_sector(loff_t from, int *block0, int *block1, int *page, *ofs = 0; } +static bool doc_oob_mode_valid(unsigned int mode) +{ + return mode == MTD_OPS_PLACE_OOB || mode == MTD_OPS_AUTO_OOB || + mode == MTD_OPS_RAW; +} + /** * doc_read_oob - Read out of band bytes from flash * @mtd: the device @@ -871,6 +877,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, u8 *oobbuf = ops->oobbuf; u8 *buf = ops->datbuf; size_t len, ooblen, nbdata, nboob; + unsigned int ooboffs = ops->ooboffs, oobavail = mtd_oobavail(mtd, ops); u8 oob[DOC_LAYOUT_OOB_SIZE] __aligned(2); u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1; struct mtd_ecc_stats old_stats; @@ -885,12 +892,9 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, else ooblen = 0; - if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB) - oobbuf += ops->ooboffs; - doc_dbg("doc_read_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n", from, ops->mode, buf, len, oobbuf, ooblen); - if (ooblen % DOC_LAYOUT_OOB_SIZE) + if (!doc_oob_mode_valid(ops->mode) || (ooblen && ooboffs >= oobavail)) return -EINVAL; ops->oobretlen = 0; @@ -900,10 +904,12 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, mutex_lock(&docg3->cascade->lock); old_stats = mtd->ecc_stats; while (ret >= 0 && (len > 0 || ooblen > 0)) { + unsigned int spare_len; + calc_block_sector(from - skip, &block0, &block1, &page, &ofs, docg3->reliable); nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip); - nboob = min_t(size_t, ooblen, (size_t)DOC_LAYOUT_OOB_SIZE); + nboob = min_t(size_t, ooblen, oobavail - ooboffs); ret = doc_read_page_prepare(docg3, block0, block1, page, ofs); if (ret < 0) goto out; @@ -919,16 +925,16 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, doc_read_page_getbytes(docg3, DOC_LAYOUT_PAGE_SIZE - nbdata - skip, NULL, 0, (skip + nbdata) % 2); - ret = doc_read_page_getbytes(docg3, nboob, oob, 0, 0); - if (ret < nboob) - goto err_in_read; - doc_read_page_getbytes(docg3, DOC_LAYOUT_OOB_SIZE - nboob, - oob + nboob, 0, nboob % 2); + /* Both spare-area phases issue NOPs, even if one is empty. */ + spare_len = nboob ? sizeof(oob) : 0; + doc_read_page_getbytes(docg3, spare_len, oob, 0, 0); + doc_read_page_getbytes(docg3, sizeof(oob) - spare_len, + oob + spare_len, 0, 0); doc_get_bch_hw_ecc(docg3, hwecc); eccconf1 = doc_register_readb(docg3, DOC_ECCCONF1); - if (nboob >= DOC_LAYOUT_OOB_SIZE) { + if (nboob) { doc_dbg("OOB - INFO: %*phC\n", 7, oob); doc_dbg("OOB - HAMMING: %02x\n", oob[7]); doc_dbg("OOB - BCH_ECC: %*phC\n", 7, oob + 8); @@ -958,15 +964,28 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, } } - if (nboob) - memcpy(oobbuf, oob, nboob); + if (nboob && ops->mode == MTD_OPS_AUTO_OOB) { + int err; + + err = mtd_ooblayout_get_databytes(mtd, oobbuf, oob, + ooboffs, nboob); + if (err) { + ret = err; + goto err_in_read; + } + } else if (nboob) { + memcpy(oobbuf, oob + ooboffs, nboob); + } doc_read_page_finish(docg3); ops->retlen += nbdata; ops->oobretlen += nboob; - buf += nbdata; - oobbuf += nboob; + if (nbdata) + buf += nbdata; + if (nboob) + oobbuf += nboob; len -= nbdata; ooblen -= nboob; + ooboffs = 0; from += DOC_LAYOUT_PAGE_SIZE; skip = 0; } @@ -1319,8 +1338,7 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops { u32 avail = mtd_oobavail(mtd, ops); - if (ops->mode != MTD_OPS_PLACE_OOB && ops->mode != MTD_OPS_AUTO_OOB && - ops->mode != MTD_OPS_RAW) + if (!doc_oob_mode_valid(ops->mode)) return false; if (to % DOC_LAYOUT_PAGE_SIZE || ops->len % DOC_LAYOUT_PAGE_SIZE || (ops->ooblen && (ops->ooboffs || ops->ooblen % avail))) -- 2.53.0 A clean last page can hide earlier bitflips in a multi-page read. doc_read_oob() tracks the maximum number of bitflips in any page, but sets its return value to zero each time it starts checking another page. An MTD caller can therefore miss the indication that the read crossed the device's bitflip threshold. Return the maximum after the last successful page. Also record it in the request statistics so a later failing page does not erase it. Fixes: edbc4540e02c ("mtd: driver _read() returns max_bitflips; mtd_read() returns -EUCLEAN") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard --- drivers/mtd/devices/docg3.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c index aa43f4b9862b..4b2d635616ec 100644 --- a/drivers/mtd/devices/docg3.c +++ b/drivers/mtd/devices/docg3.c @@ -861,13 +861,13 @@ static bool doc_oob_mode_valid(unsigned int mode) /** * doc_read_oob - Read out of band bytes from flash * @mtd: the device - * @from: the offset from first block and first page, in bytes, aligned on page - * size + * @from: byte offset in the device * @ops: the mtd oob structure * * Reads flash memory OOB area of pages. * - * Returns 0 if read successful, of -EIO, -EINVAL if an error occurred + * Return: The maximum number of bitflips in any page read, or a negative + * error code. */ static int doc_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops) @@ -960,7 +960,6 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, if (ret > 0) { mtd->ecc_stats.corrected += ret; max_bitflips = max(max_bitflips, ret); - ret = max_bitflips; } } @@ -989,6 +988,8 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, from += DOC_LAYOUT_PAGE_SIZE; skip = 0; } + if (ret >= 0) + ret = max_bitflips; out: if (ops->stats) { @@ -996,6 +997,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from, mtd->ecc_stats.failed - old_stats.failed; ops->stats->corrected_bitflips += mtd->ecc_stats.corrected - old_stats.corrected; + ops->stats->max_bitflips = max_bitflips; } mutex_unlock(&docg3->cascade->lock); return ret; -- 2.53.0