pci_stub.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371
  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. #define DRV_NAME "pciback"
  24. static char *pci_devs_to_hide;
  25. wait_queue_head_t aer_wait_queue;
  26. /*Add sem for sync AER handling and pciback remove/reconfigue ops,
  27. * We want to avoid in middle of AER ops, pciback devices is being removed
  28. */
  29. static DECLARE_RWSEM(pcistub_sem);
  30. module_param_named(hide, pci_devs_to_hide, charp, 0444);
  31. struct pcistub_device_id {
  32. struct list_head slot_list;
  33. int domain;
  34. unsigned char bus;
  35. unsigned int devfn;
  36. };
  37. static LIST_HEAD(pcistub_device_ids);
  38. static DEFINE_SPINLOCK(device_ids_lock);
  39. struct pcistub_device {
  40. struct kref kref;
  41. struct list_head dev_list;
  42. spinlock_t lock;
  43. struct pci_dev *dev;
  44. struct pciback_device *pdev;/* non-NULL if struct pci_dev is in use */
  45. };
  46. /* Access to pcistub_devices & seized_devices lists and the initialize_devices
  47. * flag must be locked with pcistub_devices_lock
  48. */
  49. static DEFINE_SPINLOCK(pcistub_devices_lock);
  50. static LIST_HEAD(pcistub_devices);
  51. /* wait for device_initcall before initializing our devices
  52. * (see pcistub_init_devices_late)
  53. */
  54. static int initialize_devices;
  55. static LIST_HEAD(seized_devices);
  56. static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
  57. {
  58. struct pcistub_device *psdev;
  59. dev_dbg(&dev->dev, "pcistub_device_alloc\n");
  60. psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
  61. if (!psdev)
  62. return NULL;
  63. psdev->dev = pci_dev_get(dev);
  64. if (!psdev->dev) {
  65. kfree(psdev);
  66. return NULL;
  67. }
  68. kref_init(&psdev->kref);
  69. spin_lock_init(&psdev->lock);
  70. return psdev;
  71. }
  72. /* Don't call this directly as it's called by pcistub_device_put */
  73. static void pcistub_device_release(struct kref *kref)
  74. {
  75. struct pcistub_device *psdev;
  76. psdev = container_of(kref, struct pcistub_device, kref);
  77. dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
  78. xen_unregister_device_domain_owner(psdev->dev);
  79. /* Clean-up the device */
  80. pciback_reset_device(psdev->dev);
  81. pciback_config_free_dyn_fields(psdev->dev);
  82. pciback_config_free_dev(psdev->dev);
  83. kfree(pci_get_drvdata(psdev->dev));
  84. pci_set_drvdata(psdev->dev, NULL);
  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 pciback_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 pciback_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 pciback_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. /*hold this lock for avoiding breaking link between
  182. * pcistub and pciback when AER is in processing
  183. */
  184. down_write(&pcistub_sem);
  185. /* Cleanup our device
  186. * (so it's ready for the next domain)
  187. */
  188. pciback_reset_device(found_psdev->dev);
  189. pciback_config_free_dyn_fields(found_psdev->dev);
  190. pciback_config_reset_dev(found_psdev->dev);
  191. spin_lock_irqsave(&found_psdev->lock, flags);
  192. found_psdev->pdev = NULL;
  193. spin_unlock_irqrestore(&found_psdev->lock, flags);
  194. pcistub_device_put(found_psdev);
  195. up_write(&pcistub_sem);
  196. }
  197. static int __devinit pcistub_match_one(struct pci_dev *dev,
  198. struct pcistub_device_id *pdev_id)
  199. {
  200. /* Match the specified device by domain, bus, slot, func and also if
  201. * any of the device's parent bridges match.
  202. */
  203. for (; dev != NULL; dev = dev->bus->self) {
  204. if (pci_domain_nr(dev->bus) == pdev_id->domain
  205. && dev->bus->number == pdev_id->bus
  206. && dev->devfn == pdev_id->devfn)
  207. return 1;
  208. /* Sometimes topmost bridge links to itself. */
  209. if (dev == dev->bus->self)
  210. break;
  211. }
  212. return 0;
  213. }
  214. static int __devinit pcistub_match(struct pci_dev *dev)
  215. {
  216. struct pcistub_device_id *pdev_id;
  217. unsigned long flags;
  218. int found = 0;
  219. spin_lock_irqsave(&device_ids_lock, flags);
  220. list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
  221. if (pcistub_match_one(dev, pdev_id)) {
  222. found = 1;
  223. break;
  224. }
  225. }
  226. spin_unlock_irqrestore(&device_ids_lock, flags);
  227. return found;
  228. }
  229. static int __devinit pcistub_init_device(struct pci_dev *dev)
  230. {
  231. struct pciback_dev_data *dev_data;
  232. int err = 0;
  233. dev_dbg(&dev->dev, "initializing...\n");
  234. /* The PCI backend is not intended to be a module (or to work with
  235. * removable PCI devices (yet). If it were, pciback_config_free()
  236. * would need to be called somewhere to free the memory allocated
  237. * here and then to call kfree(pci_get_drvdata(psdev->dev)).
  238. */
  239. dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
  240. + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
  241. if (!dev_data) {
  242. err = -ENOMEM;
  243. goto out;
  244. }
  245. pci_set_drvdata(dev, dev_data);
  246. /*
  247. * Setup name for fake IRQ handler. It will only be enabled
  248. * once the device is turned on by the guest.
  249. */
  250. sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
  251. dev_dbg(&dev->dev, "initializing config\n");
  252. init_waitqueue_head(&aer_wait_queue);
  253. err = pciback_config_init_dev(dev);
  254. if (err)
  255. goto out;
  256. /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
  257. * must do this here because pcibios_enable_device may specify
  258. * the pci device's true irq (and possibly its other resources)
  259. * if they differ from what's in the configuration space.
  260. * This makes the assumption that the device's resources won't
  261. * change after this point (otherwise this code may break!)
  262. */
  263. dev_dbg(&dev->dev, "enabling device\n");
  264. err = pci_enable_device(dev);
  265. if (err)
  266. goto config_release;
  267. /* Now disable the device (this also ensures some private device
  268. * data is setup before we export)
  269. */
  270. dev_dbg(&dev->dev, "reset device\n");
  271. pciback_reset_device(dev);
  272. return 0;
  273. config_release:
  274. pciback_config_free_dev(dev);
  275. out:
  276. pci_set_drvdata(dev, NULL);
  277. kfree(dev_data);
  278. return err;
  279. }
  280. /*
  281. * Because some initialization still happens on
  282. * devices during fs_initcall, we need to defer
  283. * full initialization of our devices until
  284. * device_initcall.
  285. */
  286. static int __init pcistub_init_devices_late(void)
  287. {
  288. struct pcistub_device *psdev;
  289. unsigned long flags;
  290. int err = 0;
  291. pr_debug("pciback: pcistub_init_devices_late\n");
  292. spin_lock_irqsave(&pcistub_devices_lock, flags);
  293. while (!list_empty(&seized_devices)) {
  294. psdev = container_of(seized_devices.next,
  295. struct pcistub_device, dev_list);
  296. list_del(&psdev->dev_list);
  297. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  298. err = pcistub_init_device(psdev->dev);
  299. if (err) {
  300. dev_err(&psdev->dev->dev,
  301. "error %d initializing device\n", err);
  302. kfree(psdev);
  303. psdev = NULL;
  304. }
  305. spin_lock_irqsave(&pcistub_devices_lock, flags);
  306. if (psdev)
  307. list_add_tail(&psdev->dev_list, &pcistub_devices);
  308. }
  309. initialize_devices = 1;
  310. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  311. return 0;
  312. }
  313. static int __devinit pcistub_seize(struct pci_dev *dev)
  314. {
  315. struct pcistub_device *psdev;
  316. unsigned long flags;
  317. int err = 0;
  318. psdev = pcistub_device_alloc(dev);
  319. if (!psdev)
  320. return -ENOMEM;
  321. spin_lock_irqsave(&pcistub_devices_lock, flags);
  322. if (initialize_devices) {
  323. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  324. /* don't want irqs disabled when calling pcistub_init_device */
  325. err = pcistub_init_device(psdev->dev);
  326. spin_lock_irqsave(&pcistub_devices_lock, flags);
  327. if (!err)
  328. list_add(&psdev->dev_list, &pcistub_devices);
  329. } else {
  330. dev_dbg(&dev->dev, "deferring initialization\n");
  331. list_add(&psdev->dev_list, &seized_devices);
  332. }
  333. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  334. if (err)
  335. pcistub_device_put(psdev);
  336. return err;
  337. }
  338. static int __devinit pcistub_probe(struct pci_dev *dev,
  339. const struct pci_device_id *id)
  340. {
  341. int err = 0;
  342. dev_dbg(&dev->dev, "probing...\n");
  343. if (pcistub_match(dev)) {
  344. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
  345. && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  346. dev_err(&dev->dev, "can't export pci devices that "
  347. "don't have a normal (0) or bridge (1) "
  348. "header type!\n");
  349. err = -ENODEV;
  350. goto out;
  351. }
  352. dev_info(&dev->dev, "seizing device\n");
  353. err = pcistub_seize(dev);
  354. } else
  355. /* Didn't find the device */
  356. err = -ENODEV;
  357. out:
  358. return err;
  359. }
  360. static void pcistub_remove(struct pci_dev *dev)
  361. {
  362. struct pcistub_device *psdev, *found_psdev = NULL;
  363. unsigned long flags;
  364. dev_dbg(&dev->dev, "removing\n");
  365. spin_lock_irqsave(&pcistub_devices_lock, flags);
  366. pciback_config_quirk_release(dev);
  367. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  368. if (psdev->dev == dev) {
  369. found_psdev = psdev;
  370. break;
  371. }
  372. }
  373. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  374. if (found_psdev) {
  375. dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
  376. found_psdev->pdev);
  377. if (found_psdev->pdev) {
  378. printk(KERN_WARNING "pciback: ****** removing device "
  379. "%s while still in-use! ******\n",
  380. pci_name(found_psdev->dev));
  381. printk(KERN_WARNING "pciback: ****** driver domain may "
  382. "still access this device's i/o resources!\n");
  383. printk(KERN_WARNING "pciback: ****** shutdown driver "
  384. "domain before binding device\n");
  385. printk(KERN_WARNING "pciback: ****** to other drivers "
  386. "or domains\n");
  387. pciback_release_pci_dev(found_psdev->pdev,
  388. found_psdev->dev);
  389. }
  390. spin_lock_irqsave(&pcistub_devices_lock, flags);
  391. list_del(&found_psdev->dev_list);
  392. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  393. /* the final put for releasing from the list */
  394. pcistub_device_put(found_psdev);
  395. }
  396. }
  397. static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
  398. {
  399. .vendor = PCI_ANY_ID,
  400. .device = PCI_ANY_ID,
  401. .subvendor = PCI_ANY_ID,
  402. .subdevice = PCI_ANY_ID,
  403. },
  404. {0,},
  405. };
  406. #define PCI_NODENAME_MAX 40
  407. static void kill_domain_by_device(struct pcistub_device *psdev)
  408. {
  409. struct xenbus_transaction xbt;
  410. int err;
  411. char nodename[PCI_NODENAME_MAX];
  412. if (!psdev)
  413. dev_err(&psdev->dev->dev,
  414. "device is NULL when do AER recovery/kill_domain\n");
  415. snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
  416. psdev->pdev->xdev->otherend_id);
  417. nodename[strlen(nodename)] = '\0';
  418. again:
  419. err = xenbus_transaction_start(&xbt);
  420. if (err) {
  421. dev_err(&psdev->dev->dev,
  422. "error %d when start xenbus transaction\n", err);
  423. return;
  424. }
  425. /*PV AER handlers will set this flag*/
  426. xenbus_printf(xbt, nodename, "aerState" , "aerfail");
  427. err = xenbus_transaction_end(xbt, 0);
  428. if (err) {
  429. if (err == -EAGAIN)
  430. goto again;
  431. dev_err(&psdev->dev->dev,
  432. "error %d when end xenbus transaction\n", err);
  433. return;
  434. }
  435. }
  436. /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
  437. * backend need to have cooperation. In pciback, those steps will do similar
  438. * jobs: send service request and waiting for front_end response.
  439. */
  440. static pci_ers_result_t common_process(struct pcistub_device *psdev,
  441. pci_channel_state_t state, int aer_cmd, pci_ers_result_t result)
  442. {
  443. pci_ers_result_t res = result;
  444. struct xen_pcie_aer_op *aer_op;
  445. int ret;
  446. /*with PV AER drivers*/
  447. aer_op = &(psdev->pdev->sh_info->aer_op);
  448. aer_op->cmd = aer_cmd ;
  449. /*useful for error_detected callback*/
  450. aer_op->err = state;
  451. /*pcifront_end BDF*/
  452. ret = pciback_get_pcifront_dev(psdev->dev, psdev->pdev,
  453. &aer_op->domain, &aer_op->bus, &aer_op->devfn);
  454. if (!ret) {
  455. dev_err(&psdev->dev->dev,
  456. "pciback: failed to get pcifront device\n");
  457. return PCI_ERS_RESULT_NONE;
  458. }
  459. wmb();
  460. dev_dbg(&psdev->dev->dev,
  461. "pciback: aer_op %x dom %x bus %x devfn %x\n",
  462. aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
  463. /*local flag to mark there's aer request, pciback callback will use this
  464. * flag to judge whether we need to check pci-front give aer service
  465. * ack signal
  466. */
  467. set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  468. /*It is possible that a pcifront conf_read_write ops request invokes
  469. * the callback which cause the spurious execution of wake_up.
  470. * Yet it is harmless and better than a spinlock here
  471. */
  472. set_bit(_XEN_PCIB_active,
  473. (unsigned long *)&psdev->pdev->sh_info->flags);
  474. wmb();
  475. notify_remote_via_irq(psdev->pdev->evtchn_irq);
  476. ret = wait_event_timeout(aer_wait_queue, !(test_bit(_XEN_PCIB_active,
  477. (unsigned long *)&psdev->pdev->sh_info->flags)), 300*HZ);
  478. if (!ret) {
  479. if (test_bit(_XEN_PCIB_active,
  480. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  481. dev_err(&psdev->dev->dev,
  482. "pcifront aer process not responding!\n");
  483. clear_bit(_XEN_PCIB_active,
  484. (unsigned long *)&psdev->pdev->sh_info->flags);
  485. aer_op->err = PCI_ERS_RESULT_NONE;
  486. return res;
  487. }
  488. }
  489. clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  490. if (test_bit(_XEN_PCIF_active,
  491. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  492. dev_dbg(&psdev->dev->dev,
  493. "schedule pci_conf service in pciback\n");
  494. test_and_schedule_op(psdev->pdev);
  495. }
  496. res = (pci_ers_result_t)aer_op->err;
  497. return res;
  498. }
  499. /*
  500. * pciback_slot_reset: it will send the slot_reset request to pcifront in case
  501. * of the device driver could provide this service, and then wait for pcifront
  502. * ack.
  503. * @dev: pointer to PCI devices
  504. * return value is used by aer_core do_recovery policy
  505. */
  506. static pci_ers_result_t pciback_slot_reset(struct pci_dev *dev)
  507. {
  508. struct pcistub_device *psdev;
  509. pci_ers_result_t result;
  510. result = PCI_ERS_RESULT_RECOVERED;
  511. dev_dbg(&dev->dev, "pciback_slot_reset(bus:%x,devfn:%x)\n",
  512. dev->bus->number, dev->devfn);
  513. down_write(&pcistub_sem);
  514. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  515. dev->bus->number,
  516. PCI_SLOT(dev->devfn),
  517. PCI_FUNC(dev->devfn));
  518. if (!psdev || !psdev->pdev) {
  519. dev_err(&dev->dev,
  520. "pciback device is not found/assigned\n");
  521. goto end;
  522. }
  523. if (!psdev->pdev->sh_info) {
  524. dev_err(&dev->dev, "pciback device is not connected or owned"
  525. " by HVM, kill it\n");
  526. kill_domain_by_device(psdev);
  527. goto release;
  528. }
  529. if (!test_bit(_XEN_PCIB_AERHANDLER,
  530. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  531. dev_err(&dev->dev,
  532. "guest with no AER driver should have been killed\n");
  533. goto release;
  534. }
  535. result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
  536. if (result == PCI_ERS_RESULT_NONE ||
  537. result == PCI_ERS_RESULT_DISCONNECT) {
  538. dev_dbg(&dev->dev,
  539. "No AER slot_reset service or disconnected!\n");
  540. kill_domain_by_device(psdev);
  541. }
  542. release:
  543. pcistub_device_put(psdev);
  544. end:
  545. up_write(&pcistub_sem);
  546. return result;
  547. }
  548. /*pciback_mmio_enabled: it will send the mmio_enabled request to pcifront
  549. * in case of the device driver could provide this service, and then wait
  550. * for pcifront ack
  551. * @dev: pointer to PCI devices
  552. * return value is used by aer_core do_recovery policy
  553. */
  554. static pci_ers_result_t pciback_mmio_enabled(struct pci_dev *dev)
  555. {
  556. struct pcistub_device *psdev;
  557. pci_ers_result_t result;
  558. result = PCI_ERS_RESULT_RECOVERED;
  559. dev_dbg(&dev->dev, "pciback_mmio_enabled(bus:%x,devfn:%x)\n",
  560. dev->bus->number, dev->devfn);
  561. down_write(&pcistub_sem);
  562. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  563. dev->bus->number,
  564. PCI_SLOT(dev->devfn),
  565. PCI_FUNC(dev->devfn));
  566. if (!psdev || !psdev->pdev) {
  567. dev_err(&dev->dev,
  568. "pciback device is not found/assigned\n");
  569. goto end;
  570. }
  571. if (!psdev->pdev->sh_info) {
  572. dev_err(&dev->dev, "pciback device is not connected or owned"
  573. " by HVM, kill it\n");
  574. kill_domain_by_device(psdev);
  575. goto release;
  576. }
  577. if (!test_bit(_XEN_PCIB_AERHANDLER,
  578. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  579. dev_err(&dev->dev,
  580. "guest with no AER driver should have been killed\n");
  581. goto release;
  582. }
  583. result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
  584. if (result == PCI_ERS_RESULT_NONE ||
  585. result == PCI_ERS_RESULT_DISCONNECT) {
  586. dev_dbg(&dev->dev,
  587. "No AER mmio_enabled service or disconnected!\n");
  588. kill_domain_by_device(psdev);
  589. }
  590. release:
  591. pcistub_device_put(psdev);
  592. end:
  593. up_write(&pcistub_sem);
  594. return result;
  595. }
  596. /*pciback_error_detected: it will send the error_detected request to pcifront
  597. * in case of the device driver could provide this service, and then wait
  598. * for pcifront ack.
  599. * @dev: pointer to PCI devices
  600. * @error: the current PCI connection state
  601. * return value is used by aer_core do_recovery policy
  602. */
  603. static pci_ers_result_t pciback_error_detected(struct pci_dev *dev,
  604. pci_channel_state_t error)
  605. {
  606. struct pcistub_device *psdev;
  607. pci_ers_result_t result;
  608. result = PCI_ERS_RESULT_CAN_RECOVER;
  609. dev_dbg(&dev->dev, "pciback_error_detected(bus:%x,devfn:%x)\n",
  610. dev->bus->number, dev->devfn);
  611. down_write(&pcistub_sem);
  612. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  613. dev->bus->number,
  614. PCI_SLOT(dev->devfn),
  615. PCI_FUNC(dev->devfn));
  616. if (!psdev || !psdev->pdev) {
  617. dev_err(&dev->dev,
  618. "pciback device is not found/assigned\n");
  619. goto end;
  620. }
  621. if (!psdev->pdev->sh_info) {
  622. dev_err(&dev->dev, "pciback device is not connected or owned"
  623. " by HVM, kill it\n");
  624. kill_domain_by_device(psdev);
  625. goto release;
  626. }
  627. /*Guest owns the device yet no aer handler regiested, kill guest*/
  628. if (!test_bit(_XEN_PCIB_AERHANDLER,
  629. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  630. dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
  631. kill_domain_by_device(psdev);
  632. goto release;
  633. }
  634. result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
  635. if (result == PCI_ERS_RESULT_NONE ||
  636. result == PCI_ERS_RESULT_DISCONNECT) {
  637. dev_dbg(&dev->dev,
  638. "No AER error_detected service or disconnected!\n");
  639. kill_domain_by_device(psdev);
  640. }
  641. release:
  642. pcistub_device_put(psdev);
  643. end:
  644. up_write(&pcistub_sem);
  645. return result;
  646. }
  647. /*pciback_error_resume: it will send the error_resume request to pcifront
  648. * in case of the device driver could provide this service, and then wait
  649. * for pcifront ack.
  650. * @dev: pointer to PCI devices
  651. */
  652. static void pciback_error_resume(struct pci_dev *dev)
  653. {
  654. struct pcistub_device *psdev;
  655. dev_dbg(&dev->dev, "pciback_error_resume(bus:%x,devfn:%x)\n",
  656. dev->bus->number, dev->devfn);
  657. down_write(&pcistub_sem);
  658. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  659. dev->bus->number,
  660. PCI_SLOT(dev->devfn),
  661. PCI_FUNC(dev->devfn));
  662. if (!psdev || !psdev->pdev) {
  663. dev_err(&dev->dev,
  664. "pciback device is not found/assigned\n");
  665. goto end;
  666. }
  667. if (!psdev->pdev->sh_info) {
  668. dev_err(&dev->dev, "pciback device is not connected or owned"
  669. " by HVM, kill it\n");
  670. kill_domain_by_device(psdev);
  671. goto release;
  672. }
  673. if (!test_bit(_XEN_PCIB_AERHANDLER,
  674. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  675. dev_err(&dev->dev,
  676. "guest with no AER driver should have been killed\n");
  677. kill_domain_by_device(psdev);
  678. goto release;
  679. }
  680. common_process(psdev, 1, XEN_PCI_OP_aer_resume,
  681. PCI_ERS_RESULT_RECOVERED);
  682. release:
  683. pcistub_device_put(psdev);
  684. end:
  685. up_write(&pcistub_sem);
  686. return;
  687. }
  688. /*add pciback AER handling*/
  689. static struct pci_error_handlers pciback_error_handler = {
  690. .error_detected = pciback_error_detected,
  691. .mmio_enabled = pciback_mmio_enabled,
  692. .slot_reset = pciback_slot_reset,
  693. .resume = pciback_error_resume,
  694. };
  695. /*
  696. * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
  697. * for a normal device. I don't want it to be loaded automatically.
  698. */
  699. static struct pci_driver pciback_pci_driver = {
  700. .name = DRV_NAME,
  701. .id_table = pcistub_ids,
  702. .probe = pcistub_probe,
  703. .remove = pcistub_remove,
  704. .err_handler = &pciback_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("pciback: 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("pciback: 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 = pciback_config_field_free;
  799. err = pciback_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. 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. 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. 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 pciback_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. 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 pciback_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. DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL, pcistub_irq_handler_switch);
  910. static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
  911. size_t count)
  912. {
  913. int domain, bus, slot, func, reg, size, mask;
  914. int err;
  915. err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
  916. &mask);
  917. if (err)
  918. goto out;
  919. err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
  920. out:
  921. if (!err)
  922. err = count;
  923. return err;
  924. }
  925. static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
  926. {
  927. int count = 0;
  928. unsigned long flags;
  929. struct pciback_config_quirk *quirk;
  930. struct pciback_dev_data *dev_data;
  931. const struct config_field *field;
  932. const struct config_field_entry *cfg_entry;
  933. spin_lock_irqsave(&device_ids_lock, flags);
  934. list_for_each_entry(quirk, &pciback_quirks, quirks_list) {
  935. if (count >= PAGE_SIZE)
  936. goto out;
  937. count += scnprintf(buf + count, PAGE_SIZE - count,
  938. "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
  939. quirk->pdev->bus->number,
  940. PCI_SLOT(quirk->pdev->devfn),
  941. PCI_FUNC(quirk->pdev->devfn),
  942. quirk->devid.vendor, quirk->devid.device,
  943. quirk->devid.subvendor,
  944. quirk->devid.subdevice);
  945. dev_data = pci_get_drvdata(quirk->pdev);
  946. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  947. field = cfg_entry->field;
  948. if (count >= PAGE_SIZE)
  949. goto out;
  950. count += scnprintf(buf + count, PAGE_SIZE - count,
  951. "\t\t%08x:%01x:%08x\n",
  952. cfg_entry->base_offset +
  953. field->offset, field->size,
  954. field->mask);
  955. }
  956. }
  957. out:
  958. spin_unlock_irqrestore(&device_ids_lock, flags);
  959. return count;
  960. }
  961. DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, pcistub_quirk_add);
  962. static ssize_t permissive_add(struct device_driver *drv, const char *buf,
  963. size_t count)
  964. {
  965. int domain, bus, slot, func;
  966. int err;
  967. struct pcistub_device *psdev;
  968. struct pciback_dev_data *dev_data;
  969. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  970. if (err)
  971. goto out;
  972. psdev = pcistub_device_find(domain, bus, slot, func);
  973. if (!psdev) {
  974. err = -ENODEV;
  975. goto out;
  976. }
  977. if (!psdev->dev) {
  978. err = -ENODEV;
  979. goto release;
  980. }
  981. dev_data = pci_get_drvdata(psdev->dev);
  982. /* the driver data for a device should never be null at this point */
  983. if (!dev_data) {
  984. err = -ENXIO;
  985. goto release;
  986. }
  987. if (!dev_data->permissive) {
  988. dev_data->permissive = 1;
  989. /* Let user know that what they're doing could be unsafe */
  990. dev_warn(&psdev->dev->dev, "enabling permissive mode "
  991. "configuration space accesses!\n");
  992. dev_warn(&psdev->dev->dev,
  993. "permissive mode is potentially unsafe!\n");
  994. }
  995. release:
  996. pcistub_device_put(psdev);
  997. out:
  998. if (!err)
  999. err = count;
  1000. return err;
  1001. }
  1002. static ssize_t permissive_show(struct device_driver *drv, char *buf)
  1003. {
  1004. struct pcistub_device *psdev;
  1005. struct pciback_dev_data *dev_data;
  1006. size_t count = 0;
  1007. unsigned long flags;
  1008. spin_lock_irqsave(&pcistub_devices_lock, flags);
  1009. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  1010. if (count >= PAGE_SIZE)
  1011. break;
  1012. if (!psdev->dev)
  1013. continue;
  1014. dev_data = pci_get_drvdata(psdev->dev);
  1015. if (!dev_data || !dev_data->permissive)
  1016. continue;
  1017. count +=
  1018. scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
  1019. pci_name(psdev->dev));
  1020. }
  1021. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  1022. return count;
  1023. }
  1024. DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, permissive_add);
  1025. static void pcistub_exit(void)
  1026. {
  1027. driver_remove_file(&pciback_pci_driver.driver, &driver_attr_new_slot);
  1028. driver_remove_file(&pciback_pci_driver.driver,
  1029. &driver_attr_remove_slot);
  1030. driver_remove_file(&pciback_pci_driver.driver, &driver_attr_slots);
  1031. driver_remove_file(&pciback_pci_driver.driver, &driver_attr_quirks);
  1032. driver_remove_file(&pciback_pci_driver.driver, &driver_attr_permissive);
  1033. driver_remove_file(&pciback_pci_driver.driver,
  1034. &driver_attr_irq_handlers);
  1035. driver_remove_file(&pciback_pci_driver.driver,
  1036. &driver_attr_irq_handler_state);
  1037. pci_unregister_driver(&pciback_pci_driver);
  1038. }
  1039. static int __init pcistub_init(void)
  1040. {
  1041. int pos = 0;
  1042. int err = 0;
  1043. int domain, bus, slot, func;
  1044. int parsed;
  1045. if (pci_devs_to_hide && *pci_devs_to_hide) {
  1046. do {
  1047. parsed = 0;
  1048. err = sscanf(pci_devs_to_hide + pos,
  1049. " (%x:%x:%x.%x) %n",
  1050. &domain, &bus, &slot, &func, &parsed);
  1051. if (err != 4) {
  1052. domain = 0;
  1053. err = sscanf(pci_devs_to_hide + pos,
  1054. " (%x:%x.%x) %n",
  1055. &bus, &slot, &func, &parsed);
  1056. if (err != 3)
  1057. goto parse_error;
  1058. }
  1059. err = pcistub_device_id_add(domain, bus, slot, func);
  1060. if (err)
  1061. goto out;
  1062. /* if parsed<=0, we've reached the end of the string */
  1063. pos += parsed;
  1064. } while (parsed > 0 && pci_devs_to_hide[pos]);
  1065. }
  1066. /* If we're the first PCI Device Driver to register, we're the
  1067. * first one to get offered PCI devices as they become
  1068. * available (and thus we can be the first to grab them)
  1069. */
  1070. err = pci_register_driver(&pciback_pci_driver);
  1071. if (err < 0)
  1072. goto out;
  1073. err = driver_create_file(&pciback_pci_driver.driver,
  1074. &driver_attr_new_slot);
  1075. if (!err)
  1076. err = driver_create_file(&pciback_pci_driver.driver,
  1077. &driver_attr_remove_slot);
  1078. if (!err)
  1079. err = driver_create_file(&pciback_pci_driver.driver,
  1080. &driver_attr_slots);
  1081. if (!err)
  1082. err = driver_create_file(&pciback_pci_driver.driver,
  1083. &driver_attr_quirks);
  1084. if (!err)
  1085. err = driver_create_file(&pciback_pci_driver.driver,
  1086. &driver_attr_permissive);
  1087. if (!err)
  1088. err = driver_create_file(&pciback_pci_driver.driver,
  1089. &driver_attr_irq_handlers);
  1090. if (!err)
  1091. err = driver_create_file(&pciback_pci_driver.driver,
  1092. &driver_attr_irq_handler_state);
  1093. if (err)
  1094. pcistub_exit();
  1095. out:
  1096. return err;
  1097. parse_error:
  1098. printk(KERN_ERR "pciback: Error parsing pci_devs_to_hide at \"%s\"\n",
  1099. pci_devs_to_hide + pos);
  1100. return -EINVAL;
  1101. }
  1102. #ifndef MODULE
  1103. /*
  1104. * fs_initcall happens before device_initcall
  1105. * so pciback *should* get called first (b/c we
  1106. * want to suck up any device before other drivers
  1107. * get a chance by being the first pci device
  1108. * driver to register)
  1109. */
  1110. fs_initcall(pcistub_init);
  1111. #endif
  1112. static int __init pciback_init(void)
  1113. {
  1114. int err;
  1115. if (!xen_initial_domain())
  1116. return -ENODEV;
  1117. err = pciback_config_init();
  1118. if (err)
  1119. return err;
  1120. #ifdef MODULE
  1121. err = pcistub_init();
  1122. if (err < 0)
  1123. return err;
  1124. #endif
  1125. pcistub_init_devices_late();
  1126. err = pciback_xenbus_register();
  1127. if (err)
  1128. pcistub_exit();
  1129. return err;
  1130. }
  1131. static void __exit pciback_cleanup(void)
  1132. {
  1133. pciback_xenbus_unregister();
  1134. pcistub_exit();
  1135. }
  1136. module_init(pciback_init);
  1137. module_exit(pciback_cleanup);
  1138. MODULE_LICENSE("Dual BSD/GPL");