mirror of
https://github.com/eclipse-threadx/levelx.git
synced 2026-09-14 04:06:00 +08:00
WARNING: This feature is brand new and has not been tested beyond the
existing regression suite. It should be thoroughly tested — including
stress, power-loss, and wear-levelling scenarios — before being used in
any production system.
When LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE is defined and free blocks
exceed LX_NAND_FLASH_SECTOR_RELEASE_LAZY_THRESHOLD (default 10), releasing
a sector from a full block defers the copy+erase. A tombstone page is
written to a freshly-allocated block; the old full block is flagged
COMPACTION_PENDING in its block status and recorded in a new per-LG
compaction table. Compaction (merge + erase) is triggered lazily:
* sector_write: before writing to a full block with a pending source
* block_data_move: intercepts wear-levelling moves for pending LGs
* defragment: iterates all pending LGs (now actually implemented)
* block_allocate: emergency compaction when the free list is empty
Reads transparently fall back to the compaction-pending block when the
primary block does not contain the requested sector.
Crash recovery is handled in open_extended:
- Phase 1: detects COMPACTION_PENDING blocks and either clears the flag
(crash before mapping update — abort) or rebuilds compaction_table
(crash after mapping update — resume deferred compaction).
- Phase 2: erases orphaned ALLOCATED blocks not present in any mapping.
Without the flag, all paths remain unchanged and defragment returns
LX_NOT_SUPPORTED as before.
Fixes: https://github.com/eclipse-threadx/levelx/issues/70
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
331 lines
13 KiB
C
331 lines
13 KiB
C
/***************************************************************************
|
|
* Copyright (c) 2024 Microsoft Corporation
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors
|
|
*
|
|
* This program and the accompanying materials are made available under the
|
|
* terms of the MIT License which is available at
|
|
* https://opensource.org/licenses/MIT.
|
|
*
|
|
* SPDX-License-Identifier: MIT
|
|
**************************************************************************/
|
|
|
|
// Some portions generated by Copilot (Sonnet 4.6).
|
|
|
|
/**************************************************************************/
|
|
/**************************************************************************/
|
|
/** */
|
|
/** LevelX Component */
|
|
/** */
|
|
/** NAND Flash */
|
|
/** */
|
|
/**************************************************************************/
|
|
/**************************************************************************/
|
|
|
|
#define LX_SOURCE_CODE
|
|
|
|
|
|
/* Disable ThreadX error checking. */
|
|
|
|
#ifndef LX_DISABLE_ERROR_CHECKING
|
|
#define LX_DISABLE_ERROR_CHECKING
|
|
#endif
|
|
|
|
|
|
/* Include necessary system files. */
|
|
|
|
#include "lx_api.h"
|
|
|
|
|
|
/**************************************************************************/
|
|
/* */
|
|
/* FUNCTION RELEASE */
|
|
/* */
|
|
/* _lx_nand_flash_sector_read PORTABLE C */
|
|
/* 6.2.1 */
|
|
/* AUTHOR */
|
|
/* */
|
|
/* Xiuwen Cai, Microsoft Corporation */
|
|
/* */
|
|
/* DESCRIPTION */
|
|
/* */
|
|
/* This function reads a logical sector from NAND flash. */
|
|
/* */
|
|
/* INPUT */
|
|
/* */
|
|
/* nand_flash NAND flash instance */
|
|
/* logical_sector Logical sector number */
|
|
/* buffer Pointer to buffer to read into*/
|
|
/* (the size is number of */
|
|
/* bytes in a page) */
|
|
/* */
|
|
/* OUTPUT */
|
|
/* */
|
|
/* return status */
|
|
/* */
|
|
/* CALLS */
|
|
/* */
|
|
/* _lx_nand_flash_block_find Find the mapped block */
|
|
/* lx_nand_flash_driver_pages_read Read pages */
|
|
/* _lx_nand_flash_system_error Internal system error handler */
|
|
/* tx_mutex_get Get thread protection */
|
|
/* tx_mutex_put Release thread protection */
|
|
/* */
|
|
/* CALLED BY */
|
|
/* */
|
|
/* Application Code */
|
|
/* */
|
|
/**************************************************************************/
|
|
UINT _lx_nand_flash_sector_read(LX_NAND_FLASH *nand_flash, ULONG logical_sector, VOID *buffer)
|
|
{
|
|
|
|
UINT status;
|
|
ULONG i;
|
|
ULONG *word_ptr;
|
|
ULONG block;
|
|
USHORT block_status;
|
|
UCHAR *spare_buffer_ptr;
|
|
ULONG available_pages;
|
|
LONG page;
|
|
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
|
|
/* Obtain the thread safe mutex. */
|
|
tx_mutex_get(&nand_flash -> lx_nand_flash_mutex, TX_WAIT_FOREVER);
|
|
#endif
|
|
|
|
/* Increment the number of read requests. */
|
|
nand_flash -> lx_nand_flash_diagnostic_sector_read_requests++;
|
|
|
|
/* See if we can find the sector in the current mapping. */
|
|
status = _lx_nand_flash_block_find(nand_flash, logical_sector, &block, &block_status);
|
|
|
|
/* Check return status. */
|
|
if (status != LX_SUCCESS)
|
|
{
|
|
|
|
/* Call system error handler. */
|
|
_lx_nand_flash_system_error(nand_flash, status, block, 0);
|
|
|
|
/* Determine if the error is fatal. */
|
|
if (status != LX_NAND_ERROR_CORRECTED)
|
|
{
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
|
|
/* Release the thread safe mutex. */
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
/* Return an error. */
|
|
return(LX_ERROR);
|
|
}
|
|
}
|
|
|
|
/* Determine if the block is mapped. */
|
|
if (block != LX_NAND_BLOCK_UNMAPPED)
|
|
{
|
|
|
|
/* Setup spare buffer pointer. */
|
|
spare_buffer_ptr = (UCHAR*)nand_flash -> lx_nand_flash_page_buffer;
|
|
|
|
/* Get available pages in this block. */
|
|
available_pages = block_status & LX_NAND_BLOCK_STATUS_FULL ? nand_flash -> lx_nand_flash_pages_per_block : block_status & LX_NAND_BLOCK_STATUS_PAGE_NUMBER_MASK;
|
|
|
|
/* Determine if the pages are recorded sequentially. */
|
|
if (block_status & LX_NAND_BLOCK_STATUS_NON_SEQUENTIAL)
|
|
{
|
|
|
|
/* Loop to search the logical page. */
|
|
for (page = (LONG)available_pages - 1; page >= 0; page--)
|
|
{
|
|
|
|
/* Read a page. */
|
|
#ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(nand_flash, block, (ULONG)page, (UCHAR*)NULL, spare_buffer_ptr, 1);
|
|
#else
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(block, (ULONG)page, (UCHAR*)NULL, spare_buffer_ptr, 1);
|
|
#endif
|
|
|
|
/* Check for an error from flash driver. */
|
|
if (status)
|
|
{
|
|
|
|
/* Call system error handler. */
|
|
_lx_nand_flash_system_error(nand_flash, status, block, 0);
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
|
|
/* Release the thread safe mutex. */
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
/* Return an error. */
|
|
return(LX_ERROR);
|
|
}
|
|
|
|
/* Get the logical sector number from spare bytes, and check if it matches the addressed sector number. */
|
|
if ((LX_UTILITY_LONG_GET(&spare_buffer_ptr[nand_flash -> lx_nand_flash_spare_data1_offset]) & LX_NAND_PAGE_TYPE_USER_DATA_MASK) == logical_sector)
|
|
{
|
|
#ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(nand_flash, block, (ULONG)page, (UCHAR*)buffer, NULL, 1);
|
|
#else
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(block, (ULONG)page, (UCHAR*)buffer, NULL, 1);
|
|
#endif
|
|
|
|
/* Check for an error from flash driver. */
|
|
if (status)
|
|
{
|
|
|
|
/* Call system error handler. */
|
|
_lx_nand_flash_system_error(nand_flash, status, block, 0);
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
|
|
/* Release the thread safe mutex. */
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
/* Return an error. */
|
|
return(LX_ERROR);
|
|
}
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
|
|
/* Release the thread safe mutex. */
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
/* Return successful completion. */
|
|
return(LX_SUCCESS);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
|
|
/* Check if the logical sector is available. */
|
|
if (logical_sector % nand_flash -> lx_nand_flash_pages_per_block < available_pages)
|
|
{
|
|
|
|
/* Read a page. */
|
|
#ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(nand_flash, block, logical_sector % nand_flash -> lx_nand_flash_pages_per_block, (UCHAR*)buffer, spare_buffer_ptr, 1);
|
|
#else
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(block, logical_sector % nand_flash -> lx_nand_flash_pages_per_block, (UCHAR*)buffer, spare_buffer_ptr, 1);
|
|
#endif
|
|
|
|
/* Check for an error from flash driver. */
|
|
if (status)
|
|
{
|
|
|
|
/* Call system error handler. */
|
|
_lx_nand_flash_system_error(nand_flash, status, block, 0);
|
|
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
|
|
/* Release the thread safe mutex. */
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
/* Return an error. */
|
|
return(LX_ERROR);
|
|
}
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
|
|
/* Release the thread safe mutex. */
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
/* Return successful completion. */
|
|
return(LX_SUCCESS);
|
|
}
|
|
}
|
|
}
|
|
|
|
#ifdef LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE
|
|
{
|
|
|
|
ULONG logical_group;
|
|
ULONG old_block;
|
|
|
|
/* Check the compaction table for a deferred source block that might hold
|
|
the requested sector. The primary block (tombstone block) was already
|
|
scanned above; fall back to the old full block here. */
|
|
logical_group = logical_sector / nand_flash -> lx_nand_flash_pages_per_block;
|
|
|
|
if (nand_flash -> lx_nand_flash_block_compaction_table[logical_group] != (USHORT)LX_NAND_BLOCK_UNMAPPED)
|
|
{
|
|
|
|
old_block = nand_flash -> lx_nand_flash_block_compaction_table[logical_group];
|
|
|
|
/* Set spare buffer pointer. */
|
|
spare_buffer_ptr = (UCHAR*)nand_flash -> lx_nand_flash_page_buffer;
|
|
|
|
/* The compaction-pending block is always FULL; scan all pages in reverse. */
|
|
for (page = (LONG)nand_flash -> lx_nand_flash_pages_per_block - 1; page >= 0; page--)
|
|
{
|
|
|
|
#ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(nand_flash, old_block, (ULONG)page, (UCHAR*)NULL, spare_buffer_ptr, 1);
|
|
#else
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(old_block, (ULONG)page, (UCHAR*)NULL, spare_buffer_ptr, 1);
|
|
#endif
|
|
|
|
/* Check for an error from flash driver. */
|
|
if (status)
|
|
{
|
|
_lx_nand_flash_system_error(nand_flash, status, old_block, 0);
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
return(LX_ERROR);
|
|
}
|
|
|
|
/* Check if this page matches the requested sector. */
|
|
if ((LX_UTILITY_LONG_GET(&spare_buffer_ptr[nand_flash -> lx_nand_flash_spare_data1_offset]) & LX_NAND_PAGE_TYPE_USER_DATA_MASK) == logical_sector)
|
|
{
|
|
|
|
#ifdef LX_NAND_ENABLE_CONTROL_BLOCK_FOR_DRIVER_INTERFACE
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(nand_flash, old_block, (ULONG)page, (UCHAR*)buffer, NULL, 1);
|
|
#else
|
|
status = (nand_flash -> lx_nand_flash_driver_pages_read)(old_block, (ULONG)page, (UCHAR*)buffer, NULL, 1);
|
|
#endif
|
|
|
|
/* Check for an error from flash driver. */
|
|
if (status)
|
|
{
|
|
_lx_nand_flash_system_error(nand_flash, status, old_block, 0);
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
return(LX_ERROR);
|
|
}
|
|
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
return(LX_SUCCESS);
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
#endif /* LX_NAND_FLASH_ENABLE_LAZY_SECTOR_RELEASE */
|
|
|
|
/* Sector hasn't been written. Simply fill the destination buffer with ones and return success. */
|
|
|
|
/* Setup pointer to users buffer. */
|
|
word_ptr = (ULONG *) buffer;
|
|
|
|
/* Put all ones in he buffer. */
|
|
for (i = 0; i < nand_flash -> lx_nand_flash_words_per_page; i++)
|
|
{
|
|
|
|
/* Copy a word. */
|
|
*word_ptr++ = LX_ALL_ONES;
|
|
}
|
|
|
|
/* Set the status to success. */
|
|
status = LX_SUCCESS;
|
|
|
|
#ifdef LX_THREAD_SAFE_ENABLE
|
|
|
|
/* Release the thread safe mutex. */
|
|
tx_mutex_put(&nand_flash -> lx_nand_flash_mutex);
|
|
#endif
|
|
|
|
/* Return status. */
|
|
return(status);
|
|
}
|
|
|