pci_stub.c 37 KB

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