mmc-sec-sysfs.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Samsung Specific feature
  4. *
  5. * Copyright (C) 2024 Samsung Electronics Co., Ltd.
  6. *
  7. * Authors:
  8. * Storage Driver <[email protected]>
  9. */
  10. #include <linux/sysfs.h>
  11. #include <linux/mmc/slot-gpio.h>
  12. #include <linux/mmc/mmc.h>
  13. #include "mmc-sec-sysfs.h"
  14. #include "../core/card.h"
  15. struct device *sec_sdcard_cmd_dev;
  16. struct device *sec_sdinfo_cmd_dev;
  17. struct device *sec_sddata_cmd_dev;
  18. static const char *const uhs_speeds[] = {
  19. [UHS_SDR12_BUS_SPEED] = "SDR12",
  20. [UHS_SDR25_BUS_SPEED] = "SDR25",
  21. [UHS_SDR50_BUS_SPEED] = "SDR50",
  22. [UHS_SDR104_BUS_SPEED] = "SDR104",
  23. [UHS_DDR50_BUS_SPEED] = "DDR50",
  24. };
  25. static ssize_t sd_sec_status_show(struct device *dev,
  26. struct device_attribute *attr, char *buf)
  27. {
  28. struct mmc_host *host = dev_get_drvdata(dev);
  29. if (!mmc_gpio_get_cd(host)) {
  30. if (sdi.sd_slot_type == SEC_HYBRID_SD_SLOT) {
  31. pr_info("SD slot tray Removed.\n");
  32. return sysfs_emit(buf, "Notray\n");
  33. }
  34. }
  35. if (host->card) {
  36. pr_info("SD card inserted.\n");
  37. return sysfs_emit(buf, "Insert\n");
  38. }
  39. pr_info("SD card removed.\n");
  40. return sysfs_emit(buf, "Remove\n");
  41. }
  42. static ssize_t sd_sec_maxmode_show(struct device *dev,
  43. struct device_attribute *attr, char *buf)
  44. {
  45. struct mmc_host *host = dev_get_drvdata(dev);
  46. const char *bus_speed_mode = "";
  47. if (host->caps & MMC_CAP_UHS_SDR104)
  48. bus_speed_mode = "SDR104";
  49. else if (host->caps & MMC_CAP_UHS_DDR50)
  50. bus_speed_mode = "DDR50";
  51. else if (host->caps & MMC_CAP_UHS_SDR50)
  52. bus_speed_mode = "SDR50";
  53. else if (host->caps & MMC_CAP_UHS_SDR25)
  54. bus_speed_mode = "SDR25";
  55. else if (host->caps & MMC_CAP_UHS_SDR12)
  56. bus_speed_mode = "SDR12";
  57. else
  58. bus_speed_mode = "HS";
  59. dev_info(dev, "%s: Max supported Host Speed Mode = %s\n",
  60. __func__, bus_speed_mode);
  61. return sysfs_emit(buf, "%s\n", bus_speed_mode);
  62. }
  63. static inline void sd_sec_get_size(struct mmc_card *card, char *buf, int len)
  64. {
  65. static const char *const unit[] = {"KB", "MB", "GB", "TB"};
  66. int capacity;
  67. int digit = 1;
  68. if (card->csd.read_blkbits == 9) /* 1 Sector = 512 Bytes */
  69. capacity = (card->csd.capacity) >> 1;
  70. else if (card->csd.read_blkbits == 11) /* 1 Sector = 2048 Bytes */
  71. capacity = (card->csd.capacity) << 1;
  72. else /* 1 Sector = 1024 Bytes */
  73. capacity = card->csd.capacity;
  74. if (capacity >= 380000000 && capacity <= 410000000)
  75. snprintf(buf, len, "400GB");
  76. else if (capacity >= 190000000 && capacity <= 210000000)
  77. snprintf(buf, len, "200GB");
  78. else {
  79. while ((capacity >> 1) > 0) {
  80. capacity >>= 1;
  81. digit++;
  82. }
  83. snprintf(buf, len, "%d%s", 1 << (digit % 10), unit[digit / 10]);
  84. }
  85. }
  86. static inline void sd_sec_get_speedmode(struct mmc_card *card,
  87. const char **speedmode)
  88. {
  89. if (mmc_card_uhs(card))
  90. *speedmode = uhs_speeds[card->sd_bus_speed];
  91. else if (mmc_card_hs(card))
  92. *speedmode = "HS";
  93. else
  94. *speedmode = "DS";
  95. }
  96. static ssize_t sd_sec_curmode_show(struct device *dev,
  97. struct device_attribute *attr, char *buf)
  98. {
  99. struct mmc_host *host = dev_get_drvdata(dev);
  100. const char *bus_speed_mode = "No Card";
  101. if (host && host->card)
  102. sd_sec_get_speedmode(host->card, &bus_speed_mode);
  103. dev_info(dev, "%s: Current SD Card Speed = %s\n",
  104. __func__, bus_speed_mode);
  105. return sysfs_emit(buf, "%s\n", bus_speed_mode);
  106. }
  107. static ssize_t sd_sec_detect_cnt_show(struct device *dev,
  108. struct device_attribute *attr, char *buf)
  109. {
  110. dev_info(dev, "%s : CD count is = %u\n",
  111. __func__, sdi.card_detect_cnt);
  112. return sysfs_emit(buf, "%u\n", sdi.card_detect_cnt);
  113. }
  114. static ssize_t sd_sec_summary_show(struct device *dev,
  115. struct device_attribute *attr, char *buf)
  116. {
  117. struct mmc_host *host = dev_get_drvdata(dev);
  118. struct mmc_card *card;
  119. struct mmc_sd_sec_status_err_info *status_err = &sdi.status_err;
  120. unsigned int serial;
  121. char size[6];
  122. const char *bus_speed_mode = "";
  123. if (host && host->card) {
  124. card = host->card;
  125. serial = card->cid.serial & (0x0000FFFF);
  126. sd_sec_get_size(card, size, sizeof(size));
  127. sd_sec_get_speedmode(card, &bus_speed_mode);
  128. dev_info(dev, "MANID: 0x%02X, SERIAL: %04X, SIZE: %s, SPEEDMODE: %s\n",
  129. card->cid.manfid, serial, size, bus_speed_mode);
  130. return sysfs_emit(buf, "\"MANID\":\"0x%02X\",\"SERIAL\":\"%04X\""\
  131. ",\"SIZE\":\"%s\",\"SPEEDMODE\":\"%s\",\"NOTI\":\"%d\"\n",
  132. card->cid.manfid, serial, size, bus_speed_mode,
  133. status_err->noti_cnt);
  134. } else {
  135. dev_info(dev, "%s : No SD Card\n", __func__);
  136. return sysfs_emit(buf, "\"MANID\":\"NoCard\",\"SERIAL\":\"NoCard\""\
  137. ",\"SIZE\":\"NoCard\",\"SPEEDMODE\":\"NoCard\""\
  138. ",\"NOTI\":\"NoCard\"\n");
  139. }
  140. }
  141. static inline void sd_sec_calc_error_count(struct mmc_sd_sec_err_info *err_log,
  142. unsigned long long *crc_cnt, unsigned long long *tmo_cnt)
  143. {
  144. int i = 0;
  145. /* Only sbc(0,1)/cmd(2,3)/data(4,5) is checked. */
  146. for (i = 0; i < 6; i++) {
  147. if (err_log[i].err_type == -EILSEQ && *crc_cnt < U64_MAX)
  148. *crc_cnt += err_log[i].count;
  149. if (err_log[i].err_type == -ETIMEDOUT && *tmo_cnt < U64_MAX)
  150. *tmo_cnt += err_log[i].count;
  151. }
  152. }
  153. static ssize_t mmc_sd_sec_error_count_show(struct device *dev,
  154. struct device_attribute *attr, char *buf)
  155. {
  156. struct mmc_host *host = dev_get_drvdata(dev);
  157. struct mmc_card *card = host->card;
  158. struct mmc_sd_sec_err_info *err_log;
  159. struct mmc_sd_sec_status_err_info *status_err;
  160. u64 crc_cnt = 0;
  161. u64 tmo_cnt = 0;
  162. int len = 0;
  163. int i;
  164. if (!card) {
  165. len = sysfs_emit(buf, "No card\n");
  166. goto out;
  167. }
  168. err_log = &sdi.err_info[0];
  169. status_err = &sdi.status_err;
  170. len += snprintf(buf, PAGE_SIZE,
  171. "type : err status: first_issue_time: last_issue_time: count\n");
  172. for (i = 0; i < MAX_LOG_INDEX; i++) {
  173. len += snprintf(buf + len, PAGE_SIZE - len,
  174. "%5s:%4d 0x%08x %16llu, %16llu, %10d\n",
  175. err_log[i].type, err_log[i].err_type,
  176. err_log[i].status,
  177. err_log[i].first_issue_time,
  178. err_log[i].last_issue_time,
  179. err_log[i].count);
  180. }
  181. sd_sec_calc_error_count(err_log, &crc_cnt, &tmo_cnt);
  182. len += snprintf(buf + len, PAGE_SIZE - len,
  183. "GE:%d,CC:%d,ECC:%d,WP:%d,OOR:%d,CRC:%lld,TMO:%lld\n",
  184. status_err->ge_cnt, status_err->cc_cnt,
  185. status_err->ecc_cnt, status_err->wp_cnt,
  186. status_err->oor_cnt, crc_cnt, tmo_cnt);
  187. out:
  188. return len;
  189. }
  190. static DEVICE_ATTR(status, 0444, sd_sec_status_show, NULL);
  191. static DEVICE_ATTR(max_mode, 0444, sd_sec_maxmode_show, NULL);
  192. static DEVICE_ATTR(current_mode, 0444, sd_sec_curmode_show, NULL);
  193. static DEVICE_ATTR(cd_cnt, 0444, sd_sec_detect_cnt_show, NULL);
  194. static DEVICE_ATTR(sdcard_summary, 0444, sd_sec_summary_show, NULL);
  195. static DEVICE_ATTR(err_count, 0444, mmc_sd_sec_error_count_show, NULL);
  196. static ssize_t sd_sec_cid_show(struct device *dev,
  197. struct device_attribute *attr, char *buf)
  198. {
  199. struct mmc_host *host = dev_get_drvdata(dev);
  200. struct mmc_card *card = host->card;
  201. if (!card)
  202. return sysfs_emit(buf, "no card\n");
  203. return sysfs_emit(buf, "%08x%08x%08x%08x\n",
  204. card->raw_cid[0], card->raw_cid[1],
  205. card->raw_cid[2], card->raw_cid[3]);
  206. }
  207. static ssize_t sd_sec_count_show(struct device *dev,
  208. struct device_attribute *attr, char *buf)
  209. {
  210. struct mmc_host *host = dev_get_drvdata(dev);
  211. struct mmc_card *card = host->card;
  212. struct mmc_sd_sec_err_info *err_log;
  213. u64 total_cnt = 0;
  214. int i;
  215. if (!card)
  216. return sysfs_emit(buf, "no card\n");
  217. err_log = &sdi.err_info[0];
  218. for (i = 0; i < 6; i++) {
  219. if (total_cnt < U64_MAX)
  220. total_cnt += err_log[i].count;
  221. }
  222. return sysfs_emit(buf, "%lld\n", total_cnt);
  223. }
  224. static inline bool is_bad_condition(struct mmc_sd_sec_status_err_info *status_err,
  225. u64 crc_cnt, u64 tmo_cnt)
  226. {
  227. return (status_err->ge_cnt > 100 || status_err->ecc_cnt > 0 ||
  228. status_err->wp_cnt > 0 || status_err->oor_cnt > 10 ||
  229. crc_cnt > 100 || tmo_cnt > 100);
  230. }
  231. static ssize_t sd_sec_health_show(struct device *dev,
  232. struct device_attribute *attr, char *buf)
  233. {
  234. struct mmc_host *host = dev_get_drvdata(dev);
  235. struct mmc_card *card = host->card;
  236. struct mmc_sd_sec_err_info *err_log = &sdi.err_info[0];
  237. struct mmc_sd_sec_status_err_info *status_err = &sdi.status_err;
  238. u64 crc_cnt = 0;
  239. u64 tmo_cnt = 0;
  240. /* There should be no spaces in 'No Card'(Vold Team). */
  241. if (!card)
  242. return sysfs_emit(buf, "NOCARD\n");
  243. sd_sec_calc_error_count(err_log, &crc_cnt, &tmo_cnt);
  244. if (is_bad_condition(status_err, crc_cnt, tmo_cnt))
  245. return sysfs_emit(buf, "BAD\n");
  246. return sysfs_emit(buf, "GOOD\n");
  247. }
  248. static ssize_t sd_sec_reason_show(struct device *dev,
  249. struct device_attribute *attr, char *buf)
  250. {
  251. struct mmc_host *host = dev_get_drvdata(dev);
  252. struct mmc_card *card = host->card;
  253. char *reason = NULL;
  254. if (card)
  255. reason = mmc_card_readonly(card) ? "PERMWP" : "NORMAL";
  256. else
  257. reason = host->unused ? "INITFAIL" : "NOCARD";
  258. return sysfs_emit(buf, "%s\n", reason);
  259. }
  260. static DEVICE_ATTR(data, 0444, sd_sec_cid_show, NULL);
  261. static DEVICE_ATTR(sd_count, 0444, sd_sec_count_show, NULL);
  262. static DEVICE_ATTR(fc, 0444, sd_sec_health_show, NULL);
  263. static DEVICE_ATTR(reason, 0444, sd_sec_reason_show, NULL);
  264. #define MMC_SD_SEC_CALC_STATUS_ERR(member) ({ \
  265. cur_status_err->member = status_err->member - saved_status_err->member; })
  266. static inline void mmc_sd_sec_get_curr_err_info(struct mmc_sd_sec_device_info *cdi,
  267. struct mmc_card *card, unsigned long long *crc_cnt,
  268. unsigned long long *tmo_cnt, struct mmc_sd_sec_status_err_info *cur_status_err)
  269. {
  270. struct mmc_sd_sec_err_info *err_log = &cdi->err_info[0];
  271. struct mmc_sd_sec_err_info *saved_err_log = &cdi->saved_err_info[0];
  272. struct mmc_sd_sec_status_err_info *status_err = &cdi->status_err;
  273. struct mmc_sd_sec_status_err_info *saved_status_err = &cdi->saved_status_err;
  274. int i;
  275. /* Only sbc(0,1)/cmd(2,3)/data(4,5) is checked. */
  276. for (i = 0; i < 6; i++) {
  277. if (err_log[i].err_type == -EILSEQ && *crc_cnt < U64_MAX)
  278. *crc_cnt += (err_log[i].count - saved_err_log[i].count);
  279. if (err_log[i].err_type == -ETIMEDOUT && *tmo_cnt < U64_MAX)
  280. *tmo_cnt += (err_log[i].count - saved_err_log[i].count);
  281. }
  282. MMC_SD_SEC_CALC_STATUS_ERR(ge_cnt);
  283. MMC_SD_SEC_CALC_STATUS_ERR(cc_cnt);
  284. MMC_SD_SEC_CALC_STATUS_ERR(ecc_cnt);
  285. MMC_SD_SEC_CALC_STATUS_ERR(wp_cnt);
  286. MMC_SD_SEC_CALC_STATUS_ERR(oor_cnt);
  287. MMC_SD_SEC_CALC_STATUS_ERR(noti_cnt);
  288. }
  289. #define MMC_SD_SEC_SAVE_STATUS_ERR(member) ({ \
  290. saved_status_err->member = status_err->member; })
  291. static inline void mmc_sd_sec_save_err_info(struct mmc_sd_sec_device_info *cdi)
  292. {
  293. struct mmc_sd_sec_err_info *err_log = &cdi->err_info[0];
  294. struct mmc_sd_sec_err_info *saved_err_log = &cdi->saved_err_info[0];
  295. struct mmc_sd_sec_status_err_info *status_err = &cdi->status_err;
  296. struct mmc_sd_sec_status_err_info *saved_status_err = &cdi->saved_status_err;
  297. int i;
  298. /* Save current error count */
  299. for (i = 0; i < MAX_LOG_INDEX; i++)
  300. saved_err_log[i].count = err_log[i].count;
  301. MMC_SD_SEC_SAVE_STATUS_ERR(ge_cnt);
  302. MMC_SD_SEC_SAVE_STATUS_ERR(cc_cnt);
  303. MMC_SD_SEC_SAVE_STATUS_ERR(ecc_cnt);
  304. MMC_SD_SEC_SAVE_STATUS_ERR(wp_cnt);
  305. MMC_SD_SEC_SAVE_STATUS_ERR(oor_cnt);
  306. MMC_SD_SEC_SAVE_STATUS_ERR(noti_cnt);
  307. }
  308. static ssize_t sd_sec_data_show(struct device *dev,
  309. struct device_attribute *attr, char *buf)
  310. {
  311. struct mmc_host *host = dev_get_drvdata(dev);
  312. struct mmc_card *card = host->card;
  313. struct mmc_sd_sec_status_err_info status_err;
  314. u64 crc_cnt = 0;
  315. u64 tmo_cnt = 0;
  316. if (!card)
  317. return sysfs_emit(buf,
  318. "\"GE\":\"0\",\"CC\":\"0\",\"ECC\":\"0\",\"WP\":\"0\"," \
  319. "\"OOR\":\"0\",\"CRC\":\"0\",\"TMO\":\"0\"\n");
  320. memset(&status_err, 0, sizeof(struct mmc_sd_sec_status_err_info));
  321. mmc_sd_sec_get_curr_err_info(&sdi, card, &crc_cnt, &tmo_cnt, &status_err);
  322. return sysfs_emit(buf,
  323. "\"GE\":\"%d\",\"CC\":\"%d\",\"ECC\":\"%d\",\"WP\":\"%d\"," \
  324. "\"OOR\":\"%d\",\"CRC\":\"%lld\",\"TMO\":\"%lld\"\n",
  325. status_err.ge_cnt,
  326. status_err.cc_cnt,
  327. status_err.ecc_cnt,
  328. status_err.wp_cnt,
  329. status_err.oor_cnt,
  330. crc_cnt, tmo_cnt);
  331. }
  332. static ssize_t sd_sec_data_store(struct device *dev,
  333. struct device_attribute *attr, const char *buf, size_t count)
  334. {
  335. struct mmc_host *host = dev_get_drvdata(dev);
  336. struct mmc_card *card = host->card;
  337. if (!card)
  338. return -ENODEV;
  339. if ((buf[0] != 'C' && buf[0] != 'c') || (count != 1))
  340. return -EINVAL;
  341. mmc_sd_sec_save_err_info(&sdi);
  342. return count;
  343. }
  344. static DEVICE_ATTR(sd_data, 0664, sd_sec_data_show, sd_sec_data_store);
  345. static struct attribute *sdcard_attributes[] = {
  346. &dev_attr_status.attr,
  347. &dev_attr_max_mode.attr,
  348. &dev_attr_current_mode.attr,
  349. &dev_attr_cd_cnt.attr,
  350. &dev_attr_sdcard_summary.attr,
  351. &dev_attr_err_count.attr,
  352. NULL,
  353. };
  354. static struct attribute_group sdcard_attr_group = {
  355. .attrs = sdcard_attributes,
  356. };
  357. static struct attribute *sdinfo_attributes[] = {
  358. &dev_attr_data.attr,
  359. &dev_attr_sd_count.attr,
  360. &dev_attr_fc.attr,
  361. &dev_attr_reason.attr,
  362. NULL,
  363. };
  364. static struct attribute_group sdinfo_attr_group = {
  365. .attrs = sdinfo_attributes,
  366. };
  367. static struct attribute *sddata_attributes[] = {
  368. &dev_attr_sd_data.attr,
  369. NULL,
  370. };
  371. static struct attribute_group sddata_attr_group = {
  372. .attrs = sddata_attributes,
  373. };
  374. void sd_sec_create_sysfs_group(struct mmc_host *host, struct device **dev,
  375. const struct attribute_group *dev_attr_group,
  376. const char *group_name)
  377. {
  378. *dev = sec_device_create(host, group_name);
  379. if (IS_ERR(*dev)) {
  380. pr_err("%s: Failed to create device for %s!\n",
  381. __func__, group_name);
  382. return;
  383. }
  384. if (sysfs_create_group(&(*dev)->kobj, dev_attr_group))
  385. pr_err("%s: Failed to create %s sysfs group\n",
  386. __func__, group_name);
  387. }
  388. void sd_sec_init_sysfs(struct mmc_host *host)
  389. {
  390. sd_sec_create_sysfs_group(host, &sec_sdcard_cmd_dev,
  391. &sdcard_attr_group, "sdcard");
  392. sd_sec_create_sysfs_group(host, &sec_sdinfo_cmd_dev,
  393. &sdinfo_attr_group, "sdinfo");
  394. sd_sec_create_sysfs_group(host, &sec_sddata_cmd_dev,
  395. &sddata_attr_group, "sddata");
  396. }