1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Purpose:	PCI Express Port Bus Driver's Core Functions
4  *
5  * Copyright (C) 2004 Intel
6  * Copyright (C) Tom Long Nguyen (tom.l.nguyen@intel.com)
7  */
8 
9 #include <linux/module.h>
10 #include <linux/pci.h>
11 #include <linux/kernel.h>
12 #include <linux/errno.h>
13 #include <linux/pm.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/string.h>
16 #include <linux/slab.h>
17 #include <linux/aer.h>
18 
19 #include "../pci.h"
20 #include "portdrv.h"
21 
22 struct portdrv_service_data {
23 	struct pcie_port_service_driver *drv;
24 	struct device *dev;
25 	u32 service;
26 };
27 
28 /**
29  * release_pcie_device - free PCI Express port service device structure
30  * @dev: Port service device to release
31  *
32  * Invoked automatically when device is being removed in response to
33  * device_unregister(dev).  Release all resources being claimed.
34  */
release_pcie_device(struct device * dev)35 static void release_pcie_device(struct device *dev)
36 {
37 	kfree(to_pcie_device(dev));
38 }
39 
40 /*
41  * Fill in *pme, *aer, *dpc with the relevant Interrupt Message Numbers if
42  * services are enabled in "mask".  Return the number of MSI/MSI-X vectors
43  * required to accommodate the largest Message Number.
44  */
pcie_message_numbers(struct pci_dev * dev,int mask,u32 * pme,u32 * aer,u32 * dpc)45 static int pcie_message_numbers(struct pci_dev *dev, int mask,
46 				u32 *pme, u32 *aer, u32 *dpc)
47 {
48 	u32 nvec = 0, pos;
49 	u16 reg16;
50 
51 	/*
52 	 * The Interrupt Message Number indicates which vector is used, i.e.,
53 	 * the MSI-X table entry or the MSI offset between the base Message
54 	 * Data and the generated interrupt message.  See PCIe r3.1, sec
55 	 * 7.8.2, 7.10.10, 7.31.2.
56 	 */
57 
58 	if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
59 		    PCIE_PORT_SERVICE_BWNOTIF)) {
60 		pcie_capability_read_word(dev, PCI_EXP_FLAGS, &reg16);
61 		*pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
62 		nvec = *pme + 1;
63 	}
64 
65 #ifdef CONFIG_PCIEAER
66 	if (mask & PCIE_PORT_SERVICE_AER) {
67 		u32 reg32;
68 
69 		pos = dev->aer_cap;
70 		if (pos) {
71 			pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
72 					      &reg32);
73 			*aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
74 			nvec = max(nvec, *aer + 1);
75 		}
76 	}
77 #endif
78 
79 	if (mask & PCIE_PORT_SERVICE_DPC) {
80 		pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
81 		if (pos) {
82 			pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
83 					     &reg16);
84 			*dpc = reg16 & PCI_EXP_DPC_IRQ;
85 			nvec = max(nvec, *dpc + 1);
86 		}
87 	}
88 
89 	return nvec;
90 }
91 
92 /**
93  * pcie_port_enable_irq_vec - try to set up MSI-X or MSI as interrupt mode
94  * for given port
95  * @dev: PCI Express port to handle
96  * @irqs: Array of interrupt vectors to populate
97  * @mask: Bitmask of port capabilities returned by get_port_device_capability()
98  *
99  * Return value: 0 on success, error code on failure
100  */
pcie_port_enable_irq_vec(struct pci_dev * dev,int * irqs,int mask)101 static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
102 {
103 	int nr_entries, nvec, pcie_irq;
104 	u32 pme = 0, aer = 0, dpc = 0;
105 
106 	/* Allocate the maximum possible number of MSI/MSI-X vectors */
107 	nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
108 			PCI_IRQ_MSIX | PCI_IRQ_MSI);
109 	if (nr_entries < 0)
110 		return nr_entries;
111 
112 	/* See how many and which Interrupt Message Numbers we actually use */
113 	nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
114 	if (nvec > nr_entries) {
115 		pci_free_irq_vectors(dev);
116 		return -EIO;
117 	}
118 
119 	/*
120 	 * If we allocated more than we need, free them and reallocate fewer.
121 	 *
122 	 * Reallocating may change the specific vectors we get, so
123 	 * pci_irq_vector() must be done *after* the reallocation.
124 	 *
125 	 * If we're using MSI, hardware is *allowed* to change the Interrupt
126 	 * Message Numbers when we free and reallocate the vectors, but we
127 	 * assume it won't because we allocate enough vectors for the
128 	 * biggest Message Number we found.
129 	 */
130 	if (nvec != nr_entries) {
131 		pci_free_irq_vectors(dev);
132 
133 		nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
134 				PCI_IRQ_MSIX | PCI_IRQ_MSI);
135 		if (nr_entries < 0)
136 			return nr_entries;
137 	}
138 
139 	/* PME, hotplug and bandwidth notification share an MSI/MSI-X vector */
140 	if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP |
141 		    PCIE_PORT_SERVICE_BWNOTIF)) {
142 		pcie_irq = pci_irq_vector(dev, pme);
143 		irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pcie_irq;
144 		irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pcie_irq;
145 		irqs[PCIE_PORT_SERVICE_BWNOTIF_SHIFT] = pcie_irq;
146 	}
147 
148 	if (mask & PCIE_PORT_SERVICE_AER)
149 		irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
150 
151 	if (mask & PCIE_PORT_SERVICE_DPC)
152 		irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
153 
154 	return 0;
155 }
156 
157 /**
158  * pcie_init_service_irqs - initialize irqs for PCI Express port services
159  * @dev: PCI Express port to handle
160  * @irqs: Array of irqs to populate
161  * @mask: Bitmask of port capabilities returned by get_port_device_capability()
162  *
163  * Return value: Interrupt mode associated with the port
164  */
pcie_init_service_irqs(struct pci_dev * dev,int * irqs,int mask)165 static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
166 {
167 	int ret, i;
168 
169 	/*
170 	 * If we support PME but can't use MSI/MSI-X for it, we have to
171 	 * fall back to INTx or other interrupts, e.g., a system shared
172 	 * interrupt.
173 	 */
174 	if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
175 		goto legacy_irq;
176 
177 	/* Try to use MSI-X or MSI if supported */
178 	if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
179 		return 0;
180 
181 legacy_irq:
182 	/* fall back to legacy IRQ */
183 	ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
184 	if (ret < 0)
185 		return -ENODEV;
186 
187 	for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
188 		irqs[i] = pci_irq_vector(dev, 0);
189 
190 	return 0;
191 }
192 
193 /**
194  * get_port_device_capability - discover capabilities of a PCI Express port
195  * @dev: PCI Express port to examine
196  *
197  * The capabilities are read from the port's PCI Express configuration registers
198  * as described in PCI Express Base Specification 1.0a sections 7.8.2, 7.8.9 and
199  * 7.9 - 7.11.
200  *
201  * Return value: Bitmask of discovered port capabilities
202  */
get_port_device_capability(struct pci_dev * dev)203 static int get_port_device_capability(struct pci_dev *dev)
204 {
205 	struct pci_host_bridge *host = pci_find_host_bridge(dev->bus);
206 	int services = 0;
207 
208 	if (dev->is_hotplug_bridge &&
209 	    (pcie_ports_native || host->native_pcie_hotplug)) {
210 		services |= PCIE_PORT_SERVICE_HP;
211 
212 		/*
213 		 * Disable hot-plug interrupts in case they have been enabled
214 		 * by the BIOS and the hot-plug service driver is not loaded.
215 		 */
216 		pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
217 			  PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
218 	}
219 
220 #ifdef CONFIG_PCIEAER
221 	if (dev->aer_cap && pci_aer_available() &&
222 	    (pcie_ports_native || host->native_aer)) {
223 		services |= PCIE_PORT_SERVICE_AER;
224 
225 		/*
226 		 * Disable AER on this port in case it's been enabled by the
227 		 * BIOS (the AER service driver will enable it when necessary).
228 		 */
229 		pci_disable_pcie_error_reporting(dev);
230 	}
231 #endif
232 
233 	/* Root Ports and Root Complex Event Collectors may generate PMEs */
234 	if ((pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT ||
235 	     pci_pcie_type(dev) == PCI_EXP_TYPE_RC_EC) &&
236 	    (pcie_ports_native || host->native_pme)) {
237 		services |= PCIE_PORT_SERVICE_PME;
238 
239 		/*
240 		 * Disable PME interrupt on this port in case it's been enabled
241 		 * by the BIOS (the PME service driver will enable it when
242 		 * necessary).
243 		 */
244 		pcie_pme_interrupt_enable(dev, false);
245 	}
246 
247 	/*
248 	 * With dpc-native, allow Linux to use DPC even if it doesn't have
249 	 * permission to use AER.
250 	 */
251 	if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC) &&
252 	    pci_aer_available() &&
253 	    (pcie_ports_dpc_native || (services & PCIE_PORT_SERVICE_AER)))
254 		services |= PCIE_PORT_SERVICE_DPC;
255 
256 	if (pci_pcie_type(dev) == PCI_EXP_TYPE_DOWNSTREAM ||
257 	    pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
258 		u32 linkcap;
259 
260 		pcie_capability_read_dword(dev, PCI_EXP_LNKCAP, &linkcap);
261 		if (linkcap & PCI_EXP_LNKCAP_LBNC)
262 			services |= PCIE_PORT_SERVICE_BWNOTIF;
263 	}
264 
265 	return services;
266 }
267 
268 /**
269  * pcie_device_init - allocate and initialize PCI Express port service device
270  * @pdev: PCI Express port to associate the service device with
271  * @service: Type of service to associate with the service device
272  * @irq: Interrupt vector to associate with the service device
273  */
pcie_device_init(struct pci_dev * pdev,int service,int irq)274 static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
275 {
276 	int retval;
277 	struct pcie_device *pcie;
278 	struct device *device;
279 
280 	pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
281 	if (!pcie)
282 		return -ENOMEM;
283 	pcie->port = pdev;
284 	pcie->irq = irq;
285 	pcie->service = service;
286 
287 	/* Initialize generic device interface */
288 	device = &pcie->device;
289 	device->bus = &pcie_port_bus_type;
290 	device->release = release_pcie_device;	/* callback to free pcie dev */
291 	dev_set_name(device, "%s:pcie%03x",
292 		     pci_name(pdev),
293 		     get_descriptor_id(pci_pcie_type(pdev), service));
294 	device->parent = &pdev->dev;
295 	device_enable_async_suspend(device);
296 
297 	retval = device_register(device);
298 	if (retval) {
299 		put_device(device);
300 		return retval;
301 	}
302 
303 	pm_runtime_no_callbacks(device);
304 
305 	return 0;
306 }
307 
308 /**
309  * pcie_port_device_register - register PCI Express port
310  * @dev: PCI Express port to register
311  *
312  * Allocate the port extension structure and register services associated with
313  * the port.
314  */
pcie_port_device_register(struct pci_dev * dev)315 int pcie_port_device_register(struct pci_dev *dev)
316 {
317 	int status, capabilities, irq_services, i, nr_service;
318 	int irqs[PCIE_PORT_DEVICE_MAXSERVICES] = {
319 		[0 ... PCIE_PORT_DEVICE_MAXSERVICES-1] = -1
320 	};
321 
322 	/* Enable PCI Express port device */
323 	status = pci_enable_device(dev);
324 	if (status)
325 		return status;
326 
327 	/* Get and check PCI Express port services */
328 	capabilities = get_port_device_capability(dev);
329 	if (!capabilities)
330 		return 0;
331 
332 	pci_set_master(dev);
333 
334 	irq_services = 0;
335 	if (IS_ENABLED(CONFIG_PCIE_PME))
336 		irq_services |= PCIE_PORT_SERVICE_PME;
337 	if (IS_ENABLED(CONFIG_PCIEAER))
338 		irq_services |= PCIE_PORT_SERVICE_AER;
339 	if (IS_ENABLED(CONFIG_HOTPLUG_PCI_PCIE))
340 		irq_services |= PCIE_PORT_SERVICE_HP;
341 	if (IS_ENABLED(CONFIG_PCIE_DPC))
342 		irq_services |= PCIE_PORT_SERVICE_DPC;
343 	irq_services &= capabilities;
344 
345 	if (irq_services) {
346 		/*
347 		 * Initialize service IRQs. Don't use service devices that
348 		 * require interrupts if there is no way to generate them.
349 		 * However, some drivers may have a polling mode (e.g.
350 		 * pciehp_poll_mode) that can be used in the absence of IRQs.
351 		 * Allow them to determine if that is to be used.
352 		 */
353 		status = pcie_init_service_irqs(dev, irqs, irq_services);
354 		if (status) {
355 			irq_services &= PCIE_PORT_SERVICE_HP;
356 			if (!irq_services)
357 				goto error_disable;
358 		}
359 	}
360 
361 	/* Allocate child services if any */
362 	status = -ENODEV;
363 	nr_service = 0;
364 	for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
365 		int service = 1 << i;
366 		if (!(capabilities & service))
367 			continue;
368 		if (!pcie_device_init(dev, service, irqs[i]))
369 			nr_service++;
370 	}
371 	if (!nr_service)
372 		goto error_cleanup_irqs;
373 
374 	return 0;
375 
376 error_cleanup_irqs:
377 	pci_free_irq_vectors(dev);
378 error_disable:
379 	pci_disable_device(dev);
380 	return status;
381 }
382 
383 typedef int (*pcie_callback_t)(struct pcie_device *);
384 
pcie_port_device_iter(struct device * dev,void * data)385 int pcie_port_device_iter(struct device *dev, void *data)
386 {
387 	struct pcie_port_service_driver *service_driver;
388 	size_t offset = *(size_t *)data;
389 	pcie_callback_t cb;
390 
391 	if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
392 		service_driver = to_service_driver(dev->driver);
393 		cb = *(pcie_callback_t *)((void *)service_driver + offset);
394 		if (cb)
395 			return cb(to_pcie_device(dev));
396 	}
397 	return 0;
398 }
399 
400 #ifdef CONFIG_PM
401 /**
402  * pcie_port_device_suspend - suspend port services associated with a PCIe port
403  * @dev: PCI Express port to handle
404  */
pcie_port_device_suspend(struct device * dev)405 int pcie_port_device_suspend(struct device *dev)
406 {
407 	size_t off = offsetof(struct pcie_port_service_driver, suspend);
408 	return device_for_each_child(dev, &off, pcie_port_device_iter);
409 }
410 
pcie_port_device_resume_noirq(struct device * dev)411 int pcie_port_device_resume_noirq(struct device *dev)
412 {
413 	size_t off = offsetof(struct pcie_port_service_driver, resume_noirq);
414 	return device_for_each_child(dev, &off, pcie_port_device_iter);
415 }
416 
417 /**
418  * pcie_port_device_resume - resume port services associated with a PCIe port
419  * @dev: PCI Express port to handle
420  */
pcie_port_device_resume(struct device * dev)421 int pcie_port_device_resume(struct device *dev)
422 {
423 	size_t off = offsetof(struct pcie_port_service_driver, resume);
424 	return device_for_each_child(dev, &off, pcie_port_device_iter);
425 }
426 
427 /**
428  * pcie_port_device_runtime_suspend - runtime suspend port services
429  * @dev: PCI Express port to handle
430  */
pcie_port_device_runtime_suspend(struct device * dev)431 int pcie_port_device_runtime_suspend(struct device *dev)
432 {
433 	size_t off = offsetof(struct pcie_port_service_driver, runtime_suspend);
434 	return device_for_each_child(dev, &off, pcie_port_device_iter);
435 }
436 
437 /**
438  * pcie_port_device_runtime_resume - runtime resume port services
439  * @dev: PCI Express port to handle
440  */
pcie_port_device_runtime_resume(struct device * dev)441 int pcie_port_device_runtime_resume(struct device *dev)
442 {
443 	size_t off = offsetof(struct pcie_port_service_driver, runtime_resume);
444 	return device_for_each_child(dev, &off, pcie_port_device_iter);
445 }
446 #endif /* PM */
447 
remove_iter(struct device * dev,void * data)448 static int remove_iter(struct device *dev, void *data)
449 {
450 	if (dev->bus == &pcie_port_bus_type)
451 		device_unregister(dev);
452 	return 0;
453 }
454 
find_service_iter(struct device * device,void * data)455 static int find_service_iter(struct device *device, void *data)
456 {
457 	struct pcie_port_service_driver *service_driver;
458 	struct portdrv_service_data *pdrvs;
459 	u32 service;
460 
461 	pdrvs = (struct portdrv_service_data *) data;
462 	service = pdrvs->service;
463 
464 	if (device->bus == &pcie_port_bus_type && device->driver) {
465 		service_driver = to_service_driver(device->driver);
466 		if (service_driver->service == service) {
467 			pdrvs->drv = service_driver;
468 			pdrvs->dev = device;
469 			return 1;
470 		}
471 	}
472 
473 	return 0;
474 }
475 
476 /**
477  * pcie_port_find_device - find the struct device
478  * @dev: PCI Express port the service is associated with
479  * @service: For the service to find
480  *
481  * Find the struct device associated with given service on a pci_dev
482  */
pcie_port_find_device(struct pci_dev * dev,u32 service)483 struct device *pcie_port_find_device(struct pci_dev *dev,
484 				      u32 service)
485 {
486 	struct device *device;
487 	struct portdrv_service_data pdrvs;
488 
489 	pdrvs.dev = NULL;
490 	pdrvs.service = service;
491 	device_for_each_child(&dev->dev, &pdrvs, find_service_iter);
492 
493 	device = pdrvs.dev;
494 	return device;
495 }
496 EXPORT_SYMBOL_GPL(pcie_port_find_device);
497 
498 /**
499  * pcie_port_device_remove - unregister PCI Express port service devices
500  * @dev: PCI Express port the service devices to unregister are associated with
501  *
502  * Remove PCI Express port service devices associated with given port and
503  * disable MSI-X or MSI for the port.
504  */
pcie_port_device_remove(struct pci_dev * dev)505 void pcie_port_device_remove(struct pci_dev *dev)
506 {
507 	device_for_each_child(&dev->dev, NULL, remove_iter);
508 	pci_free_irq_vectors(dev);
509 	pci_disable_device(dev);
510 }
511 
512 /**
513  * pcie_port_probe_service - probe driver for given PCI Express port service
514  * @dev: PCI Express port service device to probe against
515  *
516  * If PCI Express port service driver is registered with
517  * pcie_port_service_register(), this function will be called by the driver core
518  * whenever match is found between the driver and a port service device.
519  */
pcie_port_probe_service(struct device * dev)520 static int pcie_port_probe_service(struct device *dev)
521 {
522 	struct pcie_device *pciedev;
523 	struct pcie_port_service_driver *driver;
524 	int status;
525 
526 	if (!dev || !dev->driver)
527 		return -ENODEV;
528 
529 	driver = to_service_driver(dev->driver);
530 	if (!driver || !driver->probe)
531 		return -ENODEV;
532 
533 	pciedev = to_pcie_device(dev);
534 	status = driver->probe(pciedev);
535 	if (status)
536 		return status;
537 
538 	get_device(dev);
539 	return 0;
540 }
541 
542 /**
543  * pcie_port_remove_service - detach driver from given PCI Express port service
544  * @dev: PCI Express port service device to handle
545  *
546  * If PCI Express port service driver is registered with
547  * pcie_port_service_register(), this function will be called by the driver core
548  * when device_unregister() is called for the port service device associated
549  * with the driver.
550  */
pcie_port_remove_service(struct device * dev)551 static int pcie_port_remove_service(struct device *dev)
552 {
553 	struct pcie_device *pciedev;
554 	struct pcie_port_service_driver *driver;
555 
556 	if (!dev || !dev->driver)
557 		return 0;
558 
559 	pciedev = to_pcie_device(dev);
560 	driver = to_service_driver(dev->driver);
561 	if (driver && driver->remove) {
562 		driver->remove(pciedev);
563 		put_device(dev);
564 	}
565 	return 0;
566 }
567 
568 /**
569  * pcie_port_shutdown_service - shut down given PCI Express port service
570  * @dev: PCI Express port service device to handle
571  *
572  * If PCI Express port service driver is registered with
573  * pcie_port_service_register(), this function will be called by the driver core
574  * when device_shutdown() is called for the port service device associated
575  * with the driver.
576  */
pcie_port_shutdown_service(struct device * dev)577 static void pcie_port_shutdown_service(struct device *dev) {}
578 
579 /**
580  * pcie_port_service_register - register PCI Express port service driver
581  * @new: PCI Express port service driver to register
582  */
pcie_port_service_register(struct pcie_port_service_driver * new)583 int pcie_port_service_register(struct pcie_port_service_driver *new)
584 {
585 	if (pcie_ports_disabled)
586 		return -ENODEV;
587 
588 	new->driver.name = new->name;
589 	new->driver.bus = &pcie_port_bus_type;
590 	new->driver.probe = pcie_port_probe_service;
591 	new->driver.remove = pcie_port_remove_service;
592 	new->driver.shutdown = pcie_port_shutdown_service;
593 
594 	return driver_register(&new->driver);
595 }
596 EXPORT_SYMBOL(pcie_port_service_register);
597 
598 /**
599  * pcie_port_service_unregister - unregister PCI Express port service driver
600  * @drv: PCI Express port service driver to unregister
601  */
pcie_port_service_unregister(struct pcie_port_service_driver * drv)602 void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
603 {
604 	driver_unregister(&drv->driver);
605 }
606 EXPORT_SYMBOL(pcie_port_service_unregister);
607