blob: 14ad22e07e00983bea6b9e24b42e962b88019c5c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070019#include <linux/types.h>
Ingo Molnar14cc3e22006-03-26 01:37:14 -080020#include <linux/mutex.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070021
22#include <asm/sn/addrs.h>
Prarit Bhargavae55dea52006-04-04 09:26:46 -040023#include <asm/sn/geo.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070024#include <asm/sn/l1.h>
25#include <asm/sn/module.h>
26#include <asm/sn/pcibr_provider.h>
27#include <asm/sn/pcibus_provider_defs.h>
28#include <asm/sn/pcidev.h>
Prarit Bhargavae55dea52006-04-04 09:26:46 -040029#include <asm/sn/sn_feature_sets.h>
Prarit Bhargava6f354b02005-07-06 15:29:53 -070030#include <asm/sn/sn_sal.h>
31#include <asm/sn/types.h>
John Keller3e643e72007-01-30 01:18:38 -050032#include <linux/acpi.h>
33#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
191static struct hotplug_slot * sn_hp_destroy(void)
192{
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) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700253 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700254 "L1 failure %d with message: %s",
255 resp.resp_sub_errno, resp.resp_l1_msg);
256 return -EPERM;
257 }
258
259 if (rc) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700260 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700261 "insert failed with error %d sub-error %d\n",
262 rc, resp.resp_sub_errno);
263 return -EIO;
264 }
265
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400266 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
267 pcibus_info->pbi_enabled_devices |= (1 << device_num);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700268
269 return 0;
270}
271
272static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
273 int device_num, int action)
274{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400275 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700276 struct pcibus_info *pcibus_info;
277 struct pcibr_slot_disable_resp resp;
278 int rc;
279
280 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
281
282 rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
283
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400284 if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
285 (rc == PCI_SLOT_ALREADY_DOWN)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700286 dev_dbg(&slot->pci_bus->self->dev, "Slot %s already inactive\n", slot->physical_path);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400287 return 1; /* return 1 to user */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700288 }
289
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400290 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700291 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700292 "Cannot remove last 33MHz card\n");
293 return -EPERM;
294 }
295
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400296 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700297 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700298 "L1 failure %d with message \n%s\n",
299 resp.resp_sub_errno, resp.resp_l1_msg);
300 return -EPERM;
301 }
302
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400303 if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700304 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700305 "remove failed with error %d sub-error %d\n",
306 rc, resp.resp_sub_errno);
307 return -EIO;
308 }
309
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400310 if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700311 return 0;
312
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400313 if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
314 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
315 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
Tony Luck34ef30c2007-05-10 09:39:41 -0700316 dev_dbg(&slot->pci_bus->self->dev, "remove successful\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700317 return 0;
318 }
319
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400320 if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700321 dev_dbg(&slot->pci_bus->self->dev,"remove failed rc = %d\n", rc);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700322 }
323
324 return rc;
325}
326
John Keller9f581f12006-10-04 16:49:35 -0500327/*
328 * Power up and configure the slot via a SAL call to PROM.
329 * Scan slot (and any children), do any platform specific fixup,
330 * and find device driver.
331 */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700332static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
333{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400334 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700335 struct pci_bus *new_bus = NULL;
336 struct pci_dev *dev;
Yijing Wangab6380e2013-01-15 11:12:21 +0800337 int num_funcs;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700338 int new_ppb = 0;
339 int rc;
John Keller3e643e72007-01-30 01:18:38 -0500340 char *ssdt = NULL;
John Keller9f581f12006-10-04 16:49:35 -0500341 void pcibios_fixup_device_resources(struct pci_dev *);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700342
343 /* Serialize the Linux PCI infrastructure */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800344 mutex_lock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700345
346 /*
347 * Power-on and initialize the slot in the SN
John Keller3e643e72007-01-30 01:18:38 -0500348 * PCI infrastructure. Also, retrieve the ACPI SSDT
349 * table for the slot (if ACPI capable PROM).
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700350 */
John Keller3e643e72007-01-30 01:18:38 -0500351 rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700352 if (rc) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800353 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700354 return rc;
355 }
356
John Keller3e643e72007-01-30 01:18:38 -0500357 if (ssdt)
358 ssdt = __va(ssdt);
359 /* Add the new SSDT for the slot to the ACPI namespace */
360 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
361 acpi_status ret;
362
363 ret = acpi_load_table((struct acpi_table_header *)ssdt);
364 if (ACPI_FAILURE(ret)) {
365 printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800366 __func__, ret);
John Keller3e643e72007-01-30 01:18:38 -0500367 /* try to continue on */
368 }
369 }
370
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400371 num_funcs = pci_scan_slot(slot->pci_bus,
372 PCI_DEVFN(slot->device_num + 1, 0));
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700373 if (!num_funcs) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700374 dev_dbg(&slot->pci_bus->self->dev, "no device in slot\n");
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800375 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700376 return -ENODEV;
377 }
378
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700379 /*
380 * Map SN resources for all functions on the card
381 * to the Linux PCI interface and tell the drivers
382 * about them.
383 */
Yijing Wangab6380e2013-01-15 11:12:21 +0800384 list_for_each_entry(dev, &slot->pci_bus->devices, bus_list) {
385 if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
386 continue;
387
388 /* Need to do slot fixup on PPB before fixup of children
389 * (PPB's pcidev_info needs to be in pcidev_info list
390 * before child's SN_PCIDEV_INFO() call to setup
391 * pdi_host_pcidev_info).
392 */
393 pcibios_fixup_device_resources(dev);
394 if (SN_ACPI_BASE_SUPPORT())
395 sn_acpi_slot_fixup(dev);
396 else
397 sn_io_slot_fixup(dev);
398 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
399 pci_hp_add_bridge(dev);
400 if (dev->subordinate) {
401 new_bus = dev->subordinate;
402 new_ppb = 1;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700403 }
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700404 }
405 }
406
John Keller3e643e72007-01-30 01:18:38 -0500407 /*
408 * Add the slot's devices to the ACPI infrastructure */
409 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400410 unsigned long long adr;
John Keller3e643e72007-01-30 01:18:38 -0500411 struct acpi_device *pdevice;
412 struct acpi_device *device;
413 acpi_handle phandle;
414 acpi_handle chandle = NULL;
415 acpi_handle rethandle;
416 acpi_status ret;
417
418 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
419
420 if (acpi_bus_get_device(phandle, &pdevice)) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700421 dev_dbg(&slot->pci_bus->self->dev,
John Keller3e643e72007-01-30 01:18:38 -0500422 "no parent device, assuming NULL\n");
423 pdevice = NULL;
424 }
425
426 /*
427 * Walk the rootbus node's immediate children looking for
428 * the slot's device node(s). There can be more than
429 * one for multifunction devices.
430 */
431 for (;;) {
432 rethandle = NULL;
433 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
434 phandle, chandle,
435 &rethandle);
436
437 if (ret == AE_NOT_FOUND || rethandle == NULL)
438 break;
439
440 chandle = rethandle;
441
442 ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
443 NULL, &adr);
444
445 if (ACPI_SUCCESS(ret) &&
446 (adr>>16) == (slot->device_num + 1)) {
447
448 ret = acpi_bus_add(&device, pdevice, chandle,
449 ACPI_BUS_TYPE_DEVICE);
450 if (ACPI_FAILURE(ret)) {
451 printk(KERN_ERR "%s: acpi_bus_add "
452 "failed (0x%x) for slot %d "
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800453 "func %d\n", __func__,
John Keller3e643e72007-01-30 01:18:38 -0500454 ret, (int)(adr>>16),
455 (int)(adr&0xffff));
456 /* try to continue on */
457 } else {
458 acpi_bus_start(device);
459 }
460 }
461 }
462 }
463
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700464 /* Call the driver for the new device */
465 pci_bus_add_devices(slot->pci_bus);
466 /* Call the drivers for the new devices subordinate to PPB */
467 if (new_ppb)
468 pci_bus_add_devices(new_bus);
469
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800470 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700471
472 if (rc == 0)
Tony Luck34ef30c2007-05-10 09:39:41 -0700473 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700474 "insert operation successful\n");
475 else
Tony Luck34ef30c2007-05-10 09:39:41 -0700476 dev_dbg(&slot->pci_bus->self->dev,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700477 "insert operation failed rc = %d\n", rc);
478
479 return rc;
480}
481
482static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
483{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400484 struct slot *slot = bss_hotplug_slot->private;
Yijing Wangab6380e2013-01-15 11:12:21 +0800485 struct pci_dev *dev, *temp;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700486 int rc;
John Keller3e643e72007-01-30 01:18:38 -0500487 acpi_owner_id ssdt_id = 0;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700488
489 /* Acquire update access to the bus */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800490 mutex_lock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700491
492 /* is it okay to bring this slot down? */
493 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
494 PCI_REQ_SLOT_ELIGIBLE);
495 if (rc)
496 goto leaving;
497
John Keller3e643e72007-01-30 01:18:38 -0500498 /* free the ACPI resources for the slot */
499 if (SN_ACPI_BASE_SUPPORT() &&
500 PCI_CONTROLLER(slot->pci_bus)->acpi_handle) {
Matthew Wilcox27663c52008-10-10 02:22:59 -0400501 unsigned long long adr;
John Keller3e643e72007-01-30 01:18:38 -0500502 struct acpi_device *device;
503 acpi_handle phandle;
504 acpi_handle chandle = NULL;
505 acpi_handle rethandle;
506 acpi_status ret;
507
508 /* Get the rootbus node pointer */
509 phandle = PCI_CONTROLLER(slot->pci_bus)->acpi_handle;
510
511 /*
512 * Walk the rootbus node's immediate children looking for
513 * the slot's device node(s). There can be more than
514 * one for multifunction devices.
515 */
516 for (;;) {
517 rethandle = NULL;
518 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
519 phandle, chandle,
520 &rethandle);
521
522 if (ret == AE_NOT_FOUND || rethandle == NULL)
523 break;
524
525 chandle = rethandle;
526
527 ret = acpi_evaluate_integer(chandle,
528 METHOD_NAME__ADR,
529 NULL, &adr);
530 if (ACPI_SUCCESS(ret) &&
531 (adr>>16) == (slot->device_num + 1)) {
532 /* retain the owner id */
533 acpi_get_id(chandle, &ssdt_id);
534
535 ret = acpi_bus_get_device(chandle,
536 &device);
537 if (ACPI_SUCCESS(ret))
538 acpi_bus_trim(device, 1);
539 }
540 }
541
542 }
543
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700544 /* Free the SN resources assigned to the Linux device.*/
Yijing Wangab6380e2013-01-15 11:12:21 +0800545 list_for_each_entry_safe(dev, temp, &slot->pci_bus->devices, bus_list) {
546 if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
547 continue;
548
549 pci_dev_get(dev);
550 sn_bus_free_data(dev);
551 pci_stop_and_remove_bus_device(dev);
552 pci_dev_put(dev);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700553 }
554
John Keller3e643e72007-01-30 01:18:38 -0500555 /* Remove the SSDT for the slot from the ACPI namespace */
556 if (SN_ACPI_BASE_SUPPORT() && ssdt_id) {
557 acpi_status ret;
558 ret = acpi_unload_table_id(ssdt_id);
559 if (ACPI_FAILURE(ret)) {
560 printk(KERN_ERR "%s: acpi_unload_table_id "
561 "failed (0x%x) for id %d\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800562 __func__, ret, ssdt_id);
John Keller3e643e72007-01-30 01:18:38 -0500563 /* try to continue on */
564 }
565 }
566
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700567 /* free the collected sysdata pointers */
568 sn_bus_free_sysdata();
569
570 /* Deactivate slot */
571 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
572 PCI_REQ_SLOT_DISABLE);
573 leaving:
574 /* Release the bus lock */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800575 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700576
577 return rc;
578}
579
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400580static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
581 u8 *value)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700582{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400583 struct slot *slot = bss_hotplug_slot->private;
584 struct pcibus_info *pcibus_info;
Mike Habeck466ee362006-05-06 09:01:59 -0500585 u32 power;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400586
587 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800588 mutex_lock(&sn_hotplug_mutex);
Mike Habeck466ee362006-05-06 09:01:59 -0500589 power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
590 *value = power ? 1 : 0;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800591 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700592 return 0;
593}
594
595static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
596{
597 kfree(bss_hotplug_slot->info);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700598 kfree(bss_hotplug_slot->private);
599 kfree(bss_hotplug_slot);
600}
601
602static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
603{
604 int device;
Alex Chiangf46753c2008-06-10 15:28:50 -0600605 struct pci_slot *pci_slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700606 struct hotplug_slot *bss_hotplug_slot;
Alex Chiang85234ce2008-10-20 17:41:48 -0600607 char name[SN_SLOT_NAME_SIZE];
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700608 int rc = 0;
609
610 /*
611 * Currently only four devices are supported,
612 * in the future there maybe more -- up to 32.
613 */
614
615 for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
616 if (sn_pci_slot_valid(pci_bus, device) != 1)
617 continue;
618
Pekka Enberg656da9d2005-09-22 00:48:11 -0700619 bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700620 GFP_KERNEL);
621 if (!bss_hotplug_slot) {
622 rc = -ENOMEM;
623 goto alloc_err;
624 }
625
626 bss_hotplug_slot->info =
Pekka Enberg656da9d2005-09-22 00:48:11 -0700627 kzalloc(sizeof(struct hotplug_slot_info),
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700628 GFP_KERNEL);
629 if (!bss_hotplug_slot->info) {
630 rc = -ENOMEM;
631 goto alloc_err;
632 }
633
634 if (sn_hp_slot_private_alloc(bss_hotplug_slot,
Alex Chiang85234ce2008-10-20 17:41:48 -0600635 pci_bus, device, name)) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700636 rc = -ENOMEM;
637 goto alloc_err;
638 }
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700639 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
640 bss_hotplug_slot->release = &sn_release_slot;
641
Alex Chiang85234ce2008-10-20 17:41:48 -0600642 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device, name);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700643 if (rc)
644 goto register_err;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400645
Alex Chiangf46753c2008-06-10 15:28:50 -0600646 pci_slot = bss_hotplug_slot->pci_slot;
647 rc = sysfs_create_file(&pci_slot->kobj,
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400648 &sn_slot_path_attr.attr);
649 if (rc)
650 goto register_err;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700651 }
Tony Luck34ef30c2007-05-10 09:39:41 -0700652 dev_dbg(&pci_bus->self->dev, "Registered bus with hotplug\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700653 return rc;
654
655register_err:
Tony Luck34ef30c2007-05-10 09:39:41 -0700656 dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n",
Alex Chiang8344b562008-06-10 15:30:42 -0600657 rc);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700658
659alloc_err:
660 if (rc == -ENOMEM)
Tony Luck34ef30c2007-05-10 09:39:41 -0700661 dev_dbg(&pci_bus->self->dev, "Memory allocation error\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700662
663 /* destroy THIS element */
664 if (bss_hotplug_slot)
665 sn_release_slot(bss_hotplug_slot);
666
667 /* destroy anything else on the list */
668 while ((bss_hotplug_slot = sn_hp_destroy()))
669 pci_hp_deregister(bss_hotplug_slot);
670
671 return rc;
672}
673
Peter Huewedb5ed9b2009-06-06 14:58:56 +0200674static int __init sn_pci_hotplug_init(void)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700675{
676 struct pci_bus *pci_bus = NULL;
677 int rc;
678 int registered = 0;
679
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400680 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
681 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
Harvey Harrison66bef8c2008-03-03 19:09:46 -0800682 __func__);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700683 return -EPERM;
684 }
685
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400686 INIT_LIST_HEAD(&sn_hp_list);
687
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700688 while ((pci_bus = pci_find_next_bus(pci_bus))) {
689 if (!pci_bus->sysdata)
690 continue;
691
692 rc = sn_pci_bus_valid(pci_bus);
693 if (rc != 1) {
Tony Luck34ef30c2007-05-10 09:39:41 -0700694 dev_dbg(&pci_bus->self->dev, "not a valid hotplug bus\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700695 continue;
696 }
Tony Luck34ef30c2007-05-10 09:39:41 -0700697 dev_dbg(&pci_bus->self->dev, "valid hotplug bus\n");
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700698
699 rc = sn_hotplug_slot_register(pci_bus);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400700 if (!rc) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700701 registered = 1;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400702 } else {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700703 registered = 0;
704 break;
705 }
706 }
707
708 return registered == 1 ? 0 : -ENODEV;
709}
710
Peter Huewedb5ed9b2009-06-06 14:58:56 +0200711static void __exit sn_pci_hotplug_exit(void)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700712{
713 struct hotplug_slot *bss_hotplug_slot;
714
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400715 while ((bss_hotplug_slot = sn_hp_destroy()))
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700716 pci_hp_deregister(bss_hotplug_slot);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700717
718 if (!list_empty(&sn_hp_list))
719 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
720}
721
722module_init(sn_pci_hotplug_init);
723module_exit(sn_pci_hotplug_exit);