pci_stub.c 35 KB

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