]> git.karo-electronics.de Git - karo-tx-linux.git/blob - arch/powerpc/platforms/iseries/vpdinfo.c
[POWERPC] iSeries: Clean up and simplify vdpinfo.c
[karo-tx-linux.git] / arch / powerpc / platforms / iseries / vpdinfo.c
1 /*
2  * This code gets the card location of the hardware
3  * Copyright (C) 2001  <Allan H Trautman> <IBM Corp>
4  * Copyright (C) 2005  Stephen Rothwel, IBM Corp
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the:
18  * Free Software Foundation, Inc.,
19  * 59 Temple Place, Suite 330,
20  * Boston, MA  02111-1307  USA
21  *
22  * Change Activity:
23  *   Created, Feb 2, 2001
24  *   Ported to ppc64, August 20, 2001
25  * End Change Activity
26  */
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/pci.h>
30
31 #include <asm/types.h>
32 #include <asm/resource.h>
33 #include <asm/abs_addr.h>
34 #include <asm/iseries/hv_types.h>
35
36 #include "pci.h"
37 #include "call_pci.h"
38
39 /*
40  * Size of Bus VPD data
41  */
42 #define BUS_VPDSIZE      1024
43
44 /*
45  * Bus Vpd Tags
46  */
47 #define VPD_END_OF_AREA         0x79
48 #define VPD_ID_STRING           0x82
49 #define VPD_VENDOR_AREA         0x84
50
51 /*
52  * Mfg Area Tags
53  */
54 #define VPD_FRU_FRAME_ID        0x4649  /* "FI" */
55 #define VPD_SLOT_MAP_FORMAT     0x4D46  /* "MF" */
56 #define VPD_SLOT_MAP            0x534D  /* "SM" */
57
58 /*
59  * Structures of the areas
60  */
61 struct mfg_vpd_area {
62         u16     tag;
63         u8      length;
64         u8      data1;
65         u8      data2;
66 };
67 #define MFG_ENTRY_SIZE   3
68
69 struct slot_map {
70         u8      agent;
71         u8      secondary_agent;
72         u8      phb;
73         char    card_location[3];
74         char    parms[8];
75         char    reserved[2];
76 };
77 #define SLOT_ENTRY_SIZE   16
78
79 /*
80  * Parse the Slot Area
81  */
82 static void __init iseries_parse_slot_area(struct slot_map *map, int len,
83                 HvAgentId agent, u8 *phb, char card[4])
84 {
85         /*
86          * Parse Slot label until we find the one requested
87          */
88         while (len > 0) {
89                 if (map->agent == agent) {
90                         /*
91                          * If Phb wasn't found, grab the entry first one found.
92                          */
93                         if (*phb == 0xff)
94                                 *phb = map->phb;
95                         /* Found it, extract the data. */
96                         if (map->phb == *phb) {
97                                 memcpy(card, &map->card_location, 3);
98                                 card[3]  = 0;
99                                 break;
100                         }
101                 }
102                 /* Point to the next Slot */
103                 map = (struct slot_map *)((char *)map + SLOT_ENTRY_SIZE);
104                 len -= SLOT_ENTRY_SIZE;
105         }
106 }
107
108 /*
109  * Parse the Mfg Area
110  */
111 static void __init iseries_parse_mfg_area(struct mfg_vpd_area *area, int len,
112                 HvAgentId agent, u8 *phb, u8 *frame, char card[4])
113 {
114         u16 slot_map_fmt = 0;
115
116         /* Parse Mfg Data */
117         while (len > 0) {
118                 int mfg_tag_len = area->length;
119                 /* Frame ID         (FI 4649020310 ) */
120                 if (area->tag == VPD_FRU_FRAME_ID)
121                         *frame = area->data1;
122                 /* Slot Map Format  (MF 4D46020004 ) */
123                 else if (area->tag == VPD_SLOT_MAP_FORMAT)
124                         slot_map_fmt = (area->data1 * 256)
125                                 + area->data2;
126                 /* Slot Map         (SM 534D90 */
127                 else if (area->tag == VPD_SLOT_MAP) {
128                         struct slot_map *slot_map;
129
130                         if (slot_map_fmt == 0x1004)
131                                 slot_map = (struct slot_map *)((char *)area
132                                                 + MFG_ENTRY_SIZE + 1);
133                         else
134                                 slot_map = (struct slot_map *)((char *)area
135                                                 + MFG_ENTRY_SIZE);
136                         iseries_parse_slot_area(slot_map, mfg_tag_len,
137                                         agent, phb, card);
138                 }
139                 /*
140                  * Point to the next Mfg Area
141                  * Use defined size, sizeof give wrong answer
142                  */
143                 area = (struct mfg_vpd_area *)((char *)area + mfg_tag_len
144                                 + MFG_ENTRY_SIZE);
145                 len -= (mfg_tag_len + MFG_ENTRY_SIZE);
146         }
147 }
148
149 /*
150  * Look for "BUS".. Data is not Null terminated.
151  * PHBID of 0xFF indicates PHB was not found in VPD Data.
152  */
153 static u8 __init iseries_parse_phbid(u8 *area, int len)
154 {
155         while (len > 0) {
156                 if ((*area == 'B') && (*(area + 1) == 'U')
157                                 && (*(area + 2) == 'S')) {
158                         area += 3;
159                         while (*area == ' ')
160                                 area++;
161                         return *area & 0x0F;
162                 }
163                 area++;
164                 len--;
165         }
166         return 0xff;
167 }
168
169 /*
170  * Parse out the VPD Areas
171  */
172 static void __init iseries_parse_vpd(u8 *data, int data_len,
173                 HvAgentId agent, u8 *frame, char card[4])
174 {
175         u8 phb = 0xff;
176
177         while (data_len > 0) {
178                 int len;
179                 u8 tag = *data;
180
181                 if (tag == VPD_END_OF_AREA)
182                         break;
183                 len = *(data + 1) + (*(data + 2) * 256);
184                 data += 3;
185                 data_len -= 3;
186                 if (tag == VPD_ID_STRING)
187                         phb = iseries_parse_phbid(data, len);
188                 else if (tag == VPD_VENDOR_AREA)
189                         iseries_parse_mfg_area((struct mfg_vpd_area *)data, len,
190                                         agent, &phb, frame, card);
191                 /* Point to next Area. */
192                 data += len;
193                 data_len -= len;
194         }
195 }
196
197 static int __init iseries_get_location_code(u16 bus, HvAgentId agent,
198                 u8 *frame, char card[4])
199 {
200         int status = 0;
201         int bus_vpd_len = 0;
202         u8 *bus_vpd = kmalloc(BUS_VPDSIZE, GFP_KERNEL);
203
204         if (bus_vpd == NULL) {
205                 printk("PCI: Bus VPD Buffer allocation failure.\n");
206                 return 0;
207         }
208         bus_vpd_len = HvCallPci_getBusVpd(bus, iseries_hv_addr(bus_vpd),
209                                         BUS_VPDSIZE);
210         if (bus_vpd_len == 0) {
211                 printk("PCI: Bus VPD Buffer zero length.\n");
212                 goto out_free;
213         }
214         /* printk("PCI: bus_vpd: %p, %d\n",bus_vpd, bus_vpd_len); */
215         /* Make sure this is what I think it is */
216         if (*bus_vpd != VPD_ID_STRING) {
217                 printk("PCI: Bus VPD Buffer missing starting tag.\n");
218                 goto out_free;
219         }
220         iseries_parse_vpd(bus_vpd, bus_vpd_len, agent, frame, card);
221         status = 1;
222 out_free:
223         kfree(bus_vpd);
224         return status;
225 }
226
227 /*
228  * Prints the device information.
229  * - Pass in pci_dev* pointer to the device.
230  * - Pass in the device count
231  *
232  * Format:
233  * PCI: Bus  0, Device 26, Vendor 0x12AE  Frame  1, Card  C10  Ethernet
234  * controller
235  */
236 void __init iseries_device_information(struct pci_dev *pdev, int count,
237                 u16 bus, HvSubBusNumber subbus)
238 {
239         u8 frame = 0;
240         char card[4];
241         HvAgentId agent;
242
243         agent = ISERIES_PCI_AGENTID(ISERIES_GET_DEVICE_FROM_SUBBUS(subbus),
244                         ISERIES_GET_FUNCTION_FROM_SUBBUS(subbus));
245
246         if (iseries_get_location_code(bus, agent, &frame, card)) {
247                 printk("%d. PCI: Bus%3d, Device%3d, Vendor %04X Frame%3d, "
248                         "Card %4s  0x%04X\n", count, bus,
249                         PCI_SLOT(pdev->devfn), pdev->vendor, frame,
250                         card, (int)(pdev->class >> 8));
251         }
252 }