Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 1 | /* |
| 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 Keller | 8e77af6 | 2006-03-08 13:21:34 -0600 | [diff] [blame] | 6 | * Copyright (C) 2005-2006 Silicon Graphics, Inc. All rights reserved. |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 7 | * |
| 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> |
| 16 | #include <linux/proc_fs.h> |
| 17 | #include <linux/types.h> |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 18 | #include <linux/mutex.h> |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/sn/addrs.h> |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 21 | #include <asm/sn/geo.h> |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 22 | #include <asm/sn/l1.h> |
| 23 | #include <asm/sn/module.h> |
| 24 | #include <asm/sn/pcibr_provider.h> |
| 25 | #include <asm/sn/pcibus_provider_defs.h> |
| 26 | #include <asm/sn/pcidev.h> |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 27 | #include <asm/sn/sn_feature_sets.h> |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 28 | #include <asm/sn/sn_sal.h> |
| 29 | #include <asm/sn/types.h> |
| 30 | |
| 31 | #include "../pci.h" |
| 32 | #include "pci_hotplug.h" |
| 33 | |
| 34 | MODULE_LICENSE("GPL"); |
| 35 | MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)"); |
| 36 | MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver"); |
| 37 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 38 | #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 Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 47 | |
| 48 | /* internal list head */ |
| 49 | static struct list_head sn_hp_list; |
| 50 | |
| 51 | /* hotplug_slot struct's private pointer */ |
| 52 | struct 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 Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 58 | char physical_path[SN_SLOT_NAME_SIZE]; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 59 | }; |
| 60 | |
| 61 | struct pcibr_slot_enable_resp { |
| 62 | int resp_sub_errno; |
| 63 | char resp_l1_msg[PCI_L1_QSIZE + 1]; |
| 64 | }; |
| 65 | |
| 66 | struct pcibr_slot_disable_resp { |
| 67 | int resp_sub_errno; |
| 68 | char resp_l1_msg[PCI_L1_QSIZE + 1]; |
| 69 | }; |
| 70 | |
| 71 | enum sn_pci_req_e { |
| 72 | PCI_REQ_SLOT_ELIGIBLE, |
| 73 | PCI_REQ_SLOT_DISABLE |
| 74 | }; |
| 75 | |
| 76 | static int enable_slot(struct hotplug_slot *slot); |
| 77 | static int disable_slot(struct hotplug_slot *slot); |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 78 | static inline int get_power_status(struct hotplug_slot *slot, u8 *value); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 79 | |
| 80 | static 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 Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 87 | static DEFINE_MUTEX(sn_hotplug_mutex); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 88 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 89 | static 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 | |
| 102 | static struct hotplug_slot_attribute sn_slot_path_attr = __ATTR_RO(path); |
| 103 | |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 104 | static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device) |
| 105 | { |
| 106 | struct pcibus_info *pcibus_info; |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 107 | u16 busnum, segment, ioboard_type; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 108 | |
| 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 Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 115 | 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 Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 118 | |
| 119 | /* Do not allow hotplug operations on base I/O cards */ |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 120 | if ((ioboard_type == L1_BRICKTYPE_IX || |
| 121 | ioboard_type == L1_BRICKTYPE_IA) && |
| 122 | (segment == 1 && busnum == 0 && device != 1)) |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 123 | return -EPERM; |
| 124 | |
| 125 | return 1; |
| 126 | } |
| 127 | |
| 128 | static int sn_pci_bus_valid(struct pci_bus *pci_bus) |
| 129 | { |
| 130 | struct pcibus_info *pcibus_info; |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 131 | u32 asic_type; |
| 132 | u16 ioboard_type; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 133 | |
| 134 | /* Don't register slots hanging off the TIOCA bus */ |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 135 | pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 136 | 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 Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 141 | ioboard_type = sn_ioboard_to_pci_bus(pci_bus); |
| 142 | switch (ioboard_type) { |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 143 | case L1_BRICKTYPE_IX: |
| 144 | case L1_BRICKTYPE_PX: |
| 145 | case L1_BRICKTYPE_IA: |
| 146 | case L1_BRICKTYPE_PA: |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 147 | case L1_BOARDTYPE_PCIX3SLOT: |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 148 | return 1; |
| 149 | break; |
| 150 | default: |
| 151 | return -EPERM; |
| 152 | break; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | return -EIO; |
| 156 | } |
| 157 | |
| 158 | static 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 Enberg | 656da9d | 2005-09-22 00:48:11 -0700 | [diff] [blame] | 166 | slot = kzalloc(sizeof(*slot), GFP_KERNEL); |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 167 | if (!slot) |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 168 | return -ENOMEM; |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 169 | bss_hotplug_slot->private = slot; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 170 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 171 | bss_hotplug_slot->name = kmalloc(SN_SLOT_NAME_SIZE, GFP_KERNEL); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 172 | 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 Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 179 | sprintf(bss_hotplug_slot->name, "%04x:%02x:%02x", |
| 180 | pci_domain_nr(pci_bus), |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 181 | ((u16)pcibus_info->pbi_buscommon.bs_persist_busnum), |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 182 | device + 1); |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 183 | |
| 184 | sn_generate_path(pci_bus, slot->physical_path); |
| 185 | |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 186 | slot->hotplug_slot = bss_hotplug_slot; |
| 187 | list_add(&slot->hp_list, &sn_hp_list); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
| 192 | static struct hotplug_slot * sn_hp_destroy(void) |
| 193 | { |
| 194 | struct slot *slot; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 195 | struct hotplug_slot *bss_hotplug_slot = NULL; |
| 196 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 197 | list_for_each_entry(slot, &sn_hp_list, hp_list) { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 198 | bss_hotplug_slot = slot->hotplug_slot; |
| 199 | list_del(&((struct slot *)bss_hotplug_slot->private)-> |
| 200 | hp_list); |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 201 | sysfs_remove_file(&bss_hotplug_slot->kobj, |
| 202 | &sn_slot_path_attr.attr); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 203 | break; |
| 204 | } |
| 205 | return bss_hotplug_slot; |
| 206 | } |
| 207 | |
| 208 | static void sn_bus_alloc_data(struct pci_dev *dev) |
| 209 | { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 210 | struct pci_bus *subordinate_bus; |
| 211 | struct pci_dev *child; |
| 212 | |
| 213 | sn_pci_fixup_slot(dev); |
| 214 | |
| 215 | /* Recursively sets up the sn_irq_info structs */ |
| 216 | if (dev->subordinate) { |
| 217 | subordinate_bus = dev->subordinate; |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 218 | list_for_each_entry(child, &subordinate_bus->devices, bus_list) |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 219 | sn_bus_alloc_data(child); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 220 | } |
| 221 | } |
| 222 | |
| 223 | static void sn_bus_free_data(struct pci_dev *dev) |
| 224 | { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 225 | struct pci_bus *subordinate_bus; |
| 226 | struct pci_dev *child; |
| 227 | |
| 228 | /* Recursively clean up sn_irq_info structs */ |
| 229 | if (dev->subordinate) { |
| 230 | subordinate_bus = dev->subordinate; |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 231 | list_for_each_entry(child, &subordinate_bus->devices, bus_list) |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 232 | sn_bus_free_data(child); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 233 | } |
John Keller | 8e77af6 | 2006-03-08 13:21:34 -0600 | [diff] [blame] | 234 | /* |
| 235 | * Some drivers may use dma accesses during the |
| 236 | * driver remove function. We release the sysdata |
| 237 | * areas after the driver remove functions have |
| 238 | * been called. |
| 239 | */ |
| 240 | sn_bus_store_sysdata(dev); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 241 | sn_pci_unfixup_slot(dev); |
| 242 | } |
| 243 | |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 244 | static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot, |
| 245 | int device_num) |
| 246 | { |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 247 | struct slot *slot = bss_hotplug_slot->private; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 248 | struct pcibus_info *pcibus_info; |
| 249 | struct pcibr_slot_enable_resp resp; |
| 250 | int rc; |
| 251 | |
| 252 | pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus); |
| 253 | |
| 254 | /* |
| 255 | * Power-on and initialize the slot in the SN |
| 256 | * PCI infrastructure. |
| 257 | */ |
| 258 | rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp); |
| 259 | |
| 260 | if (rc == PCI_SLOT_ALREADY_UP) { |
| 261 | dev_dbg(slot->pci_bus->self, "is already active\n"); |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 262 | return 1; /* return 1 to user */ |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | if (rc == PCI_L1_ERR) { |
| 266 | dev_dbg(slot->pci_bus->self, |
| 267 | "L1 failure %d with message: %s", |
| 268 | resp.resp_sub_errno, resp.resp_l1_msg); |
| 269 | return -EPERM; |
| 270 | } |
| 271 | |
| 272 | if (rc) { |
| 273 | dev_dbg(slot->pci_bus->self, |
| 274 | "insert failed with error %d sub-error %d\n", |
| 275 | rc, resp.resp_sub_errno); |
| 276 | return -EIO; |
| 277 | } |
| 278 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 279 | pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus); |
| 280 | pcibus_info->pbi_enabled_devices |= (1 << device_num); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 281 | |
| 282 | return 0; |
| 283 | } |
| 284 | |
| 285 | static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot, |
| 286 | int device_num, int action) |
| 287 | { |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 288 | struct slot *slot = bss_hotplug_slot->private; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 289 | struct pcibus_info *pcibus_info; |
| 290 | struct pcibr_slot_disable_resp resp; |
| 291 | int rc; |
| 292 | |
| 293 | pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus); |
| 294 | |
| 295 | rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp); |
| 296 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 297 | if ((action == PCI_REQ_SLOT_ELIGIBLE) && |
| 298 | (rc == PCI_SLOT_ALREADY_DOWN)) { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 299 | dev_dbg(slot->pci_bus->self, "Slot %s already inactive\n"); |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 300 | return 1; /* return 1 to user */ |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 301 | } |
| 302 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 303 | if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 304 | dev_dbg(slot->pci_bus->self, |
| 305 | "Cannot remove last 33MHz card\n"); |
| 306 | return -EPERM; |
| 307 | } |
| 308 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 309 | if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 310 | dev_dbg(slot->pci_bus->self, |
| 311 | "L1 failure %d with message \n%s\n", |
| 312 | resp.resp_sub_errno, resp.resp_l1_msg); |
| 313 | return -EPERM; |
| 314 | } |
| 315 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 316 | if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 317 | dev_dbg(slot->pci_bus->self, |
| 318 | "remove failed with error %d sub-error %d\n", |
| 319 | rc, resp.resp_sub_errno); |
| 320 | return -EIO; |
| 321 | } |
| 322 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 323 | if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc) |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 324 | return 0; |
| 325 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 326 | if ((action == PCI_REQ_SLOT_DISABLE) && !rc) { |
| 327 | pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus); |
| 328 | pcibus_info->pbi_enabled_devices &= ~(1 << device_num); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 329 | dev_dbg(slot->pci_bus->self, "remove successful\n"); |
| 330 | return 0; |
| 331 | } |
| 332 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 333 | if ((action == PCI_REQ_SLOT_DISABLE) && rc) { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 334 | dev_dbg(slot->pci_bus->self,"remove failed rc = %d\n", rc); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | return rc; |
| 338 | } |
| 339 | |
| 340 | static int enable_slot(struct hotplug_slot *bss_hotplug_slot) |
| 341 | { |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 342 | struct slot *slot = bss_hotplug_slot->private; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 343 | struct pci_bus *new_bus = NULL; |
| 344 | struct pci_dev *dev; |
| 345 | int func, num_funcs; |
| 346 | int new_ppb = 0; |
| 347 | int rc; |
| 348 | |
| 349 | /* Serialize the Linux PCI infrastructure */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 350 | mutex_lock(&sn_hotplug_mutex); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 351 | |
| 352 | /* |
| 353 | * Power-on and initialize the slot in the SN |
| 354 | * PCI infrastructure. |
| 355 | */ |
| 356 | rc = sn_slot_enable(bss_hotplug_slot, slot->device_num); |
| 357 | if (rc) { |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 358 | mutex_unlock(&sn_hotplug_mutex); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 359 | return rc; |
| 360 | } |
| 361 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 362 | num_funcs = pci_scan_slot(slot->pci_bus, |
| 363 | PCI_DEVFN(slot->device_num + 1, 0)); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 364 | if (!num_funcs) { |
| 365 | dev_dbg(slot->pci_bus->self, "no device in slot\n"); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 366 | mutex_unlock(&sn_hotplug_mutex); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 367 | return -ENODEV; |
| 368 | } |
| 369 | |
| 370 | sn_pci_controller_fixup(pci_domain_nr(slot->pci_bus), |
| 371 | slot->pci_bus->number, |
| 372 | slot->pci_bus); |
| 373 | /* |
| 374 | * Map SN resources for all functions on the card |
| 375 | * to the Linux PCI interface and tell the drivers |
| 376 | * about them. |
| 377 | */ |
| 378 | for (func = 0; func < num_funcs; func++) { |
| 379 | dev = pci_get_slot(slot->pci_bus, |
| 380 | PCI_DEVFN(slot->device_num + 1, |
| 381 | PCI_FUNC(func))); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 382 | if (dev) { |
| 383 | if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) { |
| 384 | unsigned char sec_bus; |
| 385 | pci_read_config_byte(dev, PCI_SECONDARY_BUS, |
| 386 | &sec_bus); |
| 387 | new_bus = pci_add_new_bus(dev->bus, dev, |
| 388 | sec_bus); |
| 389 | pci_scan_child_bus(new_bus); |
| 390 | sn_pci_controller_fixup(pci_domain_nr(new_bus), |
| 391 | new_bus->number, |
| 392 | new_bus); |
| 393 | new_ppb = 1; |
| 394 | } |
| 395 | sn_bus_alloc_data(dev); |
| 396 | pci_dev_put(dev); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* Call the driver for the new device */ |
| 401 | pci_bus_add_devices(slot->pci_bus); |
| 402 | /* Call the drivers for the new devices subordinate to PPB */ |
| 403 | if (new_ppb) |
| 404 | pci_bus_add_devices(new_bus); |
| 405 | |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 406 | mutex_unlock(&sn_hotplug_mutex); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 407 | |
| 408 | if (rc == 0) |
| 409 | dev_dbg(slot->pci_bus->self, |
| 410 | "insert operation successful\n"); |
| 411 | else |
| 412 | dev_dbg(slot->pci_bus->self, |
| 413 | "insert operation failed rc = %d\n", rc); |
| 414 | |
| 415 | return rc; |
| 416 | } |
| 417 | |
| 418 | static int disable_slot(struct hotplug_slot *bss_hotplug_slot) |
| 419 | { |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 420 | struct slot *slot = bss_hotplug_slot->private; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 421 | struct pci_dev *dev; |
| 422 | int func; |
| 423 | int rc; |
| 424 | |
| 425 | /* Acquire update access to the bus */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 426 | mutex_lock(&sn_hotplug_mutex); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 427 | |
| 428 | /* is it okay to bring this slot down? */ |
| 429 | rc = sn_slot_disable(bss_hotplug_slot, slot->device_num, |
| 430 | PCI_REQ_SLOT_ELIGIBLE); |
| 431 | if (rc) |
| 432 | goto leaving; |
| 433 | |
| 434 | /* Free the SN resources assigned to the Linux device.*/ |
| 435 | for (func = 0; func < 8; func++) { |
| 436 | dev = pci_get_slot(slot->pci_bus, |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 437 | PCI_DEVFN(slot->device_num + 1, |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 438 | PCI_FUNC(func))); |
| 439 | if (dev) { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 440 | sn_bus_free_data(dev); |
| 441 | pci_remove_bus_device(dev); |
| 442 | pci_dev_put(dev); |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | /* free the collected sysdata pointers */ |
| 447 | sn_bus_free_sysdata(); |
| 448 | |
| 449 | /* Deactivate slot */ |
| 450 | rc = sn_slot_disable(bss_hotplug_slot, slot->device_num, |
| 451 | PCI_REQ_SLOT_DISABLE); |
| 452 | leaving: |
| 453 | /* Release the bus lock */ |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 454 | mutex_unlock(&sn_hotplug_mutex); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 455 | |
| 456 | return rc; |
| 457 | } |
| 458 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 459 | static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot, |
| 460 | u8 *value) |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 461 | { |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 462 | struct slot *slot = bss_hotplug_slot->private; |
| 463 | struct pcibus_info *pcibus_info; |
| 464 | |
| 465 | pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 466 | mutex_lock(&sn_hotplug_mutex); |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 467 | *value = pcibus_info->pbi_enabled_devices & (1 << slot->device_num); |
Ingo Molnar | 14cc3e2 | 2006-03-26 01:37:14 -0800 | [diff] [blame] | 468 | mutex_unlock(&sn_hotplug_mutex); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot) |
| 473 | { |
| 474 | kfree(bss_hotplug_slot->info); |
| 475 | kfree(bss_hotplug_slot->name); |
| 476 | kfree(bss_hotplug_slot->private); |
| 477 | kfree(bss_hotplug_slot); |
| 478 | } |
| 479 | |
| 480 | static int sn_hotplug_slot_register(struct pci_bus *pci_bus) |
| 481 | { |
| 482 | int device; |
| 483 | struct hotplug_slot *bss_hotplug_slot; |
| 484 | int rc = 0; |
| 485 | |
| 486 | /* |
| 487 | * Currently only four devices are supported, |
| 488 | * in the future there maybe more -- up to 32. |
| 489 | */ |
| 490 | |
| 491 | for (device = 0; device < SN_MAX_HP_SLOTS ; device++) { |
| 492 | if (sn_pci_slot_valid(pci_bus, device) != 1) |
| 493 | continue; |
| 494 | |
Pekka Enberg | 656da9d | 2005-09-22 00:48:11 -0700 | [diff] [blame] | 495 | bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot), |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 496 | GFP_KERNEL); |
| 497 | if (!bss_hotplug_slot) { |
| 498 | rc = -ENOMEM; |
| 499 | goto alloc_err; |
| 500 | } |
| 501 | |
| 502 | bss_hotplug_slot->info = |
Pekka Enberg | 656da9d | 2005-09-22 00:48:11 -0700 | [diff] [blame] | 503 | kzalloc(sizeof(struct hotplug_slot_info), |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 504 | GFP_KERNEL); |
| 505 | if (!bss_hotplug_slot->info) { |
| 506 | rc = -ENOMEM; |
| 507 | goto alloc_err; |
| 508 | } |
| 509 | |
| 510 | if (sn_hp_slot_private_alloc(bss_hotplug_slot, |
| 511 | pci_bus, device)) { |
| 512 | rc = -ENOMEM; |
| 513 | goto alloc_err; |
| 514 | } |
| 515 | |
| 516 | bss_hotplug_slot->ops = &sn_hotplug_slot_ops; |
| 517 | bss_hotplug_slot->release = &sn_release_slot; |
| 518 | |
| 519 | rc = pci_hp_register(bss_hotplug_slot); |
| 520 | if (rc) |
| 521 | goto register_err; |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 522 | |
| 523 | rc = sysfs_create_file(&bss_hotplug_slot->kobj, |
| 524 | &sn_slot_path_attr.attr); |
| 525 | if (rc) |
| 526 | goto register_err; |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 527 | } |
| 528 | dev_dbg(pci_bus->self, "Registered bus with hotplug\n"); |
| 529 | return rc; |
| 530 | |
| 531 | register_err: |
| 532 | dev_dbg(pci_bus->self, "bus failed to register with err = %d\n", |
| 533 | rc); |
| 534 | |
| 535 | alloc_err: |
| 536 | if (rc == -ENOMEM) |
| 537 | dev_dbg(pci_bus->self, "Memory allocation error\n"); |
| 538 | |
| 539 | /* destroy THIS element */ |
| 540 | if (bss_hotplug_slot) |
| 541 | sn_release_slot(bss_hotplug_slot); |
| 542 | |
| 543 | /* destroy anything else on the list */ |
| 544 | while ((bss_hotplug_slot = sn_hp_destroy())) |
| 545 | pci_hp_deregister(bss_hotplug_slot); |
| 546 | |
| 547 | return rc; |
| 548 | } |
| 549 | |
| 550 | static int sn_pci_hotplug_init(void) |
| 551 | { |
| 552 | struct pci_bus *pci_bus = NULL; |
| 553 | int rc; |
| 554 | int registered = 0; |
| 555 | |
Prarit Bhargava | e55dea5 | 2006-04-04 09:26:46 -0400 | [diff] [blame^] | 556 | if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) { |
| 557 | printk(KERN_ERR "%s: PROM version does not support hotplug.\n", |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 558 | __FUNCTION__); |
| 559 | return -EPERM; |
| 560 | } |
| 561 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 562 | INIT_LIST_HEAD(&sn_hp_list); |
| 563 | |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 564 | while ((pci_bus = pci_find_next_bus(pci_bus))) { |
| 565 | if (!pci_bus->sysdata) |
| 566 | continue; |
| 567 | |
| 568 | rc = sn_pci_bus_valid(pci_bus); |
| 569 | if (rc != 1) { |
| 570 | dev_dbg(pci_bus->self, "not a valid hotplug bus\n"); |
| 571 | continue; |
| 572 | } |
| 573 | dev_dbg(pci_bus->self, "valid hotplug bus\n"); |
| 574 | |
| 575 | rc = sn_hotplug_slot_register(pci_bus); |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 576 | if (!rc) { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 577 | registered = 1; |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 578 | } else { |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 579 | registered = 0; |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | return registered == 1 ? 0 : -ENODEV; |
| 585 | } |
| 586 | |
| 587 | static void sn_pci_hotplug_exit(void) |
| 588 | { |
| 589 | struct hotplug_slot *bss_hotplug_slot; |
| 590 | |
Prarit Bhargava | 1d2450a | 2005-08-12 10:13:34 -0400 | [diff] [blame] | 591 | while ((bss_hotplug_slot = sn_hp_destroy())) |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 592 | pci_hp_deregister(bss_hotplug_slot); |
Prarit Bhargava | 6f354b0 | 2005-07-06 15:29:53 -0700 | [diff] [blame] | 593 | |
| 594 | if (!list_empty(&sn_hp_list)) |
| 595 | printk(KERN_ERR "%s: internal list is not empty\n", __FILE__); |
| 596 | } |
| 597 | |
| 598 | module_init(sn_pci_hotplug_init); |
| 599 | module_exit(sn_pci_hotplug_exit); |