pci_stub.c 37 KB

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