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/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 #include <linux/mutex.h>
20 #include <asm/sn/addrs.h>
21 #include <asm/sn/l1.h>
22 #include <asm/sn/module.h>
23 #include <asm/sn/pcibr_provider.h>
24 #include <asm/sn/pcibus_provider_defs.h>
25 #include <asm/sn/pcidev.h>
26 #include <asm/sn/sn_sal.h>
27 #include <asm/sn/types.h>
30 #include "pci_hotplug.h"
32 MODULE_LICENSE("GPL");
33 MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
34 MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
36 #define PCIIO_ASIC_TYPE_TIOCA 4
37 #define PCI_SLOT_ALREADY_UP 2 /* slot already up */
38 #define PCI_SLOT_ALREADY_DOWN 3 /* slot already down */
39 #define PCI_L1_ERR 7 /* L1 console command error */
40 #define PCI_EMPTY_33MHZ 15 /* empty 33 MHz bus */
41 #define PCI_L1_QSIZE 128 /* our L1 message buffer size */
42 #define SN_MAX_HP_SLOTS 32 /* max hotplug slots */
43 #define SGI_HOTPLUG_PROM_REV 0x0430 /* Min. required PROM version */
44 #define SN_SLOT_NAME_SIZE 33 /* size of name string */
46 /* internal list head */
47 static struct list_head sn_hp_list
;
49 /* hotplug_slot struct's private pointer */
52 struct pci_bus
*pci_bus
;
53 /* this struct for glue internal only */
54 struct hotplug_slot
*hotplug_slot
;
55 struct list_head hp_list
;
56 char physical_path
[SN_SLOT_NAME_SIZE
];
59 struct pcibr_slot_enable_resp
{
61 char resp_l1_msg
[PCI_L1_QSIZE
+ 1];
64 struct pcibr_slot_disable_resp
{
66 char resp_l1_msg
[PCI_L1_QSIZE
+ 1];
70 PCI_REQ_SLOT_ELIGIBLE
,
74 static int enable_slot(struct hotplug_slot
*slot
);
75 static int disable_slot(struct hotplug_slot
*slot
);
76 static inline int get_power_status(struct hotplug_slot
*slot
, u8
*value
);
78 static struct hotplug_slot_ops sn_hotplug_slot_ops
= {
80 .enable_slot
= enable_slot
,
81 .disable_slot
= disable_slot
,
82 .get_power_status
= get_power_status
,
85 static DEFINE_MUTEX(sn_hotplug_mutex
);
87 static ssize_t
path_show (struct hotplug_slot
*bss_hotplug_slot
,
91 struct slot
*slot
= bss_hotplug_slot
->private;
96 retval
= sprintf (buf
, "%s\n", slot
->physical_path
);
100 static struct hotplug_slot_attribute sn_slot_path_attr
= __ATTR_RO(path
);
102 static int sn_pci_slot_valid(struct pci_bus
*pci_bus
, int device
)
104 struct pcibus_info
*pcibus_info
;
108 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
110 /* Check to see if this is a valid slot on 'pci_bus' */
111 if (!(pcibus_info
->pbi_valid_devices
& (1 << device
)))
114 bricktype
= MODULE_GET_BTYPE(pcibus_info
->pbi_moduleid
);
115 bus_num
= pcibus_info
->pbi_buscommon
.bs_persist_busnum
& 0xf;
117 /* Do not allow hotplug operations on base I/O cards */
118 if ((bricktype
== L1_BRICKTYPE_IX
|| bricktype
== L1_BRICKTYPE_IA
) &&
119 (bus_num
== 1 && device
!= 1))
125 static int sn_pci_bus_valid(struct pci_bus
*pci_bus
)
127 struct pcibus_info
*pcibus_info
;
131 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
133 /* Don't register slots hanging off the TIOCA bus */
134 asic_type
= pcibus_info
->pbi_buscommon
.bs_asic_type
;
135 if (asic_type
== PCIIO_ASIC_TYPE_TIOCA
)
138 /* Only register slots in I/O Bricks that support hotplug */
139 bricktype
= MODULE_GET_BTYPE(pcibus_info
->pbi_moduleid
);
141 case L1_BRICKTYPE_IX
:
142 case L1_BRICKTYPE_PX
:
143 case L1_BRICKTYPE_IA
:
144 case L1_BRICKTYPE_PA
:
155 static int sn_hp_slot_private_alloc(struct hotplug_slot
*bss_hotplug_slot
,
156 struct pci_bus
*pci_bus
, int device
)
158 struct pcibus_info
*pcibus_info
;
161 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
163 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
166 bss_hotplug_slot
->private = slot
;
168 bss_hotplug_slot
->name
= kmalloc(SN_SLOT_NAME_SIZE
, GFP_KERNEL
);
169 if (!bss_hotplug_slot
->name
) {
170 kfree(bss_hotplug_slot
->private);
174 slot
->device_num
= device
;
175 slot
->pci_bus
= pci_bus
;
176 sprintf(bss_hotplug_slot
->name
, "%04x:%02x:%02x",
177 pci_domain_nr(pci_bus
),
178 ((int)pcibus_info
->pbi_buscommon
.bs_persist_busnum
) & 0xf,
180 sprintf(slot
->physical_path
, "module_%c%c%c%c%.2d",
181 '0'+RACK_GET_CLASS(MODULE_GET_RACK(pcibus_info
->pbi_moduleid
)),
182 '0'+RACK_GET_GROUP(MODULE_GET_RACK(pcibus_info
->pbi_moduleid
)),
183 '0'+RACK_GET_NUM(MODULE_GET_RACK(pcibus_info
->pbi_moduleid
)),
184 MODULE_GET_BTCHAR(pcibus_info
->pbi_moduleid
),
185 MODULE_GET_BPOS(pcibus_info
->pbi_moduleid
));
186 slot
->hotplug_slot
= bss_hotplug_slot
;
187 list_add(&slot
->hp_list
, &sn_hp_list
);
192 static struct hotplug_slot
* sn_hp_destroy(void)
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 list_del(&((struct slot
*)bss_hotplug_slot
->private)->
201 sysfs_remove_file(&bss_hotplug_slot
->kobj
,
202 &sn_slot_path_attr
.attr
);
205 return bss_hotplug_slot
;
208 static void sn_bus_alloc_data(struct pci_dev
*dev
)
210 struct pci_bus
*subordinate_bus
;
211 struct pci_dev
*child
;
213 sn_pci_fixup_slot(dev
);
215 /* Recursively sets up the sn_irq_info structs */
216 if (dev
->subordinate
) {
217 subordinate_bus
= dev
->subordinate
;
218 list_for_each_entry(child
, &subordinate_bus
->devices
, bus_list
)
219 sn_bus_alloc_data(child
);
223 static void sn_bus_free_data(struct pci_dev
*dev
)
225 struct pci_bus
*subordinate_bus
;
226 struct pci_dev
*child
;
228 /* Recursively clean up sn_irq_info structs */
229 if (dev
->subordinate
) {
230 subordinate_bus
= dev
->subordinate
;
231 list_for_each_entry(child
, &subordinate_bus
->devices
, bus_list
)
232 sn_bus_free_data(child
);
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
240 sn_bus_store_sysdata(dev
);
241 sn_pci_unfixup_slot(dev
);
244 static int sn_slot_enable(struct hotplug_slot
*bss_hotplug_slot
,
247 struct slot
*slot
= bss_hotplug_slot
->private;
248 struct pcibus_info
*pcibus_info
;
249 struct pcibr_slot_enable_resp resp
;
252 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
255 * Power-on and initialize the slot in the SN
256 * PCI infrastructure.
258 rc
= sal_pcibr_slot_enable(pcibus_info
, device_num
, &resp
);
260 if (rc
== PCI_SLOT_ALREADY_UP
) {
261 dev_dbg(slot
->pci_bus
->self
, "is already active\n");
262 return 1; /* return 1 to user */
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
);
273 dev_dbg(slot
->pci_bus
->self
,
274 "insert failed with error %d sub-error %d\n",
275 rc
, resp
.resp_sub_errno
);
279 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
280 pcibus_info
->pbi_enabled_devices
|= (1 << device_num
);
285 static int sn_slot_disable(struct hotplug_slot
*bss_hotplug_slot
,
286 int device_num
, int action
)
288 struct slot
*slot
= bss_hotplug_slot
->private;
289 struct pcibus_info
*pcibus_info
;
290 struct pcibr_slot_disable_resp resp
;
293 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
295 rc
= sal_pcibr_slot_disable(pcibus_info
, device_num
, action
, &resp
);
297 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) &&
298 (rc
== PCI_SLOT_ALREADY_DOWN
)) {
299 dev_dbg(slot
->pci_bus
->self
, "Slot %s already inactive\n");
300 return 1; /* return 1 to user */
303 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && (rc
== PCI_EMPTY_33MHZ
)) {
304 dev_dbg(slot
->pci_bus
->self
,
305 "Cannot remove last 33MHz card\n");
309 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && (rc
== PCI_L1_ERR
)) {
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
);
316 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && rc
) {
317 dev_dbg(slot
->pci_bus
->self
,
318 "remove failed with error %d sub-error %d\n",
319 rc
, resp
.resp_sub_errno
);
323 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && !rc
)
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
);
329 dev_dbg(slot
->pci_bus
->self
, "remove successful\n");
333 if ((action
== PCI_REQ_SLOT_DISABLE
) && rc
) {
334 dev_dbg(slot
->pci_bus
->self
,"remove failed rc = %d\n", rc
);
340 static int enable_slot(struct hotplug_slot
*bss_hotplug_slot
)
342 struct slot
*slot
= bss_hotplug_slot
->private;
343 struct pci_bus
*new_bus
= NULL
;
349 /* Serialize the Linux PCI infrastructure */
350 mutex_lock(&sn_hotplug_mutex
);
353 * Power-on and initialize the slot in the SN
354 * PCI infrastructure.
356 rc
= sn_slot_enable(bss_hotplug_slot
, slot
->device_num
);
358 mutex_unlock(&sn_hotplug_mutex
);
362 num_funcs
= pci_scan_slot(slot
->pci_bus
,
363 PCI_DEVFN(slot
->device_num
+ 1, 0));
365 dev_dbg(slot
->pci_bus
->self
, "no device in slot\n");
366 mutex_unlock(&sn_hotplug_mutex
);
370 sn_pci_controller_fixup(pci_domain_nr(slot
->pci_bus
),
371 slot
->pci_bus
->number
,
374 * Map SN resources for all functions on the card
375 * to the Linux PCI interface and tell the drivers
378 for (func
= 0; func
< num_funcs
; func
++) {
379 dev
= pci_get_slot(slot
->pci_bus
,
380 PCI_DEVFN(slot
->device_num
+ 1,
383 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
) {
384 unsigned char sec_bus
;
385 pci_read_config_byte(dev
, PCI_SECONDARY_BUS
,
387 new_bus
= pci_add_new_bus(dev
->bus
, dev
,
389 pci_scan_child_bus(new_bus
);
390 sn_pci_controller_fixup(pci_domain_nr(new_bus
),
395 sn_bus_alloc_data(dev
);
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 */
404 pci_bus_add_devices(new_bus
);
406 mutex_unlock(&sn_hotplug_mutex
);
409 dev_dbg(slot
->pci_bus
->self
,
410 "insert operation successful\n");
412 dev_dbg(slot
->pci_bus
->self
,
413 "insert operation failed rc = %d\n", rc
);
418 static int disable_slot(struct hotplug_slot
*bss_hotplug_slot
)
420 struct slot
*slot
= bss_hotplug_slot
->private;
425 /* Acquire update access to the bus */
426 mutex_lock(&sn_hotplug_mutex
);
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
);
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
,
437 PCI_DEVFN(slot
->device_num
+ 1,
440 sn_bus_free_data(dev
);
441 pci_remove_bus_device(dev
);
446 /* free the collected sysdata pointers */
447 sn_bus_free_sysdata();
449 /* Deactivate slot */
450 rc
= sn_slot_disable(bss_hotplug_slot
, slot
->device_num
,
451 PCI_REQ_SLOT_DISABLE
);
453 /* Release the bus lock */
454 mutex_unlock(&sn_hotplug_mutex
);
459 static inline int get_power_status(struct hotplug_slot
*bss_hotplug_slot
,
462 struct slot
*slot
= bss_hotplug_slot
->private;
463 struct pcibus_info
*pcibus_info
;
465 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
466 mutex_lock(&sn_hotplug_mutex
);
467 *value
= pcibus_info
->pbi_enabled_devices
& (1 << slot
->device_num
);
468 mutex_unlock(&sn_hotplug_mutex
);
472 static void sn_release_slot(struct hotplug_slot
*bss_hotplug_slot
)
474 kfree(bss_hotplug_slot
->info
);
475 kfree(bss_hotplug_slot
->name
);
476 kfree(bss_hotplug_slot
->private);
477 kfree(bss_hotplug_slot
);
480 static int sn_hotplug_slot_register(struct pci_bus
*pci_bus
)
483 struct hotplug_slot
*bss_hotplug_slot
;
487 * Currently only four devices are supported,
488 * in the future there maybe more -- up to 32.
491 for (device
= 0; device
< SN_MAX_HP_SLOTS
; device
++) {
492 if (sn_pci_slot_valid(pci_bus
, device
) != 1)
495 bss_hotplug_slot
= kzalloc(sizeof(*bss_hotplug_slot
),
497 if (!bss_hotplug_slot
) {
502 bss_hotplug_slot
->info
=
503 kzalloc(sizeof(struct hotplug_slot_info
),
505 if (!bss_hotplug_slot
->info
) {
510 if (sn_hp_slot_private_alloc(bss_hotplug_slot
,
516 bss_hotplug_slot
->ops
= &sn_hotplug_slot_ops
;
517 bss_hotplug_slot
->release
= &sn_release_slot
;
519 rc
= pci_hp_register(bss_hotplug_slot
);
523 rc
= sysfs_create_file(&bss_hotplug_slot
->kobj
,
524 &sn_slot_path_attr
.attr
);
528 dev_dbg(pci_bus
->self
, "Registered bus with hotplug\n");
532 dev_dbg(pci_bus
->self
, "bus failed to register with err = %d\n",
537 dev_dbg(pci_bus
->self
, "Memory allocation error\n");
539 /* destroy THIS element */
540 if (bss_hotplug_slot
)
541 sn_release_slot(bss_hotplug_slot
);
543 /* destroy anything else on the list */
544 while ((bss_hotplug_slot
= sn_hp_destroy()))
545 pci_hp_deregister(bss_hotplug_slot
);
550 static int sn_pci_hotplug_init(void)
552 struct pci_bus
*pci_bus
= NULL
;
556 if (sn_sal_rev() < SGI_HOTPLUG_PROM_REV
) {
557 printk(KERN_ERR
"%s: PROM version must be greater than 4.30\n",
562 INIT_LIST_HEAD(&sn_hp_list
);
564 while ((pci_bus
= pci_find_next_bus(pci_bus
))) {
565 if (!pci_bus
->sysdata
)
568 rc
= sn_pci_bus_valid(pci_bus
);
570 dev_dbg(pci_bus
->self
, "not a valid hotplug bus\n");
573 dev_dbg(pci_bus
->self
, "valid hotplug bus\n");
575 rc
= sn_hotplug_slot_register(pci_bus
);
584 return registered
== 1 ? 0 : -ENODEV
;
587 static void sn_pci_hotplug_exit(void)
589 struct hotplug_slot
*bss_hotplug_slot
;
591 while ((bss_hotplug_slot
= sn_hp_destroy()))
592 pci_hp_deregister(bss_hotplug_slot
);
594 if (!list_empty(&sn_hp_list
))
595 printk(KERN_ERR
"%s: internal list is not empty\n", __FILE__
);
598 module_init(sn_pci_hotplug_init
);
599 module_exit(sn_pci_hotplug_exit
);