2010-07-07 11:18:46

by Adrian Hunter

[permalink] [raw]
Subject: [PATCH V4 0/5] Add MMC erase and secure erase V4

Hi

Add ability to do MMC erase and secure erase operations from userspace,
using the BLKDISCARD ioctl and a newly created BLKSECDISCARD ioctl.

This is version 4 of these patches.

Changes from V3

- rebased on mmotm
- added SD/MMC device attribute documentation to 'mmc: Add erase,
secure erase, trim and secure trim operations' patch

Changes from V2

- move the addition of BLKSECDISCARD to a separate patch and implement it
using I/O requests
- move the MMC support of secure discard to a separate patch and support
the secure discard I/O request

Changes from V1

- use discard I/O requests instead of implementing BLKDISCARD in mmc_block
- do not validate erase size for MMC erases, instead round to erase size
- rename MMC max_erase to pref_erase and expose it through sysfs as
preferred_erase_size


Adrian Hunter (5):
mmc: Add erase, secure erase, trim and secure trim operations
mmc_block: Add discard support
omap_hsmmc: Add erase capability
block: Add secure discard
mmc_block: Add support for secure discard

Documentation/00-INDEX | 2 +
Documentation/mmc/00-INDEX | 4 +
Documentation/mmc/mmc-dev-attrs.txt | 56 ++++++
block/blk-core.c | 5 +-
block/blk-lib.c | 6 +
block/compat_ioctl.c | 1 +
block/elevator.c | 6 +
block/ioctl.c | 15 +-
drivers/mmc/card/block.c | 82 ++++++++-
drivers/mmc/card/queue.c | 19 ++-
drivers/mmc/core/core.c | 346 +++++++++++++++++++++++++++++++++++
drivers/mmc/core/core.h | 2 +
drivers/mmc/core/mmc.c | 47 +++++-
drivers/mmc/core/sd.c | 82 ++++++++
drivers/mmc/core/sd_ops.c | 48 +++++
drivers/mmc/core/sd_ops.h | 1 +
drivers/mmc/host/omap_hsmmc.c | 13 +-
include/linux/bio.h | 2 +
include/linux/blkdev.h | 7 +-
include/linux/fs.h | 2 +
include/linux/mmc/card.h | 19 ++
include/linux/mmc/core.h | 19 ++
include/linux/mmc/host.h | 1 +
include/linux/mmc/mmc.h | 26 ++-
include/linux/mmc/sd.h | 5 +
kernel/trace/blktrace.c | 8 +
26 files changed, 803 insertions(+), 21 deletions(-)
create mode 100644 Documentation/mmc/00-INDEX
create mode 100644 Documentation/mmc/mmc-dev-attrs.txt


Regards
Adrian


2010-07-07 11:18:50

by Adrian Hunter

[permalink] [raw]
Subject: [PATCH V4 3/5] omap_hsmmc: Add erase capability

>From db31a11109e7f0c89a7a9ca70ad03beff354aa8e Mon Sep 17 00:00:00 2001
From: Adrian Hunter <[email protected]>
Date: Tue, 18 May 2010 17:33:13 +0300
Subject: [PATCH 3/5] omap_hsmmc: Add erase capability

Disable the data (busy) timeout for erases and set the
MMC_CAP_ERASE capability.

Signed-off-by: Adrian Hunter <[email protected]>
---
drivers/mmc/host/omap_hsmmc.c | 13 ++++++++++---
1 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/drivers/mmc/host/omap_hsmmc.c b/drivers/mmc/host/omap_hsmmc.c
index a0c8515..868498d 100644
--- a/drivers/mmc/host/omap_hsmmc.c
+++ b/drivers/mmc/host/omap_hsmmc.c
@@ -28,6 +28,7 @@
#include <linux/clk.h>
#include <linux/mmc/host.h>
#include <linux/mmc/core.h>
+#include <linux/mmc/mmc.h>
#include <linux/io.h>
#include <linux/semaphore.h>
#include <linux/gpio.h>
@@ -78,6 +79,7 @@
#define INT_EN_MASK 0x307F0033
#define BWR_ENABLE (1 << 4)
#define BRR_ENABLE (1 << 5)
+#define DTO_ENABLE (1 << 20)
#define INIT_STREAM (1 << 1)
#define DP_SELECT (1 << 21)
#define DDIR (1 << 4)
@@ -523,7 +525,8 @@ static void omap_hsmmc_stop_clock(struct omap_hsmmc_host *host)
dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
}

