pci_stub.c 37 KB

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