pci-driver.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /*
  2. * drivers/pci/pci-driver.c
  3. *
  4. */
  5. #include <linux/pci.h>
  6. #include <linux/module.h>
  7. #include <linux/init.h>
  8. #include <linux/device.h>
  9. #include <linux/mempolicy.h>
  10. #include <linux/string.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include "pci.h"
  14. /*
  15. * Registration of PCI drivers and handling of hot-pluggable devices.
  16. */
  17. /* multithreaded probe logic */
  18. static int pci_multithread_probe =
  19. #ifdef CONFIG_PCI_MULTITHREAD_PROBE
  20. 1;
  21. #else
  22. 0;
  23. #endif
  24. __module_param_call("", pci_multithread_probe, param_set_bool, param_get_bool, &pci_multithread_probe, 0644);
  25. /*
  26. * Dynamic device IDs are disabled for !CONFIG_HOTPLUG
  27. */
  28. struct pci_dynid {
  29. struct list_head node;
  30. struct pci_device_id id;
  31. };
  32. #ifdef CONFIG_HOTPLUG
  33. /**
  34. * store_new_id - add a new PCI device ID to this driver and re-probe devices
  35. * @driver: target device driver
  36. * @buf: buffer for scanning device ID data
  37. * @count: input size
  38. *
  39. * Adds a new dynamic pci device ID to this driver,
  40. * and causes the driver to probe for all devices again.
  41. */
  42. static ssize_t
  43. store_new_id(struct device_driver *driver, const char *buf, size_t count)
  44. {
  45. struct pci_dynid *dynid;
  46. struct pci_driver *pdrv = to_pci_driver(driver);
  47. __u32 vendor=PCI_ANY_ID, device=PCI_ANY_ID, subvendor=PCI_ANY_ID,
  48. subdevice=PCI_ANY_ID, class=0, class_mask=0;
  49. unsigned long driver_data=0;
  50. int fields=0;
  51. int retval = 0;
  52. fields = sscanf(buf, "%x %x %x %x %x %x %lux",
  53. &vendor, &device, &subvendor, &subdevice,
  54. &class, &class_mask, &driver_data);
  55. if (fields < 0)
  56. return -EINVAL;
  57. dynid = kzalloc(sizeof(*dynid), GFP_KERNEL);
  58. if (!dynid)
  59. return -ENOMEM;
  60. INIT_LIST_HEAD(&dynid->node);
  61. dynid->id.vendor = vendor;
  62. dynid->id.device = device;
  63. dynid->id.subvendor = subvendor;
  64. dynid->id.subdevice = subdevice;
  65. dynid->id.class = class;
  66. dynid->id.class_mask = class_mask;
  67. dynid->id.driver_data = pdrv->dynids.use_driver_data ?
  68. driver_data : 0UL;
  69. spin_lock(&pdrv->dynids.lock);
  70. list_add_tail(&pdrv->dynids.list, &dynid->node);
  71. spin_unlock(&pdrv->dynids.lock);
  72. if (get_driver(&pdrv->driver)) {
  73. retval = driver_attach(&pdrv->driver);
  74. put_driver(&pdrv->driver);
  75. }
  76. if (retval)
  77. return retval;
  78. return count;
  79. }
  80. static DRIVER_ATTR(new_id, S_IWUSR, NULL, store_new_id);
  81. static void
  82. pci_free_dynids(struct pci_driver *drv)
  83. {
  84. struct pci_dynid *dynid, *n;
  85. spin_lock(&drv->dynids.lock);
  86. list_for_each_entry_safe(dynid, n, &drv->dynids.list, node) {
  87. list_del(&dynid->node);
  88. kfree(dynid);
  89. }
  90. spin_unlock(&drv->dynids.lock);
  91. }
  92. static int
  93. pci_create_newid_file(struct pci_driver *drv)
  94. {
  95. int error = 0;
  96. if (drv->probe != NULL)
  97. error = sysfs_create_file(&drv->driver.kobj,
  98. &driver_attr_new_id.attr);
  99. return error;
  100. }
  101. #else /* !CONFIG_HOTPLUG */
  102. static inline void pci_free_dynids(struct pci_driver *drv) {}
  103. static inline int pci_create_newid_file(struct pci_driver *drv)
  104. {
  105. return 0;
  106. }
  107. #endif
  108. /**
  109. * pci_match_id - See if a pci device matches a given pci_id table
  110. * @ids: array of PCI device id structures to search in
  111. * @dev: the PCI device structure to match against.
  112. *
  113. * Used by a driver to check whether a PCI device present in the
  114. * system is in its list of supported devices. Returns the matching
  115. * pci_device_id structure or %NULL if there is no match.
  116. *
  117. * Depreciated, don't use this as it will not catch any dynamic ids
  118. * that a driver might want to check for.
  119. */
  120. const struct pci_device_id *pci_match_id(const struct pci_device_id *ids,
  121. struct pci_dev *dev)
  122. {
  123. if (ids) {
  124. while (ids->vendor || ids->subvendor || ids->class_mask) {
  125. if (pci_match_one_device(ids, dev))
  126. return ids;
  127. ids++;
  128. }
  129. }
  130. return NULL;
  131. }
  132. /**
  133. * pci_match_device - Tell if a PCI device structure has a matching
  134. * PCI device id structure
  135. * @drv: the PCI driver to match against
  136. * @dev: the PCI device structure to match against
  137. *
  138. * Used by a driver to check whether a PCI device present in the
  139. * system is in its list of supported devices. Returns the matching
  140. * pci_device_id structure or %NULL if there is no match.
  141. */
  142. const struct pci_device_id *pci_match_device(struct pci_driver *drv,
  143. struct pci_dev *dev)
  144. {
  145. const struct pci_device_id *id;
  146. struct pci_dynid *dynid;
  147. id = pci_match_id(drv->id_table, dev);
  148. if (id)
  149. return id;
  150. /* static ids didn't match, lets look at the dynamic ones */
  151. spin_lock(&drv->dynids.lock);
  152. list_for_each_entry(dynid, &drv->dynids.list, node) {
  153. if (pci_match_one_device(&dynid->id, dev)) {
  154. spin_unlock(&drv->dynids.lock);
  155. return &dynid->id;
  156. }
  157. }
  158. spin_unlock(&drv->dynids.lock);
  159. return NULL;
  160. }
  161. static int pci_call_probe(struct pci_driver *drv, struct pci_dev *dev,
  162. const struct pci_device_id *id)
  163. {
  164. int error;
  165. #ifdef CONFIG_NUMA
  166. /* Execute driver initialization on node where the
  167. device's bus is attached to. This way the driver likely
  168. allocates its local memory on the right node without
  169. any need to change it. */
  170. struct mempolicy *oldpol;
  171. cpumask_t oldmask = current->cpus_allowed;
  172. int node = pcibus_to_node(dev->bus);
  173. if (node >= 0 && node_online(node))
  174. set_cpus_allowed(current, node_to_cpumask(node));
  175. /* And set default memory allocation policy */
  176. oldpol = current->mempolicy;
  177. current->mempolicy = &default_policy;
  178. mpol_get(current->mempolicy);
  179. #endif
  180. error = drv->probe(dev, id);
  181. #ifdef CONFIG_NUMA
  182. set_cpus_allowed(current, oldmask);
  183. mpol_free(current->mempolicy);
  184. current->mempolicy = oldpol;
  185. #endif
  186. return error;
  187. }
  188. /**
  189. * __pci_device_probe()
  190. * @drv: driver to call to check if it wants the PCI device
  191. * @pci_dev: PCI device being probed
  192. *
  193. * returns 0 on success, else error.
  194. * side-effect: pci_dev->driver is set to drv when drv claims pci_dev.
  195. */
  196. static int
  197. __pci_device_probe(struct pci_driver *drv, struct pci_dev *pci_dev)
  198. {
  199. const struct pci_device_id *id;
  200. int error = 0;
  201. if (!pci_dev->driver && drv->probe) {
  202. error = -ENODEV;
  203. id = pci_match_device(drv, pci_dev);
  204. if (id)
  205. error = pci_call_probe(drv, pci_dev, id);
  206. if (error >= 0) {
  207. pci_dev->driver = drv;
  208. error = 0;
  209. }
  210. }
  211. return error;
  212. }
  213. static int pci_device_probe(struct device * dev)
  214. {
  215. int error = 0;
  216. struct pci_driver *drv;
  217. struct pci_dev *pci_dev;
  218. drv = to_pci_driver(dev->driver);
  219. pci_dev = to_pci_dev(dev);
  220. pci_dev_get(pci_dev);
  221. error = __pci_device_probe(drv, pci_dev);
  222. if (error)
  223. pci_dev_put(pci_dev);
  224. return error;
  225. }
  226. static int pci_device_remove(struct device * dev)
  227. {
  228. struct pci_dev * pci_dev = to_pci_dev(dev);
  229. struct pci_driver * drv = pci_dev->driver;
  230. if (drv) {
  231. if (drv->remove)
  232. drv->remove(pci_dev);
  233. pci_dev->driver = NULL;
  234. }
  235. /*
  236. * We would love to complain here if pci_dev->is_enabled is set, that
  237. * the driver should have called pci_disable_device(), but the
  238. * unfortunate fact is there are too many odd BIOS and bridge setups
  239. * that don't like drivers doing that all of the time.
  240. * Oh well, we can dream of sane hardware when we sleep, no matter how
  241. * horrible the crap we have to deal with is when we are awake...
  242. */
  243. pci_dev_put(pci_dev);
  244. return 0;
  245. }
  246. static int pci_device_suspend(struct device * dev, pm_message_t state)
  247. {
  248. struct pci_dev * pci_dev = to_pci_dev(dev);
  249. struct pci_driver * drv = pci_dev->driver;
  250. int i = 0;
  251. if (drv && drv->suspend) {
  252. i = drv->suspend(pci_dev, state);
  253. suspend_report_result(drv->suspend, i);
  254. } else {
  255. pci_save_state(pci_dev);
  256. }
  257. return i;
  258. }
  259. static int pci_device_suspend_late(struct device * dev, pm_message_t state)
  260. {
  261. struct pci_dev * pci_dev = to_pci_dev(dev);
  262. struct pci_driver * drv = pci_dev->driver;
  263. int i = 0;
  264. if (drv && drv->suspend_late) {
  265. i = drv->suspend_late(pci_dev, state);
  266. suspend_report_result(drv->suspend_late, i);
  267. }
  268. return i;
  269. }
  270. /*
  271. * Default resume method for devices that have no driver provided resume,
  272. * or not even a driver at all.
  273. */
  274. static int pci_default_resume(struct pci_dev *pci_dev)
  275. {
  276. int retval = 0;
  277. /* restore the PCI config space */
  278. pci_restore_state(pci_dev);
  279. /* if the device was enabled before suspend, reenable */
  280. if (pci_dev->is_enabled)
  281. retval = pci_enable_device(pci_dev);
  282. /* if the device was busmaster before the suspend, make it busmaster again */
  283. if (pci_dev->is_busmaster)
  284. pci_set_master(pci_dev);
  285. return retval;
  286. }
  287. static int pci_device_resume(struct device * dev)
  288. {
  289. int error;
  290. struct pci_dev * pci_dev = to_pci_dev(dev);
  291. struct pci_driver * drv = pci_dev->driver;
  292. if (drv && drv->resume)
  293. error = drv->resume(pci_dev);
  294. else
  295. error = pci_default_resume(pci_dev);
  296. return error;
  297. }
  298. static int pci_device_resume_early(struct device * dev)
  299. {
  300. int error = 0;
  301. struct pci_dev * pci_dev = to_pci_dev(dev);
  302. struct pci_driver * drv = pci_dev->driver;
  303. if (drv && drv->resume_early)
  304. error = drv->resume_early(pci_dev);
  305. return error;
  306. }
  307. static void pci_device_shutdown(struct device *dev)
  308. {
  309. struct pci_dev *pci_dev = to_pci_dev(dev);
  310. struct pci_driver *drv = pci_dev->driver;
  311. if (drv && drv->shutdown)
  312. drv->shutdown(pci_dev);
  313. }
  314. #define kobj_to_pci_driver(obj) container_of(obj, struct device_driver, kobj)
  315. #define attr_to_driver_attribute(obj) container_of(obj, struct driver_attribute, attr)
  316. static ssize_t
  317. pci_driver_attr_show(struct kobject * kobj, struct attribute *attr, char *buf)
  318. {
  319. struct device_driver *driver = kobj_to_pci_driver(kobj);
  320. struct driver_attribute *dattr = attr_to_driver_attribute(attr);
  321. ssize_t ret;
  322. if (!get_driver(driver))
  323. return -ENODEV;
  324. ret = dattr->show ? dattr->show(driver, buf) : -EIO;
  325. put_driver(driver);
  326. return ret;
  327. }
  328. static ssize_t
  329. pci_driver_attr_store(struct kobject * kobj, struct attribute *attr,
  330. const char *buf, size_t count)
  331. {
  332. struct device_driver *driver = kobj_to_pci_driver(kobj);
  333. struct driver_attribute *dattr = attr_to_driver_attribute(attr);
  334. ssize_t ret;
  335. if (!get_driver(driver))
  336. return -ENODEV;
  337. ret = dattr->store ? dattr->store(driver, buf, count) : -EIO;
  338. put_driver(driver);
  339. return ret;
  340. }
  341. static struct sysfs_ops pci_driver_sysfs_ops = {
  342. .show = pci_driver_attr_show,
  343. .store = pci_driver_attr_store,
  344. };
  345. static struct kobj_type pci_driver_kobj_type = {
  346. .sysfs_ops = &pci_driver_sysfs_ops,
  347. };
  348. /**
  349. * __pci_register_driver - register a new pci driver
  350. * @drv: the driver structure to register
  351. * @owner: owner module of drv
  352. *
  353. * Adds the driver structure to the list of registered drivers.
  354. * Returns a negative value on error, otherwise 0.
  355. * If no error occurred, the driver remains registered even if
  356. * no device was claimed during registration.
  357. */
  358. int __pci_register_driver(struct pci_driver *drv, struct module *owner)
  359. {
  360. int error;
  361. /* initialize common driver fields */
  362. drv->driver.name = drv->name;
  363. drv->driver.bus = &pci_bus_type;
  364. drv->driver.owner = owner;
  365. drv->driver.kobj.ktype = &pci_driver_kobj_type;
  366. if (pci_multithread_probe)
  367. drv->driver.multithread_probe = pci_multithread_probe;
  368. else
  369. drv->driver.multithread_probe = drv->multithread_probe;
  370. spin_lock_init(&drv->dynids.lock);
  371. INIT_LIST_HEAD(&drv->dynids.list);
  372. /* register with core */
  373. error = driver_register(&drv->driver);
  374. if (!error)
  375. error = pci_create_newid_file(drv);
  376. return error;
  377. }
  378. /**
  379. * pci_unregister_driver - unregister a pci driver
  380. * @drv: the driver structure to unregister
  381. *
  382. * Deletes the driver structure from the list of registered PCI drivers,
  383. * gives it a chance to clean up by calling its remove() function for
  384. * each device it was responsible for, and marks those devices as
  385. * driverless.
  386. */
  387. void
  388. pci_unregister_driver(struct pci_driver *drv)
  389. {
  390. driver_unregister(&drv->driver);
  391. pci_free_dynids(drv);
  392. }
  393. static struct pci_driver pci_compat_driver = {
  394. .name = "compat"
  395. };
  396. /**
  397. * pci_dev_driver - get the pci_driver of a device
  398. * @dev: the device to query
  399. *
  400. * Returns the appropriate pci_driver structure or %NULL if there is no
  401. * registered driver for the device.
  402. */
  403. struct pci_driver *
  404. pci_dev_driver(const struct pci_dev *dev)
  405. {
  406. if (dev->driver)
  407. return dev->driver;
  408. else {
  409. int i;
  410. for(i=0; i<=PCI_ROM_RESOURCE; i++)
  411. if (dev->resource[i].flags & IORESOURCE_BUSY)
  412. return &pci_compat_driver;
  413. }
  414. return NULL;
  415. }
  416. /**
  417. * pci_bus_match - Tell if a PCI device structure has a matching PCI device id structure
  418. * @dev: the PCI device structure to match against
  419. * @drv: the device driver to search for matching PCI device id structures
  420. *
  421. * Used by a driver to check whether a PCI device present in the
  422. * system is in its list of supported devices. Returns the matching
  423. * pci_device_id structure or %NULL if there is no match.
  424. */
  425. static int pci_bus_match(struct device *dev, struct device_driver *drv)
  426. {
  427. struct pci_dev *pci_dev = to_pci_dev(dev);
  428. struct pci_driver *pci_drv = to_pci_driver(drv);
  429. const struct pci_device_id *found_id;
  430. found_id = pci_match_device(pci_drv, pci_dev);
  431. if (found_id)
  432. return 1;
  433. return 0;
  434. }
  435. /**
  436. * pci_dev_get - increments the reference count of the pci device structure
  437. * @dev: the device being referenced
  438. *
  439. * Each live reference to a device should be refcounted.
  440. *
  441. * Drivers for PCI devices should normally record such references in
  442. * their probe() methods, when they bind to a device, and release
  443. * them by calling pci_dev_put(), in their disconnect() methods.
  444. *
  445. * A pointer to the device with the incremented reference counter is returned.
  446. */
  447. struct pci_dev *pci_dev_get(struct pci_dev *dev)
  448. {
  449. if (dev)
  450. get_device(&dev->dev);
  451. return dev;
  452. }
  453. /**
  454. * pci_dev_put - release a use of the pci device structure
  455. * @dev: device that's been disconnected
  456. *
  457. * Must be called when a user of a device is finished with it. When the last
  458. * user of the device calls this function, the memory of the device is freed.
  459. */
  460. void pci_dev_put(struct pci_dev *dev)
  461. {
  462. if (dev)
  463. put_device(&dev->dev);
  464. }
  465. #ifndef CONFIG_HOTPLUG
  466. int pci_uevent(struct device *dev, char **envp, int num_envp,
  467. char *buffer, int buffer_size)
  468. {
  469. return -ENODEV;
  470. }
  471. #endif
  472. struct bus_type pci_bus_type = {
  473. .name = "pci",
  474. .match = pci_bus_match,
  475. .uevent = pci_uevent,
  476. .probe = pci_device_probe,
  477. .remove = pci_device_remove,
  478. .suspend = pci_device_suspend,
  479. .suspend_late = pci_device_suspend_late,
  480. .resume_early = pci_device_resume_early,
  481. .resume = pci_device_resume,
  482. .shutdown = pci_device_shutdown,
  483. .dev_attrs = pci_dev_attrs,
  484. };
  485. static int __init pci_driver_init(void)
  486. {
  487. return bus_register(&pci_bus_type);
  488. }
  489. postcore_initcall(pci_driver_init);
  490. EXPORT_SYMBOL(pci_match_id);
  491. EXPORT_SYMBOL(pci_match_device);
  492. EXPORT_SYMBOL(__pci_register_driver);
  493. EXPORT_SYMBOL(pci_unregister_driver);
  494. EXPORT_SYMBOL(pci_dev_driver);
  495. EXPORT_SYMBOL(pci_bus_type);
  496. EXPORT_SYMBOL(pci_dev_get);
  497. EXPORT_SYMBOL(pci_dev_put);