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