iSeries_VpdInfo.c 9.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/************************************************************************/
/* File iSeries_vpdInfo.c created by Allan Trautman on Fri Feb  2 2001. */
/************************************************************************/
/* This code gets the card location of the hardware                     */
/* Copyright (C) 20yy  <Allan H Trautman> <IBM Corp>                    */
/*                                                                      */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or    */
/* (at your option) any later version.                                  */
/*                                                                      */
/* This program is distributed in the hope that it will be useful,      */ 
/* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
/* GNU General Public License for more details.                         */
/*                                                                      */
/* You should have received a copy of the GNU General Public License    */ 
/* along with this program; if not, write to the:                       */
/* Free Software Foundation, Inc.,                                      */ 
/* 59 Temple Place, Suite 330,                                          */ 
/* Boston, MA  02111-1307  USA                                          */
/************************************************************************/
/* Change Activity:                                                     */
/*   Created, Feb 2, 2001                                               */
/*   Ported to ppc64, August 20, 2001                                   */
/* End Change Activity                                                  */
/************************************************************************/
#include <linux/init.h>
#include <linux/pci.h>
#include <asm/types.h>
#include <asm/resource.h>

#include <asm/iSeries/HvCallPci.h>
#include <asm/iSeries/HvTypes.h>
#include <asm/iSeries/mf.h>
#include <asm/iSeries/LparData.h>
//#include <asm/iSeries/iSeries_VpdInfo.h>
#include <asm/iSeries/iSeries_pci.h>
#include "pci.h"

41 42 43
/*
 * Size of Bus VPD data
 */
44
#define BUS_VPDSIZE      1024
45 46 47
/*
 * Bus Vpd Tags
 */
48 49 50 51
#define  VpdEndOfDataTag   0x78
#define  VpdEndOfAreaTag   0x79
#define  VpdIdStringTag    0x82
#define  VpdVendorAreaTag  0x84
52 53 54
/*
 * Mfg Area Tags
 */
55 56 57 58 59 60 61
#define  VpdFruFlag       0x4647     // "FG"
#define  VpdFruFrameId    0x4649     // "FI"
#define  VpdSlotMapFormat 0x4D46     // "MF"
#define  VpdAsmPartNumber 0x504E     // "PN"
#define  VpdFruSerial     0x534E     // "SN"
#define  VpdSlotMap       0x534D     // "SM"

62 63 64
/*
 * Structures of the areas
 */
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
struct MfgVpdAreaStruct {
	u16 Tag;
	u8  TagLength;
	u8  AreaData1;
	u8  AreaData2;
};
typedef struct MfgVpdAreaStruct MfgArea;
#define MFG_ENTRY_SIZE   3

struct SlotMapStruct {
	u8   AgentId;
	u8   SecondaryAgentId;
	u8   PhbId;
	char CardLocation[3];
	char Parms[8];
	char Reserved[2];
}; 
typedef struct SlotMapStruct SlotMap;
#define SLOT_ENTRY_SIZE   16

85 86 87 88
/*
 * Bus, Card, Board, FrameId, CardLocation.
 */
LocationData* iSeries_GetLocationData(struct pci_dev *PciDev)
89
{
90 91 92 93 94
	struct iSeries_Device_Node *DevNode =
		(struct iSeries_Device_Node *)PciDev->sysdata;
	LocationData *LocationPtr =
		(LocationData *)kmalloc(LOCATION_DATA_SIZE, GFP_KERNEL);

95 96 97 98
	if (LocationPtr == NULL) {
		printk("PCI: LocationData area allocation failed!\n");
		return NULL;
	}
99 100 101 102 103 104
	memset(LocationPtr, 0, LOCATION_DATA_SIZE);
	LocationPtr->Bus = ISERIES_BUS(DevNode);
	LocationPtr->Board = DevNode->Board;
	LocationPtr->FrameId = DevNode->FrameId;
	LocationPtr->Card = PCI_SLOT(DevNode->DevFn);
	strcpy(&LocationPtr->CardLocation[0], &DevNode->CardLocation[0]);
105 106 107
	return LocationPtr;
}

108 109 110 111 112 113 114 115 116 117 118 119
/*
 * Formats the device information.
 * - Pass in pci_dev* pointer to the device.
 * - Pass in buffer to place the data.  Danger here is the buffer must
 *   be as big as the client says it is.   Should be at least 128 bytes.
 * Return will the length of the string data put in the buffer.
 * Format:
 * PCI: Bus  0, Device 26, Vendor 0x12AE  Frame  1, Card  C10  Ethernet
 * controller
 */
