Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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 <adrian.hunter@nokia.com>
Acked-by: Jens Axboe <axboe@kernel.dk>
Cc: Kyungmin Park <kmpark@infradead.org>
Cc: Madhusudhan Chikkature <madhu.cr@ti.com>
Cc: Christoph Hellwig <hch@lst.de>
Cc: Ben Gardiner <bengardiner@nanometrics.ca>
Cc: <linux-mmc@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Adrian Hunter and committed by
Linus Torvalds
dfe86cba 81d73a32

+651 -8
+2
Documentation/00-INDEX
··· 232 232 - info on typical Linux memory problems. 233 233 mips/ 234 234 - directory with info about Linux on MIPS architecture. 235 + mmc/ 236 + - directory with info about the MMC subsystem 235 237 mono.txt 236 238 - how to execute Mono-based .NET binaries with the help of BINFMT_MISC. 237 239 mutex-design.txt
+4
Documentation/mmc/00-INDEX
··· 1 + 00-INDEX 2 + - this file 3 + mmc-dev-attrs.txt 4 + - info on SD and MMC device attributes
+56
Documentation/mmc/mmc-dev-attrs.txt
··· 1 + SD and MMC Device Attributes 2 + ============================ 3 + 4 + All attributes are read-only. 5 + 6 + cid Card Identifaction Register 7 + csd Card Specific Data Register 8 + scr SD Card Configuration Register (SD only) 9 + date Manufacturing Date (from CID Register) 10 + fwrev Firmware/Product Revision (from CID Register) (SD and MMCv1 only) 11 + hwrev Hardware/Product Revision (from CID Register) (SD and MMCv1 only) 12 + manfid Manufacturer ID (from CID Register) 13 + name Product Name (from CID Register) 14 + oemid OEM/Application ID (from CID Register) 15 + serial Product Serial Number (from CID Register) 16 + erase_size Erase group size 17 + preferred_erase_size Preferred erase size 18 + 19 + Note on Erase Size and Preferred Erase Size: 20 + 21 + "erase_size" is the minimum size, in bytes, of an erase 22 + operation. For MMC, "erase_size" is the erase group size 23 + reported by the card. Note that "erase_size" does not apply 24 + to trim or secure trim operations where the minimum size is 25 + always one 512 byte sector. For SD, "erase_size" is 512 26 + if the card is block-addressed, 0 otherwise. 27 + 28 + SD/MMC cards can erase an arbitrarily large area up to and 29 + including the whole card. When erasing a large area it may 30 + be desirable to do it in smaller chunks for three reasons: 31 + 1. A single erase command will make all other I/O on 32 + the card wait. This is not a problem if the whole card 33 + is being erased, but erasing one partition will make 34 + I/O for another partition on the same card wait for the 35 + duration of the erase - which could be a several 36 + minutes. 37 + 2. To be able to inform the user of erase progress. 38 + 3. The erase timeout becomes too large to be very 39 + useful. Because the erase timeout contains a margin 40 + which is multiplied by the size of the erase area, 41 + the value can end up being several minutes for large 42 + areas. 43 + 44 + "erase_size" is not the most efficient unit to erase 45 + (especially for SD where it is just one sector), 46 + hence "preferred_erase_size" provides a good chunk 47 + size for erasing large areas. 48 + 49 + For MMC, "preferred_erase_size" is the high-capacity 50 + erase size if a card specifies one, otherwise it is 51 + based on the capacity of the card. 52 + 53 + For SD, "preferred_erase_size" is the allocation unit 54 + size specified by the card. 55 + 56 + "preferred_erase_size" is in bytes.
+346
drivers/mmc/core/core.c
··· 1050 1050 1051 1051 EXPORT_SYMBOL(mmc_detect_change); 1052 1052 1053 + void mmc_init_erase(struct mmc_card *card) 1054 + { 1055 + unsigned int sz; 1056 + 1057 + if (is_power_of_2(card->erase_size)) 1058 + card->erase_shift = ffs(card->erase_size) - 1; 1059 + else 1060 + card->erase_shift = 0; 1061 + 1062 + /* 1063 + * It is possible to erase an arbitrarily large area of an SD or MMC 1064 + * card. That is not desirable because it can take a long time 1065 + * (minutes) potentially delaying more important I/O, and also the 1066 + * timeout calculations become increasingly hugely over-estimated. 1067 + * Consequently, 'pref_erase' is defined as a guide to limit erases 1068 + * to that size and alignment. 1069 + * 1070 + * For SD cards that define Allocation Unit size, limit erases to one 1071 + * Allocation Unit at a time. For MMC cards that define High Capacity 1072 + * Erase Size, whether it is switched on or not, limit to that size. 1073 + * Otherwise just have a stab at a good value. For modern cards it 1074 + * will end up being 4MiB. Note that if the value is too small, it 1075 + * can end up taking longer to erase. 1076 + */ 1077 + if (mmc_card_sd(card) && card->ssr.au) { 1078 + card->pref_erase = card->ssr.au; 1079 + card->erase_shift = ffs(card->ssr.au) - 1; 1080 + } else if (card->ext_csd.hc_erase_size) { 1081 + card->pref_erase = card->ext_csd.hc_erase_size; 1082 + } else { 1083 + sz = (card->csd.capacity << (card->csd.read_blkbits - 9)) >> 11; 1084 + if (sz < 128) 1085 + card->pref_erase = 512 * 1024 / 512; 1086 + else if (sz < 512) 1087 + card->pref_erase = 1024 * 1024 / 512; 1088 + else if (sz < 1024) 1089 + card->pref_erase = 2 * 1024 * 1024 / 512; 1090 + else 1091 + card->pref_erase = 4 * 1024 * 1024 / 512; 1092 + if (card->pref_erase < card->erase_size) 1093 + card->pref_erase = card->erase_size; 1094 + else { 1095 + sz = card->pref_erase % card->erase_size; 1096 + if (sz) 1097 + card->pref_erase += card->erase_size - sz; 1098 + } 1099 + } 1100 + } 1101 + 1102 + static void mmc_set_mmc_erase_timeout(struct mmc_card *card, 1103 + struct mmc_command *cmd, 1104 + unsigned int arg, unsigned int qty) 1105 + { 1106 + unsigned int erase_timeout; 1107 + 1108 + if (card->ext_csd.erase_group_def & 1) { 1109 + /* High Capacity Erase Group Size uses HC timeouts */ 1110 + if (arg == MMC_TRIM_ARG) 1111 + erase_timeout = card->ext_csd.trim_timeout; 1112 + else 1113 + erase_timeout = card->ext_csd.hc_erase_timeout; 1114 + } else { 1115 + /* CSD Erase Group Size uses write timeout */ 1116 + unsigned int mult = (10 << card->csd.r2w_factor); 1117 + unsigned int timeout_clks = card->csd.tacc_clks * mult; 1118 + unsigned int timeout_us; 1119 + 1120 + /* Avoid overflow: e.g. tacc_ns=80000000 mult=1280 */ 1121 + if (card->csd.tacc_ns < 1000000) 1122 + timeout_us = (card->csd.tacc_ns * mult) / 1000; 1123 + else 1124 + timeout_us = (card->csd.tacc_ns / 1000) * mult; 1125 + 1126 + /* 1127 + * ios.clock is only a target. The real clock rate might be 1128 + * less but not that much less, so fudge it by multiplying by 2. 1129 + */ 1130 + timeout_clks <<= 1; 1131 + timeout_us += (timeout_clks * 1000) / 1132 + (card->host->ios.clock / 1000); 1133 + 1134 + erase_timeout = timeout_us / 1000; 1135 + 1136 + /* 1137 + * Theoretically, the calculation could underflow so round up 1138 + * to 1ms in that case. 1139 + */ 1140 + if (!erase_timeout) 1141 + erase_timeout = 1; 1142 + } 1143 + 1144 + /* Multiplier for secure operations */ 1145 + if (arg & MMC_SECURE_ARGS) { 1146 + if (arg == MMC_SECURE_ERASE_ARG) 1147 + erase_timeout *= card->ext_csd.sec_erase_mult; 1148 + else 1149 + erase_timeout *= card->ext_csd.sec_trim_mult; 1150 + } 1151 + 1152 + erase_timeout *= qty; 1153 + 1154 + /* 1155 + * Ensure at least a 1 second timeout for SPI as per 1156 + * 'mmc_set_data_timeout()' 1157 + */ 1158 + if (mmc_host_is_spi(card->host) && erase_timeout < 1000) 1159 + erase_timeout = 1000; 1160 + 1161 + cmd->erase_timeout = erase_timeout; 1162 + } 1163 + 1164 + static void mmc_set_sd_erase_timeout(struct mmc_card *card, 1165 + struct mmc_command *cmd, unsigned int arg, 1166 + unsigned int qty) 1167 + { 1168 + if (card->ssr.erase_timeout) { 1169 + /* Erase timeout specified in SD Status Register (SSR) */ 1170 + cmd->erase_timeout = card->ssr.erase_timeout * qty + 1171 + card->ssr.erase_offset; 1172 + } else { 1173 + /* 1174 + * Erase timeout not specified in SD Status Register (SSR) so 1175 + * use 250ms per write block. 1176 + */ 1177 + cmd->erase_timeout = 250 * qty; 1178 + } 1179 + 1180 + /* Must not be less than 1 second */ 1181 + if (cmd->erase_timeout < 1000) 1182 + cmd->erase_timeout = 1000; 1183 + } 1184 + 1185 + static void mmc_set_erase_timeout(struct mmc_card *card, 1186 + struct mmc_command *cmd, unsigned int arg, 1187 + unsigned int qty) 1188 + { 1189 + if (mmc_card_sd(card)) 1190 + mmc_set_sd_erase_timeout(card, cmd, arg, qty); 1191 + else 1192 + mmc_set_mmc_erase_timeout(card, cmd, arg, qty); 1193 + } 1194 + 1195 + static int mmc_do_erase(struct mmc_card *card, unsigned int from, 1196 + unsigned int to, unsigned int arg) 1197 + { 1198 + struct mmc_command cmd; 1199 + unsigned int qty = 0; 1200 + int err; 1201 + 1202 + /* 1203 + * qty is used to calculate the erase timeout which depends on how many 1204 + * erase groups (or allocation units in SD terminology) are affected. 1205 + * We count erasing part of an erase group as one erase group. 1206 + * For SD, the allocation units are always a power of 2. For MMC, the 1207 + * erase group size is almost certainly also power of 2, but it does not 1208 + * seem to insist on that in the JEDEC standard, so we fall back to 1209 + * division in that case. SD may not specify an allocation unit size, 1210 + * in which case the timeout is based on the number of write blocks. 1211 + * 1212 + * Note that the timeout for secure trim 2 will only be correct if the 1213 + * number of erase groups specified is the same as the total of all 1214 + * preceding secure trim 1 commands. Since the power may have been 1215 + * lost since the secure trim 1 commands occurred, it is generally 1216 + * impossible to calculate the secure trim 2 timeout correctly. 1217 + */ 1218 + if (card->erase_shift) 1219 + qty += ((to >> card->erase_shift) - 1220 + (from >> card->erase_shift)) + 1; 1221 + else if (mmc_card_sd(card)) 1222 + qty += to - from + 1; 1223 + else 1224 + qty += ((to / card->erase_size) - 1225 + (from / card->erase_size)) + 1; 1226 + 1227 + if (!mmc_card_blockaddr(card)) { 1228 + from <<= 9; 1229 + to <<= 9; 1230 + } 1231 + 1232 + memset(&cmd, 0, sizeof(struct mmc_command)); 1233 + if (mmc_card_sd(card)) 1234 + cmd.opcode = SD_ERASE_WR_BLK_START; 1235 + else 1236 + cmd.opcode = MMC_ERASE_GROUP_START; 1237 + cmd.arg = from; 1238 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1239 + err = mmc_wait_for_cmd(card->host, &cmd, 0); 1240 + if (err) { 1241 + printk(KERN_ERR "mmc_erase: group start error %d, " 1242 + "status %#x\n", err, cmd.resp[0]); 1243 + err = -EINVAL; 1244 + goto out; 1245 + } 1246 + 1247 + memset(&cmd, 0, sizeof(struct mmc_command)); 1248 + if (mmc_card_sd(card)) 1249 + cmd.opcode = SD_ERASE_WR_BLK_END; 1250 + else 1251 + cmd.opcode = MMC_ERASE_GROUP_END; 1252 + cmd.arg = to; 1253 + cmd.flags = MMC_RSP_SPI_R1 | MMC_RSP_R1 | MMC_CMD_AC; 1254 + err = mmc_wait_for_cmd(card->host, &cmd, 0); 1255 + if (err) { 1256 + printk(KERN_ERR "mmc_erase: group end error %d, status %#x\n", 1257 + err, cmd.resp[0]); 1258 + err = -EINVAL; 1259 + goto out; 1260 + } 1261 + 1262 + memset(&cmd, 0, sizeof(struct mmc_command)); 1263 + cmd.opcode = MMC_ERASE; 1264 + cmd.arg = arg; 1265 + cmd.flags = MMC_RSP_SPI_R1B | MMC_RSP_R1B | MMC_CMD_AC; 1266 + mmc_set_erase_timeout(card, &cmd, arg, qty); 1267 + err = mmc_wait_for_cmd(card->host, &cmd, 0); 1268 + if (err) { 1269 + printk(KERN_ERR "mmc_erase: erase error %d, status %#x\n", 1270 + err, cmd.resp[0]); 1271 + err = -EIO; 1272 + goto out; 1273 + } 1274 + 1275 + if (mmc_host_is_spi(card->host)) 1276 + goto out; 1277 + 1278 + do { 1279 + memset(&cmd, 0, sizeof(struct mmc_command)); 1280 + cmd.opcode = MMC_SEND_STATUS; 1281 + cmd.arg = card->rca << 16; 1282 + cmd.flags = MMC_RSP_R1 | MMC_CMD_AC; 1283 + /* Do not retry else we can't see errors */ 1284 + err = mmc_wait_for_cmd(card->host, &cmd, 0); 1285 + if (err || (cmd.resp[0] & 0xFDF92000)) { 1286 + printk(KERN_ERR "error %d requesting status %#x\n", 1287 + err, cmd.resp[0]); 1288 + err = -EIO; 1289 + goto out; 1290 + } 1291 + } while (!(cmd.resp[0] & R1_READY_FOR_DATA) || 1292 + R1_CURRENT_STATE(cmd.resp[0]) == 7); 1293 + out: 1294 + return err; 1295 + } 1296 + 1297 + /** 1298 + * mmc_erase - erase sectors. 1299 + * @card: card to erase 1300 + * @from: first sector to erase 1301 + * @nr: number of sectors to erase 1302 + * @arg: erase command argument (SD supports only %MMC_ERASE_ARG) 1303 + * 1304 + * Caller must claim host before calling this function. 1305 + */ 1306 + int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, 1307 + unsigned int arg) 1308 + { 1309 + unsigned int rem, to = from + nr; 1310 + 1311 + if (!(card->host->caps & MMC_CAP_ERASE) || 1312 + !(card->csd.cmdclass & CCC_ERASE)) 1313 + return -EOPNOTSUPP; 1314 + 1315 + if (!card->erase_size) 1316 + return -EOPNOTSUPP; 1317 + 1318 + if (mmc_card_sd(card) && arg != MMC_ERASE_ARG) 1319 + return -EOPNOTSUPP; 1320 + 1321 + if ((arg & MMC_SECURE_ARGS) && 1322 + !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN)) 1323 + return -EOPNOTSUPP; 1324 + 1325 + if ((arg & MMC_TRIM_ARGS) && 1326 + !(card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN)) 1327 + return -EOPNOTSUPP; 1328 + 1329 + if (arg == MMC_SECURE_ERASE_ARG) { 1330 + if (from % card->erase_size || nr % card->erase_size) 1331 + return -EINVAL; 1332 + } 1333 + 1334 + if (arg == MMC_ERASE_ARG) { 1335 + rem = from % card->erase_size; 1336 + if (rem) { 1337 + rem = card->erase_size - rem; 1338 + from += rem; 1339 + if (nr > rem) 1340 + nr -= rem; 1341 + else 1342 + return 0; 1343 + } 1344 + rem = nr % card->erase_size; 1345 + if (rem) 1346 + nr -= rem; 1347 + } 1348 + 1349 + if (nr == 0) 1350 + return 0; 1351 + 1352 + to = from + nr; 1353 + 1354 + if (to <= from) 1355 + return -EINVAL; 1356 + 1357 + /* 'from' and 'to' are inclusive */ 1358 + to -= 1; 1359 + 1360 + return mmc_do_erase(card, from, to, arg); 1361 + } 1362 + EXPORT_SYMBOL(mmc_erase); 1363 + 1364 + int mmc_can_erase(struct mmc_card *card) 1365 + { 1366 + if ((card->host->caps & MMC_CAP_ERASE) && 1367 + (card->csd.cmdclass & CCC_ERASE) && card->erase_size) 1368 + return 1; 1369 + return 0; 1370 + } 1371 + EXPORT_SYMBOL(mmc_can_erase); 1372 + 1373 + int mmc_can_trim(struct mmc_card *card) 1374 + { 1375 + if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_GB_CL_EN) 1376 + return 1; 1377 + return 0; 1378 + } 1379 + EXPORT_SYMBOL(mmc_can_trim); 1380 + 1381 + int mmc_can_secure_erase_trim(struct mmc_card *card) 1382 + { 1383 + if (card->ext_csd.sec_feature_support & EXT_CSD_SEC_ER_EN) 1384 + return 1; 1385 + return 0; 1386 + } 1387 + EXPORT_SYMBOL(mmc_can_secure_erase_trim); 1388 + 1389 + int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, 1390 + unsigned int nr) 1391 + { 1392 + if (!card->erase_size) 1393 + return 0; 1394 + if (from % card->erase_size || nr % card->erase_size) 1395 + return 0; 1396 + return 1; 1397 + } 1398 + EXPORT_SYMBOL(mmc_erase_group_aligned); 1053 1399 1054 1400 void mmc_rescan(struct work_struct *work) 1055 1401 {
+2
drivers/mmc/core/core.h
··· 29 29 void mmc_attach_bus(struct mmc_host *host, const struct mmc_bus_ops *ops); 30 30 void mmc_detach_bus(struct mmc_host *host); 31 31 32 + void mmc_init_erase(struct mmc_card *card); 33 + 32 34 void mmc_set_chip_select(struct mmc_host *host, int mode); 33 35 void mmc_set_clock(struct mmc_host *host, unsigned int hz); 34 36 void mmc_set_bus_mode(struct mmc_host *host, unsigned int mode);
+46 -1
drivers/mmc/core/mmc.c
··· 108 108 return 0; 109 109 } 110 110 111 + static void mmc_set_erase_size(struct mmc_card *card) 112 + { 113 + if (card->ext_csd.erase_group_def & 1) 114 + card->erase_size = card->ext_csd.hc_erase_size; 115 + else 116 + card->erase_size = card->csd.erase_size; 117 + 118 + mmc_init_erase(card); 119 + } 120 + 111 121 /* 112 122 * Given a 128-bit response, decode to our card CSD structure. 113 123 */ 114 124 static int mmc_decode_csd(struct mmc_card *card) 115 125 { 116 126 struct mmc_csd *csd = &card->csd; 117 - unsigned int e, m; 127 + unsigned int e, m, a, b; 118 128 u32 *resp = card->raw_csd; 119 129 120 130 /* ··· 161 151 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 162 152 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 163 153 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 154 + 155 + if (csd->write_blkbits >= 9) { 156 + a = UNSTUFF_BITS(resp, 42, 5); 157 + b = UNSTUFF_BITS(resp, 37, 5); 158 + csd->erase_size = (a + 1) * (b + 1); 159 + csd->erase_size <<= csd->write_blkbits - 9; 160 + } 164 161 165 162 return 0; 166 163 } ··· 278 261 if (sa_shift > 0 && sa_shift <= 0x17) 279 262 card->ext_csd.sa_timeout = 280 263 1 << ext_csd[EXT_CSD_S_A_TIMEOUT]; 264 + card->ext_csd.erase_group_def = 265 + ext_csd[EXT_CSD_ERASE_GROUP_DEF]; 266 + card->ext_csd.hc_erase_timeout = 300 * 267 + ext_csd[EXT_CSD_ERASE_TIMEOUT_MULT]; 268 + card->ext_csd.hc_erase_size = 269 + ext_csd[EXT_CSD_HC_ERASE_GRP_SIZE] << 10; 281 270 } 271 + 272 + if (card->ext_csd.rev >= 4) { 273 + card->ext_csd.sec_trim_mult = 274 + ext_csd[EXT_CSD_SEC_TRIM_MULT]; 275 + card->ext_csd.sec_erase_mult = 276 + ext_csd[EXT_CSD_SEC_ERASE_MULT]; 277 + card->ext_csd.sec_feature_support = 278 + ext_csd[EXT_CSD_SEC_FEATURE_SUPPORT]; 279 + card->ext_csd.trim_timeout = 300 * 280 + ext_csd[EXT_CSD_TRIM_MULT]; 281 + } 282 + 283 + if (ext_csd[EXT_CSD_ERASED_MEM_CONT]) 284 + card->erased_byte = 0xFF; 285 + else 286 + card->erased_byte = 0x0; 282 287 283 288 out: 284 289 kfree(ext_csd); ··· 313 274 MMC_DEV_ATTR(csd, "%08x%08x%08x%08x\n", card->raw_csd[0], card->raw_csd[1], 314 275 card->raw_csd[2], card->raw_csd[3]); 315 276 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 277 + MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); 278 + MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); 316 279 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 317 280 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 318 281 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); ··· 326 285 &dev_attr_cid.attr, 327 286 &dev_attr_csd.attr, 328 287 &dev_attr_date.attr, 288 + &dev_attr_erase_size.attr, 289 + &dev_attr_preferred_erase_size.attr, 329 290 &dev_attr_fwrev.attr, 330 291 &dev_attr_hwrev.attr, 331 292 &dev_attr_manfid.attr, ··· 464 421 err = mmc_read_ext_csd(card); 465 422 if (err) 466 423 goto free_card; 424 + /* Erase size depends on CSD and Extended CSD */ 425 + mmc_set_erase_size(card); 467 426 } 468 427 469 428 /*
+82
drivers/mmc/core/sd.c
··· 119 119 csd->r2w_factor = UNSTUFF_BITS(resp, 26, 3); 120 120 csd->write_blkbits = UNSTUFF_BITS(resp, 22, 4); 121 121 csd->write_partial = UNSTUFF_BITS(resp, 21, 1); 122 + 123 + if (UNSTUFF_BITS(resp, 46, 1)) { 124 + csd->erase_size = 1; 125 + } else if (csd->write_blkbits >= 9) { 126 + csd->erase_size = UNSTUFF_BITS(resp, 39, 7) + 1; 127 + csd->erase_size <<= csd->write_blkbits - 9; 128 + } 122 129 break; 123 130 case 1: 124 131 /* ··· 154 147 csd->r2w_factor = 4; /* Unused */ 155 148 csd->write_blkbits = 9; 156 149 csd->write_partial = 0; 150 + csd->erase_size = 1; 157 151 break; 158 152 default: 159 153 printk(KERN_ERR "%s: unrecognised CSD structure version %d\n", 160 154 mmc_hostname(card->host), csd_struct); 161 155 return -EINVAL; 162 156 } 157 + 158 + card->erase_size = csd->erase_size; 163 159 164 160 return 0; 165 161 } ··· 189 179 scr->sda_vsn = UNSTUFF_BITS(resp, 56, 4); 190 180 scr->bus_widths = UNSTUFF_BITS(resp, 48, 4); 191 181 182 + if (UNSTUFF_BITS(resp, 55, 1)) 183 + card->erased_byte = 0xFF; 184 + else 185 + card->erased_byte = 0x0; 186 + 192 187 return 0; 188 + } 189 + 190 + /* 191 + * Fetch and process SD Status register. 192 + */ 193 + static int mmc_read_ssr(struct mmc_card *card) 194 + { 195 + unsigned int au, es, et, eo; 196 + int err, i; 197 + u32 *ssr; 198 + 199 + if (!(card->csd.cmdclass & CCC_APP_SPEC)) { 200 + printk(KERN_WARNING "%s: card lacks mandatory SD Status " 201 + "function.\n", mmc_hostname(card->host)); 202 + return 0; 203 + } 204 + 205 + ssr = kmalloc(64, GFP_KERNEL); 206 + if (!ssr) 207 + return -ENOMEM; 208 + 209 + err = mmc_app_sd_status(card, ssr); 210 + if (err) { 211 + printk(KERN_WARNING "%s: problem reading SD Status " 212 + "register.\n", mmc_hostname(card->host)); 213 + err = 0; 214 + goto out; 215 + } 216 + 217 + for (i = 0; i < 16; i++) 218 + ssr[i] = be32_to_cpu(ssr[i]); 219 + 220 + /* 221 + * UNSTUFF_BITS only works with four u32s so we have to offset the 222 + * bitfield positions accordingly. 223 + */ 224 + au = UNSTUFF_BITS(ssr, 428 - 384, 4); 225 + if (au > 0 || au <= 9) { 226 + card->ssr.au = 1 << (au + 4); 227 + es = UNSTUFF_BITS(ssr, 408 - 384, 16); 228 + et = UNSTUFF_BITS(ssr, 402 - 384, 6); 229 + eo = UNSTUFF_BITS(ssr, 400 - 384, 2); 230 + if (es && et) { 231 + card->ssr.erase_timeout = (et * 1000) / es; 232 + card->ssr.erase_offset = eo * 1000; 233 + } 234 + } else { 235 + printk(KERN_WARNING "%s: SD Status: Invalid Allocation Unit " 236 + "size.\n", mmc_hostname(card->host)); 237 + } 238 + out: 239 + kfree(ssr); 240 + return err; 193 241 } 194 242 195 243 /* ··· 357 289 card->raw_csd[2], card->raw_csd[3]); 358 290 MMC_DEV_ATTR(scr, "%08x%08x\n", card->raw_scr[0], card->raw_scr[1]); 359 291 MMC_DEV_ATTR(date, "%02d/%04d\n", card->cid.month, card->cid.year); 292 + MMC_DEV_ATTR(erase_size, "%u\n", card->erase_size << 9); 293 + MMC_DEV_ATTR(preferred_erase_size, "%u\n", card->pref_erase << 9); 360 294 MMC_DEV_ATTR(fwrev, "0x%x\n", card->cid.fwrev); 361 295 MMC_DEV_ATTR(hwrev, "0x%x\n", card->cid.hwrev); 362 296 MMC_DEV_ATTR(manfid, "0x%06x\n", card->cid.manfid); ··· 372 302 &dev_attr_csd.attr, 373 303 &dev_attr_scr.attr, 374 304 &dev_attr_date.attr, 305 + &dev_attr_erase_size.attr, 306 + &dev_attr_preferred_erase_size.attr, 375 307 &dev_attr_fwrev.attr, 376 308 &dev_attr_hwrev.attr, 377 309 &dev_attr_manfid.attr, ··· 467 395 err = mmc_decode_scr(card); 468 396 if (err) 469 397 return err; 398 + 399 + /* 400 + * Fetch and process SD Status register. 401 + */ 402 + err = mmc_read_ssr(card); 403 + if (err) 404 + return err; 405 + 406 + /* Erase init depends on CSD and SSR */ 407 + mmc_init_erase(card); 470 408 471 409 /* 472 410 * Fetch switch information from card.
+48
drivers/mmc/core/sd_ops.c
··· 346 346 return 0; 347 347 } 348 348 349 + int mmc_app_sd_status(struct mmc_card *card, void *ssr) 350 + { 351 + int err; 352 + struct mmc_request mrq; 353 + struct mmc_command cmd; 354 + struct mmc_data data; 355 + struct scatterlist sg; 356 + 357 + BUG_ON(!card); 358 + BUG_ON(!card->host); 359 + BUG_ON(!ssr); 360 + 361 + /* NOTE: caller guarantees ssr is heap-allocated */ 362 + 363 + err = mmc_app_cmd(card->host, card); 364 + if (err) 365 + return err; 366 + 367 + memset(&mrq, 0, sizeof(struct mmc_request)); 368 + memset(&cmd, 0, sizeof(struct mmc_command)); 369 + memset(&data, 0, sizeof(struct mmc_data)); 370 + 371 + mrq.cmd = &cmd; 372 + mrq.data = &data; 373 + 374 + cmd.opcode = SD_APP_SD_STATUS; 375 + cmd.arg = 0; 376 + cmd.flags = MMC_RSP_SPI_R2 | MMC_RSP_R1 | MMC_CMD_ADTC; 377 + 378 + data.blksz = 64; 379 + data.blocks = 1; 380 + data.flags = MMC_DATA_READ; 381 + data.sg = &sg; 382 + data.sg_len = 1; 383 + 384 + sg_init_one(&sg, ssr, 64); 385 + 386 + mmc_set_data_timeout(&data, card); 387 + 388 + mmc_wait_for_req(card->host, &mrq); 389 + 390 + if (cmd.error) 391 + return cmd.error; 392 + if (data.error) 393 + return data.error; 394 + 395 + return 0; 396 + }
+1
drivers/mmc/core/sd_ops.h
··· 19 19 int mmc_app_send_scr(struct mmc_card *card, u32 *scr); 20 20 int mmc_sd_switch(struct mmc_card *card, int mode, int group, 21 21 u8 value, u8 *resp); 22 + int mmc_app_sd_status(struct mmc_card *card, void *ssr); 22 23 23 24 #endif 24 25
+20
include/linux/mmc/card.h
··· 31 31 unsigned int tacc_ns; 32 32 unsigned int r2w_factor; 33 33 unsigned int max_dtr; 34 + unsigned int erase_size; /* In sectors */ 34 35 unsigned int read_blkbits; 35 36 unsigned int write_blkbits; 36 37 unsigned int capacity; ··· 43 42 44 43 struct mmc_ext_csd { 45 44 u8 rev; 45 + u8 erase_group_def; 46 + u8 sec_feature_support; 46 47 unsigned int sa_timeout; /* Units: 100ns */ 47 48 unsigned int hs_max_dtr; 48 49 unsigned int sectors; 50 + unsigned int hc_erase_size; /* In sectors */ 51 + unsigned int hc_erase_timeout; /* In milliseconds */ 52 + unsigned int sec_trim_mult; /* Secure trim multiplier */ 53 + unsigned int sec_erase_mult; /* Secure erase multiplier */ 54 + unsigned int trim_timeout; /* In milliseconds */ 49 55 }; 50 56 51 57 struct sd_scr { ··· 60 52 unsigned char bus_widths; 61 53 #define SD_SCR_BUS_WIDTH_1 (1<<0) 62 54 #define SD_SCR_BUS_WIDTH_4 (1<<2) 55 + }; 56 + 57 + struct sd_ssr { 58 + unsigned int au; /* In sectors */ 59 + unsigned int erase_timeout; /* In milliseconds */ 60 + unsigned int erase_offset; /* In milliseconds */ 63 61 }; 64 62 65 63 struct sd_switch_caps { ··· 120 106 #define MMC_QUIRK_NONSTD_SDIO (1<<2) /* non-standard SDIO card attached */ 121 107 /* (missing CIA registers) */ 122 108 109 + unsigned int erase_size; /* erase size in sectors */ 110 + unsigned int erase_shift; /* if erase unit is power 2 */ 111 + unsigned int pref_erase; /* in sectors */ 112 + u8 erased_byte; /* value of erased bytes */ 113 + 123 114 u32 raw_cid[4]; /* raw card CID */ 124 115 u32 raw_csd[4]; /* raw card CSD */ 125 116 u32 raw_scr[2]; /* raw card SCR */ ··· 132 113 struct mmc_csd csd; /* card specific */ 133 114 struct mmc_ext_csd ext_csd; /* mmc v4 extended card specific */ 134 115 struct sd_scr scr; /* extra SD information */ 116 + struct sd_ssr ssr; /* yet more SD information */ 135 117 struct sd_switch_caps sw_caps; /* switch (CMD6) caps */ 136 118 137 119 unsigned int sdio_funcs; /* number of SDIO functions */
+19
include/linux/mmc/core.h
··· 92 92 * actively failing requests 93 93 */ 94 94 95 + unsigned int erase_timeout; /* in milliseconds */ 96 + 95 97 struct mmc_data *data; /* data segment associated with cmd */ 96 98 struct mmc_request *mrq; /* associated request */ 97 99 }; ··· 135 133 extern int mmc_wait_for_cmd(struct mmc_host *, struct mmc_command *, int); 136 134 extern int mmc_wait_for_app_cmd(struct mmc_host *, struct mmc_card *, 137 135 struct mmc_command *, int); 136 + 137 + #define MMC_ERASE_ARG 0x00000000 138 + #define MMC_SECURE_ERASE_ARG 0x80000000 139 + #define MMC_TRIM_ARG 0x00000001 140 + #define MMC_SECURE_TRIM1_ARG 0x80000001 141 + #define MMC_SECURE_TRIM2_ARG 0x80008000 142 + 143 + #define MMC_SECURE_ARGS 0x80000000 144 + #define MMC_TRIM_ARGS 0x00008001 145 + 146 + extern int mmc_erase(struct mmc_card *card, unsigned int from, unsigned int nr, 147 + unsigned int arg); 148 + extern int mmc_can_erase(struct mmc_card *card); 149 + extern int mmc_can_trim(struct mmc_card *card); 150 + extern int mmc_can_secure_erase_trim(struct mmc_card *card); 151 + extern int mmc_erase_group_aligned(struct mmc_card *card, unsigned int from, 152 + unsigned int nr); 138 153 139 154 extern void mmc_set_data_timeout(struct mmc_data *, const struct mmc_card *); 140 155 extern unsigned int mmc_align_data_size(struct mmc_card *, unsigned int);
+1
include/linux/mmc/host.h
··· 156 156 #define MMC_CAP_DISABLE (1 << 7) /* Can the host be disabled */ 157 157 #define MMC_CAP_NONREMOVABLE (1 << 8) /* Nonremovable e.g. eMMC */ 158 158 #define MMC_CAP_WAIT_WHILE_BUSY (1 << 9) /* Waits while card is busy */ 159 + #define MMC_CAP_ERASE (1 << 10) /* Allow erase/trim commands */ 159 160 160 161 mmc_pm_flag_t pm_caps; /* supported pm features */ 161 162
+19 -7
include/linux/mmc/mmc.h
··· 251 251 * EXT_CSD fields 252 252 */ 253 253 254 - #define EXT_CSD_BUS_WIDTH 183 /* R/W */ 255 - #define EXT_CSD_HS_TIMING 185 /* R/W */ 256 - #define EXT_CSD_CARD_TYPE 196 /* RO */ 257 - #define EXT_CSD_STRUCTURE 194 /* RO */ 258 - #define EXT_CSD_REV 192 /* RO */ 259 - #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */ 260 - #define EXT_CSD_S_A_TIMEOUT 217 254 + #define EXT_CSD_ERASE_GROUP_DEF 175 /* R/W */ 255 + #define EXT_CSD_ERASED_MEM_CONT 181 /* RO */ 256 + #define EXT_CSD_BUS_WIDTH 183 /* R/W */ 257 + #define EXT_CSD_HS_TIMING 185 /* R/W */ 258 + #define EXT_CSD_REV 192 /* RO */ 259 + #define EXT_CSD_STRUCTURE 194 /* RO */ 260 + #define EXT_CSD_CARD_TYPE 196 /* RO */ 261 + #define EXT_CSD_SEC_CNT 212 /* RO, 4 bytes */ 262 + #define EXT_CSD_S_A_TIMEOUT 217 /* RO */ 263 + #define EXT_CSD_ERASE_TIMEOUT_MULT 223 /* RO */ 264 + #define EXT_CSD_HC_ERASE_GRP_SIZE 224 /* RO */ 265 + #define EXT_CSD_SEC_TRIM_MULT 229 /* RO */ 266 + #define EXT_CSD_SEC_ERASE_MULT 230 /* RO */ 267 + #define EXT_CSD_SEC_FEATURE_SUPPORT 231 /* RO */ 268 + #define EXT_CSD_TRIM_MULT 232 /* RO */ 261 269 262 270 /* 263 271 * EXT_CSD field definitions ··· 282 274 #define EXT_CSD_BUS_WIDTH_1 0 /* Card is in 1 bit mode */ 283 275 #define EXT_CSD_BUS_WIDTH_4 1 /* Card is in 4 bit mode */ 284 276 #define EXT_CSD_BUS_WIDTH_8 2 /* Card is in 8 bit mode */ 277 + 278 + #define EXT_CSD_SEC_ER_EN BIT(0) 279 + #define EXT_CSD_SEC_BD_BLK_EN BIT(2) 280 + #define EXT_CSD_SEC_GB_CL_EN BIT(4) 285 281 286 282 /* 287 283 * MMC_SWITCH access modes
+5
include/linux/mmc/sd.h
··· 21 21 /* class 10 */ 22 22 #define SD_SWITCH 6 /* adtc [31:0] See below R1 */ 23 23 24 + /* class 5 */ 25 + #define SD_ERASE_WR_BLK_START 32 /* ac [31:0] data addr R1 */ 26 + #define SD_ERASE_WR_BLK_END 33 /* ac [31:0] data addr R1 */ 27 + 24 28 /* Application commands */ 25 29 #define SD_APP_SET_BUS_WIDTH 6 /* ac [1:0] bus width R1 */ 30 + #define SD_APP_SD_STATUS 13 /* adtc R1 */ 26 31 #define SD_APP_SEND_NUM_WR_BLKS 22 /* adtc R1 */ 27 32 #define SD_APP_OP_COND 41 /* bcr [31:0] OCR R3 */ 28 33 #define SD_APP_SEND_SCR 51 /* adtc R1 */