]> pilppa.org Git - linux-2.6-omap-h63xx.git/blob - arch/ppc64/kernel/iSeries_VpdInfo.c
a82caf98d2e37135dffc875a264a2ad22b15893e
[linux-2.6-omap-h63xx.git] / arch / ppc64 / kernel / iSeries_VpdInfo.c
1 /************************************************************************/
2 /* File iSeries_vpdInfo.c created by Allan Trautman on Fri Feb  2 2001. */
3 /************************************************************************/
4 /* This code gets the card location of the hardware                     */
5 /* Copyright (C) 20yy  <Allan H Trautman> <IBM Corp>                    */
6 /*                                                                      */
7 /* This program is free software; you can redistribute it and/or modify */
8 /* it under the terms of the GNU General Public License as published by */
9 /* the Free Software Foundation; either version 2 of the License, or    */
10 /* (at your option) any later version.                                  */
11 /*                                                                      */
12 /* This program is distributed in the hope that it will be useful,      */ 
13 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
15 /* GNU General Public License for more details.                         */
16 /*                                                                      */
17 /* You should have received a copy of the GNU General Public License    */ 
18 /* along with this program; if not, write to the:                       */
19 /* Free Software Foundation, Inc.,                                      */ 
20 /* 59 Temple Place, Suite 330,                                          */ 
21 /* Boston, MA  02111-1307  USA                                          */
22 /************************************************************************/
23 /* Change Activity:                                                     */
24 /*   Created, Feb 2, 2001                                               */
25 /*   Ported to ppc64, August 20, 2001                                   */
26 /* End Change Activity                                                  */
27 /************************************************************************/
28 #include <linux/config.h>
29 #include <linux/init.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
32 #include <asm/types.h>
33 #include <asm/resource.h>
34
35 #include <asm/iSeries/HvCallPci.h>
36 #include <asm/iSeries/HvTypes.h>
37 #include <asm/iSeries/mf.h>
38 #include <asm/iSeries/iSeries_pci.h>
39 #include "pci.h"
40
41 /*
42  * Size of Bus VPD data
43  */
44 #define BUS_VPDSIZE      1024
45 /*
46  * Bus Vpd Tags
47  */
48 #define  VpdEndOfDataTag   0x78
49 #define  VpdEndOfAreaTag   0x79
50 #define  VpdIdStringTag    0x82
51 #define  VpdVendorAreaTag  0x84
52 /*
53  * Mfg Area Tags
54  */
55 #define  VpdFruFlag       0x4647     // "FG"
56 #define  VpdFruFrameId    0x4649     // "FI"
57 #define  VpdSlotMapFormat 0x4D46     // "MF"
58 #define  VpdAsmPartNumber 0x504E     // "PN"
59 #define  VpdFruSerial     0x534E     // "SN"
60 #define  VpdSlotMap       0x534D     // "SM"
61
62 /*
63  * Structures of the areas
64  */
65 struct MfgVpdAreaStruct {
66         u16 Tag;
67         u8  TagLength;
68         u8  AreaData1;
69         u8  AreaData2;
70 };
71 typedef struct MfgVpdAreaStruct MfgArea;
72 #define MFG_ENTRY_SIZE   3
73
74 struct SlotMapStruct {
75         u8   AgentId;
76         u8   SecondaryAgentId;
77         u8   PhbId;
78         char CardLocation[3];
79         char Parms[8];
80         char Reserved[2];
81 }; 
82 typedef struct SlotMapStruct SlotMap;
83 #define SLOT_ENTRY_SIZE   16
84
85 /*
86  * Formats the device information.
87  * - Pass in pci_dev* pointer to the device.
88  * - Pass in buffer to place the data.  Danger here is the buffer must
89  *   be as big as the client says it is.   Should be at least 128 bytes.
90  * Return will the length of the string data put in the buffer.
91  * Format:
92  * PCI: Bus  0, Device 26, Vendor 0x12AE  Frame  1, Card  C10  Ethernet
93  * controller
94  */
95 int iSeries_Device_Information(struct pci_dev *PciDev, char *buffer,
96                 int BufferSize)
97 {
98         struct iSeries_Device_Node *DevNode =
99                 (struct iSeries_Device_Node *)PciDev->sysdata;
100         int len;
101
102         if (DevNode == NULL)
103                 return sprintf(buffer,
104                                 "PCI: iSeries_Device_Information DevNode is NULL");
105
106         if (BufferSize < 128)
107                 return 0;
108
109         len = sprintf(buffer, "PCI: Bus%3d, Device%3d, Vendor %04X ",
110                         ISERIES_BUS(DevNode), PCI_SLOT(PciDev->devfn),
111                         PciDev->vendor);
112         len += sprintf(buffer + len, "Frame%3d, Card %4s  ",
113                         DevNode->FrameId, DevNode->CardLocation);
114 #ifdef CONFIG_PCI
115         if (pci_class_name(PciDev->class >> 8) == 0)
116                 len += sprintf(buffer + len, "0x%04X  ",
117                                 (int)(PciDev->class >> 8));
118         else
119                 len += sprintf(buffer + len, "%s",
120                                 pci_class_name(PciDev->class >> 8));
121 #endif
122         return len;
123 }
124
125 /*
126  * Parse the Slot Area
127  */
128 void iSeries_Parse_SlotArea(SlotMap *MapPtr, int MapLen,
129                 struct iSeries_Device_Node *DevNode)
130 {
131         int SlotMapLen = MapLen;
132         SlotMap *SlotMapPtr = MapPtr;
133
134         /*
135          * Parse Slot label until we find the one requrested
136          */
137         while (SlotMapLen > 0) {
138                 if (SlotMapPtr->AgentId == DevNode->AgentId ) {
139                         /*
140                          * If Phb wasn't found, grab the entry first one found.
141                          */
142                         if (DevNode->PhbId == 0xff)
143                                 DevNode->PhbId = SlotMapPtr->PhbId; 
144                         /* Found it, extract the data. */
145                         if (SlotMapPtr->PhbId == DevNode->PhbId ) {
146                                 memcpy(&DevNode->CardLocation,
147                                                 &SlotMapPtr->CardLocation, 3);
148                                 DevNode->CardLocation[3]  = 0;
149                                 break;
150                         }
151                 }
152                 /* Point to the next Slot */
153                 SlotMapPtr = (SlotMap *)((char *)SlotMapPtr + SLOT_ENTRY_SIZE);
154                 SlotMapLen -= SLOT_ENTRY_SIZE;
155         }
156 }
157
158 /*
159  * Parse the Mfg Area
160  */
161 static void iSeries_Parse_MfgArea(u8 *AreaData, int AreaLen,
162                 struct iSeries_Device_Node *DevNode)
163 {
164         MfgArea *MfgAreaPtr = (MfgArea *)AreaData;
165         int MfgAreaLen = AreaLen;
166         u16 SlotMapFmt = 0;
167
168         /* Parse Mfg Data */
169         while (MfgAreaLen > 0) {
170                 int MfgTagLen = MfgAreaPtr->TagLength;
171                 /* Frame ID         (FI 4649020310 ) */
172                 if (MfgAreaPtr->Tag == VpdFruFrameId)           /* FI  */
173                         DevNode->FrameId = MfgAreaPtr->AreaData1;
174                 /* Slot Map Format  (MF 4D46020004 ) */
175                 else if (MfgAreaPtr->Tag == VpdSlotMapFormat)   /* MF  */
176                         SlotMapFmt = (MfgAreaPtr->AreaData1 * 256)
177                                 + MfgAreaPtr->AreaData2;
178                 /* Slot Map         (SM 534D90 */
179                 else if (MfgAreaPtr->Tag == VpdSlotMap) {       /* SM  */
180                         SlotMap *SlotMapPtr;
181
182                         if (SlotMapFmt == 0x1004)
183                                 SlotMapPtr = (SlotMap *)((char *)MfgAreaPtr
184                                                 + MFG_ENTRY_SIZE + 1);
185                         else
186                                 SlotMapPtr = (SlotMap *)((char *)MfgAreaPtr
187                                                 + MFG_ENTRY_SIZE);
188                         iSeries_Parse_SlotArea(SlotMapPtr, MfgTagLen, DevNode);
189                 }
190                 /*
191                  * Point to the next Mfg Area
192                  * Use defined size, sizeof give wrong answer
193                  */
194                 MfgAreaPtr = (MfgArea *)((char *)MfgAreaPtr + MfgTagLen
195                                 + MFG_ENTRY_SIZE);
196                 MfgAreaLen -= (MfgTagLen + MFG_ENTRY_SIZE); 
197         }       
198 }
199
200 /*
201  * Look for "BUS".. Data is not Null terminated.
202  * PHBID of 0xFF indicates PHB was not found in VPD Data.
203  */
204 static int iSeries_Parse_PhbId(u8 *AreaPtr, int AreaLength)
205 {
206         u8 *PhbPtr = AreaPtr;
207         int DataLen = AreaLength;
208         char PhbId = 0xFF;                   
209
210         while (DataLen > 0) {
211                 if ((*PhbPtr == 'B') && (*(PhbPtr + 1) == 'U')
212                                 && (*(PhbPtr + 2) == 'S')) {
213                         PhbPtr += 3;
214                         while (*PhbPtr == ' ')
215                                 ++PhbPtr;
216                         PhbId = (*PhbPtr & 0x0F);
217                         break;
218                 }
219                 ++PhbPtr;
220                 --DataLen;
221         }
222         return PhbId;
223 }
224
225 /*
226  * Parse out the VPD Areas
227  */
228 static void iSeries_Parse_Vpd(u8 *VpdData, int VpdDataLen,
229                 struct iSeries_Device_Node *DevNode)
230 {
231         u8 *TagPtr = VpdData;
232         int DataLen = VpdDataLen - 3;
233
234         while ((*TagPtr != VpdEndOfAreaTag) && (DataLen > 0)) {
235                 int AreaLen = *(TagPtr + 1) + (*(TagPtr + 2) * 256);    
236                 u8 *AreaData  = TagPtr + 3;
237
238                 if (*TagPtr == VpdIdStringTag)
239                         DevNode->PhbId = iSeries_Parse_PhbId(AreaData, AreaLen);
240                 else if (*TagPtr == VpdVendorAreaTag)
241                         iSeries_Parse_MfgArea(AreaData, AreaLen, DevNode);
242                 /* Point to next Area. */
243                 TagPtr  = AreaData + AreaLen;
244                 DataLen -= AreaLen;
245         }
246 }    
247
248 void iSeries_Get_Location_Code(struct iSeries_Device_Node *DevNode)
249 {
250         int BusVpdLen = 0;
251         u8 *BusVpdPtr = (u8 *)kmalloc(BUS_VPDSIZE, GFP_KERNEL);
252
253         if (BusVpdPtr == NULL) {
254                 printk("PCI: Bus VPD Buffer allocation failure.\n");
255                 return;
256         }
257         BusVpdLen = HvCallPci_getBusVpd(ISERIES_BUS(DevNode),
258                                         ISERIES_HV_ADDR(BusVpdPtr),
259                                         BUS_VPDSIZE);
260         if (BusVpdLen == 0) {
261                 kfree(BusVpdPtr);
262                 printk("PCI: Bus VPD Buffer zero length.\n");
263                 return;
264         }
265         /* printk("PCI: BusVpdPtr: %p, %d\n",BusVpdPtr, BusVpdLen); */
266         /* Make sure this is what I think it is */
267         if (*BusVpdPtr != VpdIdStringTag) {     /* 0x82 */
268                 printk("PCI: Bus VPD Buffer missing starting tag.\n");
269                 kfree(BusVpdPtr);
270                 return;
271         }
272         iSeries_Parse_Vpd(BusVpdPtr,BusVpdLen, DevNode);
273         sprintf(DevNode->Location, "Frame%3d, Card %-4s", DevNode->FrameId,
274                         DevNode->CardLocation);
275         kfree(BusVpdPtr);
276 }