Commit | Line | Data |
---|---|---|
736759ef | 1 | // SPDX-License-Identifier: GPL-2.0+ |
66725152 GS |
2 | /* |
3 | * PCI Hotplug Driver for PowerPC PowerNV platform. | |
4 | * | |
5 | * Copyright Gavin Shan, IBM Corporation 2016. | |
66725152 GS |
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 | ||
360aebd8 GS |
21 | struct pnv_php_event { |
22 | bool added; | |
23 | struct pnv_php_slot *php_slot; | |
24 | struct work_struct work; | |
25 | }; | |
26 | ||
66725152 GS |
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 | ||
49f4b08e GS |
34 | static void pnv_php_disable_irq(struct pnv_php_slot *php_slot, |
35 | bool disable_device) | |
360aebd8 GS |
36 | { |
37 | struct pci_dev *pdev = php_slot->pdev; | |
49f4b08e | 38 | int irq = php_slot->irq; |
360aebd8 GS |
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 | ||
49f4b08e GS |
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 | } | |
360aebd8 GS |
65 | } |
66 | ||
66725152 GS |
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)); | |
49f4b08e | 73 | pnv_php_disable_irq(php_slot, false); |
66725152 GS |
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 | ||
36c7c9da | 81 | if (!php_slot) |
66725152 GS |
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 | ||
89379f16 | 106 | struct pnv_php_slot *pnv_php_find_slot(struct device_node *dn) |
66725152 GS |
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 | } | |
89379f16 | 123 | EXPORT_SYMBOL_GPL(pnv_php_find_slot); |
66725152 GS |
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); | |
2c935bc5 | 160 | refcount = kref_read(&dn->kobj.kref); |
149ba66a | 161 | if (refcount != 1) |
b63773a8 RH |
162 | pr_warn("Invalid refcount %d on <%pOF>\n", |
163 | refcount, dn); | |
66725152 GS |
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); | |
5d9c6b8a JL |
223 | if (ret) { |
224 | of_node_put(child); | |
66725152 | 225 | break; |
5d9c6b8a | 226 | } |
66725152 GS |
227 | |
228 | ret = pnv_php_populate_changeset(ocs, child); | |
5d9c6b8a JL |
229 | if (ret) { |
230 | of_node_put(child); | |
66725152 | 231 | break; |
5d9c6b8a | 232 | } |
66725152 GS |
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); | |
149ba66a | 244 | if (!pdn) |
66725152 GS |
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); | |
149ba66a | 267 | if (!fdt1) { |
66725152 | 268 | ret = -ENOMEM; |
66725152 GS |
269 | goto out; |
270 | } | |
271 | ||
272 | ret = pnv_pci_get_device_tree(php_slot->dn->phandle, fdt1, 0x10000); | |
149ba66a | 273 | if (ret) { |
7506dc79 | 274 | pci_warn(php_slot->pdev, "Error %d getting FDT blob\n", ret); |
66725152 GS |
275 | goto free_fdt1; |
276 | } | |
277 | ||
74171e9d | 278 | fdt = kmemdup(fdt1, fdt_totalsize(fdt1), GFP_KERNEL); |
149ba66a | 279 | if (!fdt) { |
66725152 | 280 | ret = -ENOMEM; |
66725152 GS |
281 | goto free_fdt1; |
282 | } | |
283 | ||
284 | /* Unflatten device tree blob */ | |
66725152 | 285 | dt = of_fdt_unflatten_tree(fdt, php_slot->dn, NULL); |
149ba66a | 286 | if (!dt) { |
66725152 | 287 | ret = -EINVAL; |
7506dc79 | 288 | pci_warn(php_slot->pdev, "Cannot unflatten FDT\n"); |
66725152 GS |
289 | goto free_fdt; |
290 | } | |
291 | ||
292 | /* Initialize and apply the changeset */ | |
293 | of_changeset_init(&php_slot->ocs); | |
294 | pnv_php_reverse_nodes(php_slot->dn); | |
295 | ret = pnv_php_populate_changeset(&php_slot->ocs, php_slot->dn); | |
149ba66a | 296 | if (ret) { |
66725152 | 297 | pnv_php_reverse_nodes(php_slot->dn); |
7506dc79 | 298 | pci_warn(php_slot->pdev, "Error %d populating changeset\n", |
66725152 GS |
299 | ret); |
300 | goto free_dt; | |
301 | } | |
302 | ||
303 | php_slot->dn->child = NULL; | |
304 | ret = of_changeset_apply(&php_slot->ocs); | |
149ba66a | 305 | if (ret) { |
7506dc79 | 306 | pci_warn(php_slot->pdev, "Error %d applying changeset\n", ret); |
66725152 GS |
307 | goto destroy_changeset; |
308 | } | |
309 | ||
310 | /* Add device node firmware data */ | |
311 | pnv_php_add_pdns(php_slot); | |
312 | php_slot->fdt = fdt; | |
313 | php_slot->dt = dt; | |
314 | kfree(fdt1); | |
315 | goto out; | |
316 | ||
317 | destroy_changeset: | |
318 | of_changeset_destroy(&php_slot->ocs); | |
319 | free_dt: | |
320 | kfree(dt); | |
321 | php_slot->dn->child = NULL; | |
322 | free_fdt: | |
323 | kfree(fdt); | |
324 | free_fdt1: | |
325 | kfree(fdt1); | |
326 | out: | |
327 | return ret; | |
328 | } | |
329 | ||
a7da2161 LW |
330 | static inline struct pnv_php_slot *to_pnv_php_slot(struct hotplug_slot *slot) |
331 | { | |
332 | return container_of(slot, struct pnv_php_slot, slot); | |
333 | } | |
334 | ||
89379f16 AD |
335 | int pnv_php_set_slot_power_state(struct hotplug_slot *slot, |
336 | uint8_t state) | |
66725152 | 337 | { |
125450f8 | 338 | struct pnv_php_slot *php_slot = to_pnv_php_slot(slot); |
66725152 GS |
339 | struct opal_msg msg; |
340 | int ret; | |
341 | ||
342 | ret = pnv_pci_set_power_state(php_slot->id, state, &msg); | |
149ba66a | 343 | if (ret > 0) { |
66725152 GS |
344 | if (be64_to_cpu(msg.params[1]) != php_slot->dn->phandle || |
345 | be64_to_cpu(msg.params[2]) != state || | |
346 | be64_to_cpu(msg.params[3]) != OPAL_SUCCESS) { | |
7506dc79 | 347 | pci_warn(php_slot->pdev, "Wrong msg (%lld, %lld, %lld)\n", |
66725152 GS |
348 | be64_to_cpu(msg.params[1]), |
349 | be64_to_cpu(msg.params[2]), | |
350 | be64_to_cpu(msg.params[3])); | |
351 | return -ENOMSG; | |
352 | } | |
149ba66a | 353 | } else if (ret < 0) { |
7506dc79 | 354 | pci_warn(php_slot->pdev, "Error %d powering %s\n", |
66725152 GS |
355 | ret, (state == OPAL_PCI_SLOT_POWER_ON) ? "on" : "off"); |
356 | return ret; | |
357 | } | |
358 | ||
5473a6bf | 359 | if (state == OPAL_PCI_SLOT_POWER_OFF || state == OPAL_PCI_SLOT_OFFLINE) |
66725152 GS |
360 | pnv_php_rmv_devtree(php_slot); |
361 | else | |
362 | ret = pnv_php_add_devtree(php_slot); | |
363 | ||
364 | return ret; | |
365 | } | |
89379f16 | 366 | EXPORT_SYMBOL_GPL(pnv_php_set_slot_power_state); |
66725152 GS |
367 | |
368 | static int pnv_php_get_power_state(struct hotplug_slot *slot, u8 *state) | |
369 | { | |
125450f8 | 370 | struct pnv_php_slot *php_slot = to_pnv_php_slot(slot); |
66725152 GS |
371 | uint8_t power_state = OPAL_PCI_SLOT_POWER_ON; |
372 | int ret; | |
373 | ||
374 | /* | |
375 | * Retrieve power status from firmware. If we fail | |
376 | * getting that, the power status fails back to | |
377 | * be on. | |
378 | */ | |
379 | ret = pnv_pci_get_power_state(php_slot->id, &power_state); | |
149ba66a | 380 | if (ret) { |
7506dc79 | 381 | pci_warn(php_slot->pdev, "Error %d getting power status\n", |
66725152 GS |
382 | ret); |
383 | } else { | |
384 | *state = power_state; | |
66725152 GS |
385 | } |
386 | ||
387 | return 0; | |
388 | } | |
389 | ||
390 | static int pnv_php_get_adapter_state(struct hotplug_slot *slot, u8 *state) | |
391 | { | |
125450f8 | 392 | struct pnv_php_slot *php_slot = to_pnv_php_slot(slot); |
66725152 GS |
393 | uint8_t presence = OPAL_PCI_SLOT_EMPTY; |
394 | int ret; | |
395 | ||
396 | /* | |
397 | * Retrieve presence status from firmware. If we can't | |
398 | * get that, it will fail back to be empty. | |
399 | */ | |
400 | ret = pnv_pci_get_presence_state(php_slot->id, &presence); | |
149ba66a | 401 | if (ret >= 0) { |
66725152 | 402 | *state = presence; |
66725152 GS |
403 | ret = 0; |
404 | } else { | |
7506dc79 | 405 | pci_warn(php_slot->pdev, "Error %d getting presence\n", ret); |
66725152 GS |
406 | } |
407 | ||
408 | return ret; | |
409 | } | |
410 | ||
a7da2161 LW |
411 | static int pnv_php_get_attention_state(struct hotplug_slot *slot, u8 *state) |
412 | { | |
413 | struct pnv_php_slot *php_slot = to_pnv_php_slot(slot); | |
414 | ||
415 | *state = php_slot->attention_state; | |
416 | return 0; | |
417 | } | |
418 | ||
66725152 GS |
419 | static int pnv_php_set_attention_state(struct hotplug_slot *slot, u8 state) |
420 | { | |
a7da2161 LW |
421 | struct pnv_php_slot *php_slot = to_pnv_php_slot(slot); |
422 | ||
66725152 | 423 | /* FIXME: Make it real once firmware supports it */ |
a7da2161 | 424 | php_slot->attention_state = state; |
66725152 GS |
425 | |
426 | return 0; | |
427 | } | |
428 | ||
429 | static int pnv_php_enable(struct pnv_php_slot *php_slot, bool rescan) | |
430 | { | |
431 | struct hotplug_slot *slot = &php_slot->slot; | |
432 | uint8_t presence = OPAL_PCI_SLOT_EMPTY; | |
433 | uint8_t power_status = OPAL_PCI_SLOT_POWER_ON; | |
434 | int ret; | |
435 | ||
436 | /* Check if the slot has been configured */ | |
437 | if (php_slot->state != PNV_PHP_STATE_REGISTERED) | |
438 | return 0; | |
439 | ||
440 | /* Retrieve slot presence status */ | |
441 | ret = pnv_php_get_adapter_state(slot, &presence); | |
149ba66a | 442 | if (ret) |
66725152 GS |
443 | return ret; |
444 | ||
d0c42497 GS |
445 | /* |
446 | * Proceed if there have nothing behind the slot. However, | |
447 | * we should leave the slot in registered state at the | |
448 | * beginning. Otherwise, the PCI devices inserted afterwards | |
449 | * won't be probed and populated. | |
450 | */ | |
451 | if (presence == OPAL_PCI_SLOT_EMPTY) { | |
452 | if (!php_slot->power_state_check) { | |
453 | php_slot->power_state_check = true; | |
454 | ||
455 | return 0; | |
456 | } | |
457 | ||
66725152 | 458 | goto scan; |
d0c42497 | 459 | } |
66725152 GS |
460 | |
461 | /* | |
462 | * If the power supply to the slot is off, we can't detect | |
463 | * adapter presence state. That means we have to turn the | |
464 | * slot on before going to probe slot's presence state. | |
465 | * | |
466 | * On the first time, we don't change the power status to | |
467 | * boost system boot with assumption that the firmware | |
468 | * supplies consistent slot power status: empty slot always | |
469 | * has its power off and non-empty slot has its power on. | |
470 | */ | |
471 | if (!php_slot->power_state_check) { | |
472 | php_slot->power_state_check = true; | |
473 | ||
474 | ret = pnv_php_get_power_state(slot, &power_status); | |
149ba66a | 475 | if (ret) |
66725152 GS |
476 | return ret; |
477 | ||
478 | if (power_status != OPAL_PCI_SLOT_POWER_ON) | |
479 | return 0; | |
480 | } | |
481 | ||
482 | /* Check the power status. Scan the slot if it is already on */ | |
483 | ret = pnv_php_get_power_state(slot, &power_status); | |
149ba66a | 484 | if (ret) |
66725152 GS |
485 | return ret; |
486 | ||
487 | if (power_status == OPAL_PCI_SLOT_POWER_ON) | |
488 | goto scan; | |
489 | ||
490 | /* Power is off, turn it on and then scan the slot */ | |
491 | ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_ON); | |
149ba66a | 492 | if (ret) |
66725152 GS |
493 | return ret; |
494 | ||
495 | scan: | |
496 | if (presence == OPAL_PCI_SLOT_PRESENT) { | |
497 | if (rescan) { | |
498 | pci_lock_rescan_remove(); | |
499 | pci_hp_add_devices(php_slot->bus); | |
500 | pci_unlock_rescan_remove(); | |
501 | } | |
502 | ||
503 | /* Rescan for child hotpluggable slots */ | |
504 | php_slot->state = PNV_PHP_STATE_POPULATED; | |
505 | if (rescan) | |
506 | pnv_php_register(php_slot->dn); | |
507 | } else { | |
508 | php_slot->state = PNV_PHP_STATE_POPULATED; | |
509 | } | |
510 | ||
511 | return 0; | |
512 | } | |
513 | ||
514 | static int pnv_php_enable_slot(struct hotplug_slot *slot) | |
515 | { | |
a7da2161 | 516 | struct pnv_php_slot *php_slot = to_pnv_php_slot(slot); |
66725152 GS |
517 | |
518 | return pnv_php_enable(php_slot, true); | |
519 | } | |
520 | ||
521 | static int pnv_php_disable_slot(struct hotplug_slot *slot) | |
522 | { | |
125450f8 | 523 | struct pnv_php_slot *php_slot = to_pnv_php_slot(slot); |
66725152 GS |
524 | int ret; |
525 | ||
526 | if (php_slot->state != PNV_PHP_STATE_POPULATED) | |
527 | return 0; | |
528 | ||
529 | /* Remove all devices behind the slot */ | |
530 | pci_lock_rescan_remove(); | |
531 | pci_hp_remove_devices(php_slot->bus); | |
532 | pci_unlock_rescan_remove(); | |
533 | ||
534 | /* Detach the child hotpluggable slots */ | |
535 | pnv_php_unregister(php_slot->dn); | |
536 | ||
537 | /* Notify firmware and remove device nodes */ | |
538 | ret = pnv_php_set_slot_power_state(slot, OPAL_PCI_SLOT_POWER_OFF); | |
539 | ||
540 | php_slot->state = PNV_PHP_STATE_REGISTERED; | |
541 | return ret; | |
542 | } | |
543 | ||
81c4b5bf | 544 | static const struct hotplug_slot_ops php_slot_ops = { |
66725152 GS |
545 | .get_power_status = pnv_php_get_power_state, |
546 | .get_adapter_status = pnv_php_get_adapter_state, | |
a7da2161 | 547 | .get_attention_status = pnv_php_get_attention_state, |
66725152 GS |
548 | .set_attention_status = pnv_php_set_attention_state, |
549 | .enable_slot = pnv_php_enable_slot, | |
550 | .disable_slot = pnv_php_disable_slot, | |
551 | }; | |
552 | ||
51bbf9be | 553 | static void pnv_php_release(struct pnv_php_slot *php_slot) |
66725152 | 554 | { |
66725152 GS |
555 | unsigned long flags; |
556 | ||
557 | /* Remove from global or child list */ | |
558 | spin_lock_irqsave(&pnv_php_lock, flags); | |
559 | list_del(&php_slot->link); | |
560 | spin_unlock_irqrestore(&pnv_php_lock, flags); | |
561 | ||
562 | /* Detach from parent */ | |
563 | pnv_php_put_slot(php_slot); | |
564 | pnv_php_put_slot(php_slot->parent); | |
565 | } | |
566 | ||
567 | static struct pnv_php_slot *pnv_php_alloc_slot(struct device_node *dn) | |
568 | { | |
569 | struct pnv_php_slot *php_slot; | |
570 | struct pci_bus *bus; | |
571 | const char *label; | |
572 | uint64_t id; | |
39f0d6fb | 573 | int ret; |
66725152 | 574 | |
39f0d6fb GS |
575 | ret = of_property_read_string(dn, "ibm,slot-label", &label); |
576 | if (ret) | |
66725152 GS |
577 | return NULL; |
578 | ||
579 | if (pnv_pci_get_slot_id(dn, &id)) | |
580 | return NULL; | |
581 | ||
582 | bus = pci_find_bus_by_node(dn); | |
149ba66a | 583 | if (!bus) |
66725152 GS |
584 | return NULL; |
585 | ||
586 | php_slot = kzalloc(sizeof(*php_slot), GFP_KERNEL); | |
149ba66a | 587 | if (!php_slot) |
66725152 GS |
588 | return NULL; |
589 | ||
590 | php_slot->name = kstrdup(label, GFP_KERNEL); | |
149ba66a | 591 | if (!php_slot->name) { |
66725152 GS |
592 | kfree(php_slot); |
593 | return NULL; | |
594 | } | |
595 | ||
149ba66a | 596 | if (dn->child && PCI_DN(dn->child)) |
66725152 GS |
597 | php_slot->slot_no = PCI_SLOT(PCI_DN(dn->child)->devfn); |
598 | else | |
599 | php_slot->slot_no = -1; /* Placeholder slot */ | |
600 | ||
601 | kref_init(&php_slot->kref); | |
602 | php_slot->state = PNV_PHP_STATE_INITIALIZED; | |
603 | php_slot->dn = dn; | |
604 | php_slot->pdev = bus->self; | |
605 | php_slot->bus = bus; | |
606 | php_slot->id = id; | |
607 | php_slot->power_state_check = false; | |
608 | php_slot->slot.ops = &php_slot_ops; | |
66725152 GS |
609 | |
610 | INIT_LIST_HEAD(&php_slot->children); | |
611 | INIT_LIST_HEAD(&php_slot->link); | |
612 | ||
613 | return php_slot; | |
614 | } | |
615 | ||
616 | static int pnv_php_register_slot(struct pnv_php_slot *php_slot) | |
617 | { | |
618 | struct pnv_php_slot *parent; | |
619 | struct device_node *dn = php_slot->dn; | |
620 | unsigned long flags; | |
621 | int ret; | |
622 | ||
623 | /* Check if the slot is registered or not */ | |
624 | parent = pnv_php_find_slot(php_slot->dn); | |
149ba66a | 625 | if (parent) { |
66725152 GS |
626 | pnv_php_put_slot(parent); |
627 | return -EEXIST; | |
628 | } | |
629 | ||
630 | /* Register PCI slot */ | |
631 | ret = pci_hp_register(&php_slot->slot, php_slot->bus, | |
632 | php_slot->slot_no, php_slot->name); | |
149ba66a | 633 | if (ret) { |
7506dc79 | 634 | pci_warn(php_slot->pdev, "Error %d registering slot\n", ret); |
66725152 GS |
635 | return ret; |
636 | } | |
637 | ||
638 | /* Attach to the parent's child list or global list */ | |
639 | while ((dn = of_get_parent(dn))) { | |
640 | if (!PCI_DN(dn)) { | |
641 | of_node_put(dn); | |
642 | break; | |
643 | } | |
644 | ||
645 | parent = pnv_php_find_slot(dn); | |
646 | if (parent) { | |
647 | of_node_put(dn); | |
648 | break; | |
649 | } | |
650 | ||
651 | of_node_put(dn); | |
652 | } | |
653 | ||
654 | spin_lock_irqsave(&pnv_php_lock, flags); | |
655 | php_slot->parent = parent; | |
656 | if (parent) | |
657 | list_add_tail(&php_slot->link, &parent->children); | |
658 | else | |
659 | list_add_tail(&php_slot->link, &pnv_php_slot_list); | |
660 | spin_unlock_irqrestore(&pnv_php_lock, flags); | |
661 | ||
662 | php_slot->state = PNV_PHP_STATE_REGISTERED; | |
663 | return 0; | |
664 | } | |
665 | ||
360aebd8 GS |
666 | static int pnv_php_enable_msix(struct pnv_php_slot *php_slot) |
667 | { | |
668 | struct pci_dev *pdev = php_slot->pdev; | |
669 | struct msix_entry entry; | |
670 | int nr_entries, ret; | |
671 | u16 pcie_flag; | |
672 | ||
673 | /* Get total number of MSIx entries */ | |
674 | nr_entries = pci_msix_vec_count(pdev); | |
675 | if (nr_entries < 0) | |
676 | return nr_entries; | |
677 | ||
678 | /* Check hotplug MSIx entry is in range */ | |
679 | pcie_capability_read_word(pdev, PCI_EXP_FLAGS, &pcie_flag); | |
680 | entry.entry = (pcie_flag & PCI_EXP_FLAGS_IRQ) >> 9; | |
681 | if (entry.entry >= nr_entries) | |
682 | return -ERANGE; | |
683 | ||
684 | /* Enable MSIx */ | |
685 | ret = pci_enable_msix_exact(pdev, &entry, 1); | |
686 | if (ret) { | |
7506dc79 | 687 | pci_warn(pdev, "Error %d enabling MSIx\n", ret); |
360aebd8 GS |
688 | return ret; |
689 | } | |
690 | ||
691 | return entry.vector; | |
692 | } | |
693 | ||
694 | static void pnv_php_event_handler(struct work_struct *work) | |
695 | { | |
696 | struct pnv_php_event *event = | |
697 | container_of(work, struct pnv_php_event, work); | |
698 | struct pnv_php_slot *php_slot = event->php_slot; | |
699 | ||
700 | if (event->added) | |
701 | pnv_php_enable_slot(&php_slot->slot); | |
702 | else | |
703 | pnv_php_disable_slot(&php_slot->slot); | |
704 | ||
705 | kfree(event); | |
706 | } | |
707 | ||
708 | static irqreturn_t pnv_php_interrupt(int irq, void *data) | |
709 | { | |
710 | struct pnv_php_slot *php_slot = data; | |
711 | struct pci_dev *pchild, *pdev = php_slot->pdev; | |
712 | struct eeh_dev *edev; | |
713 | struct eeh_pe *pe; | |
714 | struct pnv_php_event *event; | |
715 | u16 sts, lsts; | |
716 | u8 presence; | |
717 | bool added; | |
718 | unsigned long flags; | |
719 | int ret; | |
720 | ||
721 | pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts); | |
722 | sts &= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC); | |
723 | pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts); | |
724 | if (sts & PCI_EXP_SLTSTA_DLLSC) { | |
725 | pcie_capability_read_word(pdev, PCI_EXP_LNKSTA, &lsts); | |
726 | added = !!(lsts & PCI_EXP_LNKSTA_DLLLA); | |
454593e5 GS |
727 | } else if (!(php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) && |
728 | (sts & PCI_EXP_SLTSTA_PDC)) { | |
360aebd8 | 729 | ret = pnv_pci_get_presence_state(php_slot->id, &presence); |
d7d55536 | 730 | if (ret) { |
7506dc79 | 731 | pci_warn(pdev, "PCI slot [%s] error %d getting presence (0x%04x), to retry the operation.\n", |
d7d55536 | 732 | php_slot->name, ret, sts); |
360aebd8 | 733 | return IRQ_HANDLED; |
d7d55536 GS |
734 | } |
735 | ||
360aebd8 GS |
736 | added = !!(presence == OPAL_PCI_SLOT_PRESENT); |
737 | } else { | |
738 | return IRQ_NONE; | |
739 | } | |
740 | ||
741 | /* Freeze the removed PE to avoid unexpected error reporting */ | |
742 | if (!added) { | |
743 | pchild = list_first_entry_or_null(&php_slot->bus->devices, | |
744 | struct pci_dev, bus_list); | |
745 | edev = pchild ? pci_dev_to_eeh_dev(pchild) : NULL; | |
746 | pe = edev ? edev->pe : NULL; | |
747 | if (pe) { | |
748 | eeh_serialize_lock(&flags); | |
e762bb89 | 749 | eeh_pe_mark_isolated(pe); |
360aebd8 GS |
750 | eeh_serialize_unlock(flags); |
751 | eeh_pe_set_option(pe, EEH_OPT_FREEZE_PE); | |
752 | } | |
753 | } | |
754 | ||
755 | /* | |
756 | * The PE is left in frozen state if the event is missed. It's | |
757 | * fine as the PCI devices (PE) aren't functional any more. | |
758 | */ | |
759 | event = kzalloc(sizeof(*event), GFP_ATOMIC); | |
760 | if (!event) { | |
7506dc79 | 761 | pci_warn(pdev, "PCI slot [%s] missed hotplug event 0x%04x\n", |
360aebd8 GS |
762 | php_slot->name, sts); |
763 | return IRQ_HANDLED; | |
764 | } | |
765 | ||
7506dc79 | 766 | pci_info(pdev, "PCI slot [%s] %s (IRQ: %d)\n", |
360aebd8 GS |
767 | php_slot->name, added ? "added" : "removed", irq); |
768 | INIT_WORK(&event->work, pnv_php_event_handler); | |
769 | event->added = added; | |
770 | event->php_slot = php_slot; | |
771 | queue_work(php_slot->wq, &event->work); | |
772 | ||
773 | return IRQ_HANDLED; | |
774 | } | |
775 | ||
776 | static void pnv_php_init_irq(struct pnv_php_slot *php_slot, int irq) | |
777 | { | |
778 | struct pci_dev *pdev = php_slot->pdev; | |
454593e5 | 779 | u32 broken_pdc = 0; |
360aebd8 GS |
780 | u16 sts, ctrl; |
781 | int ret; | |
782 | ||
783 | /* Allocate workqueue */ | |
784 | php_slot->wq = alloc_workqueue("pciehp-%s", 0, 0, php_slot->name); | |
785 | if (!php_slot->wq) { | |
7506dc79 | 786 | pci_warn(pdev, "Cannot alloc workqueue\n"); |
49f4b08e | 787 | pnv_php_disable_irq(php_slot, true); |
360aebd8 GS |
788 | return; |
789 | } | |
790 | ||
454593e5 GS |
791 | /* Check PDC (Presence Detection Change) is broken or not */ |
792 | ret = of_property_read_u32(php_slot->dn, "ibm,slot-broken-pdc", | |
793 | &broken_pdc); | |
794 | if (!ret && broken_pdc) | |
795 | php_slot->flags |= PNV_PHP_FLAG_BROKEN_PDC; | |
796 | ||
360aebd8 GS |
797 | /* Clear pending interrupts */ |
798 | pcie_capability_read_word(pdev, PCI_EXP_SLTSTA, &sts); | |
454593e5 GS |
799 | if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) |
800 | sts |= PCI_EXP_SLTSTA_DLLSC; | |
801 | else | |
802 | sts |= (PCI_EXP_SLTSTA_PDC | PCI_EXP_SLTSTA_DLLSC); | |
360aebd8 GS |
803 | pcie_capability_write_word(pdev, PCI_EXP_SLTSTA, sts); |
804 | ||
805 | /* Request the interrupt */ | |
806 | ret = request_irq(irq, pnv_php_interrupt, IRQF_SHARED, | |
807 | php_slot->name, php_slot); | |
808 | if (ret) { | |
49f4b08e | 809 | pnv_php_disable_irq(php_slot, true); |
7506dc79 | 810 | pci_warn(pdev, "Error %d enabling IRQ %d\n", ret, irq); |
360aebd8 GS |
811 | return; |
812 | } | |
813 | ||
814 | /* Enable the interrupts */ | |
815 | pcie_capability_read_word(pdev, PCI_EXP_SLTCTL, &ctrl); | |
454593e5 GS |
816 | if (php_slot->flags & PNV_PHP_FLAG_BROKEN_PDC) { |
817 | ctrl &= ~PCI_EXP_SLTCTL_PDCE; | |
818 | ctrl |= (PCI_EXP_SLTCTL_HPIE | | |
819 | PCI_EXP_SLTCTL_DLLSCE); | |
820 | } else { | |
821 | ctrl |= (PCI_EXP_SLTCTL_HPIE | | |
822 | PCI_EXP_SLTCTL_PDCE | | |
823 | PCI_EXP_SLTCTL_DLLSCE); | |
824 | } | |
360aebd8 GS |
825 | pcie_capability_write_word(pdev, PCI_EXP_SLTCTL, ctrl); |
826 | ||
827 | /* The interrupt is initialized successfully when @irq is valid */ | |
828 | php_slot->irq = irq; | |
829 | } | |
830 | ||
831 | static void pnv_php_enable_irq(struct pnv_php_slot *php_slot) | |
832 | { | |
833 | struct pci_dev *pdev = php_slot->pdev; | |
834 | int irq, ret; | |
835 | ||
303529d6 GS |
836 | /* |
837 | * The MSI/MSIx interrupt might have been occupied by other | |
838 | * drivers. Don't populate the surprise hotplug capability | |
839 | * in that case. | |
840 | */ | |
841 | if (pci_dev_msi_enabled(pdev)) | |
842 | return; | |
843 | ||
360aebd8 GS |
844 | ret = pci_enable_device(pdev); |
845 | if (ret) { | |
7506dc79 | 846 | pci_warn(pdev, "Error %d enabling device\n", ret); |
360aebd8 GS |
847 | return; |
848 | } | |
849 | ||
850 | pci_set_master(pdev); | |
851 | ||
852 | /* Enable MSIx interrupt */ | |
853 | irq = pnv_php_enable_msix(php_slot); | |
854 | if (irq > 0) { | |
855 | pnv_php_init_irq(php_slot, irq); | |
856 | return; | |
857 | } | |
858 | ||
859 | /* | |
860 | * Use MSI if MSIx doesn't work. Fail back to legacy INTx | |
861 | * if MSI doesn't work either | |
862 | */ | |
863 | ret = pci_enable_msi(pdev); | |
864 | if (!ret || pdev->irq) { | |
865 | irq = pdev->irq; | |
866 | pnv_php_init_irq(php_slot, irq); | |
867 | } | |
868 | } | |
869 | ||
66725152 GS |
870 | static int pnv_php_register_one(struct device_node *dn) |
871 | { | |
872 | struct pnv_php_slot *php_slot; | |
39f0d6fb | 873 | u32 prop32; |
66725152 GS |
874 | int ret; |
875 | ||
876 | /* Check if it's hotpluggable slot */ | |
39f0d6fb GS |
877 | ret = of_property_read_u32(dn, "ibm,slot-pluggable", &prop32); |
878 | if (ret || !prop32) | |
66725152 GS |
879 | return -ENXIO; |
880 | ||
39f0d6fb GS |
881 | ret = of_property_read_u32(dn, "ibm,reset-by-firmware", &prop32); |
882 | if (ret || !prop32) | |
66725152 GS |
883 | return -ENXIO; |
884 | ||
885 | php_slot = pnv_php_alloc_slot(dn); | |
149ba66a | 886 | if (!php_slot) |
66725152 GS |
887 | return -ENODEV; |
888 | ||
889 | ret = pnv_php_register_slot(php_slot); | |
149ba66a | 890 | if (ret) |
66725152 GS |
891 | goto free_slot; |
892 | ||
893 | ret = pnv_php_enable(php_slot, false); | |
149ba66a | 894 | if (ret) |
66725152 GS |
895 | goto unregister_slot; |
896 | ||
360aebd8 | 897 | /* Enable interrupt if the slot supports surprise hotplug */ |
39f0d6fb GS |
898 | ret = of_property_read_u32(dn, "ibm,slot-surprise-pluggable", &prop32); |
899 | if (!ret && prop32) | |
360aebd8 GS |
900 | pnv_php_enable_irq(php_slot); |
901 | ||
66725152 GS |
902 | return 0; |
903 | ||
904 | unregister_slot: | |
905 | pnv_php_unregister_one(php_slot->dn); | |
906 | free_slot: | |
907 | pnv_php_put_slot(php_slot); | |
908 | return ret; | |
909 | } | |
910 | ||
911 | static void pnv_php_register(struct device_node *dn) | |
912 | { | |
913 | struct device_node *child; | |
914 | ||
915 | /* | |
916 | * The parent slots should be registered before their | |
917 | * child slots. | |
918 | */ | |
919 | for_each_child_of_node(dn, child) { | |
920 | pnv_php_register_one(child); | |
921 | pnv_php_register(child); | |
922 | } | |
923 | } | |
924 | ||
925 | static void pnv_php_unregister_one(struct device_node *dn) | |
926 | { | |
927 | struct pnv_php_slot *php_slot; | |
928 | ||
929 | php_slot = pnv_php_find_slot(dn); | |
930 | if (!php_slot) | |
931 | return; | |
932 | ||
933 | php_slot->state = PNV_PHP_STATE_OFFLINE; | |
66725152 | 934 | pci_hp_deregister(&php_slot->slot); |
51bbf9be | 935 | pnv_php_release(php_slot); |
97c6f25d | 936 | pnv_php_put_slot(php_slot); |
66725152 GS |
937 | } |
938 | ||
939 | static void pnv_php_unregister(struct device_node *dn) | |
940 | { | |
941 | struct device_node *child; | |
942 | ||
943 | /* The child slots should go before their parent slots */ | |
944 | for_each_child_of_node(dn, child) { | |
945 | pnv_php_unregister(child); | |
946 | pnv_php_unregister_one(child); | |
947 | } | |
948 | } | |
949 | ||
950 | static int __init pnv_php_init(void) | |
951 | { | |
952 | struct device_node *dn; | |
953 | ||
954 | pr_info(DRIVER_DESC " version: " DRIVER_VERSION "\n"); | |
955 | for_each_compatible_node(dn, NULL, "ibm,ioda2-phb") | |
956 | pnv_php_register(dn); | |
957 | ||
958 | return 0; | |
959 | } | |
960 | ||
961 | static void __exit pnv_php_exit(void) | |
962 | { | |
963 | struct device_node *dn; | |
964 | ||
965 | for_each_compatible_node(dn, NULL, "ibm,ioda2-phb") | |
966 | pnv_php_unregister(dn); | |
967 | } | |
968 | ||
969 | module_init(pnv_php_init); | |
970 | module_exit(pnv_php_exit); | |
971 | ||
972 | MODULE_VERSION(DRIVER_VERSION); | |
973 | MODULE_LICENSE("GPL v2"); | |
974 | MODULE_AUTHOR(DRIVER_AUTHOR); | |
975 | MODULE_DESCRIPTION(DRIVER_DESC); |