blob: 7edd5c307446298994bd0b3660f9ffac6de4bd6d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/pci/pci-driver.c
3 *
Greg Kroah-Hartman2b937302007-11-28 12:23:18 -08004 * (C) Copyright 2002-2004, 2007 Greg Kroah-Hartman <greg@kroah.com>
5 * (C) Copyright 2007 Novell Inc.
6 *
7 * Released under the GPL v2 only.
8 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10
11#include <linux/pci.h>
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/device.h>
Andi Kleend42c6992005-07-06 19:56:03 +020015#include <linux/mempolicy.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080016#include <linux/string.h>
17#include <linux/slab.h>
Tim Schmielau8c65b4a2005-11-07 00:59:43 -080018#include <linux/sched.h>
Rusty Russell873392c2008-12-31 23:54:56 +103019#include <linux/cpu.h>
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +010020#include <linux/pm_runtime.h>
Rafael J. Wysockieea3fc032011-07-06 10:51:40 +020021#include <linux/suspend.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "pci.h"
23
Greg Kroah-Hartman75865852005-06-30 02:18:12 -070024struct pci_dynid {
25 struct list_head node;
26 struct pci_device_id id;
27};
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29/**
Tejun Heo9dba9102009-09-03 15:26:36 +090030 * pci_add_dynid - add a new PCI device ID to this driver and re-probe devices
31 * @drv: target pci driver
32 * @vendor: PCI vendor ID
33 * @device: PCI device ID
34 * @subvendor: PCI subvendor ID
35 * @subdevice: PCI subdevice ID
36 * @class: PCI class
37 * @class_mask: PCI class mask
38 * @driver_data: private driver data
39 *
40 * Adds a new dynamic pci device ID to this driver and causes the
41 * driver to probe for all devices again. @drv must have been
42 * registered prior to calling this function.
43 *
44 * CONTEXT:
45 * Does GFP_KERNEL allocation.
46 *
47 * RETURNS:
48 * 0 on success, -errno on failure.
49 */
50int pci_add_dynid(struct pci_driver *drv,
51 unsigned int vendor, unsigned int device,
52 unsigned int subvendor, unsigned int subdevice,
53 unsigned int class, unsigned int class_mask,
54 unsigned long driver_data)
55{
56 struct pci_dynid *dynid;
57 int retval;
58
59 dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
60 if (!dynid)
61 return -ENOMEM;
62
63 dynid->id.vendor = vendor;
64 dynid->id.device = device;
65 dynid->id.subvendor = subvendor;
66 dynid->id.subdevice = subdevice;
67 dynid->id.class = class;
68 dynid->id.class_mask = class_mask;
69 dynid->id.driver_data = driver_data;
70
71 spin_lock(&drv->dynids.lock);
72 list_add_tail(&dynid->node, &drv->dynids.list);
73 spin_unlock(&drv->dynids.lock);
74
Tejun Heo9dba9102009-09-03 15:26:36 +090075 retval = driver_attach(&drv->driver);
Tejun Heo9dba9102009-09-03 15:26:36 +090076
77 return retval;
78}
79
80static 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
Tejun Heo9dba9102009-09-03 15:26:36 +090092/**
93 * store_new_id - sysfs frontend to pci_add_dynid()
Randy Dunlap8f7020d2005-10-23 11:57:38 -070094 * @driver: target device driver
95 * @buf: buffer for scanning device ID data
96 * @count: input size
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Tejun Heo9dba9102009-09-03 15:26:36 +090098 * Allow PCI IDs to be added to an existing driver via sysfs.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
Randy Dunlapf8eb1002005-10-28 20:36:51 -0700100static ssize_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101store_new_id(struct device_driver *driver, const char *buf, size_t count)
102{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct pci_driver *pdrv = to_pci_driver(driver);
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200104 const struct pci_device_id *ids = pdrv->id_table;
Jean Delvare6ba18632007-04-07 17:21:28 +0200105 __u32 vendor, device, subvendor=PCI_ANY_ID,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 subdevice=PCI_ANY_ID, class=0, class_mask=0;
107 unsigned long driver_data=0;
108 int fields=0;
Tejun Heo9dba9102009-09-03 15:26:36 +0900109 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200111 fields = sscanf(buf, "%x %x %x %x %x %x %lx",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 &vendor, &device, &subvendor, &subdevice,
113 &class, &class_mask, &driver_data);
Jean Delvare6ba18632007-04-07 17:21:28 +0200114 if (fields < 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return -EINVAL;
116
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200117 /* Only accept driver_data values that match an existing id_table
118 entry */
Chris Wright2debb4d2008-11-25 19:36:10 -0800119 if (ids) {
120 retval = -EINVAL;
121 while (ids->vendor || ids->subvendor || ids->class_mask) {
122 if (driver_data == ids->driver_data) {
123 retval = 0;
124 break;
125 }
126 ids++;
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200127 }
Chris Wright2debb4d2008-11-25 19:36:10 -0800128 if (retval) /* No match */
129 return retval;
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200130 }
Jean Delvareb41d6cf2008-08-17 21:06:59 +0200131
Tejun Heo9dba9102009-09-03 15:26:36 +0900132 retval = pci_add_dynid(pdrv, vendor, device, subvendor, subdevice,
133 class, class_mask, driver_data);
Greg Kroah-Hartmanb19441a2006-08-28 11:43:25 -0700134 if (retval)
135 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 return count;
137}
Greg Kroah-Hartman2229c1f2013-10-07 14:51:20 -0600138static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Chris Wright09943752009-02-23 21:52:23 -0800140/**
141 * store_remove_id - remove a PCI device ID from this driver
142 * @driver: target device driver
143 * @buf: buffer for scanning device ID data
144 * @count: input size
145 *
146 * Removes a dynamic pci device ID to this driver.
147 */
148static ssize_t
149store_remove_id(struct device_driver *driver, const char *buf, size_t count)
150{
151 struct pci_dynid *dynid, *n;
152 struct pci_driver *pdrv = to_pci_driver(driver);
153 __u32 vendor, device, subvendor = PCI_ANY_ID,
154 subdevice = PCI_ANY_ID, class = 0, class_mask = 0;
155 int fields = 0;
156 int retval = -ENODEV;
157
158 fields = sscanf(buf, "%x %x %x %x %x %x",
159 &vendor, &device, &subvendor, &subdevice,
160 &class, &class_mask);
161 if (fields < 2)
162 return -EINVAL;
163
164 spin_lock(&pdrv->dynids.lock);
165 list_for_each_entry_safe(dynid, n, &pdrv->dynids.list, node) {
166 struct pci_device_id *id = &dynid->id;
167 if ((id->vendor == vendor) &&
168 (id->device == device) &&
169 (subvendor == PCI_ANY_ID || id->subvendor == subvendor) &&
170 (subdevice == PCI_ANY_ID || id->subdevice == subdevice) &&
171 !((id->class ^ class) & class_mask)) {
172 list_del(&dynid->node);
173 kfree(dynid);
174 retval = 0;
175 break;
176 }
177 }
178 spin_unlock(&pdrv->dynids.lock);
179
180 if (retval)
181 return retval;
182 return count;
183}
Greg Kroah-Hartman2229c1f2013-10-07 14:51:20 -0600184static DRIVER_ATTR(remove_id, S_IWUSR, NULL, store_remove_id);
Chris Wright09943752009-02-23 21:52:23 -0800185
Greg Kroah-Hartman2229c1f2013-10-07 14:51:20 -0600186static struct attribute *pci_drv_attrs[] = {
187 &driver_attr_new_id.attr,
188 &driver_attr_remove_id.attr,
189 NULL,
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +0400190};
Greg Kroah-Hartman2229c1f2013-10-07 14:51:20 -0600191ATTRIBUTE_GROUPS(pci_drv);
Alan Sterned283e92012-01-24 14:35:13 -0500192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/**
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700194 * pci_match_id - See if a pci device matches a given pci_id table
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 * @ids: array of PCI device id structures to search in
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700196 * @dev: the PCI device structure to match against.
197 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * Used by a driver to check whether a PCI device present in the
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700199 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 * pci_device_id structure or %NULL if there is no match.
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700201 *
Randy Dunlap8b607562007-05-09 07:19:14 +0200202 * Deprecated, don't use this as it will not catch any dynamic ids
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700203 * that a driver might want to check for.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700205const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
206 struct pci_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700208 if (ids) {
209 while (ids->vendor || ids->subvendor || ids->class_mask) {
210 if (pci_match_one_device(ids, dev))
211 return ids;
212 ids++;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 return NULL;
216}
217
218/**
Randy Dunlapae9608a2007-01-09 21:41:01 -0800219 * pci_match_device - Tell if a PCI device structure has a matching PCI device id structure
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700220 * @drv: the PCI driver to match against
Henrik Kretzschmar39ba4872006-08-15 10:57:16 +0200221 * @dev: the PCI device structure to match against
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700222 *
223 * Used by a driver to check whether a PCI device present in the
224 * system is in its list of supported devices. Returns the matching
225 * pci_device_id structure or %NULL if there is no match.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
Adrian Bunkd73460d2007-10-24 18:27:18 +0200227static const struct pci_device_id *pci_match_device(struct pci_driver *drv,
228 struct pci_dev *dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700229{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700230 struct pci_dynid *dynid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Russell King7461b602006-11-29 21:18:04 +0000232 /* Look at the dynamic ids first, before the static ones */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700233 spin_lock(&drv->dynids.lock);
234 list_for_each_entry(dynid, &drv->dynids.list, node) {
235 if (pci_match_one_device(&dynid->id, dev)) {
236 spin_unlock(&drv->dynids.lock);
237 return &dynid->id;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700240 spin_unlock(&drv->dynids.lock);
Russell King7461b602006-11-29 21:18:04 +0000241
242 return pci_match_id(drv->id_table, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
Rusty Russell873392c2008-12-31 23:54:56 +1030245struct drv_dev_and_id {
246 struct pci_driver *drv;
247 struct pci_dev *dev;
248 const struct pci_device_id *id;
249};
250
251static long local_pci_probe(void *_ddi)
252{
253 struct drv_dev_and_id *ddi = _ddi;
Huang Ying967577b2012-11-20 16:08:22 +0800254 struct pci_dev *pci_dev = ddi->dev;
255 struct pci_driver *pci_drv = ddi->drv;
256 struct device *dev = &pci_dev->dev;
Alan Sternf3ec4f82010-06-08 15:23:51 -0400257 int rc;
Rusty Russell873392c2008-12-31 23:54:56 +1030258
Huang Ying967577b2012-11-20 16:08:22 +0800259 /*
260 * Unbound PCI devices are always put in D0, regardless of
261 * runtime PM status. During probe, the device is set to
262 * active and the usage count is incremented. If the driver
263 * supports runtime PM, it should call pm_runtime_put_noidle()
264 * in its probe routine and pm_runtime_get_noresume() in its
265 * remove routine.
Alan Sternf3ec4f82010-06-08 15:23:51 -0400266 */
Huang Ying967577b2012-11-20 16:08:22 +0800267 pm_runtime_get_sync(dev);
268 pci_dev->driver = pci_drv;
269 rc = pci_drv->probe(pci_dev, ddi->id);
Stephen M. Cameronf92d74c2013-11-01 14:34:55 -0500270 if (!rc)
271 return rc;
272 if (rc < 0) {
Huang Ying967577b2012-11-20 16:08:22 +0800273 pci_dev->driver = NULL;
274 pm_runtime_put_sync(dev);
Stephen M. Cameronf92d74c2013-11-01 14:34:55 -0500275 return rc;
Alan Sternf3ec4f82010-06-08 15:23:51 -0400276 }
Stephen M. Cameronf92d74c2013-11-01 14:34:55 -0500277 /*
278 * Probe function should return < 0 for failure, 0 for success
279 * Treat values > 0 as success, but warn.
280 */
281 dev_warn(dev, "Driver probe function unexpectedly returned %d\n", rc);
282 return 0;
Rusty Russell873392c2008-12-31 23:54:56 +1030283}
284
Andi Kleend42c6992005-07-06 19:56:03 +0200285static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
286 const struct pci_device_id *id)
287{
Rusty Russell873392c2008-12-31 23:54:56 +1030288 int error, node;
289 struct drv_dev_and_id ddi = { drv, dev, id };
Mike Travisf70316d2008-04-04 18:11:06 -0700290
Alexander Duyck12c31562013-11-18 10:59:59 -0700291 /*
292 * Execute driver initialization on node where the device is
293 * attached. This way the driver likely allocates its local memory
294 * on the right node.
295 */
Rusty Russell873392c2008-12-31 23:54:56 +1030296 node = dev_to_node(&dev->dev);
Alexander Duyck12c31562013-11-18 10:59:59 -0700297
298 /*
299 * On NUMA systems, we are likely to call a PF probe function using
300 * work_on_cpu(). If that probe calls pci_enable_sriov() (which
301 * adds the VF devices via pci_bus_add_device()), we may re-enter
302 * this function to call the VF probe function. Calling
303 * work_on_cpu() again will cause a lockdep warning. Since VFs are
304 * always on the same node as the PF, we can work around this by
305 * avoiding work_on_cpu() when we're already on the correct node.
306 *
307 * Preemption is enabled, so it's theoretically unsafe to use
308 * numa_node_id(), but even if we run the probe function on the
309 * wrong node, it should be functionally correct.
310 */
311 if (node >= 0 && node != numa_node_id()) {
Rusty Russell873392c2008-12-31 23:54:56 +1030312 int cpu;
Rusty Russell873392c2008-12-31 23:54:56 +1030313
314 get_online_cpus();
Rusty Russella70f7302009-03-13 14:49:46 +1030315 cpu = cpumask_any_and(cpumask_of_node(node), cpu_online_mask);
Rusty Russell873392c2008-12-31 23:54:56 +1030316 if (cpu < nr_cpu_ids)
317 error = work_on_cpu(cpu, local_pci_probe, &ddi);
318 else
319 error = local_pci_probe(&ddi);
320 put_online_cpus();
321 } else
322 error = local_pci_probe(&ddi);
Alexander Duyck12c31562013-11-18 10:59:59 -0700323
Andi Kleend42c6992005-07-06 19:56:03 +0200324 return error;
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/**
Randy Dunlap23ea3792010-11-18 15:02:31 -0800328 * __pci_device_probe - check if a driver wants to claim a specific PCI device
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700329 * @drv: driver to call to check if it wants the PCI device
330 * @pci_dev: PCI device being probed
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700331 *
Randy Dunlap8f7020d2005-10-23 11:57:38 -0700332 * returns 0 on success, else error.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
334 */
335static int
336__pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700337{
338 const struct pci_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int error = 0;
340
341 if (!pci_dev->driver && drv->probe) {
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700342 error = -ENODEV;
343
344 id = pci_match_device(drv, pci_dev);
345 if (id)
Andi Kleend42c6992005-07-06 19:56:03 +0200346 error = pci_call_probe(drv, pci_dev, id);
Huang Ying967577b2012-11-20 16:08:22 +0800347 if (error >= 0)
Greg Kroah-Hartman75865852005-06-30 02:18:12 -0700348 error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350 return error;
351}
352
353static int pci_device_probe(struct device * dev)
354{
355 int error = 0;
356 struct pci_driver *drv;
357 struct pci_dev *pci_dev;
358
359 drv = to_pci_driver(dev->driver);
360 pci_dev = to_pci_dev(dev);
361 pci_dev_get(pci_dev);
362 error = __pci_device_probe(drv, pci_dev);
363 if (error)
364 pci_dev_put(pci_dev);
365
366 return error;
367}
368
369static int pci_device_remove(struct device * dev)
370{
371 struct pci_dev * pci_dev = to_pci_dev(dev);
372 struct pci_driver * drv = pci_dev->driver;
373
374 if (drv) {
Alan Sternf3ec4f82010-06-08 15:23:51 -0400375 if (drv->remove) {
376 pm_runtime_get_sync(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 drv->remove(pci_dev);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400378 pm_runtime_put_noidle(dev);
379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 pci_dev->driver = NULL;
381 }
382
Alan Sternf3ec4f82010-06-08 15:23:51 -0400383 /* Undo the runtime PM settings in local_pci_probe() */
Huang Ying967577b2012-11-20 16:08:22 +0800384 pm_runtime_put_sync(dev);
Alan Sternf3ec4f82010-06-08 15:23:51 -0400385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 /*
Shaohua Li2449e062006-10-20 14:45:32 -0700387 * If the device is still on, set the power state as "unknown",
388 * since it might change by the next time we load the driver.
389 */
390 if (pci_dev->current_state == PCI_D0)
391 pci_dev->current_state = PCI_UNKNOWN;
392
393 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 * We would love to complain here if pci_dev->is_enabled is set, that
395 * the driver should have called pci_disable_device(), but the
396 * unfortunate fact is there are too many odd BIOS and bridge setups
Bjorn Helgaasf7625982013-11-14 11:28:18 -0700397 * that don't like drivers doing that all of the time.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * Oh well, we can dream of sane hardware when we sleep, no matter how
399 * horrible the crap we have to deal with is when we are awake...
400 */
401
402 pci_dev_put(pci_dev);
403 return 0;
404}
405
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200406static void pci_device_shutdown(struct device *dev)
407{
408 struct pci_dev *pci_dev = to_pci_dev(dev);
409 struct pci_driver *drv = pci_dev->driver;
410
Huang Ying3ff2de92012-10-24 14:54:14 +0800411 pm_runtime_resume(dev);
412
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200413 if (drv && drv->shutdown)
414 drv->shutdown(pci_dev);
415 pci_msi_shutdown(pci_dev);
416 pci_msix_shutdown(pci_dev);
Rafael J. Wysocki5b415f12012-02-07 00:50:35 +0100417
418 /*
Khalid Azizb566a222012-04-27 13:00:33 -0600419 * Turn off Bus Master bit on the device to tell it to not
Konstantin Khlebnikov6e0eda32013-03-14 18:49:37 +0400420 * continue to do DMA. Don't touch devices in D3cold or unknown states.
Khalid Azizb566a222012-04-27 13:00:33 -0600421 */
Konstantin Khlebnikov6e0eda32013-03-14 18:49:37 +0400422 if (pci_dev->current_state <= PCI_D3hot)
423 pci_clear_master(pci_dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200424}
425
Rafael J. Wysockiaa338602011-02-11 00:06:54 +0100426#ifdef CONFIG_PM
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100427
428/* Auxiliary functions used for system resume and run-time resume. */
429
430/**
431 * pci_restore_standard_config - restore standard config registers of PCI device
432 * @pci_dev: PCI device to handle
433 */
434static int pci_restore_standard_config(struct pci_dev *pci_dev)
435{
436 pci_update_current_state(pci_dev, PCI_UNKNOWN);
437
438 if (pci_dev->current_state != PCI_D0) {
439 int error = pci_set_power_state(pci_dev, PCI_D0);
440 if (error)
441 return error;
442 }
443
Jon Mason1d3c16a2010-11-30 17:43:26 -0600444 pci_restore_state(pci_dev);
445 return 0;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100446}
447
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100448#endif
449
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200450#ifdef CONFIG_PM_SLEEP
451
Rafael J. Wysockidb288c92012-07-05 15:20:00 -0600452static void pci_pm_default_resume_early(struct pci_dev *pci_dev)
453{
454 pci_power_up(pci_dev);
455 pci_restore_state(pci_dev);
456 pci_fixup_device(pci_fixup_resume_early, pci_dev);
457}
458
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200459/*
460 * Default "suspend" method for devices that have no driver provided suspend,
Rafael J. Wysockifa58d302009-01-07 13:03:42 +0100461 * or not even a driver at all (second part).
462 */
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100463static void pci_pm_set_unknown_state(struct pci_dev *pci_dev)
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200464{
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200465 /*
466 * mark its power state as "unknown", since we don't know if
467 * e.g. the BIOS will change its device state when we suspend.
468 */
469 if (pci_dev->current_state == PCI_D0)
470 pci_dev->current_state = PCI_UNKNOWN;
471}
472
473/*
474 * Default "resume" method for devices that have no driver provided resume,
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100475 * or not even a driver at all (second part).
476 */
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100477static int pci_pm_reenable_device(struct pci_dev *pci_dev)
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100478{
479 int retval;
480
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200481 /* if the device was enabled before suspend, reenable */
482 retval = pci_reenable_device(pci_dev);
483 /*
484 * if the device was busmaster before the suspend, make it busmaster
485 * again
486 */
487 if (pci_dev->is_busmaster)
488 pci_set_master(pci_dev);
489
490 return retval;
491}
492
493static int pci_legacy_suspend(struct device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct pci_dev * pci_dev = to_pci_dev(dev);
496 struct pci_driver * drv = pci_dev->driver;
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100497
Andrew Morton02669492006-03-23 01:38:34 -0800498 if (drv && drv->suspend) {
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100499 pci_power_t prev = pci_dev->current_state;
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100500 int error;
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100501
Frans Pop57ef8022009-03-16 22:39:56 +0100502 error = drv->suspend(pci_dev, state);
503 suspend_report_result(drv->suspend, error);
504 if (error)
505 return error;
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100506
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100507 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100508 && pci_dev->current_state != PCI_UNKNOWN) {
509 WARN_ONCE(pci_dev->current_state != prev,
510 "PCI PM: Device state not saved by %pF\n",
511 drv->suspend);
Rafael J. Wysocki99dadce2009-02-04 01:59:09 +0100512 }
Andrew Morton02669492006-03-23 01:38:34 -0800513 }
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100514
515 pci_fixup_device(pci_fixup_suspend, pci_dev);
516
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100517 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518}
519
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200520static int pci_legacy_suspend_late(struct device *dev, pm_message_t state)
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700521{
522 struct pci_dev * pci_dev = to_pci_dev(dev);
523 struct pci_driver * drv = pci_dev->driver;
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700524
525 if (drv && drv->suspend_late) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100526 pci_power_t prev = pci_dev->current_state;
527 int error;
528
Frans Pop57ef8022009-03-16 22:39:56 +0100529 error = drv->suspend_late(pci_dev, state);
530 suspend_report_result(drv->suspend_late, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100531 if (error)
532 return error;
533
534 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
535 && pci_dev->current_state != PCI_UNKNOWN) {
536 WARN_ONCE(pci_dev->current_state != prev,
537 "PCI PM: Device state not saved by %pF\n",
538 drv->suspend_late);
539 return 0;
540 }
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700541 }
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100542
543 if (!pci_dev->state_saved)
544 pci_save_state(pci_dev);
545
546 pci_pm_set_unknown_state(pci_dev);
547
548 return 0;
Linus Torvaldscbd69db2006-06-24 14:50:29 -0700549}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100551static int pci_legacy_resume_early(struct device *dev)
552{
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100553 struct pci_dev * pci_dev = to_pci_dev(dev);
554 struct pci_driver * drv = pci_dev->driver;
555
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100556 return drv && drv->resume_early ?
557 drv->resume_early(pci_dev) : 0;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100558}
559
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200560static int pci_legacy_resume(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561{
562 struct pci_dev * pci_dev = to_pci_dev(dev);
563 struct pci_driver * drv = pci_dev->driver;
564
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100565 pci_fixup_device(pci_fixup_resume, pci_dev);
566
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100567 return drv && drv->resume ?
568 drv->resume(pci_dev) : pci_pm_reenable_device(pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100571/* Auxiliary functions used by the new power management framework */
572
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100573static void pci_pm_default_resume(struct pci_dev *pci_dev)
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100574{
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100575 pci_fixup_device(pci_fixup_resume, pci_dev);
576
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100577 if (!pci_is_bridge(pci_dev))
578 pci_enable_wake(pci_dev, PCI_D0, false);
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100579}
580
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100581static void pci_pm_default_suspend(struct pci_dev *pci_dev)
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100582{
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100583 /* Disable non-bridge devices without PM support */
Rafael J. Wysockicbbc2f62009-02-04 02:01:15 +0100584 if (!pci_is_bridge(pci_dev))
585 pci_disable_enabled_device(pci_dev);
Rafael J. Wysocki734104292009-01-07 13:07:15 +0100586}
587
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100588static bool pci_has_legacy_pm_support(struct pci_dev *pci_dev)
589{
590 struct pci_driver *drv = pci_dev->driver;
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100591 bool ret = drv && (drv->suspend || drv->suspend_late || drv->resume
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100592 || drv->resume_early);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100593
594 /*
595 * Legacy PM support is used by default, so warn if the new framework is
596 * supported as well. Drivers are supposed to support either the
597 * former, or the latter, but not both at the same time.
598 */
David Fries82440a82011-11-20 15:29:46 -0600599 WARN(ret && drv->driver.pm, "driver %s device %04x:%04x\n",
600 drv->name, pci_dev->vendor, pci_dev->device);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100601
602 return ret;
Rafael J. Wysocki07e836e2009-01-07 13:06:10 +0100603}
604
Rafael J. Wysocki571ff752009-01-07 13:05:05 +0100605/* New power management framework */
606
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200607static int pci_pm_prepare(struct device *dev)
608{
609 struct device_driver *drv = dev->driver;
610 int error = 0;
611
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100612 /*
613 * PCI devices suspended at run time need to be resumed at this
614 * point, because in general it is necessary to reconfigure them for
615 * system suspend. Namely, if the device is supposed to wake up the
616 * system from the sleep state, we may need to reconfigure it for this
617 * purpose. In turn, if the device is not supposed to wake up the
618 * system from the sleep state, we'll have to prevent it from signaling
619 * wake-up.
620 */
Rafael J. Wysockieea3fc032011-07-06 10:51:40 +0200621 pm_runtime_resume(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100622
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200623 if (drv && drv->pm && drv->pm->prepare)
624 error = drv->pm->prepare(dev);
625
626 return error;
627}
628
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200629
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100630#else /* !CONFIG_PM_SLEEP */
631
632#define pci_pm_prepare NULL
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100633
634#endif /* !CONFIG_PM_SLEEP */
635
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200636#ifdef CONFIG_SUSPEND
637
638static int pci_pm_suspend(struct device *dev)
639{
640 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700641 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200642
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100643 if (pci_has_legacy_pm_support(pci_dev))
644 return pci_legacy_suspend(dev, PMSG_SUSPEND);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100645
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100646 if (!pm) {
647 pci_pm_default_suspend(pci_dev);
648 goto Fixup;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200649 }
Rafael J. Wysockifa58d302009-01-07 13:03:42 +0100650
Rafael J. Wysocki82fee4d2013-02-04 15:56:05 +0400651 pci_dev->state_saved = false;
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100652 if (pm->suspend) {
653 pci_power_t prev = pci_dev->current_state;
654 int error;
655
656 error = pm->suspend(dev);
657 suspend_report_result(pm->suspend, error);
658 if (error)
659 return error;
660
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100661 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100662 && pci_dev->current_state != PCI_UNKNOWN) {
663 WARN_ONCE(pci_dev->current_state != prev,
664 "PCI PM: State of device not saved by %pF\n",
665 pm->suspend);
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100666 }
667 }
668
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100669 Fixup:
670 pci_fixup_device(pci_fixup_suspend, pci_dev);
671
672 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200673}
674
675static int pci_pm_suspend_noirq(struct device *dev)
Greg KHc8958172005-04-08 14:53:31 +0900676{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100677 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700678 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Greg KHc8958172005-04-08 14:53:31 +0900679
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100680 if (pci_has_legacy_pm_support(pci_dev))
681 return pci_legacy_suspend_late(dev, PMSG_SUSPEND);
682
Rafael J. Wysocki931ff682009-03-16 22:40:50 +0100683 if (!pm) {
684 pci_save_state(pci_dev);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100685 return 0;
Rafael J. Wysocki931ff682009-03-16 22:40:50 +0100686 }
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100687
688 if (pm->suspend_noirq) {
689 pci_power_t prev = pci_dev->current_state;
690 int error;
691
692 error = pm->suspend_noirq(dev);
693 suspend_report_result(pm->suspend_noirq, error);
694 if (error)
695 return error;
696
697 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
698 && pci_dev->current_state != PCI_UNKNOWN) {
699 WARN_ONCE(pci_dev->current_state != prev,
700 "PCI PM: State of device not saved by %pF\n",
701 pm->suspend_noirq);
702 return 0;
703 }
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200704 }
705
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100706 if (!pci_dev->state_saved) {
707 pci_save_state(pci_dev);
708 if (!pci_is_bridge(pci_dev))
709 pci_prepare_to_sleep(pci_dev);
710 }
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100711
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100712 pci_pm_set_unknown_state(pci_dev);
713
Alan Sterndbf0e4c2012-07-09 11:09:21 -0400714 /*
715 * Some BIOSes from ASUS have a bug: If a USB EHCI host controller's
716 * PCI COMMAND register isn't 0, the BIOS assumes that the controller
717 * hasn't been quiesced and tries to turn it off. If the controller
718 * is already in D3, this can hang or cause memory corruption.
719 *
720 * Since the value of the COMMAND register doesn't matter once the
721 * device has been suspended, we can safely set it to 0 here.
722 */
723 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
724 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
725
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100726 return 0;
Greg KHc8958172005-04-08 14:53:31 +0900727}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200729static int pci_pm_resume_noirq(struct device *dev)
730{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100731 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200732 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200733 int error = 0;
734
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100735 pci_pm_default_resume_early(pci_dev);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100736
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100737 if (pci_has_legacy_pm_support(pci_dev))
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100738 return pci_legacy_resume_early(dev);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100739
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100740 if (drv && drv->pm && drv->pm->resume_noirq)
741 error = drv->pm->resume_noirq(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200742
743 return error;
744}
745
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100746static int pci_pm_resume(struct device *dev)
747{
748 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700749 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100750 int error = 0;
751
Rafael J. Wysocki418e4da2009-01-26 21:43:08 +0100752 /*
753 * This is necessary for the suspend error path in which resume is
754 * called without restoring the standard config registers of the device.
755 */
756 if (pci_dev->state_saved)
757 pci_restore_standard_config(pci_dev);
758
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100759 if (pci_has_legacy_pm_support(pci_dev))
760 return pci_legacy_resume(dev);
761
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100762 pci_pm_default_resume(pci_dev);
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100763
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100764 if (pm) {
765 if (pm->resume)
766 error = pm->resume(dev);
767 } else {
768 pci_pm_reenable_device(pci_dev);
769 }
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100770
Rafael J. Wysocki999cce42009-09-09 23:51:27 +0200771 return error;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100772}
773
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200774#else /* !CONFIG_SUSPEND */
775
776#define pci_pm_suspend NULL
777#define pci_pm_suspend_noirq NULL
778#define pci_pm_resume NULL
779#define pci_pm_resume_noirq NULL
780
781#endif /* !CONFIG_SUSPEND */
782
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +0200783#ifdef CONFIG_HIBERNATE_CALLBACKS
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200784
Sebastian Ott699c1982013-08-20 16:41:02 +0200785
786/*
787 * pcibios_pm_ops - provide arch-specific hooks when a PCI device is doing
788 * a hibernate transition
789 */
790struct dev_pm_ops __weak pcibios_pm_ops;
791
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200792static int pci_pm_freeze(struct device *dev)
793{
794 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700795 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200796
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100797 if (pci_has_legacy_pm_support(pci_dev))
798 return pci_legacy_suspend(dev, PMSG_FREEZE);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100799
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100800 if (!pm) {
801 pci_pm_default_suspend(pci_dev);
802 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200803 }
804
Rafael J. Wysocki82fee4d2013-02-04 15:56:05 +0400805 pci_dev->state_saved = false;
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100806 if (pm->freeze) {
807 int error;
808
809 error = pm->freeze(dev);
810 suspend_report_result(pm->freeze, error);
811 if (error)
812 return error;
813 }
814
Sebastian Ott699c1982013-08-20 16:41:02 +0200815 if (pcibios_pm_ops.freeze)
816 return pcibios_pm_ops.freeze(dev);
817
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100818 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200819}
820
821static int pci_pm_freeze_noirq(struct device *dev)
822{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100823 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200824 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200825
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100826 if (pci_has_legacy_pm_support(pci_dev))
827 return pci_legacy_suspend_late(dev, PMSG_FREEZE);
828
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100829 if (drv && drv->pm && drv->pm->freeze_noirq) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100830 int error;
831
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100832 error = drv->pm->freeze_noirq(dev);
833 suspend_report_result(drv->pm->freeze_noirq, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100834 if (error)
835 return error;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200836 }
837
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100838 if (!pci_dev->state_saved)
839 pci_save_state(pci_dev);
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100840
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100841 pci_pm_set_unknown_state(pci_dev);
842
Sebastian Ott699c1982013-08-20 16:41:02 +0200843 if (pcibios_pm_ops.freeze_noirq)
844 return pcibios_pm_ops.freeze_noirq(dev);
845
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100846 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200847}
848
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200849static int pci_pm_thaw_noirq(struct device *dev)
850{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100851 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200852 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200853 int error = 0;
854
Sebastian Ott699c1982013-08-20 16:41:02 +0200855 if (pcibios_pm_ops.thaw_noirq) {
856 error = pcibios_pm_ops.thaw_noirq(dev);
857 if (error)
858 return error;
859 }
860
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100861 if (pci_has_legacy_pm_support(pci_dev))
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100862 return pci_legacy_resume_early(dev);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100863
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100864 pci_update_current_state(pci_dev, PCI_D0);
865
866 if (drv && drv->pm && drv->pm->thaw_noirq)
867 error = drv->pm->thaw_noirq(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200868
869 return error;
870}
871
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100872static int pci_pm_thaw(struct device *dev)
873{
874 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700875 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100876 int error = 0;
877
Sebastian Ott699c1982013-08-20 16:41:02 +0200878 if (pcibios_pm_ops.thaw) {
879 error = pcibios_pm_ops.thaw(dev);
880 if (error)
881 return error;
882 }
883
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100884 if (pci_has_legacy_pm_support(pci_dev))
885 return pci_legacy_resume(dev);
886
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100887 if (pm) {
888 if (pm->thaw)
889 error = pm->thaw(dev);
890 } else {
891 pci_pm_reenable_device(pci_dev);
892 }
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100893
Rafael J. Wysocki4b77b0a2009-09-09 23:49:59 +0200894 pci_dev->state_saved = false;
895
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100896 return error;
897}
898
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200899static int pci_pm_poweroff(struct device *dev)
900{
Rafael J. Wysocki355a72d2008-12-08 00:34:57 +0100901 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700902 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200903
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100904 if (pci_has_legacy_pm_support(pci_dev))
905 return pci_legacy_suspend(dev, PMSG_HIBERNATE);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100906
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100907 if (!pm) {
908 pci_pm_default_suspend(pci_dev);
909 goto Fixup;
910 }
911
Rafael J. Wysocki82fee4d2013-02-04 15:56:05 +0400912 pci_dev->state_saved = false;
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100913 if (pm->poweroff) {
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100914 int error;
915
Rafael J. Wysockiddb7c9d2009-02-04 01:56:14 +0100916 error = pm->poweroff(dev);
917 suspend_report_result(pm->poweroff, error);
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100918 if (error)
919 return error;
920 }
921
922 Fixup:
923 pci_fixup_device(pci_fixup_suspend, pci_dev);
924
Sebastian Ott699c1982013-08-20 16:41:02 +0200925 if (pcibios_pm_ops.poweroff)
926 return pcibios_pm_ops.poweroff(dev);
927
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100928 return 0;
929}
930
931static int pci_pm_poweroff_noirq(struct device *dev)
932{
933 struct pci_dev *pci_dev = to_pci_dev(dev);
934 struct device_driver *drv = dev->driver;
935
936 if (pci_has_legacy_pm_support(to_pci_dev(dev)))
937 return pci_legacy_suspend_late(dev, PMSG_HIBERNATE);
938
939 if (!drv || !drv->pm)
940 return 0;
941
942 if (drv->pm->poweroff_noirq) {
943 int error;
944
945 error = drv->pm->poweroff_noirq(dev);
946 suspend_report_result(drv->pm->poweroff_noirq, error);
947 if (error)
948 return error;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200949 }
950
Rafael J. Wysocki5294e252009-02-04 02:09:07 +0100951 if (!pci_dev->state_saved && !pci_is_bridge(pci_dev))
952 pci_prepare_to_sleep(pci_dev);
953
Rafael J. Wysocki0b68c8e2012-08-12 23:26:07 +0200954 /*
955 * The reason for doing this here is the same as for the analogous code
956 * in pci_pm_suspend_noirq().
957 */
958 if (pci_dev->class == PCI_CLASS_SERIAL_USB_EHCI)
959 pci_write_config_word(pci_dev, PCI_COMMAND, 0);
960
Sebastian Ott699c1982013-08-20 16:41:02 +0200961 if (pcibios_pm_ops.poweroff_noirq)
962 return pcibios_pm_ops.poweroff_noirq(dev);
963
Rafael J. Wysocki46939f82009-03-16 22:40:26 +0100964 return 0;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200965}
966
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200967static int pci_pm_restore_noirq(struct device *dev)
968{
969 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysockiadf09492008-10-06 22:46:05 +0200970 struct device_driver *drv = dev->driver;
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200971 int error = 0;
972
Sebastian Ott699c1982013-08-20 16:41:02 +0200973 if (pcibios_pm_ops.restore_noirq) {
974 error = pcibios_pm_ops.restore_noirq(dev);
975 if (error)
976 return error;
977 }
978
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +0100979 pci_pm_default_resume_early(pci_dev);
Rafael J. Wysockiaa8c6c92009-01-16 21:54:43 +0100980
Rafael J. Wysockiad8cfa12009-01-07 13:09:37 +0100981 if (pci_has_legacy_pm_support(pci_dev))
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100982 return pci_legacy_resume_early(dev);
Rafael J. Wysockibb808942009-01-07 14:15:17 +0100983
Rafael J. Wysockid67e37d2009-01-07 13:11:28 +0100984 if (drv && drv->pm && drv->pm->restore_noirq)
985 error = drv->pm->restore_noirq(dev);
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +0200986
987 return error;
988}
989
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100990static int pci_pm_restore(struct device *dev)
991{
992 struct pci_dev *pci_dev = to_pci_dev(dev);
Dmitry Torokhov8150f322009-07-24 22:11:32 -0700993 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +0100994 int error = 0;
995
Sebastian Ott699c1982013-08-20 16:41:02 +0200996 if (pcibios_pm_ops.restore) {
997 error = pcibios_pm_ops.restore(dev);
998 if (error)
999 return error;
1000 }
1001
Rafael J. Wysocki418e4da2009-01-26 21:43:08 +01001002 /*
1003 * This is necessary for the hibernation error path in which restore is
1004 * called without restoring the standard config registers of the device.
1005 */
1006 if (pci_dev->state_saved)
1007 pci_restore_standard_config(pci_dev);
1008
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +01001009 if (pci_has_legacy_pm_support(pci_dev))
1010 return pci_legacy_resume(dev);
1011
Rafael J. Wysocki5294e252009-02-04 02:09:07 +01001012 pci_pm_default_resume(pci_dev);
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +01001013
Rafael J. Wysocki5294e252009-02-04 02:09:07 +01001014 if (pm) {
1015 if (pm->restore)
1016 error = pm->restore(dev);
1017 } else {
1018 pci_pm_reenable_device(pci_dev);
1019 }
Rafael J. Wysockif6dc1e52009-01-07 13:12:22 +01001020
1021 return error;
1022}
1023
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001024#else /* !CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001025
1026#define pci_pm_freeze NULL
1027#define pci_pm_freeze_noirq NULL
1028#define pci_pm_thaw NULL
1029#define pci_pm_thaw_noirq NULL
1030#define pci_pm_poweroff NULL
1031#define pci_pm_poweroff_noirq NULL
1032#define pci_pm_restore NULL
1033#define pci_pm_restore_noirq NULL
1034
Rafael J. Wysocki1f112ce2011-04-11 22:54:42 +02001035#endif /* !CONFIG_HIBERNATE_CALLBACKS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001036
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001037#ifdef CONFIG_PM_RUNTIME
1038
1039static int pci_pm_runtime_suspend(struct device *dev)
1040{
1041 struct pci_dev *pci_dev = to_pci_dev(dev);
1042 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1043 pci_power_t prev = pci_dev->current_state;
1044 int error;
1045
Huang Ying967577b2012-11-20 16:08:22 +08001046 /*
1047 * If pci_dev->driver is not set (unbound), the device should
1048 * always remain in D0 regardless of the runtime PM status
1049 */
1050 if (!pci_dev->driver)
1051 return 0;
1052
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001053 if (!pm || !pm->runtime_suspend)
1054 return -ENOSYS;
1055
Rafael J. Wysocki82fee4d2013-02-04 15:56:05 +04001056 pci_dev->state_saved = false;
Huang Ying448bd852012-06-23 10:23:51 +08001057 pci_dev->no_d3cold = false;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001058 error = pm->runtime_suspend(dev);
1059 suspend_report_result(pm->runtime_suspend, error);
1060 if (error)
1061 return error;
Huang Ying448bd852012-06-23 10:23:51 +08001062 if (!pci_dev->d3cold_allowed)
1063 pci_dev->no_d3cold = true;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001064
1065 pci_fixup_device(pci_fixup_suspend, pci_dev);
1066
1067 if (!pci_dev->state_saved && pci_dev->current_state != PCI_D0
1068 && pci_dev->current_state != PCI_UNKNOWN) {
1069 WARN_ONCE(pci_dev->current_state != prev,
1070 "PCI PM: State of device not saved by %pF\n",
1071 pm->runtime_suspend);
1072 return 0;
1073 }
1074
Dave Airlie42eca232012-10-29 17:26:54 -06001075 if (!pci_dev->state_saved) {
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001076 pci_save_state(pci_dev);
Dave Airlie42eca232012-10-29 17:26:54 -06001077 pci_finish_runtime_suspend(pci_dev);
1078 }
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001079
1080 return 0;
1081}
1082
1083static int pci_pm_runtime_resume(struct device *dev)
1084{
Huang Ying448bd852012-06-23 10:23:51 +08001085 int rc;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001086 struct pci_dev *pci_dev = to_pci_dev(dev);
1087 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
1088
Huang Ying967577b2012-11-20 16:08:22 +08001089 /*
1090 * If pci_dev->driver is not set (unbound), the device should
1091 * always remain in D0 regardless of the runtime PM status
1092 */
1093 if (!pci_dev->driver)
1094 return 0;
1095
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001096 if (!pm || !pm->runtime_resume)
1097 return -ENOSYS;
1098
Rafael J. Wysockidb288c92012-07-05 15:20:00 -06001099 pci_restore_standard_config(pci_dev);
1100 pci_fixup_device(pci_fixup_resume_early, pci_dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001101 __pci_enable_wake(pci_dev, PCI_D0, true, false);
1102 pci_fixup_device(pci_fixup_resume, pci_dev);
1103
Huang Ying448bd852012-06-23 10:23:51 +08001104 rc = pm->runtime_resume(dev);
1105
1106 pci_dev->runtime_d3cold = false;
1107
1108 return rc;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001109}
1110
1111static int pci_pm_runtime_idle(struct device *dev)
1112{
Huang Ying967577b2012-11-20 16:08:22 +08001113 struct pci_dev *pci_dev = to_pci_dev(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001114 const struct dev_pm_ops *pm = dev->driver ? dev->driver->pm : NULL;
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001115 int ret = 0;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001116
Huang Ying967577b2012-11-20 16:08:22 +08001117 /*
1118 * If pci_dev->driver is not set (unbound), the device should
1119 * always remain in D0 regardless of the runtime PM status
1120 */
1121 if (!pci_dev->driver)
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001122 return 0;
Huang Ying967577b2012-11-20 16:08:22 +08001123
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001124 if (!pm)
1125 return -ENOSYS;
1126
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001127 if (pm->runtime_idle)
1128 ret = pm->runtime_idle(dev);
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001129
Rafael J. Wysocki45f0a852013-06-03 21:49:52 +02001130 return ret;
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001131}
1132
1133#else /* !CONFIG_PM_RUNTIME */
1134
1135#define pci_pm_runtime_suspend NULL
1136#define pci_pm_runtime_resume NULL
1137#define pci_pm_runtime_idle NULL
1138
1139#endif /* !CONFIG_PM_RUNTIME */
1140
Rafael J. Wysockiaa338602011-02-11 00:06:54 +01001141#ifdef CONFIG_PM
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001142
Sachin Kamatf91da042013-10-04 12:04:44 -06001143static const struct dev_pm_ops pci_dev_pm_ops = {
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001144 .prepare = pci_pm_prepare,
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001145 .suspend = pci_pm_suspend,
1146 .resume = pci_pm_resume,
1147 .freeze = pci_pm_freeze,
1148 .thaw = pci_pm_thaw,
1149 .poweroff = pci_pm_poweroff,
1150 .restore = pci_pm_restore,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001151 .suspend_noirq = pci_pm_suspend_noirq,
1152 .resume_noirq = pci_pm_resume_noirq,
1153 .freeze_noirq = pci_pm_freeze_noirq,
1154 .thaw_noirq = pci_pm_thaw_noirq,
1155 .poweroff_noirq = pci_pm_poweroff_noirq,
1156 .restore_noirq = pci_pm_restore_noirq,
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001157 .runtime_suspend = pci_pm_runtime_suspend,
1158 .runtime_resume = pci_pm_runtime_resume,
1159 .runtime_idle = pci_pm_runtime_idle,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001160};
1161
Rafael J. Wysockiadf09492008-10-06 22:46:05 +02001162#define PCI_PM_OPS_PTR (&pci_dev_pm_ops)
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001163
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001164#else /* !COMFIG_PM_OPS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001165
1166#define PCI_PM_OPS_PTR NULL
1167
Rafael J. Wysocki6cbf8212010-02-17 23:44:58 +01001168#endif /* !COMFIG_PM_OPS */
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001169
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170/**
Laurent riffard863b18f2005-10-27 23:12:54 +02001171 * __pci_register_driver - register a new pci driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 * @drv: the driver structure to register
Laurent riffard863b18f2005-10-27 23:12:54 +02001173 * @owner: owner module of drv
Randy Dunlapf95d8822007-02-10 14:41:56 -08001174 * @mod_name: module name string
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001175 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 * Adds the driver structure to the list of registered drivers.
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001177 * Returns a negative value on error, otherwise 0.
1178 * If no error occurred, the driver remains registered even if
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 * no device was claimed during registration.
1180 */
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -08001181int __pci_register_driver(struct pci_driver *drv, struct module *owner,
1182 const char *mod_name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 /* initialize common driver fields */
1185 drv->driver.name = drv->name;
1186 drv->driver.bus = &pci_bus_type;
Laurent riffard863b18f2005-10-27 23:12:54 +02001187 drv->driver.owner = owner;
Greg Kroah-Hartman725522b2007-01-15 11:50:02 -08001188 drv->driver.mod_name = mod_name;
Alan Cox50b00752006-08-16 17:42:18 +01001189
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001190 spin_lock_init(&drv->dynids.lock);
1191 INIT_LIST_HEAD(&drv->dynids.list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
1193 /* register with core */
Konstantin Khlebnikovbfb09a82012-08-08 14:47:51 +04001194 return driver_register(&drv->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197/**
1198 * pci_unregister_driver - unregister a pci driver
1199 * @drv: the driver structure to unregister
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001200 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 * Deletes the driver structure from the list of registered PCI drivers,
1202 * gives it a chance to clean up by calling its remove() function for
1203 * each device it was responsible for, and marks those devices as
1204 * driverless.
1205 */
1206
1207void
1208pci_unregister_driver(struct pci_driver *drv)
1209{
1210 driver_unregister(&drv->driver);
1211 pci_free_dynids(drv);
1212}
1213
1214static struct pci_driver pci_compat_driver = {
1215 .name = "compat"
1216};
1217
1218/**
1219 * pci_dev_driver - get the pci_driver of a device
1220 * @dev: the device to query
1221 *
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001222 * Returns the appropriate pci_driver structure or %NULL if there is no
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 * registered driver for the device.
1224 */
1225struct pci_driver *
1226pci_dev_driver(const struct pci_dev *dev)
1227{
1228 if (dev->driver)
1229 return dev->driver;
1230 else {
1231 int i;
1232 for(i=0; i<=PCI_ROM_RESOURCE; i++)
1233 if (dev->resource[i].flags & IORESOURCE_BUSY)
1234 return &pci_compat_driver;
1235 }
1236 return NULL;
1237}
1238
1239/**
1240 * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 * @dev: the PCI device structure to match against
Randy Dunlap8f7020d2005-10-23 11:57:38 -07001242 * @drv: the device driver to search for matching PCI device id structures
Bjorn Helgaasf7625982013-11-14 11:28:18 -07001243 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * Used by a driver to check whether a PCI device present in the
Randy Dunlap8f7020d2005-10-23 11:57:38 -07001245 * system is in its list of supported devices. Returns the matching
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 * pci_device_id structure or %NULL if there is no match.
1247 */
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001248static int pci_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249{
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001250 struct pci_dev *pci_dev = to_pci_dev(dev);
Yinghai Lu58d9a382013-01-21 13:20:51 -08001251 struct pci_driver *pci_drv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 const struct pci_device_id *found_id;
1253
Yinghai Lu58d9a382013-01-21 13:20:51 -08001254 if (!pci_dev->match_driver)
1255 return 0;
1256
1257 pci_drv = to_pci_driver(drv);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001258 found_id = pci_match_device(pci_drv, pci_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (found_id)
1260 return 1;
1261
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263}
1264
1265/**
1266 * pci_dev_get - increments the reference count of the pci device structure
1267 * @dev: the device being referenced
1268 *
1269 * Each live reference to a device should be refcounted.
1270 *
1271 * Drivers for PCI devices should normally record such references in
1272 * their probe() methods, when they bind to a device, and release
1273 * them by calling pci_dev_put(), in their disconnect() methods.
1274 *
1275 * A pointer to the device with the incremented reference counter is returned.
1276 */
1277struct pci_dev *pci_dev_get(struct pci_dev *dev)
1278{
1279 if (dev)
1280 get_device(&dev->dev);
1281 return dev;
1282}
1283
1284/**
1285 * pci_dev_put - release a use of the pci device structure
1286 * @dev: device that's been disconnected
1287 *
1288 * Must be called when a user of a device is finished with it. When the last
1289 * user of the device calls this function, the memory of the device is freed.
1290 */
1291void pci_dev_put(struct pci_dev *dev)
1292{
1293 if (dev)
1294 put_device(&dev->dev);
1295}
1296
Bill Pemberton8ccc9aa2012-11-21 15:34:58 -05001297static int pci_uevent(struct device *dev, struct kobj_uevent_env *env)
1298{
1299 struct pci_dev *pdev;
1300
1301 if (!dev)
1302 return -ENODEV;
1303
1304 pdev = to_pci_dev(dev);
1305 if (!pdev)
1306 return -ENODEV;
1307
1308 if (add_uevent_var(env, "PCI_CLASS=%04X", pdev->class))
1309 return -ENOMEM;
1310
1311 if (add_uevent_var(env, "PCI_ID=%04X:%04X", pdev->vendor, pdev->device))
1312 return -ENOMEM;
1313
1314 if (add_uevent_var(env, "PCI_SUBSYS_ID=%04X:%04X", pdev->subsystem_vendor,
1315 pdev->subsystem_device))
1316 return -ENOMEM;
1317
1318 if (add_uevent_var(env, "PCI_SLOT_NAME=%s", pci_name(pdev)))
1319 return -ENOMEM;
1320
1321 if (add_uevent_var(env, "MODALIAS=pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02x",
1322 pdev->vendor, pdev->device,
1323 pdev->subsystem_vendor, pdev->subsystem_device,
1324 (u8)(pdev->class >> 16), (u8)(pdev->class >> 8),
1325 (u8)(pdev->class)))
1326 return -ENOMEM;
1327 return 0;
1328}
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330struct bus_type pci_bus_type = {
1331 .name = "pci",
1332 .match = pci_bus_match,
Kay Sievers312c0042005-11-16 09:00:00 +01001333 .uevent = pci_uevent,
Russell Kingb15d6862006-01-05 14:30:22 +00001334 .probe = pci_device_probe,
1335 .remove = pci_device_remove,
Linus Torvaldscbd69db2006-06-24 14:50:29 -07001336 .shutdown = pci_device_shutdown,
Greg Kroah-Hartman5136b2d2013-10-06 23:55:40 -07001337 .dev_groups = pci_dev_groups,
Greg Kroah-Hartman0f49ba52013-10-07 14:51:02 -06001338 .bus_groups = pci_bus_groups,
Greg Kroah-Hartman2229c1f2013-10-07 14:51:20 -06001339 .drv_groups = pci_drv_groups,
Rafael J. Wysockibbb44d92008-05-20 00:49:04 +02001340 .pm = PCI_PM_OPS_PTR,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341};
1342
1343static int __init pci_driver_init(void)
1344{
1345 return bus_register(&pci_bus_type);
1346}
1347
1348postcore_initcall(pci_driver_init);
1349
Tejun Heo9dba9102009-09-03 15:26:36 +09001350EXPORT_SYMBOL_GPL(pci_add_dynid);
Greg Kroah-Hartman75865852005-06-30 02:18:12 -07001351EXPORT_SYMBOL(pci_match_id);
Laurent riffard863b18f2005-10-27 23:12:54 +02001352EXPORT_SYMBOL(__pci_register_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353EXPORT_SYMBOL(pci_unregister_driver);
1354EXPORT_SYMBOL(pci_dev_driver);
1355EXPORT_SYMBOL(pci_bus_type);
1356EXPORT_SYMBOL(pci_dev_get);
1357EXPORT_SYMBOL(pci_dev_put);