pci_stub.c 40 KB

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