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