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. 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. if (WARN_ON(!found_psdev))
  181. return;
  182. /*hold this lock for avoiding breaking link between
  183. * pcistub and xen_pcibk when AER is in processing
  184. */
  185. down_write(&pcistub_sem);
  186. /* Cleanup our device
  187. * (so it's ready for the next domain)
  188. */
  189. xen_pcibk_reset_device(found_psdev->dev);
  190. xen_pcibk_config_free_dyn_fields(found_psdev->dev);
  191. xen_pcibk_config_reset_dev(found_psdev->dev);
  192. spin_lock_irqsave(&found_psdev->lock, flags);
  193. found_psdev->pdev = NULL;
  194. spin_unlock_irqrestore(&found_psdev->lock, flags);
  195. pcistub_device_put(found_psdev);
  196. up_write(&pcistub_sem);
  197. }
  198. static int __devinit pcistub_match_one(struct pci_dev *dev,
  199. struct pcistub_device_id *pdev_id)
  200. {
  201. /* Match the specified device by domain, bus, slot, func and also if
  202. * any of the device's parent bridges match.
  203. */
  204. for (; dev != NULL; dev = dev->bus->self) {
  205. if (pci_domain_nr(dev->bus) == pdev_id->domain
  206. && dev->bus->number == pdev_id->bus
  207. && dev->devfn == pdev_id->devfn)
  208. return 1;
  209. /* Sometimes topmost bridge links to itself. */
  210. if (dev == dev->bus->self)
  211. break;
  212. }
  213. return 0;
  214. }
  215. static int __devinit pcistub_match(struct pci_dev *dev)
  216. {
  217. struct pcistub_device_id *pdev_id;
  218. unsigned long flags;
  219. int found = 0;
  220. spin_lock_irqsave(&device_ids_lock, flags);
  221. list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
  222. if (pcistub_match_one(dev, pdev_id)) {
  223. found = 1;
  224. break;
  225. }
  226. }
  227. spin_unlock_irqrestore(&device_ids_lock, flags);
  228. return found;
  229. }
  230. static int __devinit pcistub_init_device(struct pci_dev *dev)
  231. {
  232. struct xen_pcibk_dev_data *dev_data;
  233. int err = 0;
  234. dev_dbg(&dev->dev, "initializing...\n");
  235. /* The PCI backend is not intended to be a module (or to work with
  236. * removable PCI devices (yet). If it were, xen_pcibk_config_free()
  237. * would need to be called somewhere to free the memory allocated
  238. * here and then to call kfree(pci_get_drvdata(psdev->dev)).
  239. */
  240. dev_data = kzalloc(sizeof(*dev_data) + strlen(DRV_NAME "[]")
  241. + strlen(pci_name(dev)) + 1, GFP_ATOMIC);
  242. if (!dev_data) {
  243. err = -ENOMEM;
  244. goto out;
  245. }
  246. pci_set_drvdata(dev, dev_data);
  247. /*
  248. * Setup name for fake IRQ handler. It will only be enabled
  249. * once the device is turned on by the guest.
  250. */
  251. sprintf(dev_data->irq_name, DRV_NAME "[%s]", pci_name(dev));
  252. dev_dbg(&dev->dev, "initializing config\n");
  253. init_waitqueue_head(&xen_pcibk_aer_wait_queue);
  254. err = xen_pcibk_config_init_dev(dev);
  255. if (err)
  256. goto out;
  257. /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
  258. * must do this here because pcibios_enable_device may specify
  259. * the pci device's true irq (and possibly its other resources)
  260. * if they differ from what's in the configuration space.
  261. * This makes the assumption that the device's resources won't
  262. * change after this point (otherwise this code may break!)
  263. */
  264. dev_dbg(&dev->dev, "enabling device\n");
  265. err = pci_enable_device(dev);
  266. if (err)
  267. goto config_release;
  268. /* Now disable the device (this also ensures some private device
  269. * data is setup before we export)
  270. */
  271. dev_dbg(&dev->dev, "reset device\n");
  272. xen_pcibk_reset_device(dev);
  273. return 0;
  274. config_release:
  275. xen_pcibk_config_free_dev(dev);
  276. out:
  277. pci_set_drvdata(dev, NULL);
  278. kfree(dev_data);
  279. return err;
  280. }
  281. /*
  282. * Because some initialization still happens on
  283. * devices during fs_initcall, we need to defer
  284. * full initialization of our devices until
  285. * device_initcall.
  286. */
  287. static int __init pcistub_init_devices_late(void)
  288. {
  289. struct pcistub_device *psdev;
  290. unsigned long flags;
  291. int err = 0;
  292. pr_debug(DRV_NAME ": pcistub_init_devices_late\n");
  293. spin_lock_irqsave(&pcistub_devices_lock, flags);
  294. while (!list_empty(&seized_devices)) {
  295. psdev = container_of(seized_devices.next,
  296. struct pcistub_device, dev_list);
  297. list_del(&psdev->dev_list);
  298. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  299. err = pcistub_init_device(psdev->dev);
  300. if (err) {
  301. dev_err(&psdev->dev->dev,
  302. "error %d initializing device\n", err);
  303. kfree(psdev);
  304. psdev = NULL;
  305. }
  306. spin_lock_irqsave(&pcistub_devices_lock, flags);
  307. if (psdev)
  308. list_add_tail(&psdev->dev_list, &pcistub_devices);
  309. }
  310. initialize_devices = 1;
  311. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  312. return 0;
  313. }
  314. static int __devinit pcistub_seize(struct pci_dev *dev)
  315. {
  316. struct pcistub_device *psdev;
  317. unsigned long flags;
  318. int err = 0;
  319. psdev = pcistub_device_alloc(dev);
  320. if (!psdev)
  321. return -ENOMEM;
  322. spin_lock_irqsave(&pcistub_devices_lock, flags);
  323. if (initialize_devices) {
  324. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  325. /* don't want irqs disabled when calling pcistub_init_device */
  326. err = pcistub_init_device(psdev->dev);
  327. spin_lock_irqsave(&pcistub_devices_lock, flags);
  328. if (!err)
  329. list_add(&psdev->dev_list, &pcistub_devices);
  330. } else {
  331. dev_dbg(&dev->dev, "deferring initialization\n");
  332. list_add(&psdev->dev_list, &seized_devices);
  333. }
  334. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  335. if (err)
  336. pcistub_device_put(psdev);
  337. return err;
  338. }
  339. static int __devinit pcistub_probe(struct pci_dev *dev,
  340. const struct pci_device_id *id)
  341. {
  342. int err = 0;
  343. dev_dbg(&dev->dev, "probing...\n");
  344. if (pcistub_match(dev)) {
  345. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
  346. && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  347. dev_err(&dev->dev, "can't export pci devices that "
  348. "don't have a normal (0) or bridge (1) "
  349. "header type!\n");
  350. err = -ENODEV;
  351. goto out;
  352. }
  353. dev_info(&dev->dev, "seizing device\n");
  354. err = pcistub_seize(dev);
  355. } else
  356. /* Didn't find the device */
  357. err = -ENODEV;
  358. out:
  359. return err;
  360. }
  361. static void pcistub_remove(struct pci_dev *dev)
  362. {
  363. struct pcistub_device *psdev, *found_psdev = NULL;
  364. unsigned long flags;
  365. dev_dbg(&dev->dev, "removing\n");
  366. spin_lock_irqsave(&pcistub_devices_lock, flags);
  367. xen_pcibk_config_quirk_release(dev);
  368. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  369. if (psdev->dev == dev) {
  370. found_psdev = psdev;
  371. break;
  372. }
  373. }
  374. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  375. if (found_psdev) {
  376. dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
  377. found_psdev->pdev);
  378. if (found_psdev->pdev) {
  379. printk(KERN_WARNING DRV_NAME ": ****** removing device "
  380. "%s while still in-use! ******\n",
  381. pci_name(found_psdev->dev));
  382. printk(KERN_WARNING DRV_NAME ": ****** driver domain may"
  383. " still access this device's i/o resources!\n");
  384. printk(KERN_WARNING DRV_NAME ": ****** shutdown driver "
  385. "domain before binding device\n");
  386. printk(KERN_WARNING DRV_NAME ": ****** to other drivers "
  387. "or domains\n");
  388. xen_pcibk_release_pci_dev(found_psdev->pdev,
  389. found_psdev->dev);
  390. }
  391. spin_lock_irqsave(&pcistub_devices_lock, flags);
  392. list_del(&found_psdev->dev_list);
  393. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  394. /* the final put for releasing from the list */
  395. pcistub_device_put(found_psdev);
  396. }
  397. }
  398. static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
  399. {
  400. .vendor = PCI_ANY_ID,
  401. .device = PCI_ANY_ID,
  402. .subvendor = PCI_ANY_ID,
  403. .subdevice = PCI_ANY_ID,
  404. },
  405. {0,},
  406. };
  407. #define PCI_NODENAME_MAX 40
  408. static void kill_domain_by_device(struct pcistub_device *psdev)
  409. {
  410. struct xenbus_transaction xbt;
  411. int err;
  412. char nodename[PCI_NODENAME_MAX];
  413. BUG_ON(!psdev);
  414. snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
  415. psdev->pdev->xdev->otherend_id);
  416. again:
  417. err = xenbus_transaction_start(&xbt);
  418. if (err) {
  419. dev_err(&psdev->dev->dev,
  420. "error %d when start xenbus transaction\n", err);
  421. return;
  422. }
  423. /*PV AER handlers will set this flag*/
  424. xenbus_printf(xbt, nodename, "aerState" , "aerfail");
  425. err = xenbus_transaction_end(xbt, 0);
  426. if (err) {
  427. if (err == -EAGAIN)
  428. goto again;
  429. dev_err(&psdev->dev->dev,
  430. "error %d when end xenbus transaction\n", err);
  431. return;
  432. }
  433. }
  434. /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
  435. * backend need to have cooperation. In xen_pcibk, those steps will do similar
  436. * jobs: send service request and waiting for front_end response.
  437. */
  438. static pci_ers_result_t common_process(struct pcistub_device *psdev,
  439. pci_channel_state_t state, int aer_cmd,
  440. pci_ers_result_t result)
  441. {
  442. pci_ers_result_t res = result;
  443. struct xen_pcie_aer_op *aer_op;
  444. int ret;
  445. /*with PV AER drivers*/
  446. aer_op = &(psdev->pdev->sh_info->aer_op);
  447. aer_op->cmd = aer_cmd ;
  448. /*useful for error_detected callback*/
  449. aer_op->err = state;
  450. /*pcifront_end BDF*/
  451. ret = xen_pcibk_get_pcifront_dev(psdev->dev, psdev->pdev,
  452. &aer_op->domain, &aer_op->bus, &aer_op->devfn);
  453. if (!ret) {
  454. dev_err(&psdev->dev->dev,
  455. DRV_NAME ": failed to get pcifront device\n");
  456. return PCI_ERS_RESULT_NONE;
  457. }
  458. wmb();
  459. dev_dbg(&psdev->dev->dev,
  460. DRV_NAME ": aer_op %x dom %x bus %x devfn %x\n",
  461. aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
  462. /*local flag to mark there's aer request, xen_pcibk callback will use
  463. * this flag to judge whether we need to check pci-front give aer
  464. * service ack signal
  465. */
  466. set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  467. /*It is possible that a pcifront conf_read_write ops request invokes
  468. * the callback which cause the spurious execution of wake_up.
  469. * Yet it is harmless and better than a spinlock here
  470. */
  471. set_bit(_XEN_PCIB_active,
  472. (unsigned long *)&psdev->pdev->sh_info->flags);
  473. wmb();
  474. notify_remote_via_irq(psdev->pdev->evtchn_irq);
  475. ret = wait_event_timeout(xen_pcibk_aer_wait_queue,
  476. !(test_bit(_XEN_PCIB_active, (unsigned long *)
  477. &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 " DRV_NAME "\n");
  494. xen_pcibk_test_and_schedule_op(psdev->pdev);
  495. }
  496. res = (pci_ers_result_t)aer_op->err;
  497. return res;
  498. }
  499. /*
  500. * xen_pcibk_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 xen_pcibk_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, "xen_pcibk_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. DRV_NAME " device is not found/assigned\n");
  521. goto end;
  522. }
  523. if (!psdev->pdev->sh_info) {
  524. dev_err(&dev->dev, DRV_NAME " 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. /*xen_pcibk_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 xen_pcibk_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, "xen_pcibk_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. DRV_NAME " device is not found/assigned\n");
  569. goto end;
  570. }
  571. if (!psdev->pdev->sh_info) {
  572. dev_err(&dev->dev, DRV_NAME " 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. /*xen_pcibk_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 xen_pcibk_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, "xen_pcibk_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. DRV_NAME " device is not found/assigned\n");
  619. goto end;
  620. }
  621. if (!psdev->pdev->sh_info) {
  622. dev_err(&dev->dev, DRV_NAME " 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. /*xen_pcibk_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 xen_pcibk_error_resume(struct pci_dev *dev)
  653. {
  654. struct pcistub_device *psdev;
  655. dev_dbg(&dev->dev, "xen_pcibk_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. DRV_NAME " device is not found/assigned\n");
  665. goto end;
  666. }
  667. if (!psdev->pdev->sh_info) {
  668. dev_err(&dev->dev, DRV_NAME " 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 xen_pcibk AER handling*/
  689. static struct pci_error_handlers xen_pcibk_error_handler = {
  690. .error_detected = xen_pcibk_error_detected,
  691. .mmio_enabled = xen_pcibk_mmio_enabled,
  692. .slot_reset = xen_pcibk_slot_reset,
  693. .resume = xen_pcibk_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 xen_pcibk_pci_driver = {
  700. /* The name should be xen_pciback, but until the tools are updated
  701. * we will keep it as pciback. */
  702. .name = "pciback",
  703. .id_table = pcistub_ids,
  704. .probe = pcistub_probe,
  705. .remove = pcistub_remove,
  706. .err_handler = &xen_pcibk_error_handler,
  707. };
  708. static inline int str_to_slot(const char *buf, int *domain, int *bus,
  709. int *slot, int *func)
  710. {
  711. int err;
  712. err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
  713. if (err == 4)
  714. return 0;
  715. else if (err < 0)
  716. return -EINVAL;
  717. /* try again without domain */
  718. *domain = 0;
  719. err = sscanf(buf, " %x:%x.%x", bus, slot, func);
  720. if (err == 3)
  721. return 0;
  722. return -EINVAL;
  723. }
  724. static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
  725. *slot, int *func, int *reg, int *size, int *mask)
  726. {
  727. int err;
  728. err =
  729. sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
  730. func, reg, size, mask);
  731. if (err == 7)
  732. return 0;
  733. return -EINVAL;
  734. }
  735. static int pcistub_device_id_add(int domain, int bus, int slot, int func)
  736. {
  737. struct pcistub_device_id *pci_dev_id;
  738. unsigned long flags;
  739. pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
  740. if (!pci_dev_id)
  741. return -ENOMEM;
  742. pci_dev_id->domain = domain;
  743. pci_dev_id->bus = bus;
  744. pci_dev_id->devfn = PCI_DEVFN(slot, func);
  745. pr_debug(DRV_NAME ": wants to seize %04x:%02x:%02x.%01x\n",
  746. domain, bus, slot, func);
  747. spin_lock_irqsave(&device_ids_lock, flags);
  748. list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
  749. spin_unlock_irqrestore(&device_ids_lock, flags);
  750. return 0;
  751. }
  752. static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
  753. {
  754. struct pcistub_device_id *pci_dev_id, *t;
  755. int devfn = PCI_DEVFN(slot, func);
  756. int err = -ENOENT;
  757. unsigned long flags;
  758. spin_lock_irqsave(&device_ids_lock, flags);
  759. list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
  760. slot_list) {
  761. if (pci_dev_id->domain == domain
  762. && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
  763. /* Don't break; here because it's possible the same
  764. * slot could be in the list more than once
  765. */
  766. list_del(&pci_dev_id->slot_list);
  767. kfree(pci_dev_id);
  768. err = 0;
  769. pr_debug(DRV_NAME ": removed %04x:%02x:%02x.%01x from "
  770. "seize list\n", domain, bus, slot, func);
  771. }
  772. }
  773. spin_unlock_irqrestore(&device_ids_lock, flags);
  774. return err;
  775. }
  776. static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
  777. int size, int mask)
  778. {
  779. int err = 0;
  780. struct pcistub_device *psdev;
  781. struct pci_dev *dev;
  782. struct config_field *field;
  783. psdev = pcistub_device_find(domain, bus, slot, func);
  784. if (!psdev || !psdev->dev) {
  785. err = -ENODEV;
  786. goto out;
  787. }
  788. dev = psdev->dev;
  789. field = kzalloc(sizeof(*field), GFP_ATOMIC);
  790. if (!field) {
  791. err = -ENOMEM;
  792. goto out;
  793. }
  794. field->offset = reg;
  795. field->size = size;
  796. field->mask = mask;
  797. field->init = NULL;
  798. field->reset = NULL;
  799. field->release = NULL;
  800. field->clean = xen_pcibk_config_field_free;
  801. err = xen_pcibk_config_quirks_add_field(dev, field);
  802. if (err)
  803. kfree(field);
  804. out:
  805. return err;
  806. }
  807. static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
  808. size_t count)
  809. {
  810. int domain, bus, slot, func;
  811. int err;
  812. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  813. if (err)
  814. goto out;
  815. err = pcistub_device_id_add(domain, bus, slot, func);
  816. out:
  817. if (!err)
  818. err = count;
  819. return err;
  820. }
  821. static DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
  822. static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
  823. size_t count)
  824. {
  825. int domain, bus, slot, func;
  826. int err;
  827. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  828. if (err)
  829. goto out;
  830. err = pcistub_device_id_remove(domain, bus, slot, func);
  831. out:
  832. if (!err)
  833. err = count;
  834. return err;
  835. }
  836. static DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
  837. static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
  838. {
  839. struct pcistub_device_id *pci_dev_id;
  840. size_t count = 0;
  841. unsigned long flags;
  842. spin_lock_irqsave(&device_ids_lock, flags);
  843. list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
  844. if (count >= PAGE_SIZE)
  845. break;
  846. count += scnprintf(buf + count, PAGE_SIZE - count,
  847. "%04x:%02x:%02x.%01x\n",
  848. pci_dev_id->domain, pci_dev_id->bus,
  849. PCI_SLOT(pci_dev_id->devfn),
  850. PCI_FUNC(pci_dev_id->devfn));
  851. }
  852. spin_unlock_irqrestore(&device_ids_lock, flags);
  853. return count;
  854. }
  855. static DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
  856. static ssize_t pcistub_irq_handler_show(struct device_driver *drv, char *buf)
  857. {
  858. struct pcistub_device *psdev;
  859. struct xen_pcibk_dev_data *dev_data;
  860. size_t count = 0;
  861. unsigned long flags;
  862. spin_lock_irqsave(&pcistub_devices_lock, flags);
  863. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  864. if (count >= PAGE_SIZE)
  865. break;
  866. if (!psdev->dev)
  867. continue;
  868. dev_data = pci_get_drvdata(psdev->dev);
  869. if (!dev_data)
  870. continue;
  871. count +=
  872. scnprintf(buf + count, PAGE_SIZE - count,
  873. "%s:%s:%sing:%ld\n",
  874. pci_name(psdev->dev),
  875. dev_data->isr_on ? "on" : "off",
  876. dev_data->ack_intr ? "ack" : "not ack",
  877. dev_data->handled);
  878. }
  879. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  880. return count;
  881. }
  882. static DRIVER_ATTR(irq_handlers, S_IRUSR, pcistub_irq_handler_show, NULL);
  883. static ssize_t pcistub_irq_handler_switch(struct device_driver *drv,
  884. const char *buf,
  885. size_t count)
  886. {
  887. struct pcistub_device *psdev;
  888. struct xen_pcibk_dev_data *dev_data;
  889. int domain, bus, slot, func;
  890. int err = -ENOENT;
  891. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  892. if (err)
  893. goto out;
  894. psdev = pcistub_device_find(domain, bus, slot, func);
  895. if (!psdev)
  896. goto out;
  897. dev_data = pci_get_drvdata(psdev->dev);
  898. if (!dev_data)
  899. goto out;
  900. dev_dbg(&psdev->dev->dev, "%s fake irq handler: %d->%d\n",
  901. dev_data->irq_name, dev_data->isr_on,
  902. !dev_data->isr_on);
  903. dev_data->isr_on = !(dev_data->isr_on);
  904. if (dev_data->isr_on)
  905. dev_data->ack_intr = 1;
  906. out:
  907. if (!err)
  908. err = count;
  909. return err;
  910. }
  911. static DRIVER_ATTR(irq_handler_state, S_IWUSR, NULL,
  912. pcistub_irq_handler_switch);
  913. static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
  914. size_t count)
  915. {
  916. int domain, bus, slot, func, reg, size, mask;
  917. int err;
  918. err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
  919. &mask);
  920. if (err)
  921. goto out;
  922. err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
  923. out:
  924. if (!err)
  925. err = count;
  926. return err;
  927. }
  928. static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
  929. {
  930. int count = 0;
  931. unsigned long flags;
  932. struct xen_pcibk_config_quirk *quirk;
  933. struct xen_pcibk_dev_data *dev_data;
  934. const struct config_field *field;
  935. const struct config_field_entry *cfg_entry;
  936. spin_lock_irqsave(&device_ids_lock, flags);
  937. list_for_each_entry(quirk, &xen_pcibk_quirks, quirks_list) {
  938. if (count >= PAGE_SIZE)
  939. goto out;
  940. count += scnprintf(buf + count, PAGE_SIZE - count,
  941. "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
  942. quirk->pdev->bus->number,
  943. PCI_SLOT(quirk->pdev->devfn),
  944. PCI_FUNC(quirk->pdev->devfn),
  945. quirk->devid.vendor, quirk->devid.device,
  946. quirk->devid.subvendor,
  947. quirk->devid.subdevice);
  948. dev_data = pci_get_drvdata(quirk->pdev);
  949. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  950. field = cfg_entry->field;
  951. if (count >= PAGE_SIZE)
  952. goto out;
  953. count += scnprintf(buf + count, PAGE_SIZE - count,
  954. "\t\t%08x:%01x:%08x\n",
  955. cfg_entry->base_offset +
  956. field->offset, field->size,
  957. field->mask);
  958. }
  959. }
  960. out:
  961. spin_unlock_irqrestore(&device_ids_lock, flags);
  962. return count;
  963. }
  964. static DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show,
  965. pcistub_quirk_add);
  966. static ssize_t permissive_add(struct device_driver *drv, const char *buf,
  967. size_t count)
  968. {
  969. int domain, bus, slot, func;
  970. int err;
  971. struct pcistub_device *psdev;
  972. struct xen_pcibk_dev_data *dev_data;
  973. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  974. if (err)
  975. goto out;
  976. psdev = pcistub_device_find(domain, bus, slot, func);
  977. if (!psdev) {
  978. err = -ENODEV;
  979. goto out;
  980. }
  981. if (!psdev->dev) {
  982. err = -ENODEV;
  983. goto release;
  984. }
  985. dev_data = pci_get_drvdata(psdev->dev);
  986. /* the driver data for a device should never be null at this point */
  987. if (!dev_data) {
  988. err = -ENXIO;
  989. goto release;
  990. }
  991. if (!dev_data->permissive) {
  992. dev_data->permissive = 1;
  993. /* Let user know that what they're doing could be unsafe */
  994. dev_warn(&psdev->dev->dev, "enabling permissive mode "
  995. "configuration space accesses!\n");
  996. dev_warn(&psdev->dev->dev,
  997. "permissive mode is potentially unsafe!\n");
  998. }
  999. release:
  1000. pcistub_device_put(psdev);
  1001. out:
  1002. if (!err)
  1003. err = count;
  1004. return err;
  1005. }
  1006. static ssize_t permissive_show(struct device_driver *drv, char *buf)
  1007. {
  1008. struct pcistub_device *psdev;
  1009. struct xen_pcibk_dev_data *dev_data;
  1010. size_t count = 0;
  1011. unsigned long flags;
  1012. spin_lock_irqsave(&pcistub_devices_lock, flags);
  1013. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  1014. if (count >= PAGE_SIZE)
  1015. break;
  1016. if (!psdev->dev)
  1017. continue;
  1018. dev_data = pci_get_drvdata(psdev->dev);
  1019. if (!dev_data || !dev_data->permissive)
  1020. continue;
  1021. count +=
  1022. scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
  1023. pci_name(psdev->dev));
  1024. }
  1025. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  1026. return count;
  1027. }
  1028. static DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show,
  1029. permissive_add);
  1030. static void pcistub_exit(void)
  1031. {
  1032. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_new_slot);
  1033. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1034. &driver_attr_remove_slot);
  1035. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_slots);
  1036. driver_remove_file(&xen_pcibk_pci_driver.driver, &driver_attr_quirks);
  1037. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1038. &driver_attr_permissive);
  1039. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1040. &driver_attr_irq_handlers);
  1041. driver_remove_file(&xen_pcibk_pci_driver.driver,
  1042. &driver_attr_irq_handler_state);
  1043. pci_unregister_driver(&xen_pcibk_pci_driver);
  1044. }
  1045. static int __init pcistub_init(void)
  1046. {
  1047. int pos = 0;
  1048. int err = 0;
  1049. int domain, bus, slot, func;
  1050. int parsed;
  1051. if (pci_devs_to_hide && *pci_devs_to_hide) {
  1052. do {
  1053. parsed = 0;
  1054. err = sscanf(pci_devs_to_hide + pos,
  1055. " (%x:%x:%x.%x) %n",
  1056. &domain, &bus, &slot, &func, &parsed);
  1057. if (err != 4) {
  1058. domain = 0;
  1059. err = sscanf(pci_devs_to_hide + pos,
  1060. " (%x:%x.%x) %n",
  1061. &bus, &slot, &func, &parsed);
  1062. if (err != 3)
  1063. goto parse_error;
  1064. }
  1065. err = pcistub_device_id_add(domain, bus, slot, func);
  1066. if (err)
  1067. goto out;
  1068. /* if parsed<=0, we've reached the end of the string */
  1069. pos += parsed;
  1070. } while (parsed > 0 && pci_devs_to_hide[pos]);
  1071. }
  1072. /* If we're the first PCI Device Driver to register, we're the
  1073. * first one to get offered PCI devices as they become
  1074. * available (and thus we can be the first to grab them)
  1075. */
  1076. err = pci_register_driver(&xen_pcibk_pci_driver);
  1077. if (err < 0)
  1078. goto out;
  1079. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1080. &driver_attr_new_slot);
  1081. if (!err)
  1082. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1083. &driver_attr_remove_slot);
  1084. if (!err)
  1085. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1086. &driver_attr_slots);
  1087. if (!err)
  1088. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1089. &driver_attr_quirks);
  1090. if (!err)
  1091. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1092. &driver_attr_permissive);
  1093. if (!err)
  1094. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1095. &driver_attr_irq_handlers);
  1096. if (!err)
  1097. err = driver_create_file(&xen_pcibk_pci_driver.driver,
  1098. &driver_attr_irq_handler_state);
  1099. if (err)
  1100. pcistub_exit();
  1101. out:
  1102. return err;
  1103. parse_error:
  1104. printk(KERN_ERR DRV_NAME ": Error parsing pci_devs_to_hide at \"%s\"\n",
  1105. pci_devs_to_hide + pos);
  1106. return -EINVAL;
  1107. }
  1108. #ifndef MODULE
  1109. /*
  1110. * fs_initcall happens before device_initcall
  1111. * so xen_pcibk *should* get called first (b/c we
  1112. * want to suck up any device before other drivers
  1113. * get a chance by being the first pci device
  1114. * driver to register)
  1115. */
  1116. fs_initcall(pcistub_init);
  1117. #endif
  1118. static int __init xen_pcibk_init(void)
  1119. {
  1120. int err;
  1121. if (!xen_initial_domain())
  1122. return -ENODEV;
  1123. err = xen_pcibk_config_init();
  1124. if (err)
  1125. return err;
  1126. #ifdef MODULE
  1127. err = pcistub_init();
  1128. if (err < 0)
  1129. return err;
  1130. #endif
  1131. pcistub_init_devices_late();
  1132. err = xen_pcibk_xenbus_register();
  1133. if (err)
  1134. pcistub_exit();
  1135. return err;
  1136. }
  1137. static void __exit xen_pcibk_cleanup(void)
  1138. {
  1139. xen_pcibk_xenbus_unregister();
  1140. pcistub_exit();
  1141. }
  1142. module_init(xen_pcibk_init);
  1143. module_exit(xen_pcibk_cleanup);
  1144. MODULE_LICENSE("Dual BSD/GPL");
  1145. MODULE_ALIAS("xen-backend:pci");