-static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host)
+static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host,
+ struct mmc_command *cmd)
{
unsigned int irq_mask;

@@ -532,6 +535,10 @@ static void omap_hsmmc_enable_irq(struct omap_hsmmc_host *host)
else
irq_mask = INT_EN_MASK;

+ /* Disable timeout for erases */
+ if (cmd->opcode == MMC_ERASE)
+ irq_mask &= ~DTO_ENABLE;
+
OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
OMAP_HSMMC_WRITE(host->base, ISE, irq_mask);
OMAP_HSMMC_WRITE(host->base, IE, irq_mask);
@@ -782,7 +789,7 @@ omap_hsmmc_start_command(struct omap_hsmmc_host *host, struct mmc_command *cmd,
mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
host->cmd = cmd;

- omap_hsmmc_enable_irq(host);
+ omap_hsmmc_enable_irq(host, cmd);

host->response_busy = 0;
if (cmd->flags & MMC_RSP_PRESENT) {
@@ -2094,7 +2101,7 @@ static int __init omap_hsmmc_probe(struct platform_device *pdev)
mmc->max_seg_size = mmc->max_req_size;

mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
- MMC_CAP_WAIT_WHILE_BUSY;
+ MMC_CAP_WAIT_WHILE_BUSY | MMC_CAP_ERASE;

switch (mmc_slot(host).wires) {
case 8:
--
1.6.3.3

2010-07-07 11:18:53

by Adrian Hunter

[permalink] [raw]
Subject: [PATCH V4 1/5] mmc: Add erase, secure erase, trim and secure trim operations

>From 7f01ad3c4be6ec09318176db12db66f353b526e0 Mon Sep 17 00:00:00 2001
From: Adrian Hunter <[email protected]>
Date: Tue, 1 Jun 2010 13:20:22 +0300
Subject: [PATCH 1/5] mmc: Add erase, secure erase, trim and secure trim operations

SD/MMC cards tend to support an erase operation. In addition,
eMMC v4.4 cards can support secure erase, trim and secure trim
operations that are all variants of the basic erase command.

SD/MMC device attributes "erase_size" and "preferred_erase_size"
have been added.

"erase_size" is the minimum size, in bytes, of an erase
operation. For MMC, "erase_size" is the erase group size
reported by the card. Note that "erase_size" does not apply
to trim or secure trim operations where the minimum size is
always one 512 byte sector. For SD, "erase_size" is 512
if the card is block-addressed, 0 otherwise.

SD/MMC cards can erase an arbitrarily large area up to and
including the whole card. When erasing a large area it may
be desirable to do it in smaller chunks for three reasons:
1. A single erase command will make all other I/O on
the card wait. This is not a problem if the whole card
is being erased, but erasing one partition will make
I/O for another partition on the same card wait for the
duration of the erase - which could be a several
minutes.
2. To be able to inform the user of erase progress.
3. The erase timeout becomes too large to be very
useful. Because the erase timeout contains a margin
which is multiplied by the size of the erase area,
the value can end up being several minutes for large
areas.

"erase_size" is not the most efficient unit to erase
(especially for SD where it is just one sector),
hence "preferred_erase_size" provides a good chunk
size for erasing large areas.

For MMC, "preferred_erase_size" is the high-capacity
erase size if a card specifies one, otherwise it is
based on the capacity of the card.

For SD, "preferred_erase_size" is the allocation unit
size specified by the card.

"preferred_erase_size" is in bytes.

Signed-off-by: Adrian Hunter <[email protected]>
---
Documentation/00-INDEX | 2 +
Documentation/mmc/00-INDEX | 4 +
Documentation/mmc/mmc-dev-attrs.txt | 56 ++++++
drivers/mmc/core/core.c | 346 +++++++++++++++++++++++++++++++++++
drivers/mmc/core/core.h | 2 +
drivers/mmc/core/mmc.c | 47 +++++-
drivers/mmc/core/sd.c | 82 ++++++++
drivers/mmc/core/sd_ops.c | 48 +++++
drivers/mmc/core/sd_ops.h | 1 +
include/linux/mmc/card.h | 19 ++
include/linux/mmc/core.h | 19 ++
include/linux/mmc/host.h | 1 +
include/linux/mmc/mmc.h | 26 ++-
include/linux/mmc/sd.h | 5 +
14 files changed, 650 insertions(+), 8 deletions(-)
create mode 100644 Documentation/mmc/00-INDEX
create mode 100644 Documentation/mmc/mmc-dev-attrs.txt

diff --git a/Documentation/00-INDEX b/Documentation/00-INDEX
index dd10b51..7cfc696 100644
--- a/Documentation/00-INDEX
+++ b/Documentation/00-INDEX
@@ -232,6 +232,8 @@ memory.txt
- info on typical Linux memory problems.
mips/
- directory with info about Linux on MIPS architecture.
+mmc/
+ - directory with info about the MMC subsystem
mono.txt
- how to execute Mono-based .NET binaries with the help of BINFMT_MISC.
mutex-design.txt
diff --git a/Documentation/mmc/00-INDEX b/Documentation/mmc/00-INDEX
new file mode 100644
index 0000000..fca586f
--- /dev/null
+++ b/Documentation/mmc/00-INDEX
@@ -0,0 +1,4 @@
+00-INDEX
+ - this file
+mmc-dev-attrs.txt
+ - info on SD and MMC device attributes
diff --git a/Documentation/mmc/mmc-dev-attrs.txt b/Documentation/mmc/mmc-dev-attrs.txt
new file mode 100644
index 0000000..ff2bd68
--- /dev/null
+++ b/Documentation/mmc/mmc-dev-attrs.txt
@@ -0,0 +1,56 @@
+SD and MMC Device Attributes
+============================
+
+All attributes are read-only.
+
+ cid Card Identifaction Register
+ csd Card Specific Data Register
+ scr SD Card Configuration Register (SD only)
+ date Manufacturing Date (from CID Register)
+ fwrev Firmware/Product Revision (from CID Register) (SD and MMCv1 only)
+ hwrev Hardware/Product Revision (from CID Register) (SD and MMCv1 only)
+ manfid Manufacturer ID (from CID Register)
+ name Product Name (from CID Register)
+ oemid OEM/Application ID (from CID Register)
+ serial Product Serial Number (from CID Register)
+ erase_size Erase group size
+ preferred_erase_size Preferred erase size
+
+Note on Erase Size and Preferred Erase Size:
+
+ "erase_size" is the minimum size, in bytes, of an erase
+ operation. For MMC, "erase_size" is the erase group size
+ reported by the card. Note that "erase_size" does not apply
+ to trim or secure trim operations where the minimum size is
+ always one 512 byte sector. For SD, "erase_size" is 512
+ if the card is block-addressed, 0 otherwise.
+
+ SD/MMC cards can erase an arbitrarily large area up to and
+ including the whole card. When erasing a large area it may
+ be desirable to do it in smaller chunks for three reasons:
+ 1. A single erase command will make all other I/O on
+ the card wait. This is not a problem if the whole card
+ is being erased, but erasing one partition will make
+ I/O for another partition on the same card wait for the
+ duration of the erase - which could be a several
+ minutes.
+ 2. To be able to inform the user of erase progress.
+ 3. The erase timeout becomes too large to be very
+ useful. Because the erase timeout contains a margin
+ which is multiplied by the size of the erase area,
+ the value can end up being several minutes for large
+ areas.
+
+ "erase_size" is not the most efficient unit to erase
+ (especially for SD where it is just one sector),
+ hence "preferred_erase_size" provides a good chunk
+ size for erasing large areas.
+
+ For MMC, "preferred_erase_size" is the high-capacity
+ erase size if a card specifies one, otherwise it is
+ based on the capacity of the card.
+
+ For SD, "preferred_erase_size" is the allocation unit
+ size specified by the card.
+
+ "preferred_erase_size" is in bytes.
diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index f4e9398..16d0601 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -1050,6 +1050,352 @@ void mmc_detect_change(struct mmc_host *host, unsigned long delay)

EXPORT_SYMBOL(mmc_detect_change);

+void mmc_init_erase(struct mmc_card *card)
+{
+ unsigned int sz;
+
+ if (is_power_of_2(card->erase_size))
+ card->erase_shift = ffs(card->erase_size) - 1;
+ else
+ card->erase_shift = 0;
+
+ /*
+ * It is possible to erase an arbitrarily large area of an SD or MMC
+ * card. That is not desirable because it can take a long time
+ * (minutes) potentially delaying more important I/O, and also the
+ * timeout calculations become increasingly hugely over-estimated.
+ * Consequently, 'pref_erase' is defined as a guide to limit erases
+ * to that size and alignment.
+ *
+ * For SD cards that define Allocation Unit size, limit erases to one
+ * Allocation Unit at a time. For MMC cards that define High Capacity
+ * Erase Size, whether it is switched on or not, limit to that size.
+ * Otherwise just have a stab at a good value. For modern cards it
+ * will end up being 4MiB. Note that if the value is too small, it
+ * can end up taking longer to erase.
+ */
+ if (mmc_card_sd(card) && card->ssr.au) {
+ card->pref_erase = card->ssr.au;
+ card->erase_shift = ffs(card->ssr.au) - 1;
+ } else if (card->ext_csd.hc_erase_size) {
+ card->pref_erase = card->ext_csd.hc_erase_size;
+ } else {
+ sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11;
+ if (sz < 128)
+ card->pref_erase = 512 * 1024 / 512;
+ else if (sz < 512)
+ card->pref_erase = 1024 * 1024 / 512;
+ else if (sz < 1024)
+ card->pref_erase = 2 * 1024 * 1024 / 512;
+ else
+ card->pref_erase = 4 * 1024 * 1024 / 512;
+ if (card->pref_erase < card->erase_size)
+ card->pref_erase = card->erase_size;
+ else {
+ sz = card->pref_erase % card->erase_size;
+ if (sz)
+ card->pref_erase += card->erase_size - sz;
+ }
+ }
+}
+
+static void mmc_set_mmc_erase_timeout(struct mmc_card *card,
+ struct mmc_command *cmd,
+ unsigned int arg, unsigned int qty)
+{
+ unsigned int erase_timeout;
+
+ if (card->ext_csd.erase_group_def & 1) {
+ /* High Capacity Erase Group Size uses HC timeouts */
+ if (arg == MMC_TRIM_ARG)
+ erase_timeout = card->ext_csd.trim_timeout;
+ else
+ erase_timeout = card->ext_csd.hc_erase_timeout;
+ } else {
+ /* CSD Erase Group Size uses write timeout */
+ unsigned int mult = (10 << card->csd.r2w_factor);
+ unsigned int timeout_clks = card->csd.tacc_clks * mult;
+ unsigned int timeout_us;
+
+ /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */
+ if (card->csd.tacc_ns < 1000000)
+ timeout_us = (card->csd.tacc_ns * mult) / 1000;
+ else
+ timeout_us = (card->csd.tacc_ns / 1000) * mult;
+
+ /*
+ * ios.clock is only a target. The real clock rate might be
+ * less but not that much less, so fudge it by multiplying by 2.
+ */
+ timeout_clks <<= 1;
+ timeout_us += (timeout_clks * 1000) /
+ (card->host->ios.clock / 1000);
+
+ erase_timeout = timeout_us / 1000;
+
+ /*
+ * Theoretically, the calculation could underflow so round up
+ * to 1ms in that case.
+ */
+ if (!erase_timeout)
+ erase_timeout = 1;
+ }
+
+ /* Multiplier for secure operations */
+ if (arg & MMC_SECURE_ARGS) {
+ if (arg == MMC_SECURE_ERASE_ARG)
+ erase_timeout *= card->ext_csd.sec_erase_mult;
+ else
+ erase_timeout *= card->ext_csd.sec_trim_mult;
+ }
+
+ erase_timeout *= qty;
+
+ /*
+ * Ensure at least a 1 second timeout for SPI as per
+ * 'mmc_set_data_timeout()'
+ */
+ if (mmc_host_is_spi(card->host) && erase_timeout < 1000)
+ erase_timeout = 1000;
+
+ cmd->erase_timeout = erase_timeout;
+}
+
+static void mmc_set_sd_erase_timeout(struct mmc_card *card,
+ struct mmc_command *cmd, unsigned int arg,
+ unsigned int qty)
+{
+ if (card->ssr.erase_timeout) {
+ /* Erase timeout specified in SD Status Register (SSR) */
+ cmd->erase_timeout = card->ssr.erase_timeout * qty +
+ card->ssr.erase_offset;
+ } else {
+ /*
+ * Erase timeout not specified in SD Status Register (SSR) so
+ * use 250ms per write block.
+ */
+ cmd->erase_timeout = 250 * qty;
+ }
+
+ /* Must not be less than 1 second */
+ if (cmd->erase_timeout < 1000)
+ cmd->erase_timeout = 1000;
+}
+
+static void mmc_set_erase_timeout(struct mmc_card *card,
+ struct mmc_command *cmd, unsigned int arg,
+ unsigned int qty)
+{
+ if (mmc_card_sd(card))
+ mmc_set_sd_erase_timeout(card, cmd, arg, qty);
+ else
+ mmc_set_mmc_erase_timeout(card, cmd, arg, qty);
+}
+
+static int mmc_do_erase(struct mmc_card *card, unsigned int from,
+ unsigned int to, unsigned int arg)
+{
+ struct mmc_command cmd;
+ unsigned int qty = 0;
+ int err;
+
+ /*
+ * qty is used to calculate the erase timeout which depends on how many
+ * erase groups (or allocation units in SD terminology) are affected.
+ * We count erasing part of an erase group as one erase group.
+ * For SD, the allocation units are always a power of 2. For MMC, the
+ * erase group size is almost certainly also power of 2, but it does not
+ * seem to insist on that in the JEDEC standard, so we fall back to
+ * division in that case. SD may not specify an allocation unit size,
+ * in which case the timeout is based on the number of write blocks.
+ *
+ * Note that the timeout for secure trim 2 will only be correct if the
+ * number of erase groups specified is the same as the total of all
+ * preceding secure trim 1 commands. Since the power may have been
+ * lost since the secure trim 1 commands occurred, it is generally
+ * impossible to calculate the secure trim 2 timeout correctly.
+ */
+ if (card->erase_shift)
+ qty += ((to >> card->erase_shift) -
+ (from >> card->erase_shift)) + 1;
+ else if (mmc_card_sd(card))
+ qty += to - from + 1;
+ else
+ qty += ((to / card->erase_size) -
+ (from / card->erase_size)) + 1;
+
+ if (!mmc_card_blockaddr(card)) {
+ from <<= 9;
+ to <<= 9;
+ }
+
+ memset(&cmd, 0, sizeof(struct mmc_command));
+ if (mmc_card_sd(card))
+ cmd.opcode = SD_ERASE_WR_BLK_START;
+ else
+ cmd.opcode = MMC_ERASE_GROUP_START;
+ cmd.arg = from;
+ cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+ err = mmc_wait_for_cmd(card->host, &cmd, 0);
+ if (err) {
+ printk(KERN_ERR "mmc_erase: group start error %d, "
+ "status %#x\n", err, cmd.resp[0]);
+ err = -EINVAL;
+ goto out;
+ }
+
+ memset(&cmd, 0, sizeof(struct mmc_command));
+ if (mmc_card_sd(card))
+ cmd.opcode = SD_ERASE_WR_BLK_END;
+ else
+ cmd.opcode = MMC_ERASE_GROUP_END;
+ cmd.arg = to;
+ cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC;
+ err = mmc_wait_for_cmd(card->host, &cmd, 0);
+ if (err) {
+ printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n",
+ err, cmd.resp[0]);
+ err = -EINVAL;
+ goto out;
+ }
+
+ memset(&cmd, 0, sizeof(struct mmc_command));
+ cmd.opcode = MMC_ERASE;
+ cmd.arg = arg;
+ cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC;
+ mmc_set_erase_timeout(card, &cmd, arg, qty);
+ err = mmc_wait_for_cmd(card->host, &cmd, 0);
+ if (err) {
+ printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n",
+ err, cmd.resp[0]);
+ err = -EIO;
+ goto out;
+ }
+
+ if (mmc_host_is_spi(card->host))
+ goto out;
+
+ do {
+ memset(&cmd, 0, sizeof(struct mmc_command));
+ cmd.opcode = MMC_SEND_STATUS;
+ cmd.arg = card->rca << 16;
+ cmd.flags = MMC_RSP_R1 | MMC_CMD_AC;
+ /* Do not retry else we can't see errors */
+ err = mmc_wait_for_cmd(card->host, &cmd, 0);
+ if (err || (cmd.resp[0] & 0xFDF92000)) {
+ printk(KERN_ERR "error %d requesting status %#x\n",
+ err, cmd.resp[0]);
+ err = -EIO;
+ goto out;
+ }
+ } while (!(cmd.resp[0] & R1_READY_FOR_DATA) ||
+ R1_CURRENT_STATE(cmd.resp[0]) == 7);
+out:
+ return err;
+}
+
+/**
+ * mmc_erase - erase sectors.
+ * @card: card to erase
+ * @from: first sector to erase
+ * @nr: number of sectors to erase
+ * @arg: erase command argument (SD supports only %MMC_ERASE_ARG)
+ *
+ * Caller must claim host before calling this function.
+ */
+int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
+ unsigned int arg)
+{
+ unsigned int rem, to = from + nr;
+
+ if (!(card->host->caps & MMC_CAP_ERASE) ||
+ !(card->csd.cmdclass & CCC_ERASE))
+ return -EOPNOTSUPP;
+
+ if (!card->erase_size)
+ return -EOPNOTSUPP;
+
+ if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
+ return -EOPNOTSUPP;
+
+ if ((arg & MMC_SECURE_ARGS) &&
+ !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
+ return -EOPNOTSUPP;
+
+ if ((arg & MMC_TRIM_ARGS) &&
+ !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
+ return -EOPNOTSUPP;
+
+ if (arg == MMC_SECURE_ERASE_ARG) {
+ if (from % card->erase_size || nr % card->erase_size)
+ return -EINVAL;
+ }
+
+ if (arg == MMC_ERASE_ARG) {
+ rem = from % card->erase_size;
+ if (rem) {
+ rem = card->erase_size - rem;
+ from += rem;
+ if (nr > rem)
+ nr -= rem;
+ else
+ return 0;
+ }
+ rem = nr % card->erase_size;
+ if (rem)
+ nr -= rem;
+ }
+
+ if (nr == 0)
+ return 0;
+
+ to = from + nr;
+
+ if (to <= from)
+ return -EINVAL;
+
+ /* 'from' and 'to' are inclusive */
+ to -= 1;
+
+ return mmc_do_erase(card, from, to, arg);
+}
+EXPORT_SYMBOL(mmc_erase);
+
+int mmc_can_erase(struct mmc_card *card)
+{
+ if ((card->host->caps & MMC_CAP_ERASE) &&
+ (card->csd.cmdclass & CCC_ERASE) && card->erase_size)
+ return 1;
+ return 0;
+}
+EXPORT_SYMBOL(mmc_can_erase);
+
+int mmc_can_trim(struct mmc_card *card)
+{
+ if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
+ return 1;
+ return 0;
+}
+EXPORT_SYMBOL(mmc_can_trim);
+
+int mmc_can_secure_erase_trim(struct mmc_card *card)
+{
+ if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)
+ return 1;
+ return 0;
+}
+EXPORT_SYMBOL(mmc_can_secure_erase_trim);
+
+int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
+ unsigned int nr)
+{
+ if (!card->erase_size)
+ return 0;
+ if (from % card->erase_size || nr % card->erase_size)
+ return 0;
+ return 1;
+}
+EXPORT_SYMBOL(mmc_erase_group_aligned);

void mmc_rescan(struct work_struct *work)
{
diff --git a/drivers/mmc/core/core.h b/drivers/mmc/core/core.h
index a811c52..9d9eef5 100644
--- a/drivers/mmc/core/core.h
+++ b/drivers/mmc/core/core.h
@@ -29,6 +29,8 @@ struct mmc_bus_ops {
void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops);
void mmc_detach_bus(struct mmc_host *host);

+void mmc_init_erase(struct mmc_card *card);
+
void mmc_set_chip_select(struct mmc_host *host, int mode);
void mmc_set_clock(struct mmc_host *host, unsigned int hz);
void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode);
diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index ccba386..6909a54 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -108,13 +108,23 @@ static int mmc_decode_cid(struct mmc_card *card)
return 0;
}

