pci_stub.c 37 KB

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