skvpd.c 33 KB

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