+static void mmc_set_erase_size(struct mmc_card *card)
+{
+ if (card->ext_csd.erase_group_def & 1)
+ card->erase_size = card->ext_csd.hc_erase_size;
+ else
+ card->erase_size = card->csd.erase_size;
+
+ mmc_init_erase(card);
+}
+
/*
* Given a 128-bit response, decode to our card CSD structure.
*/
static int mmc_decode_csd(struct mmc_card *card)
{
struct mmc_csd *csd = &card->csd;
- unsigned int e, m;
+ unsigned int e, m, a, b;
u32 *resp = card->raw_csd;

/*
@@ -152,6 +162,13 @@ static int mmc_decode_csd(struct mmc_card *card)
csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
csd->write_partial = UNSTUFF_BITS(resp, 21, 1);

+ if (csd->write_blkbits >= 9) {
+ a = UNSTUFF_BITS(resp, 42, 5);
+ b = UNSTUFF_BITS(resp, 37, 5);
+ csd->erase_size = (a + 1) * (b + 1);
+ csd->erase_size <<= csd->write_blkbits - 9;
+ }
+
return 0;
}

@@ -261,8 +278,30 @@ static int mmc_read_ext_csd(struct mmc_card *card)
if (sa_shift > 0 && sa_shift <= 0x17)
card->ext_csd.sa_timeout =
1 << ext_csd[EXT_CSD_S_A_TIMEOUT];
+ card->ext_csd.erase_group_def =
+ ext_csd[EXT_CSD_ERASE_GROUP_DEF];
+ card->ext_csd.hc_erase_timeout = 300 *
+ ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT];
+ card->ext_csd.hc_erase_size =
+ ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10;
+ }
+
+ if (card->ext_csd.rev >= 4) {
+ card->ext_csd.sec_trim_mult =
+ ext_csd[EXT_CSD_SEC_TRIM_MULT];
+ card->ext_csd.sec_erase_mult =
+ ext_csd[EXT_CSD_SEC_ERASE_MULT];
+ card->ext_csd.sec_feature_support =
+ ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT];
+ card->ext_csd.trim_timeout = 300 *
+ ext_csd[EXT_CSD_TRIM_MULT];
}

+ if (ext_csd[EXT_CSD_ERASED_MEM_CONT])
+ card->erased_byte = 0xFF;
+ else
+ card->erased_byte = 0x0;
+
out:
kfree(ext_csd);

@@ -274,6 +313,8 @@ MMC_DEV_ATTR(cid, "%08x%08x%08x%08x\n", card->raw_cid[0], card->raw_cid[1],
MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
card->raw_csd[2], card->raw_csd[3]);
MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
+MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
+MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
@@ -285,6 +326,8 @@ static struct attribute *mmc_std_attrs[] = {
&dev_attr_cid.attr,
&dev_attr_csd.attr,
&dev_attr_date.attr,
+ &dev_attr_erase_size.attr,
+ &dev_attr_preferred_erase_size.attr,
&dev_attr_fwrev.attr,
&dev_attr_hwrev.attr,
&dev_attr_manfid.attr,
@@ -421,6 +464,8 @@ static int mmc_init_card(struct mmc_host *host, u32 ocr,
err = mmc_read_ext_csd(card);
if (err)
goto free_card;
+ /* Erase size depends on CSD and Extended CSD */
+ mmc_set_erase_size(card);
}

/*
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index e6d7d9f..0f52410 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -119,6 +119,13 @@ static int mmc_decode_csd(struct mmc_card *card)
csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3);
csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4);
csd->write_partial = UNSTUFF_BITS(resp, 21, 1);
+
+ if (UNSTUFF_BITS(resp, 46, 1)) {
+ csd->erase_size = 1;
+ } else if (csd->write_blkbits >= 9) {
+ csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1;
+ csd->erase_size <<= csd->write_blkbits - 9;
+ }
break;
case 1:
/*
@@ -147,6 +154,7 @@ static int mmc_decode_csd(struct mmc_card *card)
csd->r2w_factor = 4; /* Unused */
csd->write_blkbits = 9;
csd->write_partial = 0;
+ csd->erase_size = 1;
break;
default:
printk(KERN_ERR "%s: unrecognised CSD structure version %d\n",
@@ -154,6 +162,8 @@ static int mmc_decode_csd(struct mmc_card *card)
return -EINVAL;
}

+ card->erase_size = csd->erase_size;
+
return 0;
}

@@ -179,10 +189,68 @@ static int mmc_decode_scr(struct mmc_card *card)
scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4);
scr->bus_widths = UNSTUFF_BITS(resp, 48, 4);

+ if (UNSTUFF_BITS(resp, 55, 1))
+ card->erased_byte = 0xFF;
+ else
+ card->erased_byte = 0x0;
+
return 0;
}

