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