csr1212.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /*
  2. * csr1212.c -- IEEE 1212 Control and Status Register support for Linux
  3. *
  4. * Copyright (C) 2003 Francois Retief <fgretief@sun.ac.za>
  5. * Steve Kinneberg <kinnebergsteve@acmsystems.com>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  21. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  23. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  24. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  26. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. /* TODO List:
  30. * - Verify interface consistency: i.e., public functions that take a size
  31. * parameter expect size to be in bytes.
  32. * - Convenience functions for reading a block of data from a given offset.
  33. */
  34. #ifndef __KERNEL__
  35. #include <string.h>
  36. #endif
  37. #include "csr1212.h"
  38. /* Permitted key type for each key id */
  39. #define __I (1 << CSR1212_KV_TYPE_IMMEDIATE)
  40. #define __C (1 << CSR1212_KV_TYPE_CSR_OFFSET)
  41. #define __D (1 << CSR1212_KV_TYPE_DIRECTORY)
  42. #define __L (1 << CSR1212_KV_TYPE_LEAF)
  43. static const u_int8_t csr1212_key_id_type_map[0x30] = {
  44. 0, /* Reserved */
  45. __D | __L, /* Descriptor */
  46. __I | __D | __L, /* Bus_Dependent_Info */
  47. __I | __D | __L, /* Vendor */
  48. __I, /* Hardware_Version */
  49. 0, 0, /* Reserved */
  50. __D | __L, /* Module */
  51. 0, 0, 0, 0, /* Reserved */
  52. __I, /* Node_Capabilities */
  53. __L, /* EUI_64 */
  54. 0, 0, 0, /* Reserved */
  55. __D, /* Unit */
  56. __I, /* Specifier_ID */
  57. __I, /* Version */
  58. __I | __C | __D | __L, /* Dependent_Info */
  59. __L, /* Unit_Location */
  60. 0, /* Reserved */
  61. __I, /* Model */
  62. __D, /* Instance */
  63. __L, /* Keyword */
  64. __D, /* Feature */
  65. __L, /* Extended_ROM */
  66. __I, /* Extended_Key_Specifier_ID */
  67. __I, /* Extended_Key */
  68. __I | __C | __D | __L, /* Extended_Data */
  69. __L, /* Modifiable_Descriptor */
  70. __I, /* Directory_ID */
  71. __I, /* Revision */
  72. };
  73. #undef __I
  74. #undef __C
  75. #undef __D
  76. #undef __L
  77. #define quads_to_bytes(_q) ((_q) * sizeof(u_int32_t))
  78. #define bytes_to_quads(_b) (((_b) + sizeof(u_int32_t) - 1) / sizeof(u_int32_t))
  79. static inline void free_keyval(struct csr1212_keyval *kv)
  80. {
  81. if ((kv->key.type == CSR1212_KV_TYPE_LEAF) &&
  82. (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM))
  83. CSR1212_FREE(kv->value.leaf.data);
  84. CSR1212_FREE(kv);
  85. }
  86. static u_int16_t csr1212_crc16(const u_int32_t *buffer, size_t length)
  87. {
  88. int shift;
  89. u_int32_t data;
  90. u_int16_t sum, crc = 0;
  91. for (; length; length--) {
  92. data = CSR1212_BE32_TO_CPU(*buffer);
  93. buffer++;
  94. for (shift = 28; shift >= 0; shift -= 4 ) {
  95. sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
  96. crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
  97. }
  98. crc &= 0xffff;
  99. }
  100. return CSR1212_CPU_TO_BE16(crc);
  101. }
  102. #if 0
  103. /* Microsoft computes the CRC with the bytes in reverse order. Therefore we
  104. * have a special version of the CRC algorithm to account for their buggy
  105. * software. */
  106. static u_int16_t csr1212_msft_crc16(const u_int32_t *buffer, size_t length)
  107. {
  108. int shift;
  109. u_int32_t data;
  110. u_int16_t sum, crc = 0;
  111. for (; length; length--) {
  112. data = CSR1212_LE32_TO_CPU(*buffer);
  113. buffer++;
  114. for (shift = 28; shift >= 0; shift -= 4 ) {
  115. sum = ((crc >> 12) ^ (data >> shift)) & 0xf;
  116. crc = (crc << 4) ^ (sum << 12) ^ (sum << 5) ^ (sum);
  117. }
  118. crc &= 0xffff;
  119. }
  120. return CSR1212_CPU_TO_BE16(crc);
  121. }
  122. #endif
  123. static inline struct csr1212_dentry *csr1212_find_keyval(struct csr1212_keyval *dir,
  124. struct csr1212_keyval *kv)
  125. {
  126. struct csr1212_dentry *pos;
  127. for (pos = dir->value.directory.dentries_head;
  128. pos != NULL; pos = pos->next) {
  129. if (pos->kv == kv)
  130. return pos;
  131. }
  132. return NULL;
  133. }
  134. static inline struct csr1212_keyval *csr1212_find_keyval_offset(struct csr1212_keyval *kv_list,
  135. u_int32_t offset)
  136. {
  137. struct csr1212_keyval *kv;
  138. for (kv = kv_list->next; kv && (kv != kv_list); kv = kv->next) {
  139. if (kv->offset == offset)
  140. return kv;
  141. }
  142. return NULL;
  143. }
  144. /* Creation Routines */
  145. struct csr1212_csr *csr1212_create_csr(struct csr1212_bus_ops *ops,
  146. size_t bus_info_size, void *private)
  147. {
  148. struct csr1212_csr *csr;
  149. csr = CSR1212_MALLOC(sizeof(*csr));
  150. if (!csr)
  151. return NULL;
  152. csr->cache_head =
  153. csr1212_rom_cache_malloc(CSR1212_CONFIG_ROM_SPACE_OFFSET,
  154. CSR1212_CONFIG_ROM_SPACE_SIZE);
  155. if (!csr->cache_head) {
  156. CSR1212_FREE(csr);
  157. return NULL;
  158. }
  159. /* The keyval key id is not used for the root node, but a valid key id
  160. * that can be used for a directory needs to be passed to
  161. * csr1212_new_directory(). */
  162. csr->root_kv = csr1212_new_directory(CSR1212_KV_ID_VENDOR);
  163. if (!csr->root_kv) {
  164. CSR1212_FREE(csr->cache_head);
  165. CSR1212_FREE(csr);
  166. return NULL;
  167. }
  168. csr->bus_info_data = csr->cache_head->data;
  169. csr->bus_info_len = bus_info_size;
  170. csr->crc_len = bus_info_size;
  171. csr->ops = ops;
  172. csr->private = private;
  173. csr->cache_tail = csr->cache_head;
  174. return csr;
  175. }
  176. void csr1212_init_local_csr(struct csr1212_csr *csr,
  177. const u_int32_t *bus_info_data, int max_rom)
  178. {
  179. static const int mr_map[] = { 4, 64, 1024, 0 };
  180. csr->max_rom = mr_map[max_rom];
  181. memcpy(csr->bus_info_data, bus_info_data, csr->bus_info_len);
  182. }
  183. static struct csr1212_keyval *csr1212_new_keyval(u_int8_t type, u_int8_t key)
  184. {
  185. struct csr1212_keyval *kv;
  186. if (key < 0x30 && ((csr1212_key_id_type_map[key] & (1 << type)) == 0))
  187. return NULL;
  188. kv = CSR1212_MALLOC(sizeof(*kv));
  189. if (!kv)
  190. return NULL;
  191. kv->key.type = type;
  192. kv->key.id = key;
  193. kv->associate = NULL;
  194. kv->refcnt = 1;
  195. kv->next = NULL;
  196. kv->prev = NULL;
  197. kv->offset = 0;
  198. kv->valid = 0;
  199. return kv;
  200. }
  201. struct csr1212_keyval *csr1212_new_immediate(u_int8_t key, u_int32_t value)
  202. {
  203. struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_IMMEDIATE, key);
  204. if (!kv)
  205. return NULL;
  206. kv->value.immediate = value;
  207. kv->valid = 1;
  208. return kv;
  209. }
  210. struct csr1212_keyval *csr1212_new_leaf(u_int8_t key, const void *data, size_t data_len)
  211. {
  212. struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, key);
  213. if (!kv)
  214. return NULL;
  215. if (data_len > 0) {
  216. kv->value.leaf.data = CSR1212_MALLOC(data_len);
  217. if (!kv->value.leaf.data) {
  218. CSR1212_FREE(kv);
  219. return NULL;
  220. }
  221. if (data)
  222. memcpy(kv->value.leaf.data, data, data_len);
  223. } else {
  224. kv->value.leaf.data = NULL;
  225. }
  226. kv->value.leaf.len = bytes_to_quads(data_len);
  227. kv->offset = 0;
  228. kv->valid = 1;
  229. return kv;
  230. }
  231. struct csr1212_keyval *csr1212_new_csr_offset(u_int8_t key, u_int32_t csr_offset)
  232. {
  233. struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_CSR_OFFSET, key);
  234. if (!kv)
  235. return NULL;
  236. kv->value.csr_offset = csr_offset;
  237. kv->offset = 0;
  238. kv->valid = 1;
  239. return kv;
  240. }
  241. struct csr1212_keyval *csr1212_new_directory(u_int8_t key)
  242. {
  243. struct csr1212_keyval *kv = csr1212_new_keyval(CSR1212_KV_TYPE_DIRECTORY, key);
  244. if (!kv)
  245. return NULL;
  246. kv->value.directory.len = 0;
  247. kv->offset = 0;
  248. kv->value.directory.dentries_head = NULL;
  249. kv->value.directory.dentries_tail = NULL;
  250. kv->valid = 1;
  251. return kv;
  252. }
  253. int csr1212_associate_keyval(struct csr1212_keyval *kv,
  254. struct csr1212_keyval *associate)
  255. {
  256. if (!kv || !associate)
  257. return CSR1212_EINVAL;
  258. if (kv->key.id == CSR1212_KV_ID_DESCRIPTOR ||
  259. (associate->key.id != CSR1212_KV_ID_DESCRIPTOR &&
  260. associate->key.id != CSR1212_KV_ID_DEPENDENT_INFO &&
  261. associate->key.id != CSR1212_KV_ID_EXTENDED_KEY &&
  262. associate->key.id != CSR1212_KV_ID_EXTENDED_DATA &&
  263. associate->key.id < 0x30))
  264. return CSR1212_EINVAL;
  265. if (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID &&
  266. associate->key.id != CSR1212_KV_ID_EXTENDED_KEY)
  267. return CSR1212_EINVAL;
  268. if (kv->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
  269. associate->key.id != CSR1212_KV_ID_EXTENDED_DATA)
  270. return CSR1212_EINVAL;
  271. if (associate->key.id == CSR1212_KV_ID_EXTENDED_KEY &&
  272. kv->key.id != CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID)
  273. return CSR1212_EINVAL;
  274. if (associate->key.id == CSR1212_KV_ID_EXTENDED_DATA &&
  275. kv->key.id != CSR1212_KV_ID_EXTENDED_KEY)
  276. return CSR1212_EINVAL;
  277. if (kv->associate)
  278. csr1212_release_keyval(kv->associate);
  279. associate->refcnt++;
  280. kv->associate = associate;
  281. return CSR1212_SUCCESS;
  282. }
  283. int csr1212_attach_keyval_to_directory(struct csr1212_keyval *dir,
  284. struct csr1212_keyval *kv)
  285. {
  286. struct csr1212_dentry *dentry;
  287. if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY)
  288. return CSR1212_EINVAL;
  289. dentry = CSR1212_MALLOC(sizeof(*dentry));
  290. if (!dentry)
  291. return CSR1212_ENOMEM;
  292. dentry->kv = kv;
  293. kv->refcnt++;
  294. dentry->next = NULL;
  295. dentry->prev = dir->value.directory.dentries_tail;
  296. if (!dir->value.directory.dentries_head)
  297. dir->value.directory.dentries_head = dentry;
  298. if (dir->value.directory.dentries_tail)
  299. dir->value.directory.dentries_tail->next = dentry;
  300. dir->value.directory.dentries_tail = dentry;
  301. return CSR1212_SUCCESS;
  302. }
  303. struct csr1212_keyval *csr1212_new_extended_immediate(u_int32_t spec, u_int32_t key,
  304. u_int32_t value)
  305. {
  306. struct csr1212_keyval *kvs, *kvk, *kvv;
  307. kvs = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID, spec);
  308. kvk = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY, key);
  309. kvv = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_DATA, value);
  310. if (!kvs || !kvk || !kvv) {
  311. if (kvs)
  312. free_keyval(kvs);
  313. if (kvk)
  314. free_keyval(kvk);
  315. if (kvv)
  316. free_keyval(kvv);
  317. return NULL;
  318. }
  319. /* Don't keep a local reference to the extended key or value. */
  320. kvk->refcnt = 0;
  321. kvv->refcnt = 0;
  322. csr1212_associate_keyval(kvk, kvv);
  323. csr1212_associate_keyval(kvs, kvk);
  324. return kvs;
  325. }
  326. struct csr1212_keyval *csr1212_new_extended_leaf(u_int32_t spec, u_int32_t key,
  327. const void *data, size_t data_len)
  328. {
  329. struct csr1212_keyval *kvs, *kvk, *kvv;
  330. kvs = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID, spec);
  331. kvk = csr1212_new_immediate(CSR1212_KV_ID_EXTENDED_KEY, key);
  332. kvv = csr1212_new_leaf(CSR1212_KV_ID_EXTENDED_DATA, data, data_len);
  333. if (!kvs || !kvk || !kvv) {
  334. if (kvs)
  335. free_keyval(kvs);
  336. if (kvk)
  337. free_keyval(kvk);
  338. if (kvv)
  339. free_keyval(kvv);
  340. return NULL;
  341. }
  342. /* Don't keep a local reference to the extended key or value. */
  343. kvk->refcnt = 0;
  344. kvv->refcnt = 0;
  345. csr1212_associate_keyval(kvk, kvv);
  346. csr1212_associate_keyval(kvs, kvk);
  347. return kvs;
  348. }
  349. struct csr1212_keyval *csr1212_new_descriptor_leaf(u_int8_t dtype, u_int32_t specifier_id,
  350. const void *data, size_t data_len)
  351. {
  352. struct csr1212_keyval *kv;
  353. kv = csr1212_new_leaf(CSR1212_KV_ID_DESCRIPTOR, NULL,
  354. data_len + CSR1212_DESCRIPTOR_LEAF_OVERHEAD);
  355. if (!kv)
  356. return NULL;
  357. CSR1212_DESCRIPTOR_LEAF_SET_TYPE(kv, dtype);
  358. CSR1212_DESCRIPTOR_LEAF_SET_SPECIFIER_ID(kv, specifier_id);
  359. if (data) {
  360. memcpy(CSR1212_DESCRIPTOR_LEAF_DATA(kv), data, data_len);
  361. }
  362. return kv;
  363. }
  364. struct csr1212_keyval *csr1212_new_textual_descriptor_leaf(u_int8_t cwidth,
  365. u_int16_t cset,
  366. u_int16_t language,
  367. const void *data,
  368. size_t data_len)
  369. {
  370. struct csr1212_keyval *kv;
  371. char *lstr;
  372. kv = csr1212_new_descriptor_leaf(0, 0, NULL, data_len +
  373. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_OVERHEAD);
  374. if (!kv)
  375. return NULL;
  376. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_WIDTH(kv, cwidth);
  377. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_CHAR_SET(kv, cset);
  378. CSR1212_TEXTUAL_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language);
  379. lstr = (char*)CSR1212_TEXTUAL_DESCRIPTOR_LEAF_DATA(kv);
  380. /* make sure last quadlet is zeroed out */
  381. *((u_int32_t*)&(lstr[(data_len - 1) & ~0x3])) = 0;
  382. /* don't copy the NUL terminator */
  383. memcpy(lstr, data, data_len);
  384. return kv;
  385. }
  386. static int csr1212_check_minimal_ascii(const char *s)
  387. {
  388. static const char minimal_ascii_table[] = {
  389. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07,
  390. 0x00, 0x00, 0x0a, 0x00, 0x0C, 0x0D, 0x00, 0x00,
  391. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  392. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  393. 0x20, 0x21, 0x22, 0x00, 0x00, 0x25, 0x26, 0x27,
  394. 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
  395. 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
  396. 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0x3e, 0x3f,
  397. 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
  398. 0x48, 0x49, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f,
  399. 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
  400. 0x58, 0x59, 0x5a, 0x00, 0x00, 0x00, 0x00, 0x5f,
  401. 0x00, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  402. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  403. 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
  404. 0x78, 0x79, 0x7a, 0x00, 0x00, 0x00, 0x00, 0x00,
  405. };
  406. for (; *s; s++) {
  407. if (minimal_ascii_table[*s & 0x7F] != *s)
  408. return -1; /* failed */
  409. }
  410. /* String conforms to minimal-ascii, as specified by IEEE 1212,
  411. * par. 7.4 */
  412. return 0;
  413. }
  414. struct csr1212_keyval *csr1212_new_string_descriptor_leaf(const char *s)
  415. {
  416. /* Check if string conform to minimal_ascii format */
  417. if (csr1212_check_minimal_ascii(s))
  418. return NULL;
  419. /* IEEE 1212, par. 7.5.4.1 Textual descriptors (minimal ASCII) */
  420. return csr1212_new_textual_descriptor_leaf(0, 0, 0, s, strlen(s));
  421. }
  422. struct csr1212_keyval *csr1212_new_icon_descriptor_leaf(u_int32_t version,
  423. u_int8_t palette_depth,
  424. u_int8_t color_space,
  425. u_int16_t language,
  426. u_int16_t hscan,
  427. u_int16_t vscan,
  428. u_int32_t *palette,
  429. u_int32_t *pixels)
  430. {
  431. static const int pd[4] = { 0, 4, 16, 256 };
  432. static const int cs[16] = { 4, 2 };
  433. struct csr1212_keyval *kv;
  434. int palette_size = pd[palette_depth] * cs[color_space];
  435. int pixel_size = (hscan * vscan + 3) & ~0x3;
  436. if ((palette_depth && !palette) || !pixels)
  437. return NULL;
  438. kv = csr1212_new_descriptor_leaf(1, 0, NULL,
  439. palette_size + pixel_size +
  440. CSR1212_ICON_DESCRIPTOR_LEAF_OVERHEAD);
  441. if (!kv)
  442. return NULL;
  443. CSR1212_ICON_DESCRIPTOR_LEAF_SET_VERSION(kv, version);
  444. CSR1212_ICON_DESCRIPTOR_LEAF_SET_PALETTE_DEPTH(kv, palette_depth);
  445. CSR1212_ICON_DESCRIPTOR_LEAF_SET_COLOR_SPACE(kv, color_space);
  446. CSR1212_ICON_DESCRIPTOR_LEAF_SET_LANGUAGE(kv, language);
  447. CSR1212_ICON_DESCRIPTOR_LEAF_SET_HSCAN(kv, hscan);
  448. CSR1212_ICON_DESCRIPTOR_LEAF_SET_VSCAN(kv, vscan);
  449. if (palette_size)
  450. memcpy(CSR1212_ICON_DESCRIPTOR_LEAF_PALETTE(kv), palette,
  451. palette_size);
  452. memcpy(CSR1212_ICON_DESCRIPTOR_LEAF_PIXELS(kv), pixels, pixel_size);
  453. return kv;
  454. }
  455. struct csr1212_keyval *csr1212_new_modifiable_descriptor_leaf(u_int16_t max_size,
  456. u_int64_t address)
  457. {
  458. struct csr1212_keyval *kv;
  459. /* IEEE 1212, par. 7.5.4.3 Modifiable descriptors */
  460. kv = csr1212_new_leaf(CSR1212_KV_ID_MODIFIABLE_DESCRIPTOR, NULL, sizeof(u_int64_t));
  461. if(!kv)
  462. return NULL;
  463. CSR1212_MODIFIABLE_DESCRIPTOR_SET_MAX_SIZE(kv, max_size);
  464. CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_HI(kv, address);
  465. CSR1212_MODIFIABLE_DESCRIPTOR_SET_ADDRESS_LO(kv, address);
  466. return kv;
  467. }
  468. static int csr1212_check_keyword(const char *s)
  469. {
  470. for (; *s; s++) {
  471. if (('A' <= *s) && (*s <= 'Z'))
  472. continue;
  473. if (('0' <= *s) && (*s <= '9'))
  474. continue;
  475. if (*s == '-')
  476. continue;
  477. return -1; /* failed */
  478. }
  479. /* String conforms to keyword, as specified by IEEE 1212,
  480. * par. 7.6.5 */
  481. return CSR1212_SUCCESS;
  482. }
  483. struct csr1212_keyval *csr1212_new_keyword_leaf(int strc, const char *strv[])
  484. {
  485. struct csr1212_keyval *kv;
  486. char *buffer;
  487. int i, data_len = 0;
  488. /* Check all keywords to see if they conform to restrictions:
  489. * Only the following characters is allowed ['A'..'Z','0'..'9','-']
  490. * Each word is zero-terminated.
  491. * Also calculate the total length of the keywords.
  492. */
  493. for (i = 0; i < strc; i++) {
  494. if (!strv[i] || csr1212_check_keyword(strv[i])) {
  495. return NULL;
  496. }
  497. data_len += strlen(strv[i]) + 1; /* Add zero-termination char. */
  498. }
  499. /* IEEE 1212, par. 7.6.5 Keyword leaves */
  500. kv = csr1212_new_leaf(CSR1212_KV_ID_KEYWORD, NULL, data_len);
  501. if (!kv)
  502. return NULL;
  503. buffer = (char *)kv->value.leaf.data;
  504. /* make sure last quadlet is zeroed out */
  505. *((u_int32_t*)&(buffer[(data_len - 1) & ~0x3])) = 0;
  506. /* Copy keyword(s) into leaf data buffer */
  507. for (i = 0; i < strc; i++) {
  508. int len = strlen(strv[i]) + 1;
  509. memcpy(buffer, strv[i], len);
  510. buffer += len;
  511. }
  512. return kv;
  513. }
  514. /* Destruction Routines */
  515. void csr1212_detach_keyval_from_directory(struct csr1212_keyval *dir,
  516. struct csr1212_keyval *kv)
  517. {
  518. struct csr1212_dentry *dentry;
  519. if (!kv || !dir || dir->key.type != CSR1212_KV_TYPE_DIRECTORY)
  520. return;
  521. dentry = csr1212_find_keyval(dir, kv);
  522. if (!dentry)
  523. return;
  524. if (dentry->prev)
  525. dentry->prev->next = dentry->next;
  526. if (dentry->next)
  527. dentry->next->prev = dentry->prev;
  528. if (dir->value.directory.dentries_head == dentry)
  529. dir->value.directory.dentries_head = dentry->next;
  530. if (dir->value.directory.dentries_tail == dentry)
  531. dir->value.directory.dentries_tail = dentry->prev;
  532. CSR1212_FREE(dentry);
  533. csr1212_release_keyval(kv);
  534. }
  535. void csr1212_disassociate_keyval(struct csr1212_keyval *kv)
  536. {
  537. if (kv->associate) {
  538. csr1212_release_keyval(kv->associate);
  539. }
  540. kv->associate = NULL;
  541. }
  542. /* This function is used to free the memory taken by a keyval. If the given
  543. * keyval is a directory type, then any keyvals contained in that directory
  544. * will be destroyed as well if their respective refcnts are 0. By means of
  545. * list manipulation, this routine will descend a directory structure in a
  546. * non-recursive manner. */
  547. void _csr1212_destroy_keyval(struct csr1212_keyval *kv)
  548. {
  549. struct csr1212_keyval *k, *a;
  550. struct csr1212_dentry dentry;
  551. struct csr1212_dentry *head, *tail;
  552. dentry.kv = kv;
  553. dentry.next = NULL;
  554. dentry.prev = NULL;
  555. head = &dentry;
  556. tail = head;
  557. while (head) {
  558. k = head->kv;
  559. while (k) {
  560. k->refcnt--;
  561. if (k->refcnt > 0)
  562. break;
  563. a = k->associate;
  564. if (k->key.type == CSR1212_KV_TYPE_DIRECTORY) {
  565. /* If the current entry is a directory, then move all
  566. * the entries to the destruction list. */
  567. if (k->value.directory.dentries_head) {
  568. tail->next = k->value.directory.dentries_head;
  569. k->value.directory.dentries_head->prev = tail;
  570. tail = k->value.directory.dentries_tail;
  571. }
  572. }
  573. free_keyval(k);
  574. k = a;
  575. }
  576. head = head->next;
  577. if (head) {
  578. if (head->prev && head->prev != &dentry) {
  579. CSR1212_FREE(head->prev);
  580. }
  581. head->prev = NULL;
  582. } else if (tail != &dentry)
  583. CSR1212_FREE(tail);
  584. }
  585. }
  586. void csr1212_destroy_csr(struct csr1212_csr *csr)
  587. {
  588. struct csr1212_csr_rom_cache *c, *oc;
  589. struct csr1212_cache_region *cr, *ocr;
  590. csr1212_release_keyval(csr->root_kv);
  591. c = csr->cache_head;
  592. while (c) {
  593. oc = c;
  594. cr = c->filled_head;
  595. while (cr) {
  596. ocr = cr;
  597. cr = cr->next;
  598. CSR1212_FREE(ocr);
  599. }
  600. c = c->next;
  601. CSR1212_FREE(oc);
  602. }
  603. CSR1212_FREE(csr);
  604. }
  605. /* CSR Image Creation */
  606. static int csr1212_append_new_cache(struct csr1212_csr *csr, size_t romsize)
  607. {
  608. struct csr1212_csr_rom_cache *cache;
  609. u_int64_t csr_addr;
  610. if (!csr || !csr->ops->allocate_addr_range ||
  611. !csr->ops->release_addr)
  612. return CSR1212_ENOMEM;
  613. /* ROM size must be a multiple of csr->max_rom */
  614. romsize = (romsize + (csr->max_rom - 1)) & ~(csr->max_rom - 1);
  615. csr_addr = csr->ops->allocate_addr_range(romsize, csr->max_rom, csr->private);
  616. if (csr_addr == ~0ULL) {
  617. return CSR1212_ENOMEM;
  618. }
  619. if (csr_addr < CSR1212_REGISTER_SPACE_BASE) {
  620. /* Invalid address returned from allocate_addr_range(). */
  621. csr->ops->release_addr(csr_addr, csr->private);
  622. return CSR1212_ENOMEM;
  623. }
  624. cache = csr1212_rom_cache_malloc(csr_addr - CSR1212_REGISTER_SPACE_BASE, romsize);
  625. if (!cache) {
  626. csr->ops->release_addr(csr_addr, csr->private);
  627. return CSR1212_ENOMEM;
  628. }
  629. cache->ext_rom = csr1212_new_keyval(CSR1212_KV_TYPE_LEAF, CSR1212_KV_ID_EXTENDED_ROM);
  630. if (!cache->ext_rom) {
  631. csr->ops->release_addr(csr_addr, csr->private);
  632. CSR1212_FREE(cache);
  633. return CSR1212_ENOMEM;
  634. }
  635. if (csr1212_attach_keyval_to_directory(csr->root_kv, cache->ext_rom) != CSR1212_SUCCESS) {
  636. csr1212_release_keyval(cache->ext_rom);
  637. csr->ops->release_addr(csr_addr, csr->private);
  638. CSR1212_FREE(cache);
  639. return CSR1212_ENOMEM;
  640. }
  641. cache->ext_rom->offset = csr_addr - CSR1212_REGISTER_SPACE_BASE;
  642. cache->ext_rom->value.leaf.len = -1;
  643. cache->ext_rom->value.leaf.data = cache->data;
  644. /* Add cache to tail of cache list */
  645. cache->prev = csr->cache_tail;
  646. csr->cache_tail->next = cache;
  647. csr->cache_tail = cache;
  648. return CSR1212_SUCCESS;
  649. }
  650. static inline void csr1212_remove_cache(struct csr1212_csr *csr,
  651. struct csr1212_csr_rom_cache *cache)
  652. {
  653. if (csr->cache_head == cache)
  654. csr->cache_head = cache->next;
  655. if (csr->cache_tail == cache)
  656. csr->cache_tail = cache->prev;
  657. if (cache->prev)
  658. cache->prev->next = cache->next;
  659. if (cache->next)
  660. cache->next->prev = cache->prev;
  661. if (cache->ext_rom) {
  662. csr1212_detach_keyval_from_directory(csr->root_kv, cache->ext_rom);
  663. csr1212_release_keyval(cache->ext_rom);
  664. }
  665. CSR1212_FREE(cache);
  666. }
  667. static int csr1212_generate_layout_subdir(struct csr1212_keyval *dir,
  668. struct csr1212_keyval **layout_tail)
  669. {
  670. struct csr1212_dentry *dentry;
  671. struct csr1212_keyval *dkv;
  672. struct csr1212_keyval *last_extkey_spec = NULL;
  673. struct csr1212_keyval *last_extkey = NULL;
  674. int num_entries = 0;
  675. for (dentry = dir->value.directory.dentries_head; dentry;
  676. dentry = dentry->next) {
  677. for (dkv = dentry->kv; dkv; dkv = dkv->associate) {
  678. /* Special Case: Extended Key Specifier_ID */
  679. if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
  680. if (last_extkey_spec == NULL) {
  681. last_extkey_spec = dkv;
  682. } else if (dkv->value.immediate != last_extkey_spec->value.immediate) {
  683. last_extkey_spec = dkv;
  684. } else {
  685. continue;
  686. }
  687. /* Special Case: Extended Key */
  688. } else if (dkv->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
  689. if (last_extkey == NULL) {
  690. last_extkey = dkv;
  691. } else if (dkv->value.immediate != last_extkey->value.immediate) {
  692. last_extkey = dkv;
  693. } else {
  694. continue;
  695. }
  696. }
  697. num_entries += 1;
  698. switch(dkv->key.type) {
  699. default:
  700. case CSR1212_KV_TYPE_IMMEDIATE:
  701. case CSR1212_KV_TYPE_CSR_OFFSET:
  702. break;
  703. case CSR1212_KV_TYPE_LEAF:
  704. case CSR1212_KV_TYPE_DIRECTORY:
  705. /* Remove from list */
  706. if (dkv->prev && (dkv->prev->next == dkv))
  707. dkv->prev->next = dkv->next;
  708. if (dkv->next && (dkv->next->prev == dkv))
  709. dkv->next->prev = dkv->prev;
  710. //if (dkv == *layout_tail)
  711. // *layout_tail = dkv->prev;
  712. /* Special case: Extended ROM leafs */
  713. if (dkv->key.id == CSR1212_KV_ID_EXTENDED_ROM) {
  714. dkv->value.leaf.len = -1;
  715. /* Don't add Extended ROM leafs in the layout list,
  716. * they are handled differently. */
  717. break;
  718. }
  719. /* Add to tail of list */
  720. dkv->next = NULL;
  721. dkv->prev = *layout_tail;
  722. (*layout_tail)->next = dkv;
  723. *layout_tail = dkv;
  724. break;
  725. }
  726. }
  727. }
  728. return num_entries;
  729. }
  730. size_t csr1212_generate_layout_order(struct csr1212_keyval *kv)
  731. {
  732. struct csr1212_keyval *ltail = kv;
  733. size_t agg_size = 0;
  734. while(kv) {
  735. switch(kv->key.type) {
  736. case CSR1212_KV_TYPE_LEAF:
  737. /* Add 1 quadlet for crc/len field */
  738. agg_size += kv->value.leaf.len + 1;
  739. break;
  740. case CSR1212_KV_TYPE_DIRECTORY:
  741. kv->value.directory.len = csr1212_generate_layout_subdir(kv, &ltail);
  742. /* Add 1 quadlet for crc/len field */
  743. agg_size += kv->value.directory.len + 1;
  744. break;
  745. }
  746. kv = kv->next;
  747. }
  748. return quads_to_bytes(agg_size);
  749. }
  750. struct csr1212_keyval *csr1212_generate_positions(struct csr1212_csr_rom_cache *cache,
  751. struct csr1212_keyval *start_kv,
  752. int start_pos)
  753. {
  754. struct csr1212_keyval *kv = start_kv;
  755. struct csr1212_keyval *okv = start_kv;
  756. int pos = start_pos;
  757. int kv_len = 0, okv_len = 0;
  758. cache->layout_head = kv;
  759. while(kv && pos < cache->size) {
  760. /* Special case: Extended ROM leafs */
  761. if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) {
  762. kv->offset = cache->offset + pos;
  763. }
  764. switch(kv->key.type) {
  765. case CSR1212_KV_TYPE_LEAF:
  766. kv_len = kv->value.leaf.len;
  767. break;
  768. case CSR1212_KV_TYPE_DIRECTORY:
  769. kv_len = kv->value.directory.len;
  770. break;
  771. default:
  772. /* Should never get here */
  773. break;
  774. }
  775. pos += quads_to_bytes(kv_len + 1);
  776. if (pos <= cache->size) {
  777. okv = kv;
  778. okv_len = kv_len;
  779. kv = kv->next;
  780. }
  781. }
  782. cache->layout_tail = okv;
  783. cache->len = (okv->offset - cache->offset) + quads_to_bytes(okv_len + 1);
  784. return kv;
  785. }
  786. static void csr1212_generate_tree_subdir(struct csr1212_keyval *dir,
  787. u_int32_t *data_buffer)
  788. {
  789. struct csr1212_dentry *dentry;
  790. struct csr1212_keyval *last_extkey_spec = NULL;
  791. struct csr1212_keyval *last_extkey = NULL;
  792. int index = 0;
  793. for (dentry = dir->value.directory.dentries_head; dentry; dentry = dentry->next) {
  794. struct csr1212_keyval *a;
  795. for (a = dentry->kv; a; a = a->associate) {
  796. u_int32_t value = 0;
  797. /* Special Case: Extended Key Specifier_ID */
  798. if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY_SPECIFIER_ID) {
  799. if (last_extkey_spec == NULL) {
  800. last_extkey_spec = a;
  801. } else if (a->value.immediate != last_extkey_spec->value.immediate) {
  802. last_extkey_spec = a;
  803. } else {
  804. continue;
  805. }
  806. /* Special Case: Extended Key */
  807. } else if (a->key.id == CSR1212_KV_ID_EXTENDED_KEY) {
  808. if (last_extkey == NULL) {
  809. last_extkey = a;
  810. } else if (a->value.immediate != last_extkey->value.immediate) {
  811. last_extkey = a;
  812. } else {
  813. continue;
  814. }
  815. }
  816. switch(a->key.type) {
  817. case CSR1212_KV_TYPE_IMMEDIATE:
  818. value = a->value.immediate;
  819. break;
  820. case CSR1212_KV_TYPE_CSR_OFFSET:
  821. value = a->value.csr_offset;
  822. break;
  823. case CSR1212_KV_TYPE_LEAF:
  824. value = a->offset;
  825. value -= dir->offset + quads_to_bytes(1+index);
  826. value = bytes_to_quads(value);
  827. break;
  828. case CSR1212_KV_TYPE_DIRECTORY:
  829. value = a->offset;
  830. value -= dir->offset + quads_to_bytes(1+index);
  831. value = bytes_to_quads(value);
  832. break;
  833. default:
  834. /* Should never get here */
  835. break; /* GDB breakpoint */
  836. }
  837. value |= (a->key.id & CSR1212_KV_KEY_ID_MASK) << CSR1212_KV_KEY_SHIFT;
  838. value |= (a->key.type & CSR1212_KV_KEY_TYPE_MASK) <<
  839. (CSR1212_KV_KEY_SHIFT + CSR1212_KV_KEY_TYPE_SHIFT);
  840. data_buffer[index] = CSR1212_CPU_TO_BE32(value);
  841. index++;
  842. }
  843. }
  844. }
  845. void csr1212_fill_cache(struct csr1212_csr_rom_cache *cache)
  846. {
  847. struct csr1212_keyval *kv, *nkv;
  848. struct csr1212_keyval_img *kvi;
  849. for (kv = cache->layout_head; kv != cache->layout_tail->next; kv = nkv) {
  850. kvi = (struct csr1212_keyval_img *)
  851. (cache->data + bytes_to_quads(kv->offset - cache->offset));
  852. switch(kv->key.type) {
  853. default:
  854. case CSR1212_KV_TYPE_IMMEDIATE:
  855. case CSR1212_KV_TYPE_CSR_OFFSET:
  856. /* Should never get here */
  857. break; /* GDB breakpoint */
  858. case CSR1212_KV_TYPE_LEAF:
  859. /* Don't copy over Extended ROM areas, they are
  860. * already filled out! */
  861. if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
  862. memcpy(kvi->data, kv->value.leaf.data,
  863. quads_to_bytes(kv->value.leaf.len));
  864. kvi->length = CSR1212_CPU_TO_BE16(kv->value.leaf.len);
  865. kvi->crc = csr1212_crc16(kvi->data, kv->value.leaf.len);
  866. break;
  867. case CSR1212_KV_TYPE_DIRECTORY:
  868. csr1212_generate_tree_subdir(kv, kvi->data);
  869. kvi->length = CSR1212_CPU_TO_BE16(kv->value.directory.len);
  870. kvi->crc = csr1212_crc16(kvi->data, kv->value.directory.len);
  871. break;
  872. }
  873. nkv = kv->next;
  874. if (kv->prev)
  875. kv->prev->next = NULL;
  876. if (kv->next)
  877. kv->next->prev = NULL;
  878. kv->prev = NULL;
  879. kv->next = NULL;
  880. }
  881. }
  882. int csr1212_generate_csr_image(struct csr1212_csr *csr)
  883. {
  884. struct csr1212_bus_info_block_img *bi;
  885. struct csr1212_csr_rom_cache *cache;
  886. struct csr1212_keyval *kv;
  887. size_t agg_size;
  888. int ret;
  889. int init_offset;
  890. if (!csr)
  891. return CSR1212_EINVAL;
  892. cache = csr->cache_head;
  893. bi = (struct csr1212_bus_info_block_img*)cache->data;
  894. bi->length = bytes_to_quads(csr->bus_info_len) - 1;
  895. bi->crc_length = bi->length;
  896. bi->crc = csr1212_crc16(bi->data, bi->crc_length);
  897. csr->root_kv->next = NULL;
  898. csr->root_kv->prev = NULL;
  899. agg_size = csr1212_generate_layout_order(csr->root_kv);
  900. init_offset = csr->bus_info_len;
  901. for (kv = csr->root_kv, cache = csr->cache_head; kv; cache = cache->next) {
  902. if (!cache) {
  903. /* Estimate approximate number of additional cache
  904. * regions needed (it assumes that the cache holding
  905. * the first 1K Config ROM space always exists). */
  906. int est_c = agg_size / (CSR1212_EXTENDED_ROM_SIZE -
  907. (2 * sizeof(u_int32_t))) + 1;
  908. /* Add additional cache regions, extras will be
  909. * removed later */
  910. for (; est_c; est_c--) {
  911. ret = csr1212_append_new_cache(csr, CSR1212_EXTENDED_ROM_SIZE);
  912. if (ret != CSR1212_SUCCESS)
  913. return ret;
  914. }
  915. /* Need to re-layout for additional cache regions */
  916. agg_size = csr1212_generate_layout_order(csr->root_kv);
  917. kv = csr->root_kv;
  918. cache = csr->cache_head;
  919. init_offset = csr->bus_info_len;
  920. }
  921. kv = csr1212_generate_positions(cache, kv, init_offset);
  922. agg_size -= cache->len;
  923. init_offset = sizeof(u_int32_t);
  924. }
  925. /* Remove unused, excess cache regions */
  926. while (cache) {
  927. struct csr1212_csr_rom_cache *oc = cache;
  928. cache = cache->next;
  929. csr1212_remove_cache(csr, oc);
  930. }
  931. /* Go through the list backward so that when done, the correct CRC
  932. * will be calculated for the Extended ROM areas. */
  933. for(cache = csr->cache_tail; cache; cache = cache->prev) {
  934. /* Only Extended ROM caches should have this set. */
  935. if (cache->ext_rom) {
  936. int leaf_size;
  937. /* Make sure the Extended ROM leaf is a multiple of
  938. * max_rom in size. */
  939. leaf_size = (cache->len + (csr->max_rom - 1)) &
  940. ~(csr->max_rom - 1);
  941. /* Zero out the unused ROM region */
  942. memset(cache->data + bytes_to_quads(cache->len), 0x00,
  943. leaf_size - cache->len);
  944. /* Subtract leaf header */
  945. leaf_size -= sizeof(u_int32_t);
  946. /* Update the Extended ROM leaf length */
  947. cache->ext_rom->value.leaf.len =
  948. bytes_to_quads(leaf_size);
  949. } else {
  950. /* Zero out the unused ROM region */
  951. memset(cache->data + bytes_to_quads(cache->len), 0x00,
  952. cache->size - cache->len);
  953. }
  954. /* Copy the data into the cache buffer */
  955. csr1212_fill_cache(cache);
  956. if (cache != csr->cache_head) {
  957. /* Set the length and CRC of the extended ROM. */
  958. struct csr1212_keyval_img *kvi =
  959. (struct csr1212_keyval_img*)cache->data;
  960. kvi->length = CSR1212_CPU_TO_BE16(bytes_to_quads(cache->len) - 1);
  961. kvi->crc = csr1212_crc16(kvi->data,
  962. bytes_to_quads(cache->len) - 1);
  963. }
  964. }
  965. return CSR1212_SUCCESS;
  966. }
  967. int csr1212_read(struct csr1212_csr *csr, u_int32_t offset, void *buffer, u_int32_t len)
  968. {
  969. struct csr1212_csr_rom_cache *cache;
  970. for (cache = csr->cache_head; cache; cache = cache->next) {
  971. if (offset >= cache->offset &&
  972. (offset + len) <= (cache->offset + cache->size)) {
  973. memcpy(buffer,
  974. &cache->data[bytes_to_quads(offset - cache->offset)],
  975. len);
  976. return CSR1212_SUCCESS;
  977. }
  978. }
  979. return CSR1212_ENOENT;
  980. }
  981. /* Parse a chunk of data as a Config ROM */
  982. static int csr1212_parse_bus_info_block(struct csr1212_csr *csr)
  983. {
  984. struct csr1212_bus_info_block_img *bi;
  985. struct csr1212_cache_region *cr;
  986. int i;
  987. int ret;
  988. /* IEEE 1212 says that the entire bus info block should be readable in
  989. * a single transaction regardless of the max_rom value.
  990. * Unfortunately, many IEEE 1394 devices do not abide by that, so the
  991. * bus info block will be read 1 quadlet at a time. The rest of the
  992. * ConfigROM will be read according to the max_rom field. */
  993. for (i = 0; i < csr->bus_info_len; i += sizeof(csr1212_quad_t)) {
  994. ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
  995. sizeof(csr1212_quad_t),
  996. &csr->cache_head->data[bytes_to_quads(i)],
  997. csr->private);
  998. if (ret != CSR1212_SUCCESS)
  999. return ret;
  1000. }
  1001. bi = (struct csr1212_bus_info_block_img*)csr->cache_head->data;
  1002. csr->crc_len = quads_to_bytes(bi->crc_length);
  1003. /* IEEE 1212 recommends that crc_len be equal to bus_info_len, but that is not
  1004. * always the case, so read the rest of the crc area 1 quadlet at a time. */
  1005. for (i = csr->bus_info_len; i <= csr->crc_len; i += sizeof(csr1212_quad_t)) {
  1006. ret = csr->ops->bus_read(csr, CSR1212_CONFIG_ROM_SPACE_BASE + i,
  1007. sizeof(csr1212_quad_t),
  1008. &csr->cache_head->data[bytes_to_quads(i)],
  1009. csr->private);
  1010. if (ret != CSR1212_SUCCESS)
  1011. return ret;
  1012. }
  1013. if (bytes_to_quads(csr->bus_info_len - sizeof(csr1212_quad_t)) != bi->length)
  1014. return CSR1212_EINVAL;
  1015. #if 0
  1016. /* Apparently there are too many differnt wrong implementations of the
  1017. * CRC algorithm that verifying them is moot. */
  1018. if ((csr1212_crc16(bi->data, bi->crc_length) != bi->crc) &&
  1019. (csr1212_msft_crc16(bi->data, bi->crc_length) != bi->crc))
  1020. return CSR1212_EINVAL;
  1021. #endif
  1022. cr = CSR1212_MALLOC(sizeof(struct csr1212_cache_region));
  1023. if (!cr)
  1024. return CSR1212_ENOMEM;
  1025. cr->next = NULL;
  1026. cr->prev = NULL;
  1027. cr->offset_start = 0;
  1028. cr->offset_end = csr->crc_len + 4;
  1029. csr->cache_head->filled_head = cr;
  1030. csr->cache_head->filled_tail = cr;
  1031. return CSR1212_SUCCESS;
  1032. }
  1033. static int csr1212_parse_dir_entry(struct csr1212_keyval *dir,
  1034. csr1212_quad_t ki,
  1035. u_int32_t kv_pos)
  1036. {
  1037. int ret = CSR1212_SUCCESS;
  1038. struct csr1212_keyval *k = NULL;
  1039. u_int32_t offset;
  1040. switch(CSR1212_KV_KEY_TYPE(ki)) {
  1041. case CSR1212_KV_TYPE_IMMEDIATE:
  1042. k = csr1212_new_immediate(CSR1212_KV_KEY_ID(ki),
  1043. CSR1212_KV_VAL(ki));
  1044. if (!k) {
  1045. ret = CSR1212_ENOMEM;
  1046. goto fail;
  1047. }
  1048. k->refcnt = 0; /* Don't keep local reference when parsing. */
  1049. break;
  1050. case CSR1212_KV_TYPE_CSR_OFFSET:
  1051. k = csr1212_new_csr_offset(CSR1212_KV_KEY_ID(ki),
  1052. CSR1212_KV_VAL(ki));
  1053. if (!k) {
  1054. ret = CSR1212_ENOMEM;
  1055. goto fail;
  1056. }
  1057. k->refcnt = 0; /* Don't keep local reference when parsing. */
  1058. break;
  1059. default:
  1060. /* Compute the offset from 0xffff f000 0000. */
  1061. offset = quads_to_bytes(CSR1212_KV_VAL(ki)) + kv_pos;
  1062. if (offset == kv_pos) {
  1063. /* Uh-oh. Can't have a relative offset of 0 for Leaves
  1064. * or Directories. The Config ROM image is most likely
  1065. * messed up, so we'll just abort here. */
  1066. ret = CSR1212_EIO;
  1067. goto fail;
  1068. }
  1069. k = csr1212_find_keyval_offset(dir, offset);
  1070. if (k)
  1071. break; /* Found it. */
  1072. if (CSR1212_KV_KEY_TYPE(ki) == CSR1212_KV_TYPE_DIRECTORY) {
  1073. k = csr1212_new_directory(CSR1212_KV_KEY_ID(ki));
  1074. } else {
  1075. k = csr1212_new_leaf(CSR1212_KV_KEY_ID(ki), NULL, 0);
  1076. }
  1077. if (!k) {
  1078. ret = CSR1212_ENOMEM;
  1079. goto fail;
  1080. }
  1081. k->refcnt = 0; /* Don't keep local reference when parsing. */
  1082. k->valid = 0; /* Contents not read yet so it's not valid. */
  1083. k->offset = offset;
  1084. k->prev = dir;
  1085. k->next = dir->next;
  1086. dir->next->prev = k;
  1087. dir->next = k;
  1088. }
  1089. ret = csr1212_attach_keyval_to_directory(dir, k);
  1090. fail:
  1091. if (ret != CSR1212_SUCCESS) {
  1092. if (k)
  1093. free_keyval(k);
  1094. }
  1095. return ret;
  1096. }
  1097. int csr1212_parse_keyval(struct csr1212_keyval *kv,
  1098. struct csr1212_csr_rom_cache *cache)
  1099. {
  1100. struct csr1212_keyval_img *kvi;
  1101. int i;
  1102. int ret = CSR1212_SUCCESS;
  1103. int kvi_len;
  1104. kvi = (struct csr1212_keyval_img*)&cache->data[bytes_to_quads(kv->offset -
  1105. cache->offset)];
  1106. kvi_len = CSR1212_BE16_TO_CPU(kvi->length);
  1107. #if 0
  1108. /* Apparently there are too many differnt wrong implementations of the
  1109. * CRC algorithm that verifying them is moot. */
  1110. if ((csr1212_crc16(kvi->data, kvi_len) != kvi->crc) &&
  1111. (csr1212_msft_crc16(kvi->data, kvi_len) != kvi->crc)) {
  1112. ret = CSR1212_EINVAL;
  1113. goto fail;
  1114. }
  1115. #endif
  1116. switch(kv->key.type) {
  1117. case CSR1212_KV_TYPE_DIRECTORY:
  1118. for (i = 0; i < kvi_len; i++) {
  1119. csr1212_quad_t ki = kvi->data[i];
  1120. /* Some devices put null entries in their unit
  1121. * directories. If we come across such an entry,
  1122. * then skip it. */
  1123. if (ki == 0x0)
  1124. continue;
  1125. ret = csr1212_parse_dir_entry(kv, ki,
  1126. (kv->offset +
  1127. quads_to_bytes(i + 1)));
  1128. }
  1129. kv->value.directory.len = kvi_len;
  1130. break;
  1131. case CSR1212_KV_TYPE_LEAF:
  1132. if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM) {
  1133. kv->value.leaf.data = CSR1212_MALLOC(quads_to_bytes(kvi_len));
  1134. if (!kv->value.leaf.data)
  1135. {
  1136. ret = CSR1212_ENOMEM;
  1137. goto fail;
  1138. }
  1139. kv->value.leaf.len = kvi_len;
  1140. memcpy(kv->value.leaf.data, kvi->data, quads_to_bytes(kvi_len));
  1141. }
  1142. break;
  1143. }
  1144. kv->valid = 1;
  1145. fail:
  1146. return ret;
  1147. }
  1148. int _csr1212_read_keyval(struct csr1212_csr *csr, struct csr1212_keyval *kv)
  1149. {
  1150. struct csr1212_cache_region *cr, *ncr, *newcr = NULL;
  1151. struct csr1212_keyval_img *kvi = NULL;
  1152. struct csr1212_csr_rom_cache *cache;
  1153. int cache_index;
  1154. u_int64_t addr;
  1155. u_int32_t *cache_ptr;
  1156. u_int16_t kv_len = 0;
  1157. if (!csr || !kv)
  1158. return CSR1212_EINVAL;
  1159. /* First find which cache the data should be in (or go in if not read
  1160. * yet). */
  1161. for (cache = csr->cache_head; cache; cache = cache->next) {
  1162. if (kv->offset >= cache->offset &&
  1163. kv->offset < (cache->offset + cache->size))
  1164. break;
  1165. }
  1166. if (!cache) {
  1167. csr1212_quad_t q;
  1168. u_int32_t cache_size;
  1169. /* Only create a new cache for Extended ROM leaves. */
  1170. if (kv->key.id != CSR1212_KV_ID_EXTENDED_ROM)
  1171. return CSR1212_EINVAL;
  1172. if (csr->ops->bus_read(csr,
  1173. CSR1212_REGISTER_SPACE_BASE + kv->offset,
  1174. sizeof(csr1212_quad_t), &q, csr->private)) {
  1175. return CSR1212_EIO;
  1176. }
  1177. kv->value.leaf.len = CSR1212_BE32_TO_CPU(q) >> 16;
  1178. cache_size = (quads_to_bytes(kv->value.leaf.len + 1) +
  1179. (csr->max_rom - 1)) & ~(csr->max_rom - 1);
  1180. cache = csr1212_rom_cache_malloc(kv->offset, cache_size);
  1181. if (!cache)
  1182. return CSR1212_ENOMEM;
  1183. kv->value.leaf.data = &cache->data[1];
  1184. csr->cache_tail->next = cache;
  1185. cache->prev = csr->cache_tail;
  1186. cache->next = NULL;
  1187. csr->cache_tail = cache;
  1188. cache->filled_head =
  1189. CSR1212_MALLOC(sizeof(struct csr1212_cache_region));
  1190. if (!cache->filled_head) {
  1191. return CSR1212_ENOMEM;
  1192. }
  1193. cache->filled_head->offset_start = 0;
  1194. cache->filled_head->offset_end = sizeof(csr1212_quad_t);
  1195. cache->filled_tail = cache->filled_head;
  1196. cache->filled_head->next = NULL;
  1197. cache->filled_head->prev = NULL;
  1198. cache->data[0] = q;
  1199. /* Don't read the entire extended ROM now. Pieces of it will
  1200. * be read when entries inside it are read. */
  1201. return csr1212_parse_keyval(kv, cache);
  1202. }
  1203. cache_index = kv->offset - cache->offset;
  1204. /* Now seach read portions of the cache to see if it is there. */
  1205. for (cr = cache->filled_head; cr; cr = cr->next) {
  1206. if (cache_index < cr->offset_start) {
  1207. newcr = CSR1212_MALLOC(sizeof(struct csr1212_cache_region));
  1208. if (!newcr)
  1209. return CSR1212_ENOMEM;
  1210. newcr->offset_start = cache_index & ~(csr->max_rom - 1);
  1211. newcr->offset_end = newcr->offset_start;
  1212. newcr->next = cr;
  1213. newcr->prev = cr->prev;
  1214. cr->prev = newcr;
  1215. cr = newcr;
  1216. break;
  1217. } else if ((cache_index >= cr->offset_start) &&
  1218. (cache_index < cr->offset_end)) {
  1219. kvi = (struct csr1212_keyval_img*)
  1220. (&cache->data[bytes_to_quads(cache_index)]);
  1221. kv_len = quads_to_bytes(CSR1212_BE16_TO_CPU(kvi->length) +
  1222. 1);
  1223. break;
  1224. } else if (cache_index == cr->offset_end)
  1225. break;
  1226. }
  1227. if (!cr) {
  1228. cr = cache->filled_tail;
  1229. newcr = CSR1212_MALLOC(sizeof(struct csr1212_cache_region));
  1230. if (!newcr)
  1231. return CSR1212_ENOMEM;
  1232. newcr->offset_start = cache_index & ~(csr->max_rom - 1);
  1233. newcr->offset_end = newcr->offset_start;
  1234. newcr->prev = cr;
  1235. newcr->next = cr->next;
  1236. cr->next = newcr;
  1237. cr = newcr;
  1238. cache->filled_tail = newcr;
  1239. }
  1240. while(!kvi || cr->offset_end < cache_index + kv_len) {
  1241. cache_ptr = &cache->data[bytes_to_quads(cr->offset_end &
  1242. ~(csr->max_rom - 1))];
  1243. addr = (CSR1212_CSR_ARCH_REG_SPACE_BASE + cache->offset +
  1244. cr->offset_end) & ~(csr->max_rom - 1);
  1245. if (csr->ops->bus_read(csr, addr, csr->max_rom, cache_ptr,
  1246. csr->private)) {
  1247. if (csr->max_rom == 4)
  1248. /* We've got problems! */
  1249. return CSR1212_EIO;
  1250. /* Apperently the max_rom value was a lie, set it to
  1251. * do quadlet reads and try again. */
  1252. csr->max_rom = 4;
  1253. continue;
  1254. }
  1255. cr->offset_end += csr->max_rom - (cr->offset_end &
  1256. (csr->max_rom - 1));
  1257. if (!kvi && (cr->offset_end > cache_index)) {
  1258. kvi = (struct csr1212_keyval_img*)
  1259. (&cache->data[bytes_to_quads(cache_index)]);
  1260. kv_len = quads_to_bytes(CSR1212_BE16_TO_CPU(kvi->length) +
  1261. 1);
  1262. }
  1263. if ((kv_len + (kv->offset - cache->offset)) > cache->size) {
  1264. /* The Leaf or Directory claims its length extends
  1265. * beyond the ConfigROM image region and thus beyond the
  1266. * end of our cache region. Therefore, we abort now
  1267. * rather than seg faulting later. */
  1268. return CSR1212_EIO;
  1269. }
  1270. ncr = cr->next;
  1271. if (ncr && (cr->offset_end >= ncr->offset_start)) {
  1272. /* consolidate region entries */
  1273. ncr->offset_start = cr->offset_start;
  1274. if (cr->prev)
  1275. cr->prev->next = cr->next;
  1276. ncr->prev = cr->prev;
  1277. if (cache->filled_head == cr)
  1278. cache->filled_head = ncr;
  1279. CSR1212_FREE(cr);
  1280. cr = ncr;
  1281. }
  1282. }
  1283. return csr1212_parse_keyval(kv, cache);
  1284. }
  1285. int csr1212_parse_csr(struct csr1212_csr *csr)
  1286. {
  1287. static const int mr_map[] = { 4, 64, 1024, 0 };
  1288. struct csr1212_dentry *dentry;
  1289. int ret;
  1290. if (!csr || !csr->ops->bus_read)
  1291. return CSR1212_EINVAL;
  1292. ret = csr1212_parse_bus_info_block(csr);
  1293. if (ret != CSR1212_SUCCESS)
  1294. return ret;
  1295. if (!csr->ops->get_max_rom)
  1296. csr->max_rom = mr_map[0]; /* default value */
  1297. else
  1298. csr->max_rom = mr_map[csr->ops->get_max_rom(csr->bus_info_data,
  1299. csr->private)];
  1300. csr->cache_head->layout_head = csr->root_kv;
  1301. csr->cache_head->layout_tail = csr->root_kv;
  1302. csr->root_kv->offset = (CSR1212_CONFIG_ROM_SPACE_BASE & 0xffff) +
  1303. csr->bus_info_len;
  1304. csr->root_kv->valid = 0;
  1305. csr->root_kv->next = csr->root_kv;
  1306. csr->root_kv->prev = csr->root_kv;
  1307. csr1212_get_keyval(csr, csr->root_kv);
  1308. /* Scan through the Root directory finding all extended ROM regions
  1309. * and make cache regions for them */
  1310. for (dentry = csr->root_kv->value.directory.dentries_head;
  1311. dentry; dentry = dentry->next) {
  1312. if (dentry->kv->key.id == CSR1212_KV_ID_EXTENDED_ROM) {
  1313. csr1212_get_keyval(csr, dentry->kv);
  1314. if (ret != CSR1212_SUCCESS)
  1315. return ret;
  1316. }
  1317. }
  1318. return CSR1212_SUCCESS;
  1319. }