1 /******************************************************************************
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
9 ******************************************************************************/
11 /******************************************************************************
13 * (C)Copyright 1998-2003 SysKonnect GmbH.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * The information in this file is provided "AS IS" without warranty.
22 ******************************************************************************/
24 /******************************************************************************
29 * Revision 1.37 2003/01/13 10:42:45 rschmidt
30 * Replaced check for PCI device Id from YUKON with GENESIS
31 * to set the VPD size in VpdInit()
34 * Revision 1.36 2002/11/14 15:16:56 gheinig
35 * Added const specifier to key and buf parameters for VpdPara, VpdRead
36 * and VpdWrite for Diag 7 GUI
38 * Revision 1.35 2002/10/21 14:31:59 gheinig
39 * Took out CVS web garbage at head of file
41 * Revision 1.34 2002/10/21 11:47:24 gheinig
42 * Reverted to version 1.32 due to unwanted commit
44 * Revision 1.32 2002/10/14 16:04:29 rschmidt
45 * Added saving of VPD ROM Size from PCI_OUR_REG_2
46 * Avoid reading of PCI_OUR_REG_2 in VpdTransferBlock()
49 * Revision 1.31 2002/09/10 09:21:32 mkarl
50 * Replaced all if(GIChipId == CHIP_ID_GENESIS) with new entry GIGenesis
52 * Revision 1.30 2002/09/09 14:43:03 mkarl
53 * changes for diagnostics in order to read VPD data before the adapter
54 * has been initialized
57 * Revision 1.29 2002/07/26 13:20:43 mkarl
59 * save size of VPD in pAC->vpd.vpd_size
61 * Revision 1.28 2002/04/02 15:31:47 afischer
62 * Bug fix in VpdWait()
64 * Revision 1.27 2000/08/10 11:29:06 rassmann
66 * Preserving 32-bit alignment in structs for the adapter context.
67 * Removed unused function VpdWriteDword() (#if 0).
68 * Made VpdReadKeyword() available for SKDIAG only.
70 * Revision 1.26 2000/06/13 08:00:01 mkarl
71 * additional cast to avoid compile problems in 64 bit environment
73 * Revision 1.25 1999/11/22 13:39:32 cgoos
74 * Changed license header to GPL.
76 * Revision 1.24 1999/03/11 14:25:49 malthoff
77 * Replace __STDC__ with SK_KR_PROTO.
79 * Revision 1.23 1999/01/11 15:13:11 gklug
82 * Revision 1.22 1998/10/30 06:41:15 gklug
85 * Revision 1.21 1998/10/29 07:15:14 gklug
86 * fix: Write Stream function needs verify.
88 * Revision 1.20 1998/10/28 18:05:08 gklug
89 * chg: no DEBUG in VpdMayWrite
91 * Revision 1.19 1998/10/28 15:56:11 gklug
92 * fix: Return len at end of ReadStream
93 * fix: Write even less than 4 bytes correctly
95 * Revision 1.18 1998/10/28 09:00:47 gklug
96 * fix: unreferenced local vars
98 * Revision 1.17 1998/10/28 08:25:45 gklug
101 * Revision 1.16 1998/10/28 08:17:30 gklug
104 * Revision 1.15 1998/10/28 07:50:32 gklug
107 * Revision 1.14 1998/10/28 07:20:38 gklug
108 * chg: Interface functions to use IoC as parameter as well
109 * fix: VpdRead/WriteDWord now returns SK_U32
110 * chg: VPD_IN/OUT names conform to SK_IN/OUT
111 * add: usage of VPD_IN/OUT8 macros
112 * add: VpdRead/Write Stream functions to r/w a stream of data
113 * fix: VpdTransferBlock swapped illegal
116 * Revision 1.13 1998/10/22 10:02:37 gklug
117 * fix: SysKonnectFileId typo
119 * Revision 1.12 1998/10/20 10:01:01 gklug
120 * fix: parameter to SkOsGetTime
122 * Revision 1.11 1998/10/15 12:51:48 malthoff
123 * Remove unrequired parameter p in vpd_setup_para().
125 * Revision 1.10 1998/10/08 14:52:43 malthoff
126 * Remove CvsId by SysKonnectFileId.
128 * Revision 1.9 1998/09/16 07:33:52 malthoff
129 * replace memcmp() by SK_MEMCMP and
130 * memcpy() by SK_MEMCPY() to be
131 * independent from the 'C' Standard Library.
133 * Revision 1.8 1998/08/19 12:52:35 malthoff
134 * compiler fix: use SK_VPD_KEY instead of S_VPD.
136 * Revision 1.7 1998/08/19 08:14:01 gklug
137 * fix: remove struct keyword as much as possible from the C-code (see CCC)
139 * Revision 1.6 1998/08/18 13:03:58 gklug
140 * SkOsGetTime now returns SK_U64
142 * Revision 1.5 1998/08/18 08:17:29 malthoff
143 * Ensure we issue a VPD read in vpd_read_dword().
144 * Discard all VPD keywords other than Vx or Yx, where
145 * x is '0..9' or 'A..Z'.
147 * Revision 1.4 1998/07/03 14:52:19 malthoff
148 * Add category SK_DBGCAT_FATAL to some debug macros.
149 * bug fix: correct the keyword name check in vpd_write().
151 * Revision 1.3 1998/06/26 11:16:53 malthoff
152 * Correct the modified File Identifier.
154 * Revision 1.2 1998/06/26 11:13:43 malthoff
155 * Modify the File Identifier.
157 * Revision 1.1 1998/06/19 14:11:08 malthoff
158 * Created, Tests with AIX were performed successfully
161 ******************************************************************************/
166 Please refer skvpd.txt for infomation how to include this module
168 static const char SysKonnectFileId[] =
169 "@(#)$Id: skvpd.c,v 1.37 2003/01/13 10:42:45 rschmidt Exp $ (C) SK";
171 #include "h/skdrv1st.h"
172 #include "h/sktypes.h"
173 #include "h/skdebug.h"
174 #include "h/skdrv2nd.h"
180 static SK_VPD_PARA *vpd_find_para(
184 #else /* SK_KR_PROTO */
185 static SK_VPD_PARA *vpd_find_para();
186 #endif /* SK_KR_PROTO */
189 * waits for a completion of a VPD transfer
190 * The VPD transfer must complete within SK_TICKS_PER_SEC/16
192 * returns 0: success, transfer completes
193 * error exit(9) with a error message
196 SK_AC *pAC, /* Adapters context */
197 SK_IOC IoC, /* IO Context */
198 int event) /* event to wait for (VPD_READ / VPD_write) completion*/
203 SK_DBG_MSG(pAC,SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
204 ("VPD wait for %s\n", event?"Write":"Read"));
205 start_time = SkOsGetTime(pAC);
207 if (SkOsGetTime(pAC) - start_time > SK_TICKS_PER_SEC) {
209 /* Bug fix AF: Thu Mar 28 2002
210 * Do not call: VPD_STOP(pAC, IoC);
211 * A pending VPD read cycle can not be aborted by writing
212 * VPD_WRITE to the PCI_VPD_ADR_REG (VPD address register).
213 * Although the write threshold in the OUR-register protects
214 * VPD read only space from being overwritten this does not
215 * protect a VPD read from being `converted` into a VPD write
216 * operation (on the fly). As a consequence the VPD_STOP would
217 * delete VPD read only data. In case of any problems with the
218 * I2C bus we exit the loop here. The I2C read operation can
219 * not be aborted except by a reset (->LR).
221 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_FATAL | SK_DBGCAT_ERR,
222 ("ERROR:VPD wait timeout\n"));
226 VPD_IN16(pAC, IoC, PCI_VPD_ADR_REG, &state);
228 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
229 ("state = %x, event %x\n",state,event));
230 } while((int)(state & PCI_VPD_FLAG) == event);
238 * Read the dword at address 'addr' from the VPD EEPROM.
240 * Needed Time: MIN 1,3 ms MAX 2,6 ms
242 * Note: The DWord is returned in the endianess of the machine the routine
245 * Returns the data read.
248 SK_AC *pAC, /* Adapters context */
249 SK_IOC IoC, /* IO Context */
250 int addr) /* VPD address */
255 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
256 ("VPD read dword at 0x%x\n",addr));
257 addr &= ~VPD_WRITE; /* ensure the R/W bit is set to read */
259 VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, (SK_U16)addr);
261 /* ignore return code here */
262 (void)VpdWait(pAC, IoC, VPD_READ);
264 /* Don't swap here, it's a data stream of bytes */
267 VPD_IN32(pAC, IoC, PCI_VPD_DAT_REG, &Rtv);
269 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
270 ("VPD read dword data = 0x%x\n",Rtv));
279 Write the dword 'data' at address 'addr' into the VPD EEPROM, and
280 verify that the data is written.
285 . -------------------------------------------------------------------
286 . write 1.8 ms 3.6 ms
287 . internal write cyles 0.7 ms 7.0 ms
288 . -------------------------------------------------------------------
289 . over all program time 2.5 ms 10.6 ms
291 . -------------------------------------------------------------------
292 . over all 3.8 ms 13.2 ms
297 1: error, I2C transfer does not terminate
298 2: error, data verify error
301 static int VpdWriteDWord(
302 SK_AC *pAC, /* pAC pointer */
303 SK_IOC IoC, /* IO Context */
304 int addr, /* VPD address */
305 SK_U32 data) /* VPD data to write */
307 /* start VPD write */
308 /* Don't swap here, it's a data stream of bytes */
309 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
310 ("VPD write dword at addr 0x%x, data = 0x%x\n",addr,data));
311 VPD_OUT32(pAC, IoC, PCI_VPD_DAT_REG, (SK_U32)data);
315 VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, (SK_U16)(addr | VPD_WRITE));
317 /* this may take up to 10,6 ms */
318 if (VpdWait(pAC, IoC, VPD_WRITE)) {
319 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
320 ("Write Timed Out\n"));
325 if (VpdReadDWord(pAC, IoC, addr) != data) {
326 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
327 ("Data Verify Error\n"));
331 } /* VpdWriteDWord */
336 * Read one Stream of 'len' bytes of VPD data, starting at 'addr' from
337 * or to the I2C EEPROM.
339 * Returns number of bytes read / written.
341 static int VpdWriteStream(
342 SK_AC *pAC, /* Adapters context */
343 SK_IOC IoC, /* IO Context */
344 char *buf, /* data buffer */
345 int Addr, /* VPD start address */
346 int Len) /* number of bytes to read / to write */
352 SK_U8 * pComp; /* Compare pointer */
353 SK_U8 Data; /* Input Data for Compare */
355 /* Init Compare Pointer */
356 pComp = (SK_U8 *) buf;
358 for (i = 0; i < Len; i++, buf++) {
359 if ((i%sizeof(SK_U32)) == 0) {
361 * At the begin of each cycle read the Data Reg
362 * So it is initialized even if only a few bytes
365 AdrReg = (SK_U16) Addr;
366 AdrReg &= ~VPD_WRITE; /* READ operation */
368 VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, AdrReg);
370 /* Wait for termination */
371 Rtv = VpdWait(pAC, IoC, VPD_READ);
377 /* Write current Byte */
378 VPD_OUT8(pAC, IoC, PCI_VPD_DAT_REG + (i%sizeof(SK_U32)),
381 if (((i%sizeof(SK_U32)) == 3) || (i == (Len - 1))) {
382 /* New Address needs to be written to VPD_ADDR reg */
383 AdrReg = (SK_U16) Addr;
384 Addr += sizeof(SK_U32);
385 AdrReg |= VPD_WRITE; /* WRITE operation */
387 VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, AdrReg);
389 /* Wait for termination */
390 Rtv = VpdWait(pAC, IoC, VPD_WRITE);
392 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
393 ("Write Timed Out\n"));
394 return(i - (i%sizeof(SK_U32)));
398 * Now re-read to verify
400 AdrReg &= ~VPD_WRITE; /* READ operation */
402 VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, AdrReg);
404 /* Wait for termination */
405 Rtv = VpdWait(pAC, IoC, VPD_READ);
407 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
408 ("Verify Timed Out\n"));
409 return(i - (i%sizeof(SK_U32)));
412 for (j = 0; j <= (int)(i%sizeof(SK_U32)); j++, pComp++) {
414 VPD_IN8(pAC, IoC, PCI_VPD_DAT_REG + j, &Data);
416 if (Data != *pComp) {
418 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
419 ("WriteStream Verify Error\n"));
420 return(i - (i%sizeof(SK_U32)) + j);
431 * Read one Stream of 'len' bytes of VPD data, starting at 'addr' from
432 * or to the I2C EEPROM.
434 * Returns number of bytes read / written.
436 static int VpdReadStream(
437 SK_AC *pAC, /* Adapters context */
438 SK_IOC IoC, /* IO Context */
439 char *buf, /* data buffer */
440 int Addr, /* VPD start address */
441 int Len) /* number of bytes to read / to write */
447 for (i = 0; i < Len; i++, buf++) {
448 if ((i%sizeof(SK_U32)) == 0) {
449 /* New Address needs to be written to VPD_ADDR reg */
450 AdrReg = (SK_U16) Addr;
451 Addr += sizeof(SK_U32);
452 AdrReg &= ~VPD_WRITE; /* READ operation */
454 VPD_OUT16(pAC, IoC, PCI_VPD_ADR_REG, AdrReg);
456 /* Wait for termination */
457 Rtv = VpdWait(pAC, IoC, VPD_READ);
462 VPD_IN8(pAC, IoC, PCI_VPD_DAT_REG + (i%sizeof(SK_U32)),
470 * Read ore writes 'len' bytes of VPD data, starting at 'addr' from
471 * or to the I2C EEPROM.
473 * Returns number of bytes read / written.
475 static int VpdTransferBlock(
476 SK_AC *pAC, /* Adapters context */
477 SK_IOC IoC, /* IO Context */
478 char *buf, /* data buffer */
479 int addr, /* VPD start address */
480 int len, /* number of bytes to read / to write */
481 int dir) /* transfer direction may be VPD_READ or VPD_WRITE */
483 int Rtv; /* Return value */
486 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
487 ("VPD %s block, addr = 0x%x, len = %d\n",
488 dir ? "write" : "read", addr, len));
493 vpd_rom_size = pAC->vpd.rom_size;
495 if (addr > vpd_rom_size - 4) {
496 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
497 ("Address error: 0x%x, exp. < 0x%x\n",
498 addr, vpd_rom_size - 4));
502 if (addr + len > vpd_rom_size) {
503 len = vpd_rom_size - addr;
504 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
505 ("Warning: len was cut to %d\n", len));
508 if (dir == VPD_READ) {
509 Rtv = VpdReadStream(pAC, IoC, buf, addr, len);
512 Rtv = VpdWriteStream(pAC, IoC, buf, addr, len);
521 * Read 'len' bytes of VPD data, starting at 'addr'.
523 * Returns number of bytes read.
526 SK_AC *pAC, /* pAC pointer */
527 SK_IOC IoC, /* IO Context */
528 char *buf, /* buffer were the data should be stored */
529 int addr, /* start reading at the VPD address */
530 int len) /* number of bytes to read */
532 return(VpdTransferBlock(pAC, IoC, buf, addr, len, VPD_READ));
536 * Write 'len' bytes of *but to the VPD EEPROM, starting at 'addr'.
538 * Returns number of bytes writes.
541 SK_AC *pAC, /* pAC pointer */
542 SK_IOC IoC, /* IO Context */
543 char *buf, /* buffer, holds the data to write */
544 int addr, /* start writing at the VPD address */
545 int len) /* number of bytes to write */
547 return(VpdTransferBlock(pAC, IoC, buf, addr, len, VPD_WRITE));
552 * (re)initialize the VPD buffer
554 * Reads the VPD data from the EEPROM into the VPD buffer.
555 * Get the remaining read only and read / write space.
561 SK_AC *pAC, /* Adapters context */
562 SK_IOC IoC) /* IO Context */
564 SK_VPD_PARA *r, rp; /* RW or RV */
571 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_INIT, ("VpdInit .. "));
573 VPD_IN16(pAC, IoC, PCI_DEVICE_ID, &dev_id);
575 VPD_IN32(pAC, IoC, PCI_OUR_REG_2, &our_reg2);
577 pAC->vpd.rom_size = 256 << ((our_reg2 & PCI_VPD_ROM_SZ) >> 14);
580 * this function might get used before the hardware is initialized
581 * therefore we cannot always trust in GIChipId
583 if (((pAC->vpd.v.vpd_status & VPD_VALID) == 0 &&
584 dev_id != VPD_DEV_ID_GENESIS) ||
585 ((pAC->vpd.v.vpd_status & VPD_VALID) != 0 &&
586 !pAC->GIni.GIGenesis)) {
588 /* for Yukon the VPD size is always 256 */
589 vpd_size = VPD_SIZE_YUKON;
592 /* Genesis uses the maximum ROM size up to 512 for VPD */
593 if (pAC->vpd.rom_size > VPD_SIZE_GENESIS) {
594 vpd_size = VPD_SIZE_GENESIS;
597 vpd_size = pAC->vpd.rom_size;
601 /* read the VPD data into the VPD buffer */
602 if (VpdTransferBlock(pAC, IoC, pAC->vpd.vpd_buf, 0, vpd_size, VPD_READ)
605 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
606 ("Block Read Error\n"));
610 pAC->vpd.vpd_size = vpd_size;
612 /* find the end tag of the RO area */
613 if (!(r = vpd_find_para(pAC, VPD_RV, &rp))) {
614 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
615 ("Encoding Error: RV Tag not found\n"));
619 if (r->p_val + r->p_len > pAC->vpd.vpd_buf + vpd_size/2) {
620 SK_DBG_MSG(pAC,SK_DBGMOD_VPD,SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
621 ("Encoding Error: Invalid VPD struct size\n"));
624 pAC->vpd.v.vpd_free_ro = r->p_len - 1;
626 /* test the checksum */
627 for (i = 0, x = 0; (unsigned)i <= (unsigned)vpd_size/2 - r->p_len; i++) {
628 x += pAC->vpd.vpd_buf[i];
633 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
634 ("VPD Checksum Error\n"));
638 /* find and check the end tag of the RW area */
639 if (!(r = vpd_find_para(pAC, VPD_RW, &rp))) {
640 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
641 ("Encoding Error: RV Tag not found\n"));
645 if (r->p_val < pAC->vpd.vpd_buf + vpd_size/2) {
646 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
647 ("Encoding Error: Invalid VPD struct size\n"));
650 pAC->vpd.v.vpd_free_rw = r->p_len;
652 /* everything seems to be ok */
653 if (pAC->GIni.GIChipId != 0) {
654 pAC->vpd.v.vpd_status |= VPD_VALID;
657 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_INIT,
658 ("done. Free RO = %d, Free RW = %d\n",
659 pAC->vpd.v.vpd_free_ro, pAC->vpd.v.vpd_free_rw));
665 * find the Keyword 'key' in the VPD buffer and fills the
666 * parameter struct 'p' with it's values
669 * 0: parameter was not found or VPD encoding error
671 static SK_VPD_PARA *vpd_find_para(
672 SK_AC *pAC, /* common data base */
673 const char *key, /* keyword to find (e.g. "MN") */
674 SK_VPD_PARA *p) /* parameter description struct */
676 char *v ; /* points to VPD buffer */
677 int max; /* Maximum Number of Iterations */
679 v = pAC->vpd.vpd_buf;
682 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
683 ("VPD find para %s .. ",key));
685 /* check mandatory resource type ID string (Product Name) */
686 if (*v != (char)RES_ID) {
687 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
688 ("Error: 0x%x missing\n", RES_ID));
692 if (strcmp(key, VPD_NAME) == 0) {
693 p->p_len = VPD_GET_RES_LEN(v);
694 p->p_val = VPD_GET_VAL(v);
695 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
696 ("found, len = %d\n", p->p_len));
700 v += 3 + VPD_GET_RES_LEN(v) + 3;
702 if (SK_MEMCMP(key,v,2) == 0) {
703 p->p_len = VPD_GET_VPD_LEN(v);
704 p->p_val = VPD_GET_VAL(v);
705 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
706 ("found, len = %d\n",p->p_len));
710 /* exit when reaching the "RW" Tag or the maximum of itera. */
712 if (SK_MEMCMP(VPD_RW,v,2) == 0 || max == 0) {
716 if (SK_MEMCMP(VPD_RV,v,2) == 0) {
717 v += 3 + VPD_GET_VPD_LEN(v) + 3; /* skip VPD-W */
720 v += 3 + VPD_GET_VPD_LEN(v);
722 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
723 ("scanning '%c%c' len = %d\n",v[0],v[1],v[2]));
727 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL, ("not found\n"));
729 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
730 ("Key/Len Encoding error\n"));
737 * Move 'n' bytes. Begin with the last byte if 'n' is > 0,
738 * Start with the last byte if n is < 0.
742 static void vpd_move_para(
743 char *start, /* start of memory block */
744 char *end, /* end of memory block to move */
745 int n) /* number of bytes the memory block has to be moved */
748 int i; /* number of byte copied */
753 i = (int) (end - start + 1);
771 * setup the VPD keyword 'key' at 'ip'.
775 static void vpd_insert_key(
776 const char *key, /* keyword to insert */
777 const char *buf, /* buffer with the keyword value */
778 int len, /* length of the value string */
779 char *ip) /* inseration point */
783 p = (SK_VPD_KEY *) ip;
784 p->p_key[0] = key[0];
785 p->p_key[1] = key[1];
786 p->p_len = (unsigned char) len;
787 SK_MEMCPY(&p->p_val,buf,len);
791 * Setup the VPD end tag "RV" / "RW".
792 * Also correct the remaining space variables vpd_free_ro / vpd_free_rw.
797 static int vpd_mod_endtag(
798 SK_AC *pAC, /* common data base */
799 char *etp) /* end pointer input position */
806 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
807 ("VPD modify endtag at 0x%x = '%c%c'\n",etp,etp[0],etp[1]));
809 vpd_size = pAC->vpd.vpd_size;
811 p = (SK_VPD_KEY *) etp;
813 if (p->p_key[0] != 'R' || (p->p_key[1] != 'V' && p->p_key[1] != 'W')) {
814 /* something wrong here, encoding error */
815 SK_DBG_MSG(pAC,SK_DBGMOD_VPD,SK_DBGCAT_ERR | SK_DBGCAT_FATAL,
816 ("Encoding Error: invalid end tag\n"));
819 if (etp > pAC->vpd.vpd_buf + vpd_size/2) {
820 /* create "RW" tag */
821 p->p_len = (unsigned char)(pAC->vpd.vpd_buf+vpd_size-etp-3-1);
822 pAC->vpd.v.vpd_free_rw = (int) p->p_len;
823 i = pAC->vpd.v.vpd_free_rw;
827 /* create "RV" tag */
828 p->p_len = (unsigned char)(pAC->vpd.vpd_buf+vpd_size/2-etp-3);
829 pAC->vpd.v.vpd_free_ro = (int) p->p_len - 1;
832 for (i = 0, x = 0; i < vpd_size/2 - p->p_len; i++) {
833 x += pAC->vpd.vpd_buf[i];
835 p->p_val = (char) 0 - x;
836 i = pAC->vpd.v.vpd_free_ro;
848 * Insert a VPD keyword into the VPD buffer.
850 * The keyword 'key' is inserted at the position 'ip' in the
852 * The keywords behind the input position will
853 * be moved. The VPD end tag "RV" or "RW" is generated again.
856 * 2: value string was cut
857 * 4: VPD full, keyword was not written
862 SK_AC *pAC, /* common data base */
863 const char *key, /* keyword to insert */
864 const char *buf, /* buffer with the keyword value */
865 int len, /* length of the keyword value */
866 int type, /* VPD_RO_KEY or VPD_RW_KEY */
867 int op) /* operation to do: ADD_KEY or OWR_KEY */
870 char *etp; /* end tag position */
871 int free; /* remaining space in selected area */
872 char *ip; /* input position inside the VPD buffer */
873 int rtv; /* return code */
874 int head; /* additional haeder bytes to move */
875 int found; /* additinoal bytes if the keyword was found */
878 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
879 ("VPD setup para key = %s, val = %s\n",key,buf));
881 vpd_size = pAC->vpd.vpd_size;
885 if (type == VPD_RW_KEY) {
886 /* end tag is "RW" */
887 free = pAC->vpd.v.vpd_free_rw;
888 etp = pAC->vpd.vpd_buf + (vpd_size - free - 1 - 3);
891 /* end tag is "RV" */
892 free = pAC->vpd.v.vpd_free_ro;
893 etp = pAC->vpd.vpd_buf + (vpd_size/2 - free - 4);
895 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
896 ("Free RO = %d, Free RW = %d\n",
897 pAC->vpd.v.vpd_free_ro, pAC->vpd.v.vpd_free_rw));
902 if (vpd_find_para(pAC, key, &vp)) {
905 free += vp.p_len + 3;
906 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
907 ("Overwrite Key\n"));
911 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_CTRL,
921 if (len + 3 > free) {
923 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
924 ("VPD Buffer Overflow, keyword not written\n"));
930 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
931 ("VPD Buffer Full, Keyword was cut\n"));
934 vpd_move_para(ip + vp.p_len + found, etp+2, len-vp.p_len+head);
935 vpd_insert_key(key, buf, len, ip);
936 if (vpd_mod_endtag(pAC, etp + len - vp.p_len + head)) {
937 pAC->vpd.v.vpd_status &= ~VPD_VALID;
938 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
939 ("VPD Encoding Error\n"));
948 * Read the contents of the VPD EEPROM and copy it to the
949 * VPD buffer if not already done.
951 * return: A pointer to the vpd_status structure. The structure contains
954 SK_VPD_STATUS *VpdStat(
955 SK_AC *pAC, /* Adapters context */
956 SK_IOC IoC) /* IO Context */
958 if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
959 (void)VpdInit(pAC, IoC);
966 * Read the contents of the VPD EEPROM and copy it to the VPD
967 * buffer if not already done.
968 * Scan the VPD buffer for VPD keywords and create the VPD
969 * keyword list by copying the keywords to 'buf', all after
970 * each other and terminated with a '\0'.
972 * Exceptions: o The Resource Type ID String (product name) is called "Name"
973 * o The VPD end tags 'RV' and 'RW' are not listed
975 * The number of copied keywords is counted in 'elements'.
978 * 2: buffer overfull, one or more keywords are missing
981 * example values after returning:
983 * buf = "Name\0PN\0EC\0MN\0SN\0CP\0VF\0VL\0YA\0"
988 SK_AC *pAC, /* common data base */
989 SK_IOC IoC, /* IO Context */
990 char *buf, /* buffer where to copy the keywords */
991 int *len, /* buffer length */
992 int *elements) /* number of keywords returned */
997 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX, ("list VPD keys .. "));
999 if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
1000 if (VpdInit(pAC, IoC) != 0) {
1002 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1003 ("VPD Init Error, terminated\n"));
1008 if ((signed)strlen(VPD_NAME) + 1 <= *len) {
1009 v = pAC->vpd.vpd_buf;
1010 strcpy(buf,VPD_NAME);
1011 n = strlen(VPD_NAME) + 1;
1014 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX,
1015 ("'%c%c' ",v[0],v[1]));
1019 SK_DBG_MSG(pAC,SK_DBGMOD_VPD,SK_DBGCAT_ERR,
1020 ("buffer overflow\n"));
1024 v += 3 + VPD_GET_RES_LEN(v) + 3;
1026 /* exit when reaching the "RW" Tag */
1027 if (SK_MEMCMP(VPD_RW,v,2) == 0) {
1031 if (SK_MEMCMP(VPD_RV,v,2) == 0) {
1032 v += 3 + VPD_GET_VPD_LEN(v) + 3; /* skip VPD-W */
1041 v += 3 + VPD_GET_VPD_LEN(v);
1043 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX,
1044 ("'%c%c' ",v[0],v[1]));
1048 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1049 ("buffer overflow\n"));
1054 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX, ("\n"));
1061 * Read the contents of the VPD EEPROM and copy it to the
1062 * VPD buffer if not already done. Search for the VPD keyword
1063 * 'key' and copy its value to 'buf'. Add a terminating '\0'.
1064 * If the value does not fit into the buffer cut it after
1067 * returns 0: success
1068 * 1: keyword not found
1069 * 2: value string was cut
1070 * 3: VPD transfer timeout
1071 * 6: fatal VPD error
1074 SK_AC *pAC, /* common data base */
1075 SK_IOC IoC, /* IO Context */
1076 const char *key, /* keyword to read (e.g. "MN") */
1077 char *buf, /* buffer where to copy the keyword value */
1078 int *len) /* buffer length */
1082 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX, ("VPD read %s .. ", key));
1083 if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
1084 if (VpdInit(pAC, IoC) != 0) {
1086 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1087 ("VPD init error\n"));
1092 if ((p = vpd_find_para(pAC, key, &vp)) != NULL) {
1093 if (p->p_len > (*(unsigned *)len)-1) {
1094 p->p_len = *len - 1;
1096 SK_MEMCPY(buf, p->p_val, p->p_len);
1097 buf[p->p_len] = '\0';
1099 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_RX,
1100 ("%c%c%c%c.., len = %d\n",
1101 buf[0],buf[1],buf[2],buf[3],*len));
1105 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR, ("not found\n"));
1113 * Check whether a given key may be written
1116 * SK_TRUE Yes it may be written
1117 * SK_FALSE No it may be written
1119 SK_BOOL VpdMayWrite(
1120 char *key) /* keyword to write (allowed values "Yx", "Vx") */
1122 if ((*key != 'Y' && *key != 'V') ||
1123 key[1] < '0' || key[1] > 'Z' ||
1124 (key[1] > '9' && key[1] < 'A') || strlen(key) != 2) {
1132 * Read the contents of the VPD EEPROM and copy it to the VPD
1133 * buffer if not already done. Insert/overwrite the keyword 'key'
1134 * in the VPD buffer. Cut the keyword value if it does not fit
1135 * into the VPD read / write area.
1137 * returns 0: success
1138 * 2: value string was cut
1139 * 3: VPD transfer timeout
1140 * 4: VPD full, keyword was not written
1141 * 5: keyword cannot be written
1142 * 6: fatal VPD error
1145 SK_AC *pAC, /* common data base */
1146 SK_IOC IoC, /* IO Context */
1147 const char *key, /* keyword to write (allowed values "Yx", "Vx") */
1148 const char *buf) /* buffer where the keyword value can be read from */
1150 int len; /* length of the keyword to write */
1151 int rtv; /* return code */
1154 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX,
1155 ("VPD write %s = %s\n",key,buf));
1157 if ((*key != 'Y' && *key != 'V') ||
1158 key[1] < '0' || key[1] > 'Z' ||
1159 (key[1] > '9' && key[1] < 'A') || strlen(key) != 2) {
1161 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1162 ("illegal key tag, keyword not written\n"));
1166 if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
1167 if (VpdInit(pAC, IoC) != 0) {
1168 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1169 ("VPD init error\n"));
1176 if (len > VPD_MAX_LEN) {
1180 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1181 ("keyword too long, cut after %d bytes\n",VPD_MAX_LEN));
1183 if ((rtv2 = VpdSetupPara(pAC, key, buf, len, VPD_RW_KEY, OWR_KEY)) != 0) {
1184 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1185 ("VPD write error\n"));
1193 * Read the contents of the VPD EEPROM and copy it to the
1194 * VPD buffer if not already done. Remove the VPD keyword
1195 * 'key' from the VPD buffer.
1196 * Only the keywords in the read/write area can be deleted.
1197 * Keywords in the read only area cannot be deleted.
1199 * returns 0: success, keyword was removed
1200 * 1: keyword not found
1201 * 5: keyword cannot be deleted
1202 * 6: fatal VPD error
1205 SK_AC *pAC, /* common data base */
1206 SK_IOC IoC, /* IO Context */
1207 char *key) /* keyword to read (e.g. "MN") */
1213 vpd_size = pAC->vpd.vpd_size;
1215 SK_DBG_MSG(pAC,SK_DBGMOD_VPD,SK_DBGCAT_TX,("VPD delete key %s\n",key));
1216 if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
1217 if (VpdInit(pAC, IoC) != 0) {
1218 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1219 ("VPD init error\n"));
1224 if ((p = vpd_find_para(pAC, key, &vp)) != NULL) {
1225 if (p->p_val < pAC->vpd.vpd_buf + vpd_size/2) {
1226 /* try to delete read only keyword */
1227 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1228 ("cannot delete RO keyword\n"));
1232 etp = pAC->vpd.vpd_buf + (vpd_size-pAC->vpd.v.vpd_free_rw-1-3);
1234 vpd_move_para(vp.p_val+vp.p_len, etp+2,
1235 - ((int)(vp.p_len + 3)));
1236 if (vpd_mod_endtag(pAC, etp - vp.p_len - 3)) {
1237 pAC->vpd.v.vpd_status &= ~VPD_VALID;
1238 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1239 ("VPD encoding error\n"));
1244 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1245 ("keyword not found\n"));
1253 * If the VPD buffer contains valid data write the VPD
1254 * read/write area back to the VPD EEPROM.
1256 * returns 0: success
1257 * 3: VPD transfer timeout
1260 SK_AC *pAC, /* Adapters context */
1261 SK_IOC IoC) /* IO Context */
1265 vpd_size = pAC->vpd.vpd_size;
1267 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX, ("VPD update .. "));
1268 if ((pAC->vpd.v.vpd_status & VPD_VALID) != 0) {
1269 if (VpdTransferBlock(pAC, IoC, pAC->vpd.vpd_buf + vpd_size/2,
1270 vpd_size/2, vpd_size/2, VPD_WRITE) != vpd_size/2) {
1272 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1273 ("transfer timed out\n"));
1277 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX, ("done\n"));
1283 * Read the contents of the VPD EEPROM and copy it to the VPD buffer
1284 * if not already done. If the keyword "VF" is not present it will be
1285 * created and the error log message will be stored to this keyword.
1286 * If "VF" is not present the error log message will be stored to the
1287 * keyword "VL". "VL" will created or overwritten if "VF" is present.
1288 * The VPD read/write area is saved to the VPD EEPROM.
1290 * returns nothing, errors will be ignored.
1293 SK_AC *pAC, /* common data base */
1294 SK_IOC IoC, /* IO Context */
1295 char *msg) /* error log message */
1297 SK_VPD_PARA *v, vf; /* VF */
1300 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX,
1301 ("VPD error log msg %s\n", msg));
1302 if ((pAC->vpd.v.vpd_status & VPD_VALID) == 0) {
1303 if (VpdInit(pAC, IoC) != 0) {
1304 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_ERR,
1305 ("VPD init error\n"));
1311 if (len > VPD_MAX_LEN) {
1315 if ((v = vpd_find_para(pAC, VPD_VF, &vf)) != NULL) {
1316 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX, ("overwrite VL\n"));
1317 (void)VpdSetupPara(pAC, VPD_VL, msg, len, VPD_RW_KEY, OWR_KEY);
1320 SK_DBG_MSG(pAC, SK_DBGMOD_VPD, SK_DBGCAT_TX, ("write VF\n"));
1321 (void)VpdSetupPara(pAC, VPD_VF, msg, len, VPD_RW_KEY, ADD_KEY);
1324 (void)VpdUpdate(pAC, IoC);