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/init.h>
29 #include <linux/pci.h>
30 #include <asm/types.h>
31 #include <asm/resource.h>
32 
33 #include <asm/iSeries/HvCallPci.h>
34 #include <asm/iSeries/HvTypes.h>
35 #include <asm/iSeries/mf.h>
36 #include <asm/iSeries/LparData.h>
37 #include <asm/iSeries/HvCallPci.h>
38 //#include <asm/iSeries/iSeries_VpdInfo.h>
39 #include <asm/iSeries/iSeries_pci.h>
40 #include "pci.h"
41 
42 /************************************************/
43 /* Size of Bus VPD data                         */
44 /************************************************/
45 #define BUS_VPDSIZE      1024
46 /************************************************/
47 /* Bus Vpd Tags                                 */
48 /************************************************/
49 #define  VpdEndOfDataTag   0x78
50 #define  VpdEndOfAreaTag   0x79
51 #define  VpdIdStringTag    0x82
52 #define  VpdVendorAreaTag  0x84
53 /************************************************/
54 /* Mfg Area Tags                                */
55 /************************************************/
56 #define  VpdFruFlag       0x4647     // "FG"
57 #define  VpdFruFrameId    0x4649     // "FI"
58 #define  VpdSlotMapFormat 0x4D46     // "MF"
59 #define  VpdAsmPartNumber 0x504E     // "PN"
60 #define  VpdFruSerial     0x534E     // "SN"
61 #define  VpdSlotMap       0x534D     // "SM"
62 
63 /************************************************/
64 /* Structures of the areas                      */
65 /************************************************/
66 struct MfgVpdAreaStruct {
67 	u16 Tag;
68 	u8  TagLength;
69 	u8  AreaData1;
70 	u8  AreaData2;
71 };
72 typedef struct MfgVpdAreaStruct MfgArea;
73 #define MFG_ENTRY_SIZE   3
74 
75 struct SlotMapStruct {
76 	u8   AgentId;
77 	u8   SecondaryAgentId;
78 	u8   PhbId;
79 	char CardLocation[3];
80 	char Parms[8];
81 	char Reserved[2];
82 };
83 typedef struct SlotMapStruct SlotMap;
84 #define SLOT_ENTRY_SIZE   16
85 
86 /****************************************************************
87  *                                                              *
88  * Bus, Card, Board, FrameId, CardLocation.                     *
89  ****************************************************************/
iSeries_GetLocationData(struct pci_dev * PciDev)90 LocationData* iSeries_GetLocationData(struct pci_dev* PciDev)
91 {
92 	struct iSeries_Device_Node* DevNode = (struct iSeries_Device_Node*)PciDev->sysdata;
93 	LocationData* LocationPtr = (LocationData*)kmalloc(LOCATION_DATA_SIZE, GFP_KERNEL);
94 	if (LocationPtr == NULL) {
95 		printk("PCI: LocationData area allocation failed!\n");
96 		return NULL;
97 	}
98 	memset(LocationPtr,0,LOCATION_DATA_SIZE);
99 	LocationPtr->Bus              = ISERIES_BUS(DevNode);
100 	LocationPtr->Board            = DevNode->Board;
101 	LocationPtr->FrameId          = DevNode->FrameId;
102 	LocationPtr->Card             = PCI_SLOT(DevNode->DevFn);
103 	strcpy(&LocationPtr->CardLocation[0],&DevNode->CardLocation[0]);
104 	return LocationPtr;
105 }
106 
107 /************************************************************************/
108 /* Formats the device information.                                      */
109 /* - Pass in pci_dev* pointer to the device.                            */
110 /* - Pass in buffer to place the data.  Danger here is the buffer must  */
111 /*   be as big as the client says it is.   Should be at least 128 bytes.*/
112 /* Return will the length of the string data put in the buffer.         */
113 /* Format:                                                              */
114 /* PCI: Bus  0, Device 26, Vendor 0x12AE  Frame  1, Card  C10  Ethernet */
115 /* controller                                                           */
116 /************************************************************************/
iSeries_Device_Information(struct pci_dev * PciDev,char * Buffer,int BufferSize)117 int  iSeries_Device_Information(struct pci_dev* PciDev,char* Buffer, int BufferSize)
118 {
119 	struct iSeries_Device_Node* DevNode = (struct iSeries_Device_Node*)PciDev->sysdata;
120 	char*          BufPtr  = Buffer;
121 	int            LineLen = 0;
122 
123 	if (DevNode == NULL) {
124 		LineLen = sprintf(BufPtr+LineLen, "PCI: iSeries_Device_Information DevNode is NULL");
125 		return LineLen;
126 	}
127 
128 	if (BufferSize >= 128) {
129 		LineLen =  sprintf(BufPtr+LineLen,"PCI: Bus%3d, Device%3d, Vendor %04X ",
130 				   ISERIES_BUS(DevNode), PCI_SLOT(PciDev->devfn),PciDev->vendor);
131 
132 		LineLen += sprintf(BufPtr+LineLen,"Frame%3d, Card %4s  ", DevNode->FrameId,DevNode->CardLocation);
133 
134 		if (pci_class_name(PciDev->class >> 8) == 0) {
135 			LineLen += sprintf(BufPtr+LineLen,"0x%04X  ",(int)(PciDev->class >> 8));
136 		}
137 		else {
138 			LineLen += sprintf(BufPtr+LineLen,"%s",pci_class_name(PciDev->class >> 8) );
139 		}
140 	}
141 	return LineLen;
142 }
143 /************************************************************************/
144 /* Build a character string of the device location, Frame  1, Card  C10 */
145 /************************************************************************/
device_Location(struct pci_dev * PciDev,char * BufPtr)146 int   device_Location(struct pci_dev* PciDev,char* BufPtr)
147 {
148 	struct iSeries_Device_Node* DevNode = (struct iSeries_Device_Node*)PciDev->sysdata;
149 	return sprintf(BufPtr,"PCI: Bus%3d, AgentId%3d, Vendor %04X, Location %s",
150 		       DevNode->DsaAddr.busNumber,
151 		       DevNode->AgentId,
152 		       DevNode->Vendor,
153 		       DevNode->Location);
154 }
155 
156 /*****************************************************************/
157 /* Parse the Slot Area                                           */
158 /*****************************************************************/
iSeries_Parse_SlotArea(SlotMap * MapPtr,int MapLen,struct iSeries_Device_Node * DevNode)159 void  iSeries_Parse_SlotArea(SlotMap* MapPtr,int MapLen, struct iSeries_Device_Node* DevNode)
160 {
161 	int      SlotMapLen = MapLen;
162 	SlotMap* SlotMapPtr = MapPtr;
163 	/*************************************************************/
164 	/* Parse Slot label until we find the one requrested         */
165 	/*************************************************************/
166 	while (SlotMapLen > 0) {
167 		if ((SlotMapPtr->AgentId == DevNode->AgentId) &&
168 		    (SlotMapPtr->SecondaryAgentId == 0x10   ) ) {
169 			/*******************************************************/
170 			/* If Phb wasn't found, grab the entry first one found.*/
171 			/*******************************************************/
172 			if (DevNode->PhbId == 0xff) {
173 				DevNode->PhbId = SlotMapPtr->PhbId;
174 			}
175 			/**************************************************/
176 			/* Found it, extract the data.                    */
177 			/**************************************************/
178 			if (SlotMapPtr->PhbId == DevNode->PhbId ) {
179 	        		memcpy(&DevNode->CardLocation,&SlotMapPtr->CardLocation,3);
180 				DevNode->CardLocation[3]  = 0;
181 				break;
182 			}
183 		}
184 		/*********************************************************/
185 		/* Point to the next Slot                                */
186 		/*********************************************************/
187 		SlotMapPtr = (SlotMap*)((char*)SlotMapPtr+SLOT_ENTRY_SIZE);
188 		SlotMapLen -= SLOT_ENTRY_SIZE;
189 	}
190 }
191 
192 /*****************************************************************/
193 /* Parse the Mfg Area                                            */
194 /*****************************************************************/
iSeries_Parse_MfgArea(u8 * AreaData,int AreaLen,struct iSeries_Device_Node * DevNode)195 static void  iSeries_Parse_MfgArea(u8* AreaData,int AreaLen, struct iSeries_Device_Node* DevNode)
196 {
197 	MfgArea* MfgAreaPtr = (MfgArea*)AreaData;
198 	int      MfgAreaLen = AreaLen;
199 	u16      SlotMapFmt = 0;
200 
201 	/*************************************************************/
202 	/* Parse Mfg Data                                            */
203 	/*************************************************************/
204 	while (MfgAreaLen > 0) {
205 		int MfgTagLen  = MfgAreaPtr->TagLength;
206 		/*******************************************************/
207 		/* Frame ID         (FI 4649020310 )                   */
208 		/*******************************************************/
209 		if (MfgAreaPtr->Tag == VpdFruFrameId) {     /* FI  */
210 			DevNode->FrameId = MfgAreaPtr->AreaData1;
211 		}
212 		/*******************************************************/
213 		/* Slot Map Format  (MF 4D46020004 )                   */
214 		/*******************************************************/
215 		else if (MfgAreaPtr->Tag == VpdSlotMapFormat){   /* MF  */
216 			SlotMapFmt = (MfgAreaPtr->AreaData1*256)+(MfgAreaPtr->AreaData2);
217 		}
218 		/*******************************************************/
219 		/* Slot Map         (SM 534D90                         */
220 		/*******************************************************/
221 		else if (MfgAreaPtr->Tag == VpdSlotMap){         /* SM  */
222 			SlotMap*  SlotMapPtr;
223 			if (SlotMapFmt == 0x1004) SlotMapPtr = (SlotMap*)((char*)MfgAreaPtr+MFG_ENTRY_SIZE+1);
224  	    		else                     SlotMapPtr = (SlotMap*)((char*)MfgAreaPtr+MFG_ENTRY_SIZE);
225 	    		iSeries_Parse_SlotArea(SlotMapPtr,MfgTagLen, DevNode);
226 		}
227 		/*********************************************************/
228 		/* Point to the next Mfg Area                            */
229 		/* Use defined size, sizeof give wrong answer            */
230 		/*********************************************************/
231 		MfgAreaPtr = (MfgArea*)((char*)MfgAreaPtr + MfgTagLen + MFG_ENTRY_SIZE);
232 		MfgAreaLen -= (MfgTagLen + MFG_ENTRY_SIZE);
233 	}
234 }
235 
236 /*****************************************************************/
237 /* Look for "BUS".. Data is not Null terminated.                 */
238 /* PHBID of 0xFF indicates PHB was not found in VPD Data.        */
239 /*****************************************************************/
iSeries_Parse_PhbId(u8 * AreaPtr,int AreaLength)240 static int iSeries_Parse_PhbId(u8* AreaPtr,int AreaLength)
241 {
242 	u8*  PhbPtr  = AreaPtr;
243 	int  DataLen = AreaLength;
244 	char PhbId   = 0xFF;
245 	while (DataLen > 0) {
246 		if (*PhbPtr == 'B' && *(PhbPtr+1) == 'U' && *(PhbPtr+2) == 'S') {
247 			PhbPtr += 3;
248 			while(*PhbPtr == ' ') ++PhbPtr;
249 			PhbId = (*PhbPtr & 0x0F);
250 			break;
251         	}
252 		++PhbPtr;
253 		--DataLen;
254 	}
255 	return PhbId;
256 }
257 
258 /****************************************************************/
259 /* Parse out the VPD Areas                                      */
260 /****************************************************************/
iSeries_Parse_Vpd(u8 * VpdData,int VpdDataLen,struct iSeries_Device_Node * DevNode)261 static void  iSeries_Parse_Vpd(u8* VpdData, int VpdDataLen, struct iSeries_Device_Node* DevNode)
262 {
263 	u8*  TagPtr  = VpdData;
264 	int  DataLen = VpdDataLen-3;
265 	/*************************************************************/
266 	/* Parse the Areas                                           */
267 	/*************************************************************/
268 	while (*TagPtr != VpdEndOfAreaTag && DataLen > 0) {
269 		int  AreaLen   = *(TagPtr+1) + (*(TagPtr+2)*256);
270 		u8*  AreaData  = TagPtr+3;
271 
272 		if (*TagPtr == VpdIdStringTag) {
273 			DevNode->PhbId = iSeries_Parse_PhbId(AreaData,AreaLen);
274 		}
275 		else if (*TagPtr == VpdVendorAreaTag) {
276 	    		iSeries_Parse_MfgArea(AreaData,AreaLen,DevNode);
277 		}
278 		/*********************************************************
279 		 * Point to next Area.
280 		 *********************************************************/
281 		TagPtr  = AreaData + AreaLen;
282 		DataLen -= AreaLen;
283 	}
284 }
285 
286 /****************************************************************
287  *  iSeries_Get_Location_Code(struct iSeries_Device_Node*)      *
288  *
289  ****************************************************************/
iSeries_Get_Location_Code(struct iSeries_Device_Node * DevNode)290 void  iSeries_Get_Location_Code(struct iSeries_Device_Node* DevNode)
291 {
292 	int  BusVpdLen = 0;
293 	u8*  BusVpdPtr = (u8*)kmalloc(BUS_VPDSIZE, GFP_KERNEL);
294 	if (BusVpdPtr == NULL) {
295 		printk("PCI: Bus VPD Buffer allocation failure.\n");
296 		return;
297 	}
298 	BusVpdLen  = HvCallPci_getBusVpd(ISERIES_BUS(DevNode),REALADDR(BusVpdPtr),BUS_VPDSIZE);
299 	if (BusVpdLen == 0) {
300 		kfree(BusVpdPtr);
301 		printk("PCI: Bus VPD Buffer zero length.\n");
302 		return;
303 	}
304 	//printk("PCI: DevNode:0x%p BusVpdPtr:0x%p Length:%d\n",DevNode, BusVpdPtr, BusVpdLen);
305 	/*************************************************************/
306 	/* Make sure this is what I think it is                      */
307 	/*************************************************************/
308 	if (*BusVpdPtr != VpdIdStringTag) {               /*0x82     */
309 		printk("PCI: Bus VPD Buffer missing starting tag.\n");
310 		kfree(BusVpdPtr);
311 		return;
312 	}
313 	/***************************************************************/
314 	/***************************************************************/
315 	iSeries_Parse_Vpd(BusVpdPtr,BusVpdLen, DevNode);
316 	sprintf(DevNode->Location,"Frame%3d, Card %-4s",DevNode->FrameId,DevNode->CardLocation);
317 	kfree(BusVpdPtr);
318 }
319