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