blob: 6201281b9dab6cd7d5b6b2874c0b54ca854d75f3 [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)
91 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
92
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
101 func->pci_dev = pci_find_slot(func->bus, PCI_DEVFN(func->device, func->function));
102 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) {
109 pci_read_config_byte(func->pci_dev, PCI_SECONDARY_BUS, &bus);
110 child = (struct pci_bus*) pci_add_new_bus(func->pci_dev->bus, (func->pci_dev), bus);
111 pci_do_scan_bus(child);
112 }
113
114 return 0;
115}
116
117
Alex Chiang861fefb2009-03-31 09:23:11 -0600118int cpqhp_unconfigure_device(struct pci_func* func)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
120 int j;
Alex Chiang861fefb2009-03-31 09:23:11 -0600121
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800122 dbg("%s: bus/dev/func = %x/%x/%x\n", __func__, func->bus, func->device, func->function);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 for (j=0; j<8 ; j++) {
125 struct pci_dev* temp = pci_find_slot(func->bus, PCI_DEVFN(func->device, j));
126 if (temp)
127 pci_remove_bus_device(temp);
128 }
129 return 0;
130}
131
132static int PCI_RefinedAccessConfig(struct pci_bus *bus, unsigned int devfn, u8 offset, u32 *value)
133{
134 u32 vendID = 0;
135
136 if (pci_bus_read_config_dword (bus, devfn, PCI_VENDOR_ID, &vendID) == -1)
137 return -1;
138 if (vendID == 0xffffffff)
139 return -1;
140 return pci_bus_read_config_dword (bus, devfn, offset, value);
141}
142
143
144/*
145 * cpqhp_set_irq
146 *
147 * @bus_num: bus number of PCI device
148 * @dev_num: device number of PCI device
149 * @slot: pointer to u8 where slot number will be returned
150 */
151int cpqhp_set_irq (u8 bus_num, u8 dev_num, u8 int_pin, u8 irq_num)
152{
153 int rc = 0;
154
155 if (cpqhp_legacy_mode) {
156 struct pci_dev *fakedev;
157 struct pci_bus *fakebus;
158 u16 temp_word;
159
160 fakedev = kmalloc(sizeof(*fakedev), GFP_KERNEL);
161 fakebus = kmalloc(sizeof(*fakebus), GFP_KERNEL);
162 if (!fakedev || !fakebus) {
163 kfree(fakedev);
164 kfree(fakebus);
165 return -ENOMEM;
166 }
167
168 fakedev->devfn = dev_num << 3;
169 fakedev->bus = fakebus;
170 fakebus->number = bus_num;
171 dbg("%s: dev %d, bus %d, pin %d, num %d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800172 __func__, dev_num, bus_num, int_pin, irq_num);
Bjorn Helgaas98d33332008-12-09 16:11:41 -0700173 rc = pcibios_set_irq_routing(fakedev, int_pin - 1, irq_num);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 kfree(fakedev);
175 kfree(fakebus);
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800176 dbg("%s: rc %d\n", __func__, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (!rc)
178 return !rc;
179
Alex Chiang427438c2009-03-31 09:23:16 -0600180 /* set the Edge Level Control Register (ELCR) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 temp_word = inb(0x4d0);
182 temp_word |= inb(0x4d1) << 8;
183
184 temp_word |= 0x01 << irq_num;
185
Alex Chiang427438c2009-03-31 09:23:16 -0600186 /* This should only be for x86 as it sets the Edge Level
187 * Control Register
188 */
189 outb((u8) (temp_word & 0xFF), 0x4d0); outb((u8) ((temp_word &
190 0xFF00) >> 8), 0x4d1); rc = 0; }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 return rc;
193}
194
195
196/*
Alex Chiang861fefb2009-03-31 09:23:11 -0600197 * WTF??? This function isn't in the code, yet a function calls it, but the
198 * compiler optimizes it away? strange. Here as a placeholder to keep the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * compiler happy.
200 */
201static int PCI_ScanBusNonBridge (u8 bus, u8 device)
202{
203 return 0;
204}
205
206static int PCI_ScanBusForNonBridge(struct controller *ctrl, u8 bus_num, u8 * dev_num)
207{
208 u16 tdevice;
209 u32 work;
210 u8 tbus;
211
212 ctrl->pci_bus->number = bus_num;
213
214 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
Alex Chiang427438c2009-03-31 09:23:16 -0600215 /* Scan for access first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
217 continue;
218 dbg("Looking for nonbridge bus_num %d dev_num %d\n", bus_num, tdevice);
Alex Chiang427438c2009-03-31 09:23:16 -0600219 /* Yep we got one. Not a bridge ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if ((work >> 8) != PCI_TO_PCI_BRIDGE_CLASS) {
221 *dev_num = tdevice;
222 dbg("found it !\n");
223 return 0;
224 }
225 }
226 for (tdevice = 0; tdevice < 0xFF; tdevice++) {
Alex Chiang427438c2009-03-31 09:23:16 -0600227 /* Scan for access first */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 if (PCI_RefinedAccessConfig(ctrl->pci_bus, tdevice, 0x08, &work) == -1)
229 continue;
230 dbg("Looking for bridge bus_num %d dev_num %d\n", bus_num, tdevice);
Alex Chiang427438c2009-03-31 09:23:16 -0600231 /* Yep we got one. bridge ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
233 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(tdevice, 0), PCI_SECONDARY_BUS, &tbus);
234 dbg("Recurse on bus_num %d tdevice %d\n", tbus, tdevice);
235 if (PCI_ScanBusNonBridge(tbus, tdevice) == 0)
236 return 0;
237 }
238 }
239
240 return -1;
241}
242
243
244static int PCI_GetBusDevHelper(struct controller *ctrl, u8 *bus_num, u8 *dev_num, u8 slot, u8 nobridge)
245{
Alex Chiangb019ee62009-03-31 09:24:02 -0600246 int loop, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 u32 work;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 u8 tbus, tdevice, tslot;
249
Alex Chiangb019ee62009-03-31 09:24:02 -0600250 len = cpqhp_routing_table_length();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 for (loop = 0; loop < len; ++loop) {
Alex Chiangb019ee62009-03-31 09:24:02 -0600252 tbus = cpqhp_routing_table->slots[loop].bus;
253 tdevice = cpqhp_routing_table->slots[loop].devfn;
254 tslot = cpqhp_routing_table->slots[loop].slot;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
256 if (tslot == slot) {
257 *bus_num = tbus;
258 *dev_num = tdevice;
259 ctrl->pci_bus->number = tbus;
260 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_VENDOR_ID, &work);
Alex Chiangb019ee62009-03-31 09:24:02 -0600261 if (!nobridge || (work == 0xffffffff))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 dbg("bus_num %d devfn %d\n", *bus_num, *dev_num);
265 pci_bus_read_config_dword (ctrl->pci_bus, *dev_num, PCI_CLASS_REVISION, &work);
266 dbg("work >> 8 (%x) = BRIDGE (%x)\n", work >> 8, PCI_TO_PCI_BRIDGE_CLASS);
267
268 if ((work >> 8) == PCI_TO_PCI_BRIDGE_CLASS) {
269 pci_bus_read_config_byte (ctrl->pci_bus, *dev_num, PCI_SECONDARY_BUS, &tbus);
270 dbg("Scan bus for Non Bridge: bus %d\n", tbus);
271 if (PCI_ScanBusForNonBridge(ctrl, tbus, dev_num) == 0) {
272 *bus_num = tbus;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 return 0;
274 }
Alex Chiangb019ee62009-03-31 09:24:02 -0600275 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 }
278 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -1;
280}
281
282
283int cpqhp_get_bus_dev (struct controller *ctrl, u8 * bus_num, u8 * dev_num, u8 slot)
284{
Alex Chiang427438c2009-03-31 09:23:16 -0600285 /* plain (bridges allowed) */
286 return PCI_GetBusDevHelper(ctrl, bus_num, dev_num, slot, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
288
289
Alex Chiang427438c2009-03-31 09:23:16 -0600290/* More PCI configuration routines; this time centered around hotplug
291 * controller
292 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
294
295/*
296 * cpqhp_save_config
297 *
298 * Reads configuration for all slots in a PCI bus and saves info.
299 *
300 * Note: For non-hot plug busses, the slot # saved is the device #
301 *
302 * returns 0 if success
303 */
304int cpqhp_save_config(struct controller *ctrl, int busnumber, int is_hot_plug)
305{
306 long rc;
307 u8 class_code;
308 u8 header_type;
309 u32 ID;
310 u8 secondary_bus;
311 struct pci_func *new_slot;
312 int sub_bus;
313 int FirstSupported;
314 int LastSupported;
315 int max_functions;
316 int function;
317 u8 DevError;
318 int device = 0;
319 int cloop = 0;
320 int stop_it;
321 int index;
322
Alex Chiang427438c2009-03-31 09:23:16 -0600323 /* Decide which slots are supported */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
325 if (is_hot_plug) {
Alex Chiang427438c2009-03-31 09:23:16 -0600326 /*
327 * is_hot_plug is the slot mask
328 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 FirstSupported = is_hot_plug >> 4;
330 LastSupported = FirstSupported + (is_hot_plug & 0x0F) - 1;
331 } else {
332 FirstSupported = 0;
333 LastSupported = 0x1F;
334 }
335
Alex Chiang427438c2009-03-31 09:23:16 -0600336 /* Save PCI configuration space for all devices in supported slots */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 ctrl->pci_bus->number = busnumber;
338 for (device = FirstSupported; device <= LastSupported; device++) {
339 ID = 0xFFFFFFFF;
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600340 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_VENDOR_ID, &ID);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600342 if (ID == 0xFFFFFFFF) {
343 if (is_hot_plug) {
344 /* Setup slot structure with entry for empty
345 * slot
346 */
347 new_slot = cpqhp_slot_create(busnumber);
348 if (new_slot == NULL)
349 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
351 new_slot->bus = (u8) busnumber;
352 new_slot->device = (u8) device;
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600353 new_slot->function = 0;
354 new_slot->is_a_board = 0;
355 new_slot->presence_save = 0;
356 new_slot->switch_save = 0;
357 }
358 continue;
359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600361 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), 0x0B, &class_code);
362 if (rc)
363 return rc;
364
365 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, 0), PCI_HEADER_TYPE, &header_type);
366 if (rc)
367 return rc;
368
369 /* If multi-function device, set max_functions to 8 */
370 if (header_type & 0x80)
371 max_functions = 8;
372 else
373 max_functions = 1;
374
375 function = 0;
376
377 do {
378 DevError = 0;
379 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
380 /* Recurse the subordinate bus
381 * get the subordinate bus number
382 */
383 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_SECONDARY_BUS, &secondary_bus);
384 if (rc) {
385 return rc;
386 } else {
387 sub_bus = (int) secondary_bus;
388
389 /* Save secondary bus cfg spc
390 * with this recursive call.
391 */
392 rc = cpqhp_save_config(ctrl, sub_bus, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (rc)
394 return rc;
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600395 ctrl->pci_bus->number = busnumber;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600399 index = 0;
400 new_slot = cpqhp_slot_find(busnumber, device, index++);
401 while (new_slot &&
402 (new_slot->function != (u8) function))
403 new_slot = cpqhp_slot_find(busnumber, device, index++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600405 if (!new_slot) {
406 /* Setup slot structure. */
407 new_slot = cpqhp_slot_create(busnumber);
408 if (new_slot == NULL)
409 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 }
411
412 new_slot->bus = (u8) busnumber;
413 new_slot->device = (u8) device;
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600414 new_slot->function = (u8) function;
415 new_slot->is_a_board = 1;
416 new_slot->switch_save = 0x10;
417 /* In case of unsupported board */
418 new_slot->status = DevError;
419 new_slot->pci_dev = pci_find_slot(new_slot->bus, (new_slot->device << 3) | new_slot->function);
420
421 for (cloop = 0; cloop < 0x20; cloop++) {
422 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
423 if (rc)
424 return rc;
425 }
426
427 function++;
428
429 stop_it = 0;
430
431 /* this loop skips to the next present function
432 * reading in Class Code and Header type.
433 */
434 while ((function < max_functions) && (!stop_it)) {
435 rc = pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_VENDOR_ID, &ID);
436 if (ID == 0xFFFFFFFF) {
437 function++;
438 continue;
439 }
440 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), 0x0B, &class_code);
441 if (rc)
442 return rc;
443
444 rc = pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(device, function), PCI_HEADER_TYPE, &header_type);
445 if (rc)
446 return rc;
447
448 stop_it++;
449 }
450
451 } while (function < max_functions);
Alex Chiang427438c2009-03-31 09:23:16 -0600452 } /* End of FOR loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Alex Chiang1d2e8b12009-03-31 09:23:57 -0600454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
457
458/*
459 * cpqhp_save_slot_config
460 *
461 * Saves configuration info for all PCI devices in a given slot
462 * including subordinate busses.
463 *
464 * returns 0 if success
465 */
466int cpqhp_save_slot_config (struct controller *ctrl, struct pci_func * new_slot)
467{
468 long rc;
469 u8 class_code;
470 u8 header_type;
471 u32 ID;
472 u8 secondary_bus;
473 int sub_bus;
474 int max_functions;
Alex Chiangde86ae12009-03-31 09:23:46 -0600475 int function = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int cloop = 0;
477 int stop_it;
478
479 ID = 0xFFFFFFFF;
480
481 ctrl->pci_bus->number = new_slot->bus;
482 pci_bus_read_config_dword (ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_VENDOR_ID, &ID);
483
Alex Chiangde86ae12009-03-31 09:23:46 -0600484 if (ID == 0xFFFFFFFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 2;
Alex Chiangde86ae12009-03-31 09:23:46 -0600486
487 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), 0x0B, &class_code);
488 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, 0), PCI_HEADER_TYPE, &header_type);
489
490 if (header_type & 0x80) /* Multi-function device */
491 max_functions = 8;
492 else
493 max_functions = 1;
494
495 while (function < max_functions) {
496 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
497 /* Recurse the subordinate bus */
498 pci_bus_read_config_byte (ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_SECONDARY_BUS, &secondary_bus);
499
500 sub_bus = (int) secondary_bus;
501
502 /* Save the config headers for the secondary
503 * bus.
504 */
505 rc = cpqhp_save_config(ctrl, sub_bus, 0);
506 if (rc)
507 return(rc);
508 ctrl->pci_bus->number = new_slot->bus;
509
510 }
511
512 new_slot->status = 0;
513
514 for (cloop = 0; cloop < 0x20; cloop++)
515 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), cloop << 2, (u32 *) & (new_slot-> config_space [cloop]));
516
517 function++;
518
519 stop_it = 0;
520
521 /* this loop skips to the next present function
522 * reading in the Class Code and the Header type.
523 */
524 while ((function < max_functions) && (!stop_it)) {
525 pci_bus_read_config_dword(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_VENDOR_ID, &ID);
526
527 if (ID == 0xFFFFFFFF)
528 function++;
529 else {
530 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), 0x0B, &class_code);
531 pci_bus_read_config_byte(ctrl->pci_bus, PCI_DEVFN(new_slot->device, function), PCI_HEADER_TYPE, &header_type);
532 stop_it++;
533 }
534 }
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
537
538 return 0;
539}
540
541
542/*
543 * cpqhp_save_base_addr_length
544 *
545 * Saves the length of all base address registers for the
546 * specified slot. this is for hot plug REPLACE
547 *
548 * returns 0 if success
549 */
550int cpqhp_save_base_addr_length(struct controller *ctrl, struct pci_func * func)
551{
552 u8 cloop;
553 u8 header_type;
554 u8 secondary_bus;
555 u8 type;
556 int sub_bus;
557 u32 temp_register;
558 u32 base;
559 u32 rc;
560 struct pci_func *next;
561 int index = 0;
562 struct pci_bus *pci_bus = ctrl->pci_bus;
563 unsigned int devfn;
564
565 func = cpqhp_slot_find(func->bus, func->device, index++);
566
567 while (func != NULL) {
568 pci_bus->number = func->bus;
569 devfn = PCI_DEVFN(func->device, func->function);
570
Alex Chiang427438c2009-03-31 09:23:16 -0600571 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
573
574 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
576
577 sub_bus = (int) secondary_bus;
578
579 next = cpqhp_slot_list[sub_bus];
580
581 while (next != NULL) {
582 rc = cpqhp_save_base_addr_length(ctrl, next);
583 if (rc)
584 return rc;
585
586 next = next->next;
587 }
588 pci_bus->number = func->bus;
589
Alex Chiang427438c2009-03-31 09:23:16 -0600590 /* FIXME: this loop is duplicated in the non-bridge
591 * case. The two could be rolled together Figure out
592 * IO and memory base lengths
593 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
595 temp_register = 0xFFFFFFFF;
596 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
597 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
Alex Chiang427438c2009-03-31 09:23:16 -0600598 /* If this register is implemented */
599 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -0600601 /* IO base
602 * set base = amount of IO space
603 * requested
604 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 base = base & 0xFFFFFFFE;
606 base = (~base) + 1;
607
608 type = 1;
609 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600610 /* memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 base = base & 0xFFFFFFF0;
612 base = (~base) + 1;
613
614 type = 0;
615 }
616 } else {
617 base = 0x0L;
618 type = 0;
619 }
620
Alex Chiang427438c2009-03-31 09:23:16 -0600621 /* Save information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 func->base_length[(cloop - 0x10) >> 2] =
623 base;
624 func->base_type[(cloop - 0x10) >> 2] = type;
625
Alex Chiang427438c2009-03-31 09:23:16 -0600626 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
Alex Chiang427438c2009-03-31 09:23:16 -0600628 } else if ((header_type & 0x7F) == 0x00) {
629 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
631 temp_register = 0xFFFFFFFF;
632 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
633 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
634
Alex Chiang427438c2009-03-31 09:23:16 -0600635 /* If this register is implemented */
636 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -0600638 /* IO base
639 * base = amount of IO space
640 * requested
641 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 base = base & 0xFFFFFFFE;
643 base = (~base) + 1;
644
645 type = 1;
646 } else {
Alex Chiang427438c2009-03-31 09:23:16 -0600647 /* memory base
648 * base = amount of memory
649 * space requested
650 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 base = base & 0xFFFFFFF0;
652 base = (~base) + 1;
653
654 type = 0;
655 }
656 } else {
657 base = 0x0L;
658 type = 0;
659 }
660
Alex Chiang427438c2009-03-31 09:23:16 -0600661 /* Save information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 func->base_length[(cloop - 0x10) >> 2] = base;
663 func->base_type[(cloop - 0x10) >> 2] = type;
664
Alex Chiang427438c2009-03-31 09:23:16 -0600665 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Alex Chiang427438c2009-03-31 09:23:16 -0600667 } else { /* Some other unknown header type */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 }
669
Alex Chiang427438c2009-03-31 09:23:16 -0600670 /* find the next device in this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 func = cpqhp_slot_find(func->bus, func->device, index++);
672 }
673
674 return(0);
675}
676
677
678/*
679 * cpqhp_save_used_resources
680 *
681 * Stores used resource information for existing boards. this is
682 * for boards that were in the system when this driver was loaded.
683 * this function is for hot plug ADD
684 *
685 * returns 0 if success
686 */
687int cpqhp_save_used_resources (struct controller *ctrl, struct pci_func * func)
688{
689 u8 cloop;
690 u8 header_type;
691 u8 secondary_bus;
692 u8 temp_byte;
693 u8 b_base;
694 u8 b_length;
695 u16 command;
696 u16 save_command;
697 u16 w_base;
698 u16 w_length;
699 u32 temp_register;
700 u32 save_base;
701 u32 base;
702 int index = 0;
703 struct pci_resource *mem_node;
704 struct pci_resource *p_mem_node;
705 struct pci_resource *io_node;
706 struct pci_resource *bus_node;
707 struct pci_bus *pci_bus = ctrl->pci_bus;
708 unsigned int devfn;
709
710 func = cpqhp_slot_find(func->bus, func->device, index++);
711
712 while ((func != NULL) && func->is_a_board) {
713 pci_bus->number = func->bus;
714 devfn = PCI_DEVFN(func->device, func->function);
715
Alex Chiang427438c2009-03-31 09:23:16 -0600716 /* Save the command register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 pci_bus_read_config_word(pci_bus, devfn, PCI_COMMAND, &save_command);
718
Alex Chiang427438c2009-03-31 09:23:16 -0600719 /* disable card */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 command = 0x00;
721 pci_bus_write_config_word(pci_bus, devfn, PCI_COMMAND, command);
722
Alex Chiang427438c2009-03-31 09:23:16 -0600723 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 pci_bus_read_config_byte(pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
725
Alex Chiang427438c2009-03-31 09:23:16 -0600726 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
727 /* Clear Bridge Control Register */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 command = 0x00;
729 pci_bus_write_config_word(pci_bus, devfn, PCI_BRIDGE_CONTROL, command);
730 pci_bus_read_config_byte(pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
731 pci_bus_read_config_byte(pci_bus, devfn, PCI_SUBORDINATE_BUS, &temp_byte);
732
733 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
734 if (!bus_node)
735 return -ENOMEM;
736
737 bus_node->base = secondary_bus;
738 bus_node->length = temp_byte - secondary_bus + 1;
739
740 bus_node->next = func->bus_head;
741 func->bus_head = bus_node;
742
Alex Chiang427438c2009-03-31 09:23:16 -0600743 /* Save IO base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_BASE, &b_base);
745 pci_bus_read_config_byte(pci_bus, devfn, PCI_IO_LIMIT, &b_length);
746
747 if ((b_base <= b_length) && (save_command & 0x01)) {
748 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
749 if (!io_node)
750 return -ENOMEM;
751
752 io_node->base = (b_base & 0xF0) << 8;
753 io_node->length = (b_length - b_base + 0x10) << 8;
754
755 io_node->next = func->io_head;
756 func->io_head = io_node;
757 }
758
Alex Chiang427438c2009-03-31 09:23:16 -0600759 /* Save memory base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_BASE, &w_base);
761 pci_bus_read_config_word(pci_bus, devfn, PCI_MEMORY_LIMIT, &w_length);
762
763 if ((w_base <= w_length) && (save_command & 0x02)) {
764 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
765 if (!mem_node)
766 return -ENOMEM;
767
768 mem_node->base = w_base << 16;
769 mem_node->length = (w_length - w_base + 0x10) << 16;
770
771 mem_node->next = func->mem_head;
772 func->mem_head = mem_node;
773 }
774
Alex Chiang427438c2009-03-31 09:23:16 -0600775 /* Save prefetchable memory base and Limit registers */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_BASE, &w_base);
777 pci_bus_read_config_word(pci_bus, devfn, PCI_PREF_MEMORY_LIMIT, &w_length);
778
779 if ((w_base <= w_length) && (save_command & 0x02)) {
780 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
781 if (!p_mem_node)
782 return -ENOMEM;
783
784 p_mem_node->base = w_base << 16;
785 p_mem_node->length = (w_length - w_base + 0x10) << 16;
786
787 p_mem_node->next = func->p_mem_head;
788 func->p_mem_head = p_mem_node;
789 }
Alex Chiang427438c2009-03-31 09:23:16 -0600790 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 for (cloop = 0x10; cloop <= 0x14; cloop += 4) {
792 pci_bus_read_config_dword (pci_bus, devfn, cloop, &save_base);
793
794 temp_register = 0xFFFFFFFF;
795 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
796 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
797
798 temp_register = base;
799
Alex Chiang427438c2009-03-31 09:23:16 -0600800 /* If this register is implemented */
801 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (((base & 0x03L) == 0x01)
803 && (save_command & 0x01)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600804 /* IO base
805 * set temp_register = amount
806 * of IO space requested
807 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 temp_register = base & 0xFFFFFFFE;
809 temp_register = (~temp_register) + 1;
810
811 io_node = kmalloc(sizeof(*io_node),
812 GFP_KERNEL);
813 if (!io_node)
814 return -ENOMEM;
815
816 io_node->base =
817 save_base & (~0x03L);
818 io_node->length = temp_register;
819
820 io_node->next = func->io_head;
821 func->io_head = io_node;
822 } else
823 if (((base & 0x0BL) == 0x08)
824 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600825 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 temp_register = base & 0xFFFFFFF0;
827 temp_register = (~temp_register) + 1;
828
829 p_mem_node = kmalloc(sizeof(*p_mem_node),
830 GFP_KERNEL);
831 if (!p_mem_node)
832 return -ENOMEM;
833
834 p_mem_node->base = save_base & (~0x0FL);
835 p_mem_node->length = temp_register;
836
837 p_mem_node->next = func->p_mem_head;
838 func->p_mem_head = p_mem_node;
839 } else
840 if (((base & 0x0BL) == 0x00)
841 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600842 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 temp_register = base & 0xFFFFFFF0;
844 temp_register = (~temp_register) + 1;
845
846 mem_node = kmalloc(sizeof(*mem_node),
847 GFP_KERNEL);
848 if (!mem_node)
849 return -ENOMEM;
850
851 mem_node->base = save_base & (~0x0FL);
852 mem_node->length = temp_register;
853
854 mem_node->next = func->mem_head;
855 func->mem_head = mem_node;
856 } else
857 return(1);
858 }
Alex Chiang427438c2009-03-31 09:23:16 -0600859 } /* End of base register loop */
860 /* Standard header */
861 } else if ((header_type & 0x7F) == 0x00) {
862 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
864 pci_bus_read_config_dword(pci_bus, devfn, cloop, &save_base);
865
866 temp_register = 0xFFFFFFFF;
867 pci_bus_write_config_dword(pci_bus, devfn, cloop, temp_register);
868 pci_bus_read_config_dword(pci_bus, devfn, cloop, &base);
869
870 temp_register = base;
871
Alex Chiang427438c2009-03-31 09:23:16 -0600872 /* If this register is implemented */
873 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (((base & 0x03L) == 0x01)
875 && (save_command & 0x01)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600876 /* IO base
877 * set temp_register = amount
878 * of IO space requested
879 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 temp_register = base & 0xFFFFFFFE;
881 temp_register = (~temp_register) + 1;
882
883 io_node = kmalloc(sizeof(*io_node),
884 GFP_KERNEL);
885 if (!io_node)
886 return -ENOMEM;
887
888 io_node->base = save_base & (~0x01L);
889 io_node->length = temp_register;
890
891 io_node->next = func->io_head;
892 func->io_head = io_node;
893 } else
894 if (((base & 0x0BL) == 0x08)
895 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600896 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 temp_register = base & 0xFFFFFFF0;
898 temp_register = (~temp_register) + 1;
899
900 p_mem_node = kmalloc(sizeof(*p_mem_node),
901 GFP_KERNEL);
902 if (!p_mem_node)
903 return -ENOMEM;
904
905 p_mem_node->base = save_base & (~0x0FL);
906 p_mem_node->length = temp_register;
907
908 p_mem_node->next = func->p_mem_head;
909 func->p_mem_head = p_mem_node;
910 } else
911 if (((base & 0x0BL) == 0x00)
912 && (save_command & 0x02)) {
Alex Chiang427438c2009-03-31 09:23:16 -0600913 /* prefetchable memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 temp_register = base & 0xFFFFFFF0;
915 temp_register = (~temp_register) + 1;
916
917 mem_node = kmalloc(sizeof(*mem_node),
918 GFP_KERNEL);
919 if (!mem_node)
920 return -ENOMEM;
921
922 mem_node->base = save_base & (~0x0FL);
923 mem_node->length = temp_register;
924
925 mem_node->next = func->mem_head;
926 func->mem_head = mem_node;
927 } else
928 return(1);
929 }
Alex Chiang427438c2009-03-31 09:23:16 -0600930 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 }
932
Alex Chiang427438c2009-03-31 09:23:16 -0600933 /* find the next device in this slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 func = cpqhp_slot_find(func->bus, func->device, index++);
935 }
936
Alex Chiang4aabb582009-03-31 09:23:52 -0600937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938}
939
940
941/*
942 * cpqhp_configure_board
943 *
944 * Copies saved configuration information to one slot.
945 * this is called recursively for bridge devices.
946 * this is for hot plug REPLACE!
947 *
948 * returns 0 if success
949 */
950int cpqhp_configure_board(struct controller *ctrl, struct pci_func * func)
951{
952 int cloop;
953 u8 header_type;
954 u8 secondary_bus;
955 int sub_bus;
956 struct pci_func *next;
957 u32 temp;
958 u32 rc;
959 int index = 0;
960 struct pci_bus *pci_bus = ctrl->pci_bus;
961 unsigned int devfn;
962
963 func = cpqhp_slot_find(func->bus, func->device, index++);
964
965 while (func != NULL) {
966 pci_bus->number = func->bus;
967 devfn = PCI_DEVFN(func->device, func->function);
968
Alex Chiang427438c2009-03-31 09:23:16 -0600969 /* Start at the top of config space so that the control
970 * registers are programmed last
971 */
Alex Chiang4aabb582009-03-31 09:23:52 -0600972 for (cloop = 0x3C; cloop > 0; cloop -= 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 pci_bus_write_config_dword (pci_bus, devfn, cloop, func->config_space[cloop >> 2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
975 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
976
Alex Chiang427438c2009-03-31 09:23:16 -0600977 /* If this is a bridge device, restore subordinate devices */
978 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 pci_bus_read_config_byte (pci_bus, devfn, PCI_SECONDARY_BUS, &secondary_bus);
980
981 sub_bus = (int) secondary_bus;
982
983 next = cpqhp_slot_list[sub_bus];
984
985 while (next != NULL) {
986 rc = cpqhp_configure_board(ctrl, next);
987 if (rc)
988 return rc;
989
990 next = next->next;
991 }
992 } else {
993
Alex Chiang427438c2009-03-31 09:23:16 -0600994 /* Check all the base Address Registers to make sure
995 * they are the same. If not, the board is different.
996 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 for (cloop = 16; cloop < 40; cloop += 4) {
999 pci_bus_read_config_dword (pci_bus, devfn, cloop, &temp);
1000
1001 if (temp != func->config_space[cloop >> 2]) {
1002 dbg("Config space compare failure!!! offset = %x\n", cloop);
1003 dbg("bus = %x, device = %x, function = %x\n", func->bus, func->device, func->function);
1004 dbg("temp = %x, config space = %x\n\n", temp, func->config_space[cloop >> 2]);
1005 return 1;
1006 }
1007 }
1008 }
1009
1010 func->configured = 1;
1011
1012 func = cpqhp_slot_find(func->bus, func->device, index++);
1013 }
1014
1015 return 0;
1016}
1017
1018
1019/*
1020 * cpqhp_valid_replace
1021 *
1022 * this function checks to see if a board is the same as the
1023 * one it is replacing. this check will detect if the device's
1024 * vendor or device id's are the same
1025 *
1026 * returns 0 if the board is the same nonzero otherwise
1027 */
1028int cpqhp_valid_replace(struct controller *ctrl, struct pci_func * func)
1029{
1030 u8 cloop;
1031 u8 header_type;
1032 u8 secondary_bus;
1033 u8 type;
1034 u32 temp_register = 0;
1035 u32 base;
1036 u32 rc;
1037 struct pci_func *next;
1038 int index = 0;
1039 struct pci_bus *pci_bus = ctrl->pci_bus;
1040 unsigned int devfn;
1041
1042 if (!func->is_a_board)
1043 return(ADD_NOT_SUPPORTED);
1044
1045 func = cpqhp_slot_find(func->bus, func->device, index++);
1046
1047 while (func != NULL) {
1048 pci_bus->number = func->bus;
1049 devfn = PCI_DEVFN(func->device, func->function);
1050
1051 pci_bus_read_config_dword (pci_bus, devfn, PCI_VENDOR_ID, &temp_register);
1052
Alex Chiang427438c2009-03-31 09:23:16 -06001053 /* No adapter present */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 if (temp_register == 0xFFFFFFFF)
1055 return(NO_ADAPTER_PRESENT);
1056
1057 if (temp_register != func->config_space[0])
1058 return(ADAPTER_NOT_SAME);
1059
Alex Chiang427438c2009-03-31 09:23:16 -06001060 /* Check for same revision number and class code */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 pci_bus_read_config_dword (pci_bus, devfn, PCI_CLASS_REVISION, &temp_register);
1062
Alex Chiang427438c2009-03-31 09:23:16 -06001063 /* Adapter not the same */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 if (temp_register != func->config_space[0x08 >> 2])
1065 return(ADAPTER_NOT_SAME);
1066
Alex Chiang427438c2009-03-31 09:23:16 -06001067 /* Check for Bridge */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 pci_bus_read_config_byte (pci_bus, devfn, PCI_HEADER_TYPE, &header_type);
1069
Alex Chiang427438c2009-03-31 09:23:16 -06001070 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) {
1071 /* In order to continue checking, we must program the
1072 * bus registers in the bridge to respond to accesses
1073 * for its subordinate bus(es)
1074 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
1076 temp_register = func->config_space[0x18 >> 2];
1077 pci_bus_write_config_dword (pci_bus, devfn, PCI_PRIMARY_BUS, temp_register);
1078
1079 secondary_bus = (temp_register >> 8) & 0xFF;
1080
1081 next = cpqhp_slot_list[secondary_bus];
1082
1083 while (next != NULL) {
1084 rc = cpqhp_valid_replace(ctrl, next);
1085 if (rc)
1086 return rc;
1087
1088 next = next->next;
1089 }
1090
1091 }
Alex Chiang427438c2009-03-31 09:23:16 -06001092 /* Check to see if it is a standard config header */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 else if ((header_type & 0x7F) == PCI_HEADER_TYPE_NORMAL) {
Alex Chiang427438c2009-03-31 09:23:16 -06001094 /* Check subsystem vendor and ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 pci_bus_read_config_dword (pci_bus, devfn, PCI_SUBSYSTEM_VENDOR_ID, &temp_register);
1096
1097 if (temp_register != func->config_space[0x2C >> 2]) {
Alex Chiang427438c2009-03-31 09:23:16 -06001098 /* If it's a SMART-2 and the register isn't
1099 * filled in, ignore the difference because
1100 * they just have an old rev of the firmware
1101 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 if (!((func->config_space[0] == 0xAE100E11)
1103 && (temp_register == 0x00L)))
1104 return(ADAPTER_NOT_SAME);
1105 }
Alex Chiang427438c2009-03-31 09:23:16 -06001106 /* Figure out IO and memory base lengths */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 for (cloop = 0x10; cloop <= 0x24; cloop += 4) {
1108 temp_register = 0xFFFFFFFF;
1109 pci_bus_write_config_dword (pci_bus, devfn, cloop, temp_register);
1110 pci_bus_read_config_dword (pci_bus, devfn, cloop, &base);
Alex Chiang427438c2009-03-31 09:23:16 -06001111
1112 /* If this register is implemented */
1113 if (base) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 if (base & 0x01L) {
Alex Chiang427438c2009-03-31 09:23:16 -06001115 /* IO base
1116 * set base = amount of IO
1117 * space requested
1118 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 base = base & 0xFFFFFFFE;
1120 base = (~base) + 1;
1121
1122 type = 1;
1123 } else {
Alex Chiang427438c2009-03-31 09:23:16 -06001124 /* memory base */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 base = base & 0xFFFFFFF0;
1126 base = (~base) + 1;
1127
1128 type = 0;
1129 }
1130 } else {
1131 base = 0x0L;
1132 type = 0;
1133 }
1134
Alex Chiang427438c2009-03-31 09:23:16 -06001135 /* Check information in slot structure */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (func->base_length[(cloop - 0x10) >> 2] != base)
1137 return(ADAPTER_NOT_SAME);
1138
1139 if (func->base_type[(cloop - 0x10) >> 2] != type)
1140 return(ADAPTER_NOT_SAME);
1141
Alex Chiang427438c2009-03-31 09:23:16 -06001142 } /* End of base register loop */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Alex Chiang427438c2009-03-31 09:23:16 -06001144 } /* End of (type 0 config space) else */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 else {
Alex Chiang427438c2009-03-31 09:23:16 -06001146 /* this is not a type 0 or 1 config space header so
1147 * we don't know how to do it
1148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149 return(DEVICE_TYPE_NOT_SUPPORTED);
1150 }
1151
Alex Chiang427438c2009-03-31 09:23:16 -06001152 /* Get the next function */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 func = cpqhp_slot_find(func->bus, func->device, index++);
1154 }
1155
1156
1157 return 0;
1158}
1159
1160
1161/*
1162 * cpqhp_find_available_resources
1163 *
1164 * Finds available memory, IO, and IRQ resources for programming
1165 * devices which may be added to the system
1166 * this function is for hot plug ADD!
1167 *
1168 * returns 0 if success
Alex Chiang861fefb2009-03-31 09:23:11 -06001169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170int cpqhp_find_available_resources(struct controller *ctrl, void __iomem *rom_start)
1171{
1172 u8 temp;
1173 u8 populated_slot;
1174 u8 bridged_slot;
1175 void __iomem *one_slot;
1176 void __iomem *rom_resource_table;
1177 struct pci_func *func = NULL;
1178 int i = 10, index;
1179 u32 temp_dword, rc;
1180 struct pci_resource *mem_node;
1181 struct pci_resource *p_mem_node;
1182 struct pci_resource *io_node;
1183 struct pci_resource *bus_node;
1184
1185 rom_resource_table = detect_HRT_floating_pointer(rom_start, rom_start+0xffff);
1186 dbg("rom_resource_table = %p\n", rom_resource_table);
1187
Alex Chiang4aabb582009-03-31 09:23:52 -06001188 if (rom_resource_table == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return -ENODEV;
Alex Chiang4aabb582009-03-31 09:23:52 -06001190
Alex Chiang427438c2009-03-31 09:23:16 -06001191 /* Sum all resources and setup resource maps */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 unused_IRQ = readl(rom_resource_table + UNUSED_IRQ);
1193 dbg("unused_IRQ = %x\n", unused_IRQ);
1194
1195 temp = 0;
1196 while (unused_IRQ) {
1197 if (unused_IRQ & 1) {
1198 cpqhp_disk_irq = temp;
1199 break;
1200 }
1201 unused_IRQ = unused_IRQ >> 1;
1202 temp++;
1203 }
1204
1205 dbg("cpqhp_disk_irq= %d\n", cpqhp_disk_irq);
1206 unused_IRQ = unused_IRQ >> 1;
1207 temp++;
1208
1209 while (unused_IRQ) {
1210 if (unused_IRQ & 1) {
1211 cpqhp_nic_irq = temp;
1212 break;
1213 }
1214 unused_IRQ = unused_IRQ >> 1;
1215 temp++;
1216 }
1217
1218 dbg("cpqhp_nic_irq= %d\n", cpqhp_nic_irq);
1219 unused_IRQ = readl(rom_resource_table + PCIIRQ);
1220
1221 temp = 0;
1222
Alex Chiang4aabb582009-03-31 09:23:52 -06001223 if (!cpqhp_nic_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 cpqhp_nic_irq = ctrl->cfgspc_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Alex Chiang4aabb582009-03-31 09:23:52 -06001226 if (!cpqhp_disk_irq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 cpqhp_disk_irq = ctrl->cfgspc_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 dbg("cpqhp_disk_irq, cpqhp_nic_irq= %d, %d\n", cpqhp_disk_irq, cpqhp_nic_irq);
1230
1231 rc = compaq_nvram_load(rom_start, ctrl);
1232 if (rc)
1233 return rc;
1234
1235 one_slot = rom_resource_table + sizeof (struct hrt);
1236
1237 i = readb(rom_resource_table + NUMBER_OF_ENTRIES);
1238 dbg("number_of_entries = %d\n", i);
1239
1240 if (!readb(one_slot + SECONDARY_BUS))
1241 return 1;
1242
1243 dbg("dev|IO base|length|Mem base|length|Pre base|length|PB SB MB\n");
1244
1245 while (i && readb(one_slot + SECONDARY_BUS)) {
1246 u8 dev_func = readb(one_slot + DEV_FUNC);
1247 u8 primary_bus = readb(one_slot + PRIMARY_BUS);
1248 u8 secondary_bus = readb(one_slot + SECONDARY_BUS);
1249 u8 max_bus = readb(one_slot + MAX_BUS);
1250 u16 io_base = readw(one_slot + IO_BASE);
1251 u16 io_length = readw(one_slot + IO_LENGTH);
1252 u16 mem_base = readw(one_slot + MEM_BASE);
1253 u16 mem_length = readw(one_slot + MEM_LENGTH);
1254 u16 pre_mem_base = readw(one_slot + PRE_MEM_BASE);
1255 u16 pre_mem_length = readw(one_slot + PRE_MEM_LENGTH);
1256
1257 dbg("%2.2x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x | %4.4x |%2.2x %2.2x %2.2x\n",
1258 dev_func, io_base, io_length, mem_base, mem_length, pre_mem_base, pre_mem_length,
1259 primary_bus, secondary_bus, max_bus);
1260
Alex Chiang427438c2009-03-31 09:23:16 -06001261 /* If this entry isn't for our controller's bus, ignore it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (primary_bus != ctrl->bus) {
1263 i--;
1264 one_slot += sizeof (struct slot_rt);
1265 continue;
1266 }
Alex Chiang427438c2009-03-31 09:23:16 -06001267 /* find out if this entry is for an occupied slot */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 ctrl->pci_bus->number = primary_bus;
1269 pci_bus_read_config_dword (ctrl->pci_bus, dev_func, PCI_VENDOR_ID, &temp_dword);
1270 dbg("temp_D_word = %x\n", temp_dword);
1271
1272 if (temp_dword != 0xFFFFFFFF) {
1273 index = 0;
1274 func = cpqhp_slot_find(primary_bus, dev_func >> 3, 0);
1275
1276 while (func && (func->function != (dev_func & 0x07))) {
1277 dbg("func = %p (bus, dev, fun) = (%d, %d, %d)\n", func, primary_bus, dev_func >> 3, index);
1278 func = cpqhp_slot_find(primary_bus, dev_func >> 3, index++);
1279 }
1280
Alex Chiang427438c2009-03-31 09:23:16 -06001281 /* If we can't find a match, skip this table entry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 if (!func) {
1283 i--;
1284 one_slot += sizeof (struct slot_rt);
1285 continue;
1286 }
Alex Chiang427438c2009-03-31 09:23:16 -06001287 /* this may not work and shouldn't be used */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 if (secondary_bus != primary_bus)
1289 bridged_slot = 1;
1290 else
1291 bridged_slot = 0;
1292
1293 populated_slot = 1;
1294 } else {
1295 populated_slot = 0;
1296 bridged_slot = 0;
1297 }
1298
1299
Alex Chiang427438c2009-03-31 09:23:16 -06001300 /* If we've got a valid IO base, use it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 temp_dword = io_base + io_length;
1303
1304 if ((io_base) && (temp_dword < 0x10000)) {
1305 io_node = kmalloc(sizeof(*io_node), GFP_KERNEL);
1306 if (!io_node)
1307 return -ENOMEM;
1308
1309 io_node->base = io_base;
1310 io_node->length = io_length;
1311
1312 dbg("found io_node(base, length) = %x, %x\n",
1313 io_node->base, io_node->length);
1314 dbg("populated slot =%d \n", populated_slot);
1315 if (!populated_slot) {
1316 io_node->next = ctrl->io_head;
1317 ctrl->io_head = io_node;
1318 } else {
1319 io_node->next = func->io_head;
1320 func->io_head = io_node;
1321 }
1322 }
1323
Alex Chiang427438c2009-03-31 09:23:16 -06001324 /* If we've got a valid memory base, use it */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 temp_dword = mem_base + mem_length;
1326 if ((mem_base) && (temp_dword < 0x10000)) {
1327 mem_node = kmalloc(sizeof(*mem_node), GFP_KERNEL);
1328 if (!mem_node)
1329 return -ENOMEM;
1330
1331 mem_node->base = mem_base << 16;
1332
1333 mem_node->length = mem_length << 16;
1334
1335 dbg("found mem_node(base, length) = %x, %x\n",
1336 mem_node->base, mem_node->length);
1337 dbg("populated slot =%d \n", populated_slot);
1338 if (!populated_slot) {
1339 mem_node->next = ctrl->mem_head;
1340 ctrl->mem_head = mem_node;
1341 } else {
1342 mem_node->next = func->mem_head;
1343 func->mem_head = mem_node;
1344 }
1345 }
1346
Alex Chiang427438c2009-03-31 09:23:16 -06001347 /* If we've got a valid prefetchable memory base, and
1348 * the base + length isn't greater than 0xFFFF
1349 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 temp_dword = pre_mem_base + pre_mem_length;
1351 if ((pre_mem_base) && (temp_dword < 0x10000)) {
1352 p_mem_node = kmalloc(sizeof(*p_mem_node), GFP_KERNEL);
1353 if (!p_mem_node)
1354 return -ENOMEM;
1355
1356 p_mem_node->base = pre_mem_base << 16;
1357
1358 p_mem_node->length = pre_mem_length << 16;
1359 dbg("found p_mem_node(base, length) = %x, %x\n",
1360 p_mem_node->base, p_mem_node->length);
1361 dbg("populated slot =%d \n", populated_slot);
1362
1363 if (!populated_slot) {
1364 p_mem_node->next = ctrl->p_mem_head;
1365 ctrl->p_mem_head = p_mem_node;
1366 } else {
1367 p_mem_node->next = func->p_mem_head;
1368 func->p_mem_head = p_mem_node;
1369 }
1370 }
1371
Alex Chiang427438c2009-03-31 09:23:16 -06001372 /* If we've got a valid bus number, use it
1373 * The second condition is to ignore bus numbers on
1374 * populated slots that don't have PCI-PCI bridges
1375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 if (secondary_bus && (secondary_bus != primary_bus)) {
1377 bus_node = kmalloc(sizeof(*bus_node), GFP_KERNEL);
1378 if (!bus_node)
1379 return -ENOMEM;
1380
1381 bus_node->base = secondary_bus;
1382 bus_node->length = max_bus - secondary_bus + 1;
1383 dbg("found bus_node(base, length) = %x, %x\n",
1384 bus_node->base, bus_node->length);
1385 dbg("populated slot =%d \n", populated_slot);
1386 if (!populated_slot) {
1387 bus_node->next = ctrl->bus_head;
1388 ctrl->bus_head = bus_node;
1389 } else {
1390 bus_node->next = func->bus_head;
1391 func->bus_head = bus_node;
1392 }
1393 }
1394
1395 i--;
1396 one_slot += sizeof (struct slot_rt);
1397 }
1398
Alex Chiang427438c2009-03-31 09:23:16 -06001399 /* If all of the following fail, we don't have any resources for
1400 * hot plug add
1401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 rc = 1;
1403 rc &= cpqhp_resource_sort_and_combine(&(ctrl->mem_head));
1404 rc &= cpqhp_resource_sort_and_combine(&(ctrl->p_mem_head));
1405 rc &= cpqhp_resource_sort_and_combine(&(ctrl->io_head));
1406 rc &= cpqhp_resource_sort_and_combine(&(ctrl->bus_head));
1407
1408 return rc;
1409}
1410
1411
1412/*
1413 * cpqhp_return_board_resources
1414 *
1415 * this routine returns all resources allocated to a board to
1416 * the available pool.
1417 *
1418 * returns 0 if success
1419 */
1420int cpqhp_return_board_resources(struct pci_func * func, struct resource_lists * resources)
1421{
1422 int rc = 0;
1423 struct pci_resource *node;
1424 struct pci_resource *t_node;
Harvey Harrison66bef8c2008-03-03 19:09:46 -08001425 dbg("%s\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
1427 if (!func)
1428 return 1;
1429
1430 node = func->io_head;
1431 func->io_head = NULL;
1432 while (node) {
1433 t_node = node->next;
1434 return_resource(&(resources->io_head), node);
1435 node = t_node;
1436 }
1437
1438 node = func->mem_head;
1439 func->mem_head = NULL;
1440 while (node) {
1441 t_node = node->next;
1442 return_resource(&(resources->mem_head), node);
1443 node = t_node;
1444 }
1445
1446 node = func->p_mem_head;
1447 func->p_mem_head = NULL;
1448 while (node) {
1449 t_node = node->next;
1450 return_resource(&(resources->p_mem_head), node);
1451 node = t_node;
1452 }
1453
1454 node = func->bus_head;
1455 func->bus_head = NULL;
1456 while (node) {
1457 t_node = node->next;
1458 return_resource(&(resources->bus_head), node);
1459 node = t_node;
1460 }
1461
1462 rc |= cpqhp_resource_sort_and_combine(&(resources->mem_head));
1463 rc |= cpqhp_resource_sort_and_combine(&(resources->p_mem_head));
1464 rc |= cpqhp_resource_sort_and_combine(&(resources->io_head));
1465 rc |= cpqhp_resource_sort_and_combine(&(resources->bus_head));
1466
1467 return rc;
1468}
1469
1470
1471/*
1472 * cpqhp_destroy_resource_list
1473 *
1474 * Puts node back in the resource list pointed to by head
1475 */
1476void cpqhp_destroy_resource_list (struct resource_lists * resources)
1477{
1478 struct pci_resource *res, *tres;
1479
1480 res = resources->io_head;
1481 resources->io_head = NULL;
1482
1483 while (res) {
1484 tres = res;
1485 res = res->next;
1486 kfree(tres);
1487 }
1488
1489 res = resources->mem_head;
1490 resources->mem_head = NULL;
1491
1492 while (res) {
1493 tres = res;
1494 res = res->next;
1495 kfree(tres);
1496 }
1497
1498 res = resources->p_mem_head;
1499 resources->p_mem_head = NULL;
1500
1501 while (res) {
1502 tres = res;
1503 res = res->next;
1504 kfree(tres);
1505 }
1506
1507 res = resources->bus_head;
1508 resources->bus_head = NULL;
1509
1510 while (res) {
1511 tres = res;
1512 res = res->next;
1513 kfree(tres);
1514 }
1515}
1516
1517
1518/*
1519 * cpqhp_destroy_board_resources
1520 *
1521 * Puts node back in the resource list pointed to by head
1522 */
1523void cpqhp_destroy_board_resources (struct pci_func * func)
1524{
1525 struct pci_resource *res, *tres;
1526
1527 res = func->io_head;
1528 func->io_head = NULL;
1529
1530 while (res) {
1531 tres = res;
1532 res = res->next;
1533 kfree(tres);
1534 }
1535
1536 res = func->mem_head;
1537 func->mem_head = NULL;
1538
1539 while (res) {
1540 tres = res;
1541 res = res->next;
1542 kfree(tres);
1543 }
1544
1545 res = func->p_mem_head;
1546 func->p_mem_head = NULL;
1547
1548 while (res) {
1549 tres = res;
1550 res = res->next;
1551 kfree(tres);
1552 }
1553
1554 res = func->bus_head;
1555 func->bus_head = NULL;
1556
1557 while (res) {
1558 tres = res;
1559 res = res->next;
1560 kfree(tres);
1561 }
1562}
1563