/*
+ * Fetch and process SD Status register.
+ */
+static int mmc_read_ssr(struct mmc_card *card)
+{
+ unsigned int au, es, et, eo;
+ int err, i;
+ u32 *ssr;
+
+ if (!(card->csd.cmdclass & CCC_APP_SPEC)) {
+ printk(KERN_WARNING "%s: card lacks mandatory SD Status "
+ "function.\n", mmc_hostname(card->host));
+ return 0;
+ }
+
+ ssr = kmalloc(64, GFP_KERNEL);
+ if (!ssr)
+ return -ENOMEM;
+
+ err = mmc_app_sd_status(card, ssr);
+ if (err) {
+ printk(KERN_WARNING "%s: problem reading SD Status "
+ "register.\n", mmc_hostname(card->host));
+ err = 0;
+ goto out;
+ }
+
+ for (i = 0; i < 16; i++)
+ ssr[i] = be32_to_cpu(ssr[i]);
+
+ /*
+ * UNSTUFF_BITS only works with four u32s so we have to offset the
+ * bitfield positions accordingly.
+ */
+ au = UNSTUFF_BITS(ssr, 428 - 384, 4);
+ if (au > 0 || au <= 9) {
+ card->ssr.au = 1 << (au + 4);
+ es = UNSTUFF_BITS(ssr, 408 - 384, 16);
+ et = UNSTUFF_BITS(ssr, 402 - 384, 6);
+ eo = UNSTUFF_BITS(ssr, 400 - 384, 2);
+ if (es && et) {
+ card->ssr.erase_timeout = (et * 1000) / es;
+ card->ssr.erase_offset = eo * 1000;
+ }
+ } else {
+ printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit "
+ "size.\n", mmc_hostname(card->host));
+ }
+out:
+ kfree(ssr);
+ return err;
+}
+
+/*
* Fetches and decodes switch information
*/
static int mmc_read_switch(struct mmc_card *card)
@@ -289,6 +357,8 @@ MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1],
card->raw_csd[2], card->raw_csd[3]);
MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]);
MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year);
+MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9);
+MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9);
MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev);
MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev);
MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid);
@@ -302,6 +372,8 @@ static struct attribute *sd_std_attrs[] = {
&dev_attr_csd.attr,
&dev_attr_scr.attr,
&dev_attr_date.attr,
+ &dev_attr_erase_size.attr,
+ &dev_attr_preferred_erase_size.attr,
&dev_attr_fwrev.attr,
&dev_attr_hwrev.attr,
&dev_attr_manfid.attr,
@@ -397,6 +469,16 @@ int mmc_sd_setup_card(struct mmc_host *host, struct mmc_card *card,
return err;

/*
+ * Fetch and process SD Status register.
+ */
+ err = mmc_read_ssr(card);
+ if (err)
+ return err;
+
+ /* Erase init depends on CSD and SSR */
+ mmc_init_erase(card);
+
+ /*
* Fetch switch information from card.
*/
err = mmc_read_switch(card);
diff --git a/drivers/mmc/core/sd_ops.c b/drivers/mmc/core/sd_ops.c
index 63772e7..797cdb5 100644
--- a/drivers/mmc/core/sd_ops.c
+++ b/drivers/mmc/core/sd_ops.c
@@ -346,3 +346,51 @@ int mmc_sd_switch(struct mmc_card *card, int mode, int group,
return 0;
}

+int mmc_app_sd_status(struct mmc_card *card, void *ssr)
+{
+ int err;
+ struct mmc_request mrq;
+ struct mmc_command cmd;
+ struct mmc_data data;
+ struct scatterlist sg;
+
+ BUG_ON(!card);
+ BUG_ON(!card->host);
+ BUG_ON(!ssr);
+
+ /* NOTE: caller guarantees ssr is heap-allocated */
+
+ err = mmc_app_cmd(card->host, card);
+ if (err)
+ return err;
+
+ memset(&mrq, 0, sizeof(struct mmc_request));
+ memset(&cmd, 0, sizeof(struct mmc_command));
+ memset(&data, 0, sizeof(struct mmc_data));
+
+ mrq.cmd = &cmd;
+ mrq.data = &data;
+
+ cmd.opcode = SD_APP_SD_STATUS;
+ cmd.arg = 0;
+ cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC;
+
+ data.blksz = 64;
+ data.blocks = 1;
+ data.flags = MMC_DATA_READ;
+ data.sg = &sg;
+ data.sg_len = 1;
+
+ sg_init_one(&sg, ssr, 64);
+
+ mmc_set_data_timeout(&data, card);
+
+ mmc_wait_for_req(card->host, &mrq);
+
+ if (cmd.error)
+ return cmd.error;
+ if (data.error)
+ return data.error;
+
+ return 0;
+}
diff --git a/drivers/mmc/core/sd_ops.h b/drivers/mmc/core/sd_ops.h
index 9742d8a..ffc2305 100644
--- a/drivers/mmc/core/sd_ops.h
+++ b/drivers/mmc/core/sd_ops.h
@@ -19,6 +19,7 @@ int mmc_send_relative_addr(struct mmc_host *host, unsigned int *rca);
int mmc_app_send_scr(struct mmc_card *card, u32 *scr);
int mmc_sd_switch(struct mmc_card *card, int mode, int group,
u8 value, u8 *resp);
+int mmc_app_sd_status(struct mmc_card *card, void *ssr);

#endif

diff --git a/include/linux/mmc/card.h b/include/linux/mmc/card.h
index 340d391..fce3f0f 100644
--- a/include/linux/mmc/card.h
+++ b/include/linux/mmc/card.h
@@ -31,6 +31,7 @@ struct mmc_csd {
unsigned int tacc_ns;
unsigned int r2w_factor;
unsigned int max_dtr;
+ unsigned int erase_size; /* In sectors */
unsigned int read_blkbits;
unsigned int write_blkbits;
unsigned int capacity;
@@ -42,9 +43,16 @@ struct mmc_csd {

struct mmc_ext_csd {
u8 rev;
+ u8 erase_group_def;
+ u8 sec_feature_support;
unsigned int sa_timeout; /* Units: 100ns */
unsigned int hs_max_dtr;
unsigned int sectors;
+ unsigned int hc_erase_size; /* In sectors */
+ unsigned int hc_erase_timeout; /* In milliseconds */
+ unsigned int sec_trim_mult; /* Secure trim multiplier */
+ unsigned int sec_erase_mult; /* Secure erase multiplier */
+ unsigned int trim_timeout; /* In milliseconds */
};

struct sd_scr {
@@ -54,6 +62,12 @@ struct sd_scr {
#define SD_SCR_BUS_WIDTH_4 (1<<2)
};

+struct sd_ssr {
+ unsigned int au; /* In sectors */
+ unsigned int erase_timeout; /* In milliseconds */
+ unsigned int erase_offset; /* In milliseconds */
+};
+
struct sd_switch_caps {
unsigned int hs_max_dtr;
};
@@ -103,6 +117,10 @@ struct mmc_card {
#define MMC_QUIRK_LENIENT_FN0 (1<<0) /* allow SDIO FN0 writes outside of the VS CCCR range */
#define MMC_QUIRK_BLKSZ_FOR_BYTE_MODE (1<<1) /* use func->cur_blksize */
/* for byte mode */
+ unsigned int erase_size; /* erase size in sectors */
+ unsigned int erase_shift; /* if erase unit is power 2 */
+ unsigned int pref_erase; /* in sectors */
+ u8 erased_byte; /* value of erased bytes */

u32 raw_cid[4]; /* raw card CID */
u32 raw_csd[4]; /* raw card CSD */
@@ -111,6 +129,7 @@ struct mmc_card {
struct mmc_csd csd; /* card specific */
struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */
struct sd_scr scr; /* extra SD information */
+ struct sd_ssr ssr; /* yet more SD information */
struct sd_switch_caps sw_caps; /* switch (CMD6) caps */

unsigned int sdio_funcs; /* number of SDIO functions */
diff --git a/include/linux/mmc/core.h b/include/linux/mmc/core.h
index e4898e9..7429033 100644
--- a/include/linux/mmc/core.h
+++ b/include/linux/mmc/core.h
@@ -92,6 +92,8 @@ struct mmc_command {
* actively failing requests
*/

+ unsigned int erase_timeout; /* in milliseconds */
+
struct mmc_data *data; /* data segment associated with cmd */
struct mmc_request *mrq; /* associated request */
};
@@ -134,6 +136,23 @@ extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int);
extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *,
struct mmc_command *, int);

+#define MMC_ERASE_ARG 0x00000000
+#define MMC_SECURE_ERASE_ARG 0x80000000
+#define MMC_TRIM_ARG 0x00000001
+#define MMC_SECURE_TRIM1_ARG 0x80000001
+#define MMC_SECURE_TRIM2_ARG 0x80008000
+
+#define MMC_SECURE_ARGS 0x80000000
+#define MMC_TRIM_ARGS 0x00008001
+
+extern int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
+ unsigned int arg);
+extern int mmc_can_erase(struct mmc_card *card);
+extern int mmc_can_trim(struct mmc_card *card);
+extern int mmc_can_secure_erase_trim(struct mmc_card *card);
+extern int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from,
+ unsigned int nr);
+
extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *);
extern unsigned int mmc_align_data_size(struct mmc_card *, unsigned int);

diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
index 1239f37..0e802d3 100644
--- a/include/linux/mmc/host.h
+++ b/include/linux/mmc/host.h
@@ -156,6 +156,7 @@ struct mmc_host {
#define MMC_CAP_DISABLE (1 << 7) /* Can the host be disabled */
#define MMC_CAP_NONREMOVABLE (1 << 8) /* Nonremovable e.g. eMMC */
#define MMC_CAP_WAIT_WHILE_BUSY (1 << 9) /* Waits while card is busy */
+#define MMC_CAP_ERASE (1 << 10) /* Allow erase/trim commands */

mmc_pm_flag_t pm_caps; /* supported pm features */

diff --git a/include/linux/mmc/mmc.h b/include/linux/mmc/mmc.h
index 52ce988..dd11ae5 100644
--- a/include/linux/mmc/mmc.h
+++ b/include/linux/mmc/mmc.h
@@ -251,13 +251,21 @@ struct _mmc_csd {
* EXT_CSD fields
*/

-#define EXT_CSD_BUS_WIDTH 183 /* R/W */
-#define EXT_CSD_HS_TIMING 185 /* R/W */
-#define EXT_CSD_CARD_TYPE 196 /* RO */
-#define EXT_CSD_STRUCTURE 194 /* RO */
-#define EXT_CSD_REV 192 /* RO */
-#define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
-#define EXT_CSD_S_A_TIMEOUT 217
+#define EXT_CSD_ERASE_GROUP_DEF 175 /* R/W */
+#define EXT_CSD_ERASED_MEM_CONT 181 /* RO */
+#define EXT_CSD_BUS_WIDTH 183 /* R/W */
+#define EXT_CSD_HS_TIMING 185 /* R/W */
+#define EXT_CSD_REV 192 /* RO */
+#define EXT_CSD_STRUCTURE 194 /* RO */
+#define EXT_CSD_CARD_TYPE 196 /* RO */
+#define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */
+#define EXT_CSD_S_A_TIMEOUT 217 /* RO */
+#define EXT_CSD_ERASE_TIMEOUT_MULT 223 /* RO */
+#define EXT_CSD_HC_ERASE_GRP_SIZE 224 /* RO */
+#define EXT_CSD_SEC_TRIM_MULT 229 /* RO */
+#define EXT_CSD_SEC_ERASE_MULT 230 /* RO */
+#define EXT_CSD_SEC_FEATURE_SUPPORT 231 /* RO */
+#define EXT_CSD_TRIM_MULT 232 /* RO */

/*
* EXT_CSD field definitions
@@ -275,6 +283,10 @@ struct _mmc_csd {
#define EXT_CSD_BUS_WIDTH_4 1 /* Card is in 4 bit mode */
#define EXT_CSD_BUS_WIDTH_8 2 /* Card is in 8 bit mode */

+#define EXT_CSD_SEC_ER_EN BIT(0)
+#define EXT_CSD_SEC_BD_BLK_EN BIT(2)
+#define EXT_CSD_SEC_GB_CL_EN BIT(4)
+
/*
* MMC_SWITCH access modes
*/
diff --git a/include/linux/mmc/sd.h b/include/linux/mmc/sd.h
index f310062..3fd85e0 100644
--- a/include/linux/mmc/sd.h
+++ b/include/linux/mmc/sd.h
@@ -21,8 +21,13 @@
/* class 10 */
#define SD_SWITCH 6 /* adtc [31:0] See below R1 */

+ /* class 5 */
+#define SD_ERASE_WR_BLK_START 32 /* ac [31:0] data addr R1 */
+#define SD_ERASE_WR_BLK_END 33 /* ac [31:0] data addr R1 */
+
/* Application commands */
#define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */
+#define SD_APP_SD_STATUS 13 /* adtc R1 */
#define SD_APP_SEND_NUM_WR_BLKS 22 /* adtc R1 */
#define SD_APP_OP_COND 41 /* bcr [31:0] OCR R3 */
#define SD_APP_SEND_SCR 51 /* adtc R1 */
--
1.6.3.3

2010-07-07 11:18:48

by Adrian Hunter

[permalink] [raw]
Subject: [PATCH V4 5/5] mmc_block: Add support for secure discard

>From 548ec42c86a88092b92597ff2774691e5498974f Mon Sep 17 00:00:00 2001
From: Adrian Hunter <[email protected]>
Date: Wed, 23 Jun 2010 15:55:50 +0300
Subject: [PATCH 5/5] mmc_block: Add support for secure discard

Secure discard is implemented by Secure Trim if the discard
is unaligned or Secure Erase otherwise.

Signed-off-by: Adrian Hunter <[email protected]>
---
drivers/mmc/card/block.c | 46 +++++++++++++++++++++++++++++++++++++++++++---
drivers/mmc/card/queue.c | 3 +++
2 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index adb8314..0265712 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -275,6 +275,42 @@ out:
return err ? 0 : 1;
}

+static int mmc_blk_issue_secdiscard_rq(struct mmc_queue *mq,
+ struct request *req)
+{
+ struct mmc_blk_data *md = mq->data;
+ struct mmc_card *card = md->queue.card;
+ unsigned int from, nr, arg;
+ int err = 0;
+
+ mmc_claim_host(card->host);
+
+ if (!mmc_can_secure_erase_trim(card)) {
+ err = -EOPNOTSUPP;
+ goto out;
+ }
+
+ from = blk_rq_pos(req);
+ nr = blk_rq_sectors(req);
+
+ if (mmc_can_trim(card) && !mmc_erase_group_aligned(card, from, nr))
+ arg = MMC_SECURE_TRIM1_ARG;
+ else
+ arg = MMC_SECURE_ERASE_ARG;
+
+ err = mmc_erase(card, from, nr, arg);
+ if (!err && arg == MMC_SECURE_TRIM1_ARG)
+ err = mmc_erase(card, from, nr, MMC_SECURE_TRIM2_ARG);
+out:
+ spin_lock_irq(&md->lock);
+ __blk_end_request(req, err, blk_rq_bytes(req));
+ spin_unlock_irq(&md->lock);
+
+ mmc_release_host(card->host);
+
+ return err ? 0 : 1;
+}
+
static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
{
struct mmc_blk_data *md = mq->data;
@@ -505,10 +541,14 @@ static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)

static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
{
- if (req->cmd_flags & REQ_DISCARD)
- return mmc_blk_issue_discard_rq(mq, req);
- else
+ if (req->cmd_flags & REQ_DISCARD) {
+ if (req->cmd_flags & REQ_SECURE)
+ return mmc_blk_issue_secdiscard_rq(mq, req);
+ else
+ return mmc_blk_issue_discard_rq(mq, req);
+ } else {
return mmc_blk_issue_rw_rq(mq, req);
+ }
}

static inline int mmc_blk_readonly(struct mmc_card *card)
diff --git a/drivers/mmc/card/queue.c b/drivers/mmc/card/queue.c
index 0add067..a7876d5 100644
--- a/drivers/mmc/card/queue.c
+++ b/drivers/mmc/card/queue.c
@@ -141,6 +141,9 @@ int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock
mq->queue->limits.discard_alignment =
card->erase_size << 9;
}
+ if (mmc_can_secure_erase_trim(card))
+ queue_flag_set_unlocked(QUEUE_FLAG_SECDISCARD,
+ mq->queue);
}

#ifdef CONFIG_MMC_BLOCK_BOUNCE
--
1.6.3.3

2010-07-07 11:19:00

by Adrian Hunter

[permalink] [raw]
Subject: [PATCH V4 2/5] mmc_block: Add discard support

>From 3f40c236b15902aa878640d89bc11dd0bc4511fc Mon Sep 17 00:00:00 2001
From: Adrian Hunter <[email protected]>
Date: Thu, 3 Jun 2010 10:47:12 +0300
Subject: [PATCH 2/5] mmc_block: Add discard support

Enable MMC to service discard requests. In the case of SD
and MMC cards that do not support trim, discards become
erases. In the case of cards (MMC) that only allow erases
in multiples of erase group size, round to the nearest
completely discarded erase group.

Signed-off-by: Adrian Hunter <[email protected]>
---
drivers/mmc/card/block.c | 42 +++++++++++++++++++++++++++++++++++++++++-
drivers/mmc/card/queue.c | 16 ++++++++++++++--
2 files changed, 55 insertions(+), 3 deletions(-)

diff --git a/drivers/mmc/card/block.c b/drivers/mmc/card/block.c
index cb9fbc8..adb8314 100644
--- a/drivers/mmc/card/block.c
+++ b/drivers/mmc/card/block.c
@@ -242,7 +242,40 @@ static u32 get_card_status(struct mmc_card *card, struct request *req)
return cmd.resp[0];
}

