blob: 3824942618305d7a03c885118a997c398be11825 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * PCI Hotplug Driver for PowerPC PowerNV platform.
4 *
5 * Copyright Gavin Shan, IBM Corporation 2016.
6 */
7
8#include <linux/libfdt.h>
9#include <linux/module.h>
10#include <linux/pci.h>
11#include <linux/pci_hotplug.h>
12
13#include <asm/opal.h>
14#include <asm/pnv-pci.h>
15#include <asm/ppc-pci.h>
16
17#define DRIVER_VERSION "0.1"
18#define DRIVER_AUTHOR "Gavin Shan, IBM Corporation"
19#define DRIVER_DESC "PowerPC PowerNV PCI Hotplug Driver"
20
21struct pnv_php_event {
22 bool added;
23 struct pnv_php_slot *php_slot;
24 struct work_struct work;
25};
26
27static LIST_HEAD(pnv_php_slot_list);
28static DEFINE_SPINLOCK(pnv_php_lock);
29
30static void pnv_php_register(struct device_node *dn);
31static void pnv_php_unregister_one(struct device_node *dn);
32static void pnv_php_unregister(struct device_node *dn);
33
34static void pnv_php_disable_irq(struct pnv_php_slot *php_slot,
35 bool disable_device)
36{
37 struct pci_dev *pdev = php_slot->pdev;
38 u16 ctrl;
39
40 if (php_slot->irq > 0) {
41 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
42 ctrl &= ~(PCI_EXP_SLTCTL_HPIE |
43 PCI_EXP_SLTCTL_PDCE |
44 PCI_EXP_SLTCTL_DLLSCE);
45 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
46
47 free_irq(php_slot->irq, php_slot);
48 php_slot->irq = 0;
49 }
50
51 if (php_slot->wq) {
52 destroy_workqueue(php_slot->wq);
53 php_slot->wq = NULL;
54 }
55
56 if (disable_device) {
57 if (pdev->msix_enabled)
58 pci_disable_msix(pdev);
59 else if (pdev->msi_enabled)
60 pci_disable_msi(pdev);
61
62 pci_disable_device(pdev);
63 }
64}
65
66static void pnv_php_free_slot(struct kref *kref)
67{
68 struct pnv_php_slot *php_slot = container_of(kref,
69 struct pnv_php_slot, kref);
70
71 WARN_ON(!list_empty(&php_slot->children));
72 pnv_php_disable_irq(php_slot, false);
73 kfree(php_slot->name);
74 kfree(php_slot);
75}
76
77static inline void pnv_php_put_slot(struct pnv_php_slot *php_slot)
78{
79
80 if (!php_slot)
81 return;
82
83 kref_put(&php_slot->kref, pnv_php_free_slot);
84}
85
86static struct pnv_php_slot *pnv_php_match(struct device_node *dn,
87 struct pnv_php_slot *php_slot)
88{
89 struct pnv_php_slot *target, *tmp;
90
91 if (php_slot->dn == dn) {
92 kref_get(&php_slot->kref);
93 return php_slot;
94 }
95
96 list_for_each_entry(tmp, &php_slot->children, link) {
97 target = pnv_php_match(dn, tmp);
98 if (target)
99 return target;
100 }
101
102 return NULL;
103}
104
105struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn)
106{
107 struct pnv_php_slot *php_slot, *tmp;
108 unsigned long flags;
109
110 spin_lock_irqsave(&pnv_php_lock, flags);
111 list_for_each_entry(tmp, &pnv_php_slot_list, link) {
112 php_slot = pnv_php_match(dn, tmp);
113 if (php_slot) {
114 spin_unlock_irqrestore(&pnv_php_lock, flags);
115 return php_slot;
116 }
117 }
118 spin_unlock_irqrestore(&pnv_php_lock, flags);
119
120 return NULL;
121}
122EXPORT_SYMBOL_GPL(pnv_php_find_slot);
123
124/*
125 * Remove pdn for all children of the indicated device node.
126 * The function should remove pdn in a depth-first manner.
127 */
128static void pnv_php_rmv_pdns(struct device_node *dn)
129{
130 struct device_node *child;
131
132 for_each_child_of_node(dn, child) {
133 pnv_php_rmv_pdns(child);
134
135 pci_remove_device_node_info(child);
136 }
137}
138
139/*
140 * Detach all child nodes of the indicated device nodes. The
141 * function should handle device nodes in depth-first manner.
142 *
143 * We should not invoke of_node_release() as the memory for
144 * individual device node is part of large memory block. The
145 * large block is allocated from memblock (system bootup) or
146 * kmalloc() when unflattening the device tree by OF changeset.
147 * We can not free the large block allocated from memblock. For
148 * later case, it should be released at once.
149 */
150static void pnv_php_detach_device_nodes(struct device_node *parent)
151{
152 struct device_node *dn;
153 int refcount;
154
155 for_each_child_of_node(parent, dn) {
156 pnv_php_detach_device_nodes(dn);
157
158 of_node_put(dn);
159 refcount = kref_read(&dn->kobj.kref);
160 if (refcount != 1)
161 pr_warn("Invalid refcount %d on <%pOF>\n",
162 refcount, dn);
163
164 of_detach_node(dn);
165 }
166}
167
168static void pnv_php_rmv_devtree(struct pnv_php_slot *php_slot)
169{
170 pnv_php_rmv_pdns(php_slot->dn);
171
172 /*
173 * Decrease the refcount if the device nodes were created
174 * through OF changeset before detaching them.
175 */
176 if (php_slot->fdt)
177 of_changeset_destroy(&php_slot->ocs);
178 pnv_php_detach_device_nodes(php_slot->dn);
179
180 if (php_slot->fdt) {
181 kfree(php_slot->dt);
182 kfree(php_slot->fdt);
183 php_slot->dt = NULL;
184 php_slot->dn->child = NULL;
185 php_slot->fdt = NULL;
186 }
187}
188
189/*
190 * As the nodes in OF changeset are applied in reverse order, we
191 * need revert the nodes in advance so that we have correct node
192 * order after the changeset is applied.
193 */
194static void pnv_php_reverse_nodes(struct device_node *parent)
195{
196 struct device_node *child, *next;
197
198 /* In-depth first */
199 for_each_child_of_node(parent, child)
200 pnv_php_reverse_nodes(child);
201
202 /* Reverse the nodes in the child list */
203 child = parent->child;
204 parent->child = NULL;
205 while (child) {
206 next = child->sibling;
207
208 child->sibling = parent->child;
209 parent->child = child;
210 child = next;
211 }
212}
213
214static int pnv_php_populate_changeset(struct of_changeset *ocs,
215 struct device_node *dn)
216{
217 struct device_node *child;
218 int ret = 0;
219
220 for_each_child_of_node(dn, child) {
221 ret = of_changeset_attach_node(ocs, child);
222 if (ret) {
223 of_node_put(child);
224 break;
225 }
226
227 ret = pnv_php_populate_changeset(ocs, child);
228 if (ret) {
229 of_node_put(child);
230 break;
231 }
232 }
233
234 return ret;
235}
236
237static void *pnv_php_add_one_pdn(struct device_node *dn, void *data)
238{
239 struct pci_controller *hose = (struct pci_controller *)data;
240 struct pci_dn *pdn;
241
242 pdn = pci_add_device_node_info(hose, dn);
243 if (!pdn)
244 return ERR_PTR(-ENOMEM);
245
246 return NULL;
247}
248
249static void pnv_php_add_pdns(struct pnv_php_slot *slot)
250{
251 struct pci_controller *hose = pci_bus_to_host(slot->bus);
252
253 pci_traverse_device_nodes(slot->dn, pnv_php_add_one_pdn, hose);
254}
255
256static int pnv_php_add_devtree(struct pnv_php_slot *php_slot)
257{
258 void *fdt, *fdt1, *dt;
259 int ret;
260
261 /* We don't know the FDT blob size. We try to get it through
262 * maximal memory chunk and then copy it to another chunk that
263 * fits the real size.
264 */
265 fdt1 = kzalloc(0x10000, GFP_KERNEL);
266 if (!fdt1) {
267 ret = -ENOMEM;
268 goto out;
269 }
270
271 ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000);
272 if (ret) {
273 pci_warn(php_slot->pdev, "Error %d getting FDT blob\n", ret);
274 goto free_fdt1;
275 }
276
277 fdt = kmemdup(fdt1, fdt_totalsize(fdt1), GFP_KERNEL);
278 if (!fdt) {
279 ret = -ENOMEM;
280 goto free_fdt1;
281 }
282
283 /* Unflatten device tree blob */
284 dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL);
285 if (!dt) {
286 ret = -EINVAL;
287 pci_warn(php_slot->pdev, "Cannot unflatten FDT\n");
288 goto free_fdt;
289 }
290
291 /* Initialize and apply the changeset */
292 of_changeset_init(&php_slot->ocs);
293 pnv_php_reverse_nodes(php_slot->dn);
294 ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn);
295 if (ret) {
296 pnv_php_reverse_nodes(php_slot->dn);
297 pci_warn(php_slot->pdev, "Error %d populating changeset\n",
298 ret);
299 goto free_dt;
300 }
301
302 php_slot->dn->child = NULL;
303 ret = of_changeset_apply(&php_slot->ocs);
304 if (ret) {
305 pci_warn(php_slot->pdev, "Error %d applying changeset\n", ret);
306 goto destroy_changeset;
307 }
308
309 /* Add device node firmware data */
310 pnv_php_add_pdns(php_slot);
311 php_slot->fdt = fdt;
312 php_slot->dt = dt;
313 kfree(fdt1);
314 goto out;
315
316destroy_changeset:
317 of_changeset_destroy(&php_slot->ocs);
318free_dt:
319 kfree(dt);
320 php_slot->dn->child = NULL;
321free_fdt:
322 kfree(fdt);
323free_fdt1:
324 kfree(fdt1);
325out:
326 return ret;
327}
328
329static inline struct pnv_php_slot *to_pnv_php_slot(struct hotplug_slot *slot)
330{
331 return container_of(slot, struct pnv_php_slot, slot);
332}
333
334int pnv_php_set_slot_power_state(struct hotplug_slot *slot,
335 uint8_t state)
336{
337 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
338 struct opal_msg msg;
339 int ret;
340
341 ret = pnv_pci_set_power_state(php_slot->id, state, &msg);
342 if (ret > 0) {
343 if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle ||
344 be64_to_cpu(msg.params[2]) != state ||
345 be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) {
346 pci_warn(php_slot->pdev, "Wrong msg (%lld, %lld, %lld)\n",
347 be64_to_cpu(msg.params[1]),
348 be64_to_cpu(msg.params[2]),
349 be64_to_cpu(msg.params[3]));
350 return -ENOMSG;
351 }
352 } else if (ret < 0) {
353 pci_warn(php_slot->pdev, "Error %d powering %s\n",
354 ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off");
355 return ret;
356 }
357
358 if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE)
359 pnv_php_rmv_devtree(php_slot);
360 else
361 ret = pnv_php_add_devtree(php_slot);
362
363 return ret;
364}
365EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state);
366
367static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state)
368{
369 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
370 uint8_t power_state = OPAL_PCI_SLOT_POWER_ON;
371 int ret;
372
373 /*
374 * Retrieve power status from firmware. If we fail
375 * getting that, the power status fails back to
376 * be on.
377 */
378 ret = pnv_pci_get_power_state(php_slot->id, &power_state);
379 if (ret) {
380 pci_warn(php_slot->pdev, "Error %d getting power status\n",
381 ret);
382 } else {
383 *state = power_state;
384 }
385
386 return 0;
387}
388
389static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state)
390{
391 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
392 uint8_t presence = OPAL_PCI_SLOT_EMPTY;
393 int ret;
394
395 /*
396 * Retrieve presence status from firmware. If we can't
397 * get that, it will fail back to be empty.
398 */
399 ret = pnv_pci_get_presence_state(php_slot->id, &presence);
400 if (ret >= 0) {
401 *state = presence;
402 ret = 0;
403 } else {
404 pci_warn(php_slot->pdev, "Error %d getting presence\n", ret);
405 }
406
407 return ret;
408}
409
410static int pnv_php_get_attention_state(struct hotplug_slot *slot, u8 *state)
411{
412 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
413
414 *state = php_slot->attention_state;
415 return 0;
416}
417
418static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state)
419{
420 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
421 struct pci_dev *bridge = php_slot->pdev;
422 u16 new, mask;
423
424 php_slot->attention_state = state;
425 if (!bridge)
426 return 0;
427
428 mask = PCI_EXP_SLTCTL_AIC;
429
430 if (state)
431 new = PCI_EXP_SLTCTL_ATTN_IND_ON;
432 else
433 new = PCI_EXP_SLTCTL_ATTN_IND_OFF;
434
435 pcie_capability_clear_and_set_word(bridge, PCI_EXP_SLTCTL, mask, new);
436
437 return 0;
438}
439
440static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan)
441{
442 struct hotplug_slot *slot = &php_slot->slot;
443 uint8_t presence = OPAL_PCI_SLOT_EMPTY;
444 uint8_t power_status = OPAL_PCI_SLOT_POWER_ON;
445 int ret;
446
447 /* Check if the slot has been configured */
448 if (php_slot->state != PNV_PHP_STATE_REGISTERED)
449 return 0;
450
451 /* Retrieve slot presence status */
452 ret = pnv_php_get_adapter_state(slot, &presence);
453 if (ret)
454 return ret;
455
456 /*
457 * Proceed if there have nothing behind the slot. However,
458 * we should leave the slot in registered state at the
459 * beginning. Otherwise, the PCI devices inserted afterwards
460 * won't be probed and populated.
461 */
462 if (presence == OPAL_PCI_SLOT_EMPTY) {
463 if (!php_slot->power_state_check) {
464 php_slot->power_state_check = true;
465
466 return 0;
467 }
468
469 goto scan;
470 }
471
472 /*
473 * If the power supply to the slot is off, we can't detect
474 * adapter presence state. That means we have to turn the
475 * slot on before going to probe slot's presence state.
476 *
477 * On the first time, we don't change the power status to
478 * boost system boot with assumption that the firmware
479 * supplies consistent slot power status: empty slot always
480 * has its power off and non-empty slot has its power on.
481 */
482 if (!php_slot->power_state_check) {
483 php_slot->power_state_check = true;
484
485 ret = pnv_php_get_power_state(slot, &power_status);
486 if (ret)
487 return ret;
488
489 if (power_status != OPAL_PCI_SLOT_POWER_ON)
490 return 0;
491 }
492
493 /* Check the power status. Scan the slot if it is already on */
494 ret = pnv_php_get_power_state(slot, &power_status);
495 if (ret)
496 return ret;
497
498 if (power_status == OPAL_PCI_SLOT_POWER_ON)
499 goto scan;
500
501 /* Power is off, turn it on and then scan the slot */
502 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON);
503 if (ret)
504 return ret;
505
506scan:
507 if (presence == OPAL_PCI_SLOT_PRESENT) {
508 if (rescan) {
509 pci_lock_rescan_remove();
510 pci_hp_add_devices(php_slot->bus);
511 pci_unlock_rescan_remove();
512 }
513
514 /* Rescan for child hotpluggable slots */
515 php_slot->state = PNV_PHP_STATE_POPULATED;
516 if (rescan)
517 pnv_php_register(php_slot->dn);
518 } else {
519 php_slot->state = PNV_PHP_STATE_POPULATED;
520 }
521
522 return 0;
523}
524
525static int pnv_php_reset_slot(struct hotplug_slot *slot, int probe)
526{
527 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
528 struct pci_dev *bridge = php_slot->pdev;
529 uint16_t sts;
530
531 /*
532 * The CAPI folks want pnv_php to drive OpenCAPI slots
533 * which don't have a bridge. Only claim to support
534 * reset_slot() if we have a bridge device (for now...)
535 */
536 if (probe)
537 return !bridge;
538
539 /* mask our interrupt while resetting the bridge */
540 if (php_slot->irq > 0)
541 disable_irq(php_slot->irq);
542
543 pci_bridge_secondary_bus_reset(bridge);
544
545 /* clear any state changes that happened due to the reset */
546 pcie_capability_read_word(php_slot->pdev, PCI_EXP_SLTSTA, &sts);
547 sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
548 pcie_capability_write_word(php_slot->pdev, PCI_EXP_SLTSTA, sts);
549
550 if (php_slot->irq > 0)
551 enable_irq(php_slot->irq);
552
553 return 0;
554}
555
556static int pnv_php_enable_slot(struct hotplug_slot *slot)
557{
558 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
559
560 return pnv_php_enable(php_slot, true);
561}
562
563static int pnv_php_disable_slot(struct hotplug_slot *slot)
564{
565 struct pnv_php_slot *php_slot = to_pnv_php_slot(slot);
566 int ret;
567
568 if (php_slot->state != PNV_PHP_STATE_POPULATED)
569 return 0;
570
571 /* Remove all devices behind the slot */
572 pci_lock_rescan_remove();
573 pci_hp_remove_devices(php_slot->bus);
574 pci_unlock_rescan_remove();
575
576 /* Detach the child hotpluggable slots */
577 pnv_php_unregister(php_slot->dn);
578
579 /* Notify firmware and remove device nodes */
580 ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF);
581
582 php_slot->state = PNV_PHP_STATE_REGISTERED;
583 return ret;
584}
585
586static const struct hotplug_slot_ops php_slot_ops = {
587 .get_power_status = pnv_php_get_power_state,
588 .get_adapter_status = pnv_php_get_adapter_state,
589 .get_attention_status = pnv_php_get_attention_state,
590 .set_attention_status = pnv_php_set_attention_state,
591 .enable_slot = pnv_php_enable_slot,
592 .disable_slot = pnv_php_disable_slot,
593 .reset_slot = pnv_php_reset_slot,
594};
595
596static void pnv_php_release(struct pnv_php_slot *php_slot)
597{
598 unsigned long flags;
599
600 /* Remove from global or child list */
601 spin_lock_irqsave(&pnv_php_lock, flags);
602 list_del(&php_slot->link);
603 spin_unlock_irqrestore(&pnv_php_lock, flags);
604
605 /* Detach from parent */
606 pnv_php_put_slot(php_slot);
607 pnv_php_put_slot(php_slot->parent);
608}
609
610static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn)
611{
612 struct pnv_php_slot *php_slot;
613 struct pci_bus *bus;
614 const char *label;
615 uint64_t id;
616 int ret;
617
618 ret = of_property_read_string(dn, "ibm,slot-label", &label);
619 if (ret)
620 return NULL;
621
622 if (pnv_pci_get_slot_id(dn, &id))
623 return NULL;
624
625 bus = pci_find_bus_by_node(dn);
626 if (!bus)
627 return NULL;
628
629 php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL);
630 if (!php_slot)
631 return NULL;
632
633 php_slot->name = kstrdup(label, GFP_KERNEL);
634 if (!php_slot->name) {
635 kfree(php_slot);
636 return NULL;
637 }
638
639 if (dn->child && PCI_DN(dn->child))
640 php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn);
641 else
642 php_slot->slot_no = -1; /* Placeholder slot */
643
644 kref_init(&php_slot->kref);
645 php_slot->state = PNV_PHP_STATE_INITIALIZED;
646 php_slot->dn = dn;
647 php_slot->pdev = bus->self;
648 php_slot->bus = bus;
649 php_slot->id = id;
650 php_slot->power_state_check = false;
651 php_slot->slot.ops = &php_slot_ops;
652
653 INIT_LIST_HEAD(&php_slot->children);
654 INIT_LIST_HEAD(&php_slot->link);
655
656 return php_slot;
657}
658
659static int pnv_php_register_slot(struct pnv_php_slot *php_slot)
660{
661 struct pnv_php_slot *parent;
662 struct device_node *dn = php_slot->dn;
663 unsigned long flags;
664 int ret;
665
666 /* Check if the slot is registered or not */
667 parent = pnv_php_find_slot(php_slot->dn);
668 if (parent) {
669 pnv_php_put_slot(parent);
670 return -EEXIST;
671 }
672
673 /* Register PCI slot */
674 ret = pci_hp_register(&php_slot->slot, php_slot->bus,
675 php_slot->slot_no, php_slot->name);
676 if (ret) {
677 pci_warn(php_slot->pdev, "Error %d registering slot\n", ret);
678 return ret;
679 }
680
681 /* Attach to the parent's child list or global list */
682 while ((dn = of_get_parent(dn))) {
683 if (!PCI_DN(dn)) {
684 of_node_put(dn);
685 break;
686 }
687
688 parent = pnv_php_find_slot(dn);
689 if (parent) {
690 of_node_put(dn);
691 break;
692 }
693
694 of_node_put(dn);
695 }
696
697 spin_lock_irqsave(&pnv_php_lock, flags);
698 php_slot->parent = parent;
699 if (parent)
700 list_add_tail(&php_slot->link, &parent->children);
701 else
702 list_add_tail(&php_slot->link, &pnv_php_slot_list);
703 spin_unlock_irqrestore(&pnv_php_lock, flags);
704
705 php_slot->state = PNV_PHP_STATE_REGISTERED;
706 return 0;
707}
708
709static int pnv_php_enable_msix(struct pnv_php_slot *php_slot)
710{
711 struct pci_dev *pdev = php_slot->pdev;
712 struct msix_entry entry;
713 int nr_entries, ret;
714 u16 pcie_flag;
715
716 /* Get total number of MSIx entries */
717 nr_entries = pci_msix_vec_count(pdev);
718 if (nr_entries < 0)
719 return nr_entries;
720
721 /* Check hotplug MSIx entry is in range */
722 pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag);
723 entry.entry = (pcie_flag & PCI_EXP_FLAGS_IRQ) >> 9;
724 if (entry.entry >= nr_entries)
725 return -ERANGE;
726
727 /* Enable MSIx */
728 ret = pci_enable_msix_exact(pdev, &entry, 1);
729 if (ret) {
730 pci_warn(pdev, "Error %d enabling MSIx\n", ret);
731 return ret;
732 }
733
734 return entry.vector;
735}
736
737static void pnv_php_event_handler(struct work_struct *work)
738{
739 struct pnv_php_event *event =
740 container_of(work, struct pnv_php_event, work);
741 struct pnv_php_slot *php_slot = event->php_slot;
742
743 if (event->added)
744 pnv_php_enable_slot(&php_slot->slot);
745 else
746 pnv_php_disable_slot(&php_slot->slot);
747
748 kfree(event);
749}
750
751static irqreturn_t pnv_php_interrupt(int irq, void *data)
752{
753 struct pnv_php_slot *php_slot = data;
754 struct pci_dev *pchild, *pdev = php_slot->pdev;
755 struct eeh_dev *edev;
756 struct eeh_pe *pe;
757 struct pnv_php_event *event;
758 u16 sts, lsts;
759 u8 presence;
760 bool added;
761 unsigned long flags;
762 int ret;
763
764 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
765 sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
766 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
767
768 pci_dbg(pdev, "PCI slot [%s]: HP int! DLAct: %d, PresDet: %d\n",
769 php_slot->name,
770 !!(sts & PCI_EXP_SLTSTA_DLLSC),
771 !!(sts & PCI_EXP_SLTSTA_PDC));
772
773 if (sts & PCI_EXP_SLTSTA_DLLSC) {
774 pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts);
775 added = !!(lsts & PCI_EXP_LNKSTA_DLLLA);
776 } else if (!(php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) &&
777 (sts & PCI_EXP_SLTSTA_PDC)) {
778 ret = pnv_pci_get_presence_state(php_slot->id, &presence);
779 if (ret) {
780 pci_warn(pdev, "PCI slot [%s] error %d getting presence (0x%04x), to retry the operation.\n",
781 php_slot->name, ret, sts);
782 return IRQ_HANDLED;
783 }
784
785 added = !!(presence == OPAL_PCI_SLOT_PRESENT);
786 } else {
787 pci_dbg(pdev, "PCI slot [%s]: Spurious IRQ?\n", php_slot->name);
788 return IRQ_NONE;
789 }
790
791 /* Freeze the removed PE to avoid unexpected error reporting */
792 if (!added) {
793 pchild = list_first_entry_or_null(&php_slot->bus->devices,
794 struct pci_dev, bus_list);
795 edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL;
796 pe = edev ? edev->pe : NULL;
797 if (pe) {
798 eeh_serialize_lock(&flags);
799 eeh_pe_mark_isolated(pe);
800 eeh_serialize_unlock(flags);
801 eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE);
802 }
803 }
804
805 /*
806 * The PE is left in frozen state if the event is missed. It's
807 * fine as the PCI devices (PE) aren't functional any more.
808 */
809 event = kzalloc(sizeof(*event), GFP_ATOMIC);
810 if (!event) {
811 pci_warn(pdev, "PCI slot [%s] missed hotplug event 0x%04x\n",
812 php_slot->name, sts);
813 return IRQ_HANDLED;
814 }
815
816 pci_info(pdev, "PCI slot [%s] %s (IRQ: %d)\n",
817 php_slot->name, added ? "added" : "removed", irq);
818 INIT_WORK(&event->work, pnv_php_event_handler);
819 event->added = added;
820 event->php_slot = php_slot;
821 queue_work(php_slot->wq, &event->work);
822
823 return IRQ_HANDLED;
824}
825
826static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq)
827{
828 struct pci_dev *pdev = php_slot->pdev;
829 u32 broken_pdc = 0;
830 u16 sts, ctrl;
831 int ret;
832
833 /* Allocate workqueue */
834 php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name);
835 if (!php_slot->wq) {
836 pci_warn(pdev, "Cannot alloc workqueue\n");
837 pnv_php_disable_irq(php_slot, true);
838 return;
839 }
840
841 /* Check PDC (Presence Detection Change) is broken or not */
842 ret = of_property_read_u32(php_slot->dn, "ibm,slot-broken-pdc",
843 &broken_pdc);
844 if (!ret && broken_pdc)
845 php_slot->flags |= PNV_PHP_FLAG_BROKEN_PDC;
846
847 /* Clear pending interrupts */
848 pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts);
849 if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC)
850 sts |= PCI_EXP_SLTSTA_DLLSC;
851 else
852 sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC);
853 pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts);
854
855 /* Request the interrupt */
856 ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED,
857 php_slot->name, php_slot);
858 if (ret) {
859 pnv_php_disable_irq(php_slot, true);
860 pci_warn(pdev, "Error %d enabling IRQ %d\n", ret, irq);
861 return;
862 }
863
864 /* Enable the interrupts */
865 pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl);
866 if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) {
867 ctrl &= ~PCI_EXP_SLTCTL_PDCE;
868 ctrl |= (PCI_EXP_SLTCTL_HPIE |
869 PCI_EXP_SLTCTL_DLLSCE);
870 } else {
871 ctrl |= (PCI_EXP_SLTCTL_HPIE |
872 PCI_EXP_SLTCTL_PDCE |
873 PCI_EXP_SLTCTL_DLLSCE);
874 }
875 pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl);
876
877 /* The interrupt is initialized successfully when @irq is valid */
878 php_slot->irq = irq;
879}
880
881static void pnv_php_enable_irq(struct pnv_php_slot *php_slot)
882{
883 struct pci_dev *pdev = php_slot->pdev;
884 int irq, ret;
885
886 /*
887 * The MSI/MSIx interrupt might have been occupied by other
888 * drivers. Don't populate the surprise hotplug capability
889 * in that case.
890 */
891 if (pci_dev_msi_enabled(pdev))
892 return;
893
894 ret = pci_enable_device(pdev);
895 if (ret) {
896 pci_warn(pdev, "Error %d enabling device\n", ret);
897 return;
898 }
899
900 pci_set_master(pdev);
901
902 /* Enable MSIx interrupt */
903 irq = pnv_php_enable_msix(php_slot);
904 if (irq > 0) {
905 pnv_php_init_irq(php_slot, irq);
906 return;
907 }
908
909 /*
910 * Use MSI if MSIx doesn't work. Fail back to legacy INTx
911 * if MSI doesn't work either
912 */
913 ret = pci_enable_msi(pdev);
914 if (!ret || pdev->irq) {
915 irq = pdev->irq;
916 pnv_php_init_irq(php_slot, irq);
917 }
918}
919
920static int pnv_php_register_one(struct device_node *dn)
921{
922 struct pnv_php_slot *php_slot;
923 u32 prop32;
924 int ret;
925
926 /* Check if it's hotpluggable slot */
927 ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32);
928 if (ret || !prop32)
929 return -ENXIO;
930
931 ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32);
932 if (ret || !prop32)
933 return -ENXIO;
934
935 php_slot = pnv_php_alloc_slot(dn);
936 if (!php_slot)
937 return -ENODEV;
938
939 ret = pnv_php_register_slot(php_slot);
940 if (ret)
941 goto free_slot;
942
943 ret = pnv_php_enable(php_slot, false);
944 if (ret)
945 goto unregister_slot;
946
947 /* Enable interrupt if the slot supports surprise hotplug */
948 ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32);
949 if (!ret && prop32)
950 pnv_php_enable_irq(php_slot);
951
952 return 0;
953
954unregister_slot:
955 pnv_php_unregister_one(php_slot->dn);
956free_slot:
957 pnv_php_put_slot(php_slot);
958 return ret;
959}
960
961static void pnv_php_register(struct device_node *dn)
962{
963 struct device_node *child;
964
965 /*
966 * The parent slots should be registered before their
967 * child slots.
968 */
969 for_each_child_of_node(dn, child) {
970 pnv_php_register_one(child);
971 pnv_php_register(child);
972 }
973}
974
975static void pnv_php_unregister_one(struct device_node *dn)
976{
977 struct pnv_php_slot *php_slot;
978
979 php_slot = pnv_php_find_slot(dn);
980 if (!php_slot)
981 return;
982
983 php_slot->state = PNV_PHP_STATE_OFFLINE;
984 pci_hp_deregister(&php_slot->slot);
985 pnv_php_release(php_slot);
986 pnv_php_put_slot(php_slot);
987}
988
989static void pnv_php_unregister(struct device_node *dn)
990{
991 struct device_node *child;
992
993 /* The child slots should go before their parent slots */
994 for_each_child_of_node(dn, child) {
995 pnv_php_unregister(child);
996 pnv_php_unregister_one(child);
997 }
998}
999
1000static int __init pnv_php_init(void)
1001{
1002 struct device_node *dn;
1003
1004 pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n");
1005 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
1006 pnv_php_register(dn);
1007
1008 for_each_compatible_node(dn, NULL, "ibm,ioda3-phb")
1009 pnv_php_register(dn);
1010
1011 return 0;
1012}
1013
1014static void __exit pnv_php_exit(void)
1015{
1016 struct device_node *dn;
1017
1018 for_each_compatible_node(dn, NULL, "ibm,ioda2-phb")
1019 pnv_php_unregister(dn);
1020
1021 for_each_compatible_node(dn, NULL, "ibm,ioda3-phb")
1022 pnv_php_unregister(dn);
1023}
1024
1025module_init(pnv_php_init);
1026module_exit(pnv_php_exit);
1027
1028MODULE_VERSION(DRIVER_VERSION);
1029MODULE_LICENSE("GPL v2");
1030MODULE_AUTHOR(DRIVER_AUTHOR);
1031MODULE_DESCRIPTION(DRIVER_DESC);