1
2
3
4
5
6
7
8
9
10#include <linux/module.h>
11#include <linux/pci.h>
12#include <linux/kernel.h>
13#include <linux/errno.h>
14#include <linux/pm.h>
15#include <linux/pm_runtime.h>
16#include <linux/string.h>
17#include <linux/slab.h>
18#include <linux/pcieport_if.h>
19#include <linux/aer.h>
20
21#include "../pci.h"
22#include "portdrv.h"
23
24bool pciehp_msi_disabled;
25
26static int __init pciehp_setup(char *str)
27{
28 if (!strncmp(str, "nomsi", 5))
29 pciehp_msi_disabled = true;
30
31 return 1;
32}
33__setup("pcie_hp=", pciehp_setup);
34
35
36
37
38
39
40
41
42static void release_pcie_device(struct device *dev)
43{
44 kfree(to_pcie_device(dev));
45}
46
47
48
49
50
51
52static int pcie_message_numbers(struct pci_dev *dev, int mask,
53 u32 *pme, u32 *aer, u32 *dpc)
54{
55 u32 nvec = 0, pos, reg32;
56 u16 reg16;
57
58
59
60
61
62
63
64
65 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
66 pcie_capability_read_word(dev, PCI_EXP_FLAGS, ®16);
67 *pme = (reg16 & PCI_EXP_FLAGS_IRQ) >> 9;
68 nvec = *pme + 1;
69 }
70
71 if (mask & PCIE_PORT_SERVICE_AER) {
72 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR);
73 if (pos) {
74 pci_read_config_dword(dev, pos + PCI_ERR_ROOT_STATUS,
75 ®32);
76 *aer = (reg32 & PCI_ERR_ROOT_AER_IRQ) >> 27;
77 nvec = max(nvec, *aer + 1);
78 }
79 }
80
81 if (mask & PCIE_PORT_SERVICE_DPC) {
82 pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC);
83 if (pos) {
84 pci_read_config_word(dev, pos + PCI_EXP_DPC_CAP,
85 ®16);
86 *dpc = reg16 & PCI_EXP_DPC_IRQ;
87 nvec = max(nvec, *dpc + 1);
88 }
89 }
90
91 return nvec;
92}
93
94
95
96
97
98
99
100
101
102
103static int pcie_port_enable_irq_vec(struct pci_dev *dev, int *irqs, int mask)
104{
105 int nr_entries, nvec;
106 u32 pme = 0, aer = 0, dpc = 0;
107
108
109 nr_entries = pci_alloc_irq_vectors(dev, 1, PCIE_PORT_MAX_MSI_ENTRIES,
110 PCI_IRQ_MSIX | PCI_IRQ_MSI);
111 if (nr_entries < 0)
112 return nr_entries;
113
114
115 nvec = pcie_message_numbers(dev, mask, &pme, &aer, &dpc);
116 if (nvec > nr_entries) {
117 pci_free_irq_vectors(dev);
118 return -EIO;
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132 if (nvec != nr_entries) {
133 pci_free_irq_vectors(dev);
134
135 nr_entries = pci_alloc_irq_vectors(dev, nvec, nvec,
136 PCI_IRQ_MSIX | PCI_IRQ_MSI);
137 if (nr_entries < 0)
138 return nr_entries;
139 }
140
141
142 if (mask & (PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP)) {
143 irqs[PCIE_PORT_SERVICE_PME_SHIFT] = pci_irq_vector(dev, pme);
144 irqs[PCIE_PORT_SERVICE_HP_SHIFT] = pci_irq_vector(dev, pme);
145 }
146
147 if (mask & PCIE_PORT_SERVICE_AER)
148 irqs[PCIE_PORT_SERVICE_AER_SHIFT] = pci_irq_vector(dev, aer);
149
150 if (mask & PCIE_PORT_SERVICE_DPC)
151 irqs[PCIE_PORT_SERVICE_DPC_SHIFT] = pci_irq_vector(dev, dpc);
152
153 return 0;
154}
155
156
157
158
159
160
161
162
163
164static int pcie_init_service_irqs(struct pci_dev *dev, int *irqs, int mask)
165{
166 int ret, i;
167
168 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++)
169 irqs[i] = -1;
170
171
172
173
174
175
176 if ((mask & PCIE_PORT_SERVICE_PME) && pcie_pme_no_msi())
177 goto legacy_irq;
178
179 if ((mask & PCIE_PORT_SERVICE_HP) && pciehp_no_msi())
180 goto legacy_irq;
181
182
183 if (pcie_port_enable_irq_vec(dev, irqs, mask) == 0)
184 return 0;
185
186legacy_irq:
187
188 ret = pci_alloc_irq_vectors(dev, 1, 1, PCI_IRQ_LEGACY);
189 if (ret < 0)
190 return -ENODEV;
191
192 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
193 if (i != PCIE_PORT_SERVICE_VC_SHIFT)
194 irqs[i] = pci_irq_vector(dev, 0);
195 }
196
197 return 0;
198}
199
200
201
202
203
204
205
206
207
208
209
210static int get_port_device_capability(struct pci_dev *dev)
211{
212 int services = 0;
213 int cap_mask = 0;
214
215 if (pcie_ports_disabled)
216 return 0;
217
218 cap_mask = PCIE_PORT_SERVICE_PME | PCIE_PORT_SERVICE_HP
219 | PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_DPC;
220 if (pci_aer_available())
221 cap_mask |= PCIE_PORT_SERVICE_AER;
222
223 if (pcie_ports_auto)
224 pcie_port_platform_notify(dev, &cap_mask);
225
226
227 if ((cap_mask & PCIE_PORT_SERVICE_HP) && dev->is_hotplug_bridge) {
228 services |= PCIE_PORT_SERVICE_HP;
229
230
231
232
233 pcie_capability_clear_word(dev, PCI_EXP_SLTCTL,
234 PCI_EXP_SLTCTL_CCIE | PCI_EXP_SLTCTL_HPIE);
235 }
236
237 if ((cap_mask & PCIE_PORT_SERVICE_AER)
238 && pci_find_ext_capability(dev, PCI_EXT_CAP_ID_ERR)) {
239 services |= PCIE_PORT_SERVICE_AER;
240
241
242
243
244 pci_disable_pcie_error_reporting(dev);
245 }
246
247 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_VC))
248 services |= PCIE_PORT_SERVICE_VC;
249
250 if ((cap_mask & PCIE_PORT_SERVICE_PME)
251 && pci_pcie_type(dev) == PCI_EXP_TYPE_ROOT_PORT) {
252 services |= PCIE_PORT_SERVICE_PME;
253
254
255
256
257
258 pcie_pme_interrupt_enable(dev, false);
259 }
260 if (pci_find_ext_capability(dev, PCI_EXT_CAP_ID_DPC))
261 services |= PCIE_PORT_SERVICE_DPC;
262
263 return services;
264}
265
266
267
268
269
270
271
272static int pcie_device_init(struct pci_dev *pdev, int service, int irq)
273{
274 int retval;
275 struct pcie_device *pcie;
276 struct device *device;
277
278 pcie = kzalloc(sizeof(*pcie), GFP_KERNEL);
279 if (!pcie)
280 return -ENOMEM;
281 pcie->port = pdev;
282 pcie->irq = irq;
283 pcie->service = service;
284
285
286 device = &pcie->device;
287 device->bus = &pcie_port_bus_type;
288 device->release = release_pcie_device;
289 dev_set_name(device, "%s:pcie%03x",
290 pci_name(pdev),
291 get_descriptor_id(pci_pcie_type(pdev), service));
292 device->parent = &pdev->dev;
293 device_enable_async_suspend(device);
294
295 retval = device_register(device);
296 if (retval) {
297 put_device(device);
298 return retval;
299 }
300
301 pm_runtime_no_callbacks(device);
302
303 return 0;
304}
305
306
307
308
309
310
311
312
313int pcie_port_device_register(struct pci_dev *dev)
314{
315 int status, capabilities, i, nr_service;
316 int irqs[PCIE_PORT_DEVICE_MAXSERVICES];
317
318
319 status = pci_enable_device(dev);
320 if (status)
321 return status;
322
323
324 capabilities = get_port_device_capability(dev);
325 if (!capabilities)
326 return 0;
327
328 pci_set_master(dev);
329
330
331
332
333
334
335
336 status = pcie_init_service_irqs(dev, irqs, capabilities);
337 if (status) {
338 capabilities &= PCIE_PORT_SERVICE_VC | PCIE_PORT_SERVICE_HP;
339 if (!capabilities)
340 goto error_disable;
341 }
342
343
344 status = -ENODEV;
345 nr_service = 0;
346 for (i = 0; i < PCIE_PORT_DEVICE_MAXSERVICES; i++) {
347 int service = 1 << i;
348 if (!(capabilities & service))
349 continue;
350 if (!pcie_device_init(dev, service, irqs[i]))
351 nr_service++;
352 }
353 if (!nr_service)
354 goto error_cleanup_irqs;
355
356 return 0;
357
358error_cleanup_irqs:
359 pci_free_irq_vectors(dev);
360error_disable:
361 pci_disable_device(dev);
362 return status;
363}
364
365#ifdef CONFIG_PM
366static int suspend_iter(struct device *dev, void *data)
367{
368 struct pcie_port_service_driver *service_driver;
369
370 if ((dev->bus == &pcie_port_bus_type) && dev->driver) {
371 service_driver = to_service_driver(dev->driver);
372 if (service_driver->suspend)
373 service_driver->suspend(to_pcie_device(dev));
374 }
375 return 0;
376}
377
378
379
380
381
382int pcie_port_device_suspend(struct device *dev)
383{
384 return device_for_each_child(dev, NULL, suspend_iter);
385}
386
387static int resume_iter(struct device *dev, void *data)
388{
389 struct pcie_port_service_driver *service_driver;
390
391 if ((dev->bus == &pcie_port_bus_type) &&
392 (dev->driver)) {
393 service_driver = to_service_driver(dev->driver);
394 if (service_driver->resume)
395 service_driver->resume(to_pcie_device(dev));
396 }
397 return 0;
398}
399
400
401
402
403
404int pcie_port_device_resume(struct device *dev)
405{
406 return device_for_each_child(dev, NULL, resume_iter);
407}
408#endif
409
410static int remove_iter(struct device *dev, void *data)
411{
412 if (dev->bus == &pcie_port_bus_type)
413 device_unregister(dev);
414 return 0;
415}
416
417
418
419
420
421
422
423
424void pcie_port_device_remove(struct pci_dev *dev)
425{
426 device_for_each_child(&dev->dev, NULL, remove_iter);
427 pci_free_irq_vectors(dev);
428 pci_disable_device(dev);
429}
430
431
432
433
434
435
436
437
438
439static int pcie_port_probe_service(struct device *dev)
440{
441 struct pcie_device *pciedev;
442 struct pcie_port_service_driver *driver;
443 int status;
444
445 if (!dev || !dev->driver)
446 return -ENODEV;
447
448 driver = to_service_driver(dev->driver);
449 if (!driver || !driver->probe)
450 return -ENODEV;
451
452 pciedev = to_pcie_device(dev);
453 status = driver->probe(pciedev);
454 if (status)
455 return status;
456
457 get_device(dev);
458 return 0;
459}
460
461
462
463
464
465
466
467
468
469
470static int pcie_port_remove_service(struct device *dev)
471{
472 struct pcie_device *pciedev;
473 struct pcie_port_service_driver *driver;
474
475 if (!dev || !dev->driver)
476 return 0;
477
478 pciedev = to_pcie_device(dev);
479 driver = to_service_driver(dev->driver);
480 if (driver && driver->remove) {
481 driver->remove(pciedev);
482 put_device(dev);
483 }
484 return 0;
485}
486
487
488
489
490
491
492
493
494
495
496static void pcie_port_shutdown_service(struct device *dev) {}
497
498
499
500
501
502int pcie_port_service_register(struct pcie_port_service_driver *new)
503{
504 if (pcie_ports_disabled)
505 return -ENODEV;
506
507 new->driver.name = new->name;
508 new->driver.bus = &pcie_port_bus_type;
509 new->driver.probe = pcie_port_probe_service;
510 new->driver.remove = pcie_port_remove_service;
511 new->driver.shutdown = pcie_port_shutdown_service;
512
513 return driver_register(&new->driver);
514}
515EXPORT_SYMBOL(pcie_port_service_register);
516
517
518
519
520
521void pcie_port_service_unregister(struct pcie_port_service_driver *drv)
522{
523 driver_unregister(&drv->driver);
524}
525EXPORT_SYMBOL(pcie_port_service_unregister);
526