2 * PCI Express Hot Plug Controller Driver
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 * Copyright (C) 2003-2004 Intel Corporation
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
19 * NON INFRINGEMENT. See the GNU General Public License for more
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * Send feedback to <greg@kroah.com>, <kristen.c.accardi@intel.com>
30 #include <linux/config.h>
31 #include <linux/module.h>
32 #include <linux/kernel.h>
33 #include <linux/types.h>
34 #include <linux/slab.h>
35 #include <linux/workqueue.h>
36 #include <linux/proc_fs.h>
37 #include <linux/pci.h>
42 int pciehp_configure_device(struct slot *p_slot)
45 struct pci_bus *parent = p_slot->ctrl->pci_dev->subordinate;
48 dev = pci_find_slot(p_slot->bus, PCI_DEVFN(p_slot->device, 0));
50 err("Device %s already exists at %x:%x, cannot hot-add\n",
51 pci_name(dev), p_slot->bus, p_slot->device);
55 num = pci_scan_slot(parent, PCI_DEVFN(p_slot->device, 0));
57 err("No new device found\n");
61 for (fn = 0; fn < 8; fn++) {
62 if (!(dev = pci_find_slot(p_slot->bus,
63 PCI_DEVFN(p_slot->device, fn))))
65 if ((dev->class >> 16) == PCI_BASE_CLASS_DISPLAY) {
66 err("Cannot hot-add display device %s\n",
70 if ((dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) ||
71 (dev->hdr_type == PCI_HEADER_TYPE_CARDBUS)) {
72 /* Find an unused bus number for the new bridge */
73 struct pci_bus *child;
74 unsigned char busnr, start = parent->secondary;
75 unsigned char end = parent->subordinate;
76 for (busnr = start; busnr <= end; busnr++) {
77 if (!pci_find_bus(pci_domain_nr(parent),
82 err("No free bus for hot-added bridge\n");
85 child = pci_add_new_bus(parent, dev, busnr);
87 err("Cannot add new bus for %s\n",
91 child->subordinate = pci_do_scan_bus(child);
92 pci_bus_size_bridges(child);
94 /* TBD: program firmware provided _HPP values */
95 /* program_fw_provided_values(dev); */
98 pci_bus_assign_resources(parent);
99 pci_bus_add_devices(parent);
100 pci_enable_bridges(parent);
104 int pciehp_unconfigure_device(struct pci_func* func)
108 struct pci_bus *pbus;
110 dbg("%s: bus/dev/func = %x/%x/%x\n", __FUNCTION__, func->bus,
111 func->device, func->function);
112 pbus = func->pci_dev->bus;
114 for (j=0; j<8 ; j++) {
115 struct pci_dev* temp = pci_find_slot(func->bus,
116 (func->device << 3) | j);
118 pci_remove_bus_device(temp);
122 * Some PCI Express root ports require fixup after hot-plug operation.
125 pci_fixup_device(pci_fixup_final, pbus->self);
133 * Reads configuration for all slots in a PCI bus and saves info.
135 * Note: For non-hot plug busses, the slot # saved is the device #
137 * returns 0 if success
139 int pciehp_save_config(struct controller *ctrl, int busnumber, int num_ctlr_slots, int first_device_num)
146 struct pci_func *new_slot;
155 int is_hot_plug = num_ctlr_slots || first_device_num;
156 struct pci_bus lpci_bus, *pci_bus;
157 int FirstSupported, LastSupported;
159 dbg("%s: Enter\n", __FUNCTION__);
161 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
164 dbg("%s: num_ctlr_slots = %d, first_device_num = %d\n", __FUNCTION__,
165 num_ctlr_slots, first_device_num);
167 /* Decide which slots are supported */
169 /*********************************
170 * is_hot_plug is the slot mask
171 *********************************/
172 FirstSupported = first_device_num;
173 LastSupported = FirstSupported + num_ctlr_slots - 1;
176 LastSupported = 0x1F;
179 dbg("FirstSupported = %d, LastSupported = %d\n", FirstSupported,
182 /* Save PCI configuration space for all devices in supported slots */
183 dbg("%s: pci_bus->number = %x\n", __FUNCTION__, pci_bus->number);
184 pci_bus->number = busnumber;
185 dbg("%s: bus = %x, dev = %x\n", __FUNCTION__, busnumber, device);
186 for (device = FirstSupported; device <= LastSupported; device++) {
188 rc = pci_bus_read_config_dword(pci_bus, PCI_DEVFN(device, 0),
191 if (ID != 0xFFFFFFFF) { /* device in slot */
192 dbg("%s: ID = %x\n", __FUNCTION__, ID);
193 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, 0),
198 rc = pci_bus_read_config_byte(pci_bus, PCI_DEVFN(device, 0),
199 PCI_HEADER_TYPE, &header_type);
203 dbg("class_code = %x, header_type = %x\n", class_code, header_type);
205 /* If multi-function device, set max_functions to 8 */
206 if (header_type & 0x80)
215 dbg("%s: In do loop\n", __FUNCTION__);
217 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* P-P Bridge */
218 /* Recurse the subordinate bus
219 * get the subordinate bus number
221 rc = pci_bus_read_config_byte(pci_bus,
222 PCI_DEVFN(device, function),
223 PCI_SECONDARY_BUS, &secondary_bus);
227 sub_bus = (int) secondary_bus;
229 /* Save secondary bus cfg spc with this recursive call. */
230 rc = pciehp_save_config(ctrl, sub_bus, 0, 0);
237 new_slot = pciehp_slot_find(busnumber, device, index++);
239 dbg("%s: new_slot = %p bus %x dev %x fun %x\n",
240 __FUNCTION__, new_slot, busnumber, device, index-1);
242 while (new_slot && (new_slot->function != (u8) function)) {
243 new_slot = pciehp_slot_find(busnumber, device, index++);
244 dbg("%s: while loop, new_slot = %p bus %x dev %x fun %x\n",
245 __FUNCTION__, new_slot, busnumber, device, index-1);
248 /* Setup slot structure. */
249 new_slot = pciehp_slot_create(busnumber);
250 dbg("%s: if, new_slot = %p bus %x dev %x fun %x\n",
251 __FUNCTION__, new_slot, busnumber, device, function);
253 if (new_slot == NULL)
257 new_slot->bus = (u8) busnumber;
258 new_slot->device = (u8) device;
259 new_slot->function = (u8) function;
260 new_slot->is_a_board = 1;
261 new_slot->switch_save = 0x10;
262 /* In case of unsupported board */
263 new_slot->status = DevError;
264 new_slot->pci_dev = pci_find_slot(new_slot->bus,
265 (new_slot->device << 3) | new_slot->function);
266 dbg("new_slot->pci_dev = %p\n", new_slot->pci_dev);
268 for (cloop = 0; cloop < 0x20; cloop++) {
269 rc = pci_bus_read_config_dword(pci_bus,
270 PCI_DEVFN(device, function),
272 (u32 *) &(new_slot->config_space [cloop]));
273 /* dbg("new_slot->config_space[%x] = %x\n",
274 cloop, new_slot->config_space[cloop]); */
283 /* this loop skips to the next present function
284 * reading in Class Code and Header type.
287 while ((function < max_functions)&&(!stop_it)) {
288 dbg("%s: In while loop \n", __FUNCTION__);
289 rc = pci_bus_read_config_dword(pci_bus,
290 PCI_DEVFN(device, function),
293 if (ID == 0xFFFFFFFF) { /* nothing there. */
295 dbg("Nothing there\n");
296 } else { /* Something there */
297 rc = pci_bus_read_config_byte(pci_bus,
298 PCI_DEVFN(device, function),
303 rc = pci_bus_read_config_byte(pci_bus,
304 PCI_DEVFN(device, function),
305 PCI_HEADER_TYPE, &header_type);
309 dbg("class_code = %x, header_type = %x\n", class_code, header_type);
314 } while (function < max_functions);
315 /* End of IF (device in slot?) */
316 } else if (is_hot_plug) {
317 /* Setup slot structure with entry for empty slot */
318 new_slot = pciehp_slot_create(busnumber);
320 if (new_slot == NULL) {
323 dbg("new_slot = %p, bus = %x, dev = %x, fun = %x\n", new_slot,
324 new_slot->bus, new_slot->device, new_slot->function);
326 new_slot->bus = (u8) busnumber;
327 new_slot->device = (u8) device;
328 new_slot->function = 0;
329 new_slot->is_a_board = 0;
330 new_slot->presence_save = 0;
331 new_slot->switch_save = 0;
333 } /* End of FOR loop */
335 dbg("%s: Exit\n", __FUNCTION__);
341 * pciehp_save_slot_config
343 * Saves configuration info for all PCI devices in a given slot
344 * including subordinate busses.
346 * returns 0 if success
348 int pciehp_save_slot_config(struct controller *ctrl, struct pci_func * new_slot)
360 struct pci_bus lpci_bus, *pci_bus;
361 memcpy(&lpci_bus, ctrl->pci_dev->subordinate, sizeof(lpci_bus));
363 pci_bus->number = new_slot->bus;
367 pci_bus_read_config_dword(pci_bus, PCI_DEVFN(new_slot->device, 0),
370 if (ID != 0xFFFFFFFF) { /* device in slot */
371 pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, 0),
374 pci_bus_read_config_byte(pci_bus, PCI_DEVFN(new_slot->device, 0),
375 PCI_HEADER_TYPE, &header_type);
377 if (header_type & 0x80) /* Multi-function device */
385 if ((header_type & 0x7F) == PCI_HEADER_TYPE_BRIDGE) { /* PCI-PCI Bridge */
386 /* Recurse the subordinate bus */
387 pci_bus_read_config_byte(pci_bus,
388 PCI_DEVFN(new_slot->device, function),
389 PCI_SECONDARY_BUS, &secondary_bus);
391 sub_bus = (int) secondary_bus;
393 /* Save the config headers for the secondary bus. */
394 rc = pciehp_save_config(ctrl, sub_bus, 0, 0);
401 new_slot->status = 0;
403 for (cloop = 0; cloop < 0x20; cloop++) {
404 pci_bus_read_config_dword(pci_bus,
405 PCI_DEVFN(new_slot->device, function),
407 (u32 *) &(new_slot->config_space [cloop]));
414 /* this loop skips to the next present function
415 * reading in the Class Code and the Header type.
418 while ((function < max_functions) && (!stop_it)) {
419 pci_bus_read_config_dword(pci_bus,
420 PCI_DEVFN(new_slot->device, function),
423 if (ID == 0xFFFFFFFF) { /* nothing there. */
425 } else { /* Something there */
426 pci_bus_read_config_byte(pci_bus,
427 PCI_DEVFN(new_slot->device, function),
430 pci_bus_read_config_byte(pci_bus,
431 PCI_DEVFN(new_slot->device, function),
432 PCI_HEADER_TYPE, &header_type);
438 } while (function < max_functions);
439 } /* End of IF (device in slot?) */