blob: 5d188c558386b73aadd714c21cc9e305483c0d44 [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>
31
32#include "../pci.h"
Prarit Bhargava6f354b02005-07-06 15:29:53 -070033
34MODULE_LICENSE("GPL");
35MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
36MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
37
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040038#define PCIIO_ASIC_TYPE_TIOCA 4
39#define PCI_SLOT_ALREADY_UP 2 /* slot already up */
40#define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
41#define PCI_L1_ERR 7 /* L1 console command error */
42#define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
43#define PCI_L1_QSIZE 128 /* our L1 message buffer size */
44#define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
45#define SGI_HOTPLUG_PROM_REV 0x0430 /* Min. required PROM version */
46#define SN_SLOT_NAME_SIZE 33 /* size of name string */
Prarit Bhargava6f354b02005-07-06 15:29:53 -070047
48/* internal list head */
49static struct list_head sn_hp_list;
50
51/* hotplug_slot struct's private pointer */
52struct slot {
53 int device_num;
54 struct pci_bus *pci_bus;
55 /* this struct for glue internal only */
56 struct hotplug_slot *hotplug_slot;
57 struct list_head hp_list;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040058 char physical_path[SN_SLOT_NAME_SIZE];
Prarit Bhargava6f354b02005-07-06 15:29:53 -070059};
60
61struct pcibr_slot_enable_resp {
62 int resp_sub_errno;
63 char resp_l1_msg[PCI_L1_QSIZE + 1];
64};
65
66struct pcibr_slot_disable_resp {
67 int resp_sub_errno;
68 char resp_l1_msg[PCI_L1_QSIZE + 1];
69};
70
71enum sn_pci_req_e {
72 PCI_REQ_SLOT_ELIGIBLE,
73 PCI_REQ_SLOT_DISABLE
74};
75
76static int enable_slot(struct hotplug_slot *slot);
77static int disable_slot(struct hotplug_slot *slot);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040078static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
Prarit Bhargava6f354b02005-07-06 15:29:53 -070079
80static struct hotplug_slot_ops sn_hotplug_slot_ops = {
81 .owner = THIS_MODULE,
82 .enable_slot = enable_slot,
83 .disable_slot = disable_slot,
84 .get_power_status = get_power_status,
85};
86
Ingo Molnar14cc3e22006-03-26 01:37:14 -080087static DEFINE_MUTEX(sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -070088
Prarit Bhargava1d2450a2005-08-12 10:13:34 -040089static ssize_t path_show (struct hotplug_slot *bss_hotplug_slot,
90 char *buf)
91{
92 int retval = -ENOENT;
93 struct slot *slot = bss_hotplug_slot->private;
94
95 if (!slot)
96 return retval;
97
98 retval = sprintf (buf, "%s\n", slot->physical_path);
99 return retval;
100}
101
102static struct hotplug_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
103
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700104static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
105{
106 struct pcibus_info *pcibus_info;
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400107 u16 busnum, segment, ioboard_type;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700108
109 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
110
111 /* Check to see if this is a valid slot on 'pci_bus' */
112 if (!(pcibus_info->pbi_valid_devices & (1 << device)))
113 return -EPERM;
114
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400115 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
116 busnum = pcibus_info->pbi_buscommon.bs_persist_busnum;
117 segment = pci_domain_nr(pci_bus) & 0xf;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700118
119 /* Do not allow hotplug operations on base I/O cards */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400120 if ((ioboard_type == L1_BRICKTYPE_IX ||
121 ioboard_type == L1_BRICKTYPE_IA) &&
122 (segment == 1 && busnum == 0 && device != 1))
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700123 return -EPERM;
124
125 return 1;
126}
127
128static int sn_pci_bus_valid(struct pci_bus *pci_bus)
129{
130 struct pcibus_info *pcibus_info;
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400131 u32 asic_type;
132 u16 ioboard_type;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700133
134 /* Don't register slots hanging off the TIOCA bus */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400135 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700136 asic_type = pcibus_info->pbi_buscommon.bs_asic_type;
137 if (asic_type == PCIIO_ASIC_TYPE_TIOCA)
138 return -EPERM;
139
140 /* Only register slots in I/O Bricks that support hotplug */
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400141 ioboard_type = sn_ioboard_to_pci_bus(pci_bus);
142 switch (ioboard_type) {
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400143 case L1_BRICKTYPE_IX:
144 case L1_BRICKTYPE_PX:
145 case L1_BRICKTYPE_IA:
146 case L1_BRICKTYPE_PA:
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400147 case L1_BOARDTYPE_PCIX3SLOT:
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400148 return 1;
149 break;
150 default:
151 return -EPERM;
152 break;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700153 }
154
155 return -EIO;
156}
157
158static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
159 struct pci_bus *pci_bus, int device)
160{
161 struct pcibus_info *pcibus_info;
162 struct slot *slot;
163
164 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
165
Pekka Enberg656da9d2005-09-22 00:48:11 -0700166 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400167 if (!slot)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700168 return -ENOMEM;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400169 bss_hotplug_slot->private = slot;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700170
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400171 bss_hotplug_slot->name = kmalloc(SN_SLOT_NAME_SIZE, GFP_KERNEL);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700172 if (!bss_hotplug_slot->name) {
173 kfree(bss_hotplug_slot->private);
174 return -ENOMEM;
175 }
176
177 slot->device_num = device;
178 slot->pci_bus = pci_bus;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400179 sprintf(bss_hotplug_slot->name, "%04x:%02x:%02x",
180 pci_domain_nr(pci_bus),
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400181 ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum),
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400182 device + 1);
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400183
184 sn_generate_path(pci_bus, slot->physical_path);
185
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700186 slot->hotplug_slot = bss_hotplug_slot;
187 list_add(&slot->hp_list, &sn_hp_list);
188
189 return 0;
190}
191
192static struct hotplug_slot * sn_hp_destroy(void)
193{
194 struct slot *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;
199 list_del(&((struct slot *)bss_hotplug_slot->private)->
200 hp_list);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400201 sysfs_remove_file(&bss_hotplug_slot->kobj,
202 &sn_slot_path_attr.attr);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700203 break;
204 }
205 return bss_hotplug_slot;
206}
207
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700208static void sn_bus_free_data(struct pci_dev *dev)
209{
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700210 struct pci_bus *subordinate_bus;
211 struct pci_dev *child;
212
213 /* Recursively clean up sn_irq_info structs */
214 if (dev->subordinate) {
215 subordinate_bus = dev->subordinate;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400216 list_for_each_entry(child, &subordinate_bus->devices, bus_list)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700217 sn_bus_free_data(child);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700218 }
John Keller8e77af62006-03-08 13:21:34 -0600219 /*
220 * Some drivers may use dma accesses during the
221 * driver remove function. We release the sysdata
222 * areas after the driver remove functions have
223 * been called.
224 */
225 sn_bus_store_sysdata(dev);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700226 sn_pci_unfixup_slot(dev);
227}
228
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700229static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
230 int device_num)
231{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400232 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700233 struct pcibus_info *pcibus_info;
234 struct pcibr_slot_enable_resp resp;
235 int rc;
236
237 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
238
239 /*
240 * Power-on and initialize the slot in the SN
241 * PCI infrastructure.
242 */
243 rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp);
244
245 if (rc == PCI_SLOT_ALREADY_UP) {
246 dev_dbg(slot->pci_bus->self, "is already active\n");
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400247 return 1; /* return 1 to user */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700248 }
249
250 if (rc == PCI_L1_ERR) {
251 dev_dbg(slot->pci_bus->self,
252 "L1 failure %d with message: %s",
253 resp.resp_sub_errno, resp.resp_l1_msg);
254 return -EPERM;
255 }
256
257 if (rc) {
258 dev_dbg(slot->pci_bus->self,
259 "insert failed with error %d sub-error %d\n",
260 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)) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700284 dev_dbg(slot->pci_bus->self, "Slot %s already inactive\n");
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)) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700289 dev_dbg(slot->pci_bus->self,
290 "Cannot remove last 33MHz card\n");
291 return -EPERM;
292 }
293
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400294 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700295 dev_dbg(slot->pci_bus->self,
296 "L1 failure %d with message \n%s\n",
297 resp.resp_sub_errno, resp.resp_l1_msg);
298 return -EPERM;
299 }
300
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400301 if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700302 dev_dbg(slot->pci_bus->self,
303 "remove failed with error %d sub-error %d\n",
304 rc, resp.resp_sub_errno);
305 return -EIO;
306 }
307
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400308 if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700309 return 0;
310
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400311 if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
312 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
313 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700314 dev_dbg(slot->pci_bus->self, "remove successful\n");
315 return 0;
316 }
317
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400318 if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700319 dev_dbg(slot->pci_bus->self,"remove failed rc = %d\n", rc);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700320 }
321
322 return rc;
323}
324
John Keller9f581f12006-10-04 16:49:35 -0500325/*
326 * Power up and configure the slot via a SAL call to PROM.
327 * Scan slot (and any children), do any platform specific fixup,
328 * and find device driver.
329 */
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700330static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
331{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400332 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700333 struct pci_bus *new_bus = NULL;
334 struct pci_dev *dev;
335 int func, num_funcs;
336 int new_ppb = 0;
337 int rc;
John Keller9f581f12006-10-04 16:49:35 -0500338 void pcibios_fixup_device_resources(struct pci_dev *);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700339
340 /* Serialize the Linux PCI infrastructure */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800341 mutex_lock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700342
343 /*
344 * Power-on and initialize the slot in the SN
345 * PCI infrastructure.
346 */
347 rc = sn_slot_enable(bss_hotplug_slot, slot->device_num);
348 if (rc) {
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800349 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700350 return rc;
351 }
352
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400353 num_funcs = pci_scan_slot(slot->pci_bus,
354 PCI_DEVFN(slot->device_num + 1, 0));
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700355 if (!num_funcs) {
356 dev_dbg(slot->pci_bus->self, "no device in slot\n");
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800357 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700358 return -ENODEV;
359 }
360
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700361 /*
362 * Map SN resources for all functions on the card
363 * to the Linux PCI interface and tell the drivers
364 * about them.
365 */
366 for (func = 0; func < num_funcs; func++) {
367 dev = pci_get_slot(slot->pci_bus,
368 PCI_DEVFN(slot->device_num + 1,
369 PCI_FUNC(func)));
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700370 if (dev) {
John Keller9f581f12006-10-04 16:49:35 -0500371 /* Need to do slot fixup on PPB before fixup of children
372 * (PPB's pcidev_info needs to be in pcidev_info list
373 * before child's SN_PCIDEV_INFO() call to setup
374 * pdi_host_pcidev_info).
375 */
376 pcibios_fixup_device_resources(dev);
377 sn_pci_fixup_slot(dev);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700378 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
379 unsigned char sec_bus;
380 pci_read_config_byte(dev, PCI_SECONDARY_BUS,
381 &sec_bus);
382 new_bus = pci_add_new_bus(dev->bus, dev,
383 sec_bus);
384 pci_scan_child_bus(new_bus);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700385 new_ppb = 1;
386 }
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700387 pci_dev_put(dev);
388 }
389 }
390
391 /* Call the driver for the new device */
392 pci_bus_add_devices(slot->pci_bus);
393 /* Call the drivers for the new devices subordinate to PPB */
394 if (new_ppb)
395 pci_bus_add_devices(new_bus);
396
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800397 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700398
399 if (rc == 0)
400 dev_dbg(slot->pci_bus->self,
401 "insert operation successful\n");
402 else
403 dev_dbg(slot->pci_bus->self,
404 "insert operation failed rc = %d\n", rc);
405
406 return rc;
407}
408
409static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
410{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400411 struct slot *slot = bss_hotplug_slot->private;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700412 struct pci_dev *dev;
413 int func;
414 int rc;
415
416 /* Acquire update access to the bus */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800417 mutex_lock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700418
419 /* is it okay to bring this slot down? */
420 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
421 PCI_REQ_SLOT_ELIGIBLE);
422 if (rc)
423 goto leaving;
424
425 /* Free the SN resources assigned to the Linux device.*/
426 for (func = 0; func < 8; func++) {
427 dev = pci_get_slot(slot->pci_bus,
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400428 PCI_DEVFN(slot->device_num + 1,
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700429 PCI_FUNC(func)));
430 if (dev) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700431 sn_bus_free_data(dev);
432 pci_remove_bus_device(dev);
433 pci_dev_put(dev);
434 }
435 }
436
437 /* free the collected sysdata pointers */
438 sn_bus_free_sysdata();
439
440 /* Deactivate slot */
441 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
442 PCI_REQ_SLOT_DISABLE);
443 leaving:
444 /* Release the bus lock */
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800445 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700446
447 return rc;
448}
449
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400450static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
451 u8 *value)
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700452{
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400453 struct slot *slot = bss_hotplug_slot->private;
454 struct pcibus_info *pcibus_info;
Mike Habeck466ee362006-05-06 09:01:59 -0500455 u32 power;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400456
457 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800458 mutex_lock(&sn_hotplug_mutex);
Mike Habeck466ee362006-05-06 09:01:59 -0500459 power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
460 *value = power ? 1 : 0;
Ingo Molnar14cc3e22006-03-26 01:37:14 -0800461 mutex_unlock(&sn_hotplug_mutex);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700462 return 0;
463}
464
465static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
466{
467 kfree(bss_hotplug_slot->info);
468 kfree(bss_hotplug_slot->name);
469 kfree(bss_hotplug_slot->private);
470 kfree(bss_hotplug_slot);
471}
472
473static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
474{
475 int device;
476 struct hotplug_slot *bss_hotplug_slot;
477 int rc = 0;
478
479 /*
480 * Currently only four devices are supported,
481 * in the future there maybe more -- up to 32.
482 */
483
484 for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
485 if (sn_pci_slot_valid(pci_bus, device) != 1)
486 continue;
487
Pekka Enberg656da9d2005-09-22 00:48:11 -0700488 bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700489 GFP_KERNEL);
490 if (!bss_hotplug_slot) {
491 rc = -ENOMEM;
492 goto alloc_err;
493 }
494
495 bss_hotplug_slot->info =
Pekka Enberg656da9d2005-09-22 00:48:11 -0700496 kzalloc(sizeof(struct hotplug_slot_info),
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700497 GFP_KERNEL);
498 if (!bss_hotplug_slot->info) {
499 rc = -ENOMEM;
500 goto alloc_err;
501 }
502
503 if (sn_hp_slot_private_alloc(bss_hotplug_slot,
504 pci_bus, device)) {
505 rc = -ENOMEM;
506 goto alloc_err;
507 }
508
509 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
510 bss_hotplug_slot->release = &sn_release_slot;
511
512 rc = pci_hp_register(bss_hotplug_slot);
513 if (rc)
514 goto register_err;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400515
516 rc = sysfs_create_file(&bss_hotplug_slot->kobj,
517 &sn_slot_path_attr.attr);
518 if (rc)
519 goto register_err;
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700520 }
521 dev_dbg(pci_bus->self, "Registered bus with hotplug\n");
522 return rc;
523
524register_err:
525 dev_dbg(pci_bus->self, "bus failed to register with err = %d\n",
526 rc);
527
528alloc_err:
529 if (rc == -ENOMEM)
530 dev_dbg(pci_bus->self, "Memory allocation error\n");
531
532 /* destroy THIS element */
533 if (bss_hotplug_slot)
534 sn_release_slot(bss_hotplug_slot);
535
536 /* destroy anything else on the list */
537 while ((bss_hotplug_slot = sn_hp_destroy()))
538 pci_hp_deregister(bss_hotplug_slot);
539
540 return rc;
541}
542
543static int sn_pci_hotplug_init(void)
544{
545 struct pci_bus *pci_bus = NULL;
546 int rc;
547 int registered = 0;
548
Prarit Bhargavae55dea52006-04-04 09:26:46 -0400549 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
550 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700551 __FUNCTION__);
552 return -EPERM;
553 }
554
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400555 INIT_LIST_HEAD(&sn_hp_list);
556
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700557 while ((pci_bus = pci_find_next_bus(pci_bus))) {
558 if (!pci_bus->sysdata)
559 continue;
560
561 rc = sn_pci_bus_valid(pci_bus);
562 if (rc != 1) {
563 dev_dbg(pci_bus->self, "not a valid hotplug bus\n");
564 continue;
565 }
566 dev_dbg(pci_bus->self, "valid hotplug bus\n");
567
568 rc = sn_hotplug_slot_register(pci_bus);
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400569 if (!rc) {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700570 registered = 1;
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400571 } else {
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700572 registered = 0;
573 break;
574 }
575 }
576
577 return registered == 1 ? 0 : -ENODEV;
578}
579
580static void sn_pci_hotplug_exit(void)
581{
582 struct hotplug_slot *bss_hotplug_slot;
583
Prarit Bhargava1d2450a2005-08-12 10:13:34 -0400584 while ((bss_hotplug_slot = sn_hp_destroy()))
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700585 pci_hp_deregister(bss_hotplug_slot);
Prarit Bhargava6f354b02005-07-06 15:29:53 -0700586
587 if (!list_empty(&sn_hp_list))
588 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
589}
590
591module_init(sn_pci_hotplug_init);
592module_exit(sn_pci_hotplug_exit);