pci_stub.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525
  1. /*
  2. * PCI Stub Driver - Grabs devices in backend to be exported later
  3. *
  4. * Ryan Wilson <hap9@epoch.ncsc.mil>
  5. * Chris Bookholt <hap10@epoch.ncsc.mil>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/rwsem.h>
  11. #include <linux/list.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/kref.h>
  14. #include <linux/pci.h>
  15. #include <linux/wait.h>
  16. #include <linux/sched.h>
  17. #include <linux/atomic.h>
  18. #include <xen/events.h>
  19. #include <asm/xen/pci.h>
  20. #include <asm/xen/hypervisor.h>
  21. #include <xen/interface/physdev.h>
  22. #include "pciback.h"
  23. #include "conf_space.h"
  24. #include "conf_space_quirks.h"
  25. static char *pci_devs_to_hide;
  26. wait_queue_head_t xen_pcibk_aer_wait_queue;
  27. /*Add sem for sync AER handling and xen_pcibk remove/reconfigue ops,
  28. * We want to avoid in middle of AER ops, xen_pcibk devices is being removed
  29. */
  30. static DECLARE_RWSEM(pcistub_sem);
  31. module_param_named(hide, pci_devs_to_hide, charp, 0444);
  32. struct pcistub_device_id {
  33. struct list_head slot_list;
  34. int domain;
  35. unsigned char bus;
  36. unsigned int devfn;
  37. };
  38. static LIST_HEAD(pcistub_device_ids);
  39. static DEFINE_SPINLOCK(device_ids_lock);
  40. struct pcistub_device {
  41. struct kref kref;
  42. struct list_head dev_list;
  43. spinlock_t lock;
  44. struct pci_dev *dev;
  45. struct xen_pcibk_device *pdev;/* non-NULL if struct pci_dev is in use */
  46. };
  47. /* Access to pcistub_devices & seized_devices lists and the initialize_devices
  48. * flag must be locked with pcistub_devices_lock
  49. */
  50. static DEFINE_SPINLOCK(pcistub_devices_lock);
  51. static LIST_HEAD(pcistub_devices);
  52. /* wait for device_initcall before initializing our devices
  53. * (see pcistub_init_devices_late)
  54. */
  55. static int initialize_devices;
  56. static LIST_HEAD(seized_devices);
  57. static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
  58. {
  59. struct pcistub_device *psdev;
  60. dev_dbg(&dev->dev, "pcistub_device_alloc\n");
  61. psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
  62. if (!psdev)
  63. return NULL;
  64. psdev->dev = pci_dev_get(dev);
  65. if (!psdev->dev) {
  66. kfree(psdev);
  67. return NULL;
  68. }
  69. kref_init(&psdev->kref);
  70. spin_lock_init(&psdev->lock);
  71. return psdev;
  72. }
  73. /* Don't call this directly as it's called by pcistub_device_put */
  74. static void pcistub_device_release(struct kref *kref)
  75. {
  76. struct pcistub_device *psdev;
  77. struct pci_dev *dev;
  78. struct xen_pcibk_dev_data *dev_data;
  79. psdev = container_of(kref, struct pcistub_device, kref);
  80. dev = psdev->dev;
  81. dev_data = pci_get_drvdata(dev);
  82. dev_dbg(&dev->dev, "pcistub_device_release\n");
  83. xen_unregister_device_domain_owner(dev);
  84. /* Call the reset function which does not take lock as this
  85. * is called from "unbind" which takes a device_lock mutex.
  86. */
  87. __pci_reset_function_locked(dev);
  88. if (pci_load_and_free_saved_state(dev, &dev_data->pci_saved_state))
  89. dev_dbg(&dev->dev, "Could not reload PCI state\n");
  90. else
  91. pci_restore_state(dev);
  92. if (dev->msix_cap) {
  93. struct physdev_pci_device ppdev = {
  94. .seg = pci_domain_nr(dev->bus),
  95. .bus = dev->bus->number,
  96. .devfn = dev->devfn
  97. };
  98. int err = HYPERVISOR_physdev_op(PHYSDEVOP_release_msix,
  99. &ppdev);
  100. if (err)
  101. dev_warn(&dev->dev, "MSI-X release failed (%d)\n",
  102. err);
  103. }
  104. /* Disable the device */
  105. xen_pcibk_reset_device(dev);
  106. kfree(dev_data);
  107. pci_set_drvdata(dev, NULL);
  108. /* Clean-up the device */
  109. xen_pcibk_config_free_dyn_fields(dev);
  110. xen_pcibk_config_free_dev(dev);
  111. dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
  112. pci_dev_put(dev);
  113. kfree(psdev);
  114. }
  115. static inline void pcistub_device_get(struct pcistub_device *psdev)
  116. {
  117. kref_get(&psdev->kref);
  118. }
  119. static inline void pcistub_device_put(struct pcistub_device *psdev)
  120. {
  121. kref_put(&psdev->kref, pcistub_device_release);
  122. }
  123. static struct pcistub_device *pcistub_device_find(int domain, int bus,
  124. int slot, int func)
  125. {
  126. struct pcistub_device *psdev = NULL;
  127. unsigned long flags;
  128. spin_lock_irqsave(&pcistub_devices_lock, flags);
  129. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  130. if (psdev->dev != NULL
  131. && domain == pci_domain_nr(psdev->dev->bus)
  132. && bus == psdev->dev->bus->number
  133. && slot == PCI_SLOT(psdev->dev->devfn)
  134. && func == PCI_FUNC(psdev->dev->devfn)) {
  135. pcistub_device_get(psdev);
  136. goto out;
  137. }
  138. }
  139. /* didn't find it */
  140. psdev = NULL;
  141. out:
  142. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  143. return psdev;
  144. }
  145. static struct pci_dev *pcistub_device_get_pci_dev(struct xen_pcibk_device *pdev,
  146. struct pcistub_device *psdev)
  147. {
  148. struct pci_dev *pci_dev = NULL;
  149. unsigned long flags;
  150. pcistub_device_get(psdev);
  151. spin_lock_irqsave(&psdev->lock, flags);
  152. if (!psdev->pdev) {
  153. psdev->pdev = pdev;
  154. pci_dev = psdev->dev;
  155. }
  156. spin_unlock_irqrestore(&psdev->lock, flags);
  157. if (!pci_dev)
  158. pcistub_device_put(psdev);
  159. return pci_dev;
  160. }
  161. struct pci_dev *pcistub_get_pci_dev_by_slot(struct xen_pcibk_device *pdev,
  162. int domain, int bus,
  163. int slot, int func)
  164. {
  165. struct pcistub_device *psdev;
  166. struct pci_dev *found_dev = NULL;
  167. unsigned long flags;
  168. spin_lock_irqsave(&pcistub_devices_lock, flags);
  169. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  170. if (psdev->dev != NULL
  171. && domain == pci_domain_nr(psdev->dev->bus)
  172. && bus == psdev->dev->bus->number
  173. && slot == PCI_SLOT(psdev->dev->devfn)
  174. && func == PCI_FUNC(psdev->dev->devfn)) {
  175. found_dev = pcistub_device_get_pci_dev(pdev, psdev);
  176. break;
  177. }
  178. }
  179. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  180. return found_dev;
  181. }
  182. struct pci_dev *pcistub_get_pci_dev(struct xen_pcibk_device *pdev,
  183. struct pci_dev *dev)
  184. {
  185. struct pcistub_device *psdev;
  186. struct pci_dev *found_dev = NULL;
  187. unsigned long flags;
  188. spin_lock_irqsave(&pcistub_devices_lock, flags);
  189. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  190. if (psdev->dev == dev) {
  191. found_dev = pcistub_device_get_pci_dev(pdev, psdev);
  192. break;
  193. }
  194. }
  195. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  196. return found_dev;
  197. }
  198. void pcistub_put_pci_dev(struct pci_dev *dev)
  199. {
  200. struct pcistub_device *psdev, *found_psdev = NULL;
  201. unsigned long flags;
  202. spin_lock_irqsave(&pcistub_devices_lock, flags);
  203. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  204. if (psdev->dev == dev) {
  205. found_psdev = psdev;
  206. break;
  207. }
  208. }
  209. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  210. if (WARN_ON(!found_psdev))
  211. return;
  212. /*hold this lock for avoiding breaking link between
  213. * pcistub and xen_pcibk when AER is in processing
  214. */
  215. down_write(&pcistub_sem);
  216. /* Cleanup our device
  217. * (so it's ready for the next domain)
  218. */
  219. /* This is OK - we are running from workqueue context
  220. * and want to inhibit the user from fiddling with 'reset'
  221. */
  222. pci_reset_function(dev);
  223. pci_restore_state(psdev->dev);
  224. /* This disables the device. */
  225. xen_pcibk_reset_device(found_psdev->dev);
  226. /* And cleanup up our emulated fields. */
  227. xen_pcibk_config_free_dyn_fields(found_psdev->dev);
  228. xen_pcibk_config_reset_dev(found_psdev->dev);
  229. xen_unregister_device_domain_owner(found_psdev->dev);
  230. spin_lock_irqsave(&found_psdev->lock, flags);
  231. found_psdev->pdev = NULL;
  232. spin_unlock_irqrestore(&found_psdev->lock, flags);
  233. pcistub_device_put(found_psdev);
  234. up_write(&pcistub_sem);
  235. }
  236. static int pcistub_match_one(struct pci_dev *dev,
  237. struct pcistub_device_id *pdev_id)
  238. {
  239. /* Match the specified device by domain, bus, slot, func and also if
  240. * any of the device's parent bridges match.
  241. */
  242. for (; dev != NULL; dev = dev->bus->self) {
  243. if (pci_domain_nr(dev->bus) == pdev_id->domain
  244. && dev->bus->number == pdev_id->bus
  245. && dev->devfn == pdev_id->devfn)
  246. return 1;
  247. /* Sometimes topmost bridge links to itself. */
  248. if (dev == dev->bus->self)
  249. break;
  250. }
  251. return 0;
  252. }
  253. static int pcistub_match(struct pci_dev *dev)
  254. {
  255. struct pcistub_device_id *pdev_id;
  256. unsigned long flags;
  257. int found = 0;
  258. spin_lock_irqsave(&device_ids_lock, flags);
  259. list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
  260. if (pcistub_match_one(dev, pdev_id)) {
  261. found = 1;
  262. break;
  263. }
  264. }
  265. spin_unlock_irqrestore(&device_ids_lock, flags);
  266. return found;
  267. }
  268. static int pcistub_init_device(struct pci_dev *dev)
  269. {
  270. struct xen_pcibk_dev_data *dev_data;
  271. int err = 0;
  272. dev_dbg(&dev->dev, "initializing...\n");
  273. /* The PCI backend is not intended to be a module (or to work with
  274. * removable PCI devices (yet). If it were, xen_pcibk_config_free()
  275. * would need to be called somewhere to free the memory allocated
  276. * here and then to call kfree(pci_get_drvdata(psdev->dev)).
  277. */
  278. dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
  279. + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
  280. if (!dev_data) {
  281. err = -ENOMEM;
  282. goto out;
  283. }
  284. pci_set_drvdata(dev, dev_data);
  285. /*
  286. * Setup name for fake IRQ handler. It will only be enabled
  287. * once the device is turned on by the guest.
  288. */
  289. sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
  290. dev_dbg(&dev->dev, "initializing config\n");
  291. init_waitqueue_head(&xen_pcibk_aer_wait_queue);
  292. err = xen_pcibk_config_init_dev(dev);
  293. if (err)
  294. goto out;
  295. /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
  296. * must do this here because pcibios_enable_device may specify
  297. * the pci device's true irq (and possibly its other resources)
  298. * if they differ from what's in the configuration space.
  299. * This makes the assumption that the device's resources won't
  300. * change after this point (otherwise this code may break!)
  301. */
  302. dev_dbg(&dev->dev, "enabling device\n");
  303. err = pci_enable_device(dev);
  304. if (err)
  305. goto config_release;
  306. if (dev->msix_cap) {
  307. struct physdev_pci_device ppdev = {
  308. .seg = pci_domain_nr(dev->bus),
  309. .bus = dev->bus->number,
  310. .devfn = dev->devfn
  311. };
  312. err = HYPERVISOR_physdev_op(PHYSDEVOP_prepare_msix, &ppdev);
  313. if (err)
  314. dev_err(&dev->dev, "MSI-X preparation failed (%d)\n",
  315. err);
  316. }
  317. /* We need the device active to save the state. */
  318. dev_dbg(&dev->dev, "save state of device\n");
  319. pci_save_state(dev);
  320. dev_data->pci_saved_state = pci_store_saved_state(dev);
  321. if (!dev_data->pci_saved_state)
  322. dev_err(&dev->dev, "Could not store PCI conf saved state!\n");
  323. else {
  324. dev_dbg(&dev->dev, "resetting (FLR, D3, etc) the device\n");
  325. __pci_reset_function_locked(dev);
  326. pci_restore_state(dev);
  327. }
  328. /* Now disable the device (this also ensures some private device
  329. * data is setup before we export)
  330. */
  331. dev_dbg(&dev->dev, "reset device\n");
  332. xen_pcibk_reset_device(dev);
  333. dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
  334. return 0;
  335. config_release:
  336. xen_pcibk_config_free_dev(dev);
  337. out:
  338. pci_set_drvdata(dev, NULL);
  339. kfree(dev_data);
  340. return err;
  341. }
  342. /*
  343. * Because some initialization still happens on
  344. * devices during fs_initcall, we need to defer
  345. * full initialization of our devices until
  346. * device_initcall.
  347. */
  348. static int __init pcistub_init_devices_late(void)
  349. {
  350. struct pcistub_device *psdev;
  351. unsigned long flags;
  352. int err = 0;
  353. spin_lock_irqsave(&pcistub_devices_lock, flags);
  354. while (!list_empty(&seized_devices)) {
  355. psdev = container_of(seized_devices.next,
  356. struct pcistub_device, dev_list);
  357. list_del(&psdev->dev_list);
  358. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  359. err = pcistub_init_device(psdev->dev);
  360. if (err) {
  361. dev_err(&psdev->dev->dev,
  362. "error %d initializing device\n", err);
  363. kfree(psdev);
  364. psdev = NULL;
  365. }
  366. spin_lock_irqsave(&pcistub_devices_lock, flags);
  367. if (psdev)
  368. list_add_tail(&psdev->dev_list, &pcistub_devices);
  369. }
  370. initialize_devices = 1;
  371. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  372. return 0;
  373. }
  374. static int pcistub_seize(struct pci_dev *dev)
  375. {
  376. struct pcistub_device *psdev;
  377. unsigned long flags;
  378. int err = 0;
  379. psdev = pcistub_device_alloc(dev);
  380. if (!psdev)
  381. return -ENOMEM;
  382. spin_lock_irqsave(&pcistub_devices_lock, flags);
  383. if (initialize_devices) {
  384. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  385. /* don't want irqs disabled when calling pcistub_init_device */
  386. err = pcistub_init_device(psdev->dev);
  387. spin_lock_irqsave(&pcistub_devices_lock, flags);
  388. if (!err)
  389. list_add(&psdev->dev_list, &pcistub_devices);
  390. } else {
  391. dev_dbg(&dev->dev, "deferring initialization\n");
  392. list_add(&psdev->dev_list, &seized_devices);
  393. }
  394. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  395. if (err)
  396. pcistub_device_put(psdev);
  397. return err;
  398. }
  399. static int pcistub_probe(struct pci_dev *dev, const struct pci_device_id *id)
  400. {
  401. int err = 0;
  402. dev_dbg(&dev->dev, "probing...\n");
  403. if (pcistub_match(dev)) {
  404. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
  405. && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  406. dev_err(&dev->dev, "can't export pci devices that "
  407. "don't have a normal (0) or bridge (1) "
  408. "header type!\n");
  409. err = -ENODEV;
  410. goto out;
  411. }
  412. dev_info(&dev->dev, "seizing device\n");
  413. err = pcistub_seize(dev);
  414. } else
  415. /* Didn't find the device */
  416. err = -ENODEV;
  417. out:
  418. return err;
  419. }
  420. static void pcistub_remove(struct pci_dev *dev)
  421. {
  422. struct pcistub_device *psdev, *found_psdev = NULL;
  423. unsigned long flags;
  424. dev_dbg(&dev->dev, "removing\n");
  425. spin_lock_irqsave(&pcistub_devices_lock, flags);
  426. xen_pcibk_config_quirk_release(dev);
  427. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  428. if (psdev->dev == dev) {
  429. found_psdev = psdev;
  430. break;
  431. }
  432. }
  433. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  434. if (found_psdev) {
  435. dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
  436. found_psdev->pdev);
  437. if (found_psdev->pdev) {
  438. pr_warn("****** removing device %s while still in-use! ******\n",
  439. pci_name(found_psdev->dev));
  440. pr_warn("****** driver domain may still access this device's i/o resources!\n");
  441. pr_warn("****** shutdown driver domain before binding device\n");
  442. pr_warn("****** to other drivers or domains\n");
  443. xen_pcibk_release_pci_dev(found_psdev->pdev,
  444. found_psdev->dev);
  445. }
  446. spin_lock_irqsave(&pcistub_devices_lock, flags);
  447. list_del(&found_psdev->dev_list);
  448. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  449. /* the final put for releasing from the list */
  450. pcistub_device_put(found_psdev);
  451. }
  452. }
  453. static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
  454. {
  455. .vendor = PCI_ANY_ID,
  456. .device = PCI_ANY_ID,
  457. .subvendor = PCI_ANY_ID,
  458. .subdevice = PCI_ANY_ID,
  459. },
  460. {0,},
  461. };
  462. #define PCI_NODENAME_MAX 40
  463. static void kill_domain_by_device(struct pcistub_device *psdev)
  464. {
  465. struct xenbus_transaction xbt;
  466. int err;
  467. char nodename[PCI_NODENAME_MAX];
  468. BUG_ON(!psdev);
  469. snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
  470. psdev->pdev->xdev->otherend_id);
  471. again:
  472. err = xenbus_transaction_start(&xbt);
  473. if (err) {
  474. dev_err(&psdev->dev->dev,
  475. "error %d when start xenbus transaction\n", err);
  476. return;
  477. }
  478. /*PV AER handlers will set this flag*/
  479. xenbus_printf(xbt, nodename, "aerState" , "aerfail");
  480. err = xenbus_transaction_end(xbt, 0);
  481. if (err) {
  482. if (err == -EAGAIN)
  483. goto again;
  484. dev_err(&psdev->dev->dev,
  485. "error %d when end xenbus transaction\n", err);
  486. return;
  487. }
  488. }
  489. /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
  490. * backend need to have cooperation. In xen_pcibk, those steps will do similar
  491. * jobs: send service request and waiting for front_end response.
  492. */
  493. static pci_ers_result_t common_process(struct pcistub_device *psdev,
  494. pci_channel_state_t state, int aer_cmd,
  495. pci_ers_result_t result)
  496. {
  497. pci_ers_result_t res = result;
  498. struct xen_pcie_aer_op *aer_op;
  499. int ret;
  500. /*with PV AER drivers*/
  501. aer_op = &(psdev->pdev->sh_info->aer_op);
  502. aer_op->cmd = aer_cmd ;
  503. /*useful for error_detected callback*/
  504. aer_op->err = state;
  505. /*pcifront_end BDF*/
  506. ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
  507. &aer_op->domain, &aer_op->bus, &aer_op->devfn);
  508. if (!ret) {
  509. dev_err(&psdev->dev->dev,
  510. DRV_NAME ": failed to get pcifront device\n");
  511. return PCI_ERS_RESULT_NONE;
  512. }
  513. wmb();
  514. dev_dbg(&psdev->dev->dev,
  515. DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
  516. aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
  517. /*local flag to mark there's aer request, xen_pcibk callback will use
  518. * this flag to judge whether we need to check pci-front give aer
  519. * service ack signal
  520. */
  521. set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  522. /*It is possible that a pcifront conf_read_write ops request invokes
  523. * the callback which cause the spurious execution of wake_up.
  524. * Yet it is harmless and better than a spinlock here
  525. */
  526. set_bit(_XEN_PCIB_active,
  527. (unsigned long *)&psdev->pdev->sh_info->flags);
  528. wmb();
  529. notify_remote_via_irq(psdev->pdev->evtchn_irq);
  530. ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
  531. !(test_bit(_XEN_PCIB_active, (unsigned long *)
  532. &psdev->pdev->sh_info->flags)), 300*HZ);
  533. if (!ret) {
  534. if (test_bit(_XEN_PCIB_active,
  535. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  536. dev_err(&psdev->dev->dev,
  537. "pcifront aer process not responding!\n");
  538. clear_bit(_XEN_PCIB_active,
  539. (unsigned long *)&psdev->pdev->sh_info->flags);
  540. aer_op->err = PCI_ERS_RESULT_NONE;
  541. return res;
  542. }
  543. }
  544. clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  545. if (test_bit(_XEN_PCIF_active,
  546. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  547. dev_dbg(&psdev->dev->dev,
  548. "schedule pci_conf service in " DRV_NAME "\n");
  549. xen_pcibk_test_and_schedule_op(psdev->pdev);
  550. }
  551. res = (pci_ers_result_t)aer_op->err;
  552. return res;
  553. }
  554. /*
  555. * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
  556. * of the device driver could provide this service, and then wait for pcifront
  557. * ack.
  558. * @dev: pointer to PCI devices
  559. * return value is used by aer_core do_recovery policy
  560. */
  561. static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
  562. {
  563. struct pcistub_device *psdev;
  564. pci_ers_result_t result;
  565. result = PCI_ERS_RESULT_RECOVERED;
  566. dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
  567. dev->bus->number, dev->devfn);
  568. down_write(&pcistub_sem);
  569. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  570. dev->bus->number,
  571. PCI_SLOT(dev->devfn),
  572. PCI_FUNC(dev->devfn));
  573. if (!psdev || !psdev->pdev) {
  574. dev_err(&dev->dev,
  575. DRV_NAME " device is not found/assigned\n");
  576. goto end;
  577. }
  578. if (!psdev->pdev->sh_info) {
  579. dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
  580. " by HVM, kill it\n");
  581. kill_domain_by_device(psdev);
  582. goto end;
  583. }
  584. if (!test_bit(_XEN_PCIB_AERHANDLER,
  585. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  586. dev_err(&dev->dev,
  587. "guest with no AER driver should have been killed\n");
  588. goto end;
  589. }
  590. result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
  591. if (result == PCI_ERS_RESULT_NONE ||
  592. result == PCI_ERS_RESULT_DISCONNECT) {
  593. dev_dbg(&dev->dev,
  594. "No AER slot_reset service or disconnected!\n");
  595. kill_domain_by_device(psdev);
  596. }
  597. end:
  598. if (psdev)
  599. pcistub_device_put(psdev);
  600. up_write(&pcistub_sem);
  601. return result;
  602. }
  603. /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
  604. * in case of the device driver could provide this service, and then wait
  605. * for pcifront ack
  606. * @dev: pointer to PCI devices
  607. * return value is used by aer_core do_recovery policy
  608. */
  609. static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
  610. {
  611. struct pcistub_device *psdev;
  612. pci_ers_result_t result;
  613. result = PCI_ERS_RESULT_RECOVERED;
  614. dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
  615. dev->bus->number, dev->devfn);
  616. down_write(&pcistub_sem);
  617. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  618. dev->bus->number,
  619. PCI_SLOT(dev->devfn),
  620. PCI_FUNC(dev->devfn));
  621. if (!psdev || !psdev->pdev) {
  622. dev_err(&dev->dev,
  623. DRV_NAME " device is not found/assigned\n");
  624. goto end;
  625. }
  626. if (!psdev->pdev->sh_info) {
  627. dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
  628. " by HVM, kill it\n");
  629. kill_domain_by_device(psdev);
  630. goto end;
  631. }
  632. if (!test_bit(_XEN_PCIB_AERHANDLER,
  633. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  634. dev_err(&dev->dev,
  635. "guest with no AER driver should have been killed\n");
  636. goto end;
  637. }
  638. result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
  639. if (result == PCI_ERS_RESULT_NONE ||
  640. result == PCI_ERS_RESULT_DISCONNECT) {
  641. dev_dbg(&dev->dev,
  642. "No AER mmio_enabled service or disconnected!\n");
  643. kill_domain_by_device(psdev);
  644. }
  645. end:
  646. if (psdev)
  647. pcistub_device_put(psdev);
  648. up_write(&pcistub_sem);
  649. return result;
  650. }
  651. /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
  652. * in case of the device driver could provide this service, and then wait
  653. * for pcifront ack.
  654. * @dev: pointer to PCI devices
  655. * @error: the current PCI connection state
  656. * return value is used by aer_core do_recovery policy
  657. */
  658. static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
  659. pci_channel_state_t error)
  660. {
  661. struct pcistub_device *psdev;
  662. pci_ers_result_t result;
  663. result = PCI_ERS_RESULT_CAN_RECOVER;
  664. dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
  665. dev->bus->number, dev->devfn);
  666. down_write(&pcistub_sem);
  667. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  668. dev->bus->number,
  669. PCI_SLOT(dev->devfn),
  670. PCI_FUNC(dev->devfn));
  671. if (!psdev || !psdev->pdev) {
  672. dev_err(&dev->dev,
  673. DRV_NAME " device is not found/assigned\n");
  674. goto end;
  675. }
  676. if (!psdev->pdev->sh_info) {
  677. dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
  678. " by HVM, kill it\n");
  679. kill_domain_by_device(psdev);
  680. goto end;
  681. }
  682. /*Guest owns the device yet no aer handler regiested, kill guest*/
  683. if (!test_bit(_XEN_PCIB_AERHANDLER,
  684. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  685. dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
  686. kill_domain_by_device(psdev);
  687. goto end;
  688. }
  689. result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
  690. if (result == PCI_ERS_RESULT_NONE ||
  691. result == PCI_ERS_RESULT_DISCONNECT) {
  692. dev_dbg(&dev->dev,
  693. "No AER error_detected service or disconnected!\n");
  694. kill_domain_by_device(psdev);
  695. }
  696. end:
  697. if (psdev)
  698. pcistub_device_put(psdev);
  699. up_write(&pcistub_sem);
  700. return result;
  701. }
  702. /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
  703. * in case of the device driver could provide this service, and then wait
  704. * for pcifront ack.
  705. * @dev: pointer to PCI devices
  706. */
  707. static void xen_pcibk_error_resume(struct pci_dev *dev)
  708. {
  709. struct pcistub_device *psdev;
  710. dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
  711. dev->bus->number, dev->devfn);
  712. down_write(&pcistub_sem);
  713. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  714. dev->bus->number,
  715. PCI_SLOT(dev->devfn),
  716. PCI_FUNC(dev->devfn));
  717. if (!psdev || !psdev->pdev) {
  718. dev_err(&dev->dev,
  719. DRV_NAME " device is not found/assigned\n");
  720. goto end;
  721. }
  722. if (!psdev->pdev->sh_info) {
  723. dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
  724. " by HVM, kill it\n");
  725. kill_domain_by_device(psdev);
  726. goto end;
  727. }
  728. if (!test_bit(_XEN_PCIB_AERHANDLER,
  729. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  730. dev_err(&dev->dev,
  731. "guest with no AER driver should have been killed\n");
  732. kill_domain_by_device(psdev);
  733. goto end;
  734. }
  735. common_process(psdev, 1, XEN_PCI_OP_aer_resume,
  736. PCI_ERS_RESULT_RECOVERED);
  737. end:
  738. if (psdev)
  739. pcistub_device_put(psdev);
  740. up_write(&pcistub_sem);
  741. return;
  742. }
  743. /*add xen_pcibk AER handling*/
  744. static const struct pci_error_handlers xen_pcibk_error_handler = {
  745. .error_detected = xen_pcibk_error_detected,
  746. .mmio_enabled = xen_pcibk_mmio_enabled,
  747. .slot_reset = xen_pcibk_slot_reset,
  748. .resume = xen_pcibk_error_resume,
  749. };
  750. /*
  751. * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
  752. * for a normal device. I don't want it to be loaded automatically.
  753. */
  754. static struct pci_driver xen_pcibk_pci_driver = {
  755. /* The name should be xen_pciback, but until the tools are updated
  756. * we will keep it as pciback. */
  757. .name = "pciback",
  758. .id_table = pcistub_ids,
  759. .probe = pcistub_probe,
  760. .remove = pcistub_remove,
  761. .err_handler = &xen_pcibk_error_handler,
  762. };
  763. static inline int str_to_slot(const char *buf, int *domain, int *bus,
  764. int *slot, int *func)
  765. {
  766. int parsed = 0;
  767. switch (sscanf(buf, " %x:%x:%x.%x %n", domain, bus, slot, func,
  768. &parsed)) {
  769. case 3:
  770. *func = -1;
  771. sscanf(buf, " %x:%x:%x.* %n", domain, bus, slot, &parsed);
  772. break;
  773. case 2:
  774. *slot = *func = -1;
  775. sscanf(buf, " %x:%x:*.* %n", domain, bus, &parsed);
  776. break;
  777. }
  778. if (parsed && !buf[parsed])
  779. return 0;
  780. /* try again without domain */
  781. *domain = 0;
  782. switch (sscanf(buf, " %x:%x.%x %n", bus, slot, func, &parsed)) {
  783. case 2:
  784. *func = -1;
  785. sscanf(buf, " %x:%x.* %n", bus, slot, &parsed);
  786. break;
  787. case 1:
  788. *slot = *func = -1;
  789. sscanf(buf, " %x:*.* %n", bus, &parsed);
  790. break;
  791. }
  792. if (parsed && !buf[parsed])
  793. return 0;
  794. return -EINVAL;
  795. }
  796. static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
  797. *slot, int *func, int *reg, int *size, int *mask)
  798. {
  799. int parsed = 0;
  800. sscanf(buf, " %x:%x:%x.%x-%x:%x:%x %n", domain, bus, slot, func,
  801. reg, size, mask, &parsed);
  802. if (parsed && !buf[parsed])
  803. return 0;
  804. /* try again without domain */
  805. *domain = 0;
  806. sscanf(buf, " %x:%x.%x-%x:%x:%x %n", bus, slot, func, reg, size,
  807. mask, &parsed);
  808. if (parsed && !buf[parsed])
  809. return 0;
  810. return -EINVAL;
  811. }
  812. static int pcistub_device_id_add(int domain, int bus, int slot, int func)
  813. {
  814. struct pcistub_device_id *pci_dev_id;
  815. unsigned long flags;
  816. int rc = 0, devfn = PCI_DEVFN(slot, func);
  817. if (slot < 0) {
  818. for (slot = 0; !rc && slot < 32; ++slot)
  819. rc = pcistub_device_id_add(domain, bus, slot, func);
  820. return rc;
  821. }
  822. if (func < 0) {
  823. for (func = 0; !rc && func < 8; ++func)
  824. rc = pcistub_device_id_add(domain, bus, slot, func);
  825. return rc;
  826. }
  827. if ((
  828. #if !defined(MODULE) /* pci_domains_supported is not being exported */ \
  829. || !defined(CONFIG_PCI_DOMAINS)
  830. !pci_domains_supported ? domain :
  831. #endif
  832. domain < 0 || domain > 0xffff)
  833. || bus < 0 || bus > 0xff
  834. || PCI_SLOT(devfn) != slot
  835. || PCI_FUNC(devfn) != func)
  836. return -EINVAL;
  837. pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
  838. if (!pci_dev_id)
  839. return -ENOMEM;
  840. pci_dev_id->domain = domain;
  841. pci_dev_id->bus = bus;
  842. pci_dev_id->devfn = devfn;
  843. pr_debug("wants to seize %04x:%02x:%02x.%d\n",
  844. domain, bus, slot, func);
  845. spin_lock_irqsave(&device_ids_lock, flags);
  846. list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
  847. spin_unlock_irqrestore(&device_ids_lock, flags);
  848. return 0;
  849. }
  850. static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
  851. {
  852. struct pcistub_device_id *pci_dev_id, *t;
  853. int err = -ENOENT;
  854. unsigned long flags;
  855. spin_lock_irqsave(&device_ids_lock, flags);
  856. list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
  857. slot_list) {
  858. if (pci_dev_id->domain == domain && pci_dev_id->bus == bus
  859. && (slot < 0 || PCI_SLOT(pci_dev_id->devfn) == slot)
  860. && (func < 0 || PCI_FUNC(pci_dev_id->devfn) == func)) {
  861. /* Don't break; here because it's possible the same
  862. * slot could be in the list more than once
  863. */
  864. list_del(&pci_dev_id->slot_list);
  865. kfree(pci_dev_id);
  866. err = 0;
  867. pr_debug("removed %04x:%02x:%02x.%d from seize list\n",
  868. domain, bus, slot, func);
  869. }
  870. }
  871. spin_unlock_irqrestore(&device_ids_lock, flags);
  872. return err;
  873. }
  874. static int pcistub_reg_add(int domain, int bus, int slot, int func,
  875. unsigned int reg, unsigned int size,
  876. unsigned int mask)
  877. {
  878. int err = 0;
  879. struct pcistub_device *psdev;
  880. struct pci_dev *dev;
  881. struct config_field *field;
  882. if (reg > 0xfff || (size < 4 && (mask >> (size * 8))))
  883. return -EINVAL;
  884. psdev = pcistub_device_find(domain, bus, slot, func);
  885. if (!psdev) {
  886. err = -ENODEV;
  887. goto out;
  888. }
  889. dev = psdev->dev;
  890. field = kzalloc(sizeof(*field), GFP_ATOMIC);
  891. if (!field) {
  892. err = -ENOMEM;
  893. goto out;
  894. }
  895. field->offset = reg;
  896. field->size = size;
  897. field->mask = mask;
  898. field->init = NULL;
  899. field->reset = NULL;
  900. field->release = NULL;
  901. field->clean = xen_pcibk_config_field_free;
  902. err = xen_pcibk_config_quirks_add_field(dev, field);
  903. if (err)
  904. kfree(field);
  905. out:
  906. if (psdev)
  907. pcistub_device_put(psdev);
  908. return err;
  909. }
  910. static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
  911. size_t count)
  912. {
  913. int domain, bus, slot, func;
  914. int err;
  915. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  916. if (err)
  917. goto out;
  918. err = pcistub_device_id_add(domain, bus, slot, func);
  919. out:
  920. if (!err)
  921. err = count;
  922. return err;
  923. }
  924. static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
  925. static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
  926. size_t count)
  927. {
  928. int domain, bus, slot, func;
  929. int err;
  930. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  931. if (err)
  932. goto out;
  933. err = pcistub_device_id_remove(domain, bus, slot, func);
  934. out:
  935. if (!err)
  936. err = count;
  937. return err;
  938. }
  939. static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
  940. static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
  941. {
  942. struct pcistub_device_id *pci_dev_id;
  943. size_t count = 0;
  944. unsigned long flags;
  945. spin_lock_irqsave(&device_ids_lock, flags);
  946. list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
  947. if (count >= PAGE_SIZE)
  948. break;
  949. count += scnprintf(buf + count, PAGE_SIZE - count,
  950. "%04x:%02x:%02x.%d\n",
  951. pci_dev_id->domain, pci_dev_id->bus,
  952. PCI_SLOT(pci_dev_id->devfn),
  953. PCI_FUNC(pci_dev_id->devfn));
  954. }
  955. spin_unlock_irqrestore(&device_ids_lock, flags);
  956. return count;
  957. }
  958. static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
  959. static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
  960. {
  961. struct pcistub_device *psdev;
  962. struct xen_pcibk_dev_data *dev_data;
  963. size_t count = 0;
  964. unsigned long flags;
  965. spin_lock_irqsave(&pcistub_devices_lock, flags);
  966. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  967. if (count >= PAGE_SIZE)
  968. break;
  969. if (!psdev->dev)
  970. continue;
  971. dev_data = pci_get_drvdata(psdev->dev);
  972. if (!dev_data)
  973. continue;
  974. count +=
  975. scnprintf(buf + count, PAGE_SIZE - count,
  976. "%s:%s:%sing:%ld\n",
  977. pci_name(psdev->dev),
  978. dev_data->isr_on ? "on" : "off",
  979. dev_data->ack_intr ? "ack" : "not ack",
  980. dev_data->handled);
  981. }
  982. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  983. return count;
  984. }
  985. static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
  986. static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
  987. const char *buf,
  988. size_t count)
  989. {
  990. struct pcistub_device *psdev;
  991. struct xen_pcibk_dev_data *dev_data;
  992. int domain, bus, slot, func;
  993. int err;
  994. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  995. if (err)
  996. return err;
  997. psdev = pcistub_device_find(domain, bus, slot, func);
  998. if (!psdev) {
  999. err = -ENOENT;
  1000. goto out;
  1001. }
  1002. dev_data = pci_get_drvdata(psdev->dev);
  1003. if (!dev_data) {
  1004. err = -ENOENT;
  1005. goto out;
  1006. }
  1007. dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
  1008. dev_data->irq_name, dev_data->isr_on,
  1009. !dev_data->isr_on);
  1010. dev_data->isr_on = !(dev_data->isr_on);
  1011. if (dev_data->isr_on)
  1012. dev_data->ack_intr = 1;
  1013. out:
  1014. if (psdev)
  1015. pcistub_device_put(psdev);
  1016. if (!err)
  1017. err = count;
  1018. return err;
  1019. }
  1020. static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
  1021. pcistub_irq_handler_switch);
  1022. static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
  1023. size_t count)
  1024. {
  1025. int domain, bus, slot, func, reg, size, mask;
  1026. int err;
  1027. err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
  1028. &mask);
  1029. if (err)
  1030. goto out;
  1031. err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
  1032. out:
  1033. if (!err)
  1034. err = count;
  1035. return err;
  1036. }
  1037. static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
  1038. {
  1039. int count = 0;
  1040. unsigned long flags;
  1041. struct xen_pcibk_config_quirk *quirk;
  1042. struct xen_pcibk_dev_data *dev_data;
  1043. const struct config_field *field;
  1044. const struct config_field_entry *cfg_entry;
  1045. spin_lock_irqsave(&device_ids_lock, flags);
  1046. list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
  1047. if (count >= PAGE_SIZE)
  1048. goto out;
  1049. count += scnprintf(buf + count, PAGE_SIZE - count,
  1050. "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
  1051. quirk->pdev->bus->number,
  1052. PCI_SLOT(quirk->pdev->devfn),
  1053. PCI_FUNC(quirk->pdev->devfn),
  1054. quirk->devid.vendor, quirk->devid.device,
  1055. quirk->devid.subvendor,
  1056. quirk->devid.subdevice);
  1057. dev_data = pci_get_drvdata(quirk->pdev);
  1058. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  1059. field = cfg_entry->field;
  1060. if (count >= PAGE_SIZE)
  1061. goto out;
  1062. count += scnprintf(buf + count, PAGE_SIZE - count,
  1063. "\t\t%08x:%01x:%08x\n",
  1064. cfg_entry->base_offset +
  1065. field->offset, field->size,
  1066. field->mask);
  1067. }
  1068. }
  1069. out:
  1070. spin_unlock_irqrestore(&device_ids_lock, flags);
  1071. return count;
  1072. }
  1073. static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
  1074. pcistub_quirk_add);
  1075. static ssize_t permissive_add(struct device_driver *drv, const char *buf,
  1076. size_t count)
  1077. {
  1078. int domain, bus, slot, func;
  1079. int err;
  1080. struct pcistub_device *psdev;
  1081. struct xen_pcibk_dev_data *dev_data;
  1082. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  1083. if (err)
  1084. goto out;
  1085. psdev = pcistub_device_find(domain, bus, slot, func);
  1086. if (!psdev) {
  1087. err = -ENODEV;
  1088. goto out;
  1089. }
  1090. dev_data = pci_get_drvdata(psdev->dev);
  1091. /* the driver data for a device should never be null at this point */
  1092. if (!dev_data) {
  1093. err = -ENXIO;
  1094. goto release;
  1095. }
  1096. if (!dev_data->permissive) {
  1097. dev_data->permissive = 1;
  1098. /* Let user know that what they're doing could be unsafe */
  1099. dev_warn(&psdev->dev->dev, "enabling permissive mode "
  1100. "configuration space accesses!\n");
  1101. dev_warn(&psdev->dev->dev,
  1102. "permissive mode is potentially unsafe!\n");
  1103. }
  1104. release:
  1105. pcistub_device_put(psdev);
  1106. out:
  1107. if (!err)
  1108. err = count;
  1109. return err;
  1110. }
  1111. static ssize_t permissive_show(struct device_driver *drv, char *buf)
  1112. {
  1113. struct pcistub_device *psdev;
  1114. struct xen_pcibk_dev_data *dev_data;
  1115. size_t count = 0;
  1116. unsigned long flags;
  1117. spin_lock_irqsave(&pcistub_devices_lock, flags);
  1118. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  1119. if (count >= PAGE_SIZE)
  1120. break;
  1121. if (!psdev->dev)
  1122. continue;
  1123. dev_data = pci_get_drvdata(psdev->dev);
  1124. if (!dev_data || !dev_data->permissive)
  1125. continue;
  1126. count +=
  1127. scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
  1128. pci_name(psdev->dev));
  1129. }
  1130. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  1131. return count;
  1132. }
  1133. static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
  1134. permissive_add);
  1135. static void pcistub_exit(void)
  1136. {
  1137. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
  1138. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1139. &driver_attr_remove_slot);
  1140. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
  1141. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
  1142. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1143. &driver_attr_permissive);
  1144. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1145. &driver_attr_irq_handlers);
  1146. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1147. &driver_attr_irq_handler_state);
  1148. pci_unregister_driver(&xen_pcibk_pci_driver);
  1149. }
  1150. static int __init pcistub_init(void)
  1151. {
  1152. int pos = 0;
  1153. int err = 0;
  1154. int domain, bus, slot, func;
  1155. int parsed;
  1156. if (pci_devs_to_hide && *pci_devs_to_hide) {
  1157. do {
  1158. parsed = 0;
  1159. err = sscanf(pci_devs_to_hide + pos,
  1160. " (%x:%x:%x.%x) %n",
  1161. &domain, &bus, &slot, &func, &parsed);
  1162. switch (err) {
  1163. case 3:
  1164. func = -1;
  1165. sscanf(pci_devs_to_hide + pos,
  1166. " (%x:%x:%x.*) %n",
  1167. &domain, &bus, &slot, &parsed);
  1168. break;
  1169. case 2:
  1170. slot = func = -1;
  1171. sscanf(pci_devs_to_hide + pos,
  1172. " (%x:%x:*.*) %n",
  1173. &domain, &bus, &parsed);
  1174. break;
  1175. }
  1176. if (!parsed) {
  1177. domain = 0;
  1178. err = sscanf(pci_devs_to_hide + pos,
  1179. " (%x:%x.%x) %n",
  1180. &bus, &slot, &func, &parsed);
  1181. switch (err) {
  1182. case 2:
  1183. func = -1;
  1184. sscanf(pci_devs_to_hide + pos,
  1185. " (%x:%x.*) %n",
  1186. &bus, &slot, &parsed);
  1187. break;
  1188. case 1:
  1189. slot = func = -1;
  1190. sscanf(pci_devs_to_hide + pos,
  1191. " (%x:*.*) %n",
  1192. &bus, &parsed);
  1193. break;
  1194. }
  1195. }
  1196. if (parsed <= 0)
  1197. goto parse_error;
  1198. err = pcistub_device_id_add(domain, bus, slot, func);
  1199. if (err)
  1200. goto out;
  1201. pos += parsed;
  1202. } while (pci_devs_to_hide[pos]);
  1203. }
  1204. /* If we're the first PCI Device Driver to register, we're the
  1205. * first one to get offered PCI devices as they become
  1206. * available (and thus we can be the first to grab them)
  1207. */
  1208. err = pci_register_driver(&xen_pcibk_pci_driver);
  1209. if (err < 0)
  1210. goto out;
  1211. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1212. &driver_attr_new_slot);
  1213. if (!err)
  1214. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1215. &driver_attr_remove_slot);
  1216. if (!err)
  1217. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1218. &driver_attr_slots);
  1219. if (!err)
  1220. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1221. &driver_attr_quirks);
  1222. if (!err)
  1223. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1224. &driver_attr_permissive);
  1225. if (!err)
  1226. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1227. &driver_attr_irq_handlers);
  1228. if (!err)
  1229. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1230. &driver_attr_irq_handler_state);
  1231. if (err)
  1232. pcistub_exit();
  1233. out:
  1234. return err;
  1235. parse_error:
  1236. pr_err("Error parsing pci_devs_to_hide at \"%s\"\n",
  1237. pci_devs_to_hide + pos);
  1238. return -EINVAL;
  1239. }
  1240. #ifndef MODULE
  1241. /*
  1242. * fs_initcall happens before device_initcall
  1243. * so xen_pcibk *should* get called first (b/c we
  1244. * want to suck up any device before other drivers
  1245. * get a chance by being the first pci device
  1246. * driver to register)
  1247. */
  1248. fs_initcall(pcistub_init);
  1249. #endif
  1250. static int __init xen_pcibk_init(void)
  1251. {
  1252. int err;
  1253. if (!xen_initial_domain())
  1254. return -ENODEV;
  1255. err = xen_pcibk_config_init();
  1256. if (err)
  1257. return err;
  1258. #ifdef MODULE
  1259. err = pcistub_init();
  1260. if (err < 0)
  1261. return err;
  1262. #endif
  1263. pcistub_init_devices_late();
  1264. err = xen_pcibk_xenbus_register();
  1265. if (err)
  1266. pcistub_exit();
  1267. return err;
  1268. }
  1269. static void __exit xen_pcibk_cleanup(void)
  1270. {
  1271. xen_pcibk_xenbus_unregister();
  1272. pcistub_exit();
  1273. }
  1274. module_init(xen_pcibk_init);
  1275. module_exit(xen_pcibk_cleanup);
  1276. MODULE_LICENSE("Dual BSD/GPL");
  1277. MODULE_ALIAS("xen-backend:pci");