pci_stub.c 34 KB

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