csr1212.c 42 KB

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