pci_stub.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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. }
  299. /* Now disable the device (this also ensures some private device
  300. * data is setup before we export)
  301. */
  302. dev_dbg(&dev->dev, "reset device\n");
  303. xen_pcibk_reset_device(dev);
  304. dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
  305. return 0;
  306. config_release:
  307. xen_pcibk_config_free_dev(dev);
  308. out:
  309. pci_set_drvdata(dev, NULL);
  310. kfree(dev_data);
  311. return err;
  312. }
  313. /*
  314. * Because some initialization still happens on
  315. * devices during fs_initcall, we need to defer
  316. * full initialization of our devices until
  317. * device_initcall.
  318. */
  319. static int __init pcistub_init_devices_late(void)
  320. {
  321. struct pcistub_device *psdev;
  322. unsigned long flags;
  323. int err = 0;
  324. pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
  325. spin_lock_irqsave(&pcistub_devices_lock, flags);
  326. while (!list_empty(&seized_devices)) {
  327. psdev = container_of(seized_devices.next,
  328. struct pcistub_device, dev_list);
  329. list_del(&psdev->dev_list);
  330. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  331. err = pcistub_init_device(psdev->dev);
  332. if (err) {
  333. dev_err(&psdev->dev->dev,
  334. "error %d initializing device\n", err);
  335. kfree(psdev);
  336. psdev = NULL;
  337. }
  338. spin_lock_irqsave(&pcistub_devices_lock, flags);
  339. if (psdev)
  340. list_add_tail(&psdev->dev_list, &pcistub_devices);
  341. }
  342. initialize_devices = 1;
  343. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  344. return 0;
  345. }
  346. static int __devinit pcistub_seize(struct pci_dev *dev)
  347. {
  348. struct pcistub_device *psdev;
  349. unsigned long flags;
  350. int err = 0;
  351. psdev = pcistub_device_alloc(dev);
  352. if (!psdev)
  353. return -ENOMEM;
  354. spin_lock_irqsave(&pcistub_devices_lock, flags);
  355. if (initialize_devices) {
  356. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  357. /* don't want irqs disabled when calling pcistub_init_device */
  358. err = pcistub_init_device(psdev->dev);
  359. spin_lock_irqsave(&pcistub_devices_lock, flags);
  360. if (!err)
  361. list_add(&psdev->dev_list, &pcistub_devices);
  362. } else {
  363. dev_dbg(&dev->dev, "deferring initialization\n");
  364. list_add(&psdev->dev_list, &seized_devices);
  365. }
  366. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  367. if (err)
  368. pcistub_device_put(psdev);
  369. return err;
  370. }
  371. static int __devinit pcistub_probe(struct pci_dev *dev,
  372. const struct pci_device_id *id)
  373. {
  374. int err = 0;
  375. dev_dbg(&dev->dev, "probing...\n");
  376. if (pcistub_match(dev)) {
  377. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
  378. && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  379. dev_err(&dev->dev, "can't export pci devices that "
  380. "don't have a normal (0) or bridge (1) "
  381. "header type!\n");
  382. err = -ENODEV;
  383. goto out;
  384. }
  385. dev_info(&dev->dev, "seizing device\n");
  386. err = pcistub_seize(dev);
  387. } else
  388. /* Didn't find the device */
  389. err = -ENODEV;
  390. out:
  391. return err;
  392. }
  393. static void pcistub_remove(struct pci_dev *dev)
  394. {
  395. struct pcistub_device *psdev, *found_psdev = NULL;
  396. unsigned long flags;
  397. dev_dbg(&dev->dev, "removing\n");
  398. spin_lock_irqsave(&pcistub_devices_lock, flags);
  399. xen_pcibk_config_quirk_release(dev);
  400. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  401. if (psdev->dev == dev) {
  402. found_psdev = psdev;
  403. break;
  404. }
  405. }
  406. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  407. if (found_psdev) {
  408. dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
  409. found_psdev->pdev);
  410. if (found_psdev->pdev) {
  411. printk(KERN_WARNING DRV_NAME ": ****** removing device "
  412. "%s while still in-use! ******\n",
  413. pci_name(found_psdev->dev));
  414. printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
  415. " still access this device's i/o resources!\n");
  416. printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
  417. "domain before binding device\n");
  418. printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
  419. "or domains\n");
  420. xen_pcibk_release_pci_dev(found_psdev->pdev,
  421. found_psdev->dev);
  422. }
  423. spin_lock_irqsave(&pcistub_devices_lock, flags);
  424. list_del(&found_psdev->dev_list);
  425. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  426. /* the final put for releasing from the list */
  427. pcistub_device_put(found_psdev);
  428. }
  429. }
  430. static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
  431. {
  432. .vendor = PCI_ANY_ID,
  433. .device = PCI_ANY_ID,
  434. .subvendor = PCI_ANY_ID,
  435. .subdevice = PCI_ANY_ID,
  436. },
  437. {0,},
  438. };
  439. #define PCI_NODENAME_MAX 40
  440. static void kill_domain_by_device(struct pcistub_device *psdev)
  441. {
  442. struct xenbus_transaction xbt;
  443. int err;
  444. char nodename[PCI_NODENAME_MAX];
  445. BUG_ON(!psdev);
  446. snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
  447. psdev->pdev->xdev->otherend_id);
  448. again:
  449. err = xenbus_transaction_start(&xbt);
  450. if (err) {
  451. dev_err(&psdev->dev->dev,
  452. "error %d when start xenbus transaction\n", err);
  453. return;
  454. }
  455. /*PV AER handlers will set this flag*/
  456. xenbus_printf(xbt, nodename, "aerState" , "aerfail");
  457. err = xenbus_transaction_end(xbt, 0);
  458. if (err) {
  459. if (err == -EAGAIN)
  460. goto again;
  461. dev_err(&psdev->dev->dev,
  462. "error %d when end xenbus transaction\n", err);
  463. return;
  464. }
  465. }
  466. /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
  467. * backend need to have cooperation. In xen_pcibk, those steps will do similar
  468. * jobs: send service request and waiting for front_end response.
  469. */
  470. static pci_ers_result_t common_process(struct pcistub_device *psdev,
  471. pci_channel_state_t state, int aer_cmd,
  472. pci_ers_result_t result)
  473. {
  474. pci_ers_result_t res = result;
  475. struct xen_pcie_aer_op *aer_op;
  476. int ret;
  477. /*with PV AER drivers*/
  478. aer_op = &(psdev->pdev->sh_info->aer_op);
  479. aer_op->cmd = aer_cmd ;
  480. /*useful for error_detected callback*/
  481. aer_op->err = state;
  482. /*pcifront_end BDF*/
  483. ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
  484. &aer_op->domain, &aer_op->bus, &aer_op->devfn);
  485. if (!ret) {
  486. dev_err(&psdev->dev->dev,
  487. DRV_NAME ": failed to get pcifront device\n");
  488. return PCI_ERS_RESULT_NONE;
  489. }
  490. wmb();
  491. dev_dbg(&psdev->dev->dev,
  492. DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
  493. aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
  494. /*local flag to mark there's aer request, xen_pcibk callback will use
  495. * this flag to judge whether we need to check pci-front give aer
  496. * service ack signal
  497. */
  498. set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  499. /*It is possible that a pcifront conf_read_write ops request invokes
  500. * the callback which cause the spurious execution of wake_up.
  501. * Yet it is harmless and better than a spinlock here
  502. */
  503. set_bit(_XEN_PCIB_active,
  504. (unsigned long *)&psdev->pdev->sh_info->flags);
  505. wmb();
  506. notify_remote_via_irq(psdev->pdev->evtchn_irq);
  507. ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
  508. !(test_bit(_XEN_PCIB_active, (unsigned long *)
  509. &psdev->pdev->sh_info->flags)), 300*HZ);
  510. if (!ret) {
  511. if (test_bit(_XEN_PCIB_active,
  512. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  513. dev_err(&psdev->dev->dev,
  514. "pcifront aer process not responding!\n");
  515. clear_bit(_XEN_PCIB_active,
  516. (unsigned long *)&psdev->pdev->sh_info->flags);
  517. aer_op->err = PCI_ERS_RESULT_NONE;
  518. return res;
  519. }
  520. }
  521. clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  522. if (test_bit(_XEN_PCIF_active,
  523. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  524. dev_dbg(&psdev->dev->dev,
  525. "schedule pci_conf service in " DRV_NAME "\n");
  526. xen_pcibk_test_and_schedule_op(psdev->pdev);
  527. }
  528. res = (pci_ers_result_t)aer_op->err;
  529. return res;
  530. }
  531. /*
  532. * xen_pcibk_slot_reset: it will send the slot_reset request to pcifront in case
  533. * of the device driver could provide this service, and then wait for pcifront
  534. * ack.
  535. * @dev: pointer to PCI devices
  536. * return value is used by aer_core do_recovery policy
  537. */
  538. static pci_ers_result_t xen_pcibk_slot_reset(struct pci_dev *dev)
  539. {
  540. struct pcistub_device *psdev;
  541. pci_ers_result_t result;
  542. result = PCI_ERS_RESULT_RECOVERED;
  543. dev_dbg(&dev->dev, "xen_pcibk_slot_reset(bus:%x,devfn:%x)\n",
  544. dev->bus->number, dev->devfn);
  545. down_write(&pcistub_sem);
  546. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  547. dev->bus->number,
  548. PCI_SLOT(dev->devfn),
  549. PCI_FUNC(dev->devfn));
  550. if (!psdev || !psdev->pdev) {
  551. dev_err(&dev->dev,
  552. DRV_NAME " device is not found/assigned\n");
  553. goto end;
  554. }
  555. if (!psdev->pdev->sh_info) {
  556. dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
  557. " by HVM, kill it\n");
  558. kill_domain_by_device(psdev);
  559. goto release;
  560. }
  561. if (!test_bit(_XEN_PCIB_AERHANDLER,
  562. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  563. dev_err(&dev->dev,
  564. "guest with no AER driver should have been killed\n");
  565. goto release;
  566. }
  567. result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
  568. if (result == PCI_ERS_RESULT_NONE ||
  569. result == PCI_ERS_RESULT_DISCONNECT) {
  570. dev_dbg(&dev->dev,
  571. "No AER slot_reset service or disconnected!\n");
  572. kill_domain_by_device(psdev);
  573. }
  574. release:
  575. pcistub_device_put(psdev);
  576. end:
  577. up_write(&pcistub_sem);
  578. return result;
  579. }
  580. /*xen_pcibk_mmio_enabled: it will send the mmio_enabled request to pcifront
  581. * in case of the device driver could provide this service, and then wait
  582. * for pcifront ack
  583. * @dev: pointer to PCI devices
  584. * return value is used by aer_core do_recovery policy
  585. */
  586. static pci_ers_result_t xen_pcibk_mmio_enabled(struct pci_dev *dev)
  587. {
  588. struct pcistub_device *psdev;
  589. pci_ers_result_t result;
  590. result = PCI_ERS_RESULT_RECOVERED;
  591. dev_dbg(&dev->dev, "xen_pcibk_mmio_enabled(bus:%x,devfn:%x)\n",
  592. dev->bus->number, dev->devfn);
  593. down_write(&pcistub_sem);
  594. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  595. dev->bus->number,
  596. PCI_SLOT(dev->devfn),
  597. PCI_FUNC(dev->devfn));
  598. if (!psdev || !psdev->pdev) {
  599. dev_err(&dev->dev,
  600. DRV_NAME " device is not found/assigned\n");
  601. goto end;
  602. }
  603. if (!psdev->pdev->sh_info) {
  604. dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
  605. " by HVM, kill it\n");
  606. kill_domain_by_device(psdev);
  607. goto release;
  608. }
  609. if (!test_bit(_XEN_PCIB_AERHANDLER,
  610. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  611. dev_err(&dev->dev,
  612. "guest with no AER driver should have been killed\n");
  613. goto release;
  614. }
  615. result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
  616. if (result == PCI_ERS_RESULT_NONE ||
  617. result == PCI_ERS_RESULT_DISCONNECT) {
  618. dev_dbg(&dev->dev,
  619. "No AER mmio_enabled service or disconnected!\n");
  620. kill_domain_by_device(psdev);
  621. }
  622. release:
  623. pcistub_device_put(psdev);
  624. end:
  625. up_write(&pcistub_sem);
  626. return result;
  627. }
  628. /*xen_pcibk_error_detected: it will send the error_detected request to pcifront
  629. * in case of the device driver could provide this service, and then wait
  630. * for pcifront ack.
  631. * @dev: pointer to PCI devices
  632. * @error: the current PCI connection state
  633. * return value is used by aer_core do_recovery policy
  634. */
  635. static pci_ers_result_t xen_pcibk_error_detected(struct pci_dev *dev,
  636. pci_channel_state_t error)
  637. {
  638. struct pcistub_device *psdev;
  639. pci_ers_result_t result;
  640. result = PCI_ERS_RESULT_CAN_RECOVER;
  641. dev_dbg(&dev->dev, "xen_pcibk_error_detected(bus:%x,devfn:%x)\n",
  642. dev->bus->number, dev->devfn);
  643. down_write(&pcistub_sem);
  644. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  645. dev->bus->number,
  646. PCI_SLOT(dev->devfn),
  647. PCI_FUNC(dev->devfn));
  648. if (!psdev || !psdev->pdev) {
  649. dev_err(&dev->dev,
  650. DRV_NAME " device is not found/assigned\n");
  651. goto end;
  652. }
  653. if (!psdev->pdev->sh_info) {
  654. dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
  655. " by HVM, kill it\n");
  656. kill_domain_by_device(psdev);
  657. goto release;
  658. }
  659. /*Guest owns the device yet no aer handler regiested, kill guest*/
  660. if (!test_bit(_XEN_PCIB_AERHANDLER,
  661. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  662. dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
  663. kill_domain_by_device(psdev);
  664. goto release;
  665. }
  666. result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
  667. if (result == PCI_ERS_RESULT_NONE ||
  668. result == PCI_ERS_RESULT_DISCONNECT) {
  669. dev_dbg(&dev->dev,
  670. "No AER error_detected service or disconnected!\n");
  671. kill_domain_by_device(psdev);
  672. }
  673. release:
  674. pcistub_device_put(psdev);
  675. end:
  676. up_write(&pcistub_sem);
  677. return result;
  678. }
  679. /*xen_pcibk_error_resume: it will send the error_resume request to pcifront
  680. * in case of the device driver could provide this service, and then wait
  681. * for pcifront ack.
  682. * @dev: pointer to PCI devices
  683. */
  684. static void xen_pcibk_error_resume(struct pci_dev *dev)
  685. {
  686. struct pcistub_device *psdev;
  687. dev_dbg(&dev->dev, "xen_pcibk_error_resume(bus:%x,devfn:%x)\n",
  688. dev->bus->number, dev->devfn);
  689. down_write(&pcistub_sem);
  690. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  691. dev->bus->number,
  692. PCI_SLOT(dev->devfn),
  693. PCI_FUNC(dev->devfn));
  694. if (!psdev || !psdev->pdev) {
  695. dev_err(&dev->dev,
  696. DRV_NAME " device is not found/assigned\n");
  697. goto end;
  698. }
  699. if (!psdev->pdev->sh_info) {
  700. dev_err(&dev->dev, DRV_NAME " device is not connected or owned"
  701. " by HVM, kill it\n");
  702. kill_domain_by_device(psdev);
  703. goto release;
  704. }
  705. if (!test_bit(_XEN_PCIB_AERHANDLER,
  706. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  707. dev_err(&dev->dev,
  708. "guest with no AER driver should have been killed\n");
  709. kill_domain_by_device(psdev);
  710. goto release;
  711. }
  712. common_process(psdev, 1, XEN_PCI_OP_aer_resume,
  713. PCI_ERS_RESULT_RECOVERED);
  714. release:
  715. pcistub_device_put(psdev);
  716. end:
  717. up_write(&pcistub_sem);
  718. return;
  719. }
  720. /*add xen_pcibk AER handling*/
  721. static struct pci_error_handlers xen_pcibk_error_handler = {
  722. .error_detected = xen_pcibk_error_detected,
  723. .mmio_enabled = xen_pcibk_mmio_enabled,
  724. .slot_reset = xen_pcibk_slot_reset,
  725. .resume = xen_pcibk_error_resume,
  726. };
  727. /*
  728. * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
  729. * for a normal device. I don't want it to be loaded automatically.
  730. */
  731. static struct pci_driver xen_pcibk_pci_driver = {
  732. /* The name should be xen_pciback, but until the tools are updated
  733. * we will keep it as pciback. */
  734. .name = "pciback",
  735. .id_table = pcistub_ids,
  736. .probe = pcistub_probe,
  737. .remove = pcistub_remove,
  738. .err_handler = &xen_pcibk_error_handler,
  739. };
  740. static inline int str_to_slot(const char *buf, int *domain, int *bus,
  741. int *slot, int *func)
  742. {
  743. int err;
  744. err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
  745. if (err == 4)
  746. return 0;
  747. else if (err < 0)
  748. return -EINVAL;
  749. /* try again without domain */
  750. *domain = 0;
  751. err = sscanf(buf, " %x:%x.%x", bus, slot, func);
  752. if (err == 3)
  753. return 0;
  754. return -EINVAL;
  755. }
  756. static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
  757. *slot, int *func, int *reg, int *size, int *mask)
  758. {
  759. int err;
  760. err =
  761. sscanf(buf, " %04x:%02x:%02x.%d-%08x:%1x:%08x", domain, bus, slot,
  762. func, reg, size, mask);
  763. if (err == 7)
  764. return 0;
  765. return -EINVAL;
  766. }
  767. static int pcistub_device_id_add(int domain, int bus, int slot, int func)
  768. {
  769. struct pcistub_device_id *pci_dev_id;
  770. unsigned long flags;
  771. pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
  772. if (!pci_dev_id)
  773. return -ENOMEM;
  774. pci_dev_id->domain = domain;
  775. pci_dev_id->bus = bus;
  776. pci_dev_id->devfn = PCI_DEVFN(slot, func);
  777. pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%d\n",
  778. domain, bus, slot, func);
  779. spin_lock_irqsave(&device_ids_lock, flags);
  780. list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
  781. spin_unlock_irqrestore(&device_ids_lock, flags);
  782. return 0;
  783. }
  784. static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
  785. {
  786. struct pcistub_device_id *pci_dev_id, *t;
  787. int devfn = PCI_DEVFN(slot, func);
  788. int err = -ENOENT;
  789. unsigned long flags;
  790. spin_lock_irqsave(&device_ids_lock, flags);
  791. list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
  792. slot_list) {
  793. if (pci_dev_id->domain == domain
  794. && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
  795. /* Don't break; here because it's possible the same
  796. * slot could be in the list more than once
  797. */
  798. list_del(&pci_dev_id->slot_list);
  799. kfree(pci_dev_id);
  800. err = 0;
  801. pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%d from "
  802. "seize list\n", domain, bus, slot, func);
  803. }
  804. }
  805. spin_unlock_irqrestore(&device_ids_lock, flags);
  806. return err;
  807. }
  808. static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
  809. int size, int mask)
  810. {
  811. int err = 0;
  812. struct pcistub_device *psdev;
  813. struct pci_dev *dev;
  814. struct config_field *field;
  815. psdev = pcistub_device_find(domain, bus, slot, func);
  816. if (!psdev || !psdev->dev) {
  817. err = -ENODEV;
  818. goto out;
  819. }
  820. dev = psdev->dev;
  821. field = kzalloc(sizeof(*field), GFP_ATOMIC);
  822. if (!field) {
  823. err = -ENOMEM;
  824. goto out;
  825. }
  826. field->offset = reg;
  827. field->size = size;
  828. field->mask = mask;
  829. field->init = NULL;
  830. field->reset = NULL;
  831. field->release = NULL;
  832. field->clean = xen_pcibk_config_field_free;
  833. err = xen_pcibk_config_quirks_add_field(dev, field);
  834. if (err)
  835. kfree(field);
  836. out:
  837. return err;
  838. }
  839. static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
  840. size_t count)
  841. {
  842. int domain, bus, slot, func;
  843. int err;
  844. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  845. if (err)
  846. goto out;
  847. err = pcistub_device_id_add(domain, bus, slot, func);
  848. out:
  849. if (!err)
  850. err = count;
  851. return err;
  852. }
  853. static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
  854. static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
  855. size_t count)
  856. {
  857. int domain, bus, slot, func;
  858. int err;
  859. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  860. if (err)
  861. goto out;
  862. err = pcistub_device_id_remove(domain, bus, slot, func);
  863. out:
  864. if (!err)
  865. err = count;
  866. return err;
  867. }
  868. static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
  869. static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
  870. {
  871. struct pcistub_device_id *pci_dev_id;
  872. size_t count = 0;
  873. unsigned long flags;
  874. spin_lock_irqsave(&device_ids_lock, flags);
  875. list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
  876. if (count >= PAGE_SIZE)
  877. break;
  878. count += scnprintf(buf + count, PAGE_SIZE - count,
  879. "%04x:%02x:%02x.%d\n",
  880. pci_dev_id->domain, pci_dev_id->bus,
  881. PCI_SLOT(pci_dev_id->devfn),
  882. PCI_FUNC(pci_dev_id->devfn));
  883. }
  884. spin_unlock_irqrestore(&device_ids_lock, flags);
  885. return count;
  886. }
  887. static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
  888. static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
  889. {
  890. struct pcistub_device *psdev;
  891. struct xen_pcibk_dev_data *dev_data;
  892. size_t count = 0;
  893. unsigned long flags;
  894. spin_lock_irqsave(&pcistub_devices_lock, flags);
  895. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  896. if (count >= PAGE_SIZE)
  897. break;
  898. if (!psdev->dev)
  899. continue;
  900. dev_data = pci_get_drvdata(psdev->dev);
  901. if (!dev_data)
  902. continue;
  903. count +=
  904. scnprintf(buf + count, PAGE_SIZE - count,
  905. "%s:%s:%sing:%ld\n",
  906. pci_name(psdev->dev),
  907. dev_data->isr_on ? "on" : "off",
  908. dev_data->ack_intr ? "ack" : "not ack",
  909. dev_data->handled);
  910. }
  911. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  912. return count;
  913. }
  914. static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
  915. static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
  916. const char *buf,
  917. size_t count)
  918. {
  919. struct pcistub_device *psdev;
  920. struct xen_pcibk_dev_data *dev_data;
  921. int domain, bus, slot, func;
  922. int err = -ENOENT;
  923. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  924. if (err)
  925. goto out;
  926. psdev = pcistub_device_find(domain, bus, slot, func);
  927. if (!psdev)
  928. goto out;
  929. dev_data = pci_get_drvdata(psdev->dev);
  930. if (!dev_data)
  931. goto out;
  932. dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
  933. dev_data->irq_name, dev_data->isr_on,
  934. !dev_data->isr_on);
  935. dev_data->isr_on = !(dev_data->isr_on);
  936. if (dev_data->isr_on)
  937. dev_data->ack_intr = 1;
  938. out:
  939. if (!err)
  940. err = count;
  941. return err;
  942. }
  943. static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
  944. pcistub_irq_handler_switch);
  945. static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
  946. size_t count)
  947. {
  948. int domain, bus, slot, func, reg, size, mask;
  949. int err;
  950. err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
  951. &mask);
  952. if (err)
  953. goto out;
  954. err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
  955. out:
  956. if (!err)
  957. err = count;
  958. return err;
  959. }
  960. static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
  961. {
  962. int count = 0;
  963. unsigned long flags;
  964. struct xen_pcibk_config_quirk *quirk;
  965. struct xen_pcibk_dev_data *dev_data;
  966. const struct config_field *field;
  967. const struct config_field_entry *cfg_entry;
  968. spin_lock_irqsave(&device_ids_lock, flags);
  969. list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
  970. if (count >= PAGE_SIZE)
  971. goto out;
  972. count += scnprintf(buf + count, PAGE_SIZE - count,
  973. "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
  974. quirk->pdev->bus->number,
  975. PCI_SLOT(quirk->pdev->devfn),
  976. PCI_FUNC(quirk->pdev->devfn),
  977. quirk->devid.vendor, quirk->devid.device,
  978. quirk->devid.subvendor,
  979. quirk->devid.subdevice);
  980. dev_data = pci_get_drvdata(quirk->pdev);
  981. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  982. field = cfg_entry->field;
  983. if (count >= PAGE_SIZE)
  984. goto out;
  985. count += scnprintf(buf + count, PAGE_SIZE - count,
  986. "\t\t%08x:%01x:%08x\n",
  987. cfg_entry->base_offset +
  988. field->offset, field->size,
  989. field->mask);
  990. }
  991. }
  992. out:
  993. spin_unlock_irqrestore(&device_ids_lock, flags);
  994. return count;
  995. }
  996. static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
  997. pcistub_quirk_add);
  998. static ssize_t permissive_add(struct device_driver *drv, const char *buf,
  999. size_t count)
  1000. {
  1001. int domain, bus, slot, func;
  1002. int err;
  1003. struct pcistub_device *psdev;
  1004. struct xen_pcibk_dev_data *dev_data;
  1005. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  1006. if (err)
  1007. goto out;
  1008. psdev = pcistub_device_find(domain, bus, slot, func);
  1009. if (!psdev) {
  1010. err = -ENODEV;
  1011. goto out;
  1012. }
  1013. if (!psdev->dev) {
  1014. err = -ENODEV;
  1015. goto release;
  1016. }
  1017. dev_data = pci_get_drvdata(psdev->dev);
  1018. /* the driver data for a device should never be null at this point */
  1019. if (!dev_data) {
  1020. err = -ENXIO;
  1021. goto release;
  1022. }
  1023. if (!dev_data->permissive) {
  1024. dev_data->permissive = 1;
  1025. /* Let user know that what they're doing could be unsafe */
  1026. dev_warn(&psdev->dev->dev, "enabling permissive mode "
  1027. "configuration space accesses!\n");
  1028. dev_warn(&psdev->dev->dev,
  1029. "permissive mode is potentially unsafe!\n");
  1030. }
  1031. release:
  1032. pcistub_device_put(psdev);
  1033. out:
  1034. if (!err)
  1035. err = count;
  1036. return err;
  1037. }
  1038. static ssize_t permissive_show(struct device_driver *drv, char *buf)
  1039. {
  1040. struct pcistub_device *psdev;
  1041. struct xen_pcibk_dev_data *dev_data;
  1042. size_t count = 0;
  1043. unsigned long flags;
  1044. spin_lock_irqsave(&pcistub_devices_lock, flags);
  1045. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  1046. if (count >= PAGE_SIZE)
  1047. break;
  1048. if (!psdev->dev)
  1049. continue;
  1050. dev_data = pci_get_drvdata(psdev->dev);
  1051. if (!dev_data || !dev_data->permissive)
  1052. continue;
  1053. count +=
  1054. scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
  1055. pci_name(psdev->dev));
  1056. }
  1057. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  1058. return count;
  1059. }
  1060. static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
  1061. permissive_add);
  1062. static void pcistub_exit(void)
  1063. {
  1064. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
  1065. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1066. &driver_attr_remove_slot);
  1067. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
  1068. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
  1069. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1070. &driver_attr_permissive);
  1071. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1072. &driver_attr_irq_handlers);
  1073. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1074. &driver_attr_irq_handler_state);
  1075. pci_unregister_driver(&xen_pcibk_pci_driver);
  1076. }
  1077. static int __init pcistub_init(void)
  1078. {
  1079. int pos = 0;
  1080. int err = 0;
  1081. int domain, bus, slot, func;
  1082. int parsed;
  1083. if (pci_devs_to_hide && *pci_devs_to_hide) {
  1084. do {
  1085. parsed = 0;
  1086. err = sscanf(pci_devs_to_hide + pos,
  1087. " (%x:%x:%x.%x) %n",
  1088. &domain, &bus, &slot, &func, &parsed);
  1089. if (err != 4) {
  1090. domain = 0;
  1091. err = sscanf(pci_devs_to_hide + pos,
  1092. " (%x:%x.%x) %n",
  1093. &bus, &slot, &func, &parsed);
  1094. if (err != 3)
  1095. goto parse_error;
  1096. }
  1097. err = pcistub_device_id_add(domain, bus, slot, func);
  1098. if (err)
  1099. goto out;
  1100. /* if parsed<=0, we've reached the end of the string */
  1101. pos += parsed;
  1102. } while (parsed > 0 && pci_devs_to_hide[pos]);
  1103. }
  1104. /* If we're the first PCI Device Driver to register, we're the
  1105. * first one to get offered PCI devices as they become
  1106. * available (and thus we can be the first to grab them)
  1107. */
  1108. err = pci_register_driver(&xen_pcibk_pci_driver);
  1109. if (err < 0)
  1110. goto out;
  1111. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1112. &driver_attr_new_slot);
  1113. if (!err)
  1114. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1115. &driver_attr_remove_slot);
  1116. if (!err)
  1117. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1118. &driver_attr_slots);
  1119. if (!err)
  1120. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1121. &driver_attr_quirks);
  1122. if (!err)
  1123. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1124. &driver_attr_permissive);
  1125. if (!err)
  1126. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1127. &driver_attr_irq_handlers);
  1128. if (!err)
  1129. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1130. &driver_attr_irq_handler_state);
  1131. if (err)
  1132. pcistub_exit();
  1133. out:
  1134. return err;
  1135. parse_error:
  1136. printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
  1137. pci_devs_to_hide + pos);
  1138. return -EINVAL;
  1139. }
  1140. #ifndef MODULE
  1141. /*
  1142. * fs_initcall happens before device_initcall
  1143. * so xen_pcibk *should* get called first (b/c we
  1144. * want to suck up any device before other drivers
  1145. * get a chance by being the first pci device
  1146. * driver to register)
  1147. */
  1148. fs_initcall(pcistub_init);
  1149. #endif
  1150. static int __init xen_pcibk_init(void)
  1151. {
  1152. int err;
  1153. if (!xen_initial_domain())
  1154. return -ENODEV;
  1155. err = xen_pcibk_config_init();
  1156. if (err)
  1157. return err;
  1158. #ifdef MODULE
  1159. err = pcistub_init();
  1160. if (err < 0)
  1161. return err;
  1162. #endif
  1163. pcistub_init_devices_late();
  1164. err = xen_pcibk_xenbus_register();
  1165. if (err)
  1166. pcistub_exit();
  1167. return err;
  1168. }
  1169. static void __exit xen_pcibk_cleanup(void)
  1170. {
  1171. xen_pcibk_xenbus_unregister();
  1172. pcistub_exit();
  1173. }
  1174. module_init(xen_pcibk_init);
  1175. module_exit(xen_pcibk_cleanup);
  1176. MODULE_LICENSE("Dual BSD/GPL");
  1177. MODULE_ALIAS("xen-backend:pci");