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