int iSeries_Device_Information(struct pci_dev *PciDev, char *buffer,
		int BufferSize)
120
{
121 122 123
	struct iSeries_Device_Node *DevNode =
		(struct iSeries_Device_Node *)PciDev->sysdata;
	int len;
124

125 126 127
	if (DevNode == NULL)
		return sprintf(buffer,
				"PCI: iSeries_Device_Information DevNode is NULL");
128

129 130
	if (BufferSize < 128)
		return 0;
131

132 133 134 135 136 137 138 139 140 141 142 143 144 145
	len = sprintf(buffer, "PCI: Bus%3d, Device%3d, Vendor %04X ",
			ISERIES_BUS(DevNode), PCI_SLOT(PciDev->devfn),
			PciDev->vendor);
	len += sprintf(buffer + len, "Frame%3d, Card %4s  ",
			DevNode->FrameId, DevNode->CardLocation);
#ifdef CONFIG_PCI
	if (pci_class_name(PciDev->class >> 8) == 0)
		len += sprintf(buffer + len, "0x%04X  ",
				(int)(PciDev->class >> 8));
	else
		len += sprintf(buffer + len, "%s",
				pci_class_name(PciDev->class >> 8));
#endif
	return len;
146
}
147 148 149 150 151

/*
 * Build a character string of the device location, Frame  1, Card  C10
 */
int device_Location(struct pci_dev *PciDev, char *BufPtr)
152
{
153 154 155 156 157
	struct iSeries_Device_Node *DevNode =
		(struct iSeries_Device_Node *)PciDev->sysdata;
	return sprintf(BufPtr, "PCI: Bus%3d, AgentId%3d, Vendor %04X, Location %s",
		       DevNode->DsaAddr.busNumber, DevNode->AgentId,
		       DevNode->Vendor, DevNode->Location);
158 159
}

160 161 162 163 164
/*
 * Parse the Slot Area
 */
void iSeries_Parse_SlotArea(SlotMap *MapPtr, int MapLen,
		struct iSeries_Device_Node *DevNode)
165
{
166 167 168 169 170 171
	int SlotMapLen = MapLen;
	SlotMap *SlotMapPtr = MapPtr;

	/*
	 * Parse Slot label until we find the one requrested
	 */
172 173
	while (SlotMapLen > 0) {
		if (SlotMapPtr->AgentId == DevNode->AgentId ) {
174 175 176 177
			/*
			 * If Phb wasn't found, grab the entry first one found.
			 */
			if (DevNode->PhbId == 0xff)
178
				DevNode->PhbId = SlotMapPtr->PhbId; 
179
			/* Found it, extract the data. */
180
			if (SlotMapPtr->PhbId == DevNode->PhbId ) {
181 182
	        		memcpy(&DevNode->CardLocation,
						&SlotMapPtr->CardLocation, 3);
183 184 185 186
				DevNode->CardLocation[3]  = 0;
				break;
			}
		}
187 188
		/* Point to the next Slot */
		SlotMapPtr = (SlotMap *)((char *)SlotMapPtr + SLOT_ENTRY_SIZE);
189 190 191 192
		SlotMapLen -= SLOT_ENTRY_SIZE;
	}
}

193 194 195 196 197
/*
 * Parse the Mfg Area
 */
static void iSeries_Parse_MfgArea(u8 *AreaData, int AreaLen,
		struct iSeries_Device_Node *DevNode)
