eMMC卡安全擦除问题:BLKSECDISCARD返回EOPNOTSUPP错误
EOPNOTSUPP for BLKSECDISCARD/BLKDISCARD on MMC Cards Alright, let's break down why you're hitting the EOPNOTSUPP (Operation Not Supported) error when trying to securely erase your MMC card via those ioctls, and walk through how to resolve it.
Root Cause
The core issue is that your kernel's MMC subsystem hasn't enabled DISCARD functionality for your block device. As you found in the code, mmc_queue_setup_discard() (in kernel/linux/drivers/mmc/card/queue.c) only sets the QUEUE_FLAG_DISCARD flag if mmc_can_erase(card) returns true. If that check fails, the block device won't advertise support for BLKDISCARD or BLKSECDISCARD, hence the error when you call those ioctls.
Deep Dive into mmc_can_erase()
Let's unpack what mmc_can_erase() (defined in kernel/linux/drivers/mmc/core/core.c) actually checks:
- First, it verifies the card supports the basic
MMC_CMD_ERASEcommand—this is the low-level erase instruction the card understands. - Next, it validates the card type: it must be a standard MMC card (not an SD card, which uses different erase logic), and it can't be an MMC Mobile or reduced-function MMC card.
- Finally, it checks that the card's CSD register has a valid
erase_grp_sizevalue—this tells the kernel the card supports block-aligned erase operations.
If any of these conditions aren't met, mmc_can_erase() returns false, and DISCARD support stays disabled.
Troubleshooting & Fix Steps
Here's how to tackle this step by step:
- Verify the MMC card's hardware capabilities first
Use tools likemmc-utils(try themmc erasecommand) to test if the card responds to low-level erase commands. If the hardware itself doesn't support erase, no kernel tweaks will help—you'll need to swap in an MMC card that does support this feature. - Check your kernel configuration
Make sureCONFIG_MMC_BLOCK_DISCARDis enabled in your kernel config. This is the compile-time switch that allows the MMC block driver to expose DISCARD functionality to user space. Without it, even a compatible card won't support those ioctls. - Debug why
mmc_can_erase()is failing
If your card should support erase but the function returns false, add debug prints to themmc_can_erase()function to pinpoint which check is failing:
Check the dmesg output after booting to see which condition isn't satisfied. For example, if the card type is misidentified, you might need to adjust the driver's card detection logic.pr_debug("MMC card type: %d, erase cmd support: %d, erase group size: %d\n", card->type, mmc_card_erase(card), card->csd.erase_grp_size); - Alternative approach if DISCARD can't be enabled
If you can't getBLKSECDISCARDworking, you can skip the block layer entirely and call the MMC subsystem's low-level erase API directly from kernel space:
If you need user-space access, you could wrap this in a custom character device driver to expose the erase functionality to your application.int ret = mmc_erase(card, start_sector, num_sectors);
内容的提问来源于stack exchange,提问作者Matt Muggeridge

