pci_stub.c 38 KB

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