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
6 * Copyright (C) 2005-2006 Silicon Graphics, Inc. All rights reserved.
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.
12 #include <linux/acpi.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/pci.h>
17 #include <linux/pci_hotplug.h>
18 #include <linux/proc_fs.h>
19 #include <linux/slab.h>
20 #include <linux/types.h>
21 #include <linux/mutex.h>
23 #include <asm/sn/addrs.h>
24 #include <asm/sn/geo.h>
25 #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>
30 #include <asm/sn/sn_feature_sets.h>
31 #include <asm/sn/sn_sal.h>
32 #include <asm/sn/types.h>
33 #include <asm/sn/acpi.h>
37 MODULE_LICENSE("GPL");
38 MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
39 MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
42 /* SAL call error codes. Keep in sync with prom header io/include/pcibr.h */
43 #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 */
49 #define PCIIO_ASIC_TYPE_TIOCA 4
50 #define PCI_L1_QSIZE 128 /* our L1 message buffer size */
51 #define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
52 #define SN_SLOT_NAME_SIZE 33 /* size of name string */
54 /* internal list head */
55 static struct list_head sn_hp_list
;
57 /* hotplug_slot struct's private pointer */
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
;
64 char physical_path
[SN_SLOT_NAME_SIZE
];
67 struct pcibr_slot_enable_resp
{
69 char resp_l1_msg
[PCI_L1_QSIZE
+ 1];
72 struct pcibr_slot_disable_resp
{
74 char resp_l1_msg
[PCI_L1_QSIZE
+ 1];
78 PCI_REQ_SLOT_ELIGIBLE
,
82 static int enable_slot(struct hotplug_slot
*slot
);
83 static int disable_slot(struct hotplug_slot
*slot
);
84 static inline int get_power_status(struct hotplug_slot
*slot
, u8
*value
);
86 static struct hotplug_slot_ops sn_hotplug_slot_ops
= {
87 .enable_slot
= enable_slot
,
88 .disable_slot
= disable_slot
,
89 .get_power_status
= get_power_status
,
92 static DEFINE_MUTEX(sn_hotplug_mutex
);
94 static ssize_t
path_show(struct pci_slot
*pci_slot
, char *buf
)
97 struct slot
*slot
= pci_slot
->hotplug
->private;
102 retval
= sprintf (buf
, "%s\n", slot
->physical_path
);
106 static struct pci_slot_attribute sn_slot_path_attr
= __ATTR_RO(path
);
108 static int sn_pci_slot_valid(struct pci_bus
*pci_bus
, int device
)
110 struct pcibus_info
*pcibus_info
;
111 u16 busnum
, segment
, ioboard_type
;
113 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
115 /* Check to see if this is a valid slot on 'pci_bus' */
116 if (!(pcibus_info
->pbi_valid_devices
& (1 << device
)))
119 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;
123 /* Do not allow hotplug operations on base I/O cards */
124 if ((ioboard_type
== L1_BRICKTYPE_IX
||
125 ioboard_type
== L1_BRICKTYPE_IA
) &&
126 (segment
== 1 && busnum
== 0 && device
!= 1))
132 static int sn_pci_bus_valid(struct pci_bus
*pci_bus
)
134 struct pcibus_info
*pcibus_info
;
138 /* Don't register slots hanging off the TIOCA bus */
139 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
140 asic_type
= pcibus_info
->pbi_buscommon
.bs_asic_type
;
141 if (asic_type
== PCIIO_ASIC_TYPE_TIOCA
)
144 /* Only register slots in I/O Bricks that support hotplug */
145 ioboard_type
= sn_ioboard_to_pci_bus(pci_bus
);
146 switch (ioboard_type
) {
147 case L1_BRICKTYPE_IX
:
148 case L1_BRICKTYPE_PX
:
149 case L1_BRICKTYPE_IA
:
150 case L1_BRICKTYPE_PA
:
151 case L1_BOARDTYPE_PCIX3SLOT
:
162 static int sn_hp_slot_private_alloc(struct hotplug_slot
*bss_hotplug_slot
,
163 struct pci_bus
*pci_bus
, int device
,
166 struct pcibus_info
*pcibus_info
;
169 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
171 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
174 bss_hotplug_slot
->private = slot
;
176 slot
->device_num
= device
;
177 slot
->pci_bus
= pci_bus
;
178 sprintf(name
, "%04x:%02x:%02x",
179 pci_domain_nr(pci_bus
),
180 ((u16
)pcibus_info
->pbi_buscommon
.bs_persist_busnum
),
183 sn_generate_path(pci_bus
, slot
->physical_path
);
185 slot
->hotplug_slot
= bss_hotplug_slot
;
186 list_add(&slot
->hp_list
, &sn_hp_list
);
191 static struct hotplug_slot
* sn_hp_destroy(void)
194 struct pci_slot
*pci_slot
;
195 struct hotplug_slot
*bss_hotplug_slot
= NULL
;
197 list_for_each_entry(slot
, &sn_hp_list
, hp_list
) {
198 bss_hotplug_slot
= slot
->hotplug_slot
;
199 pci_slot
= bss_hotplug_slot
->pci_slot
;
200 list_del(&((struct slot
*)bss_hotplug_slot
->private)->
202 sysfs_remove_file(&pci_slot
->kobj
,
203 &sn_slot_path_attr
.attr
);
206 return bss_hotplug_slot
;
209 static void sn_bus_free_data(struct pci_dev
*dev
)
211 struct pci_bus
*subordinate_bus
;
212 struct pci_dev
*child
;
214 /* Recursively clean up sn_irq_info structs */
215 if (dev
->subordinate
) {
216 subordinate_bus
= dev
->subordinate
;
217 list_for_each_entry(child
, &subordinate_bus
->devices
, bus_list
)
218 sn_bus_free_data(child
);
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
226 sn_bus_store_sysdata(dev
);
227 sn_pci_unfixup_slot(dev
);
230 static int sn_slot_enable(struct hotplug_slot
*bss_hotplug_slot
,
231 int device_num
, char **ssdt
)
233 struct slot
*slot
= bss_hotplug_slot
->private;
234 struct pcibus_info
*pcibus_info
;
235 struct pcibr_slot_enable_resp resp
;
238 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
241 * Power-on and initialize the slot in the SN
242 * PCI infrastructure.
244 rc
= sal_pcibr_slot_enable(pcibus_info
, device_num
, &resp
, ssdt
);
247 if (rc
== PCI_SLOT_ALREADY_UP
) {
248 dev_dbg(&slot
->pci_bus
->self
->dev
, "is already active\n");
249 return 1; /* return 1 to user */
252 if (rc
== PCI_L1_ERR
) {
253 dev_dbg(&slot
->pci_bus
->self
->dev
,
254 "L1 failure %d with message: %s",
255 resp
.resp_sub_errno
, resp
.resp_l1_msg
);
260 dev_dbg(&slot
->pci_bus
->self
->dev
,
261 "insert failed with error %d sub-error %d\n",
262 rc
, resp
.resp_sub_errno
);
266 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
267 pcibus_info
->pbi_enabled_devices
|= (1 << device_num
);
272 static int sn_slot_disable(struct hotplug_slot
*bss_hotplug_slot
,
273 int device_num
, int action
)
275 struct slot
*slot
= bss_hotplug_slot
->private;
276 struct pcibus_info
*pcibus_info
;
277 struct pcibr_slot_disable_resp resp
;
280 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
282 rc
= sal_pcibr_slot_disable(pcibus_info
, device_num
, action
, &resp
);
284 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) &&
285 (rc
== PCI_SLOT_ALREADY_DOWN
)) {
286 dev_dbg(&slot
->pci_bus
->self
->dev
, "Slot %s already inactive\n", slot
->physical_path
);
287 return 1; /* return 1 to user */
290 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && (rc
== PCI_EMPTY_33MHZ
)) {
291 dev_dbg(&slot
->pci_bus
->self
->dev
,
292 "Cannot remove last 33MHz card\n");
296 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && (rc
== PCI_L1_ERR
)) {
297 dev_dbg(&slot
->pci_bus
->self
->dev
,
298 "L1 failure %d with message \n%s\n",
299 resp
.resp_sub_errno
, resp
.resp_l1_msg
);
303 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && rc
) {
304 dev_dbg(&slot
->pci_bus
->self
->dev
,
305 "remove failed with error %d sub-error %d\n",
306 rc
, resp
.resp_sub_errno
);
310 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && !rc
)
313 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
);
316 dev_dbg(&slot
->pci_bus
->self
->dev
, "remove successful\n");
320 if ((action
== PCI_REQ_SLOT_DISABLE
) && rc
) {
321 dev_dbg(&slot
->pci_bus
->self
->dev
,"remove failed rc = %d\n", rc
);
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.
332 static int enable_slot(struct hotplug_slot
*bss_hotplug_slot
)
334 struct slot
*slot
= bss_hotplug_slot
->private;
335 struct pci_bus
*new_bus
= NULL
;
341 void pcibios_fixup_device_resources(struct pci_dev
*);
343 /* Serialize the Linux PCI infrastructure */
344 mutex_lock(&sn_hotplug_mutex
);
347 * Power-on and initialize the slot in the SN
348 * PCI infrastructure. Also, retrieve the ACPI SSDT
349 * table for the slot (if ACPI capable PROM).
351 rc
= sn_slot_enable(bss_hotplug_slot
, slot
->device_num
, &ssdt
);
353 mutex_unlock(&sn_hotplug_mutex
);
359 /* Add the new SSDT for the slot to the ACPI namespace */
360 if (SN_ACPI_BASE_SUPPORT() && ssdt
) {
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",
367 /* try to continue on */
371 num_funcs
= pci_scan_slot(slot
->pci_bus
,
372 PCI_DEVFN(slot
->device_num
+ 1, 0));
374 dev_dbg(&slot
->pci_bus
->self
->dev
, "no device in slot\n");
375 mutex_unlock(&sn_hotplug_mutex
);
380 * Map SN resources for all functions on the card
381 * to the Linux PCI interface and tell the drivers
384 list_for_each_entry(dev
, &slot
->pci_bus
->devices
, bus_list
) {
385 if (PCI_SLOT(dev
->devfn
) != slot
->device_num
+ 1)
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).
393 pcibios_fixup_device_resources(dev
);
394 if (SN_ACPI_BASE_SUPPORT())
395 sn_acpi_slot_fixup(dev
);
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
;
408 * Add the slot's devices to the ACPI infrastructure */
409 if (SN_ACPI_BASE_SUPPORT() && ssdt
) {
410 unsigned long long adr
;
411 struct acpi_device
*pdevice
;
413 acpi_handle chandle
= NULL
;
414 acpi_handle rethandle
;
417 phandle
= acpi_device_handle(PCI_CONTROLLER(slot
->pci_bus
)->companion
);
419 if (acpi_bus_get_device(phandle
, &pdevice
)) {
420 dev_dbg(&slot
->pci_bus
->self
->dev
,
421 "no parent device, assuming NULL\n");
425 acpi_scan_lock_acquire();
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.
433 ret
= acpi_get_next_object(ACPI_TYPE_DEVICE
,
437 if (ret
== AE_NOT_FOUND
|| rethandle
== NULL
)
442 ret
= acpi_evaluate_integer(chandle
, METHOD_NAME__ADR
,
445 if (ACPI_SUCCESS(ret
) &&
446 (adr
>>16) == (slot
->device_num
+ 1)) {
448 ret
= acpi_bus_scan(chandle
);
449 if (ACPI_FAILURE(ret
)) {
450 printk(KERN_ERR
"%s: acpi_bus_scan "
451 "failed (0x%x) for slot %d "
452 "func %d\n", __func__
,
455 /* try to continue on */
459 acpi_scan_lock_release();
462 pci_lock_rescan_remove();
464 /* 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 */
468 pci_bus_add_devices(new_bus
);
470 pci_unlock_rescan_remove();
471 mutex_unlock(&sn_hotplug_mutex
);
474 dev_dbg(&slot
->pci_bus
->self
->dev
,
475 "insert operation successful\n");
477 dev_dbg(&slot
->pci_bus
->self
->dev
,
478 "insert operation failed rc = %d\n", rc
);
483 static int disable_slot(struct hotplug_slot
*bss_hotplug_slot
)
485 struct slot
*slot
= bss_hotplug_slot
->private;
486 struct pci_dev
*dev
, *temp
;
488 acpi_owner_id ssdt_id
= 0;
490 /* Acquire update access to the bus */
491 mutex_lock(&sn_hotplug_mutex
);
493 /* is it okay to bring this slot down? */
494 rc
= sn_slot_disable(bss_hotplug_slot
, slot
->device_num
,
495 PCI_REQ_SLOT_ELIGIBLE
);
499 /* free the ACPI resources for the slot */
500 if (SN_ACPI_BASE_SUPPORT() &&
501 PCI_CONTROLLER(slot
->pci_bus
)->companion
) {
502 unsigned long long adr
;
503 struct acpi_device
*device
;
505 acpi_handle chandle
= NULL
;
506 acpi_handle rethandle
;
509 /* Get the rootbus node pointer */
510 phandle
= acpi_device_handle(PCI_CONTROLLER(slot
->pci_bus
)->companion
);
512 acpi_scan_lock_acquire();
514 * Walk the rootbus node's immediate children looking for
515 * the slot's device node(s). There can be more than
516 * one for multifunction devices.
520 ret
= acpi_get_next_object(ACPI_TYPE_DEVICE
,
524 if (ret
== AE_NOT_FOUND
|| rethandle
== NULL
)
529 ret
= acpi_evaluate_integer(chandle
,
532 if (ACPI_SUCCESS(ret
) &&
533 (adr
>>16) == (slot
->device_num
+ 1)) {
534 /* retain the owner id */
535 acpi_get_id(chandle
, &ssdt_id
);
537 ret
= acpi_bus_get_device(chandle
,
539 if (ACPI_SUCCESS(ret
))
540 acpi_bus_trim(device
);
543 acpi_scan_lock_release();
546 pci_lock_rescan_remove();
547 /* Free the SN resources assigned to the Linux device.*/
548 list_for_each_entry_safe(dev
, temp
, &slot
->pci_bus
->devices
, bus_list
) {
549 if (PCI_SLOT(dev
->devfn
) != slot
->device_num
+ 1)
553 sn_bus_free_data(dev
);
554 pci_stop_and_remove_bus_device(dev
);
557 pci_unlock_rescan_remove();
559 /* Remove the SSDT for the slot from the ACPI namespace */
560 if (SN_ACPI_BASE_SUPPORT() && ssdt_id
) {
562 ret
= acpi_unload_table_id(ssdt_id
);
563 if (ACPI_FAILURE(ret
)) {
564 printk(KERN_ERR
"%s: acpi_unload_table_id "
565 "failed (0x%x) for id %d\n",
566 __func__
, ret
, ssdt_id
);
567 /* try to continue on */
571 /* free the collected sysdata pointers */
572 sn_bus_free_sysdata();
574 /* Deactivate slot */
575 rc
= sn_slot_disable(bss_hotplug_slot
, slot
->device_num
,
576 PCI_REQ_SLOT_DISABLE
);
578 /* Release the bus lock */
579 mutex_unlock(&sn_hotplug_mutex
);
584 static inline int get_power_status(struct hotplug_slot
*bss_hotplug_slot
,
587 struct slot
*slot
= bss_hotplug_slot
->private;
588 struct pcibus_info
*pcibus_info
;
591 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
592 mutex_lock(&sn_hotplug_mutex
);
593 power
= pcibus_info
->pbi_enabled_devices
& (1 << slot
->device_num
);
594 *value
= power
? 1 : 0;
595 mutex_unlock(&sn_hotplug_mutex
);
599 static void sn_release_slot(struct hotplug_slot
*bss_hotplug_slot
)
601 kfree(bss_hotplug_slot
->info
);
602 kfree(bss_hotplug_slot
->private);
603 kfree(bss_hotplug_slot
);
606 static int sn_hotplug_slot_register(struct pci_bus
*pci_bus
)
609 struct pci_slot
*pci_slot
;
610 struct hotplug_slot
*bss_hotplug_slot
;
611 char name
[SN_SLOT_NAME_SIZE
];
615 * Currently only four devices are supported,
616 * in the future there maybe more -- up to 32.
619 for (device
= 0; device
< SN_MAX_HP_SLOTS
; device
++) {
620 if (sn_pci_slot_valid(pci_bus
, device
) != 1)
623 bss_hotplug_slot
= kzalloc(sizeof(*bss_hotplug_slot
),
625 if (!bss_hotplug_slot
) {
630 bss_hotplug_slot
->info
=
631 kzalloc(sizeof(struct hotplug_slot_info
),
633 if (!bss_hotplug_slot
->info
) {
638 if (sn_hp_slot_private_alloc(bss_hotplug_slot
,
639 pci_bus
, device
, name
)) {
643 bss_hotplug_slot
->ops
= &sn_hotplug_slot_ops
;
644 bss_hotplug_slot
->release
= &sn_release_slot
;
646 rc
= pci_hp_register(bss_hotplug_slot
, pci_bus
, device
, name
);
650 pci_slot
= bss_hotplug_slot
->pci_slot
;
651 rc
= sysfs_create_file(&pci_slot
->kobj
,
652 &sn_slot_path_attr
.attr
);
656 dev_dbg(&pci_bus
->self
->dev
, "Registered bus with hotplug\n");
660 dev_dbg(&pci_bus
->self
->dev
, "bus failed to register with err = %d\n",
665 dev_dbg(&pci_bus
->self
->dev
, "Memory allocation error\n");
667 /* destroy THIS element */
668 if (bss_hotplug_slot
)
669 sn_release_slot(bss_hotplug_slot
);
671 /* destroy anything else on the list */
672 while ((bss_hotplug_slot
= sn_hp_destroy()))
673 pci_hp_deregister(bss_hotplug_slot
);
678 static int __init
sn_pci_hotplug_init(void)
680 struct pci_bus
*pci_bus
= NULL
;
684 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT
)) {
685 printk(KERN_ERR
"%s: PROM version does not support hotplug.\n",
690 INIT_LIST_HEAD(&sn_hp_list
);
692 while ((pci_bus
= pci_find_next_bus(pci_bus
))) {
693 if (!pci_bus
->sysdata
)
696 rc
= sn_pci_bus_valid(pci_bus
);
698 dev_dbg(&pci_bus
->self
->dev
, "not a valid hotplug bus\n");
701 dev_dbg(&pci_bus
->self
->dev
, "valid hotplug bus\n");
703 rc
= sn_hotplug_slot_register(pci_bus
);
712 return registered
== 1 ? 0 : -ENODEV
;
715 static void __exit
sn_pci_hotplug_exit(void)
717 struct hotplug_slot
*bss_hotplug_slot
;
719 while ((bss_hotplug_slot
= sn_hp_destroy()))
720 pci_hp_deregister(bss_hotplug_slot
);
722 if (!list_empty(&sn_hp_list
))
723 printk(KERN_ERR
"%s: internal list is not empty\n", __FILE__
);
726 module_init(sn_pci_hotplug_init
);
727 module_exit(sn_pci_hotplug_exit
);