ee5868810a5bb83741f8d430cc63987e8b56deb7
[linux-2.6-block.git] / drivers / pci / pci-driver.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
4  * (C) Copyright 2007 Novell Inc.
5  */
6
7 #include <linux/pci.h>
8 #include <linux/module.h>
9 #include <linux/init.h>
10 #include <linux/device.h>
11 #include <linux/mempolicy.h>
12 #include <linux/string.h>
13 #include <linux/slab.h>
14 #include <linux/sched.h>
15 #include <linux/sched/isolation.h>
16 #include <linux/cpu.h>
17 #include <linux/pm_runtime.h>
18 #include <linux/suspend.h>
19 #include <linux/kexec.h>
20 #include <linux/of_device.h>
21 #include <linux/acpi.h>
22 #include <linux/dma-map-ops.h>
23 #include "pci.h"
24 #include "pcie/portdrv.h"
25
26 struct pci_dynid {
27         struct list_head node;
28         struct pci_device_id id;
29 };
30
31 /**
32  * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
33  * @drv: target pci driver
34  * @vendor: PCI vendor ID
35  * @device: PCI device ID
36  * @subvendor: PCI subvendor ID
37  * @subdevice: PCI subdevice ID
38  * @class: PCI class
39  * @class_mask: PCI class mask
40  * @driver_data: private driver data
41  *
42  * Adds a new dynamic pci device ID to this driver and causes the
43  * driver to probe for all devices again.  @drv must have been
44  * registered prior to calling this function.
45  *
46  * CONTEXT:
47  * Does GFP_KERNEL allocation.
48  *
49  * RETURNS:
50  * 0 on success, -errno on failure.
51  */
52 int pci_add_dynid(struct pci_driver *drv,
53                   unsigned int vendor, unsigned int device,
54                   unsigned int subvendor, unsigned int subdevice,
55                   unsigned int class, unsigned int class_mask,
56                   unsigned long driver_data)
57 {
58         struct pci_dynid *dynid;
59
60         dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
61         if (!dynid)
62                 return -ENOMEM;
63
64         dynid->id.vendor = vendor;
65         dynid->id.device = device;
66         dynid->id.subvendor = subvendor;
67         dynid->id.subdevice = subdevice;
68         dynid->id.class = class;
69         dynid->id.class_mask = class_mask;
70         dynid->id.driver_data = driver_data;
71
72         spin_lock(&drv->dynids.lock);
73         list_add_tail(&dynid->node, &drv->dynids.list);
74         spin_unlock(&drv->dynids.lock);
75
76         return driver_attach(&drv->driver);
77 }
78 EXPORT_SYMBOL_GPL(pci_add_dynid);
79
80 static void pci_free_dynids(struct pci_driver *drv)
81 {
82         struct pci_dynid *dynid, *n;
83
84         spin_lock(&drv->dynids.lock);
85         list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
86                 list_del(&dynid->node);
87                 kfree(dynid);
88         }
89         spin_unlock(&drv->dynids.lock);
90 }
91
92 /**
93  * pci_match_id - See if a PCI device matches a given pci_id table
94  * @ids: array of PCI device ID structures to search in
95  * @dev: the PCI device structure to match against.
96  *
97  * Used by a driver to check whether a PCI device is in its list of
98  * supported devices.  Returns the matching pci_device_id structure or
99  * %NULL if there is no match.
100  *
101  * Deprecated; don't use this as it will not catch any dynamic IDs
102  * that a driver might want to check for.
103  */
104 const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
105                                          struct pci_dev *dev)
106 {
107         if (ids) {
108                 while (ids->vendor || ids->subvendor || ids->class_mask) {
109                         if (pci_match_one_device(ids, dev))
110                                 return ids;
111                         ids++;
112                 }
113         }
114         return NULL;
115 }
116 EXPORT_SYMBOL(pci_match_id);
117
118 static const struct pci_device_id pci_device_id_any = {
119         .vendor = PCI_ANY_ID,
120         .device = PCI_ANY_ID,
121         .subvendor = PCI_ANY_ID,
122         .subdevice = PCI_ANY_ID,
123 };
124
125 /**
126  * pci_match_device - See if a device matches a driver's list of IDs
127  * @drv: the PCI driver to match against
128  * @dev: the PCI device structure to match against
129  *
130  * Used by a driver to check whether a PCI device is in its list of
131  * supported devices or in the dynids list, which may have been augmented
132  * via the sysfs "new_id" file.  Returns the matching pci_device_id
133  * structure or %NULL if there is no match.
134  */
135 static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
136                                                     struct pci_dev *dev)
137 {
138         struct pci_dynid *dynid;
139         const struct pci_device_id *found_id = NULL, *ids;
140
141         /* When driver_override is set, only bind to the matching driver */
142         if (dev->driver_override && strcmp(dev->driver_override, drv->name))
143                 return NULL;
144
145         /* Look at the dynamic ids first, before the static ones */
146         spin_lock(&drv->dynids.lock);
147         list_for_each_entry(dynid, &drv->dynids.list, node) {
148                 if (pci_match_one_device(&dynid->id, dev)) {
149                         found_id = &dynid->id;
150                         break;
151                 }
152         }
153         spin_unlock(&drv->dynids.lock);
154
155         if (found_id)
156                 return found_id;
157
158         for (ids = drv->id_table; (found_id = pci_match_id(ids, dev));
159              ids = found_id + 1) {
160                 /*
161                  * The match table is split based on driver_override.
162                  * In case override_only was set, enforce driver_override
163                  * matching.
164                  */
165                 if (found_id->override_only) {
166                         if (dev->driver_override)
167                                 return found_id;
168                 } else {
169                         return found_id;
170                 }
171         }
172
173         /* driver_override will always match, send a dummy id */
174         if (dev->driver_override)
175                 return &pci_device_id_any;
176         return NULL;
177 }
178
179 /**
180  * new_id_store - sysfs frontend to pci_add_dynid()
181  * @driver: target device driver
182  * @buf: buffer for scanning device ID data
183  * @count: input size
184  *
185  * Allow PCI IDs to be added to an existing driver via sysfs.
186  */
187 static ssize_t new_id_store(struct device_driver *driver, const char *buf,
188                             size_t count)
189 {
190         struct pci_driver *pdrv = to_pci_driver(driver);
191         const struct pci_device_id *ids = pdrv->id_table;
192         u32 vendor, device, subvendor = PCI_ANY_ID,
193                 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
194         unsigned long driver_data = 0;
195         int fields = 0;
196         int retval = 0;
197
198         fields = sscanf(buf, "%x %x %x %x %x %x %lx",
199                         &vendor, &device, &subvendor, &subdevice,
200                         &class, &class_mask, &driver_data);
201         if (fields < 2)
202                 return -EINVAL;
203
204         if (fields != 7) {
205                 struct pci_dev *pdev = kzalloc(sizeof(*pdev), GFP_KERNEL);
206                 if (!pdev)
207                         return -ENOMEM;
208
209                 pdev->vendor = vendor;
210                 pdev->device = device;
211                 pdev->subsystem_vendor = subvendor;
212                 pdev->subsystem_device = subdevice;
213                 pdev->class = class;
214
215                 if (pci_match_device(pdrv, pdev))
216                         retval = -EEXIST;
217
218                 kfree(pdev);
219
220                 if (retval)
221                         return retval;
222         }
223
224         /* Only accept driver_data values that match an existing id_table
225            entry */
226         if (ids) {
227                 retval = -EINVAL;
228                 while (ids->vendor || ids->subvendor || ids->class_mask) {
229                         if (driver_data == ids->driver_data) {
230                                 retval = 0;
231                                 break;
232                         }
233                         ids++;
234                 }
235                 if (retval)     /* No match */
236                         return retval;
237         }
238
239         retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
240                                class, class_mask, driver_data);
241         if (retval)
242                 return retval;
243         return count;
244 }
245 static DRIVER_ATTR_WO(new_id);
246
247 /**
248  * remove_id_store - remove a PCI device ID from this driver
249  * @driver: target device driver
250  * @buf: buffer for scanning device ID data
251  * @count: input size
252  *
253  * Removes a dynamic pci device ID to this driver.
254  */
255 static ssize_t remove_id_store(struct device_driver *driver, const char *buf,
256                                size_t count)
257 {
258         struct pci_dynid *dynid, *n;
259         struct pci_driver *pdrv = to_pci_driver(driver);
260         u32 vendor, device, subvendor = PCI_ANY_ID,
261                 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
262         int fields = 0;
263         size_t retval = -ENODEV;
264
265         fields = sscanf(buf, "%x %x %x %x %x %x",
266                         &vendor, &device, &subvendor, &subdevice,
267                         &class, &class_mask);
268         if (fields < 2)
269                 return -EINVAL;
270
271         spin_lock(&pdrv->dynids.lock);
272         list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
273                 struct pci_device_id *id = &dynid->id;
274                 if ((id->vendor == vendor) &&
275                     (id->device == device) &&
276                     (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
277                     (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
278                     !((id->class ^ class) & class_mask)) {
279                         list_del(&dynid->node);
280                         kfree(dynid);
281                         retval = count;
282                         break;
283                 }
284         }
285         spin_unlock(&pdrv->dynids.lock);
286
287         return retval;
288 }
289 static DRIVER_ATTR_WO(remove_id);
290
291 static struct attribute *pci_drv_attrs[] = {
292         &driver_attr_new_id.attr,
293         &driver_attr_remove_id.attr,
294         NULL,
295 };
296 ATTRIBUTE_GROUPS(pci_drv);
297
298 struct drv_dev_and_id {
299         struct pci_driver *drv;
300         struct pci_dev *dev;
301         const struct pci_device_id *id;
302 };
303
304 static long local_pci_probe(void *_ddi)
305 {
306         struct drv_dev_and_id *ddi = _ddi;
307         struct pci_dev *pci_dev = ddi->dev;
308         struct pci_driver *pci_drv = ddi->drv;
309         struct device *dev = &pci_dev->dev;
310         int rc;
311
312         /*
313          * Unbound PCI devices are always put in D0, regardless of
314          * runtime PM status.  During probe, the device is set to
315          * active and the usage count is incremented.  If the driver
316          * supports runtime PM, it should call pm_runtime_put_noidle(),
317          * or any other runtime PM helper function decrementing the usage
318          * count, in its probe routine and pm_runtime_get_noresume() in
319          * its remove routine.
320          */
321         pm_runtime_get_sync(dev);
322         pci_dev->driver = pci_drv;
323         rc = pci_drv->probe(pci_dev, ddi->id);
324         if (!rc)
325                 return rc;
326         if (rc < 0) {
327                 pci_dev->driver = NULL;
328                 pm_runtime_put_sync(dev);
329                 return rc;
330         }
331         /*
332          * Probe function should return < 0 for failure, 0 for success
333          * Treat values > 0 as success, but warn.
334          */
335         pci_warn(pci_dev, "Driver probe function unexpectedly returned %d\n",
336                  rc);
337         return 0;
338 }
339
340 static bool pci_physfn_is_probed(struct pci_dev *dev)
341 {
342 #ifdef CONFIG_PCI_IOV
343         return dev->is_virtfn && dev->physfn->is_probed;
344 #else
345         return false;
346 #endif
347 }
348
349 static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
350                           const struct pci_device_id *id)
351 {
352         int error, node, cpu;
353         struct drv_dev_and_id ddi = { drv, dev, id };
354
355         /*
356          * Execute driver initialization on node where the device is
357          * attached.  This way the driver likely allocates its local memory
358          * on the right node.
359          */
360         node = dev_to_node(&dev->dev);
361         dev->is_probed = 1;
362
363         cpu_hotplug_disable();
364
365         /*
366          * Prevent nesting work_on_cpu() for the case where a Virtual Function
367          * device is probed from work_on_cpu() of the Physical device.
368          */
369         if (node < 0 || node >= MAX_NUMNODES || !node_online(node) ||
370             pci_physfn_is_probed(dev)) {
371                 cpu = nr_cpu_ids;
372         } else {
373                 cpumask_var_t wq_domain_mask;
374
375                 if (!zalloc_cpumask_var(&wq_domain_mask, GFP_KERNEL)) {
376                         error = -ENOMEM;
377                         goto out;
378                 }
379                 cpumask_and(wq_domain_mask,
380                             housekeeping_cpumask(HK_TYPE_WQ),
381                             housekeeping_cpumask(HK_TYPE_DOMAIN));
382
383                 cpu = cpumask_any_and(cpumask_of_node(node),
384                                       wq_domain_mask);
385                 free_cpumask_var(wq_domain_mask);
386         }
387
388         if (cpu < nr_cpu_ids)
389                 error = work_on_cpu(cpu, local_pci_probe, &ddi);
390         else
391                 error = local_pci_probe(&ddi);
392 out:
393         dev->is_probed = 0;
394         cpu_hotplug_enable();
395         return error;
396 }
397
398 /**
399  * __pci_device_probe - check if a driver wants to claim a specific PCI device
400  * @drv: driver to call to check if it wants the PCI device
401  * @pci_dev: PCI device being probed
402  *
403  * returns 0 on success, else error.
404  * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
405  */
406 static int __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
407 {
408         const struct pci_device_id *id;
409         int error = 0;
410
411         if (drv->probe) {
412                 error = -ENODEV;
413
414                 id = pci_match_device(drv, pci_dev);
415                 if (id)
416                         error = pci_call_probe(drv, pci_dev, id);
417         }
418         return error;
419 }
420
421 int __weak pcibios_alloc_irq(struct pci_dev *dev)
422 {
423         return 0;
424 }
425
426 void __weak pcibios_free_irq(struct pci_dev *dev)
427 {
428 }
429
430 #ifdef CONFIG_PCI_IOV
431 static inline bool pci_device_can_probe(struct pci_dev *pdev)
432 {
433         return (!pdev->is_virtfn || pdev->physfn->sriov->drivers_autoprobe ||
434                 pdev->driver_override);
435 }
436 #else
437 static inline bool pci_device_can_probe(struct pci_dev *pdev)
438 {
439         return true;
440 }
441 #endif
442
443 static int pci_device_probe(struct device *dev)
444 {
445         int error;
446         struct pci_dev *pci_dev = to_pci_dev(dev);
447         struct pci_driver *drv = to_pci_driver(dev->driver);
448
449         if (!pci_device_can_probe(pci_dev))
450                 return -ENODEV;
451
452         pci_assign_irq(pci_dev);
453
454         error = pcibios_alloc_irq(pci_dev);
455         if (error < 0)
456                 return error;
457
458         pci_dev_get(pci_dev);
459         error = __pci_device_probe(drv, pci_dev);
460         if (error) {
461                 pcibios_free_irq(pci_dev);
462                 pci_dev_put(pci_dev);
463         }
464
465         return error;
466 }
467
468 static void pci_device_remove(struct device *dev)
469 {
470         struct pci_dev *pci_dev = to_pci_dev(dev);
471         struct pci_driver *drv = pci_dev->driver;
472
473         if (drv->remove) {
474                 pm_runtime_get_sync(dev);
475                 drv->remove(pci_dev);
476                 pm_runtime_put_noidle(dev);
477         }
478         pcibios_free_irq(pci_dev);
479         pci_dev->driver = NULL;
480         pci_iov_remove(pci_dev);
481
482         /* Undo the runtime PM settings in local_pci_probe() */
483         pm_runtime_put_sync(dev);
484
485         /*
486          * If the device is still on, set the power state as "unknown",
487          * since it might change by the next time we load the driver.
488          */
489         if (pci_dev->current_state == PCI_D0)
490                 pci_dev->current_state = PCI_UNKNOWN;
491
492         /*
493          * We would love to complain here if pci_dev->is_enabled is set, that
494          * the driver should have called pci_disable_device(), but the
495          * unfortunate fact is there are too many odd BIOS and bridge setups
496          * that don't like drivers doing that all of the time.
497          * Oh well, we can dream of sane hardware when we sleep, no matter how
498          * horrible the crap we have to deal with is when we are awake...
499          */
500
501         pci_dev_put(pci_dev);
502 }
503
504 static void pci_device_shutdown(struct device *dev)
505 {
506         struct pci_dev *pci_dev = to_pci_dev(dev);
507         struct pci_driver *drv = pci_dev->driver;
508
509         pm_runtime_resume(dev);
510
511         if (drv && drv->shutdown)
512                 drv->shutdown(pci_dev);
513
514         /*
515          * If this is a kexec reboot, turn off Bus Master bit on the
516          * device to tell it to not continue to do DMA. Don't touch
517          * devices in D3cold or unknown states.
518          * If it is not a kexec reboot, firmware will hit the PCI
519          * devices with big hammer and stop their DMA any way.
520          */
521         if (kexec_in_progress && (pci_dev->current_state <= PCI_D3hot))
522                 pci_clear_master(pci_dev);
523 }
524
525 #ifdef CONFIG_PM_SLEEP
526
527 /* Auxiliary functions used for system resume */
528
529 /**
530  * pci_restore_standard_config - restore standard config registers of PCI device
531  * @pci_dev: PCI device to handle
532  */
533 static int pci_restore_standard_config(struct pci_dev *pci_dev)
534 {
535         pci_update_current_state(pci_dev, PCI_UNKNOWN);
536
537         if (pci_dev->current_state != PCI_D0) {
538                 int error = pci_set_power_state(pci_dev, PCI_D0);
539                 if (error)
540                         return error;
541         }
542
543         pci_restore_state(pci_dev);
544         pci_pme_restore(pci_dev);
545         return 0;
546 }
547 #endif /* CONFIG_PM_SLEEP */
548
549 #ifdef CONFIG_PM
550
551 /* Auxiliary functions used for system resume and run-time resume */
552
553 static void pci_pm_default_resume(struct pci_dev *pci_dev)
554 {
555         pci_fixup_device(pci_fixup_resume, pci_dev);
556         pci_enable_wake(pci_dev, PCI_D0, false);
557 }
558
559 static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
560 {
561         pci_power_up(pci_dev);
562         pci_update_current_state(pci_dev, PCI_D0);
563         pci_restore_state(pci_dev);
564         pci_pme_restore(pci_dev);
565 }
566
567 static void pci_pm_bridge_power_up_actions(struct pci_dev *pci_dev)
568 {
569         pci_bridge_wait_for_secondary_bus(pci_dev);
570         /*
571          * When powering on a bridge from D3cold, the whole hierarchy may be
572          * powered on into D0uninitialized state, resume them to give them a
573          * chance to suspend again
574          */
575         pci_resume_bus(pci_dev->subordinate);
576 }
577
578 #endif /* CONFIG_PM */
579
580 #ifdef CONFIG_PM_SLEEP
581
582 /*
583  * Default "suspend" method for devices that have no driver provided suspend,
584  * or not even a driver at all (second part).
585  */
586 static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
587 {
588         /*
589          * mark its power state as "unknown", since we don't know if
590          * e.g. the BIOS will change its device state when we suspend.
591          */
592         if (pci_dev->current_state == PCI_D0)
593                 pci_dev->current_state = PCI_UNKNOWN;
594 }
595
596 /*
597  * Default "resume" method for devices that have no driver provided resume,
598  * or not even a driver at all (second part).
599  */
600 static int pci_pm_reenable_device(struct pci_dev *pci_dev)
601 {
602         int retval;
603
604         /* if the device was enabled before suspend, re-enable */
605         retval = pci_reenable_device(pci_dev);
606         /*
607          * if the device was busmaster before the suspend, make it busmaster
608          * again
609          */
610         if (pci_dev->is_busmaster)
611                 pci_set_master(pci_dev);
612
613         return retval;
614 }
615
616 static int pci_legacy_suspend(struct device *dev, pm_message_t state)
617 {
618         struct pci_dev *pci_dev = to_pci_dev(dev);
619         struct pci_driver *drv = pci_dev->driver;
620
621         if (drv && drv->suspend) {
622                 pci_power_t prev = pci_dev->current_state;
623                 int error;
624
625                 error = drv->suspend(pci_dev, state);
626                 suspend_report_result(dev, drv->suspend, error);
627                 if (error)
628                         return error;
629
630                 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
631                     && pci_dev->current_state != PCI_UNKNOWN) {
632                         pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
633                                       "PCI PM: Device state not saved by %pS\n",
634                                       drv->suspend);
635                 }
636         }
637
638         pci_fixup_device(pci_fixup_suspend, pci_dev);
639
640         return 0;
641 }
642
643 static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
644 {
645         struct pci_dev *pci_dev = to_pci_dev(dev);
646
647         if (!pci_dev->state_saved)
648                 pci_save_state(pci_dev);
649
650         pci_pm_set_unknown_state(pci_dev);
651
652         pci_fixup_device(pci_fixup_suspend_late, pci_dev);
653
654         return 0;
655 }
656
657 static int pci_legacy_resume(struct device *dev)
658 {
659         struct pci_dev *pci_dev = to_pci_dev(dev);
660         struct pci_driver *drv = pci_dev->driver;
661
662         pci_fixup_device(pci_fixup_resume, pci_dev);
663
664         return drv && drv->resume ?
665                         drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
666 }
667
668 /* Auxiliary functions used by the new power management framework */
669
670 static void pci_pm_default_suspend(struct pci_dev *pci_dev)
671 {
672         /* Disable non-bridge devices without PM support */
673         if (!pci_has_subordinate(pci_dev))
674                 pci_disable_enabled_device(pci_dev);
675 }
676
677 static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
678 {
679         struct pci_driver *drv = pci_dev->driver;
680         bool ret = drv && (drv->suspend || drv->resume);
681
682         /*
683          * Legacy PM support is used by default, so warn if the new framework is
684          * supported as well.  Drivers are supposed to support either the
685          * former, or the latter, but not both at the same time.
686          */
687         pci_WARN(pci_dev, ret && drv->driver.pm, "device %04x:%04x\n",
688                  pci_dev->vendor, pci_dev->device);
689
690         return ret;
691 }
692
693 /* New power management framework */
694
695 static int pci_pm_prepare(struct device *dev)
696 {
697         struct pci_dev *pci_dev = to_pci_dev(dev);
698         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
699
700         if (pm && pm->prepare) {
701                 int error = pm->prepare(dev);
702                 if (error < 0)
703                         return error;
704
705                 if (!error && dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_PREPARE))
706                         return 0;
707         }
708         if (pci_dev_need_resume(pci_dev))
709                 return 0;
710
711         /*
712          * The PME setting needs to be adjusted here in case the direct-complete
713          * optimization is used with respect to this device.
714          */
715         pci_dev_adjust_pme(pci_dev);
716         return 1;
717 }
718
719 static void pci_pm_complete(struct device *dev)
720 {
721         struct pci_dev *pci_dev = to_pci_dev(dev);
722
723         pci_dev_complete_resume(pci_dev);
724         pm_generic_complete(dev);
725
726         /* Resume device if platform firmware has put it in reset-power-on */
727         if (pm_runtime_suspended(dev) && pm_resume_via_firmware()) {
728                 pci_power_t pre_sleep_state = pci_dev->current_state;
729
730                 pci_refresh_power_state(pci_dev);
731                 /*
732                  * On platforms with ACPI this check may also trigger for
733                  * devices sharing power resources if one of those power
734                  * resources has been activated as a result of a change of the
735                  * power state of another device sharing it.  However, in that
736                  * case it is also better to resume the device, in general.
737                  */
738                 if (pci_dev->current_state < pre_sleep_state)
739                         pm_request_resume(dev);
740         }
741 }
742
743 #else /* !CONFIG_PM_SLEEP */
744
745 #define pci_pm_prepare  NULL
746 #define pci_pm_complete NULL
747
748 #endif /* !CONFIG_PM_SLEEP */
749
750 #ifdef CONFIG_SUSPEND
751 static void pcie_pme_root_status_cleanup(struct pci_dev *pci_dev)
752 {
753         /*
754          * Some BIOSes forget to clear Root PME Status bits after system
755          * wakeup, which breaks ACPI-based runtime wakeup on PCI Express.
756          * Clear those bits now just in case (shouldn't hurt).
757          */
758         if (pci_is_pcie(pci_dev) &&
759             (pci_pcie_type(pci_dev) == PCI_EXP_TYPE_ROOT_PORT ||
760              pci_pcie_type(pci_dev) == PCI_EXP_TYPE_RC_EC))
761                 pcie_clear_root_pme_status(pci_dev);
762 }
763
764 static int pci_pm_suspend(struct device *dev)
765 {
766         struct pci_dev *pci_dev = to_pci_dev(dev);
767         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
768
769         pci_dev->skip_bus_pm = false;
770
771         if (pci_has_legacy_pm_support(pci_dev))
772                 return pci_legacy_suspend(dev, PMSG_SUSPEND);
773
774         if (!pm) {
775                 pci_pm_default_suspend(pci_dev);
776                 return 0;
777         }
778
779         /*
780          * PCI devices suspended at run time may need to be resumed at this
781          * point, because in general it may be necessary to reconfigure them for
782          * system suspend.  Namely, if the device is expected to wake up the
783          * system from the sleep state, it may have to be reconfigured for this
784          * purpose, or if the device is not expected to wake up the system from
785          * the sleep state, it should be prevented from signaling wakeup events
786          * going forward.
787          *
788          * Also if the driver of the device does not indicate that its system
789          * suspend callbacks can cope with runtime-suspended devices, it is
790          * better to resume the device from runtime suspend here.
791          */
792         if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
793             pci_dev_need_resume(pci_dev)) {
794                 pm_runtime_resume(dev);
795                 pci_dev->state_saved = false;
796         } else {
797                 pci_dev_adjust_pme(pci_dev);
798         }
799
800         if (pm->suspend) {
801                 pci_power_t prev = pci_dev->current_state;
802                 int error;
803
804                 error = pm->suspend(dev);
805                 suspend_report_result(dev, pm->suspend, error);
806                 if (error)
807                         return error;
808
809                 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
810                     && pci_dev->current_state != PCI_UNKNOWN) {
811                         pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
812                                       "PCI PM: State of device not saved by %pS\n",
813                                       pm->suspend);
814                 }
815         }
816
817         return 0;
818 }
819
820 static int pci_pm_suspend_late(struct device *dev)
821 {
822         if (dev_pm_skip_suspend(dev))
823                 return 0;
824
825         pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
826
827         return pm_generic_suspend_late(dev);
828 }
829
830 static int pci_pm_suspend_noirq(struct device *dev)
831 {
832         struct pci_dev *pci_dev = to_pci_dev(dev);
833         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
834
835         if (dev_pm_skip_suspend(dev))
836                 return 0;
837
838         if (pci_has_legacy_pm_support(pci_dev))
839                 return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
840
841         if (!pm) {
842                 pci_save_state(pci_dev);
843                 goto Fixup;
844         }
845
846         if (pm->suspend_noirq) {
847                 pci_power_t prev = pci_dev->current_state;
848                 int error;
849
850                 error = pm->suspend_noirq(dev);
851                 suspend_report_result(dev, pm->suspend_noirq, error);
852                 if (error)
853                         return error;
854
855                 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
856                     && pci_dev->current_state != PCI_UNKNOWN) {
857                         pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
858                                       "PCI PM: State of device not saved by %pS\n",
859                                       pm->suspend_noirq);
860                         goto Fixup;
861                 }
862         }
863
864         if (pci_dev->skip_bus_pm) {
865                 /*
866                  * Either the device is a bridge with a child in D0 below it, or
867                  * the function is running for the second time in a row without
868                  * going through full resume, which is possible only during
869                  * suspend-to-idle in a spurious wakeup case.  The device should
870                  * be in D0 at this point, but if it is a bridge, it may be
871                  * necessary to save its state.
872                  */
873                 if (!pci_dev->state_saved)
874                         pci_save_state(pci_dev);
875         } else if (!pci_dev->state_saved) {
876                 pci_save_state(pci_dev);
877                 if (pci_power_manageable(pci_dev))
878                         pci_prepare_to_sleep(pci_dev);
879         }
880
881         pci_dbg(pci_dev, "PCI PM: Suspend power state: %s\n",
882                 pci_power_name(pci_dev->current_state));
883
884         if (pci_dev->current_state == PCI_D0) {
885                 pci_dev->skip_bus_pm = true;
886                 /*
887                  * Per PCI PM r1.2, table 6-1, a bridge must be in D0 if any
888                  * downstream device is in D0, so avoid changing the power state
889                  * of the parent bridge by setting the skip_bus_pm flag for it.
890                  */
891                 if (pci_dev->bus->self)
892                         pci_dev->bus->self->skip_bus_pm = true;
893         }
894
895         if (pci_dev->skip_bus_pm && pm_suspend_no_platform()) {
896                 pci_dbg(pci_dev, "PCI PM: Skipped\n");
897                 goto Fixup;
898         }
899
900         pci_pm_set_unknown_state(pci_dev);
901
902         /*
903          * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's
904          * PCI COMMAND register isn't 0, the BIOS assumes that the controller
905          * hasn't been quiesced and tries to turn it off.  If the controller
906          * is already in D3, this can hang or cause memory corruption.
907          *
908          * Since the value of the COMMAND register doesn't matter once the
909          * device has been suspended, we can safely set it to 0 here.
910          */
911         if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
912                 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
913
914 Fixup:
915         pci_fixup_device(pci_fixup_suspend_late, pci_dev);
916
917         /*
918          * If the target system sleep state is suspend-to-idle, it is sufficient
919          * to check whether or not the device's wakeup settings are good for
920          * runtime PM.  Otherwise, the pm_resume_via_firmware() check will cause
921          * pci_pm_complete() to take care of fixing up the device's state
922          * anyway, if need be.
923          */
924         if (device_can_wakeup(dev) && !device_may_wakeup(dev))
925                 dev->power.may_skip_resume = false;
926
927         return 0;
928 }
929
930 static int pci_pm_resume_noirq(struct device *dev)
931 {
932         struct pci_dev *pci_dev = to_pci_dev(dev);
933         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
934         pci_power_t prev_state = pci_dev->current_state;
935         bool skip_bus_pm = pci_dev->skip_bus_pm;
936
937         if (dev_pm_skip_resume(dev))
938                 return 0;
939
940         /*
941          * In the suspend-to-idle case, devices left in D0 during suspend will
942          * stay in D0, so it is not necessary to restore or update their
943          * configuration here and attempting to put them into D0 again is
944          * pointless, so avoid doing that.
945          */
946         if (!(skip_bus_pm && pm_suspend_no_platform()))
947                 pci_pm_default_resume_early(pci_dev);
948
949         pci_fixup_device(pci_fixup_resume_early, pci_dev);
950         pcie_pme_root_status_cleanup(pci_dev);
951
952         if (!skip_bus_pm && prev_state == PCI_D3cold)
953                 pci_pm_bridge_power_up_actions(pci_dev);
954
955         if (pci_has_legacy_pm_support(pci_dev))
956                 return 0;
957
958         if (pm && pm->resume_noirq)
959                 return pm->resume_noirq(dev);
960
961         return 0;
962 }
963
964 static int pci_pm_resume_early(struct device *dev)
965 {
966         if (dev_pm_skip_resume(dev))
967                 return 0;
968
969         return pm_generic_resume_early(dev);
970 }
971
972 static int pci_pm_resume(struct device *dev)
973 {
974         struct pci_dev *pci_dev = to_pci_dev(dev);
975         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
976
977         /*
978          * This is necessary for the suspend error path in which resume is
979          * called without restoring the standard config registers of the device.
980          */
981         if (pci_dev->state_saved)
982                 pci_restore_standard_config(pci_dev);
983
984         if (pci_has_legacy_pm_support(pci_dev))
985                 return pci_legacy_resume(dev);
986
987         pci_pm_default_resume(pci_dev);
988
989         if (pm) {
990                 if (pm->resume)
991                         return pm->resume(dev);
992         } else {
993                 pci_pm_reenable_device(pci_dev);
994         }
995
996         return 0;
997 }
998
999 #else /* !CONFIG_SUSPEND */
1000
1001 #define pci_pm_suspend          NULL
1002 #define pci_pm_suspend_late     NULL
1003 #define pci_pm_suspend_noirq    NULL
1004 #define pci_pm_resume           NULL
1005 #define pci_pm_resume_early     NULL
1006 #define pci_pm_resume_noirq     NULL
1007
1008 #endif /* !CONFIG_SUSPEND */
1009
1010 #ifdef CONFIG_HIBERNATE_CALLBACKS
1011
1012 static int pci_pm_freeze(struct device *dev)
1013 {
1014         struct pci_dev *pci_dev = to_pci_dev(dev);
1015         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1016
1017         if (pci_has_legacy_pm_support(pci_dev))
1018                 return pci_legacy_suspend(dev, PMSG_FREEZE);
1019
1020         if (!pm) {
1021                 pci_pm_default_suspend(pci_dev);
1022                 return 0;
1023         }
1024
1025         /*
1026          * Resume all runtime-suspended devices before creating a snapshot
1027          * image of system memory, because the restore kernel generally cannot
1028          * be expected to always handle them consistently and they need to be
1029          * put into the runtime-active metastate during system resume anyway,
1030          * so it is better to ensure that the state saved in the image will be
1031          * always consistent with that.
1032          */
1033         pm_runtime_resume(dev);
1034         pci_dev->state_saved = false;
1035
1036         if (pm->freeze) {
1037                 int error;
1038
1039                 error = pm->freeze(dev);
1040                 suspend_report_result(dev, pm->freeze, error);
1041                 if (error)
1042                         return error;
1043         }
1044
1045         return 0;
1046 }
1047
1048 static int pci_pm_freeze_noirq(struct device *dev)
1049 {
1050         struct pci_dev *pci_dev = to_pci_dev(dev);
1051         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1052
1053         if (pci_has_legacy_pm_support(pci_dev))
1054                 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
1055
1056         if (pm && pm->freeze_noirq) {
1057                 int error;
1058
1059                 error = pm->freeze_noirq(dev);
1060                 suspend_report_result(dev, pm->freeze_noirq, error);
1061                 if (error)
1062                         return error;
1063         }
1064
1065         if (!pci_dev->state_saved)
1066                 pci_save_state(pci_dev);
1067
1068         pci_pm_set_unknown_state(pci_dev);
1069
1070         return 0;
1071 }
1072
1073 static int pci_pm_thaw_noirq(struct device *dev)
1074 {
1075         struct pci_dev *pci_dev = to_pci_dev(dev);
1076         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1077
1078         /*
1079          * The pm->thaw_noirq() callback assumes the device has been
1080          * returned to D0 and its config state has been restored.
1081          *
1082          * In addition, pci_restore_state() restores MSI-X state in MMIO
1083          * space, which requires the device to be in D0, so return it to D0
1084          * in case the driver's "freeze" callbacks put it into a low-power
1085          * state.
1086          */
1087         pci_set_power_state(pci_dev, PCI_D0);
1088         pci_restore_state(pci_dev);
1089
1090         if (pci_has_legacy_pm_support(pci_dev))
1091                 return 0;
1092
1093         if (pm && pm->thaw_noirq)
1094                 return pm->thaw_noirq(dev);
1095
1096         return 0;
1097 }
1098
1099 static int pci_pm_thaw(struct device *dev)
1100 {
1101         struct pci_dev *pci_dev = to_pci_dev(dev);
1102         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1103         int error = 0;
1104
1105         if (pci_has_legacy_pm_support(pci_dev))
1106                 return pci_legacy_resume(dev);
1107
1108         if (pm) {
1109                 if (pm->thaw)
1110                         error = pm->thaw(dev);
1111         } else {
1112                 pci_pm_reenable_device(pci_dev);
1113         }
1114
1115         pci_dev->state_saved = false;
1116
1117         return error;
1118 }
1119
1120 static int pci_pm_poweroff(struct device *dev)
1121 {
1122         struct pci_dev *pci_dev = to_pci_dev(dev);
1123         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1124
1125         if (pci_has_legacy_pm_support(pci_dev))
1126                 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
1127
1128         if (!pm) {
1129                 pci_pm_default_suspend(pci_dev);
1130                 return 0;
1131         }
1132
1133         /* The reason to do that is the same as in pci_pm_suspend(). */
1134         if (!dev_pm_test_driver_flags(dev, DPM_FLAG_SMART_SUSPEND) ||
1135             pci_dev_need_resume(pci_dev)) {
1136                 pm_runtime_resume(dev);
1137                 pci_dev->state_saved = false;
1138         } else {
1139                 pci_dev_adjust_pme(pci_dev);
1140         }
1141
1142         if (pm->poweroff) {
1143                 int error;
1144
1145                 error = pm->poweroff(dev);
1146                 suspend_report_result(dev, pm->poweroff, error);
1147                 if (error)
1148                         return error;
1149         }
1150
1151         return 0;
1152 }
1153
1154 static int pci_pm_poweroff_late(struct device *dev)
1155 {
1156         if (dev_pm_skip_suspend(dev))
1157                 return 0;
1158
1159         pci_fixup_device(pci_fixup_suspend, to_pci_dev(dev));
1160
1161         return pm_generic_poweroff_late(dev);
1162 }
1163
1164 static int pci_pm_poweroff_noirq(struct device *dev)
1165 {
1166         struct pci_dev *pci_dev = to_pci_dev(dev);
1167         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1168
1169         if (dev_pm_skip_suspend(dev))
1170                 return 0;
1171
1172         if (pci_has_legacy_pm_support(pci_dev))
1173                 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
1174
1175         if (!pm) {
1176                 pci_fixup_device(pci_fixup_suspend_late, pci_dev);
1177                 return 0;
1178         }
1179
1180         if (pm->poweroff_noirq) {
1181                 int error;
1182
1183                 error = pm->poweroff_noirq(dev);
1184                 suspend_report_result(dev, pm->poweroff_noirq, error);
1185                 if (error)
1186                         return error;
1187         }
1188
1189         if (!pci_dev->state_saved && !pci_has_subordinate(pci_dev))
1190                 pci_prepare_to_sleep(pci_dev);
1191
1192         /*
1193          * The reason for doing this here is the same as for the analogous code
1194          * in pci_pm_suspend_noirq().
1195          */
1196         if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
1197                 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
1198
1199         pci_fixup_device(pci_fixup_suspend_late, pci_dev);
1200
1201         return 0;
1202 }
1203
1204 static int pci_pm_restore_noirq(struct device *dev)
1205 {
1206         struct pci_dev *pci_dev = to_pci_dev(dev);
1207         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1208
1209         pci_pm_default_resume_early(pci_dev);
1210         pci_fixup_device(pci_fixup_resume_early, pci_dev);
1211
1212         if (pci_has_legacy_pm_support(pci_dev))
1213                 return 0;
1214
1215         if (pm && pm->restore_noirq)
1216                 return pm->restore_noirq(dev);
1217
1218         return 0;
1219 }
1220
1221 static int pci_pm_restore(struct device *dev)
1222 {
1223         struct pci_dev *pci_dev = to_pci_dev(dev);
1224         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1225
1226         /*
1227          * This is necessary for the hibernation error path in which restore is
1228          * called without restoring the standard config registers of the device.
1229          */
1230         if (pci_dev->state_saved)
1231                 pci_restore_standard_config(pci_dev);
1232
1233         if (pci_has_legacy_pm_support(pci_dev))
1234                 return pci_legacy_resume(dev);
1235
1236         pci_pm_default_resume(pci_dev);
1237
1238         if (pm) {
1239                 if (pm->restore)
1240                         return pm->restore(dev);
1241         } else {
1242                 pci_pm_reenable_device(pci_dev);
1243         }
1244
1245         return 0;
1246 }
1247
1248 #else /* !CONFIG_HIBERNATE_CALLBACKS */
1249
1250 #define pci_pm_freeze           NULL
1251 #define pci_pm_freeze_noirq     NULL
1252 #define pci_pm_thaw             NULL
1253 #define pci_pm_thaw_noirq       NULL
1254 #define pci_pm_poweroff         NULL
1255 #define pci_pm_poweroff_late    NULL
1256 #define pci_pm_poweroff_noirq   NULL
1257 #define pci_pm_restore          NULL
1258 #define pci_pm_restore_noirq    NULL
1259
1260 #endif /* !CONFIG_HIBERNATE_CALLBACKS */
1261
1262 #ifdef CONFIG_PM
1263
1264 static int pci_pm_runtime_suspend(struct device *dev)
1265 {
1266         struct pci_dev *pci_dev = to_pci_dev(dev);
1267         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1268         pci_power_t prev = pci_dev->current_state;
1269         int error;
1270
1271         /*
1272          * If pci_dev->driver is not set (unbound), we leave the device in D0,
1273          * but it may go to D3cold when the bridge above it runtime suspends.
1274          * Save its config space in case that happens.
1275          */
1276         if (!pci_dev->driver) {
1277                 pci_save_state(pci_dev);
1278                 return 0;
1279         }
1280
1281         pci_dev->state_saved = false;
1282         if (pm && pm->runtime_suspend) {
1283                 error = pm->runtime_suspend(dev);
1284                 /*
1285                  * -EBUSY and -EAGAIN is used to request the runtime PM core
1286                  * to schedule a new suspend, so log the event only with debug
1287                  * log level.
1288                  */
1289                 if (error == -EBUSY || error == -EAGAIN) {
1290                         pci_dbg(pci_dev, "can't suspend now (%ps returned %d)\n",
1291                                 pm->runtime_suspend, error);
1292                         return error;
1293                 } else if (error) {
1294                         pci_err(pci_dev, "can't suspend (%ps returned %d)\n",
1295                                 pm->runtime_suspend, error);
1296                         return error;
1297                 }
1298         }
1299
1300         pci_fixup_device(pci_fixup_suspend, pci_dev);
1301
1302         if (pm && pm->runtime_suspend
1303             && !pci_dev->state_saved && pci_dev->current_state != PCI_D0
1304             && pci_dev->current_state != PCI_UNKNOWN) {
1305                 pci_WARN_ONCE(pci_dev, pci_dev->current_state != prev,
1306                               "PCI PM: State of device not saved by %pS\n",
1307                               pm->runtime_suspend);
1308                 return 0;
1309         }
1310
1311         if (!pci_dev->state_saved) {
1312                 pci_save_state(pci_dev);
1313                 pci_finish_runtime_suspend(pci_dev);
1314         }
1315
1316         return 0;
1317 }
1318
1319 static int pci_pm_runtime_resume(struct device *dev)
1320 {
1321         struct pci_dev *pci_dev = to_pci_dev(dev);
1322         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1323         pci_power_t prev_state = pci_dev->current_state;
1324         int error = 0;
1325
1326         /*
1327          * Restoring config space is necessary even if the device is not bound
1328          * to a driver because although we left it in D0, it may have gone to
1329          * D3cold when the bridge above it runtime suspended.
1330          */
1331         pci_pm_default_resume_early(pci_dev);
1332
1333         if (!pci_dev->driver)
1334                 return 0;
1335
1336         pci_fixup_device(pci_fixup_resume_early, pci_dev);
1337         pci_pm_default_resume(pci_dev);
1338
1339         if (prev_state == PCI_D3cold)
1340                 pci_pm_bridge_power_up_actions(pci_dev);
1341
1342         if (pm && pm->runtime_resume)
1343                 error = pm->runtime_resume(dev);
1344
1345         pci_dev->runtime_d3cold = false;
1346
1347         return error;
1348 }
1349
1350 static int pci_pm_runtime_idle(struct device *dev)
1351 {
1352         struct pci_dev *pci_dev = to_pci_dev(dev);
1353         const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1354
1355         /*
1356          * If pci_dev->driver is not set (unbound), the device should
1357          * always remain in D0 regardless of the runtime PM status
1358          */
1359         if (!pci_dev->driver)
1360                 return 0;
1361
1362         if (!pm)
1363                 return -ENOSYS;
1364
1365         if (pm->runtime_idle)
1366                 return pm->runtime_idle(dev);
1367
1368         return 0;
1369 }
1370
1371 static const struct dev_pm_ops pci_dev_pm_ops = {
1372         .prepare = pci_pm_prepare,
1373         .complete = pci_pm_complete,
1374         .suspend = pci_pm_suspend,
1375         .suspend_late = pci_pm_suspend_late,
1376         .resume = pci_pm_resume,
1377         .resume_early = pci_pm_resume_early,
1378         .freeze = pci_pm_freeze,
1379         .thaw = pci_pm_thaw,
1380         .poweroff = pci_pm_poweroff,
1381         .poweroff_late = pci_pm_poweroff_late,
1382         .restore = pci_pm_restore,
1383         .suspend_noirq = pci_pm_suspend_noirq,
1384         .resume_noirq = pci_pm_resume_noirq,
1385         .freeze_noirq = pci_pm_freeze_noirq,
1386         .thaw_noirq = pci_pm_thaw_noirq,
1387         .poweroff_noirq = pci_pm_poweroff_noirq,
1388         .restore_noirq = pci_pm_restore_noirq,
1389         .runtime_suspend = pci_pm_runtime_suspend,
1390         .runtime_resume = pci_pm_runtime_resume,
1391         .runtime_idle = pci_pm_runtime_idle,
1392 };
1393
1394 #define PCI_PM_OPS_PTR  (&pci_dev_pm_ops)
1395
1396 #else /* !CONFIG_PM */
1397
1398 #define pci_pm_runtime_suspend  NULL
1399 #define pci_pm_runtime_resume   NULL
1400 #define pci_pm_runtime_idle     NULL
1401
1402 #define PCI_PM_OPS_PTR  NULL
1403
1404 #endif /* !CONFIG_PM */
1405
1406 /**
1407  * __pci_register_driver - register a new pci driver
1408  * @drv: the driver structure to register
1409  * @owner: owner module of drv
1410  * @mod_name: module name string
1411  *
1412  * Adds the driver structure to the list of registered drivers.
1413  * Returns a negative value on error, otherwise 0.
1414  * If no error occurred, the driver remains registered even if
1415  * no device was claimed during registration.
1416  */
1417 int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1418                           const char *mod_name)
1419 {
1420         /* initialize common driver fields */
1421         drv->driver.name = drv->name;
1422         drv->driver.bus = &pci_bus_type;
1423         drv->driver.owner = owner;
1424         drv->driver.mod_name = mod_name;
1425         drv->driver.groups = drv->groups;
1426         drv->driver.dev_groups = drv->dev_groups;
1427
1428         spin_lock_init(&drv->dynids.lock);
1429         INIT_LIST_HEAD(&drv->dynids.list);
1430
1431         /* register with core */
1432         return driver_register(&drv->driver);
1433 }
1434 EXPORT_SYMBOL(__pci_register_driver);
1435
1436 /**
1437  * pci_unregister_driver - unregister a pci driver
1438  * @drv: the driver structure to unregister
1439  *
1440  * Deletes the driver structure from the list of registered PCI drivers,
1441  * gives it a chance to clean up by calling its remove() function for
1442  * each device it was responsible for, and marks those devices as
1443  * driverless.
1444  */
1445
1446 void pci_unregister_driver(struct pci_driver *drv)
1447 {
1448         driver_unregister(&drv->driver);
1449         pci_free_dynids(drv);
1450 }
1451 EXPORT_SYMBOL(pci_unregister_driver);
1452
1453 static struct pci_driver pci_compat_driver = {
1454         .name = "compat"
1455 };
1456
1457 /**
1458  * pci_dev_driver - get the pci_driver of a device
1459  * @dev: the device to query
1460  *
1461  * Returns the appropriate pci_driver structure or %NULL if there is no
1462  * registered driver for the device.
1463  */
1464 struct pci_driver *pci_dev_driver(const struct pci_dev *dev)
1465 {
1466         if (dev->driver)
1467                 return dev->driver;
1468         else {
1469                 int i;
1470                 for (i = 0; i <= PCI_ROM_RESOURCE; i++)
1471                         if (dev->resource[i].flags & IORESOURCE_BUSY)
1472                                 return &pci_compat_driver;
1473         }
1474         return NULL;
1475 }
1476 EXPORT_SYMBOL(pci_dev_driver);
1477
1478 /**
1479  * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
1480  * @dev: the PCI device structure to match against
1481  * @drv: the device driver to search for matching PCI device id structures
1482  *
1483  * Used by a driver to check whether a PCI device present in the
1484  * system is in its list of supported devices. Returns the matching
1485  * pci_device_id structure or %NULL if there is no match.
1486  */
1487 static int pci_bus_match(struct device *dev, struct device_driver *drv)
1488 {
1489         struct pci_dev *pci_dev = to_pci_dev(dev);
1490         struct pci_driver *pci_drv;
1491         const struct pci_device_id *found_id;
1492
1493         if (!pci_dev->match_driver)
1494                 return 0;
1495
1496         pci_drv = to_pci_driver(drv);
1497         found_id = pci_match_device(pci_drv, pci_dev);
1498         if (found_id)
1499                 return 1;
1500
1501         return 0;
1502 }
1503
1504 /**
1505  * pci_dev_get - increments the reference count of the pci device structure
1506  * @dev: the device being referenced
1507  *
1508  * Each live reference to a device should be refcounted.
1509  *
1510  * Drivers for PCI devices should normally record such references in
1511  * their probe() methods, when they bind to a device, and release
1512  * them by calling pci_dev_put(), in their disconnect() methods.
1513  *
1514  * A pointer to the device with the incremented reference counter is returned.
1515  */
1516 struct pci_dev *pci_dev_get(struct pci_dev *dev)
1517 {
1518         if (dev)
1519                 get_device(&dev->dev);
1520         return dev;
1521 }
1522 EXPORT_SYMBOL(pci_dev_get);
1523
1524 /**
1525  * pci_dev_put - release a use of the pci device structure
1526  * @dev: device that's been disconnected
1527  *
1528  * Must be called when a user of a device is finished with it.  When the last
1529  * user of the device calls this function, the memory of the device is freed.
1530  */
1531 void pci_dev_put(struct pci_dev *dev)
1532 {
1533         if (dev)
1534                 put_device(&dev->dev);
1535 }
1536 EXPORT_SYMBOL(pci_dev_put);
1537
1538 static int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1539 {
1540         struct pci_dev *pdev;
1541
1542         if (!dev)
1543                 return -ENODEV;
1544
1545         pdev = to_pci_dev(dev);
1546
1547         if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class))
1548                 return -ENOMEM;
1549
1550         if (add_uevent_var(env, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device))
1551                 return -ENOMEM;
1552
1553         if (add_uevent_var(env, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor,
1554                            pdev->subsystem_device))
1555                 return -ENOMEM;
1556
1557         if (add_uevent_var(env, "PCI_SLOT_NAME=%s", pci_name(pdev)))
1558                 return -ENOMEM;
1559
1560         if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X",
1561                            pdev->vendor, pdev->device,
1562                            pdev->subsystem_vendor, pdev->subsystem_device,
1563                            (u8)(pdev->class >> 16), (u8)(pdev->class >> 8),
1564                            (u8)(pdev->class)))
1565                 return -ENOMEM;
1566
1567         return 0;
1568 }
1569
1570 #if defined(CONFIG_PCIEAER) || defined(CONFIG_EEH)
1571 /**
1572  * pci_uevent_ers - emit a uevent during recovery path of PCI device
1573  * @pdev: PCI device undergoing error recovery
1574  * @err_type: type of error event
1575  */
1576 void pci_uevent_ers(struct pci_dev *pdev, enum pci_ers_result err_type)
1577 {
1578         int idx = 0;
1579         char *envp[3];
1580
1581         switch (err_type) {
1582         case PCI_ERS_RESULT_NONE:
1583         case PCI_ERS_RESULT_CAN_RECOVER:
1584                 envp[idx++] = "ERROR_EVENT=BEGIN_RECOVERY";
1585                 envp[idx++] = "DEVICE_ONLINE=0";
1586                 break;
1587         case PCI_ERS_RESULT_RECOVERED:
1588                 envp[idx++] = "ERROR_EVENT=SUCCESSFUL_RECOVERY";
1589                 envp[idx++] = "DEVICE_ONLINE=1";
1590                 break;
1591         case PCI_ERS_RESULT_DISCONNECT:
1592                 envp[idx++] = "ERROR_EVENT=FAILED_RECOVERY";
1593                 envp[idx++] = "DEVICE_ONLINE=0";
1594                 break;
1595         default:
1596                 break;
1597         }
1598
1599         if (idx > 0) {
1600                 envp[idx++] = NULL;
1601                 kobject_uevent_env(&pdev->dev.kobj, KOBJ_CHANGE, envp);
1602         }
1603 }
1604 #endif
1605
1606 static int pci_bus_num_vf(struct device *dev)
1607 {
1608         return pci_num_vf(to_pci_dev(dev));
1609 }
1610
1611 /**
1612  * pci_dma_configure - Setup DMA configuration
1613  * @dev: ptr to dev structure
1614  *
1615  * Function to update PCI devices's DMA configuration using the same
1616  * info from the OF node or ACPI node of host bridge's parent (if any).
1617  */
1618 static int pci_dma_configure(struct device *dev)
1619 {
1620         struct device *bridge;
1621         int ret = 0;
1622
1623         bridge = pci_get_host_bridge_device(to_pci_dev(dev));
1624
1625         if (IS_ENABLED(CONFIG_OF) && bridge->parent &&
1626             bridge->parent->of_node) {
1627                 ret = of_dma_configure(dev, bridge->parent->of_node, true);
1628         } else if (has_acpi_companion(bridge)) {
1629                 struct acpi_device *adev = to_acpi_device_node(bridge->fwnode);
1630
1631                 ret = acpi_dma_configure(dev, acpi_get_dma_attr(adev));
1632         }
1633
1634         pci_put_host_bridge_device(bridge);
1635         return ret;
1636 }
1637
1638 struct bus_type pci_bus_type = {
1639         .name           = "pci",
1640         .match          = pci_bus_match,
1641         .uevent         = pci_uevent,
1642         .probe          = pci_device_probe,
1643         .remove         = pci_device_remove,
1644         .shutdown       = pci_device_shutdown,
1645         .dev_groups     = pci_dev_groups,
1646         .bus_groups     = pci_bus_groups,
1647         .drv_groups     = pci_drv_groups,
1648         .pm             = PCI_PM_OPS_PTR,
1649         .num_vf         = pci_bus_num_vf,
1650         .dma_configure  = pci_dma_configure,
1651 };
1652 EXPORT_SYMBOL(pci_bus_type);
1653
1654 #ifdef CONFIG_PCIEPORTBUS
1655 static int pcie_port_bus_match(struct device *dev, struct device_driver *drv)
1656 {
1657         struct pcie_device *pciedev;
1658         struct pcie_port_service_driver *driver;
1659
1660         if (drv->bus != &pcie_port_bus_type || dev->bus != &pcie_port_bus_type)
1661                 return 0;
1662
1663         pciedev = to_pcie_device(dev);
1664         driver = to_service_driver(drv);
1665
1666         if (driver->service != pciedev->service)
1667                 return 0;
1668
1669         if (driver->port_type != PCIE_ANY_PORT &&
1670             driver->port_type != pci_pcie_type(pciedev->port))
1671                 return 0;
1672
1673         return 1;
1674 }
1675
1676 struct bus_type pcie_port_bus_type = {
1677         .name           = "pci_express",
1678         .match          = pcie_port_bus_match,
1679 };
1680 EXPORT_SYMBOL_GPL(pcie_port_bus_type);
1681 #endif
1682
1683 static int __init pci_driver_init(void)
1684 {
1685         int ret;
1686
1687         ret = bus_register(&pci_bus_type);
1688         if (ret)
1689                 return ret;
1690
1691 #ifdef CONFIG_PCIEPORTBUS
1692         ret = bus_register(&pcie_port_bus_type);
1693         if (ret)
1694                 return ret;
1695 #endif
1696         dma_debug_add_bus(&pci_bus_type);
1697         return 0;
1698 }
1699 postcore_initcall(pci_driver_init);