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 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>
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>
29 #include "pci_hotplug.h"
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");
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 */
45 /* internal list head */
46 static struct list_head sn_hp_list
;
48 /* hotplug_slot struct's private pointer */
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
;
55 char physical_path
[SN_SLOT_NAME_SIZE
];
58 struct pcibr_slot_enable_resp
{
60 char resp_l1_msg
[PCI_L1_QSIZE
+ 1];
63 struct pcibr_slot_disable_resp
{
65 char resp_l1_msg
[PCI_L1_QSIZE
+ 1];
69 PCI_REQ_SLOT_ELIGIBLE
,
73 static int enable_slot(struct hotplug_slot
*slot
);
74 static int disable_slot(struct hotplug_slot
*slot
);
75 static inline int get_power_status(struct hotplug_slot
*slot
, u8
*value
);
77 static struct hotplug_slot_ops sn_hotplug_slot_ops
= {
79 .enable_slot
= enable_slot
,
80 .disable_slot
= disable_slot
,
81 .get_power_status
= get_power_status
,
84 static DECLARE_MUTEX(sn_hotplug_sem
);
86 static ssize_t
path_show (struct hotplug_slot
*bss_hotplug_slot
,
90 struct slot
*slot
= bss_hotplug_slot
->private;
95 retval
= sprintf (buf
, "%s\n", slot
->physical_path
);
99 static struct hotplug_slot_attribute sn_slot_path_attr
= __ATTR_RO(path
);
101 static int sn_pci_slot_valid(struct pci_bus
*pci_bus
, int device
)
103 struct pcibus_info
*pcibus_info
;
107 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
109 /* Check to see if this is a valid slot on 'pci_bus' */
110 if (!(pcibus_info
->pbi_valid_devices
& (1 << device
)))
113 bricktype
= MODULE_GET_BTYPE(pcibus_info
->pbi_moduleid
);
114 bus_num
= pcibus_info
->pbi_buscommon
.bs_persist_busnum
& 0xf;
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))
124 static int sn_pci_bus_valid(struct pci_bus
*pci_bus
)
126 struct pcibus_info
*pcibus_info
;
130 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
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
)
137 /* Only register slots in I/O Bricks that support hotplug */
138 bricktype
= MODULE_GET_BTYPE(pcibus_info
->pbi_moduleid
);
140 case L1_BRICKTYPE_IX
:
141 case L1_BRICKTYPE_PX
:
142 case L1_BRICKTYPE_IA
:
143 case L1_BRICKTYPE_PA
:
154 static int sn_hp_slot_private_alloc(struct hotplug_slot
*bss_hotplug_slot
,
155 struct pci_bus
*pci_bus
, int device
)
157 struct pcibus_info
*pcibus_info
;
160 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(pci_bus
);
162 slot
= kzalloc(sizeof(*slot
), GFP_KERNEL
);
165 bss_hotplug_slot
->private = slot
;
167 bss_hotplug_slot
->name
= kmalloc(SN_SLOT_NAME_SIZE
, GFP_KERNEL
);
168 if (!bss_hotplug_slot
->name
) {
169 kfree(bss_hotplug_slot
->private);
173 slot
->device_num
= device
;
174 slot
->pci_bus
= pci_bus
;
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,
179 sprintf(slot
->physical_path
, "module_%c%c%c%c%.2d",
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
),
184 MODULE_GET_BPOS(pcibus_info
->pbi_moduleid
));
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 hotplug_slot
*bss_hotplug_slot
= NULL
;
196 list_for_each_entry(slot
, &sn_hp_list
, hp_list
) {
197 bss_hotplug_slot
= slot
->hotplug_slot
;
198 list_del(&((struct slot
*)bss_hotplug_slot
->private)->
200 sysfs_remove_file(&bss_hotplug_slot
->kobj
,
201 &sn_slot_path_attr
.attr
);
204 return bss_hotplug_slot
;
207 static void sn_bus_alloc_data(struct pci_dev
*dev
)
209 struct pci_bus
*subordinate_bus
;
210 struct pci_dev
*child
;
212 sn_pci_fixup_slot(dev
);
214 /* Recursively sets up the 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_alloc_data(child
);
222 static void sn_bus_free_data(struct pci_dev
*dev
)
224 struct pci_bus
*subordinate_bus
;
225 struct pci_dev
*child
;
227 /* Recursively clean up sn_irq_info structs */
228 if (dev
->subordinate
) {
229 subordinate_bus
= dev
->subordinate
;
230 list_for_each_entry(child
, &subordinate_bus
->devices
, bus_list
)
231 sn_bus_free_data(child
);
233 sn_pci_unfixup_slot(dev
);
236 static int sn_slot_enable(struct hotplug_slot
*bss_hotplug_slot
,
239 struct slot
*slot
= bss_hotplug_slot
->private;
240 struct pcibus_info
*pcibus_info
;
241 struct pcibr_slot_enable_resp resp
;
244 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
247 * Power-on and initialize the slot in the SN
248 * PCI infrastructure.
250 rc
= sal_pcibr_slot_enable(pcibus_info
, device_num
, &resp
);
252 if (rc
== PCI_SLOT_ALREADY_UP
) {
253 dev_dbg(slot
->pci_bus
->self
, "is already active\n");
254 return 1; /* return 1 to user */
257 if (rc
== PCI_L1_ERR
) {
258 dev_dbg(slot
->pci_bus
->self
,
259 "L1 failure %d with message: %s",
260 resp
.resp_sub_errno
, resp
.resp_l1_msg
);
265 dev_dbg(slot
->pci_bus
->self
,
266 "insert failed with error %d sub-error %d\n",
267 rc
, resp
.resp_sub_errno
);
271 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
272 pcibus_info
->pbi_enabled_devices
|= (1 << device_num
);
277 static int sn_slot_disable(struct hotplug_slot
*bss_hotplug_slot
,
278 int device_num
, int action
)
280 struct slot
*slot
= bss_hotplug_slot
->private;
281 struct pcibus_info
*pcibus_info
;
282 struct pcibr_slot_disable_resp resp
;
285 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
287 rc
= sal_pcibr_slot_disable(pcibus_info
, device_num
, action
, &resp
);
289 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) &&
290 (rc
== PCI_SLOT_ALREADY_DOWN
)) {
291 dev_dbg(slot
->pci_bus
->self
, "Slot %s already inactive\n");
292 return 1; /* return 1 to user */
295 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && (rc
== PCI_EMPTY_33MHZ
)) {
296 dev_dbg(slot
->pci_bus
->self
,
297 "Cannot remove last 33MHz card\n");
301 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && (rc
== PCI_L1_ERR
)) {
302 dev_dbg(slot
->pci_bus
->self
,
303 "L1 failure %d with message \n%s\n",
304 resp
.resp_sub_errno
, resp
.resp_l1_msg
);
308 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && rc
) {
309 dev_dbg(slot
->pci_bus
->self
,
310 "remove failed with error %d sub-error %d\n",
311 rc
, resp
.resp_sub_errno
);
315 if ((action
== PCI_REQ_SLOT_ELIGIBLE
) && !rc
)
318 if ((action
== PCI_REQ_SLOT_DISABLE
) && !rc
) {
319 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
320 pcibus_info
->pbi_enabled_devices
&= ~(1 << device_num
);
321 dev_dbg(slot
->pci_bus
->self
, "remove successful\n");
325 if ((action
== PCI_REQ_SLOT_DISABLE
) && rc
) {
326 dev_dbg(slot
->pci_bus
->self
,"remove failed rc = %d\n", rc
);
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 /* Serialize the Linux PCI infrastructure */
342 down(&sn_hotplug_sem
);
345 * Power-on and initialize the slot in the SN
346 * PCI infrastructure.
348 rc
= sn_slot_enable(bss_hotplug_slot
, slot
->device_num
);
354 num_funcs
= pci_scan_slot(slot
->pci_bus
,
355 PCI_DEVFN(slot
->device_num
+ 1, 0));
357 dev_dbg(slot
->pci_bus
->self
, "no device in slot\n");
362 sn_pci_controller_fixup(pci_domain_nr(slot
->pci_bus
),
363 slot
->pci_bus
->number
,
366 * Map SN resources for all functions on the card
367 * to the Linux PCI interface and tell the drivers
370 for (func
= 0; func
< num_funcs
; func
++) {
371 dev
= pci_get_slot(slot
->pci_bus
,
372 PCI_DEVFN(slot
->device_num
+ 1,
375 if (dev
->hdr_type
== PCI_HEADER_TYPE_BRIDGE
) {
376 unsigned char sec_bus
;
377 pci_read_config_byte(dev
, PCI_SECONDARY_BUS
,
379 new_bus
= pci_add_new_bus(dev
->bus
, dev
,
381 pci_scan_child_bus(new_bus
);
382 sn_pci_controller_fixup(pci_domain_nr(new_bus
),
387 sn_bus_alloc_data(dev
);
392 /* Call the driver for the new device */
393 pci_bus_add_devices(slot
->pci_bus
);
394 /* Call the drivers for the new devices subordinate to PPB */
396 pci_bus_add_devices(new_bus
);
401 dev_dbg(slot
->pci_bus
->self
,
402 "insert operation successful\n");
404 dev_dbg(slot
->pci_bus
->self
,
405 "insert operation failed rc = %d\n", rc
);
410 static int disable_slot(struct hotplug_slot
*bss_hotplug_slot
)
412 struct slot
*slot
= bss_hotplug_slot
->private;
417 /* Acquire update access to the bus */
418 down(&sn_hotplug_sem
);
420 /* is it okay to bring this slot down? */
421 rc
= sn_slot_disable(bss_hotplug_slot
, slot
->device_num
,
422 PCI_REQ_SLOT_ELIGIBLE
);
426 /* Free the SN resources assigned to the Linux device.*/
427 for (func
= 0; func
< 8; func
++) {
428 dev
= pci_get_slot(slot
->pci_bus
,
429 PCI_DEVFN(slot
->device_num
+ 1,
433 * Some drivers may use dma accesses during the
434 * driver remove function. We release the sysdata
435 * areas after the driver remove functions have
438 sn_bus_store_sysdata(dev
);
439 sn_bus_free_data(dev
);
440 pci_remove_bus_device(dev
);
445 /* free the collected sysdata pointers */
446 sn_bus_free_sysdata();
448 /* Deactivate slot */
449 rc
= sn_slot_disable(bss_hotplug_slot
, slot
->device_num
,
450 PCI_REQ_SLOT_DISABLE
);
452 /* Release the bus lock */
458 static inline int get_power_status(struct hotplug_slot
*bss_hotplug_slot
,
461 struct slot
*slot
= bss_hotplug_slot
->private;
462 struct pcibus_info
*pcibus_info
;
464 pcibus_info
= SN_PCIBUS_BUSSOFT_INFO(slot
->pci_bus
);
465 down(&sn_hotplug_sem
);
466 *value
= pcibus_info
->pbi_enabled_devices
& (1 << slot
->device_num
);
471 static void sn_release_slot(struct hotplug_slot
*bss_hotplug_slot
)
473 kfree(bss_hotplug_slot
->info
);
474 kfree(bss_hotplug_slot
->name
);
475 kfree(bss_hotplug_slot
->private);
476 kfree(bss_hotplug_slot
);
479 static int sn_hotplug_slot_register(struct pci_bus
*pci_bus
)
482 struct hotplug_slot
*bss_hotplug_slot
;
486 * Currently only four devices are supported,
487 * in the future there maybe more -- up to 32.
490 for (device
= 0; device
< SN_MAX_HP_SLOTS
; device
++) {
491 if (sn_pci_slot_valid(pci_bus
, device
) != 1)
494 bss_hotplug_slot
= kzalloc(sizeof(*bss_hotplug_slot
),
496 if (!bss_hotplug_slot
) {
501 bss_hotplug_slot
->info
=
502 kzalloc(sizeof(struct hotplug_slot_info
),
504 if (!bss_hotplug_slot
->info
) {
509 if (sn_hp_slot_private_alloc(bss_hotplug_slot
,
515 bss_hotplug_slot
->ops
= &sn_hotplug_slot_ops
;
516 bss_hotplug_slot
->release
= &sn_release_slot
;
518 rc
= pci_hp_register(bss_hotplug_slot
);
522 rc
= sysfs_create_file(&bss_hotplug_slot
->kobj
,
523 &sn_slot_path_attr
.attr
);
527 dev_dbg(pci_bus
->self
, "Registered bus with hotplug\n");
531 dev_dbg(pci_bus
->self
, "bus failed to register with err = %d\n",
536 dev_dbg(pci_bus
->self
, "Memory allocation error\n");
538 /* destroy THIS element */
539 if (bss_hotplug_slot
)
540 sn_release_slot(bss_hotplug_slot
);
542 /* destroy anything else on the list */
543 while ((bss_hotplug_slot
= sn_hp_destroy()))
544 pci_hp_deregister(bss_hotplug_slot
);
549 static int sn_pci_hotplug_init(void)
551 struct pci_bus
*pci_bus
= NULL
;
555 if (sn_sal_rev() < SGI_HOTPLUG_PROM_REV
) {
556 printk(KERN_ERR
"%s: PROM version must be greater than 4.30\n",
561 INIT_LIST_HEAD(&sn_hp_list
);
563 while ((pci_bus
= pci_find_next_bus(pci_bus
))) {
564 if (!pci_bus
->sysdata
)
567 rc
= sn_pci_bus_valid(pci_bus
);
569 dev_dbg(pci_bus
->self
, "not a valid hotplug bus\n");
572 dev_dbg(pci_bus
->self
, "valid hotplug bus\n");
574 rc
= sn_hotplug_slot_register(pci_bus
);
583 return registered
== 1 ? 0 : -ENODEV
;
586 static void sn_pci_hotplug_exit(void)
588 struct hotplug_slot
*bss_hotplug_slot
;
590 while ((bss_hotplug_slot
= sn_hp_destroy()))
591 pci_hp_deregister(bss_hotplug_slot
);
593 if (!list_empty(&sn_hp_list
))
594 printk(KERN_ERR
"%s: internal list is not empty\n", __FILE__
);
597 module_init(sn_pci_hotplug_init
);
598 module_exit(sn_pci_hotplug_exit
);