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