blob: 410fe0394a8ea63fb8de8542654d514d2ec36992 [file] [log] [blame]
Prarit Bhargava6f354b02005-07-06 15:29:53 -07001/*
2 * This file is subject to the terms and conditions of the GNU General Public
3 * License. See the file "COPYING" in the main directory of this archive
4 * for more details.
5 *
John Keller8e77af62006-03-08 13:21:34 -06006 * Copyright (C) 2005-2006 Silicon Graphics, Inc. All rights reserved.
Prarit Bhargava6f354b02005-07-06 15:29:53 -07007 *
8 * This work was based on the 2.4/2.6 kernel development by Dick Reigner.
9 * Work to add BIOS PROM support was completed by Mike Habeck.
10 */
11
12#include <linux/init.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070016#include <linux/pci_hotplug.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070017#include <linux/proc_fs.h>
18#include <linux/types.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080019#include <linux/mutex.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070020
21#include <asm/sn/addrs.h>
Prarit Bhargavae55dea52006-04-04 09:26:46 -040022#include <asm/sn/geo.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070023#include <asm/sn/l1.h>
24#include <asm/sn/module.h>
25#include <asm/sn/pcibr_provider.h>
26#include <asm/sn/pcibus_provider_defs.h>
27#include <asm/sn/pcidev.h>
Prarit Bhargavae55dea52006-04-04 09:26:46 -040028#include <asm/sn/sn_feature_sets.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070029#include <asm/sn/sn_sal.h>
30#include <asm/sn/types.h>
John Keller3e643e72007-01-30 01:18:38 -050031#include <linux/acpi.h>
32#include <asm/sn/acpi.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070033
34#include "../pci.h"
Prarit Bhargava6f354b02005-07-06 15:29:53 -070035
36MODULE_LICENSE("GPL");
37MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
38MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
39
John Keller3e643e72007-01-30 01:18:38 -050040
41/* SAL call error codes. Keep in sync with prom header io/include/pcibr.h */
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040042#define PCI_SLOT_ALREADY_UP 2 /* slot already up */
43#define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
44#define PCI_L1_ERR 7 /* L1 console command error */
45#define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
John Keller3e643e72007-01-30 01:18:38 -050046
47
48#define PCIIO_ASIC_TYPE_TIOCA 4
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040049#define PCI_L1_QSIZE 128 /* our L1 message buffer size */
50#define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040051#define SN_SLOT_NAME_SIZE 33 /* size of name string */
Prarit Bhargava6f354b02005-07-06 15:29:53 -070052
53/* internal list head */
54static struct list_head sn_hp_list;
55
56/* hotplug_slot struct's private pointer */
57struct slot {
58 int device_num;
59 struct pci_bus *pci_bus;
60 /* this struct for glue internal only */
61 struct hotplug_slot *hotplug_slot;
62 struct list_head hp_list;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040063 char physical_path[SN_SLOT_NAME_SIZE];
Prarit Bhargava6f354b02005-07-06 15:29:53 -070064};
65
66struct pcibr_slot_enable_resp {
67 int resp_sub_errno;
68 char resp_l1_msg[PCI_L1_QSIZE + 1];
69};
70
71struct pcibr_slot_disable_resp {
72 int resp_sub_errno;
73 char resp_l1_msg[PCI_L1_QSIZE + 1];
74};
75
76enum sn_pci_req_e {
77 PCI_REQ_SLOT_ELIGIBLE,
78 PCI_REQ_SLOT_DISABLE
79};
80
81static int enable_slot(struct hotplug_slot *slot);
82static int disable_slot(struct hotplug_slot *slot);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040083static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
Prarit Bhargava6f354b02005-07-06 15:29:53 -070084
85static struct hotplug_slot_ops sn_hotplug_slot_ops = {
86 .owner = THIS_MODULE,
87 .enable_slot = enable_slot,
88 .disable_slot = disable_slot,
89 .get_power_status = get_power_status,
90};
91
Ingo Molnar14cc3e22006-03-26 01:37:14 -080092static DEFINE_MUTEX(sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -070093
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040094static ssize_t path_show (struct hotplug_slot *bss_hotplug_slot,
95 char *buf)
96{
97 int retval = -ENOENT;
98 struct slot *slot = bss_hotplug_slot->private;
99
100 if (!slot)
101 return retval;
102
103 retval = sprintf (buf, "%s\n", slot->physical_path);
104 return retval;
105}
106
107static struct hotplug_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
108
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700109static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
110{
111 struct pcibus_info *pcibus_info;
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400112 u16 busnum, segment, ioboard_type;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700113
114 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
115
116 /* Check to see if this is a valid slot on 'pci_bus' */
117 if (!(pcibus_info->pbi_valid_devices & (1 << device)))
118 return -EPERM;
119
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400120 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
121 busnum = pcibus_info->pbi_buscommon.bs_persist_busnum;
122 segment = pci_domain_nr(pci_bus) & 0xf;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700123
124 /* Do not allow hotplug operations on base I/O cards */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400125 if ((ioboard_type == L1_BRICKTYPE_IX ||
126 ioboard_type == L1_BRICKTYPE_IA) &&
127 (segment == 1 && busnum == 0 && device != 1))
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700128 return -EPERM;
129
130 return 1;
131}
132
133static int sn_pci_bus_valid(struct pci_bus *pci_bus)
134{
135 struct pcibus_info *pcibus_info;
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400136 u32 asic_type;
137 u16 ioboard_type;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700138
139 /* Don't register slots hanging off the TIOCA bus */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400140 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700141 asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
142 if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
143 return -EPERM;
144
145 /* Only register slots in I/O Bricks that support hotplug */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400146 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
147 switch (ioboard_type) {
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400148 case L1_BRICKTYPE_IX:
149 case L1_BRICKTYPE_PX:
150 case L1_BRICKTYPE_IA:
151 case L1_BRICKTYPE_PA:
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400152 case L1_BOARDTYPE_PCIX3SLOT:
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400153 return 1;
154 break;
155 default:
156 return -EPERM;
157 break;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700158 }
159
160 return -EIO;
161}
162
163static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
164 struct pci_bus *pci_bus, int device)
165{
166 struct pcibus_info *pcibus_info;
167 struct slot *slot;
168
169 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
170
Pekka Enberg656da9d2005-09-22 00:48:11 -0700171 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400172 if (!slot)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700173 return -ENOMEM;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400174 bss_hotplug_slot->private = slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700175
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400176 bss_hotplug_slot->name = kmalloc(SN_SLOT_NAME_SIZE, GFP_KERNEL);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700177 if (!bss_hotplug_slot->name) {
178 kfree(bss_hotplug_slot->private);
179 return -ENOMEM;
180 }
181
182 slot->device_num = device;
183 slot->pci_bus = pci_bus;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400184 sprintf(bss_hotplug_slot->name, "%04x:%02x:%02x",
185 pci_domain_nr(pci_bus),
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400186 ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400187 device + 1);
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400188
189 sn_generate_path(pci_bus, slot->physical_path);
190
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700191 slot->hotplug_slot = bss_hotplug_slot;
192 list_add(&slot->hp_list, &sn_hp_list);
193
194 return 0;
195}
196
197static struct hotplug_slot * sn_hp_destroy(void)
198{
199 struct slot *slot;
Alex Chiangf46753c2008-06-10 15:28:50 -0600200 struct pci_slot *pci_slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700201 struct hotplug_slot *bss_hotplug_slot = NULL;
202
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400203 list_for_each_entry(slot, &sn_hp_list, hp_list) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700204 bss_hotplug_slot = slot->hotplug_slot;
Alex Chiangf46753c2008-06-10 15:28:50 -0600205 pci_slot = bss_hotplug_slot->pci_slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700206 list_del(&((struct slot *)bss_hotplug_slot->private)->
207 hp_list);
Alex Chiangf46753c2008-06-10 15:28:50 -0600208 sysfs_remove_file(&pci_slot->kobj,
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400209 &sn_slot_path_attr.attr);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700210 break;
211 }
212 return bss_hotplug_slot;
213}
214
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700215static void sn_bus_free_data(struct pci_dev *dev)
216{
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700217 struct pci_bus *subordinate_bus;
218 struct pci_dev *child;
219
220 /* Recursively clean up sn_irq_info structs */
221 if (dev->subordinate) {
222 subordinate_bus = dev->subordinate;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400223 list_for_each_entry(child, &subordinate_bus->devices, bus_list)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700224 sn_bus_free_data(child);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700225 }
John Keller8e77af62006-03-08 13:21:34 -0600226 /*
227 * Some drivers may use dma accesses during the
228 * driver remove function. We release the sysdata
229 * areas after the driver remove functions have
230 * been called.
231 */
232 sn_bus_store_sysdata(dev);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700233 sn_pci_unfixup_slot(dev);
234}
235
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700236static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
John Keller3e643e72007-01-30 01:18:38 -0500237 int device_num, char **ssdt)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700238{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400239 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700240 struct pcibus_info *pcibus_info;
241 struct pcibr_slot_enable_resp resp;
242 int rc;
243
244 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
245
246 /*
247 * Power-on and initialize the slot in the SN
248 * PCI infrastructure.
249 */
John Keller3e643e72007-01-30 01:18:38 -0500250 rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp, ssdt);
251
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700252
253 if (rc == PCI_SLOT_ALREADY_UP) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700254 dev_dbg(&slot->pci_bus->self->dev, "is already active\n");
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400255 return 1; /* return 1 to user */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700256 }
257
258 if (rc == PCI_L1_ERR) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700259 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700260 "L1 failure %d with message: %s",
261 resp.resp_sub_errno, resp.resp_l1_msg);
262 return -EPERM;
263 }
264
265 if (rc) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700266 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700267 "insert failed with error %d sub-error %d\n",
268 rc, resp.resp_sub_errno);
269 return -EIO;
270 }
271
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400272 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
273 pcibus_info->pbi_enabled_devices |= (1 << device_num);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700274
275 return 0;
276}
277
278static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
279 int device_num, int action)
280{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400281 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700282 struct pcibus_info *pcibus_info;
283 struct pcibr_slot_disable_resp resp;
284 int rc;
285
286 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
287
288 rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
289
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400290 if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
291 (rc == PCI_SLOT_ALREADY_DOWN)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700292 dev_dbg(&slot->pci_bus->self->dev, "Slot %s already inactive\n", slot->physical_path);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400293 return 1; /* return 1 to user */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700294 }
295
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400296 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700297 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700298 "Cannot remove last 33MHz card\n");
299 return -EPERM;
300 }
301
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400302 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700303 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700304 "L1 failure %d with message \n%s\n",
305 resp.resp_sub_errno, resp.resp_l1_msg);
306 return -EPERM;
307 }
308
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400309 if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700310 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700311 "remove failed with error %d sub-error %d\n",
312 rc, resp.resp_sub_errno);
313 return -EIO;
314 }
315
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400316 if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700317 return 0;
318
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400319 if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
320 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
321 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
Tony Luck34ef30c2007-05-10 09:39:41 -0700322 dev_dbg(&slot->pci_bus->self->dev, "remove successful\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700323 return 0;
324 }
325
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400326 if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700327 dev_dbg(&slot->pci_bus->self->dev,"remove failed rc = %d\n", rc);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700328 }
329
330 return rc;
331}
332
John Keller9f581f12006-10-04 16:49:35 -0500333/*
334 * Power up and configure the slot via a SAL call to PROM.
335 * Scan slot (and any children), do any platform specific fixup,
336 * and find device driver.
337 */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700338static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
339{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400340 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700341 struct pci_bus *new_bus = NULL;
342 struct pci_dev *dev;
343 int func, num_funcs;
344 int new_ppb = 0;
345 int rc;
John Keller3e643e72007-01-30 01:18:38 -0500346 char *ssdt = NULL;
John Keller9f581f12006-10-04 16:49:35 -0500347 void pcibios_fixup_device_resources(struct pci_dev *);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700348
349 /* Serialize the Linux PCI infrastructure */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800350 mutex_lock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700351
352 /*
353 * Power-on and initialize the slot in the SN
John Keller3e643e72007-01-30 01:18:38 -0500354 * PCI infrastructure. Also, retrieve the ACPI SSDT
355 * table for the slot (if ACPI capable PROM).
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700356 */
John Keller3e643e72007-01-30 01:18:38 -0500357 rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700358 if (rc) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800359 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700360 return rc;
361 }
362
John Keller3e643e72007-01-30 01:18:38 -0500363 if (ssdt)
364 ssdt = __va(ssdt);
365 /* Add the new SSDT for the slot to the ACPI namespace */
366 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
367 acpi_status ret;
368
369 ret = acpi_load_table((struct acpi_table_header *)ssdt);
370 if (ACPI_FAILURE(ret)) {
371 printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800372 __func__, ret);
John Keller3e643e72007-01-30 01:18:38 -0500373 /* try to continue on */
374 }
375 }
376
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400377 num_funcs = pci_scan_slot(slot->pci_bus,
378 PCI_DEVFN(slot->device_num + 1, 0));
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700379 if (!num_funcs) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700380 dev_dbg(&slot->pci_bus->self->dev, "no device in slot\n");
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800381 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700382 return -ENODEV;
383 }
384
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700385 /*
386 * Map SN resources for all functions on the card
387 * to the Linux PCI interface and tell the drivers
388 * about them.
389 */
390 for (func = 0; func < num_funcs; func++) {
391 dev = pci_get_slot(slot->pci_bus,
392 PCI_DEVFN(slot->device_num + 1,
393 PCI_FUNC(func)));
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700394 if (dev) {
John Keller9f581f12006-10-04 16:49:35 -0500395 /* Need to do slot fixup on PPB before fixup of children
396 * (PPB's pcidev_info needs to be in pcidev_info list
397 * before child's SN_PCIDEV_INFO() call to setup
398 * pdi_host_pcidev_info).
399 */
400 pcibios_fixup_device_resources(dev);
John Keller3e643e72007-01-30 01:18:38 -0500401 if (SN_ACPI_BASE_SUPPORT())
402 sn_acpi_slot_fixup(dev);
403 else
404 sn_io_slot_fixup(dev);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700405 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
406 unsigned char sec_bus;
407 pci_read_config_byte(dev, PCI_SECONDARY_BUS,
408 &sec_bus);
409 new_bus = pci_add_new_bus(dev->bus, dev,
410 sec_bus);
411 pci_scan_child_bus(new_bus);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700412 new_ppb = 1;
413 }
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700414 pci_dev_put(dev);
415 }
416 }
417
John Keller3e643e72007-01-30 01:18:38 -0500418 /*
419 * Add the slot's devices to the ACPI infrastructure */
420 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
421 unsigned long adr;
422 struct acpi_device *pdevice;
423 struct acpi_device *device;
424 acpi_handle phandle;
425 acpi_handle chandle = NULL;
426 acpi_handle rethandle;
427 acpi_status ret;
428
429 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
430
431 if (acpi_bus_get_device(phandle, &pdevice)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700432 dev_dbg(&slot->pci_bus->self->dev,
John Keller3e643e72007-01-30 01:18:38 -0500433 "no parent device, assuming NULL\n");
434 pdevice = NULL;
435 }
436
437 /*
438 * Walk the rootbus node's immediate children looking for
439 * the slot's device node(s). There can be more than
440 * one for multifunction devices.
441 */
442 for (;;) {
443 rethandle = NULL;
444 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
445 phandle, chandle,
446 &rethandle);
447
448 if (ret == AE_NOT_FOUND || rethandle == NULL)
449 break;
450
451 chandle = rethandle;
452
453 ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
454 NULL, &adr);
455
456 if (ACPI_SUCCESS(ret) &&
457 (adr>>16) == (slot->device_num + 1)) {
458
459 ret = acpi_bus_add(&device, pdevice, chandle,
460 ACPI_BUS_TYPE_DEVICE);
461 if (ACPI_FAILURE(ret)) {
462 printk(KERN_ERR "%s: acpi_bus_add "
463 "failed (0x%x) for slot %d "
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800464 "func %d\n", __func__,
John Keller3e643e72007-01-30 01:18:38 -0500465 ret, (int)(adr>>16),
466 (int)(adr&0xffff));
467 /* try to continue on */
468 } else {
469 acpi_bus_start(device);
470 }
471 }
472 }
473 }
474
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700475 /* Call the driver for the new device */
476 pci_bus_add_devices(slot->pci_bus);
477 /* Call the drivers for the new devices subordinate to PPB */
478 if (new_ppb)
479 pci_bus_add_devices(new_bus);
480
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800481 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700482
483 if (rc == 0)
Tony Luck34ef30c2007-05-10 09:39:41 -0700484 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700485 "insert operation successful\n");
486 else
Tony Luck34ef30c2007-05-10 09:39:41 -0700487 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700488 "insert operation failed rc = %d\n", rc);
489
490 return rc;
491}
492
493static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
494{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400495 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700496 struct pci_dev *dev;
497 int func;
498 int rc;
John Keller3e643e72007-01-30 01:18:38 -0500499 acpi_owner_id ssdt_id = 0;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700500
501 /* Acquire update access to the bus */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800502 mutex_lock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700503
504 /* is it okay to bring this slot down? */
505 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
506 PCI_REQ_SLOT_ELIGIBLE);
507 if (rc)
508 goto leaving;
509
John Keller3e643e72007-01-30 01:18:38 -0500510 /* free the ACPI resources for the slot */
511 if (SN_ACPI_BASE_SUPPORT() &&
512 PCI_CONTROLLER(slot->pci_bus)->acpi_handle) {
513 unsigned long adr;
514 struct acpi_device *device;
515 acpi_handle phandle;
516 acpi_handle chandle = NULL;
517 acpi_handle rethandle;
518 acpi_status ret;
519
520 /* Get the rootbus node pointer */
521 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
522
523 /*
524 * Walk the rootbus node's immediate children looking for
525 * the slot's device node(s). There can be more than
526 * one for multifunction devices.
527 */
528 for (;;) {
529 rethandle = NULL;
530 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
531 phandle, chandle,
532 &rethandle);
533
534 if (ret == AE_NOT_FOUND || rethandle == NULL)
535 break;
536
537 chandle = rethandle;
538
539 ret = acpi_evaluate_integer(chandle,
540 METHOD_NAME__ADR,
541 NULL, &adr);
542 if (ACPI_SUCCESS(ret) &&
543 (adr>>16) == (slot->device_num + 1)) {
544 /* retain the owner id */
545 acpi_get_id(chandle, &ssdt_id);
546
547 ret = acpi_bus_get_device(chandle,
548 &device);
549 if (ACPI_SUCCESS(ret))
550 acpi_bus_trim(device, 1);
551 }
552 }
553
554 }
555
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700556 /* Free the SN resources assigned to the Linux device.*/
557 for (func = 0; func < 8; func++) {
558 dev = pci_get_slot(slot->pci_bus,
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400559 PCI_DEVFN(slot->device_num + 1,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700560 PCI_FUNC(func)));
561 if (dev) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700562 sn_bus_free_data(dev);
563 pci_remove_bus_device(dev);
564 pci_dev_put(dev);
565 }
566 }
567
John Keller3e643e72007-01-30 01:18:38 -0500568 /* Remove the SSDT for the slot from the ACPI namespace */
569 if (SN_ACPI_BASE_SUPPORT() && ssdt_id) {
570 acpi_status ret;
571 ret = acpi_unload_table_id(ssdt_id);
572 if (ACPI_FAILURE(ret)) {
573 printk(KERN_ERR "%s: acpi_unload_table_id "
574 "failed (0x%x) for id %d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800575 __func__, ret, ssdt_id);
John Keller3e643e72007-01-30 01:18:38 -0500576 /* try to continue on */
577 }
578 }
579
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700580 /* free the collected sysdata pointers */
581 sn_bus_free_sysdata();
582
583 /* Deactivate slot */
584 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
585 PCI_REQ_SLOT_DISABLE);
586 leaving:
587 /* Release the bus lock */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800588 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700589
590 return rc;
591}
592
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400593static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
594 u8 *value)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700595{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400596 struct slot *slot = bss_hotplug_slot->private;
597 struct pcibus_info *pcibus_info;
Mike Habeck466ee362006-05-06 09:01:59 -0500598 u32 power;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400599
600 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800601 mutex_lock(&sn_hotplug_mutex);
Mike Habeck466ee362006-05-06 09:01:59 -0500602 power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
603 *value = power ? 1 : 0;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800604 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700605 return 0;
606}
607
608static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
609{
610 kfree(bss_hotplug_slot->info);
611 kfree(bss_hotplug_slot->name);
612 kfree(bss_hotplug_slot->private);
613 kfree(bss_hotplug_slot);
614}
615
616static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
617{
618 int device;
Alex Chiangf46753c2008-06-10 15:28:50 -0600619 struct pci_slot *pci_slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700620 struct hotplug_slot *bss_hotplug_slot;
621 int rc = 0;
622
623 /*
624 * Currently only four devices are supported,
625 * in the future there maybe more -- up to 32.
626 */
627
628 for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
629 if (sn_pci_slot_valid(pci_bus, device) != 1)
630 continue;
631
Pekka Enberg656da9d2005-09-22 00:48:11 -0700632 bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700633 GFP_KERNEL);
634 if (!bss_hotplug_slot) {
635 rc = -ENOMEM;
636 goto alloc_err;
637 }
638
639 bss_hotplug_slot->info =
Pekka Enberg656da9d2005-09-22 00:48:11 -0700640 kzalloc(sizeof(struct hotplug_slot_info),
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700641 GFP_KERNEL);
642 if (!bss_hotplug_slot->info) {
643 rc = -ENOMEM;
644 goto alloc_err;
645 }
646
647 if (sn_hp_slot_private_alloc(bss_hotplug_slot,
648 pci_bus, device)) {
649 rc = -ENOMEM;
650 goto alloc_err;
651 }
652
653 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
654 bss_hotplug_slot->release = &sn_release_slot;
655
Alex Chiangf46753c2008-06-10 15:28:50 -0600656 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700657 if (rc)
658 goto register_err;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400659
Alex Chiangf46753c2008-06-10 15:28:50 -0600660 pci_slot = bss_hotplug_slot->pci_slot;
661 rc = sysfs_create_file(&pci_slot->kobj,
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400662 &sn_slot_path_attr.attr);
663 if (rc)
664 goto register_err;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700665 }
Tony Luck34ef30c2007-05-10 09:39:41 -0700666 dev_dbg(&pci_bus->self->dev, "Registered bus with hotplug\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700667 return rc;
668
669register_err:
Tony Luck34ef30c2007-05-10 09:39:41 -0700670 dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n",
Alex Chiang8344b562008-06-10 15:30:42 -0600671 rc);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700672
673alloc_err:
674 if (rc == -ENOMEM)
Tony Luck34ef30c2007-05-10 09:39:41 -0700675 dev_dbg(&pci_bus->self->dev, "Memory allocation error\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700676
677 /* destroy THIS element */
678 if (bss_hotplug_slot)
679 sn_release_slot(bss_hotplug_slot);
680
681 /* destroy anything else on the list */
682 while ((bss_hotplug_slot = sn_hp_destroy()))
683 pci_hp_deregister(bss_hotplug_slot);
684
685 return rc;
686}
687
688static int sn_pci_hotplug_init(void)
689{
690 struct pci_bus *pci_bus = NULL;
691 int rc;
692 int registered = 0;
693
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400694 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
695 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800696 __func__);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700697 return -EPERM;
698 }
699
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400700 INIT_LIST_HEAD(&sn_hp_list);
701
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700702 while ((pci_bus = pci_find_next_bus(pci_bus))) {
703 if (!pci_bus->sysdata)
704 continue;
705
706 rc = sn_pci_bus_valid(pci_bus);
707 if (rc != 1) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700708 dev_dbg(&pci_bus->self->dev, "not a valid hotplug bus\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700709 continue;
710 }
Tony Luck34ef30c2007-05-10 09:39:41 -0700711 dev_dbg(&pci_bus->self->dev, "valid hotplug bus\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700712
713 rc = sn_hotplug_slot_register(pci_bus);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400714 if (!rc) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700715 registered = 1;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400716 } else {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700717 registered = 0;
718 break;
719 }
720 }
721
722 return registered == 1 ? 0 : -ENODEV;
723}
724
725static void sn_pci_hotplug_exit(void)
726{
727 struct hotplug_slot *bss_hotplug_slot;
728
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400729 while ((bss_hotplug_slot = sn_hp_destroy()))
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700730 pci_hp_deregister(bss_hotplug_slot);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700731
732 if (!list_empty(&sn_hp_list))
733 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
734}
735
736module_init(sn_pci_hotplug_init);
737module_exit(sn_pci_hotplug_exit);