blob: 24716725263f0e7869158291a33e5b781584229a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Compaq Hot Plug Controller Driver
3 *
4 * Copyright (C) 1995,2001 Compaq Computer Corporation
5 * Copyright (C) 2001 Greg Kroah-Hartman (greg@kroah.com)
6 * Copyright (C) 2001 IBM Corp.
7 *
8 * All rights reserved.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful, but
16 * WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
18 * NON INFRINGEMENT. See the GNU General Public License for more
19 * details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 * Send feedback to <greg@kroah.com>
26 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/module.h>
30#include <linux/kernel.h>
31#include <linux/types.h>
32#include <linux/slab.h>
33#include <linux/workqueue.h>
34#include <linux/proc_fs.h>
35#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070036#include <linux/pci_hotplug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "../pci.h"
38#include "cpqphp.h"
39#include "cpqphp_nvram.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41
42u8 cpqhp_nic_irq;
43u8 cpqhp_disk_irq;
44
45static u16 unused_IRQ;
46
47/*
48 * detect_HRT_floating_pointer
49 *
50 * find the Hot Plug Resource Table in the specified region of memory.
51 *
52 */
53static void __iomem *detect_HRT_floating_pointer(void __iomem *begin, void __iomem *end)
54{
55 void __iomem *fp;
56 void __iomem *endp;
57 u8 temp1, temp2, temp3, temp4;
58 int status = 0;
59
60 endp = (end - sizeof(struct hrt) + 1);
61
62 for (fp = begin; fp <= endp; fp += 16) {
63 temp1 = readb(fp + SIG0);
64 temp2 = readb(fp + SIG1);
65 temp3 = readb(fp + SIG2);
66 temp4 = readb(fp + SIG3);
67 if (temp1 == '$' &&
68 temp2 == 'H' &&
69 temp3 == 'R' &&
70 temp4 == 'T') {
71 status = 1;
72 break;
73 }
74 }
75
76 if (!status)
77 fp = NULL;
78
79 dbg("Discovered Hotplug Resource Table at %p\n", fp);
80 return fp;
81}
82
83
Alex Chiang861fefb2009-03-31 09:23:11 -060084int cpqhp_configure_device (struct controller* ctrl, struct pci_func* func)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
86 unsigned char bus;
87 struct pci_bus *child;
88 int num;
89
90 if (func->pci_dev == NULL)
Alex Chiang12a9da02009-03-31 09:24:17 -060091 func->pci_dev = pci_get_bus_and_slot(func->bus,PCI_DEVFN(func->device, func->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
93 /* No pci device, we need to create it then */
94 if (func->pci_dev == NULL) {
95 dbg("INFO: pci_dev still null\n");
96
97 num = pci_scan_slot(ctrl->pci_dev->bus, PCI_DEVFN(func->device, func->function));
98 if (num)
99 pci_bus_add_devices(ctrl->pci_dev->bus);
100
Alex Chiang12a9da02009-03-31 09:24:17 -0600101 func->pci_dev = pci_get_bus_and_slot(func->bus, PCI_DEVFN(func->device, func->function));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (func->pci_dev == NULL) {
103 dbg("ERROR: pci_dev still null\n");
104 return 0;
105 }
106 }
107
108 if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
Yinghai Lu45087872012-05-17 18:51:13 -0700109 int max;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
111 child = (struct pci_bus*) pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
Yinghai Lu45087872012-05-17 18:51:13 -0700112 max = pci_do_scan_bus(child);
113 pci_bus_update_busn_res_end(child, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
115
Alex Chiang12a9da02009-03-31 09:24:17 -0600116 pci_dev_put(func->pci_dev);
117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return 0;
119}
120
121
Alex Chiang861fefb2009-03-31 09:23:11 -0600122int cpqhp_unconfigure_device(struct pci_func* func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 int j;
Alex Chiang861fefb2009-03-31 09:23:11 -0600125
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800126 dbg("%s: bus/dev/func = %x/%x/%x\n", __func__, func->bus, func->device, func->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 for (j=0; j<8 ; j++) {
Alex Chiang12a9da02009-03-31 09:24:17 -0600129 struct pci_dev* temp = pci_get_bus_and_slot(func->bus, PCI_DEVFN(func->device, j));
130 if (temp) {
131 pci_dev_put(temp);
Yinghai Lu210647a2012-02-25 13:54:20 -0800132 pci_stop_and_remove_bus_device(temp);
Alex Chiang12a9da02009-03-31 09:24:17 -0600133 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135 return 0;
136}
137
138static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
139{
140 u32 vendID = 0;
141
142 if (pci_bus_read_config_dword (bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
143 return -1;
144 if (vendID == 0xffffffff)
145 return -1;
146 return pci_bus_read_config_dword (bus, devfn, offset, value);
147}
148
149
150/*
151 * cpqhp_set_irq
152 *
153 * @bus_num: bus number of PCI device
154 * @dev_num: device number of PCI device
155 * @slot: pointer to u8 where slot number will be returned
156 */
157int cpqhp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
158{
159 int rc = 0;
160
161 if (cpqhp_legacy_mode) {
162 struct pci_dev *fakedev;
163 struct pci_bus *fakebus;
164 u16 temp_word;
165
166 fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
167 fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
168 if (!fakedev || !fakebus) {
169 kfree(fakedev);
170 kfree(fakebus);
171 return -ENOMEM;
172 }
173
174 fakedev->devfn = dev_num << 3;
175 fakedev->bus = fakebus;
176 fakebus->number = bus_num;
177 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800178 __func__, dev_num, bus_num, int_pin, irq_num);
Bjorn Helgaas98d33332008-12-09 16:11:41 -0700179 rc = pcibios_set_irq_routing(fakedev, int_pin - 1, irq_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 kfree(fakedev);
181 kfree(fakebus);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800182 dbg("%s: rc %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (!rc)
184 return !rc;
185
Alex Chiang427438c2009-03-31 09:23:16 -0600186 /* set the Edge Level Control Register (ELCR) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 temp_word = inb(0x4d0);
188 temp_word |= inb(0x4d1) << 8;
189
190 temp_word |= 0x01 << irq_num;
191
Alex Chiang427438c2009-03-31 09:23:16 -0600192 /* This should only be for x86 as it sets the Edge Level
193 * Control Register
194 */
195 outb((u8) (temp_word & 0xFF), 0x4d0); outb((u8) ((temp_word &
196 0xFF00) >> 8), 0x4d1); rc = 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
198 return rc;
199}
200
201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 * dev_num)
203{
204 u16 tdevice;
205 u32 work;
206 u8 tbus;
207
208 ctrl->pci_bus->number = bus_num;
209
210 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
Alex Chiang427438c2009-03-31 09:23:16 -0600211 /* Scan for access first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
213 continue;
214 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
Alex Chiang427438c2009-03-31 09:23:16 -0600215 /* Yep we got one. Not a bridge ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
217 *dev_num = tdevice;
218 dbg("found it !\n");
219 return 0;
220 }
221 }
222 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
Alex Chiang427438c2009-03-31 09:23:16 -0600223 /* Scan for access first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
225 continue;
226 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
Alex Chiang427438c2009-03-31 09:23:16 -0600227 /* Yep we got one. bridge ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
229 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
Alex Chiange3265ea2009-03-31 09:24:07 -0600230 /* XXX: no recursion, wtf? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
Alex Chiange3265ea2009-03-31 09:24:07 -0600232 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234 }
235
236 return -1;
237}
238
239
240static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
241{
Alex Chiangb019ee62009-03-31 09:24:02 -0600242 int loop, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 u32 work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 u8 tbus, tdevice, tslot;
245
Alex Chiangb019ee62009-03-31 09:24:02 -0600246 len = cpqhp_routing_table_length();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 for (loop = 0; loop < len; ++loop) {
Alex Chiangb019ee62009-03-31 09:24:02 -0600248 tbus = cpqhp_routing_table->slots[loop].bus;
249 tdevice = cpqhp_routing_table->slots[loop].devfn;
250 tslot = cpqhp_routing_table->slots[loop].slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
252 if (tslot == slot) {
253 *bus_num = tbus;
254 *dev_num = tdevice;
255 ctrl->pci_bus->number = tbus;
256 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
Alex Chiangb019ee62009-03-31 09:24:02 -0600257 if (!nobridge || (work == 0xffffffff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
261 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
262 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
263
264 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
265 pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
266 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
267 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
268 *bus_num = tbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 return 0;
270 }
Alex Chiangb019ee62009-03-31 09:24:02 -0600271 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 return -1;
276}
277
278
279int cpqhp_get_bus_dev (struct controller *ctrl, u8 * bus_num, u8 * dev_num, u8 slot)
280{
Alex Chiang427438c2009-03-31 09:23:16 -0600281 /* plain (bridges allowed) */
282 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285
Alex Chiang427438c2009-03-31 09:23:16 -0600286/* More PCI configuration routines; this time centered around hotplug
287 * controller
288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290
291/*
292 * cpqhp_save_config
293 *
294 * Reads configuration for all slots in a PCI bus and saves info.
295 *
296 * Note: For non-hot plug busses, the slot # saved is the device #
297 *
298 * returns 0 if success
299 */
300int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
301{
302 long rc;
303 u8 class_code;
304 u8 header_type;
305 u32 ID;
306 u8 secondary_bus;
307 struct pci_func *new_slot;
308 int sub_bus;
309 int FirstSupported;
310 int LastSupported;
311 int max_functions;
312 int function;
313 u8 DevError;
314 int device = 0;
315 int cloop = 0;
316 int stop_it;
317 int index;
318
Alex Chiang427438c2009-03-31 09:23:16 -0600319 /* Decide which slots are supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
321 if (is_hot_plug) {
Alex Chiang427438c2009-03-31 09:23:16 -0600322 /*
323 * is_hot_plug is the slot mask
324 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 FirstSupported = is_hot_plug >> 4;
326 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
327 } else {
328 FirstSupported = 0;
329 LastSupported = 0x1F;
330 }
331
Alex Chiang427438c2009-03-31 09:23:16 -0600332 /* Save PCI configuration space for all devices in supported slots */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 ctrl->pci_bus->number = busnumber;
334 for (device = FirstSupported; device <= LastSupported; device++) {
335 ID = 0xFFFFFFFF;
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600336 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600338 if (ID == 0xFFFFFFFF) {
339 if (is_hot_plug) {
340 /* Setup slot structure with entry for empty
341 * slot
342 */
343 new_slot = cpqhp_slot_create(busnumber);
344 if (new_slot == NULL)
345 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 new_slot->bus = (u8) busnumber;
348 new_slot->device = (u8) device;
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600349 new_slot->function = 0;
350 new_slot->is_a_board = 0;
351 new_slot->presence_save = 0;
352 new_slot->switch_save = 0;
353 }
354 continue;
355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600357 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
358 if (rc)
359 return rc;
360
361 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
362 if (rc)
363 return rc;
364
365 /* If multi-function device, set max_functions to 8 */
366 if (header_type & 0x80)
367 max_functions = 8;
368 else
369 max_functions = 1;
370
371 function = 0;
372
373 do {
374 DevError = 0;
375 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
376 /* Recurse the subordinate bus
377 * get the subordinate bus number
378 */
379 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
380 if (rc) {
381 return rc;
382 } else {
383 sub_bus = (int) secondary_bus;
384
385 /* Save secondary bus cfg spc
386 * with this recursive call.
387 */
388 rc = cpqhp_save_config(ctrl, sub_bus, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 if (rc)
390 return rc;
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600391 ctrl->pci_bus->number = busnumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600395 index = 0;
396 new_slot = cpqhp_slot_find(busnumber, device, index++);
397 while (new_slot &&
398 (new_slot->function != (u8) function))
399 new_slot = cpqhp_slot_find(busnumber, device, index++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600401 if (!new_slot) {
402 /* Setup slot structure. */
403 new_slot = cpqhp_slot_create(busnumber);
404 if (new_slot == NULL)
405 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 new_slot->bus = (u8) busnumber;
409 new_slot->device = (u8) device;
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600410 new_slot->function = (u8) function;
411 new_slot->is_a_board = 1;
412 new_slot->switch_save = 0x10;
413 /* In case of unsupported board */
414 new_slot->status = DevError;
Alex Chiang12a9da02009-03-31 09:24:17 -0600415 new_slot->pci_dev = pci_get_bus_and_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600416
417 for (cloop = 0; cloop < 0x20; cloop++) {
418 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
419 if (rc)
420 return rc;
421 }
422
Alex Chiang12a9da02009-03-31 09:24:17 -0600423 pci_dev_put(new_slot->pci_dev);
424
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600425 function++;
426
427 stop_it = 0;
428
429 /* this loop skips to the next present function
430 * reading in Class Code and Header type.
431 */
432 while ((function < max_functions) && (!stop_it)) {
433 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
434 if (ID == 0xFFFFFFFF) {
435 function++;
436 continue;
437 }
438 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
439 if (rc)
440 return rc;
441
442 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
443 if (rc)
444 return rc;
445
446 stop_it++;
447 }
448
449 } while (function < max_functions);
Alex Chiang427438c2009-03-31 09:23:16 -0600450 } /* End of FOR loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600452 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
455
456/*
457 * cpqhp_save_slot_config
458 *
459 * Saves configuration info for all PCI devices in a given slot
460 * including subordinate busses.
461 *
462 * returns 0 if success
463 */
464int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
465{
466 long rc;
467 u8 class_code;
468 u8 header_type;
469 u32 ID;
470 u8 secondary_bus;
471 int sub_bus;
472 int max_functions;
Alex Chiangde86ae12009-03-31 09:23:46 -0600473 int function = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 int cloop = 0;
475 int stop_it;
476
477 ID = 0xFFFFFFFF;
478
479 ctrl->pci_bus->number = new_slot->bus;
480 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
481
Alex Chiangde86ae12009-03-31 09:23:46 -0600482 if (ID == 0xFFFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 2;
Alex Chiangde86ae12009-03-31 09:23:46 -0600484
485 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
486 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
487
488 if (header_type & 0x80) /* Multi-function device */
489 max_functions = 8;
490 else
491 max_functions = 1;
492
493 while (function < max_functions) {
494 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
495 /* Recurse the subordinate bus */
496 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
497
498 sub_bus = (int) secondary_bus;
499
500 /* Save the config headers for the secondary
501 * bus.
502 */
503 rc = cpqhp_save_config(ctrl, sub_bus, 0);
504 if (rc)
505 return(rc);
506 ctrl->pci_bus->number = new_slot->bus;
507
508 }
509
510 new_slot->status = 0;
511
512 for (cloop = 0; cloop < 0x20; cloop++)
513 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
514
515 function++;
516
517 stop_it = 0;
518
519 /* this loop skips to the next present function
520 * reading in the Class Code and the Header type.
521 */
522 while ((function < max_functions) && (!stop_it)) {
523 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
524
525 if (ID == 0xFFFFFFFF)
526 function++;
527 else {
528 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
529 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
530 stop_it++;
531 }
532 }
533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 }
535
536 return 0;
537}
538
539
540/*
541 * cpqhp_save_base_addr_length
542 *
543 * Saves the length of all base address registers for the
544 * specified slot. this is for hot plug REPLACE
545 *
546 * returns 0 if success
547 */
548int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func * func)
549{
550 u8 cloop;
551 u8 header_type;
552 u8 secondary_bus;
553 u8 type;
554 int sub_bus;
555 u32 temp_register;
556 u32 base;
557 u32 rc;
558 struct pci_func *next;
559 int index = 0;
560 struct pci_bus *pci_bus = ctrl->pci_bus;
561 unsigned int devfn;
562
563 func = cpqhp_slot_find(func->bus, func->device, index++);
564
565 while (func != NULL) {
566 pci_bus->number = func->bus;
567 devfn = PCI_DEVFN(func->device, func->function);
568
Alex Chiang427438c2009-03-31 09:23:16 -0600569 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
571
572 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
574
575 sub_bus = (int) secondary_bus;
576
577 next = cpqhp_slot_list[sub_bus];
578
579 while (next != NULL) {
580 rc = cpqhp_save_base_addr_length(ctrl, next);
581 if (rc)
582 return rc;
583
584 next = next->next;
585 }
586 pci_bus->number = func->bus;
587
Alex Chiang427438c2009-03-31 09:23:16 -0600588 /* FIXME: this loop is duplicated in the non-bridge
589 * case. The two could be rolled together Figure out
590 * IO and memory base lengths
591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
593 temp_register = 0xFFFFFFFF;
594 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
595 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
Alex Chiang427438c2009-03-31 09:23:16 -0600596 /* If this register is implemented */
597 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -0600599 /* IO base
600 * set base = amount of IO space
601 * requested
602 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 base = base & 0xFFFFFFFE;
604 base = (~base) + 1;
605
606 type = 1;
607 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600608 /* memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 base = base & 0xFFFFFFF0;
610 base = (~base) + 1;
611
612 type = 0;
613 }
614 } else {
615 base = 0x0L;
616 type = 0;
617 }
618
Alex Chiang427438c2009-03-31 09:23:16 -0600619 /* Save information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 func->base_length[(cloop - 0x10) >> 2] =
621 base;
622 func->base_type[(cloop - 0x10) >> 2] = type;
623
Alex Chiang427438c2009-03-31 09:23:16 -0600624 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Alex Chiang427438c2009-03-31 09:23:16 -0600626 } else if ((header_type & 0x7F) == 0x00) {
627 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
629 temp_register = 0xFFFFFFFF;
630 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
631 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
632
Alex Chiang427438c2009-03-31 09:23:16 -0600633 /* If this register is implemented */
634 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -0600636 /* IO base
637 * base = amount of IO space
638 * requested
639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 base = base & 0xFFFFFFFE;
641 base = (~base) + 1;
642
643 type = 1;
644 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600645 /* memory base
646 * base = amount of memory
647 * space requested
648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 base = base & 0xFFFFFFF0;
650 base = (~base) + 1;
651
652 type = 0;
653 }
654 } else {
655 base = 0x0L;
656 type = 0;
657 }
658
Alex Chiang427438c2009-03-31 09:23:16 -0600659 /* Save information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 func->base_length[(cloop - 0x10) >> 2] = base;
661 func->base_type[(cloop - 0x10) >> 2] = type;
662
Alex Chiang427438c2009-03-31 09:23:16 -0600663 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Alex Chiang427438c2009-03-31 09:23:16 -0600665 } else { /* Some other unknown header type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
Alex Chiang427438c2009-03-31 09:23:16 -0600668 /* find the next device in this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 func = cpqhp_slot_find(func->bus, func->device, index++);
670 }
671
672 return(0);
673}
674
675
676/*
677 * cpqhp_save_used_resources
678 *
679 * Stores used resource information for existing boards. this is
680 * for boards that were in the system when this driver was loaded.
681 * this function is for hot plug ADD
682 *
683 * returns 0 if success
684 */
685int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func * func)
686{
687 u8 cloop;
688 u8 header_type;
689 u8 secondary_bus;
690 u8 temp_byte;
691 u8 b_base;
692 u8 b_length;
693 u16 command;
694 u16 save_command;
695 u16 w_base;
696 u16 w_length;
697 u32 temp_register;
698 u32 save_base;
699 u32 base;
700 int index = 0;
701 struct pci_resource *mem_node;
702 struct pci_resource *p_mem_node;
703 struct pci_resource *io_node;
704 struct pci_resource *bus_node;
705 struct pci_bus *pci_bus = ctrl->pci_bus;
706 unsigned int devfn;
707
708 func = cpqhp_slot_find(func->bus, func->device, index++);
709
710 while ((func != NULL) && func->is_a_board) {
711 pci_bus->number = func->bus;
712 devfn = PCI_DEVFN(func->device, func->function);
713
Alex Chiang427438c2009-03-31 09:23:16 -0600714 /* Save the command register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
716
Alex Chiang427438c2009-03-31 09:23:16 -0600717 /* disable card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 command = 0x00;
719 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
720
Alex Chiang427438c2009-03-31 09:23:16 -0600721 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
723
Alex Chiang427438c2009-03-31 09:23:16 -0600724 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
725 /* Clear Bridge Control Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 command = 0x00;
727 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
728 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
729 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
730
731 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
732 if (!bus_node)
733 return -ENOMEM;
734
735 bus_node->base = secondary_bus;
736 bus_node->length = temp_byte - secondary_bus + 1;
737
738 bus_node->next = func->bus_head;
739 func->bus_head = bus_node;
740
Alex Chiang427438c2009-03-31 09:23:16 -0600741 /* Save IO base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
743 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
744
745 if ((b_base <= b_length) && (save_command & 0x01)) {
746 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
747 if (!io_node)
748 return -ENOMEM;
749
750 io_node->base = (b_base & 0xF0) << 8;
751 io_node->length = (b_length - b_base + 0x10) << 8;
752
753 io_node->next = func->io_head;
754 func->io_head = io_node;
755 }
756
Alex Chiang427438c2009-03-31 09:23:16 -0600757 /* Save memory base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
759 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
760
761 if ((w_base <= w_length) && (save_command & 0x02)) {
762 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
763 if (!mem_node)
764 return -ENOMEM;
765
766 mem_node->base = w_base << 16;
767 mem_node->length = (w_length - w_base + 0x10) << 16;
768
769 mem_node->next = func->mem_head;
770 func->mem_head = mem_node;
771 }
772
Alex Chiang427438c2009-03-31 09:23:16 -0600773 /* Save prefetchable memory base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
775 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
776
777 if ((w_base <= w_length) && (save_command & 0x02)) {
778 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
779 if (!p_mem_node)
780 return -ENOMEM;
781
782 p_mem_node->base = w_base << 16;
783 p_mem_node->length = (w_length - w_base + 0x10) << 16;
784
785 p_mem_node->next = func->p_mem_head;
786 func->p_mem_head = p_mem_node;
787 }
Alex Chiang427438c2009-03-31 09:23:16 -0600788 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
790 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
791
792 temp_register = 0xFFFFFFFF;
793 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
794 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
795
796 temp_register = base;
797
Alex Chiang427438c2009-03-31 09:23:16 -0600798 /* If this register is implemented */
799 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (((base & 0x03L) == 0x01)
801 && (save_command & 0x01)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600802 /* IO base
803 * set temp_register = amount
804 * of IO space requested
805 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 temp_register = base & 0xFFFFFFFE;
807 temp_register = (~temp_register) + 1;
808
809 io_node = kmalloc(sizeof(*io_node),
810 GFP_KERNEL);
811 if (!io_node)
812 return -ENOMEM;
813
814 io_node->base =
815 save_base & (~0x03L);
816 io_node->length = temp_register;
817
818 io_node->next = func->io_head;
819 func->io_head = io_node;
820 } else
821 if (((base & 0x0BL) == 0x08)
822 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600823 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 temp_register = base & 0xFFFFFFF0;
825 temp_register = (~temp_register) + 1;
826
827 p_mem_node = kmalloc(sizeof(*p_mem_node),
828 GFP_KERNEL);
829 if (!p_mem_node)
830 return -ENOMEM;
831
832 p_mem_node->base = save_base & (~0x0FL);
833 p_mem_node->length = temp_register;
834
835 p_mem_node->next = func->p_mem_head;
836 func->p_mem_head = p_mem_node;
837 } else
838 if (((base & 0x0BL) == 0x00)
839 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600840 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 temp_register = base & 0xFFFFFFF0;
842 temp_register = (~temp_register) + 1;
843
844 mem_node = kmalloc(sizeof(*mem_node),
845 GFP_KERNEL);
846 if (!mem_node)
847 return -ENOMEM;
848
849 mem_node->base = save_base & (~0x0FL);
850 mem_node->length = temp_register;
851
852 mem_node->next = func->mem_head;
853 func->mem_head = mem_node;
854 } else
855 return(1);
856 }
Alex Chiang427438c2009-03-31 09:23:16 -0600857 } /* End of base register loop */
858 /* Standard header */
859 } else if ((header_type & 0x7F) == 0x00) {
860 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
862 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
863
864 temp_register = 0xFFFFFFFF;
865 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
866 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
867
868 temp_register = base;
869
Alex Chiang427438c2009-03-31 09:23:16 -0600870 /* If this register is implemented */
871 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (((base & 0x03L) == 0x01)
873 && (save_command & 0x01)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600874 /* IO base
875 * set temp_register = amount
876 * of IO space requested
877 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 temp_register = base & 0xFFFFFFFE;
879 temp_register = (~temp_register) + 1;
880
881 io_node = kmalloc(sizeof(*io_node),
882 GFP_KERNEL);
883 if (!io_node)
884 return -ENOMEM;
885
886 io_node->base = save_base & (~0x01L);
887 io_node->length = temp_register;
888
889 io_node->next = func->io_head;
890 func->io_head = io_node;
891 } else
892 if (((base & 0x0BL) == 0x08)
893 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600894 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 temp_register = base & 0xFFFFFFF0;
896 temp_register = (~temp_register) + 1;
897
898 p_mem_node = kmalloc(sizeof(*p_mem_node),
899 GFP_KERNEL);
900 if (!p_mem_node)
901 return -ENOMEM;
902
903 p_mem_node->base = save_base & (~0x0FL);
904 p_mem_node->length = temp_register;
905
906 p_mem_node->next = func->p_mem_head;
907 func->p_mem_head = p_mem_node;
908 } else
909 if (((base & 0x0BL) == 0x00)
910 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600911 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 temp_register = base & 0xFFFFFFF0;
913 temp_register = (~temp_register) + 1;
914
915 mem_node = kmalloc(sizeof(*mem_node),
916 GFP_KERNEL);
917 if (!mem_node)
918 return -ENOMEM;
919
920 mem_node->base = save_base & (~0x0FL);
921 mem_node->length = temp_register;
922
923 mem_node->next = func->mem_head;
924 func->mem_head = mem_node;
925 } else
926 return(1);
927 }
Alex Chiang427438c2009-03-31 09:23:16 -0600928 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
Alex Chiang427438c2009-03-31 09:23:16 -0600931 /* find the next device in this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 func = cpqhp_slot_find(func->bus, func->device, index++);
933 }
934
Alex Chiang4aabb582009-03-31 09:23:52 -0600935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936}
937
938
939/*
940 * cpqhp_configure_board
941 *
942 * Copies saved configuration information to one slot.
943 * this is called recursively for bridge devices.
944 * this is for hot plug REPLACE!
945 *
946 * returns 0 if success
947 */
948int cpqhp_configure_board(struct controller *ctrl, struct pci_func * func)
949{
950 int cloop;
951 u8 header_type;
952 u8 secondary_bus;
953 int sub_bus;
954 struct pci_func *next;
955 u32 temp;
956 u32 rc;
957 int index = 0;
958 struct pci_bus *pci_bus = ctrl->pci_bus;
959 unsigned int devfn;
960
961 func = cpqhp_slot_find(func->bus, func->device, index++);
962
963 while (func != NULL) {
964 pci_bus->number = func->bus;
965 devfn = PCI_DEVFN(func->device, func->function);
966
Alex Chiang427438c2009-03-31 09:23:16 -0600967 /* Start at the top of config space so that the control
968 * registers are programmed last
969 */
Alex Chiang4aabb582009-03-31 09:23:52 -0600970 for (cloop = 0x3C; cloop > 0; cloop -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972
973 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
974
Alex Chiang427438c2009-03-31 09:23:16 -0600975 /* If this is a bridge device, restore subordinate devices */
976 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
978
979 sub_bus = (int) secondary_bus;
980
981 next = cpqhp_slot_list[sub_bus];
982
983 while (next != NULL) {
984 rc = cpqhp_configure_board(ctrl, next);
985 if (rc)
986 return rc;
987
988 next = next->next;
989 }
990 } else {
991
Alex Chiang427438c2009-03-31 09:23:16 -0600992 /* Check all the base Address Registers to make sure
993 * they are the same. If not, the board is different.
994 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 for (cloop = 16; cloop < 40; cloop += 4) {
997 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
998
999 if (temp != func->config_space[cloop >> 2]) {
1000 dbg("Config space compare failure!!! offset = %x\n", cloop);
1001 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
1002 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1003 return 1;
1004 }
1005 }
1006 }
1007
1008 func->configured = 1;
1009
1010 func = cpqhp_slot_find(func->bus, func->device, index++);
1011 }
1012
1013 return 0;
1014}
1015
1016
1017/*
1018 * cpqhp_valid_replace
1019 *
1020 * this function checks to see if a board is the same as the
1021 * one it is replacing. this check will detect if the device's
1022 * vendor or device id's are the same
1023 *
1024 * returns 0 if the board is the same nonzero otherwise
1025 */
1026int cpqhp_valid_replace(struct controller *ctrl, struct pci_func * func)
1027{
1028 u8 cloop;
1029 u8 header_type;
1030 u8 secondary_bus;
1031 u8 type;
1032 u32 temp_register = 0;
1033 u32 base;
1034 u32 rc;
1035 struct pci_func *next;
1036 int index = 0;
1037 struct pci_bus *pci_bus = ctrl->pci_bus;
1038 unsigned int devfn;
1039
1040 if (!func->is_a_board)
1041 return(ADD_NOT_SUPPORTED);
1042
1043 func = cpqhp_slot_find(func->bus, func->device, index++);
1044
1045 while (func != NULL) {
1046 pci_bus->number = func->bus;
1047 devfn = PCI_DEVFN(func->device, func->function);
1048
1049 pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1050
Alex Chiang427438c2009-03-31 09:23:16 -06001051 /* No adapter present */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 if (temp_register == 0xFFFFFFFF)
1053 return(NO_ADAPTER_PRESENT);
1054
1055 if (temp_register != func->config_space[0])
1056 return(ADAPTER_NOT_SAME);
1057
Alex Chiang427438c2009-03-31 09:23:16 -06001058 /* Check for same revision number and class code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1060
Alex Chiang427438c2009-03-31 09:23:16 -06001061 /* Adapter not the same */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 if (temp_register != func->config_space[0x08 >> 2])
1063 return(ADAPTER_NOT_SAME);
1064
Alex Chiang427438c2009-03-31 09:23:16 -06001065 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1067
Alex Chiang427438c2009-03-31 09:23:16 -06001068 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1069 /* In order to continue checking, we must program the
1070 * bus registers in the bridge to respond to accesses
1071 * for its subordinate bus(es)
1072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
1074 temp_register = func->config_space[0x18 >> 2];
1075 pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1076
1077 secondary_bus = (temp_register >> 8) & 0xFF;
1078
1079 next = cpqhp_slot_list[secondary_bus];
1080
1081 while (next != NULL) {
1082 rc = cpqhp_valid_replace(ctrl, next);
1083 if (rc)
1084 return rc;
1085
1086 next = next->next;
1087 }
1088
1089 }
Alex Chiang427438c2009-03-31 09:23:16 -06001090 /* Check to see if it is a standard config header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
Alex Chiang427438c2009-03-31 09:23:16 -06001092 /* Check subsystem vendor and ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1094
1095 if (temp_register != func->config_space[0x2C >> 2]) {
Alex Chiang427438c2009-03-31 09:23:16 -06001096 /* If it's a SMART-2 and the register isn't
1097 * filled in, ignore the difference because
1098 * they just have an old rev of the firmware
1099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 if (!((func->config_space[0] == 0xAE100E11)
1101 && (temp_register == 0x00L)))
1102 return(ADAPTER_NOT_SAME);
1103 }
Alex Chiang427438c2009-03-31 09:23:16 -06001104 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1106 temp_register = 0xFFFFFFFF;
1107 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
1108 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
Alex Chiang427438c2009-03-31 09:23:16 -06001109
1110 /* If this register is implemented */
1111 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -06001113 /* IO base
1114 * set base = amount of IO
1115 * space requested
1116 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 base = base & 0xFFFFFFFE;
1118 base = (~base) + 1;
1119
1120 type = 1;
1121 } else {
Alex Chiang427438c2009-03-31 09:23:16 -06001122 /* memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 base = base & 0xFFFFFFF0;
1124 base = (~base) + 1;
1125
1126 type = 0;
1127 }
1128 } else {
1129 base = 0x0L;
1130 type = 0;
1131 }
1132
Alex Chiang427438c2009-03-31 09:23:16 -06001133 /* Check information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (func->base_length[(cloop - 0x10) >> 2] != base)
1135 return(ADAPTER_NOT_SAME);
1136
1137 if (func->base_type[(cloop - 0x10) >> 2] != type)
1138 return(ADAPTER_NOT_SAME);
1139
Alex Chiang427438c2009-03-31 09:23:16 -06001140 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Alex Chiang427438c2009-03-31 09:23:16 -06001142 } /* End of (type 0 config space) else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 else {
Alex Chiang427438c2009-03-31 09:23:16 -06001144 /* this is not a type 0 or 1 config space header so
1145 * we don't know how to do it
1146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return(DEVICE_TYPE_NOT_SUPPORTED);
1148 }
1149
Alex Chiang427438c2009-03-31 09:23:16 -06001150 /* Get the next function */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 func = cpqhp_slot_find(func->bus, func->device, index++);
1152 }
1153
1154
1155 return 0;
1156}
1157
1158
1159/*
1160 * cpqhp_find_available_resources
1161 *
1162 * Finds available memory, IO, and IRQ resources for programming
1163 * devices which may be added to the system
1164 * this function is for hot plug ADD!
1165 *
1166 * returns 0 if success
Alex Chiang861fefb2009-03-31 09:23:11 -06001167 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1169{
1170 u8 temp;
1171 u8 populated_slot;
1172 u8 bridged_slot;
1173 void __iomem *one_slot;
1174 void __iomem *rom_resource_table;
1175 struct pci_func *func = NULL;
1176 int i = 10, index;
1177 u32 temp_dword, rc;
1178 struct pci_resource *mem_node;
1179 struct pci_resource *p_mem_node;
1180 struct pci_resource *io_node;
1181 struct pci_resource *bus_node;
1182
1183 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1184 dbg("rom_resource_table = %p\n", rom_resource_table);
1185
Alex Chiang4aabb582009-03-31 09:23:52 -06001186 if (rom_resource_table == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 return -ENODEV;
Alex Chiang4aabb582009-03-31 09:23:52 -06001188
Alex Chiang427438c2009-03-31 09:23:16 -06001189 /* Sum all resources and setup resource maps */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1191 dbg("unused_IRQ = %x\n", unused_IRQ);
1192
1193 temp = 0;
1194 while (unused_IRQ) {
1195 if (unused_IRQ & 1) {
1196 cpqhp_disk_irq = temp;
1197 break;
1198 }
1199 unused_IRQ = unused_IRQ >> 1;
1200 temp++;
1201 }
1202
1203 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1204 unused_IRQ = unused_IRQ >> 1;
1205 temp++;
1206
1207 while (unused_IRQ) {
1208 if (unused_IRQ & 1) {
1209 cpqhp_nic_irq = temp;
1210 break;
1211 }
1212 unused_IRQ = unused_IRQ >> 1;
1213 temp++;
1214 }
1215
1216 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1217 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1218
1219 temp = 0;
1220
Alex Chiang4aabb582009-03-31 09:23:52 -06001221 if (!cpqhp_nic_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 cpqhp_nic_irq = ctrl->cfgspc_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Alex Chiang4aabb582009-03-31 09:23:52 -06001224 if (!cpqhp_disk_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 cpqhp_disk_irq = ctrl->cfgspc_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1228
1229 rc = compaq_nvram_load(rom_start, ctrl);
1230 if (rc)
1231 return rc;
1232
1233 one_slot = rom_resource_table + sizeof (struct hrt);
1234
1235 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1236 dbg("number_of_entries = %d\n", i);
1237
1238 if (!readb(one_slot + SECONDARY_BUS))
1239 return 1;
1240
1241 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1242
1243 while (i && readb(one_slot + SECONDARY_BUS)) {
1244 u8 dev_func = readb(one_slot + DEV_FUNC);
1245 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1246 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1247 u8 max_bus = readb(one_slot + MAX_BUS);
1248 u16 io_base = readw(one_slot + IO_BASE);
1249 u16 io_length = readw(one_slot + IO_LENGTH);
1250 u16 mem_base = readw(one_slot + MEM_BASE);
1251 u16 mem_length = readw(one_slot + MEM_LENGTH);
1252 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1253 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1254
1255 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1256 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1257 primary_bus, secondary_bus, max_bus);
1258
Alex Chiang427438c2009-03-31 09:23:16 -06001259 /* If this entry isn't for our controller's bus, ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (primary_bus != ctrl->bus) {
1261 i--;
1262 one_slot += sizeof (struct slot_rt);
1263 continue;
1264 }
Alex Chiang427438c2009-03-31 09:23:16 -06001265 /* find out if this entry is for an occupied slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 ctrl->pci_bus->number = primary_bus;
1267 pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1268 dbg("temp_D_word = %x\n", temp_dword);
1269
1270 if (temp_dword != 0xFFFFFFFF) {
1271 index = 0;
1272 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1273
1274 while (func && (func->function != (dev_func & 0x07))) {
1275 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1276 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1277 }
1278
Alex Chiang427438c2009-03-31 09:23:16 -06001279 /* If we can't find a match, skip this table entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 if (!func) {
1281 i--;
1282 one_slot += sizeof (struct slot_rt);
1283 continue;
1284 }
Alex Chiang427438c2009-03-31 09:23:16 -06001285 /* this may not work and shouldn't be used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 if (secondary_bus != primary_bus)
1287 bridged_slot = 1;
1288 else
1289 bridged_slot = 0;
1290
1291 populated_slot = 1;
1292 } else {
1293 populated_slot = 0;
1294 bridged_slot = 0;
1295 }
1296
1297
Alex Chiang427438c2009-03-31 09:23:16 -06001298 /* If we've got a valid IO base, use it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 temp_dword = io_base + io_length;
1301
1302 if ((io_base) && (temp_dword < 0x10000)) {
1303 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1304 if (!io_node)
1305 return -ENOMEM;
1306
1307 io_node->base = io_base;
1308 io_node->length = io_length;
1309
1310 dbg("found io_node(base, length) = %x, %x\n",
1311 io_node->base, io_node->length);
1312 dbg("populated slot =%d \n", populated_slot);
1313 if (!populated_slot) {
1314 io_node->next = ctrl->io_head;
1315 ctrl->io_head = io_node;
1316 } else {
1317 io_node->next = func->io_head;
1318 func->io_head = io_node;
1319 }
1320 }
1321
Alex Chiang427438c2009-03-31 09:23:16 -06001322 /* If we've got a valid memory base, use it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 temp_dword = mem_base + mem_length;
1324 if ((mem_base) && (temp_dword < 0x10000)) {
1325 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
1326 if (!mem_node)
1327 return -ENOMEM;
1328
1329 mem_node->base = mem_base << 16;
1330
1331 mem_node->length = mem_length << 16;
1332
1333 dbg("found mem_node(base, length) = %x, %x\n",
1334 mem_node->base, mem_node->length);
1335 dbg("populated slot =%d \n", populated_slot);
1336 if (!populated_slot) {
1337 mem_node->next = ctrl->mem_head;
1338 ctrl->mem_head = mem_node;
1339 } else {
1340 mem_node->next = func->mem_head;
1341 func->mem_head = mem_node;
1342 }
1343 }
1344
Alex Chiang427438c2009-03-31 09:23:16 -06001345 /* If we've got a valid prefetchable memory base, and
1346 * the base + length isn't greater than 0xFFFF
1347 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 temp_dword = pre_mem_base + pre_mem_length;
1349 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1350 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
1351 if (!p_mem_node)
1352 return -ENOMEM;
1353
1354 p_mem_node->base = pre_mem_base << 16;
1355
1356 p_mem_node->length = pre_mem_length << 16;
1357 dbg("found p_mem_node(base, length) = %x, %x\n",
1358 p_mem_node->base, p_mem_node->length);
1359 dbg("populated slot =%d \n", populated_slot);
1360
1361 if (!populated_slot) {
1362 p_mem_node->next = ctrl->p_mem_head;
1363 ctrl->p_mem_head = p_mem_node;
1364 } else {
1365 p_mem_node->next = func->p_mem_head;
1366 func->p_mem_head = p_mem_node;
1367 }
1368 }
1369
Alex Chiang427438c2009-03-31 09:23:16 -06001370 /* If we've got a valid bus number, use it
1371 * The second condition is to ignore bus numbers on
1372 * populated slots that don't have PCI-PCI bridges
1373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 if (secondary_bus && (secondary_bus != primary_bus)) {
1375 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1376 if (!bus_node)
1377 return -ENOMEM;
1378
1379 bus_node->base = secondary_bus;
1380 bus_node->length = max_bus - secondary_bus + 1;
1381 dbg("found bus_node(base, length) = %x, %x\n",
1382 bus_node->base, bus_node->length);
1383 dbg("populated slot =%d \n", populated_slot);
1384 if (!populated_slot) {
1385 bus_node->next = ctrl->bus_head;
1386 ctrl->bus_head = bus_node;
1387 } else {
1388 bus_node->next = func->bus_head;
1389 func->bus_head = bus_node;
1390 }
1391 }
1392
1393 i--;
1394 one_slot += sizeof (struct slot_rt);
1395 }
1396
Alex Chiang427438c2009-03-31 09:23:16 -06001397 /* If all of the following fail, we don't have any resources for
1398 * hot plug add
1399 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 rc = 1;
1401 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1402 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1403 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1404 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1405
1406 return rc;
1407}
1408
1409
1410/*
1411 * cpqhp_return_board_resources
1412 *
1413 * this routine returns all resources allocated to a board to
1414 * the available pool.
1415 *
1416 * returns 0 if success
1417 */
1418int cpqhp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
1419{
1420 int rc = 0;
1421 struct pci_resource *node;
1422 struct pci_resource *t_node;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001423 dbg("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 if (!func)
1426 return 1;
1427
1428 node = func->io_head;
1429 func->io_head = NULL;
1430 while (node) {
1431 t_node = node->next;
1432 return_resource(&(resources->io_head), node);
1433 node = t_node;
1434 }
1435
1436 node = func->mem_head;
1437 func->mem_head = NULL;
1438 while (node) {
1439 t_node = node->next;
1440 return_resource(&(resources->mem_head), node);
1441 node = t_node;
1442 }
1443
1444 node = func->p_mem_head;
1445 func->p_mem_head = NULL;
1446 while (node) {
1447 t_node = node->next;
1448 return_resource(&(resources->p_mem_head), node);
1449 node = t_node;
1450 }
1451
1452 node = func->bus_head;
1453 func->bus_head = NULL;
1454 while (node) {
1455 t_node = node->next;
1456 return_resource(&(resources->bus_head), node);
1457 node = t_node;
1458 }
1459
1460 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1461 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1462 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1463 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1464
1465 return rc;
1466}
1467
1468
1469/*
1470 * cpqhp_destroy_resource_list
1471 *
1472 * Puts node back in the resource list pointed to by head
1473 */
1474void cpqhp_destroy_resource_list (struct resource_lists * resources)
1475{
1476 struct pci_resource *res, *tres;
1477
1478 res = resources->io_head;
1479 resources->io_head = NULL;
1480
1481 while (res) {
1482 tres = res;
1483 res = res->next;
1484 kfree(tres);
1485 }
1486
1487 res = resources->mem_head;
1488 resources->mem_head = NULL;
1489
1490 while (res) {
1491 tres = res;
1492 res = res->next;
1493 kfree(tres);
1494 }
1495
1496 res = resources->p_mem_head;
1497 resources->p_mem_head = NULL;
1498
1499 while (res) {
1500 tres = res;
1501 res = res->next;
1502 kfree(tres);
1503 }
1504
1505 res = resources->bus_head;
1506 resources->bus_head = NULL;
1507
1508 while (res) {
1509 tres = res;
1510 res = res->next;
1511 kfree(tres);
1512 }
1513}
1514
1515
1516/*
1517 * cpqhp_destroy_board_resources
1518 *
1519 * Puts node back in the resource list pointed to by head
1520 */
1521void cpqhp_destroy_board_resources (struct pci_func * func)
1522{
1523 struct pci_resource *res, *tres;
1524
1525 res = func->io_head;
1526 func->io_head = NULL;
1527
1528 while (res) {
1529 tres = res;
1530 res = res->next;
1531 kfree(tres);
1532 }
1533
1534 res = func->mem_head;
1535 func->mem_head = NULL;
1536
1537 while (res) {
1538 tres = res;
1539 res = res->next;
1540 kfree(tres);
1541 }
1542
1543 res = func->p_mem_head;
1544 func->p_mem_head = NULL;
1545
1546 while (res) {
1547 tres = res;
1548 res = res->next;
1549 kfree(tres);
1550 }
1551
1552 res = func->bus_head;
1553 func->bus_head = NULL;
1554
1555 while (res) {
1556 tres = res;
1557 res = res->next;
1558 kfree(tres);
1559 }
1560}
1561