sdio_cis.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * linux/drivers/mmc/core/sdio_cis.c
  4. *
  5. * Author: Nicolas Pitre
  6. * Created: June 11, 2007
  7. * Copyright: MontaVista Software Inc.
  8. *
  9. * Copyright 2007 Pierre Ossman
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/slab.h>
  13. #include <linux/mmc/host.h>
  14. #include <linux/mmc/card.h>
  15. #include <linux/mmc/sdio.h>
  16. #include <linux/mmc/sdio_func.h>
  17. #include "sdio_cis.h"
  18. #include "sdio_ops.h"
  19. #define SDIO_READ_CIS_TIMEOUT_MS (10 * 1000) /* 10s */
  20. static int cistpl_vers_1(struct mmc_card *card, struct sdio_func *func,
  21. const unsigned char *buf, unsigned size)
  22. {
  23. u8 major_rev, minor_rev;
  24. unsigned i, nr_strings;
  25. char **buffer, *string;
  26. if (size < 2)
  27. return 0;
  28. major_rev = buf[0];
  29. minor_rev = buf[1];
  30. /* Find all null-terminated (including zero length) strings in
  31. the TPLLV1_INFO field. Trailing garbage is ignored. */
  32. buf += 2;
  33. size -= 2;
  34. nr_strings = 0;
  35. for (i = 0; i < size; i++) {
  36. if (buf[i] == 0xff)
  37. break;
  38. if (buf[i] == 0)
  39. nr_strings++;
  40. }
  41. if (nr_strings == 0)
  42. return 0;
  43. size = i;
  44. buffer = kzalloc(sizeof(char*) * nr_strings + size, GFP_KERNEL);
  45. if (!buffer)
  46. return -ENOMEM;
  47. string = (char*)(buffer + nr_strings);
  48. for (i = 0; i < nr_strings; i++) {
  49. buffer[i] = string;
  50. strcpy(string, buf);
  51. string += strlen(string) + 1;
  52. buf += strlen(buf) + 1;
  53. }
  54. if (func) {
  55. func->major_rev = major_rev;
  56. func->minor_rev = minor_rev;
  57. func->num_info = nr_strings;
  58. func->info = (const char**)buffer;
  59. } else {
  60. card->major_rev = major_rev;
  61. card->minor_rev = minor_rev;
  62. card->num_info = nr_strings;
  63. card->info = (const char**)buffer;
  64. }
  65. return 0;
  66. }
  67. static int cistpl_manfid(struct mmc_card *card, struct sdio_func *func,
  68. const unsigned char *buf, unsigned size)
  69. {
  70. unsigned int vendor, device;
  71. /* TPLMID_MANF */
  72. vendor = buf[0] | (buf[1] << 8);
  73. /* TPLMID_CARD */
  74. device = buf[2] | (buf[3] << 8);
  75. if (func) {
  76. func->vendor = vendor;
  77. func->device = device;
  78. } else {
  79. card->cis.vendor = vendor;
  80. card->cis.device = device;
  81. }
  82. return 0;
  83. }
  84. static const unsigned char speed_val[16] =
  85. { 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80 };
  86. static const unsigned int speed_unit[8] =
  87. { 10000, 100000, 1000000, 10000000, 0, 0, 0, 0 };
  88. typedef int (tpl_parse_t)(struct mmc_card *, struct sdio_func *,
  89. const unsigned char *, unsigned);
  90. struct cis_tpl {
  91. unsigned char code;
  92. unsigned char min_size;
  93. tpl_parse_t *parse;
  94. };
  95. static int cis_tpl_parse(struct mmc_card *card, struct sdio_func *func,
  96. const char *tpl_descr,
  97. const struct cis_tpl *tpl, int tpl_count,
  98. unsigned char code,
  99. const unsigned char *buf, unsigned size)
  100. {
  101. int i, ret;
  102. /* look for a matching code in the table */
  103. for (i = 0; i < tpl_count; i++, tpl++) {
  104. if (tpl->code == code)
  105. break;
  106. }
  107. if (i < tpl_count) {
  108. if (size >= tpl->min_size) {
  109. if (tpl->parse)
  110. ret = tpl->parse(card, func, buf, size);
  111. else
  112. ret = -EILSEQ; /* known tuple, not parsed */
  113. } else {
  114. /* invalid tuple */
  115. ret = -EINVAL;
  116. }
  117. if (ret && ret != -EILSEQ && ret != -ENOENT) {
  118. pr_err("%s: bad %s tuple 0x%02x (%u bytes)\n",
  119. mmc_hostname(card->host), tpl_descr, code, size);
  120. }
  121. } else {
  122. /* unknown tuple */
  123. ret = -ENOENT;
  124. }
  125. return ret;
  126. }
  127. static int cistpl_funce_common(struct mmc_card *card, struct sdio_func *func,
  128. const unsigned char *buf, unsigned size)
  129. {
  130. /* Only valid for the common CIS (function 0) */
  131. if (func)
  132. return -EINVAL;
  133. /* TPLFE_FN0_BLK_SIZE */
  134. card->cis.blksize = buf[1] | (buf[2] << 8);
  135. /* TPLFE_MAX_TRAN_SPEED */
  136. card->cis.max_dtr = speed_val[(buf[3] >> 3) & 15] *
  137. speed_unit[buf[3] & 7];
  138. return 0;
  139. }
  140. static int cistpl_funce_func(struct mmc_card *card, struct sdio_func *func,
  141. const unsigned char *buf, unsigned size)
  142. {
  143. unsigned vsn;
  144. unsigned min_size;
  145. /* Only valid for the individual function's CIS (1-7) */
  146. if (!func)
  147. return -EINVAL;
  148. /*
  149. * This tuple has a different length depending on the SDIO spec
  150. * version.
  151. */
  152. vsn = func->card->cccr.sdio_vsn;
  153. min_size = (vsn == SDIO_SDIO_REV_1_00) ? 28 : 42;
  154. if (size == 28 && vsn == SDIO_SDIO_REV_1_10) {
  155. pr_warn("%s: card has broken SDIO 1.1 CIS, forcing SDIO 1.0\n",
  156. mmc_hostname(card->host));
  157. vsn = SDIO_SDIO_REV_1_00;
  158. } else if (size < min_size) {
  159. return -EINVAL;
  160. }
  161. /* TPLFE_MAX_BLK_SIZE */
  162. func->max_blksize = buf[12] | (buf[13] << 8);
  163. /* TPLFE_ENABLE_TIMEOUT_VAL, present in ver 1.1 and above */
  164. if (vsn > SDIO_SDIO_REV_1_00)
  165. func->enable_timeout = (buf[28] | (buf[29] << 8)) * 10;
  166. else
  167. func->enable_timeout = jiffies_to_msecs(HZ);
  168. return 0;
  169. }
  170. /*
  171. * Known TPLFE_TYPEs table for CISTPL_FUNCE tuples.
  172. *
  173. * Note that, unlike PCMCIA, CISTPL_FUNCE tuples are not parsed depending
  174. * on the TPLFID_FUNCTION value of the previous CISTPL_FUNCID as on SDIO
  175. * TPLFID_FUNCTION is always hardcoded to 0x0C.
  176. */
  177. static const struct cis_tpl cis_tpl_funce_list[] = {
  178. { 0x00, 4, cistpl_funce_common },
  179. { 0x01, 0, cistpl_funce_func },
  180. { 0x04, 1+1+6, /* CISTPL_FUNCE_LAN_NODE_ID */ },
  181. };
  182. static int cistpl_funce(struct mmc_card *card, struct sdio_func *func,
  183. const unsigned char *buf, unsigned size)
  184. {
  185. if (size < 1)
  186. return -EINVAL;
  187. return cis_tpl_parse(card, func, "CISTPL_FUNCE",
  188. cis_tpl_funce_list,
  189. ARRAY_SIZE(cis_tpl_funce_list),
  190. buf[0], buf, size);
  191. }
  192. /* Known TPL_CODEs table for CIS tuples */
  193. static const struct cis_tpl cis_tpl_list[] = {
  194. { 0x15, 3, cistpl_vers_1 },
  195. { 0x20, 4, cistpl_manfid },
  196. { 0x21, 2, /* cistpl_funcid */ },
  197. { 0x22, 0, cistpl_funce },
  198. { 0x91, 2, /* cistpl_sdio_std */ },
  199. };
  200. static int sdio_read_cis(struct mmc_card *card, struct sdio_func *func)
  201. {
  202. int ret;
  203. struct sdio_func_tuple *this, **prev;
  204. unsigned i, ptr = 0;
  205. /*
  206. * Note that this works for the common CIS (function number 0) as
  207. * well as a function's CIS * since SDIO_CCCR_CIS and SDIO_FBR_CIS
  208. * have the same offset.
  209. */
  210. for (i = 0; i < 3; i++) {
  211. unsigned char x, fn;
  212. if (func)
  213. fn = func->num;
  214. else
  215. fn = 0;
  216. ret = mmc_io_rw_direct(card, 0, 0,
  217. SDIO_FBR_BASE(fn) + SDIO_FBR_CIS + i, 0, &x);
  218. if (ret)
  219. return ret;
  220. ptr |= x << (i * 8);
  221. }
  222. if (func)
  223. prev = &func->tuples;
  224. else
  225. prev = &card->tuples;
  226. if (*prev)
  227. return -EINVAL;
  228. do {
  229. unsigned char tpl_code, tpl_link;
  230. unsigned long timeout = jiffies +
  231. msecs_to_jiffies(SDIO_READ_CIS_TIMEOUT_MS);
  232. ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_code);
  233. if (ret)
  234. break;
  235. /* 0xff means we're done */
  236. if (tpl_code == 0xff)
  237. break;
  238. /* null entries have no link field or data */
  239. if (tpl_code == 0x00)
  240. continue;
  241. ret = mmc_io_rw_direct(card, 0, 0, ptr++, 0, &tpl_link);
  242. if (ret)
  243. break;
  244. /* a size of 0xff also means we're done */
  245. if (tpl_link == 0xff)
  246. break;
  247. this = kmalloc(sizeof(*this) + tpl_link, GFP_KERNEL);
  248. if (!this)
  249. return -ENOMEM;
  250. for (i = 0; i < tpl_link; i++) {
  251. ret = mmc_io_rw_direct(card, 0, 0,
  252. ptr + i, 0, &this->data[i]);
  253. if (ret)
  254. break;
  255. }
  256. if (ret) {
  257. kfree(this);
  258. break;
  259. }
  260. /* Try to parse the CIS tuple */
  261. ret = cis_tpl_parse(card, func, "CIS",
  262. cis_tpl_list, ARRAY_SIZE(cis_tpl_list),
  263. tpl_code, this->data, tpl_link);
  264. if (ret == -EILSEQ || ret == -ENOENT) {
  265. /*
  266. * The tuple is unknown or known but not parsed.
  267. * Queue the tuple for the function driver.
  268. */
  269. this->next = NULL;
  270. this->code = tpl_code;
  271. this->size = tpl_link;
  272. *prev = this;
  273. prev = &this->next;
  274. if (ret == -ENOENT) {
  275. if (time_after(jiffies, timeout))
  276. break;
  277. #define FMT(type) "%s: queuing " type " CIS tuple 0x%02x [%*ph] (%u bytes)\n"
  278. /*
  279. * Tuples in this range are reserved for
  280. * vendors, so don't warn about them
  281. */
  282. if (tpl_code >= 0x80 && tpl_code <= 0x8f)
  283. pr_debug_ratelimited(FMT("vendor"),
  284. mmc_hostname(card->host),
  285. tpl_code, tpl_link, this->data,
  286. tpl_link);
  287. else
  288. pr_warn_ratelimited(FMT("unknown"),
  289. mmc_hostname(card->host),
  290. tpl_code, tpl_link, this->data,
  291. tpl_link);
  292. }
  293. /* keep on analyzing tuples */
  294. ret = 0;
  295. } else {
  296. /*
  297. * We don't need the tuple anymore if it was
  298. * successfully parsed by the SDIO core or if it is
  299. * not going to be queued for a driver.
  300. */
  301. kfree(this);
  302. }
  303. ptr += tpl_link;
  304. } while (!ret);
  305. /*
  306. * Link in all unknown tuples found in the common CIS so that
  307. * drivers don't have to go digging in two places.
  308. */
  309. if (func)
  310. *prev = card->tuples;
  311. return ret;
  312. }
  313. int sdio_read_common_cis(struct mmc_card *card)
  314. {
  315. return sdio_read_cis(card, NULL);
  316. }
  317. void sdio_free_common_cis(struct mmc_card *card)
  318. {
  319. struct sdio_func_tuple *tuple, *victim;
  320. tuple = card->tuples;
  321. while (tuple) {
  322. victim = tuple;
  323. tuple = tuple->next;
  324. kfree(victim);
  325. }
  326. card->tuples = NULL;
  327. }
  328. int sdio_read_func_cis(struct sdio_func *func)
  329. {
  330. int ret;
  331. ret = sdio_read_cis(func->card, func);
  332. if (ret)
  333. return ret;
  334. /*
  335. * Vendor/device id is optional for function CIS, so
  336. * copy it from the card structure as needed.
  337. */
  338. if (func->vendor == 0) {
  339. func->vendor = func->card->cis.vendor;
  340. func->device = func->card->cis.device;
  341. }
  342. return 0;
  343. }
  344. void sdio_free_func_cis(struct sdio_func *func)
  345. {
  346. struct sdio_func_tuple *tuple, *victim;
  347. tuple = func->tuples;
  348. while (tuple && tuple != func->card->tuples) {
  349. victim = tuple;
  350. tuple = tuple->next;
  351. kfree(victim);
  352. }
  353. func->tuples = NULL;
  354. }