-static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
+static int mmc_blk_issue_discard_rq(struct mmc_queue *mq, struct request *req)
+{
+ struct mmc_blk_data *md = mq->data;
+ struct mmc_card *card = md->queue.card;
+ unsigned int from, nr, arg;
+ int err = 0;
+
+ mmc_claim_host(card->host);
+
+ if (!mmc_can_erase(card)) {
+ err = -EOPNOTSUPP;
+ goto out;
+ }
+
+ from = blk_rq_pos(req);
+ nr = blk_rq_sectors(req);
+
+ if (mmc_can_trim(card))
+ arg = MMC_TRIM_ARG;
+ else
+ arg = MMC_ERASE_ARG;
+
+ err = mmc_erase(card, from, nr, arg);
+out:
+ spin_lock_irq(&md->lock);
+ __blk_end_request(req, err, blk_rq_bytes(req));
+ spin_unlock_irq(&md->lock);
+
+ mmc_release_host(card->host);
+
+ return err ? 0 : 1;
+}
+
+static int mmc_blk_issue_rw_rq(struct mmc_queue *mq, struct request *req)
{
struct mmc_blk_data *md = mq->data;
struct mmc_card *card = md->queue.card;
@@ -470,6 +503,13 @@ static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
return 0;
}

+static int mmc_blk_issue_rq(struct mmc_queue *mq, struct request *req)
+{
+ if (req->cmd_flags & REQ_DISCARD)
+ return mmc_blk_issue_discard_rq(mq, req);
+ else
+ return mmc_blk_issue_rw_rq(mq, req);
+}

