From 9b4eac736a8404eecd695a68398c39e589faa7f9 Mon Sep 17 00:00:00 2001
From: Andres Rodriguez <andresx7@gmail.com>
Date: Tue, 8 Nov 2022 17:32:25 -0500
Subject: [PATCH 21/21] mmc: core: add safe_trim_quirk attribute

This attribute is intended to be used by userspace tools to identify
if trimming a device is safe or not. It helps in cases where a
userspace tool is aware of the necessity of the quirk and a user
accidentally downgrades their kernel to a one that doesn't have
support for the quirk.

Signed-off-by: Andres Rodriguez <andresx7@gmail.com>
(cherry picked from commit dfc5952c55f58d0e0863ccba0aa598fdae16ed14)
Signed-off-by: Cristian Ciocaltea <cristian.ciocaltea@collabora.com>
---
 drivers/mmc/core/mmc.c    | 2 ++
 drivers/mmc/core/quirks.h | 2 ++
 drivers/mmc/core/sd.c     | 2 ++
 include/linux/mmc/card.h  | 1 +
 4 files changed, 7 insertions(+)

diff --git a/drivers/mmc/core/mmc.c b/drivers/mmc/core/mmc.c
index 5b2f7c285461..46b7971d0a33 100644
--- a/drivers/mmc/core/mmc.c
+++ b/drivers/mmc/core/mmc.c
@@ -820,6 +820,7 @@ MMC_DEV_ATTR(rel_sectors, "%#x\n", card->ext_csd.rel_sectors);
 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
 MMC_DEV_ATTR(cmdq_en, "%d\n", card->ext_csd.cmdq_en);
+MMC_DEV_ATTR(safe_trim_quirk, "%d\n", card->safe_trim_quirk_version);
 
 static ssize_t mmc_fwrev_show(struct device *dev,
 			      struct device_attribute *attr,
@@ -879,6 +880,7 @@ static struct attribute *mmc_std_attrs[] = {
 	&dev_attr_rca.attr,
 	&dev_attr_dsr.attr,
 	&dev_attr_cmdq_en.attr,
+	&dev_attr_safe_trim_quirk.attr,
 	NULL,
 };
 ATTRIBUTE_GROUPS(mmc_std);
diff --git a/drivers/mmc/core/quirks.h b/drivers/mmc/core/quirks.h
index cca71867bc4a..6418bfdf849e 100644
--- a/drivers/mmc/core/quirks.h
+++ b/drivers/mmc/core/quirks.h
@@ -241,4 +241,6 @@ static inline void mmc_fixup_device(struct mmc_card *card,
 		dev_dbg(&card->dev, "calling %ps\n", f->vendor_fixup);
 		f->vendor_fixup(card, f->data);
 	}
+
+	card->safe_trim_quirk_version = 1;
 }
diff --git a/drivers/mmc/core/sd.c b/drivers/mmc/core/sd.c
index 1c8148cdda50..e8b99bbb4b0b 100644
--- a/drivers/mmc/core/sd.c
+++ b/drivers/mmc/core/sd.c
@@ -714,6 +714,7 @@ MMC_DEV_ATTR(oemid, "0x%04x\n", card->cid.oemid);
 MMC_DEV_ATTR(serial, "0x%08x\n", card->cid.serial);
 MMC_DEV_ATTR(ocr, "0x%08x\n", card->ocr);
 MMC_DEV_ATTR(rca, "0x%04x\n", card->rca);
+MMC_DEV_ATTR(safe_trim_quirk, "%d\n", card->safe_trim_quirk_version);
 
 
 static ssize_t mmc_dsr_show(struct device *dev, struct device_attribute *attr,
@@ -776,6 +777,7 @@ static struct attribute *sd_std_attrs[] = {
 	&dev_attr_ocr.attr,
 	&dev_attr_rca.attr,
 	&dev_attr_dsr.attr,
+	&dev_attr_safe_trim_quirk.attr,
 	NULL,
 };
 
diff --git a/include/linux/mmc/card.h b/include/linux/mmc/card.h
index f34407cc2788..12fdace380f0 100644
--- a/include/linux/mmc/card.h
+++ b/include/linux/mmc/card.h
@@ -275,6 +275,7 @@ struct mmc_card {
 	unsigned int		state;		/* (our) card state */
 	unsigned int		quirks; 	/* card quirks */
 	unsigned int		quirk_max_rate;	/* max rate set by quirks */
+	unsigned int		safe_trim_quirk_version; /* advertise if we have protection for cards which misbehave with trim */
 #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 */
-- 
2.45.2

