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