blob: 2e96bae3c82a0db33e3c89c7d6d35b02b4e5c46e [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"
Jaswinder Singh Rajput82487712008-12-27 18:32:28 +053040#include <asm/pci_x86.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
42
43u8 cpqhp_nic_irq;
44u8 cpqhp_disk_irq;
45
46static u16 unused_IRQ;
47
48/*
49 * detect_HRT_floating_pointer
50 *
51 * find the Hot Plug Resource Table in the specified region of memory.
52 *
53 */
54static void __iomem *detect_HRT_floating_pointer(void __iomem *begin, void __iomem *end)
55{
56 void __iomem *fp;
57 void __iomem *endp;
58 u8 temp1, temp2, temp3, temp4;
59 int status = 0;
60
61 endp = (end - sizeof(struct hrt) + 1);
62
63 for (fp = begin; fp <= endp; fp += 16) {
64 temp1 = readb(fp + SIG0);
65 temp2 = readb(fp + SIG1);
66 temp3 = readb(fp + SIG2);
67 temp4 = readb(fp + SIG3);
68 if (temp1 == '$' &&
69 temp2 == 'H' &&
70 temp3 == 'R' &&
71 temp4 == 'T') {
72 status = 1;
73 break;
74 }
75 }
76
77 if (!status)
78 fp = NULL;
79
80 dbg("Discovered Hotplug Resource Table at %p\n", fp);
81 return fp;
82}
83
84
Alex Chiang861fefb2009-03-31 09:23:11 -060085int cpqhp_configure_device (struct controller* ctrl, struct pci_func* func)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 unsigned char bus;
88 struct pci_bus *child;
89 int num;
90
91 if (func->pci_dev == NULL)
92 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
93
94 /* No pci device, we need to create it then */
95 if (func->pci_dev == NULL) {
96 dbg("INFO: pci_dev still null\n");
97
98 num = pci_scan_slot(ctrl->pci_dev->bus, PCI_DEVFN(func->device, func->function));
99 if (num)
100 pci_bus_add_devices(ctrl->pci_dev->bus);
101
102 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
103 if (func->pci_dev == NULL) {
104 dbg("ERROR: pci_dev still null\n");
105 return 0;
106 }
107 }
108
109 if (func->pci_dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
110 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);
112 pci_do_scan_bus(child);
113 }
114
115 return 0;
116}
117
118
Alex Chiang861fefb2009-03-31 09:23:11 -0600119int cpqhp_unconfigure_device(struct pci_func* func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 int j;
Alex Chiang861fefb2009-03-31 09:23:11 -0600122
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800123 dbg("%s: bus/dev/func = %x/%x/%x\n", __func__, func->bus, func->device, func->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 for (j=0; j<8 ; j++) {
126 struct pci_dev* temp = pci_find_slot(func->bus, PCI_DEVFN(func->device, j));
127 if (temp)
128 pci_remove_bus_device(temp);
129 }
130 return 0;
131}
132
133static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
134{
135 u32 vendID = 0;
136
137 if (pci_bus_read_config_dword (bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
138 return -1;
139 if (vendID == 0xffffffff)
140 return -1;
141 return pci_bus_read_config_dword (bus, devfn, offset, value);
142}
143
144
145/*
146 * cpqhp_set_irq
147 *
148 * @bus_num: bus number of PCI device
149 * @dev_num: device number of PCI device
150 * @slot: pointer to u8 where slot number will be returned
151 */
152int cpqhp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
153{
154 int rc = 0;
155
156 if (cpqhp_legacy_mode) {
157 struct pci_dev *fakedev;
158 struct pci_bus *fakebus;
159 u16 temp_word;
160
161 fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
162 fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
163 if (!fakedev || !fakebus) {
164 kfree(fakedev);
165 kfree(fakebus);
166 return -ENOMEM;
167 }
168
169 fakedev->devfn = dev_num << 3;
170 fakedev->bus = fakebus;
171 fakebus->number = bus_num;
172 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800173 __func__, dev_num, bus_num, int_pin, irq_num);
Bjorn Helgaas98d33332008-12-09 16:11:41 -0700174 rc = pcibios_set_irq_routing(fakedev, int_pin - 1, irq_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 kfree(fakedev);
176 kfree(fakebus);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800177 dbg("%s: rc %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (!rc)
179 return !rc;
180
Alex Chiang427438c2009-03-31 09:23:16 -0600181 /* set the Edge Level Control Register (ELCR) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 temp_word = inb(0x4d0);
183 temp_word |= inb(0x4d1) << 8;
184
185 temp_word |= 0x01 << irq_num;
186
Alex Chiang427438c2009-03-31 09:23:16 -0600187 /* This should only be for x86 as it sets the Edge Level
188 * Control Register
189 */
190 outb((u8) (temp_word & 0xFF), 0x4d0); outb((u8) ((temp_word &
191 0xFF00) >> 8), 0x4d1); rc = 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 return rc;
194}
195
196
197/*
Alex Chiang861fefb2009-03-31 09:23:11 -0600198 * WTF??? This function isn't in the code, yet a function calls it, but the
199 * compiler optimizes it away? strange. Here as a placeholder to keep the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * compiler happy.
201 */
202static int PCI_ScanBusNonBridge (u8 bus, u8 device)
203{
204 return 0;
205}
206
207static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 * dev_num)
208{
209 u16 tdevice;
210 u32 work;
211 u8 tbus;
212
213 ctrl->pci_bus->number = bus_num;
214
215 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
Alex Chiang427438c2009-03-31 09:23:16 -0600216 /* Scan for access first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
218 continue;
219 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
Alex Chiang427438c2009-03-31 09:23:16 -0600220 /* Yep we got one. Not a bridge ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
222 *dev_num = tdevice;
223 dbg("found it !\n");
224 return 0;
225 }
226 }
227 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
Alex Chiang427438c2009-03-31 09:23:16 -0600228 /* Scan for access first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
230 continue;
231 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
Alex Chiang427438c2009-03-31 09:23:16 -0600232 /* Yep we got one. bridge ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
234 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
235 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
236 if (PCI_ScanBusNonBridge(tbus, tdevice) == 0)
237 return 0;
238 }
239 }
240
241 return -1;
242}
243
244
245static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
246{
247 struct irq_routing_table *PCIIRQRoutingInfoLength;
248 long len;
249 long loop;
250 u32 work;
251
252 u8 tbus, tdevice, tslot;
253
254 PCIIRQRoutingInfoLength = pcibios_get_irq_routing_table();
255 if (!PCIIRQRoutingInfoLength)
256 return -1;
257
258 len = (PCIIRQRoutingInfoLength->size -
259 sizeof(struct irq_routing_table)) / sizeof(struct irq_info);
Alex Chiang427438c2009-03-31 09:23:16 -0600260 /* Make sure I got at least one entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (len == 0) {
Jesper Juhl6044ec82005-11-07 01:01:32 -0800262 kfree(PCIIRQRoutingInfoLength );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -1;
264 }
265
266 for (loop = 0; loop < len; ++loop) {
267 tbus = PCIIRQRoutingInfoLength->slots[loop].bus;
268 tdevice = PCIIRQRoutingInfoLength->slots[loop].devfn;
269 tslot = PCIIRQRoutingInfoLength->slots[loop].slot;
270
271 if (tslot == slot) {
272 *bus_num = tbus;
273 *dev_num = tdevice;
274 ctrl->pci_bus->number = tbus;
275 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
276 if (!nobridge || (work == 0xffffffff)) {
Jesper Juhl6044ec82005-11-07 01:01:32 -0800277 kfree(PCIIRQRoutingInfoLength );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 return 0;
279 }
280
281 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
282 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
283 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
284
285 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
286 pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
287 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
288 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
289 *bus_num = tbus;
Jesper Juhl6044ec82005-11-07 01:01:32 -0800290 kfree(PCIIRQRoutingInfoLength );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return 0;
292 }
293 } else {
Jesper Juhl6044ec82005-11-07 01:01:32 -0800294 kfree(PCIIRQRoutingInfoLength );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return 0;
296 }
297
298 }
299 }
Jesper Juhl6044ec82005-11-07 01:01:32 -0800300 kfree(PCIIRQRoutingInfoLength );
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return -1;
302}
303
304
305int cpqhp_get_bus_dev (struct controller *ctrl, u8 * bus_num, u8 * dev_num, u8 slot)
306{
Alex Chiang427438c2009-03-31 09:23:16 -0600307 /* plain (bridges allowed) */
308 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
311
Alex Chiang427438c2009-03-31 09:23:16 -0600312/* More PCI configuration routines; this time centered around hotplug
313 * controller
314 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316
317/*
318 * cpqhp_save_config
319 *
320 * Reads configuration for all slots in a PCI bus and saves info.
321 *
322 * Note: For non-hot plug busses, the slot # saved is the device #
323 *
324 * returns 0 if success
325 */
326int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
327{
328 long rc;
329 u8 class_code;
330 u8 header_type;
331 u32 ID;
332 u8 secondary_bus;
333 struct pci_func *new_slot;
334 int sub_bus;
335 int FirstSupported;
336 int LastSupported;
337 int max_functions;
338 int function;
339 u8 DevError;
340 int device = 0;
341 int cloop = 0;
342 int stop_it;
343 int index;
344
Alex Chiang427438c2009-03-31 09:23:16 -0600345 /* Decide which slots are supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 if (is_hot_plug) {
Alex Chiang427438c2009-03-31 09:23:16 -0600348 /*
349 * is_hot_plug is the slot mask
350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 FirstSupported = is_hot_plug >> 4;
352 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
353 } else {
354 FirstSupported = 0;
355 LastSupported = 0x1F;
356 }
357
Alex Chiang427438c2009-03-31 09:23:16 -0600358 /* Save PCI configuration space for all devices in supported slots */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 ctrl->pci_bus->number = busnumber;
360 for (device = FirstSupported; device <= LastSupported; device++) {
361 ID = 0xFFFFFFFF;
362 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
363
Alex Chiang427438c2009-03-31 09:23:16 -0600364 if (ID != 0xFFFFFFFF) { /* device in slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
366 if (rc)
367 return rc;
368
369 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
370 if (rc)
371 return rc;
372
Alex Chiang427438c2009-03-31 09:23:16 -0600373 /* If multi-function device, set max_functions to 8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 if (header_type & 0x80)
375 max_functions = 8;
376 else
377 max_functions = 1;
378
379 function = 0;
380
381 do {
382 DevError = 0;
Alex Chiang427438c2009-03-31 09:23:16 -0600383 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
384 /* Recurse the subordinate bus
385 * get the subordinate bus number
386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
388 if (rc) {
389 return rc;
390 } else {
391 sub_bus = (int) secondary_bus;
392
Alex Chiang427438c2009-03-31 09:23:16 -0600393 /* Save secondary bus cfg spc
394 * with this recursive call.
395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 rc = cpqhp_save_config(ctrl, sub_bus, 0);
397 if (rc)
398 return rc;
399 ctrl->pci_bus->number = busnumber;
400 }
401 }
402
403 index = 0;
404 new_slot = cpqhp_slot_find(busnumber, device, index++);
Alex Chiang861fefb2009-03-31 09:23:11 -0600405 while (new_slot &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 (new_slot->function != (u8) function))
407 new_slot = cpqhp_slot_find(busnumber, device, index++);
408
409 if (!new_slot) {
Alex Chiang427438c2009-03-31 09:23:16 -0600410 /* Setup slot structure. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 new_slot = cpqhp_slot_create(busnumber);
412
413 if (new_slot == NULL)
414 return(1);
415 }
416
417 new_slot->bus = (u8) busnumber;
418 new_slot->device = (u8) device;
419 new_slot->function = (u8) function;
420 new_slot->is_a_board = 1;
421 new_slot->switch_save = 0x10;
Alex Chiang427438c2009-03-31 09:23:16 -0600422 /* In case of unsupported board */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 new_slot->status = DevError;
424 new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
425
426 for (cloop = 0; cloop < 0x20; cloop++) {
427 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
428 if (rc)
429 return rc;
430 }
431
432 function++;
433
434 stop_it = 0;
435
Alex Chiang427438c2009-03-31 09:23:16 -0600436 /* this loop skips to the next present function
437 * reading in Class Code and Header type.
438 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 while ((function < max_functions)&&(!stop_it)) {
441 rc = pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
Alex Chiang427438c2009-03-31 09:23:16 -0600442 if (ID == 0xFFFFFFFF) { /* nothing there. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 function++;
Alex Chiang427438c2009-03-31 09:23:16 -0600444 } else { /* Something there */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
446 if (rc)
447 return rc;
448
449 rc = pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
450 if (rc)
451 return rc;
452
453 stop_it++;
454 }
455 }
456
457 } while (function < max_functions);
Alex Chiang427438c2009-03-31 09:23:16 -0600458 } /* End of IF (device in slot?) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 else if (is_hot_plug) {
Alex Chiang427438c2009-03-31 09:23:16 -0600460 /* Setup slot structure with entry for empty slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 new_slot = cpqhp_slot_create(busnumber);
462
463 if (new_slot == NULL) {
464 return(1);
465 }
466
467 new_slot->bus = (u8) busnumber;
468 new_slot->device = (u8) device;
469 new_slot->function = 0;
470 new_slot->is_a_board = 0;
471 new_slot->presence_save = 0;
472 new_slot->switch_save = 0;
473 }
Alex Chiang427438c2009-03-31 09:23:16 -0600474 } /* End of FOR loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 return(0);
477}
478
479
480/*
481 * cpqhp_save_slot_config
482 *
483 * Saves configuration info for all PCI devices in a given slot
484 * including subordinate busses.
485 *
486 * returns 0 if success
487 */
488int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
489{
490 long rc;
491 u8 class_code;
492 u8 header_type;
493 u32 ID;
494 u8 secondary_bus;
495 int sub_bus;
496 int max_functions;
497 int function;
498 int cloop = 0;
499 int stop_it;
500
501 ID = 0xFFFFFFFF;
502
503 ctrl->pci_bus->number = new_slot->bus;
504 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
505
Alex Chiang427438c2009-03-31 09:23:16 -0600506 if (ID != 0xFFFFFFFF) { /* device in slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
508 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
509
Alex Chiang427438c2009-03-31 09:23:16 -0600510 if (header_type & 0x80) /* Multi-function device */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 max_functions = 8;
512 else
513 max_functions = 1;
514
515 function = 0;
516
517 do {
Alex Chiang427438c2009-03-31 09:23:16 -0600518 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
519 /* Recurse the subordinate bus */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
521
522 sub_bus = (int) secondary_bus;
523
Alex Chiang427438c2009-03-31 09:23:16 -0600524 /* Save the config headers for the secondary
525 * bus.
526 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 rc = cpqhp_save_config(ctrl, sub_bus, 0);
528 if (rc)
529 return(rc);
530 ctrl->pci_bus->number = new_slot->bus;
531
Alex Chiang427438c2009-03-31 09:23:16 -0600532 } /* End of IF */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 new_slot->status = 0;
535
536 for (cloop = 0; cloop < 0x20; cloop++) {
537 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
538 }
539
540 function++;
541
542 stop_it = 0;
543
Alex Chiang427438c2009-03-31 09:23:16 -0600544 /* this loop skips to the next present function
545 * reading in the Class Code and the Header type.
546 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 while ((function < max_functions) && (!stop_it)) {
548 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
549
Alex Chiang427438c2009-03-31 09:23:16 -0600550 if (ID == 0xFFFFFFFF) { /* nothing there. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 function++;
Alex Chiang427438c2009-03-31 09:23:16 -0600552 } else { /* Something there */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
554
555 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
556
557 stop_it++;
558 }
559 }
560
561 } while (function < max_functions);
Alex Chiang427438c2009-03-31 09:23:16 -0600562 } /* End of IF (device in slot?) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 else {
564 return 2;
565 }
566
567 return 0;
568}
569
570
571/*
572 * cpqhp_save_base_addr_length
573 *
574 * Saves the length of all base address registers for the
575 * specified slot. this is for hot plug REPLACE
576 *
577 * returns 0 if success
578 */
579int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func * func)
580{
581 u8 cloop;
582 u8 header_type;
583 u8 secondary_bus;
584 u8 type;
585 int sub_bus;
586 u32 temp_register;
587 u32 base;
588 u32 rc;
589 struct pci_func *next;
590 int index = 0;
591 struct pci_bus *pci_bus = ctrl->pci_bus;
592 unsigned int devfn;
593
594 func = cpqhp_slot_find(func->bus, func->device, index++);
595
596 while (func != NULL) {
597 pci_bus->number = func->bus;
598 devfn = PCI_DEVFN(func->device, func->function);
599
Alex Chiang427438c2009-03-31 09:23:16 -0600600 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
602
603 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
605
606 sub_bus = (int) secondary_bus;
607
608 next = cpqhp_slot_list[sub_bus];
609
610 while (next != NULL) {
611 rc = cpqhp_save_base_addr_length(ctrl, next);
612 if (rc)
613 return rc;
614
615 next = next->next;
616 }
617 pci_bus->number = func->bus;
618
Alex Chiang427438c2009-03-31 09:23:16 -0600619 /* FIXME: this loop is duplicated in the non-bridge
620 * case. The two could be rolled together Figure out
621 * IO and memory base lengths
622 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
624 temp_register = 0xFFFFFFFF;
625 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
626 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
Alex Chiang427438c2009-03-31 09:23:16 -0600627 /* If this register is implemented */
628 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -0600630 /* IO base
631 * set base = amount of IO space
632 * requested
633 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 base = base & 0xFFFFFFFE;
635 base = (~base) + 1;
636
637 type = 1;
638 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600639 /* memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 base = base & 0xFFFFFFF0;
641 base = (~base) + 1;
642
643 type = 0;
644 }
645 } else {
646 base = 0x0L;
647 type = 0;
648 }
649
Alex Chiang427438c2009-03-31 09:23:16 -0600650 /* Save information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 func->base_length[(cloop - 0x10) >> 2] =
652 base;
653 func->base_type[(cloop - 0x10) >> 2] = type;
654
Alex Chiang427438c2009-03-31 09:23:16 -0600655 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Alex Chiang427438c2009-03-31 09:23:16 -0600657 } else if ((header_type & 0x7F) == 0x00) {
658 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
660 temp_register = 0xFFFFFFFF;
661 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
662 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
663
Alex Chiang427438c2009-03-31 09:23:16 -0600664 /* If this register is implemented */
665 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -0600667 /* IO base
668 * base = amount of IO space
669 * requested
670 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 base = base & 0xFFFFFFFE;
672 base = (~base) + 1;
673
674 type = 1;
675 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600676 /* memory base
677 * base = amount of memory
678 * space requested
679 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 base = base & 0xFFFFFFF0;
681 base = (~base) + 1;
682
683 type = 0;
684 }
685 } else {
686 base = 0x0L;
687 type = 0;
688 }
689
Alex Chiang427438c2009-03-31 09:23:16 -0600690 /* Save information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 func->base_length[(cloop - 0x10) >> 2] = base;
692 func->base_type[(cloop - 0x10) >> 2] = type;
693
Alex Chiang427438c2009-03-31 09:23:16 -0600694 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Alex Chiang427438c2009-03-31 09:23:16 -0600696 } else { /* Some other unknown header type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
Alex Chiang427438c2009-03-31 09:23:16 -0600699 /* find the next device in this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 func = cpqhp_slot_find(func->bus, func->device, index++);
701 }
702
703 return(0);
704}
705
706
707/*
708 * cpqhp_save_used_resources
709 *
710 * Stores used resource information for existing boards. this is
711 * for boards that were in the system when this driver was loaded.
712 * this function is for hot plug ADD
713 *
714 * returns 0 if success
715 */
716int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func * func)
717{
718 u8 cloop;
719 u8 header_type;
720 u8 secondary_bus;
721 u8 temp_byte;
722 u8 b_base;
723 u8 b_length;
724 u16 command;
725 u16 save_command;
726 u16 w_base;
727 u16 w_length;
728 u32 temp_register;
729 u32 save_base;
730 u32 base;
731 int index = 0;
732 struct pci_resource *mem_node;
733 struct pci_resource *p_mem_node;
734 struct pci_resource *io_node;
735 struct pci_resource *bus_node;
736 struct pci_bus *pci_bus = ctrl->pci_bus;
737 unsigned int devfn;
738
739 func = cpqhp_slot_find(func->bus, func->device, index++);
740
741 while ((func != NULL) && func->is_a_board) {
742 pci_bus->number = func->bus;
743 devfn = PCI_DEVFN(func->device, func->function);
744
Alex Chiang427438c2009-03-31 09:23:16 -0600745 /* Save the command register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
747
Alex Chiang427438c2009-03-31 09:23:16 -0600748 /* disable card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 command = 0x00;
750 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
751
Alex Chiang427438c2009-03-31 09:23:16 -0600752 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
754
Alex Chiang427438c2009-03-31 09:23:16 -0600755 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
756 /* Clear Bridge Control Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 command = 0x00;
758 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
759 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
760 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
761
762 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
763 if (!bus_node)
764 return -ENOMEM;
765
766 bus_node->base = secondary_bus;
767 bus_node->length = temp_byte - secondary_bus + 1;
768
769 bus_node->next = func->bus_head;
770 func->bus_head = bus_node;
771
Alex Chiang427438c2009-03-31 09:23:16 -0600772 /* Save IO base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
774 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
775
776 if ((b_base <= b_length) && (save_command & 0x01)) {
777 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
778 if (!io_node)
779 return -ENOMEM;
780
781 io_node->base = (b_base & 0xF0) << 8;
782 io_node->length = (b_length - b_base + 0x10) << 8;
783
784 io_node->next = func->io_head;
785 func->io_head = io_node;
786 }
787
Alex Chiang427438c2009-03-31 09:23:16 -0600788 /* Save memory base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
790 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
791
792 if ((w_base <= w_length) && (save_command & 0x02)) {
793 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
794 if (!mem_node)
795 return -ENOMEM;
796
797 mem_node->base = w_base << 16;
798 mem_node->length = (w_length - w_base + 0x10) << 16;
799
800 mem_node->next = func->mem_head;
801 func->mem_head = mem_node;
802 }
803
Alex Chiang427438c2009-03-31 09:23:16 -0600804 /* Save prefetchable memory base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
806 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
807
808 if ((w_base <= w_length) && (save_command & 0x02)) {
809 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
810 if (!p_mem_node)
811 return -ENOMEM;
812
813 p_mem_node->base = w_base << 16;
814 p_mem_node->length = (w_length - w_base + 0x10) << 16;
815
816 p_mem_node->next = func->p_mem_head;
817 func->p_mem_head = p_mem_node;
818 }
Alex Chiang427438c2009-03-31 09:23:16 -0600819 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
821 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
822
823 temp_register = 0xFFFFFFFF;
824 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
825 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
826
827 temp_register = base;
828
Alex Chiang427438c2009-03-31 09:23:16 -0600829 /* If this register is implemented */
830 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 if (((base & 0x03L) == 0x01)
832 && (save_command & 0x01)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600833 /* IO base
834 * set temp_register = amount
835 * of IO space requested
836 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 temp_register = base & 0xFFFFFFFE;
838 temp_register = (~temp_register) + 1;
839
840 io_node = kmalloc(sizeof(*io_node),
841 GFP_KERNEL);
842 if (!io_node)
843 return -ENOMEM;
844
845 io_node->base =
846 save_base & (~0x03L);
847 io_node->length = temp_register;
848
849 io_node->next = func->io_head;
850 func->io_head = io_node;
851 } else
852 if (((base & 0x0BL) == 0x08)
853 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600854 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 temp_register = base & 0xFFFFFFF0;
856 temp_register = (~temp_register) + 1;
857
858 p_mem_node = kmalloc(sizeof(*p_mem_node),
859 GFP_KERNEL);
860 if (!p_mem_node)
861 return -ENOMEM;
862
863 p_mem_node->base = save_base & (~0x0FL);
864 p_mem_node->length = temp_register;
865
866 p_mem_node->next = func->p_mem_head;
867 func->p_mem_head = p_mem_node;
868 } else
869 if (((base & 0x0BL) == 0x00)
870 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600871 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 temp_register = base & 0xFFFFFFF0;
873 temp_register = (~temp_register) + 1;
874
875 mem_node = kmalloc(sizeof(*mem_node),
876 GFP_KERNEL);
877 if (!mem_node)
878 return -ENOMEM;
879
880 mem_node->base = save_base & (~0x0FL);
881 mem_node->length = temp_register;
882
883 mem_node->next = func->mem_head;
884 func->mem_head = mem_node;
885 } else
886 return(1);
887 }
Alex Chiang427438c2009-03-31 09:23:16 -0600888 } /* End of base register loop */
889 /* Standard header */
890 } else if ((header_type & 0x7F) == 0x00) {
891 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
893 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
894
895 temp_register = 0xFFFFFFFF;
896 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
897 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
898
899 temp_register = base;
900
Alex Chiang427438c2009-03-31 09:23:16 -0600901 /* If this register is implemented */
902 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 if (((base & 0x03L) == 0x01)
904 && (save_command & 0x01)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600905 /* IO base
906 * set temp_register = amount
907 * of IO space requested
908 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 temp_register = base & 0xFFFFFFFE;
910 temp_register = (~temp_register) + 1;
911
912 io_node = kmalloc(sizeof(*io_node),
913 GFP_KERNEL);
914 if (!io_node)
915 return -ENOMEM;
916
917 io_node->base = save_base & (~0x01L);
918 io_node->length = temp_register;
919
920 io_node->next = func->io_head;
921 func->io_head = io_node;
922 } else
923 if (((base & 0x0BL) == 0x08)
924 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600925 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 temp_register = base & 0xFFFFFFF0;
927 temp_register = (~temp_register) + 1;
928
929 p_mem_node = kmalloc(sizeof(*p_mem_node),
930 GFP_KERNEL);
931 if (!p_mem_node)
932 return -ENOMEM;
933
934 p_mem_node->base = save_base & (~0x0FL);
935 p_mem_node->length = temp_register;
936
937 p_mem_node->next = func->p_mem_head;
938 func->p_mem_head = p_mem_node;
939 } else
940 if (((base & 0x0BL) == 0x00)
941 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600942 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 temp_register = base & 0xFFFFFFF0;
944 temp_register = (~temp_register) + 1;
945
946 mem_node = kmalloc(sizeof(*mem_node),
947 GFP_KERNEL);
948 if (!mem_node)
949 return -ENOMEM;
950
951 mem_node->base = save_base & (~0x0FL);
952 mem_node->length = temp_register;
953
954 mem_node->next = func->mem_head;
955 func->mem_head = mem_node;
956 } else
957 return(1);
958 }
Alex Chiang427438c2009-03-31 09:23:16 -0600959 } /* End of base register loop */
960 /* Some other unknown header type */
961 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963
Alex Chiang427438c2009-03-31 09:23:16 -0600964 /* find the next device in this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 func = cpqhp_slot_find(func->bus, func->device, index++);
966 }
967
968 return(0);
969}
970
971
972/*
973 * cpqhp_configure_board
974 *
975 * Copies saved configuration information to one slot.
976 * this is called recursively for bridge devices.
977 * this is for hot plug REPLACE!
978 *
979 * returns 0 if success
980 */
981int cpqhp_configure_board(struct controller *ctrl, struct pci_func * func)
982{
983 int cloop;
984 u8 header_type;
985 u8 secondary_bus;
986 int sub_bus;
987 struct pci_func *next;
988 u32 temp;
989 u32 rc;
990 int index = 0;
991 struct pci_bus *pci_bus = ctrl->pci_bus;
992 unsigned int devfn;
993
994 func = cpqhp_slot_find(func->bus, func->device, index++);
995
996 while (func != NULL) {
997 pci_bus->number = func->bus;
998 devfn = PCI_DEVFN(func->device, func->function);
999
Alex Chiang427438c2009-03-31 09:23:16 -06001000 /* Start at the top of config space so that the control
1001 * registers are programmed last
1002 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 for (cloop = 0x3C; cloop > 0; cloop -= 4) {
1004 pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
1005 }
1006
1007 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1008
Alex Chiang427438c2009-03-31 09:23:16 -06001009 /* If this is a bridge device, restore subordinate devices */
1010 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
1012
1013 sub_bus = (int) secondary_bus;
1014
1015 next = cpqhp_slot_list[sub_bus];
1016
1017 while (next != NULL) {
1018 rc = cpqhp_configure_board(ctrl, next);
1019 if (rc)
1020 return rc;
1021
1022 next = next->next;
1023 }
1024 } else {
1025
Alex Chiang427438c2009-03-31 09:23:16 -06001026 /* Check all the base Address Registers to make sure
1027 * they are the same. If not, the board is different.
1028 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 for (cloop = 16; cloop < 40; cloop += 4) {
1031 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
1032
1033 if (temp != func->config_space[cloop >> 2]) {
1034 dbg("Config space compare failure!!! offset = %x\n", cloop);
1035 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
1036 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1037 return 1;
1038 }
1039 }
1040 }
1041
1042 func->configured = 1;
1043
1044 func = cpqhp_slot_find(func->bus, func->device, index++);
1045 }
1046
1047 return 0;
1048}
1049
1050
1051/*
1052 * cpqhp_valid_replace
1053 *
1054 * this function checks to see if a board is the same as the
1055 * one it is replacing. this check will detect if the device's
1056 * vendor or device id's are the same
1057 *
1058 * returns 0 if the board is the same nonzero otherwise
1059 */
1060int cpqhp_valid_replace(struct controller *ctrl, struct pci_func * func)
1061{
1062 u8 cloop;
1063 u8 header_type;
1064 u8 secondary_bus;
1065 u8 type;
1066 u32 temp_register = 0;
1067 u32 base;
1068 u32 rc;
1069 struct pci_func *next;
1070 int index = 0;
1071 struct pci_bus *pci_bus = ctrl->pci_bus;
1072 unsigned int devfn;
1073
1074 if (!func->is_a_board)
1075 return(ADD_NOT_SUPPORTED);
1076
1077 func = cpqhp_slot_find(func->bus, func->device, index++);
1078
1079 while (func != NULL) {
1080 pci_bus->number = func->bus;
1081 devfn = PCI_DEVFN(func->device, func->function);
1082
1083 pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1084
Alex Chiang427438c2009-03-31 09:23:16 -06001085 /* No adapter present */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 if (temp_register == 0xFFFFFFFF)
1087 return(NO_ADAPTER_PRESENT);
1088
1089 if (temp_register != func->config_space[0])
1090 return(ADAPTER_NOT_SAME);
1091
Alex Chiang427438c2009-03-31 09:23:16 -06001092 /* Check for same revision number and class code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1094
Alex Chiang427438c2009-03-31 09:23:16 -06001095 /* Adapter not the same */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (temp_register != func->config_space[0x08 >> 2])
1097 return(ADAPTER_NOT_SAME);
1098
Alex Chiang427438c2009-03-31 09:23:16 -06001099 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1101
Alex Chiang427438c2009-03-31 09:23:16 -06001102 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1103 /* In order to continue checking, we must program the
1104 * bus registers in the bridge to respond to accesses
1105 * for its subordinate bus(es)
1106 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 temp_register = func->config_space[0x18 >> 2];
1109 pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1110
1111 secondary_bus = (temp_register >> 8) & 0xFF;
1112
1113 next = cpqhp_slot_list[secondary_bus];
1114
1115 while (next != NULL) {
1116 rc = cpqhp_valid_replace(ctrl, next);
1117 if (rc)
1118 return rc;
1119
1120 next = next->next;
1121 }
1122
1123 }
Alex Chiang427438c2009-03-31 09:23:16 -06001124 /* Check to see if it is a standard config header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
Alex Chiang427438c2009-03-31 09:23:16 -06001126 /* Check subsystem vendor and ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1128
1129 if (temp_register != func->config_space[0x2C >> 2]) {
Alex Chiang427438c2009-03-31 09:23:16 -06001130 /* If it's a SMART-2 and the register isn't
1131 * filled in, ignore the difference because
1132 * they just have an old rev of the firmware
1133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (!((func->config_space[0] == 0xAE100E11)
1135 && (temp_register == 0x00L)))
1136 return(ADAPTER_NOT_SAME);
1137 }
Alex Chiang427438c2009-03-31 09:23:16 -06001138 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1140 temp_register = 0xFFFFFFFF;
1141 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
1142 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
Alex Chiang427438c2009-03-31 09:23:16 -06001143
1144 /* If this register is implemented */
1145 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -06001147 /* IO base
1148 * set base = amount of IO
1149 * space requested
1150 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 base = base & 0xFFFFFFFE;
1152 base = (~base) + 1;
1153
1154 type = 1;
1155 } else {
Alex Chiang427438c2009-03-31 09:23:16 -06001156 /* memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 base = base & 0xFFFFFFF0;
1158 base = (~base) + 1;
1159
1160 type = 0;
1161 }
1162 } else {
1163 base = 0x0L;
1164 type = 0;
1165 }
1166
Alex Chiang427438c2009-03-31 09:23:16 -06001167 /* Check information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 if (func->base_length[(cloop - 0x10) >> 2] != base)
1169 return(ADAPTER_NOT_SAME);
1170
1171 if (func->base_type[(cloop - 0x10) >> 2] != type)
1172 return(ADAPTER_NOT_SAME);
1173
Alex Chiang427438c2009-03-31 09:23:16 -06001174 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Alex Chiang427438c2009-03-31 09:23:16 -06001176 } /* End of (type 0 config space) else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 else {
Alex Chiang427438c2009-03-31 09:23:16 -06001178 /* this is not a type 0 or 1 config space header so
1179 * we don't know how to do it
1180 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return(DEVICE_TYPE_NOT_SUPPORTED);
1182 }
1183
Alex Chiang427438c2009-03-31 09:23:16 -06001184 /* Get the next function */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 func = cpqhp_slot_find(func->bus, func->device, index++);
1186 }
1187
1188
1189 return 0;
1190}
1191
1192
1193/*
1194 * cpqhp_find_available_resources
1195 *
1196 * Finds available memory, IO, and IRQ resources for programming
1197 * devices which may be added to the system
1198 * this function is for hot plug ADD!
1199 *
1200 * returns 0 if success
Alex Chiang861fefb2009-03-31 09:23:11 -06001201 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1203{
1204 u8 temp;
1205 u8 populated_slot;
1206 u8 bridged_slot;
1207 void __iomem *one_slot;
1208 void __iomem *rom_resource_table;
1209 struct pci_func *func = NULL;
1210 int i = 10, index;
1211 u32 temp_dword, rc;
1212 struct pci_resource *mem_node;
1213 struct pci_resource *p_mem_node;
1214 struct pci_resource *io_node;
1215 struct pci_resource *bus_node;
1216
1217 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1218 dbg("rom_resource_table = %p\n", rom_resource_table);
1219
1220 if (rom_resource_table == NULL) {
1221 return -ENODEV;
1222 }
Alex Chiang427438c2009-03-31 09:23:16 -06001223 /* Sum all resources and setup resource maps */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1225 dbg("unused_IRQ = %x\n", unused_IRQ);
1226
1227 temp = 0;
1228 while (unused_IRQ) {
1229 if (unused_IRQ & 1) {
1230 cpqhp_disk_irq = temp;
1231 break;
1232 }
1233 unused_IRQ = unused_IRQ >> 1;
1234 temp++;
1235 }
1236
1237 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1238 unused_IRQ = unused_IRQ >> 1;
1239 temp++;
1240
1241 while (unused_IRQ) {
1242 if (unused_IRQ & 1) {
1243 cpqhp_nic_irq = temp;
1244 break;
1245 }
1246 unused_IRQ = unused_IRQ >> 1;
1247 temp++;
1248 }
1249
1250 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1251 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1252
1253 temp = 0;
1254
1255 if (!cpqhp_nic_irq) {
1256 cpqhp_nic_irq = ctrl->cfgspc_irq;
1257 }
1258
1259 if (!cpqhp_disk_irq) {
1260 cpqhp_disk_irq = ctrl->cfgspc_irq;
1261 }
1262
1263 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1264
1265 rc = compaq_nvram_load(rom_start, ctrl);
1266 if (rc)
1267 return rc;
1268
1269 one_slot = rom_resource_table + sizeof (struct hrt);
1270
1271 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1272 dbg("number_of_entries = %d\n", i);
1273
1274 if (!readb(one_slot + SECONDARY_BUS))
1275 return 1;
1276
1277 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1278
1279 while (i && readb(one_slot + SECONDARY_BUS)) {
1280 u8 dev_func = readb(one_slot + DEV_FUNC);
1281 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1282 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1283 u8 max_bus = readb(one_slot + MAX_BUS);
1284 u16 io_base = readw(one_slot + IO_BASE);
1285 u16 io_length = readw(one_slot + IO_LENGTH);
1286 u16 mem_base = readw(one_slot + MEM_BASE);
1287 u16 mem_length = readw(one_slot + MEM_LENGTH);
1288 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1289 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1290
1291 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1292 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1293 primary_bus, secondary_bus, max_bus);
1294
Alex Chiang427438c2009-03-31 09:23:16 -06001295 /* If this entry isn't for our controller's bus, ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (primary_bus != ctrl->bus) {
1297 i--;
1298 one_slot += sizeof (struct slot_rt);
1299 continue;
1300 }
Alex Chiang427438c2009-03-31 09:23:16 -06001301 /* find out if this entry is for an occupied slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 ctrl->pci_bus->number = primary_bus;
1303 pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1304 dbg("temp_D_word = %x\n", temp_dword);
1305
1306 if (temp_dword != 0xFFFFFFFF) {
1307 index = 0;
1308 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1309
1310 while (func && (func->function != (dev_func & 0x07))) {
1311 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1312 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1313 }
1314
Alex Chiang427438c2009-03-31 09:23:16 -06001315 /* If we can't find a match, skip this table entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (!func) {
1317 i--;
1318 one_slot += sizeof (struct slot_rt);
1319 continue;
1320 }
Alex Chiang427438c2009-03-31 09:23:16 -06001321 /* this may not work and shouldn't be used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 if (secondary_bus != primary_bus)
1323 bridged_slot = 1;
1324 else
1325 bridged_slot = 0;
1326
1327 populated_slot = 1;
1328 } else {
1329 populated_slot = 0;
1330 bridged_slot = 0;
1331 }
1332
1333
Alex Chiang427438c2009-03-31 09:23:16 -06001334 /* If we've got a valid IO base, use it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335
1336 temp_dword = io_base + io_length;
1337
1338 if ((io_base) && (temp_dword < 0x10000)) {
1339 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1340 if (!io_node)
1341 return -ENOMEM;
1342
1343 io_node->base = io_base;
1344 io_node->length = io_length;
1345
1346 dbg("found io_node(base, length) = %x, %x\n",
1347 io_node->base, io_node->length);
1348 dbg("populated slot =%d \n", populated_slot);
1349 if (!populated_slot) {
1350 io_node->next = ctrl->io_head;
1351 ctrl->io_head = io_node;
1352 } else {
1353 io_node->next = func->io_head;
1354 func->io_head = io_node;
1355 }
1356 }
1357
Alex Chiang427438c2009-03-31 09:23:16 -06001358 /* If we've got a valid memory base, use it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 temp_dword = mem_base + mem_length;
1360 if ((mem_base) && (temp_dword < 0x10000)) {
1361 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
1362 if (!mem_node)
1363 return -ENOMEM;
1364
1365 mem_node->base = mem_base << 16;
1366
1367 mem_node->length = mem_length << 16;
1368
1369 dbg("found mem_node(base, length) = %x, %x\n",
1370 mem_node->base, mem_node->length);
1371 dbg("populated slot =%d \n", populated_slot);
1372 if (!populated_slot) {
1373 mem_node->next = ctrl->mem_head;
1374 ctrl->mem_head = mem_node;
1375 } else {
1376 mem_node->next = func->mem_head;
1377 func->mem_head = mem_node;
1378 }
1379 }
1380
Alex Chiang427438c2009-03-31 09:23:16 -06001381 /* If we've got a valid prefetchable memory base, and
1382 * the base + length isn't greater than 0xFFFF
1383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 temp_dword = pre_mem_base + pre_mem_length;
1385 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1386 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
1387 if (!p_mem_node)
1388 return -ENOMEM;
1389
1390 p_mem_node->base = pre_mem_base << 16;
1391
1392 p_mem_node->length = pre_mem_length << 16;
1393 dbg("found p_mem_node(base, length) = %x, %x\n",
1394 p_mem_node->base, p_mem_node->length);
1395 dbg("populated slot =%d \n", populated_slot);
1396
1397 if (!populated_slot) {
1398 p_mem_node->next = ctrl->p_mem_head;
1399 ctrl->p_mem_head = p_mem_node;
1400 } else {
1401 p_mem_node->next = func->p_mem_head;
1402 func->p_mem_head = p_mem_node;
1403 }
1404 }
1405
Alex Chiang427438c2009-03-31 09:23:16 -06001406 /* If we've got a valid bus number, use it
1407 * The second condition is to ignore bus numbers on
1408 * populated slots that don't have PCI-PCI bridges
1409 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 if (secondary_bus && (secondary_bus != primary_bus)) {
1411 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1412 if (!bus_node)
1413 return -ENOMEM;
1414
1415 bus_node->base = secondary_bus;
1416 bus_node->length = max_bus - secondary_bus + 1;
1417 dbg("found bus_node(base, length) = %x, %x\n",
1418 bus_node->base, bus_node->length);
1419 dbg("populated slot =%d \n", populated_slot);
1420 if (!populated_slot) {
1421 bus_node->next = ctrl->bus_head;
1422 ctrl->bus_head = bus_node;
1423 } else {
1424 bus_node->next = func->bus_head;
1425 func->bus_head = bus_node;
1426 }
1427 }
1428
1429 i--;
1430 one_slot += sizeof (struct slot_rt);
1431 }
1432
Alex Chiang427438c2009-03-31 09:23:16 -06001433 /* If all of the following fail, we don't have any resources for
1434 * hot plug add
1435 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 rc = 1;
1437 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1438 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1439 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1440 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1441
1442 return rc;
1443}
1444
1445
1446/*
1447 * cpqhp_return_board_resources
1448 *
1449 * this routine returns all resources allocated to a board to
1450 * the available pool.
1451 *
1452 * returns 0 if success
1453 */
1454int cpqhp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
1455{
1456 int rc = 0;
1457 struct pci_resource *node;
1458 struct pci_resource *t_node;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001459 dbg("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
1461 if (!func)
1462 return 1;
1463
1464 node = func->io_head;
1465 func->io_head = NULL;
1466 while (node) {
1467 t_node = node->next;
1468 return_resource(&(resources->io_head), node);
1469 node = t_node;
1470 }
1471
1472 node = func->mem_head;
1473 func->mem_head = NULL;
1474 while (node) {
1475 t_node = node->next;
1476 return_resource(&(resources->mem_head), node);
1477 node = t_node;
1478 }
1479
1480 node = func->p_mem_head;
1481 func->p_mem_head = NULL;
1482 while (node) {
1483 t_node = node->next;
1484 return_resource(&(resources->p_mem_head), node);
1485 node = t_node;
1486 }
1487
1488 node = func->bus_head;
1489 func->bus_head = NULL;
1490 while (node) {
1491 t_node = node->next;
1492 return_resource(&(resources->bus_head), node);
1493 node = t_node;
1494 }
1495
1496 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1497 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1498 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1499 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1500
1501 return rc;
1502}
1503
1504
1505/*
1506 * cpqhp_destroy_resource_list
1507 *
1508 * Puts node back in the resource list pointed to by head
1509 */
1510void cpqhp_destroy_resource_list (struct resource_lists * resources)
1511{
1512 struct pci_resource *res, *tres;
1513
1514 res = resources->io_head;
1515 resources->io_head = NULL;
1516
1517 while (res) {
1518 tres = res;
1519 res = res->next;
1520 kfree(tres);
1521 }
1522
1523 res = resources->mem_head;
1524 resources->mem_head = NULL;
1525
1526 while (res) {
1527 tres = res;
1528 res = res->next;
1529 kfree(tres);
1530 }
1531
1532 res = resources->p_mem_head;
1533 resources->p_mem_head = NULL;
1534
1535 while (res) {
1536 tres = res;
1537 res = res->next;
1538 kfree(tres);
1539 }
1540
1541 res = resources->bus_head;
1542 resources->bus_head = NULL;
1543
1544 while (res) {
1545 tres = res;
1546 res = res->next;
1547 kfree(tres);
1548 }
1549}
1550
1551
1552/*
1553 * cpqhp_destroy_board_resources
1554 *
1555 * Puts node back in the resource list pointed to by head
1556 */
1557void cpqhp_destroy_board_resources (struct pci_func * func)
1558{
1559 struct pci_resource *res, *tres;
1560
1561 res = func->io_head;
1562 func->io_head = NULL;
1563
1564 while (res) {
1565 tres = res;
1566 res = res->next;
1567 kfree(tres);
1568 }
1569
1570 res = func->mem_head;
1571 func->mem_head = NULL;
1572
1573 while (res) {
1574 tres = res;
1575 res = res->next;
1576 kfree(tres);
1577 }
1578
1579 res = func->p_mem_head;
1580 func->p_mem_head = NULL;
1581
1582 while (res) {
1583 tres = res;
1584 res = res->next;
1585 kfree(tres);
1586 }
1587
1588 res = func->bus_head;
1589 func->bus_head = NULL;
1590
1591 while (res) {
1592 tres = res;
1593 res = res->next;
1594 kfree(tres);
1595 }
1596}
1597