skvpd.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  1. /******************************************************************************
  2. *
  3. * Name: skvpd.c
  4. * Project: GEnesis, PCI Gigabit Ethernet Adapter
  5. * Version: $Revision: 1.37 $
  6. * Date: $Date: 2003/01/13 10:42:45 $
  7. * Purpose: Shared software to read and write VPD data
  8. *
  9. ******************************************************************************/
  10. /******************************************************************************
  11. *
  12. * (C)Copyright 1998-2003 SysKonnect GmbH.
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * The information in this file is provided "AS IS" without warranty.
  20. *
  21. ******************************************************************************/
  22. /******************************************************************************
  23. *
  24. * History:
  25. *
  26. * $Log: skvpd.c,v $
  27. * Revision 1.37 2003/01/13 10:42:45 rschmidt
  28. * Replaced check for PCI device Id from YUKON with GENESIS
  29. * to set the VPD size in VpdInit()
  30. * Editorial changes
  31. *
  32. * Revision 1.36 2002/11/14 15:16:56 gheinig
  33. * Added const specifier to key and buf parameters for VpdPara, VpdRead
  34. * and VpdWrite for Diag 7 GUI
  35. *
  36. * Revision 1.35 2002/10/21 14:31:59 gheinig
  37. * Took out CVS web garbage at head of file
  38. *
  39. * Revision 1.34 2002/10/21 11:47:24 gheinig
  40. * Reverted to version 1.32 due to unwanted commit
  41. *
  42. * Revision 1.32 2002/10/14 16:04:29 rschmidt
  43. * Added saving of VPD ROM Size from PCI_OUR_REG_2
  44. * Avoid reading of PCI_OUR_REG_2 in VpdTransferBlock()
  45. * Editorial changes
  46. *
  47. * Revision 1.31 2002/09/10 09:21:32 mkarl
  48. * Replaced all if(GIChipId == CHIP_ID_GENESIS) with new entry GIGenesis
  49. *
  50. * Revision 1.30 2002/09/09 14:43:03 mkarl
  51. * changes for diagnostics in order to read VPD data before the adapter
  52. * has been initialized
  53. * editorial changes
  54. *
  55. * Revision 1.29 2002/07/26 13:20:43 mkarl
  56. * added Yukon support
  57. * save size of VPD in pAC->vpd.vpd_size
  58. *
  59. * Revision 1.28 2002/04/02 15:31:47 afischer
  60. * Bug fix in VpdWait()
  61. *
  62. * Revision 1.27 2000/08/10 11:29:06 rassmann
  63. * Editorial changes.
  64. * Preserving 32-bit alignment in structs for the adapter context.
  65. * Removed unused function VpdWriteDword() (#if 0).
  66. * Made VpdReadKeyword() available for SKDIAG only.
  67. *
  68. * Revision 1.26 2000/06/13 08:00:01 mkarl
  69. * additional cast to avoid compile problems in 64 bit environment
  70. *
  71. * Revision 1.25 1999/11/22 13:39:32 cgoos
  72. * Changed license header to GPL.
  73. *
  74. * Revision 1.24 1999/03/11 14:25:49 malthoff
  75. * Replace __STDC__ with SK_KR_PROTO.
  76. *
  77. * Revision 1.23 1999/01/11 15:13:11 gklug
  78. * fix: syntax error
  79. *
  80. * Revision 1.22 1998/10/30 06:41:15 gklug
  81. * rmv: WARNING
  82. *
  83. * Revision 1.21 1998/10/29 07:15:14 gklug
  84. * fix: Write Stream function needs verify.
  85. *
  86. * Revision 1.20 1998/10/28 18:05:08 gklug
  87. * chg: no DEBUG in VpdMayWrite
  88. *
  89. * Revision 1.19 1998/10/28 15:56:11 gklug
  90. * fix: Return len at end of ReadStream
  91. * fix: Write even less than 4 bytes correctly
  92. *
  93. * Revision 1.18 1998/10/28 09:00:47 gklug
  94. * fix: unreferenced local vars
  95. *
  96. * Revision 1.17 1998/10/28 08:25:45 gklug
  97. * fix: WARNING
  98. *
  99. * Revision 1.16 1998/10/28 08:17:30 gklug
  100. * fix: typo
  101. *
  102. * Revision 1.15 1998/10/28 07:50:32 gklug
  103. * fix: typo
  104. *
  105. * Revision 1.14 1998/10/28 07:20:38 gklug
  106. * chg: Interface functions to use IoC as parameter as well
  107. * fix: VpdRead/WriteDWord now returns SK_U32
  108. * chg: VPD_IN/OUT names conform to SK_IN/OUT
  109. * add: usage of VPD_IN/OUT8 macros
  110. * add: VpdRead/Write Stream functions to r/w a stream of data
  111. * fix: VpdTransferBlock swapped illegal
  112. * add: VpdMayWrite
  113. *
  114. * Revision 1.13 1998/10/22 10:02:37 gklug
  115. * fix: SysKonnectFileId typo
  116. *
  117. * Revision 1.12 1998/10/20 10:01:01 gklug
  118. * fix: parameter to SkOsGetTime
  119. *
  120. * Revision 1.11 1998/10/15 12:51:48 malthoff
  121. * Remove unrequired parameter p in vpd_setup_para().
  122. *
  123. * Revision 1.10 1998/10/08 14:52:43 malthoff
  124. * Remove CvsId by SysKonnectFileId.
  125. *
  126. * Revision 1.9 1998/09/16 07:33:52 malthoff
  127. * replace memcmp() by SK_MEMCMP and
  128. * memcpy() by SK_MEMCPY() to be
  129. * independent from the 'C' Standard Library.
  130. *
  131. * Revision 1.8 1998/08/19 12:52:35 malthoff
  132. * compiler fix: use SK_VPD_KEY instead of S_VPD.
  133. *
  134. * Revision 1.7 1998/08/19 08:14:01 gklug
  135. * fix: remove struct keyword as much as possible from the C-code (see CCC)
  136. *
  137. * Revision 1.6 1998/08/18 13:03:58 gklug
  138. * SkOsGetTime now returns SK_U64
  139. *
  140. * Revision 1.5 1998/08/18 08:17:29 malthoff
  141. * Ensure we issue a VPD read in vpd_read_dword().
  142. * Discard all VPD keywords other than Vx or Yx, where
  143. * x is '0..9' or 'A..Z'.
  144. *
  145. * Revision 1.4 1998/07/03 14:52:19 malthoff
  146. * Add category SK_DBGCAT_FATAL to some debug macros.
  147. * bug fix: correct the keyword name check in vpd_write().
  148. *
  149. * Revision 1.3 1998/06/26 11:16:53 malthoff
  150. * Correct the modified File Identifier.
  151. *
  152. * Revision 1.2 1998/06/26 11:13:43 malthoff
  153. * Modify the File Identifier.
  154. *
  155. * Revision 1.1 1998/06/19 14:11:08 malthoff
  156. * Created, Tests with AIX were performed successfully
  157. *
  158. *
  159. ******************************************************************************/
  160. #include <config.h>
  161. #ifdef CONFIG_SK98
  162. /*
  163. Please refer skvpd.txt for infomation how to include this module
  164. */
  165. static const char SysKonnectFileId[] =
  166. "@(#)$Id: skvpd.c,v 1.37 2003/01/13 10:42:45 rschmidt Exp $ (C) SK";
  167. #include "h/skdrv1st.h"
  168. #include "h/sktypes.h"
  169. #include "h/skdebug.h"
  170. #include "h/skdrv2nd.h"
  171. /*
  172. * Static functions
  173. */
  174. #ifndef SK_KR_PROTO
  175. static SK_VPD_PARA *vpd_find_para(
  176. SK_AC *pAC,
  177. const char *key,
  178. SK_VPD_PARA *p);
  179. #else /* SK_KR_PROTO */
  180. static SK_VPD_PARA *vpd_find_para();
  181. #endif /* SK_KR_PROTO */
  182. /*
  183. * waits for a completion of a VPD transfer
  184. * The VPD transfer must complete within SK_TICKS_PER_SEC/16
  185. *
  186. * returns 0: success, transfer completes
  187. * error exit(9) with a error message
  188. */
  189. static int VpdWait(
  190. SK_AC *pAC, /* Adapters context */
  191. SK_IOC IoC, /* IO Context */
  192. int event) /* event to wait for (VPD_READ / VPD_write) completion*/
  193. {
  194. SK_U64 start_time;
  195. SK_U16 state;
  196. SK_DBG_MSG(pAC,SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  197. ("VPD wait for %s\n", event?"Write":"Read"));
  198. start_time = SkOsGetTime(pAC);
  199. do {
  200. if (SkOsGetTime(pAC) - start_time > SK_TICKS_PER_SEC) {
  201. /* Bug fix AF: Thu Mar 28 2002
  202. * Do not call: VPD_STOP(pAC, IoC);
  203. * A pending VPD read cycle can not be aborted by writing
  204. * VPD_WRITE to the PCI_VPD_ADR_REG (VPD address register).
  205. * Although the write threshold in the OUR-register protects
  206. * VPD read only space from being overwritten this does not
  207. * protect a VPD read from being `converted` into a VPD write
  208. * operation (on the fly). As a consequence the VPD_STOP would
  209. * delete VPD read only data. In case of any problems with the
  210. * I2C bus we exit the loop here. The I2C read operation can
  211. * not be aborted except by a reset (->LR).
  212. */
  213. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_FATAL | SK_DBGCAT_ERR,
  214. ("ERROR:VPD wait timeout\n"));
  215. return(1);
  216. }
  217. VPD_IN16(pAC, IoC, PCI_VPD_ADR_REG, &state);
  218. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  219. ("state = %x, event %x\n",state,event));
  220. } while((int)(state & PCI_VPD_FLAG) == event);
  221. return(0);
  222. }
  223. #ifdef SKDIAG
  224. /*
  225. * Read the dword at address 'addr' from the VPD EEPROM.
  226. *
  227. * Needed Time: MIN 1,3 ms MAX 2,6 ms
  228. *
  229. * Note: The DWord is returned in the endianess of the machine the routine
  230. * is running on.
  231. *
  232. * Returns the data read.
  233. */
  234. SK_U32 VpdReadDWord(
  235. SK_AC *pAC, /* Adapters context */
  236. SK_IOC IoC, /* IO Context */
  237. int addr) /* VPD address */
  238. {
  239. SK_U32 Rtv;
  240. /* start VPD read */
  241. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  242. ("VPD read dword at 0x%x\n",addr));
  243. addr &= ~VPD_WRITE; /* ensure the R/W bit is set to read */
  244. VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, (SK_U16)addr);
  245. /* ignore return code here */
  246. (void)VpdWait(pAC, IoC, VPD_READ);
  247. /* Don't swap here, it's a data stream of bytes */
  248. Rtv = 0;
  249. VPD_IN32(pAC, IoC, PCI_VPD_DAT_REG, &Rtv);
  250. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  251. ("VPD read dword data = 0x%x\n",Rtv));
  252. return(Rtv);
  253. }
  254. #endif /* SKDIAG */
  255. #if 0
  256. /*
  257. Write the dword 'data' at address 'addr' into the VPD EEPROM, and
  258. verify that the data is written.
  259. Needed Time:
  260. . MIN MAX
  261. . -------------------------------------------------------------------
  262. . write 1.8 ms 3.6 ms
  263. . internal write cyles 0.7 ms 7.0 ms
  264. . -------------------------------------------------------------------
  265. . over all program time 2.5 ms 10.6 ms
  266. . read 1.3 ms 2.6 ms
  267. . -------------------------------------------------------------------
  268. . over all 3.8 ms 13.2 ms
  269. .
  270. Returns 0: success
  271. 1: error, I2C transfer does not terminate
  272. 2: error, data verify error
  273. */
  274. static int VpdWriteDWord(
  275. SK_AC *pAC, /* pAC pointer */
  276. SK_IOC IoC, /* IO Context */
  277. int addr, /* VPD address */
  278. SK_U32 data) /* VPD data to write */
  279. {
  280. /* start VPD write */
  281. /* Don't swap here, it's a data stream of bytes */
  282. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  283. ("VPD write dword at addr 0x%x, data = 0x%x\n",addr,data));
  284. VPD_OUT32(pAC, IoC, PCI_VPD_DAT_REG, (SK_U32)data);
  285. /* But do it here */
  286. addr |= VPD_WRITE;
  287. VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, (SK_U16)(addr | VPD_WRITE));
  288. /* this may take up to 10,6 ms */
  289. if (VpdWait(pAC, IoC, VPD_WRITE)) {
  290. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  291. ("Write Timed Out\n"));
  292. return(1);
  293. };
  294. /* verify data */
  295. if (VpdReadDWord(pAC, IoC, addr) != data) {
  296. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  297. ("Data Verify Error\n"));
  298. return(2);
  299. }
  300. return(0);
  301. } /* VpdWriteDWord */
  302. #endif /* 0 */
  303. /*
  304. * Read one Stream of 'len' bytes of VPD data, starting at 'addr' from
  305. * or to the I2C EEPROM.
  306. *
  307. * Returns number of bytes read / written.
  308. */
  309. static int VpdWriteStream(
  310. SK_AC *pAC, /* Adapters context */
  311. SK_IOC IoC, /* IO Context */
  312. char *buf, /* data buffer */
  313. int Addr, /* VPD start address */
  314. int Len) /* number of bytes to read / to write */
  315. {
  316. int i;
  317. int j;
  318. SK_U16 AdrReg;
  319. int Rtv;
  320. SK_U8 * pComp; /* Compare pointer */
  321. SK_U8 Data; /* Input Data for Compare */
  322. /* Init Compare Pointer */
  323. pComp = (SK_U8 *) buf;
  324. for (i = 0; i < Len; i++, buf++) {
  325. if ((i%sizeof(SK_U32)) == 0) {
  326. /*
  327. * At the begin of each cycle read the Data Reg
  328. * So it is initialized even if only a few bytes
  329. * are written.
  330. */
  331. AdrReg = (SK_U16) Addr;
  332. AdrReg &= ~VPD_WRITE; /* READ operation */
  333. VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, AdrReg);
  334. /* Wait for termination */
  335. Rtv = VpdWait(pAC, IoC, VPD_READ);
  336. if (Rtv != 0) {
  337. return(i);
  338. }
  339. }
  340. /* Write current Byte */
  341. VPD_OUT8(pAC, IoC, PCI_VPD_DAT_REG + (i%sizeof(SK_U32)),
  342. *(SK_U8*)buf);
  343. if (((i%sizeof(SK_U32)) == 3) || (i == (Len - 1))) {
  344. /* New Address needs to be written to VPD_ADDR reg */
  345. AdrReg = (SK_U16) Addr;
  346. Addr += sizeof(SK_U32);
  347. AdrReg |= VPD_WRITE; /* WRITE operation */
  348. VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, AdrReg);
  349. /* Wait for termination */
  350. Rtv = VpdWait(pAC, IoC, VPD_WRITE);
  351. if (Rtv != 0) {
  352. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  353. ("Write Timed Out\n"));
  354. return(i - (i%sizeof(SK_U32)));
  355. }
  356. /*
  357. * Now re-read to verify
  358. */
  359. AdrReg &= ~VPD_WRITE; /* READ operation */
  360. VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, AdrReg);
  361. /* Wait for termination */
  362. Rtv = VpdWait(pAC, IoC, VPD_READ);
  363. if (Rtv != 0) {
  364. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  365. ("Verify Timed Out\n"));
  366. return(i - (i%sizeof(SK_U32)));
  367. }
  368. for (j = 0; j <= (int)(i%sizeof(SK_U32)); j++, pComp++) {
  369. VPD_IN8(pAC, IoC, PCI_VPD_DAT_REG + j, &Data);
  370. if (Data != *pComp) {
  371. /* Verify Error */
  372. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  373. ("WriteStream Verify Error\n"));
  374. return(i - (i%sizeof(SK_U32)) + j);
  375. }
  376. }
  377. }
  378. }
  379. return(Len);
  380. }
  381. /*
  382. * Read one Stream of 'len' bytes of VPD data, starting at 'addr' from
  383. * or to the I2C EEPROM.
  384. *
  385. * Returns number of bytes read / written.
  386. */
  387. static int VpdReadStream(
  388. SK_AC *pAC, /* Adapters context */
  389. SK_IOC IoC, /* IO Context */
  390. char *buf, /* data buffer */
  391. int Addr, /* VPD start address */
  392. int Len) /* number of bytes to read / to write */
  393. {
  394. int i;
  395. SK_U16 AdrReg;
  396. int Rtv;
  397. for (i = 0; i < Len; i++, buf++) {
  398. if ((i%sizeof(SK_U32)) == 0) {
  399. /* New Address needs to be written to VPD_ADDR reg */
  400. AdrReg = (SK_U16) Addr;
  401. Addr += sizeof(SK_U32);
  402. AdrReg &= ~VPD_WRITE; /* READ operation */
  403. VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, AdrReg);
  404. /* Wait for termination */
  405. Rtv = VpdWait(pAC, IoC, VPD_READ);
  406. if (Rtv != 0) {
  407. return(i);
  408. }
  409. }
  410. VPD_IN8(pAC, IoC, PCI_VPD_DAT_REG + (i%sizeof(SK_U32)),
  411. (SK_U8 *)buf);
  412. }
  413. return(Len);
  414. }
  415. /*
  416. * Read ore writes 'len' bytes of VPD data, starting at 'addr' from
  417. * or to the I2C EEPROM.
  418. *
  419. * Returns number of bytes read / written.
  420. */
  421. static int VpdTransferBlock(
  422. SK_AC *pAC, /* Adapters context */
  423. SK_IOC IoC, /* IO Context */
  424. char *buf, /* data buffer */
  425. int addr, /* VPD start address */
  426. int len, /* number of bytes to read / to write */
  427. int dir) /* transfer direction may be VPD_READ or VPD_WRITE */
  428. {
  429. int Rtv; /* Return value */
  430. int vpd_rom_size;
  431. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  432. ("VPD %s block, addr = 0x%x, len = %d\n",
  433. dir ? "write" : "read", addr, len));
  434. if (len == 0)
  435. return(0);
  436. vpd_rom_size = pAC->vpd.rom_size;
  437. if (addr > vpd_rom_size - 4) {
  438. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  439. ("Address error: 0x%x, exp. < 0x%x\n",
  440. addr, vpd_rom_size - 4));
  441. return(0);
  442. }
  443. if (addr + len > vpd_rom_size) {
  444. len = vpd_rom_size - addr;
  445. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  446. ("Warning: len was cut to %d\n", len));
  447. }
  448. if (dir == VPD_READ) {
  449. Rtv = VpdReadStream(pAC, IoC, buf, addr, len);
  450. }
  451. else {
  452. Rtv = VpdWriteStream(pAC, IoC, buf, addr, len);
  453. }
  454. return(Rtv);
  455. }
  456. #ifdef SKDIAG
  457. /*
  458. * Read 'len' bytes of VPD data, starting at 'addr'.
  459. *
  460. * Returns number of bytes read.
  461. */
  462. int VpdReadBlock(
  463. SK_AC *pAC, /* pAC pointer */
  464. SK_IOC IoC, /* IO Context */
  465. char *buf, /* buffer were the data should be stored */
  466. int addr, /* start reading at the VPD address */
  467. int len) /* number of bytes to read */
  468. {
  469. return(VpdTransferBlock(pAC, IoC, buf, addr, len, VPD_READ));
  470. }
  471. /*
  472. * Write 'len' bytes of *but to the VPD EEPROM, starting at 'addr'.
  473. *
  474. * Returns number of bytes writes.
  475. */
  476. int VpdWriteBlock(
  477. SK_AC *pAC, /* pAC pointer */
  478. SK_IOC IoC, /* IO Context */
  479. char *buf, /* buffer, holds the data to write */
  480. int addr, /* start writing at the VPD address */
  481. int len) /* number of bytes to write */
  482. {
  483. return(VpdTransferBlock(pAC, IoC, buf, addr, len, VPD_WRITE));
  484. }
  485. #endif /* SKDIAG */
  486. /*
  487. * (re)initialize the VPD buffer
  488. *
  489. * Reads the VPD data from the EEPROM into the VPD buffer.
  490. * Get the remaining read only and read / write space.
  491. *
  492. * return 0: success
  493. * 1: fatal VPD error
  494. */
  495. static int VpdInit(
  496. SK_AC *pAC, /* Adapters context */
  497. SK_IOC IoC) /* IO Context */
  498. {
  499. SK_VPD_PARA *r, rp; /* RW or RV */
  500. int i;
  501. unsigned char x;
  502. int vpd_size;
  503. SK_U16 dev_id;
  504. SK_U32 our_reg2;
  505. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_INIT, ("VpdInit .. "));
  506. VPD_IN16(pAC, IoC, PCI_DEVICE_ID, &dev_id);
  507. VPD_IN32(pAC, IoC, PCI_OUR_REG_2, &our_reg2);
  508. pAC->vpd.rom_size = 256 << ((our_reg2 & PCI_VPD_ROM_SZ) >> 14);
  509. /*
  510. * this function might get used before the hardware is initialized
  511. * therefore we cannot always trust in GIChipId
  512. */
  513. if (((pAC->vpd.v.vpd_status & VPD_VALID) == 0 &&
  514. dev_id != VPD_DEV_ID_GENESIS) ||
  515. ((pAC->vpd.v.vpd_status & VPD_VALID) != 0 &&
  516. !pAC->GIni.GIGenesis)) {
  517. /* for Yukon the VPD size is always 256 */
  518. vpd_size = VPD_SIZE_YUKON;
  519. }
  520. else {
  521. /* Genesis uses the maximum ROM size up to 512 for VPD */
  522. if (pAC->vpd.rom_size > VPD_SIZE_GENESIS) {
  523. vpd_size = VPD_SIZE_GENESIS;
  524. }
  525. else {
  526. vpd_size = pAC->vpd.rom_size;
  527. }
  528. }
  529. /* read the VPD data into the VPD buffer */
  530. if (VpdTransferBlock(pAC, IoC, pAC->vpd.vpd_buf, 0, vpd_size, VPD_READ)
  531. != vpd_size) {
  532. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  533. ("Block Read Error\n"));
  534. return(1);
  535. }
  536. pAC->vpd.vpd_size = vpd_size;
  537. /* find the end tag of the RO area */
  538. if (!(r = vpd_find_para(pAC, VPD_RV, &rp))) {
  539. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  540. ("Encoding Error: RV Tag not found\n"));
  541. return(1);
  542. }
  543. if (r->p_val + r->p_len > pAC->vpd.vpd_buf + vpd_size/2) {
  544. SK_DBG_MSG(pAC,SK_DBGMOD_VPD,SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  545. ("Encoding Error: Invalid VPD struct size\n"));
  546. return(1);
  547. }
  548. pAC->vpd.v.vpd_free_ro = r->p_len - 1;
  549. /* test the checksum */
  550. for (i = 0, x = 0; (unsigned)i <= (unsigned)vpd_size/2 - r->p_len; i++) {
  551. x += pAC->vpd.vpd_buf[i];
  552. }
  553. if (x != 0) {
  554. /* checksum error */
  555. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  556. ("VPD Checksum Error\n"));
  557. return(1);
  558. }
  559. /* find and check the end tag of the RW area */
  560. if (!(r = vpd_find_para(pAC, VPD_RW, &rp))) {
  561. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  562. ("Encoding Error: RV Tag not found\n"));
  563. return(1);
  564. }
  565. if (r->p_val < pAC->vpd.vpd_buf + vpd_size/2) {
  566. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  567. ("Encoding Error: Invalid VPD struct size\n"));
  568. return(1);
  569. }
  570. pAC->vpd.v.vpd_free_rw = r->p_len;
  571. /* everything seems to be ok */
  572. if (pAC->GIni.GIChipId != 0) {
  573. pAC->vpd.v.vpd_status |= VPD_VALID;
  574. }
  575. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_INIT,
  576. ("done. Free RO = %d, Free RW = %d\n",
  577. pAC->vpd.v.vpd_free_ro, pAC->vpd.v.vpd_free_rw));
  578. return(0);
  579. }
  580. /*
  581. * find the Keyword 'key' in the VPD buffer and fills the
  582. * parameter struct 'p' with it's values
  583. *
  584. * returns *p success
  585. * 0: parameter was not found or VPD encoding error
  586. */
  587. static SK_VPD_PARA *vpd_find_para(
  588. SK_AC *pAC, /* common data base */
  589. const char *key, /* keyword to find (e.g. "MN") */
  590. SK_VPD_PARA *p) /* parameter description struct */
  591. {
  592. char *v ; /* points to VPD buffer */
  593. int max; /* Maximum Number of Iterations */
  594. v = pAC->vpd.vpd_buf;
  595. max = 128;
  596. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  597. ("VPD find para %s .. ",key));
  598. /* check mandatory resource type ID string (Product Name) */
  599. if (*v != (char)RES_ID) {
  600. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  601. ("Error: 0x%x missing\n", RES_ID));
  602. return(0);
  603. }
  604. if (strcmp(key, VPD_NAME) == 0) {
  605. p->p_len = VPD_GET_RES_LEN(v);
  606. p->p_val = VPD_GET_VAL(v);
  607. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  608. ("found, len = %d\n", p->p_len));
  609. return(p);
  610. }
  611. v += 3 + VPD_GET_RES_LEN(v) + 3;
  612. for (;; ) {
  613. if (SK_MEMCMP(key,v,2) == 0) {
  614. p->p_len = VPD_GET_VPD_LEN(v);
  615. p->p_val = VPD_GET_VAL(v);
  616. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  617. ("found, len = %d\n",p->p_len));
  618. return(p);
  619. }
  620. /* exit when reaching the "RW" Tag or the maximum of itera. */
  621. max--;
  622. if (SK_MEMCMP(VPD_RW,v,2) == 0 || max == 0) {
  623. break;
  624. }
  625. if (SK_MEMCMP(VPD_RV,v,2) == 0) {
  626. v += 3 + VPD_GET_VPD_LEN(v) + 3; /* skip VPD-W */
  627. }
  628. else {
  629. v += 3 + VPD_GET_VPD_LEN(v);
  630. }
  631. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  632. ("scanning '%c%c' len = %d\n",v[0],v[1],v[2]));
  633. }
  634. #ifdef DEBUG
  635. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL, ("not found\n"));
  636. if (max == 0) {
  637. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  638. ("Key/Len Encoding error\n"));
  639. }
  640. #endif /* DEBUG */
  641. return(0);
  642. }
  643. /*
  644. * Move 'n' bytes. Begin with the last byte if 'n' is > 0,
  645. * Start with the last byte if n is < 0.
  646. *
  647. * returns nothing
  648. */
  649. static void vpd_move_para(
  650. char *start, /* start of memory block */
  651. char *end, /* end of memory block to move */
  652. int n) /* number of bytes the memory block has to be moved */
  653. {
  654. char *p;
  655. int i; /* number of byte copied */
  656. if (n == 0)
  657. return;
  658. i = (int) (end - start + 1);
  659. if (n < 0) {
  660. p = start + n;
  661. while (i != 0) {
  662. *p++ = *start++;
  663. i--;
  664. }
  665. }
  666. else {
  667. p = end + n;
  668. while (i != 0) {
  669. *p-- = *end--;
  670. i--;
  671. }
  672. }
  673. }
  674. /*
  675. * setup the VPD keyword 'key' at 'ip'.
  676. *
  677. * returns nothing
  678. */
  679. static void vpd_insert_key(
  680. const char *key, /* keyword to insert */
  681. const char *buf, /* buffer with the keyword value */
  682. int len, /* length of the value string */
  683. char *ip) /* inseration point */
  684. {
  685. SK_VPD_KEY *p;
  686. p = (SK_VPD_KEY *) ip;
  687. p->p_key[0] = key[0];
  688. p->p_key[1] = key[1];
  689. p->p_len = (unsigned char) len;
  690. SK_MEMCPY(&p->p_val,buf,len);
  691. }
  692. /*
  693. * Setup the VPD end tag "RV" / "RW".
  694. * Also correct the remaining space variables vpd_free_ro / vpd_free_rw.
  695. *
  696. * returns 0: success
  697. * 1: encoding error
  698. */
  699. static int vpd_mod_endtag(
  700. SK_AC *pAC, /* common data base */
  701. char *etp) /* end pointer input position */
  702. {
  703. SK_VPD_KEY *p;
  704. unsigned char x;
  705. int i;
  706. int vpd_size;
  707. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  708. ("VPD modify endtag at 0x%x = '%c%c'\n",etp,etp[0],etp[1]));
  709. vpd_size = pAC->vpd.vpd_size;
  710. p = (SK_VPD_KEY *) etp;
  711. if (p->p_key[0] != 'R' || (p->p_key[1] != 'V' && p->p_key[1] != 'W')) {
  712. /* something wrong here, encoding error */
  713. SK_DBG_MSG(pAC,SK_DBGMOD_VPD,SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
  714. ("Encoding Error: invalid end tag\n"));
  715. return(1);
  716. }
  717. if (etp > pAC->vpd.vpd_buf + vpd_size/2) {
  718. /* create "RW" tag */
  719. p->p_len = (unsigned char)(pAC->vpd.vpd_buf+vpd_size-etp-3-1);
  720. pAC->vpd.v.vpd_free_rw = (int) p->p_len;
  721. i = pAC->vpd.v.vpd_free_rw;
  722. etp += 3;
  723. }
  724. else {
  725. /* create "RV" tag */
  726. p->p_len = (unsigned char)(pAC->vpd.vpd_buf+vpd_size/2-etp-3);
  727. pAC->vpd.v.vpd_free_ro = (int) p->p_len - 1;
  728. /* setup checksum */
  729. for (i = 0, x = 0; i < vpd_size/2 - p->p_len; i++) {
  730. x += pAC->vpd.vpd_buf[i];
  731. }
  732. p->p_val = (char) 0 - x;
  733. i = pAC->vpd.v.vpd_free_ro;
  734. etp += 4;
  735. }
  736. while (i) {
  737. *etp++ = 0x00;
  738. i--;
  739. }
  740. return(0);
  741. }
  742. /*
  743. * Insert a VPD keyword into the VPD buffer.
  744. *
  745. * The keyword 'key' is inserted at the position 'ip' in the
  746. * VPD buffer.
  747. * The keywords behind the input position will
  748. * be moved. The VPD end tag "RV" or "RW" is generated again.
  749. *
  750. * returns 0: success
  751. * 2: value string was cut
  752. * 4: VPD full, keyword was not written
  753. * 6: fatal VPD error
  754. *
  755. */
  756. int VpdSetupPara(
  757. SK_AC *pAC, /* common data base */
  758. const char *key, /* keyword to insert */
  759. const char *buf, /* buffer with the keyword value */
  760. int len, /* length of the keyword value */
  761. int type, /* VPD_RO_KEY or VPD_RW_KEY */
  762. int op) /* operation to do: ADD_KEY or OWR_KEY */
  763. {
  764. SK_VPD_PARA vp;
  765. char *etp; /* end tag position */
  766. int free; /* remaining space in selected area */
  767. char *ip; /* input position inside the VPD buffer */
  768. int rtv; /* return code */
  769. int head; /* additional haeder bytes to move */
  770. int found; /* additinoal bytes if the keyword was found */
  771. int vpd_size;
  772. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  773. ("VPD setup para key = %s, val = %s\n",key,buf));
  774. vpd_size = pAC->vpd.vpd_size;
  775. rtv = 0;
  776. ip = 0;
  777. if (type == VPD_RW_KEY) {
  778. /* end tag is "RW" */
  779. free = pAC->vpd.v.vpd_free_rw;
  780. etp = pAC->vpd.vpd_buf + (vpd_size - free - 1 - 3);
  781. }
  782. else {
  783. /* end tag is "RV" */
  784. free = pAC->vpd.v.vpd_free_ro;
  785. etp = pAC->vpd.vpd_buf + (vpd_size/2 - free - 4);
  786. }
  787. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  788. ("Free RO = %d, Free RW = %d\n",
  789. pAC->vpd.v.vpd_free_ro, pAC->vpd.v.vpd_free_rw));
  790. head = 0;
  791. found = 0;
  792. if (op == OWR_KEY) {
  793. if (vpd_find_para(pAC, key, &vp)) {
  794. found = 3;
  795. ip = vp.p_val - 3;
  796. free += vp.p_len + 3;
  797. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  798. ("Overwrite Key\n"));
  799. }
  800. else {
  801. op = ADD_KEY;
  802. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
  803. ("Add Key\n"));
  804. }
  805. }
  806. if (op == ADD_KEY) {
  807. ip = etp;
  808. vp.p_len = 0;
  809. head = 3;
  810. }
  811. if (len + 3 > free) {
  812. if (free < 7) {
  813. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  814. ("VPD Buffer Overflow, keyword not written\n"));
  815. return(4);
  816. }
  817. /* cut it again */
  818. len = free - 3;
  819. rtv = 2;
  820. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  821. ("VPD Buffer Full, Keyword was cut\n"));
  822. }
  823. vpd_move_para(ip + vp.p_len + found, etp+2, len-vp.p_len+head);
  824. vpd_insert_key(key, buf, len, ip);
  825. if (vpd_mod_endtag(pAC, etp + len - vp.p_len + head)) {
  826. pAC->vpd.v.vpd_status &= ~VPD_VALID;
  827. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  828. ("VPD Encoding Error\n"));
  829. return(6);
  830. }
  831. return(rtv);
  832. }
  833. /*
  834. * Read the contents of the VPD EEPROM and copy it to the
  835. * VPD buffer if not already done.
  836. *
  837. * return: A pointer to the vpd_status structure. The structure contains
  838. * this fields.
  839. */
  840. SK_VPD_STATUS *VpdStat(
  841. SK_AC *pAC, /* Adapters context */
  842. SK_IOC IoC) /* IO Context */
  843. {
  844. if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
  845. (void)VpdInit(pAC, IoC);
  846. }
  847. return(&pAC->vpd.v);
  848. }
  849. /*
  850. * Read the contents of the VPD EEPROM and copy it to the VPD
  851. * buffer if not already done.
  852. * Scan the VPD buffer for VPD keywords and create the VPD
  853. * keyword list by copying the keywords to 'buf', all after
  854. * each other and terminated with a '\0'.
  855. *
  856. * Exceptions: o The Resource Type ID String (product name) is called "Name"
  857. * o The VPD end tags 'RV' and 'RW' are not listed
  858. *
  859. * The number of copied keywords is counted in 'elements'.
  860. *
  861. * returns 0: success
  862. * 2: buffer overfull, one or more keywords are missing
  863. * 6: fatal VPD error
  864. *
  865. * example values after returning:
  866. *
  867. * buf = "Name\0PN\0EC\0MN\0SN\0CP\0VF\0VL\0YA\0"
  868. * *len = 30
  869. * *elements = 9
  870. */
  871. int VpdKeys(
  872. SK_AC *pAC, /* common data base */
  873. SK_IOC IoC, /* IO Context */
  874. char *buf, /* buffer where to copy the keywords */
  875. int *len, /* buffer length */
  876. int *elements) /* number of keywords returned */
  877. {
  878. char *v;
  879. int n;
  880. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX, ("list VPD keys .. "));
  881. *elements = 0;
  882. if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
  883. if (VpdInit(pAC, IoC) != 0) {
  884. *len = 0;
  885. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  886. ("VPD Init Error, terminated\n"));
  887. return(6);
  888. }
  889. }
  890. if ((signed)strlen(VPD_NAME) + 1 <= *len) {
  891. v = pAC->vpd.vpd_buf;
  892. strcpy(buf,VPD_NAME);
  893. n = strlen(VPD_NAME) + 1;
  894. buf += n;
  895. *elements = 1;
  896. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX,
  897. ("'%c%c' ",v[0],v[1]));
  898. }
  899. else {
  900. *len = 0;
  901. SK_DBG_MSG(pAC,SK_DBGMOD_VPD,SK_DBGCAT_ERR,
  902. ("buffer overflow\n"));
  903. return(2);
  904. }
  905. v += 3 + VPD_GET_RES_LEN(v) + 3;
  906. for (;; ) {
  907. /* exit when reaching the "RW" Tag */
  908. if (SK_MEMCMP(VPD_RW,v,2) == 0) {
  909. break;
  910. }
  911. if (SK_MEMCMP(VPD_RV,v,2) == 0) {
  912. v += 3 + VPD_GET_VPD_LEN(v) + 3; /* skip VPD-W */
  913. continue;
  914. }
  915. if (n+3 <= *len) {
  916. SK_MEMCPY(buf,v,2);
  917. buf += 2;
  918. *buf++ = '\0';
  919. n += 3;
  920. v += 3 + VPD_GET_VPD_LEN(v);
  921. *elements += 1;
  922. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX,
  923. ("'%c%c' ",v[0],v[1]));
  924. }
  925. else {
  926. *len = n;
  927. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  928. ("buffer overflow\n"));
  929. return(2);
  930. }
  931. }
  932. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX, ("\n"));
  933. *len = n;
  934. return(0);
  935. }
  936. /*
  937. * Read the contents of the VPD EEPROM and copy it to the
  938. * VPD buffer if not already done. Search for the VPD keyword
  939. * 'key' and copy its value to 'buf'. Add a terminating '\0'.
  940. * If the value does not fit into the buffer cut it after
  941. * 'len' - 1 bytes.
  942. *
  943. * returns 0: success
  944. * 1: keyword not found
  945. * 2: value string was cut
  946. * 3: VPD transfer timeout
  947. * 6: fatal VPD error
  948. */
  949. int VpdRead(
  950. SK_AC *pAC, /* common data base */
  951. SK_IOC IoC, /* IO Context */
  952. const char *key, /* keyword to read (e.g. "MN") */
  953. char *buf, /* buffer where to copy the keyword value */
  954. int *len) /* buffer length */
  955. {
  956. SK_VPD_PARA *p, vp;
  957. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX, ("VPD read %s .. ", key));
  958. if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
  959. if (VpdInit(pAC, IoC) != 0) {
  960. *len = 0;
  961. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  962. ("VPD init error\n"));
  963. return(6);
  964. }
  965. }
  966. if ((p = vpd_find_para(pAC, key, &vp)) != NULL) {
  967. if (p->p_len > (*(unsigned *)len)-1) {
  968. p->p_len = *len - 1;
  969. }
  970. SK_MEMCPY(buf, p->p_val, p->p_len);
  971. buf[p->p_len] = '\0';
  972. *len = p->p_len;
  973. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX,
  974. ("%c%c%c%c.., len = %d\n",
  975. buf[0],buf[1],buf[2],buf[3],*len));
  976. }
  977. else {
  978. *len = 0;
  979. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR, ("not found\n"));
  980. return(1);
  981. }
  982. return(0);
  983. }
  984. /*
  985. * Check whether a given key may be written
  986. *
  987. * returns
  988. * SK_TRUE Yes it may be written
  989. * SK_FALSE No it may be written
  990. */
  991. SK_BOOL VpdMayWrite(
  992. char *key) /* keyword to write (allowed values "Yx", "Vx") */
  993. {
  994. if ((*key != 'Y' && *key != 'V') ||
  995. key[1] < '0' || key[1] > 'Z' ||
  996. (key[1] > '9' && key[1] < 'A') || strlen(key) != 2) {
  997. return(SK_FALSE);
  998. }
  999. return(SK_TRUE);
  1000. }
  1001. /*
  1002. * Read the contents of the VPD EEPROM and copy it to the VPD
  1003. * buffer if not already done. Insert/overwrite the keyword 'key'
  1004. * in the VPD buffer. Cut the keyword value if it does not fit
  1005. * into the VPD read / write area.
  1006. *
  1007. * returns 0: success
  1008. * 2: value string was cut
  1009. * 3: VPD transfer timeout
  1010. * 4: VPD full, keyword was not written
  1011. * 5: keyword cannot be written
  1012. * 6: fatal VPD error
  1013. */
  1014. int VpdWrite(
  1015. SK_AC *pAC, /* common data base */
  1016. SK_IOC IoC, /* IO Context */
  1017. const char *key, /* keyword to write (allowed values "Yx", "Vx") */
  1018. const char *buf) /* buffer where the keyword value can be read from */
  1019. {
  1020. int len; /* length of the keyword to write */
  1021. int rtv; /* return code */
  1022. int rtv2;
  1023. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX,
  1024. ("VPD write %s = %s\n",key,buf));
  1025. if ((*key != 'Y' && *key != 'V') ||
  1026. key[1] < '0' || key[1] > 'Z' ||
  1027. (key[1] > '9' && key[1] < 'A') || strlen(key) != 2) {
  1028. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1029. ("illegal key tag, keyword not written\n"));
  1030. return(5);
  1031. }
  1032. if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
  1033. if (VpdInit(pAC, IoC) != 0) {
  1034. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1035. ("VPD init error\n"));
  1036. return(6);
  1037. }
  1038. }
  1039. rtv = 0;
  1040. len = strlen(buf);
  1041. if (len > VPD_MAX_LEN) {
  1042. /* cut it */
  1043. len = VPD_MAX_LEN;
  1044. rtv = 2;
  1045. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1046. ("keyword too long, cut after %d bytes\n",VPD_MAX_LEN));
  1047. }
  1048. if ((rtv2 = VpdSetupPara(pAC, key, buf, len, VPD_RW_KEY, OWR_KEY)) != 0) {
  1049. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1050. ("VPD write error\n"));
  1051. return(rtv2);
  1052. }
  1053. return(rtv);
  1054. }
  1055. /*
  1056. * Read the contents of the VPD EEPROM and copy it to the
  1057. * VPD buffer if not already done. Remove the VPD keyword
  1058. * 'key' from the VPD buffer.
  1059. * Only the keywords in the read/write area can be deleted.
  1060. * Keywords in the read only area cannot be deleted.
  1061. *
  1062. * returns 0: success, keyword was removed
  1063. * 1: keyword not found
  1064. * 5: keyword cannot be deleted
  1065. * 6: fatal VPD error
  1066. */
  1067. int VpdDelete(
  1068. SK_AC *pAC, /* common data base */
  1069. SK_IOC IoC, /* IO Context */
  1070. char *key) /* keyword to read (e.g. "MN") */
  1071. {
  1072. SK_VPD_PARA *p, vp;
  1073. char *etp;
  1074. int vpd_size;
  1075. vpd_size = pAC->vpd.vpd_size;
  1076. SK_DBG_MSG(pAC,SK_DBGMOD_VPD,SK_DBGCAT_TX,("VPD delete key %s\n",key));
  1077. if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
  1078. if (VpdInit(pAC, IoC) != 0) {
  1079. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1080. ("VPD init error\n"));
  1081. return(6);
  1082. }
  1083. }
  1084. if ((p = vpd_find_para(pAC, key, &vp)) != NULL) {
  1085. if (p->p_val < pAC->vpd.vpd_buf + vpd_size/2) {
  1086. /* try to delete read only keyword */
  1087. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1088. ("cannot delete RO keyword\n"));
  1089. return(5);
  1090. }
  1091. etp = pAC->vpd.vpd_buf + (vpd_size-pAC->vpd.v.vpd_free_rw-1-3);
  1092. vpd_move_para(vp.p_val+vp.p_len, etp+2,
  1093. - ((int)(vp.p_len + 3)));
  1094. if (vpd_mod_endtag(pAC, etp - vp.p_len - 3)) {
  1095. pAC->vpd.v.vpd_status &= ~VPD_VALID;
  1096. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1097. ("VPD encoding error\n"));
  1098. return(6);
  1099. }
  1100. }
  1101. else {
  1102. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1103. ("keyword not found\n"));
  1104. return(1);
  1105. }
  1106. return(0);
  1107. }
  1108. /*
  1109. * If the VPD buffer contains valid data write the VPD
  1110. * read/write area back to the VPD EEPROM.
  1111. *
  1112. * returns 0: success
  1113. * 3: VPD transfer timeout
  1114. */
  1115. int VpdUpdate(
  1116. SK_AC *pAC, /* Adapters context */
  1117. SK_IOC IoC) /* IO Context */
  1118. {
  1119. int vpd_size;
  1120. vpd_size = pAC->vpd.vpd_size;
  1121. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX, ("VPD update .. "));
  1122. if ((pAC->vpd.v.vpd_status & VPD_VALID) != 0) {
  1123. if (VpdTransferBlock(pAC, IoC, pAC->vpd.vpd_buf + vpd_size/2,
  1124. vpd_size/2, vpd_size/2, VPD_WRITE) != vpd_size/2) {
  1125. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1126. ("transfer timed out\n"));
  1127. return(3);
  1128. }
  1129. }
  1130. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX, ("done\n"));
  1131. return(0);
  1132. }
  1133. /*
  1134. * Read the contents of the VPD EEPROM and copy it to the VPD buffer
  1135. * if not already done. If the keyword "VF" is not present it will be
  1136. * created and the error log message will be stored to this keyword.
  1137. * If "VF" is not present the error log message will be stored to the
  1138. * keyword "VL". "VL" will created or overwritten if "VF" is present.
  1139. * The VPD read/write area is saved to the VPD EEPROM.
  1140. *
  1141. * returns nothing, errors will be ignored.
  1142. */
  1143. void VpdErrLog(
  1144. SK_AC *pAC, /* common data base */
  1145. SK_IOC IoC, /* IO Context */
  1146. char *msg) /* error log message */
  1147. {
  1148. SK_VPD_PARA *v, vf; /* VF */
  1149. int len;
  1150. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX,
  1151. ("VPD error log msg %s\n", msg));
  1152. if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
  1153. if (VpdInit(pAC, IoC) != 0) {
  1154. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
  1155. ("VPD init error\n"));
  1156. return;
  1157. }
  1158. }
  1159. len = strlen(msg);
  1160. if (len > VPD_MAX_LEN) {
  1161. /* cut it */
  1162. len = VPD_MAX_LEN;
  1163. }
  1164. if ((v = vpd_find_para(pAC, VPD_VF, &vf)) != NULL) {
  1165. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX, ("overwrite VL\n"));
  1166. (void)VpdSetupPara(pAC, VPD_VL, msg, len, VPD_RW_KEY, OWR_KEY);
  1167. }
  1168. else {
  1169. SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX, ("write VF\n"));
  1170. (void)VpdSetupPara(pAC, VPD_VF, msg, len, VPD_RW_KEY, ADD_KEY);
  1171. }
  1172. (void)VpdUpdate(pAC, IoC);
  1173. }
  1174. #endif /* CONFIG_SK98 */