198
{
199 200 201
	MfgArea *MfgAreaPtr = (MfgArea *)AreaData;
	int MfgAreaLen = AreaLen;
	u16 SlotMapFmt = 0;
202

203
	/* Parse Mfg Data */
204
	while (MfgAreaLen > 0) {
205 206 207
		int MfgTagLen = MfgAreaPtr->TagLength;
		/* Frame ID         (FI 4649020310 ) */
		if (MfgAreaPtr->Tag == VpdFruFrameId)		/* FI  */
208
			DevNode->FrameId = MfgAreaPtr->AreaData1;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
		/* Slot Map Format  (MF 4D46020004 ) */
		else if (MfgAreaPtr->Tag == VpdSlotMapFormat)	/* MF  */
			SlotMapFmt = (MfgAreaPtr->AreaData1 * 256)
				+ MfgAreaPtr->AreaData2;
		/* Slot Map         (SM 534D90 */
		else if (MfgAreaPtr->Tag == VpdSlotMap)	{	/* SM  */
			SlotMap *SlotMapPtr;

			if (SlotMapFmt == 0x1004)
				SlotMapPtr = (SlotMap *)((char *)MfgAreaPtr
						+ MFG_ENTRY_SIZE + 1);
 	    		else
				SlotMapPtr = (SlotMap *)((char *)MfgAreaPtr
						+ MFG_ENTRY_SIZE);
	    		iSeries_Parse_SlotArea(SlotMapPtr, MfgTagLen, DevNode);
224
		}
225 226 227 228 229 230
		/*
		 * Point to the next Mfg Area
		 * Use defined size, sizeof give wrong answer
		 */
		MfgAreaPtr = (MfgArea *)((char *)MfgAreaPtr + MfgTagLen
				+ MFG_ENTRY_SIZE);
231 232 233 234
		MfgAreaLen -= (MfgTagLen + MFG_ENTRY_SIZE); 
	}	
}

235 236 237 238 239
/*
 * Look for "BUS".. Data is not Null terminated.
 * PHBID of 0xFF indicates PHB was not found in VPD Data.
 */
static int iSeries_Parse_PhbId(u8 *AreaPtr, int AreaLength)
240
{
241 242 243 244
	u8 *PhbPtr = AreaPtr;
	int DataLen = AreaLength;
	char PhbId = 0xFF;                   

245
	while (DataLen > 0) {
246 247
		if ((*PhbPtr == 'B') && (*(PhbPtr + 1) == 'U')
				&& (*(PhbPtr + 2) == 'S')) {
248
			PhbPtr += 3;
249 250
			while (*PhbPtr == ' ')
				++PhbPtr;
251 252 253 254 255 256 257 258 259
			PhbId = (*PhbPtr & 0x0F);
			break;
        	}
		++PhbPtr;
		--DataLen;
	}
	return PhbId;
}

260 261 262 263 264
/*
 * Parse out the VPD Areas
 */
static void iSeries_Parse_Vpd(u8 *VpdData, int VpdDataLen,
		struct iSeries_Device_Node *DevNode)
265
{
266 267
	u8 *TagPtr = VpdData;
	int DataLen = VpdDataLen - 3;
268

269 270 271 272 273 274 275 276 277
	while ((*TagPtr != VpdEndOfAreaTag) && (DataLen > 0)) {
		int AreaLen = *(TagPtr + 1) + (*(TagPtr + 2) * 256);	
		u8 *AreaData  = TagPtr + 3;

		if (*TagPtr == VpdIdStringTag)
			DevNode->PhbId = iSeries_Parse_PhbId(AreaData, AreaLen);
		else if (*TagPtr == VpdVendorAreaTag)
	    		iSeries_Parse_MfgArea(AreaData, AreaLen, DevNode);
		/* Point to next Area. */
278 279 280 281 282
		TagPtr  = AreaData + AreaLen;
		DataLen -= AreaLen;
	}
}    

283
void iSeries_Get_Location_Code(struct iSeries_Device_Node *DevNode)
284
{
285 286 287
	int BusVpdLen = 0;
	u8 *BusVpdPtr = (u8 *)kmalloc(BUS_VPDSIZE, GFP_KERNEL);

288 289 290 291
	if (BusVpdPtr == NULL) {
		printk("PCI: Bus VPD Buffer allocation failure.\n");
		return;
	}
292 293
	BusVpdLen = HvCallPci_getBusVpd(ISERIES_BUS(DevNode),
			REALADDR(BusVpdPtr), BUS_VPDSIZE);
294 295 296 297 298
	if (BusVpdLen == 0) {
		kfree(BusVpdPtr);
		printk("PCI: Bus VPD Buffer zero length.\n");
		return;
	}
299 300 301
	/* printk("PCI: BusVpdPtr: %p, %d\n",BusVpdPtr, BusVpdLen); */
	/* Make sure this is what I think it is */
	if (*BusVpdPtr != VpdIdStringTag) {	/* 0x82 */
302 303 304 305 306
		printk("PCI: Bus VPD Buffer missing starting tag.\n");
		kfree(BusVpdPtr);
		return;
	}
	iSeries_Parse_Vpd(BusVpdPtr,BusVpdLen, DevNode);
307 308
	sprintf(DevNode->Location, "Frame%3d, Card %-4s", DevNode->FrameId,
			DevNode->CardLocation);
309 310
	kfree(BusVpdPtr);
}