blob: c32fb786d48e3c76f37b25e4b422e5796e884acf [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
Rafael J. Wysocki7b199812013-11-11 22:41:56 +010012#include <linux/acpi.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070013#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/pci.h>
Greg Kroah-Hartman7a54f252006-10-13 20:05:19 -070017#include <linux/pci_hotplug.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070018#include <linux/proc_fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070020#include <linux/types.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080021#include <linux/mutex.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070022
23#include <asm/sn/addrs.h>
Prarit Bhargavae55dea52006-04-04 09:26:46 -040024#include <asm/sn/geo.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070025#include <asm/sn/l1.h>
26#include <asm/sn/module.h>
27#include <asm/sn/pcibr_provider.h>
28#include <asm/sn/pcibus_provider_defs.h>
29#include <asm/sn/pcidev.h>
Prarit Bhargavae55dea52006-04-04 09:26:46 -040030#include <asm/sn/sn_feature_sets.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070031#include <asm/sn/sn_sal.h>
32#include <asm/sn/types.h>
John Keller3e643e72007-01-30 01:18:38 -050033#include <asm/sn/acpi.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070034
35#include "../pci.h"
Prarit Bhargava6f354b02005-07-06 15:29:53 -070036
37MODULE_LICENSE("GPL");
38MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
39MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
40
John Keller3e643e72007-01-30 01:18:38 -050041
42/* SAL call error codes. Keep in sync with prom header io/include/pcibr.h */
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040043#define PCI_SLOT_ALREADY_UP 2 /* slot already up */
44#define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
45#define PCI_L1_ERR 7 /* L1 console command error */
46#define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
John Keller3e643e72007-01-30 01:18:38 -050047
48
49#define PCIIO_ASIC_TYPE_TIOCA 4
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040050#define PCI_L1_QSIZE 128 /* our L1 message buffer size */
51#define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040052#define SN_SLOT_NAME_SIZE 33 /* size of name string */
Prarit Bhargava6f354b02005-07-06 15:29:53 -070053
54/* internal list head */
55static struct list_head sn_hp_list;
56
57/* hotplug_slot struct's private pointer */
58struct slot {
59 int device_num;
60 struct pci_bus *pci_bus;
61 /* this struct for glue internal only */
62 struct hotplug_slot *hotplug_slot;
63 struct list_head hp_list;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040064 char physical_path[SN_SLOT_NAME_SIZE];
Prarit Bhargava6f354b02005-07-06 15:29:53 -070065};
66
67struct pcibr_slot_enable_resp {
68 int resp_sub_errno;
69 char resp_l1_msg[PCI_L1_QSIZE + 1];
70};
71
72struct pcibr_slot_disable_resp {
73 int resp_sub_errno;
74 char resp_l1_msg[PCI_L1_QSIZE + 1];
75};
76
77enum sn_pci_req_e {
78 PCI_REQ_SLOT_ELIGIBLE,
79 PCI_REQ_SLOT_DISABLE
80};
81
82static int enable_slot(struct hotplug_slot *slot);
83static int disable_slot(struct hotplug_slot *slot);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040084static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
Prarit Bhargava6f354b02005-07-06 15:29:53 -070085
86static struct hotplug_slot_ops sn_hotplug_slot_ops = {
Prarit Bhargava6f354b02005-07-06 15:29:53 -070087 .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
Kenji Kaneshige94f81a42009-07-27 12:06:46 +090094static ssize_t path_show(struct pci_slot *pci_slot, char *buf)
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040095{
96 int retval = -ENOENT;
Kenji Kaneshige94f81a42009-07-27 12:06:46 +090097 struct slot *slot = pci_slot->hotplug->private;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040098
99 if (!slot)
100 return retval;
101
102 retval = sprintf (buf, "%s\n", slot->physical_path);
103 return retval;
104}
105
Kenji Kaneshige94f81a42009-07-27 12:06:46 +0900106static struct pci_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400107
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700108static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
109{
110 struct pcibus_info *pcibus_info;
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400111 u16 busnum, segment, ioboard_type;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700112
113 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
114
115 /* Check to see if this is a valid slot on 'pci_bus' */
116 if (!(pcibus_info->pbi_valid_devices & (1 << device)))
117 return -EPERM;
118
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400119 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
120 busnum = pcibus_info->pbi_buscommon.bs_persist_busnum;
121 segment = pci_domain_nr(pci_bus) & 0xf;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700122
123 /* Do not allow hotplug operations on base I/O cards */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400124 if ((ioboard_type == L1_BRICKTYPE_IX ||
125 ioboard_type == L1_BRICKTYPE_IA) &&
126 (segment == 1 && busnum == 0 && device != 1))
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700127 return -EPERM;
128
129 return 1;
130}
131
132static int sn_pci_bus_valid(struct pci_bus *pci_bus)
133{
134 struct pcibus_info *pcibus_info;
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400135 u32 asic_type;
136 u16 ioboard_type;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700137
138 /* Don't register slots hanging off the TIOCA bus */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400139 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700140 asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
141 if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
142 return -EPERM;
143
144 /* Only register slots in I/O Bricks that support hotplug */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400145 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
146 switch (ioboard_type) {
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400147 case L1_BRICKTYPE_IX:
148 case L1_BRICKTYPE_PX:
149 case L1_BRICKTYPE_IA:
150 case L1_BRICKTYPE_PA:
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400151 case L1_BOARDTYPE_PCIX3SLOT:
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400152 return 1;
153 break;
154 default:
155 return -EPERM;
156 break;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700157 }
158
159 return -EIO;
160}
161
162static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
Alex Chiang85234ce2008-10-20 17:41:48 -0600163 struct pci_bus *pci_bus, int device,
164 char *name)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700165{
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 Bhargava6f354b02005-07-06 15:29:53 -0700176 slot->device_num = device;
177 slot->pci_bus = pci_bus;
Alex Chiang85234ce2008-10-20 17:41:48 -0600178 sprintf(name, "%04x:%02x:%02x",
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400179 pci_domain_nr(pci_bus),
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400180 ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400181 device + 1);
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400182
183 sn_generate_path(pci_bus, slot->physical_path);
184
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700185 slot->hotplug_slot = bss_hotplug_slot;
186 list_add(&slot->hp_list, &sn_hp_list);
187
188 return 0;
189}
190
Ryan Desfosses3c78bc62014-04-18 20:13:49 -0400191static struct hotplug_slot *sn_hp_destroy(void)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700192{
193 struct slot *slot;
Alex Chiangf46753c2008-06-10 15:28:50 -0600194 struct pci_slot *pci_slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700195 struct hotplug_slot *bss_hotplug_slot = NULL;
196
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400197 list_for_each_entry(slot, &sn_hp_list, hp_list) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700198 bss_hotplug_slot = slot->hotplug_slot;
Alex Chiangf46753c2008-06-10 15:28:50 -0600199 pci_slot = bss_hotplug_slot->pci_slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700200 list_del(&((struct slot *)bss_hotplug_slot->private)->
201 hp_list);
Alex Chiangf46753c2008-06-10 15:28:50 -0600202 sysfs_remove_file(&pci_slot->kobj,
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400203 &sn_slot_path_attr.attr);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700204 break;
205 }
206 return bss_hotplug_slot;
207}
208
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700209static void sn_bus_free_data(struct pci_dev *dev)
210{
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700211 struct pci_bus *subordinate_bus;
212 struct pci_dev *child;
213
214 /* Recursively clean up sn_irq_info structs */
215 if (dev->subordinate) {
216 subordinate_bus = dev->subordinate;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400217 list_for_each_entry(child, &subordinate_bus->devices, bus_list)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700218 sn_bus_free_data(child);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700219 }
John Keller8e77af62006-03-08 13:21:34 -0600220 /*
221 * Some drivers may use dma accesses during the
222 * driver remove function. We release the sysdata
223 * areas after the driver remove functions have
224 * been called.
225 */
226 sn_bus_store_sysdata(dev);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700227 sn_pci_unfixup_slot(dev);
228}
229
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700230static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
John Keller3e643e72007-01-30 01:18:38 -0500231 int device_num, char **ssdt)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700232{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400233 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700234 struct pcibus_info *pcibus_info;
235 struct pcibr_slot_enable_resp resp;
236 int rc;
237
238 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
239
240 /*
241 * Power-on and initialize the slot in the SN
242 * PCI infrastructure.
243 */
John Keller3e643e72007-01-30 01:18:38 -0500244 rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp, ssdt);
245
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700246
247 if (rc == PCI_SLOT_ALREADY_UP) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700248 dev_dbg(&slot->pci_bus->self->dev, "is already active\n");
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400249 return 1; /* return 1 to user */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700250 }
251
252 if (rc == PCI_L1_ERR) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400253 dev_dbg(&slot->pci_bus->self->dev, "L1 failure %d with message: %s",
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700254 resp.resp_sub_errno, resp.resp_l1_msg);
255 return -EPERM;
256 }
257
258 if (rc) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400259 dev_dbg(&slot->pci_bus->self->dev, "insert failed with error %d sub-error %d\n",
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700260 rc, resp.resp_sub_errno);
261 return -EIO;
262 }
263
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400264 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
265 pcibus_info->pbi_enabled_devices |= (1 << device_num);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700266
267 return 0;
268}
269
270static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
271 int device_num, int action)
272{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400273 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700274 struct pcibus_info *pcibus_info;
275 struct pcibr_slot_disable_resp resp;
276 int rc;
277
278 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
279
280 rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
281
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400282 if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
283 (rc == PCI_SLOT_ALREADY_DOWN)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700284 dev_dbg(&slot->pci_bus->self->dev, "Slot %s already inactive\n", slot->physical_path);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400285 return 1; /* return 1 to user */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700286 }
287
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400288 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400289 dev_dbg(&slot->pci_bus->self->dev, "Cannot remove last 33MHz card\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700290 return -EPERM;
291 }
292
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400293 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400294 dev_dbg(&slot->pci_bus->self->dev, "L1 failure %d with message \n%s\n",
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700295 resp.resp_sub_errno, resp.resp_l1_msg);
296 return -EPERM;
297 }
298
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400299 if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400300 dev_dbg(&slot->pci_bus->self->dev, "remove failed with error %d sub-error %d\n",
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700301 rc, resp.resp_sub_errno);
302 return -EIO;
303 }
304
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400305 if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700306 return 0;
307
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400308 if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
309 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
310 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
Tony Luck34ef30c2007-05-10 09:39:41 -0700311 dev_dbg(&slot->pci_bus->self->dev, "remove successful\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700312 return 0;
313 }
314
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400315 if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700316 dev_dbg(&slot->pci_bus->self->dev,"remove failed rc = %d\n", rc);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700317 }
318
319 return rc;
320}
321
John Keller9f581f12006-10-04 16:49:35 -0500322/*
323 * Power up and configure the slot via a SAL call to PROM.
324 * Scan slot (and any children), do any platform specific fixup,
325 * and find device driver.
326 */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700327static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
328{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400329 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700330 struct pci_bus *new_bus = NULL;
331 struct pci_dev *dev;
Yijing Wangab6380e2013-01-15 11:12:21 +0800332 int num_funcs;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700333 int new_ppb = 0;
334 int rc;
John Keller3e643e72007-01-30 01:18:38 -0500335 char *ssdt = NULL;
John Keller9f581f12006-10-04 16:49:35 -0500336 void pcibios_fixup_device_resources(struct pci_dev *);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700337
338 /* Serialize the Linux PCI infrastructure */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800339 mutex_lock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700340
341 /*
342 * Power-on and initialize the slot in the SN
John Keller3e643e72007-01-30 01:18:38 -0500343 * PCI infrastructure. Also, retrieve the ACPI SSDT
344 * table for the slot (if ACPI capable PROM).
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700345 */
John Keller3e643e72007-01-30 01:18:38 -0500346 rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700347 if (rc) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800348 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700349 return rc;
350 }
351
John Keller3e643e72007-01-30 01:18:38 -0500352 if (ssdt)
353 ssdt = __va(ssdt);
354 /* Add the new SSDT for the slot to the ACPI namespace */
355 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
356 acpi_status ret;
357
358 ret = acpi_load_table((struct acpi_table_header *)ssdt);
359 if (ACPI_FAILURE(ret)) {
360 printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800361 __func__, ret);
John Keller3e643e72007-01-30 01:18:38 -0500362 /* try to continue on */
363 }
364 }
365
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400366 num_funcs = pci_scan_slot(slot->pci_bus,
367 PCI_DEVFN(slot->device_num + 1, 0));
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700368 if (!num_funcs) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700369 dev_dbg(&slot->pci_bus->self->dev, "no device in slot\n");
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800370 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700371 return -ENODEV;
372 }
373
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700374 /*
375 * Map SN resources for all functions on the card
376 * to the Linux PCI interface and tell the drivers
377 * about them.
378 */
Yijing Wangab6380e2013-01-15 11:12:21 +0800379 list_for_each_entry(dev, &slot->pci_bus->devices, bus_list) {
380 if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
381 continue;
382
383 /* Need to do slot fixup on PPB before fixup of children
384 * (PPB's pcidev_info needs to be in pcidev_info list
385 * before child's SN_PCIDEV_INFO() call to setup
386 * pdi_host_pcidev_info).
387 */
388 pcibios_fixup_device_resources(dev);
389 if (SN_ACPI_BASE_SUPPORT())
390 sn_acpi_slot_fixup(dev);
391 else
392 sn_io_slot_fixup(dev);
393 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
394 pci_hp_add_bridge(dev);
395 if (dev->subordinate) {
396 new_bus = dev->subordinate;
397 new_ppb = 1;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700398 }
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700399 }
400 }
401
John Keller3e643e72007-01-30 01:18:38 -0500402 /*
403 * Add the slot's devices to the ACPI infrastructure */
404 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400405 unsigned long long adr;
John Keller3e643e72007-01-30 01:18:38 -0500406 struct acpi_device *pdevice;
John Keller3e643e72007-01-30 01:18:38 -0500407 acpi_handle phandle;
408 acpi_handle chandle = NULL;
409 acpi_handle rethandle;
410 acpi_status ret;
411
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100412 phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
John Keller3e643e72007-01-30 01:18:38 -0500413
414 if (acpi_bus_get_device(phandle, &pdevice)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400415 dev_dbg(&slot->pci_bus->self->dev, "no parent device, assuming NULL\n");
John Keller3e643e72007-01-30 01:18:38 -0500416 pdevice = NULL;
417 }
418
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100419 acpi_scan_lock_acquire();
John Keller3e643e72007-01-30 01:18:38 -0500420 /*
421 * Walk the rootbus node's immediate children looking for
422 * the slot's device node(s). There can be more than
423 * one for multifunction devices.
424 */
425 for (;;) {
426 rethandle = NULL;
427 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
428 phandle, chandle,
429 &rethandle);
430
431 if (ret == AE_NOT_FOUND || rethandle == NULL)
432 break;
433
434 chandle = rethandle;
435
436 ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
437 NULL, &adr);
438
439 if (ACPI_SUCCESS(ret) &&
440 (adr>>16) == (slot->device_num + 1)) {
441
Rafael J. Wysockib8bd7592013-01-19 01:27:35 +0100442 ret = acpi_bus_scan(chandle);
John Keller3e643e72007-01-30 01:18:38 -0500443 if (ACPI_FAILURE(ret)) {
Ryan Desfosses227f0642014-04-18 20:13:50 -0400444 printk(KERN_ERR "%s: acpi_bus_scan failed (0x%x) for slot %d func %d\n",
445 __func__, ret, (int)(adr>>16),
John Keller3e643e72007-01-30 01:18:38 -0500446 (int)(adr&0xffff));
447 /* try to continue on */
John Keller3e643e72007-01-30 01:18:38 -0500448 }
449 }
450 }
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100451 acpi_scan_lock_release();
John Keller3e643e72007-01-30 01:18:38 -0500452 }
453
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -0700454 pci_lock_rescan_remove();
455
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700456 /* Call the driver for the new device */
457 pci_bus_add_devices(slot->pci_bus);
458 /* Call the drivers for the new devices subordinate to PPB */
459 if (new_ppb)
460 pci_bus_add_devices(new_bus);
461
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -0700462 pci_unlock_rescan_remove();
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800463 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700464
465 if (rc == 0)
Ryan Desfosses227f0642014-04-18 20:13:50 -0400466 dev_dbg(&slot->pci_bus->self->dev, "insert operation successful\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700467 else
Ryan Desfosses227f0642014-04-18 20:13:50 -0400468 dev_dbg(&slot->pci_bus->self->dev, "insert operation failed rc = %d\n", rc);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700469
470 return rc;
471}
472
473static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
474{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400475 struct slot *slot = bss_hotplug_slot->private;
Yijing Wangab6380e2013-01-15 11:12:21 +0800476 struct pci_dev *dev, *temp;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700477 int rc;
Lv Zhenge044d8f2015-01-26 16:58:48 +0800478 acpi_handle ssdt_hdl = NULL;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700479
480 /* Acquire update access to the bus */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800481 mutex_lock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700482
483 /* is it okay to bring this slot down? */
484 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
485 PCI_REQ_SLOT_ELIGIBLE);
486 if (rc)
487 goto leaving;
488
John Keller3e643e72007-01-30 01:18:38 -0500489 /* free the ACPI resources for the slot */
490 if (SN_ACPI_BASE_SUPPORT() &&
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100491 PCI_CONTROLLER(slot->pci_bus)->companion) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400492 unsigned long long adr;
John Keller3e643e72007-01-30 01:18:38 -0500493 struct acpi_device *device;
494 acpi_handle phandle;
495 acpi_handle chandle = NULL;
496 acpi_handle rethandle;
497 acpi_status ret;
498
499 /* Get the rootbus node pointer */
Rafael J. Wysocki7b199812013-11-11 22:41:56 +0100500 phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
John Keller3e643e72007-01-30 01:18:38 -0500501
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100502 acpi_scan_lock_acquire();
John Keller3e643e72007-01-30 01:18:38 -0500503 /*
504 * Walk the rootbus node's immediate children looking for
505 * the slot's device node(s). There can be more than
506 * one for multifunction devices.
507 */
508 for (;;) {
509 rethandle = NULL;
510 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
511 phandle, chandle,
512 &rethandle);
513
514 if (ret == AE_NOT_FOUND || rethandle == NULL)
515 break;
516
517 chandle = rethandle;
518
519 ret = acpi_evaluate_integer(chandle,
520 METHOD_NAME__ADR,
521 NULL, &adr);
522 if (ACPI_SUCCESS(ret) &&
523 (adr>>16) == (slot->device_num + 1)) {
524 /* retain the owner id */
Lv Zhenge044d8f2015-01-26 16:58:48 +0800525 ssdt_hdl = chandle;
John Keller3e643e72007-01-30 01:18:38 -0500526
527 ret = acpi_bus_get_device(chandle,
528 &device);
529 if (ACPI_SUCCESS(ret))
Rafael J. Wysockiae281792013-01-15 13:23:53 +0100530 acpi_bus_trim(device);
John Keller3e643e72007-01-30 01:18:38 -0500531 }
532 }
Rafael J. Wysocki3757b942013-02-13 14:36:47 +0100533 acpi_scan_lock_release();
John Keller3e643e72007-01-30 01:18:38 -0500534 }
535
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -0700536 pci_lock_rescan_remove();
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700537 /* Free the SN resources assigned to the Linux device.*/
Yijing Wangab6380e2013-01-15 11:12:21 +0800538 list_for_each_entry_safe(dev, temp, &slot->pci_bus->devices, bus_list) {
539 if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
540 continue;
541
542 pci_dev_get(dev);
543 sn_bus_free_data(dev);
544 pci_stop_and_remove_bus_device(dev);
545 pci_dev_put(dev);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700546 }
Rafael J. Wysockic4ec84c2014-01-14 12:03:14 -0700547 pci_unlock_rescan_remove();
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700548
John Keller3e643e72007-01-30 01:18:38 -0500549 /* Remove the SSDT for the slot from the ACPI namespace */
Lv Zhenge044d8f2015-01-26 16:58:48 +0800550 if (SN_ACPI_BASE_SUPPORT() && ssdt_hdl) {
John Keller3e643e72007-01-30 01:18:38 -0500551 acpi_status ret;
Lv Zhenge044d8f2015-01-26 16:58:48 +0800552 ret = acpi_unload_parent_table(ssdt_hdl);
John Keller3e643e72007-01-30 01:18:38 -0500553 if (ACPI_FAILURE(ret)) {
Lv Zhenge044d8f2015-01-26 16:58:48 +0800554 acpi_handle_err(ssdt_hdl,
555 "%s: acpi_unload_parent_table failed (0x%x)\n",
556 __func__, ret);
John Keller3e643e72007-01-30 01:18:38 -0500557 /* try to continue on */
558 }
559 }
560
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700561 /* free the collected sysdata pointers */
562 sn_bus_free_sysdata();
563
564 /* Deactivate slot */
565 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
566 PCI_REQ_SLOT_DISABLE);
567 leaving:
568 /* Release the bus lock */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800569 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700570
571 return rc;
572}
573
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400574static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
575 u8 *value)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700576{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400577 struct slot *slot = bss_hotplug_slot->private;
578 struct pcibus_info *pcibus_info;
Mike Habeck466ee362006-05-06 09:01:59 -0500579 u32 power;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400580
581 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800582 mutex_lock(&sn_hotplug_mutex);
Mike Habeck466ee362006-05-06 09:01:59 -0500583 power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
584 *value = power ? 1 : 0;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800585 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700586 return 0;
587}
588
589static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
590{
591 kfree(bss_hotplug_slot->info);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700592 kfree(bss_hotplug_slot->private);
593 kfree(bss_hotplug_slot);
594}
595
596static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
597{
598 int device;
Alex Chiangf46753c2008-06-10 15:28:50 -0600599 struct pci_slot *pci_slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700600 struct hotplug_slot *bss_hotplug_slot;
Alex Chiang85234ce2008-10-20 17:41:48 -0600601 char name[SN_SLOT_NAME_SIZE];
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700602 int rc = 0;
603
604 /*
605 * Currently only four devices are supported,
606 * in the future there maybe more -- up to 32.
607 */
608
609 for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
610 if (sn_pci_slot_valid(pci_bus, device) != 1)
611 continue;
612
Pekka Enberg656da9d2005-09-22 00:48:11 -0700613 bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700614 GFP_KERNEL);
615 if (!bss_hotplug_slot) {
616 rc = -ENOMEM;
617 goto alloc_err;
618 }
619
620 bss_hotplug_slot->info =
Pekka Enberg656da9d2005-09-22 00:48:11 -0700621 kzalloc(sizeof(struct hotplug_slot_info),
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700622 GFP_KERNEL);
623 if (!bss_hotplug_slot->info) {
624 rc = -ENOMEM;
625 goto alloc_err;
626 }
627
628 if (sn_hp_slot_private_alloc(bss_hotplug_slot,
Alex Chiang85234ce2008-10-20 17:41:48 -0600629 pci_bus, device, name)) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700630 rc = -ENOMEM;
631 goto alloc_err;
632 }
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700633 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
634 bss_hotplug_slot->release = &sn_release_slot;
635
Alex Chiang85234ce2008-10-20 17:41:48 -0600636 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device, name);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700637 if (rc)
638 goto register_err;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400639
Alex Chiangf46753c2008-06-10 15:28:50 -0600640 pci_slot = bss_hotplug_slot->pci_slot;
641 rc = sysfs_create_file(&pci_slot->kobj,
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400642 &sn_slot_path_attr.attr);
643 if (rc)
644 goto register_err;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700645 }
Tony Luck34ef30c2007-05-10 09:39:41 -0700646 dev_dbg(&pci_bus->self->dev, "Registered bus with hotplug\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700647 return rc;
648
649register_err:
Tony Luck34ef30c2007-05-10 09:39:41 -0700650 dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n",
Alex Chiang8344b562008-06-10 15:30:42 -0600651 rc);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700652
653alloc_err:
654 if (rc == -ENOMEM)
Tony Luck34ef30c2007-05-10 09:39:41 -0700655 dev_dbg(&pci_bus->self->dev, "Memory allocation error\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700656
657 /* destroy THIS element */
658 if (bss_hotplug_slot)
659 sn_release_slot(bss_hotplug_slot);
660
661 /* destroy anything else on the list */
662 while ((bss_hotplug_slot = sn_hp_destroy()))
663 pci_hp_deregister(bss_hotplug_slot);
664
665 return rc;
666}
667
Peter Huewedb5ed9b2009-06-06 14:58:56 +0200668static int __init sn_pci_hotplug_init(void)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700669{
670 struct pci_bus *pci_bus = NULL;
671 int rc;
672 int registered = 0;
673
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400674 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
675 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800676 __func__);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700677 return -EPERM;
678 }
679
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400680 INIT_LIST_HEAD(&sn_hp_list);
681
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700682 while ((pci_bus = pci_find_next_bus(pci_bus))) {
683 if (!pci_bus->sysdata)
684 continue;
685
686 rc = sn_pci_bus_valid(pci_bus);
687 if (rc != 1) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700688 dev_dbg(&pci_bus->self->dev, "not a valid hotplug bus\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700689 continue;
690 }
Tony Luck34ef30c2007-05-10 09:39:41 -0700691 dev_dbg(&pci_bus->self->dev, "valid hotplug bus\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700692
693 rc = sn_hotplug_slot_register(pci_bus);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400694 if (!rc) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700695 registered = 1;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400696 } else {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700697 registered = 0;
698 break;
699 }
700 }
701
702 return registered == 1 ? 0 : -ENODEV;
703}
704
Peter Huewedb5ed9b2009-06-06 14:58:56 +0200705static void __exit sn_pci_hotplug_exit(void)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700706{
707 struct hotplug_slot *bss_hotplug_slot;
708
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400709 while ((bss_hotplug_slot = sn_hp_destroy()))
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700710 pci_hp_deregister(bss_hotplug_slot);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700711
712 if (!list_empty(&sn_hp_list))
713 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
714}
715
716module_init(sn_pci_hotplug_init);
717module_exit(sn_pci_hotplug_exit);