1
2
3
4
5
6
7
8
9
10
11
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>
22
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>
34
35#include "../pci.h"
36
37MODULE_LICENSE("GPL");
38MODULE_AUTHOR("SGI (prarit@sgi.com, dickie@sgi.com, habeck@sgi.com)");
39MODULE_DESCRIPTION("SGI Altix Hot Plug PCI Controller Driver");
40
41
42
43#define PCI_SLOT_ALREADY_UP 2
44#define PCI_SLOT_ALREADY_DOWN 3
45#define PCI_L1_ERR 7
46#define PCI_EMPTY_33MHZ 15
47
48
49#define PCIIO_ASIC_TYPE_TIOCA 4
50#define PCI_L1_QSIZE 128
51#define SN_MAX_HP_SLOTS 32
52#define SN_SLOT_NAME_SIZE 33
53
54
55static struct list_head sn_hp_list;
56
57
58struct slot {
59 int device_num;
60 struct pci_bus *pci_bus;
61
62 struct hotplug_slot *hotplug_slot;
63 struct list_head hp_list;
64 char physical_path[SN_SLOT_NAME_SIZE];
65};
66
67struct pcibr_slot_enable_resp {
68 int resp_sub_errno;
69 char resp_l1_msg[PCI_L1_QSIZE + 1];
70};
71
72struct pcibr_slot_disable_resp {
73 int resp_sub_errno;
74 char resp_l1_msg[PCI_L1_QSIZE + 1];
75};
76
77enum sn_pci_req_e {
78 PCI_REQ_SLOT_ELIGIBLE,
79 PCI_REQ_SLOT_DISABLE
80};
81
82static int enable_slot(struct hotplug_slot *slot);
83static int disable_slot(struct hotplug_slot *slot);
84static inline int get_power_status(struct hotplug_slot *slot, u8 *value);
85
86static 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,
90};
91
92static DEFINE_MUTEX(sn_hotplug_mutex);
93
94static ssize_t path_show(struct pci_slot *pci_slot, char *buf)
95{
96 int retval = -ENOENT;
97 struct slot *slot = pci_slot->hotplug->private;
98
99 if (!slot)
100 return retval;
101
102 retval = sprintf(buf, "%s\n", slot->physical_path);
103 return retval;
104}
105
106static struct pci_slot_attribute sn_slot_path_attr = __ATTR_RO(path);
107
108static int sn_pci_slot_valid(struct pci_bus *pci_bus, int device)
109{
110 struct pcibus_info *pcibus_info;
111 u16 busnum, segment, ioboard_type;
112
113 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
114
115
116 if (!(pcibus_info->pbi_valid_devices & (1 << device)))
117 return -EPERM;
118
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;
122
123
124 if ((ioboard_type == L1_BRICKTYPE_IX ||
125 ioboard_type == L1_BRICKTYPE_IA) &&
126 (segment == 1 && busnum == 0 && device != 1))
127 return -EPERM;
128
129 return 1;
130}
131
132static int sn_pci_bus_valid(struct pci_bus *pci_bus)
133{
134 struct pcibus_info *pcibus_info;
135 u32 asic_type;
136 u16 ioboard_type;
137
138
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)
142 return -EPERM;
143
144
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:
152 return 1;
153 break;
154 default:
155 return -EPERM;
156 break;
157 }
158
159 return -EIO;
160}
161
162static int sn_hp_slot_private_alloc(struct hotplug_slot *bss_hotplug_slot,
163 struct pci_bus *pci_bus, int device,
164 char *name)
165{
166 struct pcibus_info *pcibus_info;
167 struct slot *slot;
168
169 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(pci_bus);
170
171 slot = kzalloc(sizeof(*slot), GFP_KERNEL);
172 if (!slot)
173 return -ENOMEM;
174 bss_hotplug_slot->private = slot;
175
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),
181 device + 1);
182
183 sn_generate_path(pci_bus, slot->physical_path);
184
185 slot->hotplug_slot = bss_hotplug_slot;
186 list_add(&slot->hp_list, &sn_hp_list);
187
188 return 0;
189}
190
191static struct hotplug_slot *sn_hp_destroy(void)
192{
193 struct slot *slot;
194 struct pci_slot *pci_slot;
195 struct hotplug_slot *bss_hotplug_slot = NULL;
196
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)->
201 hp_list);
202 sysfs_remove_file(&pci_slot->kobj,
203 &sn_slot_path_attr.attr);
204 break;
205 }
206 return bss_hotplug_slot;
207}
208
209static void sn_bus_free_data(struct pci_dev *dev)
210{
211 struct pci_bus *subordinate_bus;
212 struct pci_dev *child;
213
214
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);
219 }
220
221
222
223
224
225
226 sn_bus_store_sysdata(dev);
227 sn_pci_unfixup_slot(dev);
228}
229
230static int sn_slot_enable(struct hotplug_slot *bss_hotplug_slot,
231 int device_num, char **ssdt)
232{
233 struct slot *slot = bss_hotplug_slot->private;
234 struct pcibus_info *pcibus_info;
235 struct pcibr_slot_enable_resp resp;
236 int rc;
237
238 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
239
240
241
242
243
244 rc = sal_pcibr_slot_enable(pcibus_info, device_num, &resp, ssdt);
245
246
247 if (rc == PCI_SLOT_ALREADY_UP) {
248 dev_dbg(&slot->pci_bus->self->dev, "is already active\n");
249 return 1;
250 }
251
252 if (rc == PCI_L1_ERR) {
253 dev_dbg(&slot->pci_bus->self->dev, "L1 failure %d with message: %s",
254 resp.resp_sub_errno, resp.resp_l1_msg);
255 return -EPERM;
256 }
257
258 if (rc) {
259 dev_dbg(&slot->pci_bus->self->dev, "insert failed with error %d sub-error %d\n",
260 rc, resp.resp_sub_errno);
261 return -EIO;
262 }
263
264 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
265 pcibus_info->pbi_enabled_devices |= (1 << device_num);
266
267 return 0;
268}
269
270static int sn_slot_disable(struct hotplug_slot *bss_hotplug_slot,
271 int device_num, int action)
272{
273 struct slot *slot = bss_hotplug_slot->private;
274 struct pcibus_info *pcibus_info;
275 struct pcibr_slot_disable_resp resp;
276 int rc;
277
278 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
279
280 rc = sal_pcibr_slot_disable(pcibus_info, device_num, action, &resp);
281
282 if ((action == PCI_REQ_SLOT_ELIGIBLE) &&
283 (rc == PCI_SLOT_ALREADY_DOWN)) {
284 dev_dbg(&slot->pci_bus->self->dev, "Slot %s already inactive\n", slot->physical_path);
285 return 1;
286 }
287
288 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_EMPTY_33MHZ)) {
289 dev_dbg(&slot->pci_bus->self->dev, "Cannot remove last 33MHz card\n");
290 return -EPERM;
291 }
292
293 if ((action == PCI_REQ_SLOT_ELIGIBLE) && (rc == PCI_L1_ERR)) {
294 dev_dbg(&slot->pci_bus->self->dev, "L1 failure %d with message \n%s\n",
295 resp.resp_sub_errno, resp.resp_l1_msg);
296 return -EPERM;
297 }
298
299 if ((action == PCI_REQ_SLOT_ELIGIBLE) && rc) {
300 dev_dbg(&slot->pci_bus->self->dev, "remove failed with error %d sub-error %d\n",
301 rc, resp.resp_sub_errno);
302 return -EIO;
303 }
304
305 if ((action == PCI_REQ_SLOT_ELIGIBLE) && !rc)
306 return 0;
307
308 if ((action == PCI_REQ_SLOT_DISABLE) && !rc) {
309 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
310 pcibus_info->pbi_enabled_devices &= ~(1 << device_num);
311 dev_dbg(&slot->pci_bus->self->dev, "remove successful\n");
312 return 0;
313 }
314
315 if ((action == PCI_REQ_SLOT_DISABLE) && rc) {
316 dev_dbg(&slot->pci_bus->self->dev, "remove failed rc = %d\n", rc);
317 }
318
319 return rc;
320}
321
322
323
324
325
326
327static int enable_slot(struct hotplug_slot *bss_hotplug_slot)
328{
329 struct slot *slot = bss_hotplug_slot->private;
330 struct pci_bus *new_bus = NULL;
331 struct pci_dev *dev;
332 int num_funcs;
333 int new_ppb = 0;
334 int rc;
335 char *ssdt = NULL;
336 void pcibios_fixup_device_resources(struct pci_dev *);
337
338
339 mutex_lock(&sn_hotplug_mutex);
340
341
342
343
344
345
346 rc = sn_slot_enable(bss_hotplug_slot, slot->device_num, &ssdt);
347 if (rc) {
348 mutex_unlock(&sn_hotplug_mutex);
349 return rc;
350 }
351
352 if (ssdt)
353 ssdt = __va(ssdt);
354
355 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
356 acpi_status ret;
357
358 ret = acpi_load_table((struct acpi_table_header *)ssdt);
359 if (ACPI_FAILURE(ret)) {
360 printk(KERN_ERR "%s: acpi_load_table failed (0x%x)\n",
361 __func__, ret);
362
363 }
364 }
365
366 num_funcs = pci_scan_slot(slot->pci_bus,
367 PCI_DEVFN(slot->device_num + 1, 0));
368 if (!num_funcs) {
369 dev_dbg(&slot->pci_bus->self->dev, "no device in slot\n");
370 mutex_unlock(&sn_hotplug_mutex);
371 return -ENODEV;
372 }
373
374
375
376
377
378
379 list_for_each_entry(dev, &slot->pci_bus->devices, bus_list) {
380 if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
381 continue;
382
383
384
385
386
387
388 pcibios_fixup_device_resources(dev);
389 if (SN_ACPI_BASE_SUPPORT())
390 sn_acpi_slot_fixup(dev);
391 else
392 sn_io_slot_fixup(dev);
393 if (dev->hdr_type == PCI_HEADER_TYPE_BRIDGE) {
394 pci_hp_add_bridge(dev);
395 if (dev->subordinate) {
396 new_bus = dev->subordinate;
397 new_ppb = 1;
398 }
399 }
400 }
401
402
403
404 if (SN_ACPI_BASE_SUPPORT() && ssdt) {
405 unsigned long long adr;
406 struct acpi_device *pdevice;
407 acpi_handle phandle;
408 acpi_handle chandle = NULL;
409 acpi_handle rethandle;
410 acpi_status ret;
411
412 phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
413
414 if (acpi_bus_get_device(phandle, &pdevice)) {
415 dev_dbg(&slot->pci_bus->self->dev, "no parent device, assuming NULL\n");
416 pdevice = NULL;
417 }
418
419 acpi_scan_lock_acquire();
420
421
422
423
424
425 for (;;) {
426 rethandle = NULL;
427 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
428 phandle, chandle,
429 &rethandle);
430
431 if (ret == AE_NOT_FOUND || rethandle == NULL)
432 break;
433
434 chandle = rethandle;
435
436 ret = acpi_evaluate_integer(chandle, METHOD_NAME__ADR,
437 NULL, &adr);
438
439 if (ACPI_SUCCESS(ret) &&
440 (adr>>16) == (slot->device_num + 1)) {
441
442 ret = acpi_bus_scan(chandle);
443 if (ACPI_FAILURE(ret)) {
444 printk(KERN_ERR "%s: acpi_bus_scan failed (0x%x) for slot %d func %d\n",
445 __func__, ret, (int)(adr>>16),
446 (int)(adr&0xffff));
447
448 }
449 }
450 }
451 acpi_scan_lock_release();
452 }
453
454 pci_lock_rescan_remove();
455
456
457 pci_bus_add_devices(slot->pci_bus);
458
459 if (new_ppb)
460 pci_bus_add_devices(new_bus);
461
462 pci_unlock_rescan_remove();
463 mutex_unlock(&sn_hotplug_mutex);
464
465 if (rc == 0)
466 dev_dbg(&slot->pci_bus->self->dev, "insert operation successful\n");
467 else
468 dev_dbg(&slot->pci_bus->self->dev, "insert operation failed rc = %d\n", rc);
469
470 return rc;
471}
472
473static int disable_slot(struct hotplug_slot *bss_hotplug_slot)
474{
475 struct slot *slot = bss_hotplug_slot->private;
476 struct pci_dev *dev, *temp;
477 int rc;
478 acpi_handle ssdt_hdl = NULL;
479
480
481 mutex_lock(&sn_hotplug_mutex);
482
483
484 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
485 PCI_REQ_SLOT_ELIGIBLE);
486 if (rc)
487 goto leaving;
488
489
490 if (SN_ACPI_BASE_SUPPORT() &&
491 PCI_CONTROLLER(slot->pci_bus)->companion) {
492 unsigned long long adr;
493 struct acpi_device *device;
494 acpi_handle phandle;
495 acpi_handle chandle = NULL;
496 acpi_handle rethandle;
497 acpi_status ret;
498
499
500 phandle = acpi_device_handle(PCI_CONTROLLER(slot->pci_bus)->companion);
501
502 acpi_scan_lock_acquire();
503
504
505
506
507
508 for (;;) {
509 rethandle = NULL;
510 ret = acpi_get_next_object(ACPI_TYPE_DEVICE,
511 phandle, chandle,
512 &rethandle);
513
514 if (ret == AE_NOT_FOUND || rethandle == NULL)
515 break;
516
517 chandle = rethandle;
518
519 ret = acpi_evaluate_integer(chandle,
520 METHOD_NAME__ADR,
521 NULL, &adr);
522 if (ACPI_SUCCESS(ret) &&
523 (adr>>16) == (slot->device_num + 1)) {
524
525 ssdt_hdl = chandle;
526
527 ret = acpi_bus_get_device(chandle,
528 &device);
529 if (ACPI_SUCCESS(ret))
530 acpi_bus_trim(device);
531 }
532 }
533 acpi_scan_lock_release();
534 }
535
536 pci_lock_rescan_remove();
537
538 list_for_each_entry_safe(dev, temp, &slot->pci_bus->devices, bus_list) {
539 if (PCI_SLOT(dev->devfn) != slot->device_num + 1)
540 continue;
541
542 pci_dev_get(dev);
543 sn_bus_free_data(dev);
544 pci_stop_and_remove_bus_device(dev);
545 pci_dev_put(dev);
546 }
547 pci_unlock_rescan_remove();
548
549
550 if (SN_ACPI_BASE_SUPPORT() && ssdt_hdl) {
551 acpi_status ret;
552 ret = acpi_unload_parent_table(ssdt_hdl);
553 if (ACPI_FAILURE(ret)) {
554 acpi_handle_err(ssdt_hdl,
555 "%s: acpi_unload_parent_table failed (0x%x)\n",
556 __func__, ret);
557
558 }
559 }
560
561
562 sn_bus_free_sysdata();
563
564
565 rc = sn_slot_disable(bss_hotplug_slot, slot->device_num,
566 PCI_REQ_SLOT_DISABLE);
567 leaving:
568
569 mutex_unlock(&sn_hotplug_mutex);
570
571 return rc;
572}
573
574static inline int get_power_status(struct hotplug_slot *bss_hotplug_slot,
575 u8 *value)
576{
577 struct slot *slot = bss_hotplug_slot->private;
578 struct pcibus_info *pcibus_info;
579 u32 power;
580
581 pcibus_info = SN_PCIBUS_BUSSOFT_INFO(slot->pci_bus);
582 mutex_lock(&sn_hotplug_mutex);
583 power = pcibus_info->pbi_enabled_devices & (1 << slot->device_num);
584 *value = power ? 1 : 0;
585 mutex_unlock(&sn_hotplug_mutex);
586 return 0;
587}
588
589static void sn_release_slot(struct hotplug_slot *bss_hotplug_slot)
590{
591 kfree(bss_hotplug_slot->info);
592 kfree(bss_hotplug_slot->private);
593 kfree(bss_hotplug_slot);
594}
595
596static int sn_hotplug_slot_register(struct pci_bus *pci_bus)
597{
598 int device;
599 struct pci_slot *pci_slot;
600 struct hotplug_slot *bss_hotplug_slot;
601 char name[SN_SLOT_NAME_SIZE];
602 int rc = 0;
603
604
605
606
607
608
609 for (device = 0; device < SN_MAX_HP_SLOTS ; device++) {
610 if (sn_pci_slot_valid(pci_bus, device) != 1)
611 continue;
612
613 bss_hotplug_slot = kzalloc(sizeof(*bss_hotplug_slot),
614 GFP_KERNEL);
615 if (!bss_hotplug_slot) {
616 rc = -ENOMEM;
617 goto alloc_err;
618 }
619
620 bss_hotplug_slot->info =
621 kzalloc(sizeof(struct hotplug_slot_info),
622 GFP_KERNEL);
623 if (!bss_hotplug_slot->info) {
624 rc = -ENOMEM;
625 goto alloc_err;
626 }
627
628 if (sn_hp_slot_private_alloc(bss_hotplug_slot,
629 pci_bus, device, name)) {
630 rc = -ENOMEM;
631 goto alloc_err;
632 }
633 bss_hotplug_slot->ops = &sn_hotplug_slot_ops;
634 bss_hotplug_slot->release = &sn_release_slot;
635
636 rc = pci_hp_register(bss_hotplug_slot, pci_bus, device, name);
637 if (rc)
638 goto register_err;
639
640 pci_slot = bss_hotplug_slot->pci_slot;
641 rc = sysfs_create_file(&pci_slot->kobj,
642 &sn_slot_path_attr.attr);
643 if (rc)
644 goto register_err;
645 }
646 dev_dbg(&pci_bus->self->dev, "Registered bus with hotplug\n");
647 return rc;
648
649register_err:
650 dev_dbg(&pci_bus->self->dev, "bus failed to register with err = %d\n",
651 rc);
652
653alloc_err:
654 if (rc == -ENOMEM)
655 dev_dbg(&pci_bus->self->dev, "Memory allocation error\n");
656
657
658 if (bss_hotplug_slot)
659 sn_release_slot(bss_hotplug_slot);
660
661
662 while ((bss_hotplug_slot = sn_hp_destroy()))
663 pci_hp_deregister(bss_hotplug_slot);
664
665 return rc;
666}
667
668static int __init sn_pci_hotplug_init(void)
669{
670 struct pci_bus *pci_bus = NULL;
671 int rc;
672 int registered = 0;
673
674 if (!sn_prom_feature_available(PRF_HOTPLUG_SUPPORT)) {
675 printk(KERN_ERR "%s: PROM version does not support hotplug.\n",
676 __func__);
677 return -EPERM;
678 }
679
680 INIT_LIST_HEAD(&sn_hp_list);
681
682 while ((pci_bus = pci_find_next_bus(pci_bus))) {
683 if (!pci_bus->sysdata)
684 continue;
685
686 rc = sn_pci_bus_valid(pci_bus);
687 if (rc != 1) {
688 dev_dbg(&pci_bus->self->dev, "not a valid hotplug bus\n");
689 continue;
690 }
691 dev_dbg(&pci_bus->self->dev, "valid hotplug bus\n");
692
693 rc = sn_hotplug_slot_register(pci_bus);
694 if (!rc) {
695 registered = 1;
696 } else {
697 registered = 0;
698 break;
699 }
700 }
701
702 return registered == 1 ? 0 : -ENODEV;
703}
704
705static void __exit sn_pci_hotplug_exit(void)
706{
707 struct hotplug_slot *bss_hotplug_slot;
708
709 while ((bss_hotplug_slot = sn_hp_destroy()))
710 pci_hp_deregister(bss_hotplug_slot);
711
712 if (!list_empty(&sn_hp_list))
713 printk(KERN_ERR "%s: internal list is not empty\n", __FILE__);
714}
715
716module_init(sn_pci_hotplug_init);
717module_exit(sn_pci_hotplug_exit);
718