pci_stub.c 34 KB

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