static inline int mmc_blk_readonly(struct mmc_card *card)
{
diff --git a/drivers/mmc/card/queue.c b/drivers/mmc/card/queue.c
index ec92bcb..0add067 100644
--- a/drivers/mmc/card/queue.c
+++ b/drivers/mmc/card/queue.c
@@ -30,9 +30,9 @@
static int mmc_prep_request(struct request_queue *q, struct request *req)
{
/*
- * We only like normal block requests.
+ * We only like normal block requests and discards.
*/
- if (req->cmd_type != REQ_TYPE_FS) {
+ if (req->cmd_type != REQ_TYPE_FS && !(req->cmd_flags & REQ_DISCARD)) {
blk_dump_rq_flags(req, "MMC bad request");
return BLKPREP_KILL;
}
@@ -130,6 +130,18 @@ int mmc_init_queue(struct mmc_queue *mq, struct mmc_card *card, spinlock_t *lock
blk_queue_prep_rq(mq->queue, mmc_prep_request);
blk_queue_ordered(mq->queue, QUEUE_ORDERED_DRAIN, NULL);
queue_flag_set_unlocked(QUEUE_FLAG_NONROT, mq->queue);
+ if (mmc_can_erase(card)) {
+ queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mq->queue);
+ mq->queue->limits.max_discard_sectors = UINT_MAX;
+ if (card->erased_byte == 0)
+ mq->queue->limits.discard_zeroes_data = 1;
+ if (!mmc_can_trim(card) && is_power_of_2(card->erase_size)) {
+ mq->queue->limits.discard_granularity =
+ card->erase_size << 9;
+ mq->queue->limits.discard_alignment =
+ card->erase_size << 9;
+ }
+ }

#ifdef CONFIG_MMC_BLOCK_BOUNCE
if (host->max_hw_segs == 1) {
--
1.6.3.3

2010-07-07 11:19:13

by Adrian Hunter

[permalink] [raw]
Subject: [PATCH V4 4/5] block: Add secure discard

>From 1cdd5b576bfafcd53857ed4d37830446e96e8206 Mon Sep 17 00:00:00 2001
From: Adrian Hunter <[email protected]>
Date: Wed, 23 Jun 2010 15:41:38 +0300
Subject: [PATCH 4/5] block: Add secure discard

Secure discard is the same as discard except that all copies
of the discarded sectors (perhaps created by garbage collection)
must also be erased.

Signed-off-by: Adrian Hunter <[email protected]>
---
block/blk-core.c | 5 ++++-
block/blk-lib.c | 6 ++++++
block/compat_ioctl.c | 1 +
block/elevator.c | 6 ++++++
block/ioctl.c | 15 ++++++++++-----
include/linux/bio.h | 2 ++
include/linux/blkdev.h | 7 ++++++-
include/linux/fs.h | 2 ++
kernel/trace/blktrace.c | 8 ++++++++
9 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/block/blk-core.c b/block/blk-core.c
index 3c37894..7931081 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -1513,7 +1513,10 @@ static inline void __generic_make_request(struct bio *bio)
if (bio_check_eod(bio, nr_sectors))
goto end_io;

- if ((bio->bi_rw & REQ_DISCARD) && !blk_queue_discard(q)) {
+ if ((bio->bi_rw & REQ_DISCARD) &&
+ (!blk_queue_discard(q) ||
+ ((bio->bi_rw & REQ_SECURE) &&
+ !blk_queue_secdiscard(q)))) {
err = -EOPNOTSUPP;
goto end_io;
}
diff --git a/block/blk-lib.c b/block/blk-lib.c
index e16185b..ba8c0f9 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -50,6 +50,12 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
if (!blk_queue_discard(q))
return -EOPNOTSUPP;

+ if (flags & BLKDEV_IFL_SECURE) {
+ if (!blk_queue_secdiscard(q))
+ return -EOPNOTSUPP;
+ type |= DISCARD_SECURE;
+ }
+
while (nr_sects && !ret) {
unsigned int max_discard_sectors =
min(q->limits.max_discard_sectors, UINT_MAX >> 9);
diff --git a/block/compat_ioctl.c b/block/compat_ioctl.c
index f26051f..24a146d 100644
--- a/block/compat_ioctl.c
+++ b/block/compat_ioctl.c
@@ -753,6 +753,7 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
case BLKFLSBUF:
case BLKROSET:
case BLKDISCARD:
+ case BLKSECDISCARD:
/*
* the ones below are implemented in blkdev_locked_ioctl,
* but we call blkdev_ioctl, which gets the lock for us
diff --git a/block/elevator.c b/block/elevator.c
index 816a7c8..ec585c9 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -83,6 +83,12 @@ int elv_rq_merge_ok(struct request *rq, struct bio *bio)
return 0;

/*
+ * Don't merge discard requests and secure discard requests
+ */
+ if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
+ return 0;
+
+ /*
* different data direction or already started, don't merge
*/
if (bio_data_dir(bio) != rq_data_dir(rq))
diff --git a/block/ioctl.c b/block/ioctl.c
index 6f3db6f..afcfe48 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -114,8 +114,10 @@ static int blkdev_reread_part(struct block_device *bdev)
}

static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
- uint64_t len)
+ uint64_t len, int secure)
{
+ unsigned long flags = BLKDEV_IFL_WAIT;
+
if (start & 511)
return -EINVAL;
if (len & 511)
@@ -125,8 +127,9 @@ static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,

if (start + len > (bdev->bd_inode->i_size >> 9))
return -EINVAL;
- return blkdev_issue_discard(bdev, start, len, GFP_KERNEL,
- BLKDEV_IFL_WAIT);
+ if (secure)
+ flags |= BLKDEV_IFL_SECURE;
+ return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
}

static int put_ushort(unsigned long arg, unsigned short val)
@@ -226,7 +229,8 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
unlock_kernel();
return 0;

- case BLKDISCARD: {
+ case BLKDISCARD:
+ case BLKSECDISCARD: {
uint64_t range[2];

if (!(mode & FMODE_WRITE))
@@ -235,7 +239,8 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
if (copy_from_user(range, (void __user *)arg, sizeof(range)))
return -EFAULT;

- return blk_ioctl_discard(bdev, range[0], range[1]);
+ return blk_ioctl_discard(bdev, range[0], range[1],
+ cmd == BLKSECDISCARD);
}

case HDIO_GETGEO: {
diff --git a/include/linux/bio.h b/include/linux/bio.h
index 4d379c8..147e631 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -176,6 +176,7 @@ enum rq_flag_bits {
__REQ_INTEGRITY, /* integrity metadata has been remapped */
__REQ_IO_STAT, /* account I/O stat */
__REQ_MIXED_MERGE, /* merge of different types, fail separately */
+ __REQ_SECURE, /* secure discard (used with __REQ_DISCARD) */
__REQ_NR_BITS, /* stops here */
};

@@ -215,6 +216,7 @@ enum rq_flag_bits {
#define REQ_INTEGRITY (1 << __REQ_INTEGRITY)
#define REQ_IO_STAT (1 << __REQ_IO_STAT)
#define REQ_MIXED_MERGE (1 << __REQ_MIXED_MERGE)
+#define REQ_SECURE (1 << __REQ_SECURE)

/*
* upper 16 bits of bi_rw define the io priority of this bio
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index 204fbe2..da643f4 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -404,6 +404,7 @@ struct request_queue
#define QUEUE_FLAG_DISCARD 16 /* supports DISCARD */
#define QUEUE_FLAG_NOXMERGES 17 /* No extended merges */
#define QUEUE_FLAG_ADD_RANDOM 18 /* Contributes to random pool */
+#define QUEUE_FLAG_SECDISCARD 19 /* supports SECDISCARD */

#define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
(1 << QUEUE_FLAG_CLUSTER) | \
@@ -539,6 +540,8 @@ enum {
#define blk_queue_stackable(q) \
test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
#define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
+#define blk_queue_secdiscard(q) (blk_queue_discard(q) && \
+ test_bit(QUEUE_FLAG_SECDISCARD, &(q)->queue_flags))

#define blk_noretry_request(rq) \
((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
@@ -931,10 +934,12 @@ static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt,
}
enum{
BLKDEV_WAIT, /* wait for completion */
- BLKDEV_BARRIER, /*issue request with barrier */
+ BLKDEV_BARRIER, /* issue request with barrier */
+ BLKDEV_SECURE, /* secure discard */
};
#define BLKDEV_IFL_WAIT (1 << BLKDEV_WAIT)
#define BLKDEV_IFL_BARRIER (1 << BLKDEV_BARRIER)
+#define BLKDEV_IFL_SECURE (1 << BLKDEV_SECURE)
extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *,
unsigned long);
extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
diff --git a/include/linux/fs.h b/include/linux/fs.h
index dc9d185..2f5df1f 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -176,6 +176,7 @@ struct inodes_stat_t {
*/
#define DISCARD_NOBARRIER (WRITE | REQ_DISCARD)
#define DISCARD_BARRIER (WRITE | REQ_DISCARD | REQ_HARDBARRIER)
+#define DISCARD_SECURE (DISCARD_NOBARRIER | REQ_SECURE)

#define SEL_IN 1
#define SEL_OUT 2
@@ -318,6 +319,7 @@ struct inodes_stat_t {
#define BLKALIGNOFF _IO(0x12,122)
#define BLKPBSZGET _IO(0x12,123)
#define BLKDISCARDZEROES _IO(0x12,124)
+#define BLKSECDISCARD _IO(0x12,125)

#define BMAP_IOCTL 1 /* obsolete - kept for compatibility */
#define FIBMAP _IO(0x00,1) /* bmap access */
diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index 3b4a695..a711b47 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -667,6 +667,9 @@ static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
if (rq->cmd_flags & REQ_DISCARD)
rw |= REQ_DISCARD;

+ if (rq->cmd_flags & REQ_SECURE)
+ rw |= REQ_SECURE;
+
if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
what |= BLK_TC_ACT(BLK_TC_PC);
__blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
@@ -1773,6 +1776,8 @@ void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
rwbs[i++] = 'S';
if (rw & REQ_META)
rwbs[i++] = 'M';
+ if (rw & REQ_SECURE)
+ rwbs[i++] = 'E';

rwbs[i] = '\0';
}
@@ -1785,6 +1790,9 @@ void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
if (rq->cmd_flags & REQ_DISCARD)
rw |= REQ_DISCARD;

+ if (rq->cmd_flags & REQ_SECURE)
+ rw |= REQ_SECURE;
+
bytes = blk_rq_bytes(rq);

blk_fill_rwbs(rwbs, rw, bytes);
--
1.6.3.3

2010-07-07 15:07:42

by Ben Gardiner

[permalink] [raw]
Subject: Re: [PATCH V4 1/5] mmc: Add erase, secure erase, trim and secure trim operations

On Wed, Jul 7, 2010 at 7:17 AM, Adrian Hunter <[email protected]> wrote:
> From 7f01ad3c4be6ec09318176db12db66f353b526e0 Mon Sep 17 00:00:00 2001

> SD/MMC cards tend to support an erase operation. ?In addition,
> eMMC v4.4 cards can support secure erase, trim and secure trim
> operations that are all variants of the basic erase command.

This is great. I am interested primarily in SD media.

Please forgive my naive perspective: it seems that with the features
enabled by this patchset and a filesystem that is capable of issuing
erase block commands, the wear-leveling on SD media will be improved
-- much like with CF TRIM commands. Do you also think that is the
case? I would be very interested in hearing your expert opinion on
this.

I have a couple comments regarding mostly the SD support introduced in
this patch. Patches 2..5 of 5 seem fine to me but I'm not sure I'm
qualified to add acks or reviewed-by's.

> +int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
> + ? ? ? ? ? ? unsigned int arg)
> +{
> + ? ? ? unsigned int rem, to = from + nr;
> +
> + ? ? ? if (!(card->host->caps & MMC_CAP_ERASE) ||
> + ? ? ? ? ? !(card->csd.cmdclass & CCC_ERASE))
> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
> +
> + ? ? ? if (!card->erase_size)
> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
> +
> + ? ? ? if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
> +
> + ? ? ? if ((arg & MMC_SECURE_ARGS) &&
> + ? ? ? ? ? !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
> +
> + ? ? ? if ((arg & MMC_TRIM_ARGS) &&
> + ? ? ? ? ? !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
> +

> +int mmc_can_trim(struct mmc_card *card)
> +{
> + ? ? ? if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
> + ? ? ? ? ? ? ? return 1;
> + ? ? ? return 0;
> +}
> +EXPORT_SYMBOL(mmc_can_trim);

It looks like mmc_can_trim(card) would return true when
mmc_card_sd(card) is true; but the mmc_erase function will return
-EOPNOTSUPP in such a case. I think that this results in the
mmc_blk_issue_discard_rq function (from 2/5) also returning
-EOPNOTSUPP whenever mmc_card_sd(card) is true:

>From 2/5:
+ if (mmc_can_trim(card))
+ arg = MMC_TRIM_ARG;
+ else
+ arg = MMC_ERASE_ARG;
+
+ err = mmc_erase(card, from, nr, arg);

Also, there is some duplication between the sec_feature_support
checking in mmc_erase and in the mmc_can* functions; If mmc_erase
could call the mmc_can_* functions then the the bit-checking logic
could be centralized.


> /*
> + * Fetch and process SD Status register.
> + */
> +static int mmc_read_ssr(struct mmc_card *card)
> +{

It looks like the conventional function prefix for SD-specific
functions in the rest of this file is mmc_sd_ ; 'mmc_read_ssr' ->
'mmc_sd_read_ssr' or -> 'mmc_read_sd_sr' perhaps?

> + ssr = kmalloc(64, GFP_KERNEL);

Why '64' instead of 'sizeof(*ssr)' ?

Best Regards,
Ben Gardiner

---
Nanometrics Inc.
http://www.nanometrics.ca

2010-07-07 19:06:17

by Adrian Hunter

[permalink] [raw]
Subject: Re: [PATCH V4 1/5] mmc: Add erase, secure erase, trim and secure trim operations

Ben Gardiner wrote:
> On Wed, Jul 7, 2010 at 7:17 AM, Adrian Hunter <[email protected]> wrote:
>> From 7f01ad3c4be6ec09318176db12db66f353b526e0 Mon Sep 17 00:00:00 2001
>
>> SD/MMC cards tend to support an erase operation. In addition,
>> eMMC v4.4 cards can support secure erase, trim and secure trim
>> operations that are all variants of the basic erase command.
>
> This is great. I am interested primarily in SD media.
>
> Please forgive my naive perspective: it seems that with the features
> enabled by this patchset and a filesystem that is capable of issuing
> erase block commands, the wear-leveling on SD media will be improved
> -- much like with CF TRIM commands. Do you also think that is the
> case? I would be very interested in hearing your expert opinion on
> this.

I am sorry but I don't know. Wear-levelling in cards tends to be kept
secret by the manufacturers. However, it is not clear to me that cards
bother to record whether or not anything has been erased. For example,
erase a card twice - it takes the same amount of time the second time
as the first time, whereas if the card knew it was already erased, why
wasn't the second time much quicker?

>
> I have a couple comments regarding mostly the SD support introduced in
> this patch. Patches 2..5 of 5 seem fine to me but I'm not sure I'm
> qualified to add acks or reviewed-by's.
>
>> +int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
>> + unsigned int arg)
>> +{
>> + unsigned int rem, to = from + nr;
>> +
>> + if (!(card->host->caps & MMC_CAP_ERASE) ||
>> + !(card->csd.cmdclass & CCC_ERASE))
>> + return -EOPNOTSUPP;
>> +
>> + if (!card->erase_size)
>> + return -EOPNOTSUPP;
>> +
>> + if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
>> + return -EOPNOTSUPP;
>> +
>> + if ((arg & MMC_SECURE_ARGS) &&
>> + !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
>> + return -EOPNOTSUPP;
>> +
>> + if ((arg & MMC_TRIM_ARGS) &&
>> + !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
>> + return -EOPNOTSUPP;
>> +
>
>> +int mmc_can_trim(struct mmc_card *card)
>> +{
>> + if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
>> + return 1;
>> + return 0;
>> +}
>> +EXPORT_SYMBOL(mmc_can_trim);
>
> It looks like mmc_can_trim(card) would return true when
> mmc_card_sd(card) is true;

It will return false for SD. card->ext_csd.sec_feature_support
is only used by MMC.

> but the mmc_erase function will return
> -EOPNOTSUPP in such a case. I think that this results in the
> mmc_blk_issue_discard_rq function (from 2/5) also returning
> -EOPNOTSUPP whenever mmc_card_sd(card) is true:
>
>>From 2/5:
> + if (mmc_can_trim(card))
> + arg = MMC_TRIM_ARG;
> + else
> + arg = MMC_ERASE_ARG;
> +
> + err = mmc_erase(card, from, nr, arg);
>
> Also, there is some duplication between the sec_feature_support
> checking in mmc_erase and in the mmc_can* functions; If mmc_erase
> could call the mmc_can_* functions then the the bit-checking logic
> could be centralized.

I can do that if there is a V5 but I do not think it is worth
rolling another set of patches just for that.

>
>
>> /*
>> + * Fetch and process SD Status register.
>> + */
>> +static int mmc_read_ssr(struct mmc_card *card)
>> +{
>
> It looks like the conventional function prefix for SD-specific
> functions in the rest of this file is mmc_sd_ ; 'mmc_read_ssr' ->
> 'mmc_sd_read_ssr' or -> 'mmc_read_sd_sr' perhaps?

Well there is also mmc_decode_*, and mmc_read_switch so the other
functions that do smilar things also do not follow that convention.

>
>> + ssr = kmalloc(64, GFP_KERNEL);
>
> Why '64' instead of 'sizeof(*ssr)' ?

sizeof(*ssr) is 4

>
> Best Regards,
> Ben Gardiner
>
> ---
> Nanometrics Inc.
> http://www.nanometrics.ca
>

2010-07-07 20:41:20

by Ben Gardiner

[permalink] [raw]
Subject: Re: [PATCH V4 1/5] mmc: Add erase, secure erase, trim and secure trim operations

On Wed, Jul 7, 2010 at 3:05 PM, Adrian Hunter <[email protected]> wrote:
> Ben Gardiner wrote:
>>
>> On Wed, Jul 7, 2010 at 7:17 AM, Adrian Hunter <[email protected]>
>> wrote:
>>>
>>> From 7f01ad3c4be6ec09318176db12db66f353b526e0 Mon Sep 17 00:00:00 2001
>>
>>> SD/MMC cards tend to support an erase operation. ?In addition,
>>> eMMC v4.4 cards can support secure erase, trim and secure trim
>>> operations that are all variants of the basic erase command.
>>
>> This is great. I am interested primarily in SD media.
>>
>> Please forgive my naive perspective: it seems that with the features
>> enabled by this patchset and a filesystem that is capable of issuing
>> erase block commands, the wear-leveling on SD media will be improved
>> -- much like with CF TRIM commands. Do you also think that is the
>> case? I would be very interested in hearing your expert opinion on
>> this.
>
> I am sorry but I don't know. ?Wear-levelling in cards tends to be kept
> secret by the manufacturers. ?However, it is not clear to me that cards
> bother to record whether or not anything has been erased. ? For example,
> erase a card twice - it takes the same amount of time the second time
> as the first time, whereas if the card knew it was already erased, why
> wasn't the second time much quicker?

No worries. I'm happy to hear your opinion anyways.

Interesting observation re: erase time of cards, I assume that is
"erase" as in the SD erase operations as proposed in this patch as
opposed to erase as in 'mkfs'.

>>
>> I have a couple comments regarding mostly the SD support introduced in
>> this patch. Patches 2..5 of 5 seem fine to me but I'm not sure I'm
>> qualified to add acks or reviewed-by's.
>>
>>> +int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr,
>>> + ? ? ? ? ? ? unsigned int arg)
>>> +{
>>> + ? ? ? unsigned int rem, to = from + nr;
>>> +
>>> + ? ? ? if (!(card->host->caps & MMC_CAP_ERASE) ||
>>> + ? ? ? ? ? !(card->csd.cmdclass & CCC_ERASE))
>>> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
>>> +
>>> + ? ? ? if (!card->erase_size)
>>> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
>>> +
>>> + ? ? ? if (mmc_card_sd(card) && arg != MMC_ERASE_ARG)
>>> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
>>> +
>>> + ? ? ? if ((arg & MMC_SECURE_ARGS) &&
>>> + ? ? ? ? ? !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN))
>>> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
>>> +
>>> + ? ? ? if ((arg & MMC_TRIM_ARGS) &&
>>> + ? ? ? ? ? !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN))
>>> + ? ? ? ? ? ? ? return -EOPNOTSUPP;
>>> +
>>
>>> +int mmc_can_trim(struct mmc_card *card)
>>> +{
>>> + ? ? ? if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)
>>> + ? ? ? ? ? ? ? return 1;
>>> + ? ? ? return 0;
>>> +}
>>> +EXPORT_SYMBOL(mmc_can_trim);
>>
>> It looks like mmc_can_trim(card) would return true when
>> mmc_card_sd(card) is true;
>
> It will return false for SD. ?card->ext_csd.sec_feature_support
> is only used by MMC.

Makes sense now, thanks.

>>> /*
>>> + * Fetch and process SD Status register.
>>> + */
>>> +static int mmc_read_ssr(struct mmc_card *card)
>>> +{
>>
>> It looks like the conventional function prefix for SD-specific
>> functions in the rest of this file is mmc_sd_ ; 'mmc_read_ssr' ->
>> 'mmc_sd_read_ssr' ?or -> 'mmc_read_sd_sr' perhaps?
>
> Well there is also mmc_decode_*, and mmc_read_switch so the other
> functions that do smilar things also do not follow that convention.

Good point.

>>
>>> + ? ? ? ssr = kmalloc(64, GFP_KERNEL);
>>
>> Why '64' instead of 'sizeof(*ssr)' ?
>
> sizeof(*ssr) is 4

Right -- my mistake :)

I guess I was _thinking_ 16*sizeof(*ssr) or SSR_SIZE*sizeof(*ssr)
instead of a magic number '64'. I see now that this wouldn't be the
only kmalloc of a magic number in sd.c -- so I'll stop being so picky.

Reviewed-by: Ben Gardiner <[email protected]>

---
Ben Gardiner
Nanometrics Inc.
http://www.nanometrics.ca

2010-07-13 06:41:51

by Adrian Hunter

[permalink] [raw]
Subject: Re: [PATCH V4 4/5] block: Add secure discard

Hunter Adrian (Nokia-MS/Helsinki) wrote:
>>From 1cdd5b576bfafcd53857ed4d37830446e96e8206 Mon Sep 17 00:00:00 2001
> From: Adrian Hunter <[email protected]>
> Date: Wed, 23 Jun 2010 15:41:38 +0300
> Subject: [PATCH 4/5] block: Add secure discard
>
> Secure discard is the same as discard except that all copies
> of the discarded sectors (perhaps created by garbage collection)
> must also be erased.
>
> Signed-off-by: Adrian Hunter <[email protected]>

Christoph will you comment on this patch? Is it doing what you
asked?

> ---
> block/blk-core.c | 5 ++++-
> block/blk-lib.c | 6 ++++++
> block/compat_ioctl.c | 1 +
> block/elevator.c | 6 ++++++
> block/ioctl.c | 15 ++++++++++-----
> include/linux/bio.h | 2 ++
> include/linux/blkdev.h | 7 ++++++-
> include/linux/fs.h | 2 ++
> kernel/trace/blktrace.c | 8 ++++++++
> 9 files changed, 45 insertions(+), 7 deletions(-)
>
> diff --git a/block/blk-core.c b/block/blk-core.c
> index 3c37894..7931081 100644
> --- a/block/blk-core.c
> +++ b/block/blk-core.c
> @@ -1513,7 +1513,10 @@ static inline void __generic_make_request(struct bio *bio)
> if (bio_check_eod(bio, nr_sectors))
> goto end_io;
>
> - if ((bio->bi_rw & REQ_DISCARD) && !blk_queue_discard(q)) {
> + if ((bio->bi_rw & REQ_DISCARD) &&
> + (!blk_queue_discard(q) ||
> + ((bio->bi_rw & REQ_SECURE) &&
> + !blk_queue_secdiscard(q)))) {
> err = -EOPNOTSUPP;
> goto end_io;
> }
> diff --git a/block/blk-lib.c b/block/blk-lib.c
> index e16185b..ba8c0f9 100644
> --- a/block/blk-lib.c
> +++ b/block/blk-lib.c
> @@ -50,6 +50,12 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
> if (!blk_queue_discard(q))
> return -EOPNOTSUPP;
>
> + if (flags & BLKDEV_IFL_SECURE) {
> + if (!blk_queue_secdiscard(q))
> + return -EOPNOTSUPP;
> + type |= DISCARD_SECURE;
> + }
> +
> while (nr_sects && !ret) {
> unsigned int max_discard_sectors =
> min(q->limits.max_discard_sectors, UINT_MAX >> 9);
> diff --git a/block/compat_ioctl.c b/block/compat_ioctl.c
> index f26051f..24a146d 100644
> --- a/block/compat_ioctl.c
> +++ b/block/compat_ioctl.c
> @@ -753,6 +753,7 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
> case BLKFLSBUF:
> case BLKROSET:
> case BLKDISCARD:
> + case BLKSECDISCARD:
> /*
> * the ones below are implemented in blkdev_locked_ioctl,
> * but we call blkdev_ioctl, which gets the lock for us
> diff --git a/block/elevator.c b/block/elevator.c
> index 816a7c8..ec585c9 100644
> --- a/block/elevator.c
> +++ b/block/elevator.c
> @@ -83,6 +83,12 @@ int elv_rq_merge_ok(struct request *rq, struct bio *bio)
> return 0;
>
> /*
> + * Don't merge discard requests and secure discard requests
> + */
> + if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
> + return 0;
> +
> + /*
> * different data direction or already started, don't merge
> */
> if (bio_data_dir(bio) != rq_data_dir(rq))
> diff --git a/block/ioctl.c b/block/ioctl.c
> index 6f3db6f..afcfe48 100644
> --- a/block/ioctl.c
> +++ b/block/ioctl.c
> @@ -114,8 +114,10 @@ static int blkdev_reread_part(struct block_device *bdev)
> }
>
> static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
> - uint64_t len)
> + uint64_t len, int secure)
> {
> + unsigned long flags = BLKDEV_IFL_WAIT;
> +
> if (start & 511)
> return -EINVAL;
> if (len & 511)
> @@ -125,8 +127,9 @@ static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
>
> if (start + len > (bdev->bd_inode->i_size >> 9))
> return -EINVAL;
> - return blkdev_issue_discard(bdev, start, len, GFP_KERNEL,
> - BLKDEV_IFL_WAIT);
> + if (secure)
> + flags |= BLKDEV_IFL_SECURE;
> + return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
> }
>
> static int put_ushort(unsigned long arg, unsigned short val)
> @@ -226,7 +229,8 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
> unlock_kernel();
> return 0;
>
> - case BLKDISCARD: {
> + case BLKDISCARD:
> + case BLKSECDISCARD: {
> uint64_t range[2];
>
> if (!(mode & FMODE_WRITE))
> @@ -235,7 +239,8 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
> if (copy_from_user(range, (void __user *)arg, sizeof(range)))
> return -EFAULT;
>
> - return blk_ioctl_discard(bdev, range[0], range[1]);
> + return blk_ioctl_discard(bdev, range[0], range[1],
> + cmd == BLKSECDISCARD);
> }
>
> case HDIO_GETGEO: {
> diff --git a/include/linux/bio.h b/include/linux/bio.h
> index 4d379c8..147e631 100644
> --- a/include/linux/bio.h
> +++ b/include/linux/bio.h
> @@ -176,6 +176,7 @@ enum rq_flag_bits {
> __REQ_INTEGRITY, /* integrity metadata has been remapped */
> __REQ_IO_STAT, /* account I/O stat */
> __REQ_MIXED_MERGE, /* merge of different types, fail separately */
> + __REQ_SECURE, /* secure discard (used with __REQ_DISCARD) */
> __REQ_NR_BITS, /* stops here */
> };
>
> @@ -215,6 +216,7 @@ enum rq_flag_bits {
> #define REQ_INTEGRITY (1 << __REQ_INTEGRITY)
> #define REQ_IO_STAT (1 << __REQ_IO_STAT)
> #define REQ_MIXED_MERGE (1 << __REQ_MIXED_MERGE)
> +#define REQ_SECURE (1 << __REQ_SECURE)
>
> /*
> * upper 16 bits of bi_rw define the io priority of this bio
> diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
> index 204fbe2..da643f4 100644
> --- a/include/linux/blkdev.h
> +++ b/include/linux/blkdev.h
> @@ -404,6 +404,7 @@ struct request_queue
> #define QUEUE_FLAG_DISCARD 16 /* supports DISCARD */
> #define QUEUE_FLAG_NOXMERGES 17 /* No extended merges */
> #define QUEUE_FLAG_ADD_RANDOM 18 /* Contributes to random pool */
> +#define QUEUE_FLAG_SECDISCARD 19 /* supports SECDISCARD */
>
> #define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
> (1 << QUEUE_FLAG_CLUSTER) | \
> @@ -539,6 +540,8 @@ enum {
> #define blk_queue_stackable(q) \
> test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
> #define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
> +#define blk_queue_secdiscard(q) (blk_queue_discard(q) && \
> + test_bit(QUEUE_FLAG_SECDISCARD, &(q)->queue_flags))
>
> #define blk_noretry_request(rq) \
> ((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
> @@ -931,10 +934,12 @@ static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt,
> }
> enum{
> BLKDEV_WAIT, /* wait for completion */
> - BLKDEV_BARRIER, /*issue request with barrier */
> + BLKDEV_BARRIER, /* issue request with barrier */
> + BLKDEV_SECURE, /* secure discard */
> };
> #define BLKDEV_IFL_WAIT (1 << BLKDEV_WAIT)
> #define BLKDEV_IFL_BARRIER (1 << BLKDEV_BARRIER)
> +#define BLKDEV_IFL_SECURE (1 << BLKDEV_SECURE)
> extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *,
> unsigned long);
> extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
> diff --git a/include/linux/fs.h b/include/linux/fs.h
> index dc9d185..2f5df1f 100644
> --- a/include/linux/fs.h
> +++ b/include/linux/fs.h
> @@ -176,6 +176,7 @@ struct inodes_stat_t {
> */
> #define DISCARD_NOBARRIER (WRITE | REQ_DISCARD)
> #define DISCARD_BARRIER (WRITE | REQ_DISCARD | REQ_HARDBARRIER)
> +#define DISCARD_SECURE (DISCARD_NOBARRIER | REQ_SECURE)
>
> #define SEL_IN 1
> #define SEL_OUT 2
> @@ -318,6 +319,7 @@ struct inodes_stat_t {
> #define BLKALIGNOFF _IO(0x12,122)
> #define BLKPBSZGET _IO(0x12,123)
> #define BLKDISCARDZEROES _IO(0x12,124)
> +#define BLKSECDISCARD _IO(0x12,125)
>
> #define BMAP_IOCTL 1 /* obsolete - kept for compatibility */
> #define FIBMAP _IO(0x00,1) /* bmap access */
> diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
> index 3b4a695..a711b47 100644
> --- a/kernel/trace/blktrace.c
> +++ b/kernel/trace/blktrace.c
> @@ -667,6 +667,9 @@ static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
> if (rq->cmd_flags & REQ_DISCARD)
> rw |= REQ_DISCARD;
>
> + if (rq->cmd_flags & REQ_SECURE)
> + rw |= REQ_SECURE;
> +
> if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
> what |= BLK_TC_ACT(BLK_TC_PC);
> __blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
> @@ -1773,6 +1776,8 @@ void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
> rwbs[i++] = 'S';
> if (rw & REQ_META)
> rwbs[i++] = 'M';
> + if (rw & REQ_SECURE)
> + rwbs[i++] = 'E';
>
> rwbs[i] = '\0';
> }
> @@ -1785,6 +1790,9 @@ void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
> if (rq->cmd_flags & REQ_DISCARD)
> rw |= REQ_DISCARD;
>
> + if (rq->cmd_flags & REQ_SECURE)
> + rw |= REQ_SECURE;
> +
> bytes = blk_rq_bytes(rq);
>
> blk_fill_rwbs(rwbs, rw, bytes);

2010-07-14 03:16:46

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH V4 4/5] block: Add secure discard

The patches look good to me, but they'll need some work to apply against
Jens' block tree.

2010-07-14 10:51:05

by Adrian Hunter

[permalink] [raw]
Subject: [PATCH] block: Add secure discard

>From 9ba7750fb96d9994e8715b14d8091c37b832b3e6 Mon Sep 17 00:00:00 2001
From: Adrian Hunter <[email protected]>
Date: Wed, 23 Jun 2010 15:41:38 +0300
Subject: [PATCH] block: Add secure discard

Secure discard is the same as discard except that all copies
of the discarded sectors (perhaps created by garbage collection)
must also be erased.

Signed-off-by: Adrian Hunter <[email protected]>
---



Here is "[PATCH V4 4/5] block: Add secure discard" applied to
git://git.kernel.dk/linux-2.6-block.git for-next.

If Jens takes this patch (originally 4th of 5), Andrew takes patches 1 to 3,
and patch 5 waits for this one to propagate, then there will be no dependency
problem.






block/blk-core.c | 5 ++++-
block/blk-lib.c | 6 ++++++
block/compat_ioctl.c | 1 +
block/elevator.c | 6 ++++++
block/ioctl.c | 15 ++++++++++-----
include/linux/bio.h | 2 ++
include/linux/blkdev.h | 7 ++++++-
include/linux/fs.h | 2 ++
kernel/trace/blktrace.c | 8 ++++++++
9 files changed, 45 insertions(+), 7 deletions(-)

diff --git a/block/blk-core.c b/block/blk-core.c
index 5ab3ac2..529aa57 100644
--- a/block/blk-core.c
+++ b/block/blk-core.c
@@ -1514,7 +1514,10 @@ static inline void __generic_make_request(struct bio *bio)
if (bio_check_eod(bio, nr_sectors))
goto end_io;

- if ((bio->bi_rw & REQ_DISCARD) && !blk_queue_discard(q)) {
+ if ((bio->bi_rw & REQ_DISCARD) &&
+ (!blk_queue_discard(q) ||
+ ((bio->bi_rw & REQ_SECURE) &&
+ !blk_queue_secdiscard(q)))) {
err = -EOPNOTSUPP;
goto end_io;
}
diff --git a/block/blk-lib.c b/block/blk-lib.c
index e16185b..ba8c0f9 100644
--- a/block/blk-lib.c
+++ b/block/blk-lib.c
@@ -50,6 +50,12 @@ int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
if (!blk_queue_discard(q))
return -EOPNOTSUPP;

+ if (flags & BLKDEV_IFL_SECURE) {
+ if (!blk_queue_secdiscard(q))
+ return -EOPNOTSUPP;
+ type |= DISCARD_SECURE;
+ }
+
while (nr_sects && !ret) {
unsigned int max_discard_sectors =
min(q->limits.max_discard_sectors, UINT_MAX >> 9);
diff --git a/block/compat_ioctl.c b/block/compat_ioctl.c
index d530856..119f07b 100644
--- a/block/compat_ioctl.c
+++ b/block/compat_ioctl.c
@@ -703,6 +703,7 @@ long compat_blkdev_ioctl(struct file *file, unsigned cmd, unsigned long arg)
case BLKFLSBUF:
case BLKROSET:
case BLKDISCARD:
+ case BLKSECDISCARD:
/*
* the ones below are implemented in blkdev_locked_ioctl,
* but we call blkdev_ioctl, which gets the lock for us
diff --git a/block/elevator.c b/block/elevator.c
index 816a7c8..ec585c9 100644
--- a/block/elevator.c
+++ b/block/elevator.c
@@ -83,6 +83,12 @@ int elv_rq_merge_ok(struct request *rq, struct bio *bio)
return 0;

/*
+ * Don't merge discard requests and secure discard requests
+ */
+ if ((bio->bi_rw & REQ_SECURE) != (rq->bio->bi_rw & REQ_SECURE))
+ return 0;
+
+ /*
* different data direction or already started, don't merge
*/
if (bio_data_dir(bio) != rq_data_dir(rq))
diff --git a/block/ioctl.c b/block/ioctl.c
index 09fd7f1..d8052f0 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -114,8 +114,10 @@ static int blkdev_reread_part(struct block_device *bdev)
}

static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,
- uint64_t len)
+ uint64_t len, int secure)
{
+ unsigned long flags = BLKDEV_IFL_WAIT;
+
if (start & 511)
return -EINVAL;
if (len & 511)
@@ -125,8 +127,9 @@ static int blk_ioctl_discard(struct block_device *bdev, uint64_t start,

if (start + len > (bdev->bd_inode->i_size >> 9))
return -EINVAL;
- return blkdev_issue_discard(bdev, start, len, GFP_KERNEL,
- BLKDEV_IFL_WAIT);
+ if (secure)
+ flags |= BLKDEV_IFL_SECURE;
+ return blkdev_issue_discard(bdev, start, len, GFP_KERNEL, flags);
}

static int put_ushort(unsigned long arg, unsigned short val)
@@ -213,7 +216,8 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
set_device_ro(bdev, n);
return 0;

- case BLKDISCARD: {
+ case BLKDISCARD:
+ case BLKSECDISCARD: {
uint64_t range[2];

if (!(mode & FMODE_WRITE))
@@ -222,7 +226,8 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
if (copy_from_user(range, (void __user *)arg, sizeof(range)))
return -EFAULT;

- return blk_ioctl_discard(bdev, range[0], range[1]);
+ return blk_ioctl_discard(bdev, range[0], range[1],
+ cmd == BLKSECDISCARD);
}

case HDIO_GETGEO: {
diff --git a/include/linux/bio.h b/include/linux/bio.h
index f655b54..5cb73bb 100644
--- a/include/linux/bio.h
+++ b/include/linux/bio.h
@@ -177,6 +177,7 @@ enum rq_flag_bits {
__REQ_FLUSH, /* request for cache flush */
__REQ_IO_STAT, /* account I/O stat */
__REQ_MIXED_MERGE, /* merge of different types, fail separately */
+ __REQ_SECURE, /* secure discard (used with __REQ_DISCARD) */
__REQ_NR_BITS, /* stops here */
};

@@ -217,6 +218,7 @@ enum rq_flag_bits {
#define REQ_FLUSH (1 << __REQ_FLUSH)
#define REQ_IO_STAT (1 << __REQ_IO_STAT)
#define REQ_MIXED_MERGE (1 << __REQ_MIXED_MERGE)
+#define REQ_SECURE (1 << __REQ_SECURE)

/*
* upper 16 bits of bi_rw define the io priority of this bio
diff --git a/include/linux/blkdev.h b/include/linux/blkdev.h
index a8b05fc..cd2dc1f 100644
--- a/include/linux/blkdev.h
+++ b/include/linux/blkdev.h
@@ -389,6 +389,7 @@ struct request_queue
#define QUEUE_FLAG_DISCARD 16 /* supports DISCARD */
#define QUEUE_FLAG_NOXMERGES 17 /* No extended merges */
#define QUEUE_FLAG_ADD_RANDOM 18 /* Contributes to random pool */
+#define QUEUE_FLAG_SECDISCARD 19 /* supports SECDISCARD */

#define QUEUE_FLAG_DEFAULT ((1 << QUEUE_FLAG_IO_STAT) | \
(1 << QUEUE_FLAG_CLUSTER) | \
@@ -524,6 +525,8 @@ enum {
#define blk_queue_stackable(q) \
test_bit(QUEUE_FLAG_STACKABLE, &(q)->queue_flags)
#define blk_queue_discard(q) test_bit(QUEUE_FLAG_DISCARD, &(q)->queue_flags)
+#define blk_queue_secdiscard(q) (blk_queue_discard(q) && \
+ test_bit(QUEUE_FLAG_SECDISCARD, &(q)->queue_flags))

#define blk_noretry_request(rq) \
((rq)->cmd_flags & (REQ_FAILFAST_DEV|REQ_FAILFAST_TRANSPORT| \
@@ -918,10 +921,12 @@ static inline struct request *blk_map_queue_find_tag(struct blk_queue_tag *bqt,
}
enum{
BLKDEV_WAIT, /* wait for completion */
- BLKDEV_BARRIER, /*issue request with barrier */
+ BLKDEV_BARRIER, /* issue request with barrier */
+ BLKDEV_SECURE, /* secure discard */
};
#define BLKDEV_IFL_WAIT (1 << BLKDEV_WAIT)
#define BLKDEV_IFL_BARRIER (1 << BLKDEV_BARRIER)
+#define BLKDEV_IFL_SECURE (1 << BLKDEV_SECURE)
extern int blkdev_issue_flush(struct block_device *, gfp_t, sector_t *,
unsigned long);
extern int blkdev_issue_discard(struct block_device *bdev, sector_t sector,
diff --git a/include/linux/fs.h b/include/linux/fs.h
index c5c9294..90ac60c 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -169,6 +169,7 @@ struct inodes_stat_t {
*/
#define DISCARD_NOBARRIER (WRITE | REQ_DISCARD)
#define DISCARD_BARRIER (WRITE | REQ_DISCARD | REQ_HARDBARRIER)
+#define DISCARD_SECURE (DISCARD_NOBARRIER | REQ_SECURE)

#define SEL_IN 1
#define SEL_OUT 2
@@ -311,6 +312,7 @@ struct inodes_stat_t {
#define BLKALIGNOFF _IO(0x12,122)
#define BLKPBSZGET _IO(0x12,123)
#define BLKDISCARDZEROES _IO(0x12,124)
+#define BLKSECDISCARD _IO(0x12,125)

#define BMAP_IOCTL 1 /* obsolete - kept for compatibility */
#define FIBMAP _IO(0x00,1) /* bmap access */
diff --git a/kernel/trace/blktrace.c b/kernel/trace/blktrace.c
index 82499a5..959f8d6 100644
--- a/kernel/trace/blktrace.c
+++ b/kernel/trace/blktrace.c
@@ -710,6 +710,9 @@ static void blk_add_trace_rq(struct request_queue *q, struct request *rq,
if (rq->cmd_flags & REQ_DISCARD)
rw |= REQ_DISCARD;

+ if (rq->cmd_flags & REQ_SECURE)
+ rw |= REQ_SECURE;
+
if (rq->cmd_type == REQ_TYPE_BLOCK_PC) {
what |= BLK_TC_ACT(BLK_TC_PC);
__blk_add_trace(bt, 0, blk_rq_bytes(rq), rw,
@@ -1816,6 +1819,8 @@ void blk_fill_rwbs(char *rwbs, u32 rw, int bytes)
rwbs[i++] = 'S';
if (rw & REQ_META)
rwbs[i++] = 'M';
+ if (rw & REQ_SECURE)
+ rwbs[i++] = 'E';

rwbs[i] = '\0';
}
@@ -1828,6 +1833,9 @@ void blk_fill_rwbs_rq(char *rwbs, struct request *rq)
if (rq->cmd_flags & REQ_DISCARD)
rw |= REQ_DISCARD;

+ if (rq->cmd_flags & REQ_SECURE)
+ rw |= REQ_SECURE;
+
bytes = blk_rq_bytes(rq);

blk_fill_rwbs(rwbs, rw, bytes);
--
1.6.3.3

2010-07-15 19:27:01

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] block: Add secure discard

On Wed, 14 Jul 2010 13:50:39 +0300
Adrian Hunter <[email protected]> wrote:

> >From 9ba7750fb96d9994e8715b14d8091c37b832b3e6 Mon Sep 17 00:00:00 2001
> From: Adrian Hunter <[email protected]>
> Date: Wed, 23 Jun 2010 15:41:38 +0300
> Subject: [PATCH] block: Add secure discard
>
> Secure discard is the same as discard except that all copies
> of the discarded sectors (perhaps created by garbage collection)
> must also be erased.
>
> Signed-off-by: Adrian Hunter <[email protected]>
> ---
>
>
>
> Here is "[PATCH V4 4/5] block: Add secure discard" applied to
> git://git.kernel.dk/linux-2.6-block.git for-next.
>
> If Jens takes this patch (originally 4th of 5), Andrew takes patches 1 to 3,
> and patch 5 waits for this one to propagate, then there will be no dependency
> problem.

hm, complicated.

I merged all five into -mm. If Jens sends along some acks then I send
all five along to Linus, nice and simple.

2010-07-15 20:03:01

by Jens Axboe

[permalink] [raw]
Subject: Re: [PATCH] block: Add secure discard

On 07/15/2010 01:26 PM, Andrew Morton wrote:
> On Wed, 14 Jul 2010 13:50:39 +0300
> Adrian Hunter <[email protected]> wrote:
>
>> >From 9ba7750fb96d9994e8715b14d8091c37b832b3e6 Mon Sep 17 00:00:00 2001
>> From: Adrian Hunter <[email protected]>
>> Date: Wed, 23 Jun 2010 15:41:38 +0300
>> Subject: [PATCH] block: Add secure discard
>>
>> Secure discard is the same as discard except that all copies
>> of the discarded sectors (perhaps created by garbage collection)
>> must also be erased.
>>
>> Signed-off-by: Adrian Hunter <[email protected]>
>> ---
>>
>>
>>
>> Here is "[PATCH V4 4/5] block: Add secure discard" applied to
>> git://git.kernel.dk/linux-2.6-block.git for-next.
>>
>> If Jens takes this patch (originally 4th of 5), Andrew takes patches 1 to 3,
>> and patch 5 waits for this one to propagate, then there will be no dependency
>> problem.
>
> hm, complicated.
>
> I merged all five into -mm. If Jens sends along some acks then I send
> all five along to Linus, nice and simple.
>

I was about to pick them up, but yeah just adds my acks to the series
and shove them off for the next series.

--
Jens Axboe