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