pci_stub.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285
  1. /*
  2. * PCI Stub Driver - Grabs devices in backend to be exported later
  3. *
  4. * Ryan Wilson <hap9@epoch.ncsc.mil>
  5. * Chris Bookholt <hap10@epoch.ncsc.mil>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/rwsem.h>
  10. #include <linux/list.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/kref.h>
  13. #include <linux/pci.h>
  14. #include <linux/wait.h>
  15. #include <linux/sched.h>
  16. #include <linux/atomic.h>
  17. #include <xen/events.h>
  18. #include <asm/xen/pci.h>
  19. #include <asm/xen/hypervisor.h>
  20. #include "pciback.h"
  21. #include "conf_space.h"
  22. #include "conf_space_quirks.h"
  23. static char *pci_devs_to_hide;
  24. wait_queue_head_t aer_wait_queue;
  25. /*Add sem for sync AER handling and pciback remove/reconfigue ops,
  26. * We want to avoid in middle of AER ops, pciback devices is being removed
  27. */
  28. static DECLARE_RWSEM(pcistub_sem);
  29. module_param_named(hide, pci_devs_to_hide, charp, 0444);
  30. struct pcistub_device_id {
  31. struct list_head slot_list;
  32. int domain;
  33. unsigned char bus;
  34. unsigned int devfn;
  35. };
  36. static LIST_HEAD(pcistub_device_ids);
  37. static DEFINE_SPINLOCK(device_ids_lock);
  38. struct pcistub_device {
  39. struct kref kref;
  40. struct list_head dev_list;
  41. spinlock_t lock;
  42. struct pci_dev *dev;
  43. struct pciback_device *pdev;/* non-NULL if struct pci_dev is in use */
  44. };
  45. /* Access to pcistub_devices & seized_devices lists and the initialize_devices
  46. * flag must be locked with pcistub_devices_lock
  47. */
  48. static DEFINE_SPINLOCK(pcistub_devices_lock);
  49. static LIST_HEAD(pcistub_devices);
  50. /* wait for device_initcall before initializing our devices
  51. * (see pcistub_init_devices_late)
  52. */
  53. static int initialize_devices;
  54. static LIST_HEAD(seized_devices);
  55. static struct pcistub_device *pcistub_device_alloc(struct pci_dev *dev)
  56. {
  57. struct pcistub_device *psdev;
  58. dev_dbg(&dev->dev, "pcistub_device_alloc\n");
  59. psdev = kzalloc(sizeof(*psdev), GFP_ATOMIC);
  60. if (!psdev)
  61. return NULL;
  62. psdev->dev = pci_dev_get(dev);
  63. if (!psdev->dev) {
  64. kfree(psdev);
  65. return NULL;
  66. }
  67. kref_init(&psdev->kref);
  68. spin_lock_init(&psdev->lock);
  69. return psdev;
  70. }
  71. /* Don't call this directly as it's called by pcistub_device_put */
  72. static void pcistub_device_release(struct kref *kref)
  73. {
  74. struct pcistub_device *psdev;
  75. psdev = container_of(kref, struct pcistub_device, kref);
  76. dev_dbg(&psdev->dev->dev, "pcistub_device_release\n");
  77. /* Clean-up the device */
  78. pciback_reset_device(psdev->dev);
  79. pciback_config_free_dyn_fields(psdev->dev);
  80. pciback_config_free_dev(psdev->dev);
  81. kfree(pci_get_drvdata(psdev->dev));
  82. pci_set_drvdata(psdev->dev, NULL);
  83. pci_dev_put(psdev->dev);
  84. kfree(psdev);
  85. }
  86. static inline void pcistub_device_get(struct pcistub_device *psdev)
  87. {
  88. kref_get(&psdev->kref);
  89. }
  90. static inline void pcistub_device_put(struct pcistub_device *psdev)
  91. {
  92. kref_put(&psdev->kref, pcistub_device_release);
  93. }
  94. static struct pcistub_device *pcistub_device_find(int domain, int bus,
  95. int slot, int func)
  96. {
  97. struct pcistub_device *psdev = NULL;
  98. unsigned long flags;
  99. spin_lock_irqsave(&pcistub_devices_lock, flags);
  100. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  101. if (psdev->dev != NULL
  102. && domain == pci_domain_nr(psdev->dev->bus)
  103. && bus == psdev->dev->bus->number
  104. && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
  105. pcistub_device_get(psdev);
  106. goto out;
  107. }
  108. }
  109. /* didn't find it */
  110. psdev = NULL;
  111. out:
  112. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  113. return psdev;
  114. }
  115. static struct pci_dev *pcistub_device_get_pci_dev(struct pciback_device *pdev,
  116. struct pcistub_device *psdev)
  117. {
  118. struct pci_dev *pci_dev = NULL;
  119. unsigned long flags;
  120. pcistub_device_get(psdev);
  121. spin_lock_irqsave(&psdev->lock, flags);
  122. if (!psdev->pdev) {
  123. psdev->pdev = pdev;
  124. pci_dev = psdev->dev;
  125. }
  126. spin_unlock_irqrestore(&psdev->lock, flags);
  127. if (!pci_dev)
  128. pcistub_device_put(psdev);
  129. return pci_dev;
  130. }
  131. struct pci_dev *pcistub_get_pci_dev_by_slot(struct pciback_device *pdev,
  132. int domain, int bus,
  133. int slot, int func)
  134. {
  135. struct pcistub_device *psdev;
  136. struct pci_dev *found_dev = NULL;
  137. unsigned long flags;
  138. spin_lock_irqsave(&pcistub_devices_lock, flags);
  139. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  140. if (psdev->dev != NULL
  141. && domain == pci_domain_nr(psdev->dev->bus)
  142. && bus == psdev->dev->bus->number
  143. && PCI_DEVFN(slot, func) == psdev->dev->devfn) {
  144. found_dev = pcistub_device_get_pci_dev(pdev, psdev);
  145. break;
  146. }
  147. }
  148. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  149. return found_dev;
  150. }
  151. struct pci_dev *pcistub_get_pci_dev(struct pciback_device *pdev,
  152. struct pci_dev *dev)
  153. {
  154. struct pcistub_device *psdev;
  155. struct pci_dev *found_dev = NULL;
  156. unsigned long flags;
  157. spin_lock_irqsave(&pcistub_devices_lock, flags);
  158. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  159. if (psdev->dev == dev) {
  160. found_dev = pcistub_device_get_pci_dev(pdev, psdev);
  161. break;
  162. }
  163. }
  164. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  165. return found_dev;
  166. }
  167. void pcistub_put_pci_dev(struct pci_dev *dev)
  168. {
  169. struct pcistub_device *psdev, *found_psdev = NULL;
  170. unsigned long flags;
  171. spin_lock_irqsave(&pcistub_devices_lock, flags);
  172. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  173. if (psdev->dev == dev) {
  174. found_psdev = psdev;
  175. break;
  176. }
  177. }
  178. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  179. /*hold this lock for avoiding breaking link between
  180. * pcistub and pciback when AER is in processing
  181. */
  182. down_write(&pcistub_sem);
  183. /* Cleanup our device
  184. * (so it's ready for the next domain)
  185. */
  186. pciback_reset_device(found_psdev->dev);
  187. pciback_config_free_dyn_fields(found_psdev->dev);
  188. pciback_config_reset_dev(found_psdev->dev);
  189. spin_lock_irqsave(&found_psdev->lock, flags);
  190. found_psdev->pdev = NULL;
  191. spin_unlock_irqrestore(&found_psdev->lock, flags);
  192. pcistub_device_put(found_psdev);
  193. up_write(&pcistub_sem);
  194. }
  195. static int __devinit pcistub_match_one(struct pci_dev *dev,
  196. struct pcistub_device_id *pdev_id)
  197. {
  198. /* Match the specified device by domain, bus, slot, func and also if
  199. * any of the device's parent bridges match.
  200. */
  201. for (; dev != NULL; dev = dev->bus->self) {
  202. if (pci_domain_nr(dev->bus) == pdev_id->domain
  203. && dev->bus->number == pdev_id->bus
  204. && dev->devfn == pdev_id->devfn)
  205. return 1;
  206. /* Sometimes topmost bridge links to itself. */
  207. if (dev == dev->bus->self)
  208. break;
  209. }
  210. return 0;
  211. }
  212. static int __devinit pcistub_match(struct pci_dev *dev)
  213. {
  214. struct pcistub_device_id *pdev_id;
  215. unsigned long flags;
  216. int found = 0;
  217. spin_lock_irqsave(&device_ids_lock, flags);
  218. list_for_each_entry(pdev_id, &pcistub_device_ids, slot_list) {
  219. if (pcistub_match_one(dev, pdev_id)) {
  220. found = 1;
  221. break;
  222. }
  223. }
  224. spin_unlock_irqrestore(&device_ids_lock, flags);
  225. return found;
  226. }
  227. static int __devinit pcistub_init_device(struct pci_dev *dev)
  228. {
  229. struct pciback_dev_data *dev_data;
  230. int err = 0;
  231. dev_dbg(&dev->dev, "initializing...\n");
  232. /* The PCI backend is not intended to be a module (or to work with
  233. * removable PCI devices (yet). If it were, pciback_config_free()
  234. * would need to be called somewhere to free the memory allocated
  235. * here and then to call kfree(pci_get_drvdata(psdev->dev)).
  236. */
  237. dev_data = kzalloc(sizeof(*dev_data), GFP_ATOMIC);
  238. if (!dev_data) {
  239. err = -ENOMEM;
  240. goto out;
  241. }
  242. pci_set_drvdata(dev, dev_data);
  243. dev_dbg(&dev->dev, "initializing config\n");
  244. init_waitqueue_head(&aer_wait_queue);
  245. err = pciback_config_init_dev(dev);
  246. if (err)
  247. goto out;
  248. /* HACK: Force device (& ACPI) to determine what IRQ it's on - we
  249. * must do this here because pcibios_enable_device may specify
  250. * the pci device's true irq (and possibly its other resources)
  251. * if they differ from what's in the configuration space.
  252. * This makes the assumption that the device's resources won't
  253. * change after this point (otherwise this code may break!)
  254. */
  255. dev_dbg(&dev->dev, "enabling device\n");
  256. err = pci_enable_device(dev);
  257. if (err)
  258. goto config_release;
  259. /* Now disable the device (this also ensures some private device
  260. * data is setup before we export)
  261. */
  262. dev_dbg(&dev->dev, "reset device\n");
  263. pciback_reset_device(dev);
  264. return 0;
  265. config_release:
  266. pciback_config_free_dev(dev);
  267. out:
  268. pci_set_drvdata(dev, NULL);
  269. kfree(dev_data);
  270. return err;
  271. }
  272. /*
  273. * Because some initialization still happens on
  274. * devices during fs_initcall, we need to defer
  275. * full initialization of our devices until
  276. * device_initcall.
  277. */
  278. static int __init pcistub_init_devices_late(void)
  279. {
  280. struct pcistub_device *psdev;
  281. unsigned long flags;
  282. int err = 0;
  283. pr_debug("pciback: pcistub_init_devices_late\n");
  284. spin_lock_irqsave(&pcistub_devices_lock, flags);
  285. while (!list_empty(&seized_devices)) {
  286. psdev = container_of(seized_devices.next,
  287. struct pcistub_device, dev_list);
  288. list_del(&psdev->dev_list);
  289. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  290. err = pcistub_init_device(psdev->dev);
  291. if (err) {
  292. dev_err(&psdev->dev->dev,
  293. "error %d initializing device\n", err);
  294. kfree(psdev);
  295. psdev = NULL;
  296. }
  297. spin_lock_irqsave(&pcistub_devices_lock, flags);
  298. if (psdev)
  299. list_add_tail(&psdev->dev_list, &pcistub_devices);
  300. }
  301. initialize_devices = 1;
  302. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  303. return 0;
  304. }
  305. static int __devinit pcistub_seize(struct pci_dev *dev)
  306. {
  307. struct pcistub_device *psdev;
  308. unsigned long flags;
  309. int err = 0;
  310. psdev = pcistub_device_alloc(dev);
  311. if (!psdev)
  312. return -ENOMEM;
  313. spin_lock_irqsave(&pcistub_devices_lock, flags);
  314. if (initialize_devices) {
  315. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  316. /* don't want irqs disabled when calling pcistub_init_device */
  317. err = pcistub_init_device(psdev->dev);
  318. spin_lock_irqsave(&pcistub_devices_lock, flags);
  319. if (!err)
  320. list_add(&psdev->dev_list, &pcistub_devices);
  321. } else {
  322. dev_dbg(&dev->dev, "deferring initialization\n");
  323. list_add(&psdev->dev_list, &seized_devices);
  324. }
  325. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  326. if (err)
  327. pcistub_device_put(psdev);
  328. return err;
  329. }
  330. static int __devinit pcistub_probe(struct pci_dev *dev,
  331. const struct pci_device_id *id)
  332. {
  333. int err = 0;
  334. dev_dbg(&dev->dev, "probing...\n");
  335. if (pcistub_match(dev)) {
  336. if (dev->hdr_type != PCI_HEADER_TYPE_NORMAL
  337. && dev->hdr_type != PCI_HEADER_TYPE_BRIDGE) {
  338. dev_err(&dev->dev, "can't export pci devices that "
  339. "don't have a normal (0) or bridge (1) "
  340. "header type!\n");
  341. err = -ENODEV;
  342. goto out;
  343. }
  344. dev_info(&dev->dev, "seizing device\n");
  345. err = pcistub_seize(dev);
  346. } else
  347. /* Didn't find the device */
  348. err = -ENODEV;
  349. out:
  350. return err;
  351. }
  352. static void pcistub_remove(struct pci_dev *dev)
  353. {
  354. struct pcistub_device *psdev, *found_psdev = NULL;
  355. unsigned long flags;
  356. dev_dbg(&dev->dev, "removing\n");
  357. spin_lock_irqsave(&pcistub_devices_lock, flags);
  358. pciback_config_quirk_release(dev);
  359. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  360. if (psdev->dev == dev) {
  361. found_psdev = psdev;
  362. break;
  363. }
  364. }
  365. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  366. if (found_psdev) {
  367. dev_dbg(&dev->dev, "found device to remove - in use? %p\n",
  368. found_psdev->pdev);
  369. if (found_psdev->pdev) {
  370. printk(KERN_WARNING "pciback: ****** removing device "
  371. "%s while still in-use! ******\n",
  372. pci_name(found_psdev->dev));
  373. printk(KERN_WARNING "pciback: ****** driver domain may "
  374. "still access this device's i/o resources!\n");
  375. printk(KERN_WARNING "pciback: ****** shutdown driver "
  376. "domain before binding device\n");
  377. printk(KERN_WARNING "pciback: ****** to other drivers "
  378. "or domains\n");
  379. pciback_release_pci_dev(found_psdev->pdev,
  380. found_psdev->dev);
  381. }
  382. spin_lock_irqsave(&pcistub_devices_lock, flags);
  383. list_del(&found_psdev->dev_list);
  384. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  385. /* the final put for releasing from the list */
  386. pcistub_device_put(found_psdev);
  387. }
  388. }
  389. static DEFINE_PCI_DEVICE_TABLE(pcistub_ids) = {
  390. {
  391. .vendor = PCI_ANY_ID,
  392. .device = PCI_ANY_ID,
  393. .subvendor = PCI_ANY_ID,
  394. .subdevice = PCI_ANY_ID,
  395. },
  396. {0,},
  397. };
  398. #define PCI_NODENAME_MAX 40
  399. static void kill_domain_by_device(struct pcistub_device *psdev)
  400. {
  401. struct xenbus_transaction xbt;
  402. int err;
  403. char nodename[PCI_NODENAME_MAX];
  404. if (!psdev)
  405. dev_err(&psdev->dev->dev,
  406. "device is NULL when do AER recovery/kill_domain\n");
  407. snprintf(nodename, PCI_NODENAME_MAX, "/local/domain/0/backend/pci/%d/0",
  408. psdev->pdev->xdev->otherend_id);
  409. nodename[strlen(nodename)] = '\0';
  410. again:
  411. err = xenbus_transaction_start(&xbt);
  412. if (err) {
  413. dev_err(&psdev->dev->dev,
  414. "error %d when start xenbus transaction\n", err);
  415. return;
  416. }
  417. /*PV AER handlers will set this flag*/
  418. xenbus_printf(xbt, nodename, "aerState" , "aerfail");
  419. err = xenbus_transaction_end(xbt, 0);
  420. if (err) {
  421. if (err == -EAGAIN)
  422. goto again;
  423. dev_err(&psdev->dev->dev,
  424. "error %d when end xenbus transaction\n", err);
  425. return;
  426. }
  427. }
  428. /* For each aer recovery step error_detected, mmio_enabled, etc, front_end and
  429. * backend need to have cooperation. In pciback, those steps will do similar
  430. * jobs: send service request and waiting for front_end response.
  431. */
  432. static pci_ers_result_t common_process(struct pcistub_device *psdev,
  433. pci_channel_state_t state, int aer_cmd, pci_ers_result_t result)
  434. {
  435. pci_ers_result_t res = result;
  436. struct xen_pcie_aer_op *aer_op;
  437. int ret;
  438. /*with PV AER drivers*/
  439. aer_op = &(psdev->pdev->sh_info->aer_op);
  440. aer_op->cmd = aer_cmd ;
  441. /*useful for error_detected callback*/
  442. aer_op->err = state;
  443. /*pcifront_end BDF*/
  444. ret = pciback_get_pcifront_dev(psdev->dev, psdev->pdev,
  445. &aer_op->domain, &aer_op->bus, &aer_op->devfn);
  446. if (!ret) {
  447. dev_err(&psdev->dev->dev,
  448. "pciback: failed to get pcifront device\n");
  449. return PCI_ERS_RESULT_NONE;
  450. }
  451. wmb();
  452. dev_dbg(&psdev->dev->dev,
  453. "pciback: aer_op %x dom %x bus %x devfn %x\n",
  454. aer_cmd, aer_op->domain, aer_op->bus, aer_op->devfn);
  455. /*local flag to mark there's aer request, pciback callback will use this
  456. * flag to judge whether we need to check pci-front give aer service
  457. * ack signal
  458. */
  459. set_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  460. /*It is possible that a pcifront conf_read_write ops request invokes
  461. * the callback which cause the spurious execution of wake_up.
  462. * Yet it is harmless and better than a spinlock here
  463. */
  464. set_bit(_XEN_PCIB_active,
  465. (unsigned long *)&psdev->pdev->sh_info->flags);
  466. wmb();
  467. notify_remote_via_irq(psdev->pdev->evtchn_irq);
  468. ret = wait_event_timeout(aer_wait_queue, !(test_bit(_XEN_PCIB_active,
  469. (unsigned long *)&psdev->pdev->sh_info->flags)), 300*HZ);
  470. if (!ret) {
  471. if (test_bit(_XEN_PCIB_active,
  472. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  473. dev_err(&psdev->dev->dev,
  474. "pcifront aer process not responding!\n");
  475. clear_bit(_XEN_PCIB_active,
  476. (unsigned long *)&psdev->pdev->sh_info->flags);
  477. aer_op->err = PCI_ERS_RESULT_NONE;
  478. return res;
  479. }
  480. }
  481. clear_bit(_PCIB_op_pending, (unsigned long *)&psdev->pdev->flags);
  482. if (test_bit(_XEN_PCIF_active,
  483. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  484. dev_dbg(&psdev->dev->dev,
  485. "schedule pci_conf service in pciback\n");
  486. test_and_schedule_op(psdev->pdev);
  487. }
  488. res = (pci_ers_result_t)aer_op->err;
  489. return res;
  490. }
  491. /*
  492. * pciback_slot_reset: it will send the slot_reset request to pcifront in case
  493. * of the device driver could provide this service, and then wait for pcifront
  494. * ack.
  495. * @dev: pointer to PCI devices
  496. * return value is used by aer_core do_recovery policy
  497. */
  498. static pci_ers_result_t pciback_slot_reset(struct pci_dev *dev)
  499. {
  500. struct pcistub_device *psdev;
  501. pci_ers_result_t result;
  502. result = PCI_ERS_RESULT_RECOVERED;
  503. dev_dbg(&dev->dev, "pciback_slot_reset(bus:%x,devfn:%x)\n",
  504. dev->bus->number, dev->devfn);
  505. down_write(&pcistub_sem);
  506. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  507. dev->bus->number,
  508. PCI_SLOT(dev->devfn),
  509. PCI_FUNC(dev->devfn));
  510. if (!psdev || !psdev->pdev) {
  511. dev_err(&dev->dev,
  512. "pciback device is not found/assigned\n");
  513. goto end;
  514. }
  515. if (!psdev->pdev->sh_info) {
  516. dev_err(&dev->dev, "pciback device is not connected or owned"
  517. " by HVM, kill it\n");
  518. kill_domain_by_device(psdev);
  519. goto release;
  520. }
  521. if (!test_bit(_XEN_PCIB_AERHANDLER,
  522. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  523. dev_err(&dev->dev,
  524. "guest with no AER driver should have been killed\n");
  525. goto release;
  526. }
  527. result = common_process(psdev, 1, XEN_PCI_OP_aer_slotreset, result);
  528. if (result == PCI_ERS_RESULT_NONE ||
  529. result == PCI_ERS_RESULT_DISCONNECT) {
  530. dev_dbg(&dev->dev,
  531. "No AER slot_reset service or disconnected!\n");
  532. kill_domain_by_device(psdev);
  533. }
  534. release:
  535. pcistub_device_put(psdev);
  536. end:
  537. up_write(&pcistub_sem);
  538. return result;
  539. }
  540. /*pciback_mmio_enabled: it will send the mmio_enabled request to pcifront
  541. * in case of the device driver could provide this service, and then wait
  542. * for pcifront ack
  543. * @dev: pointer to PCI devices
  544. * return value is used by aer_core do_recovery policy
  545. */
  546. static pci_ers_result_t pciback_mmio_enabled(struct pci_dev *dev)
  547. {
  548. struct pcistub_device *psdev;
  549. pci_ers_result_t result;
  550. result = PCI_ERS_RESULT_RECOVERED;
  551. dev_dbg(&dev->dev, "pciback_mmio_enabled(bus:%x,devfn:%x)\n",
  552. dev->bus->number, dev->devfn);
  553. down_write(&pcistub_sem);
  554. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  555. dev->bus->number,
  556. PCI_SLOT(dev->devfn),
  557. PCI_FUNC(dev->devfn));
  558. if (!psdev || !psdev->pdev) {
  559. dev_err(&dev->dev,
  560. "pciback device is not found/assigned\n");
  561. goto end;
  562. }
  563. if (!psdev->pdev->sh_info) {
  564. dev_err(&dev->dev, "pciback device is not connected or owned"
  565. " by HVM, kill it\n");
  566. kill_domain_by_device(psdev);
  567. goto release;
  568. }
  569. if (!test_bit(_XEN_PCIB_AERHANDLER,
  570. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  571. dev_err(&dev->dev,
  572. "guest with no AER driver should have been killed\n");
  573. goto release;
  574. }
  575. result = common_process(psdev, 1, XEN_PCI_OP_aer_mmio, result);
  576. if (result == PCI_ERS_RESULT_NONE ||
  577. result == PCI_ERS_RESULT_DISCONNECT) {
  578. dev_dbg(&dev->dev,
  579. "No AER mmio_enabled service or disconnected!\n");
  580. kill_domain_by_device(psdev);
  581. }
  582. release:
  583. pcistub_device_put(psdev);
  584. end:
  585. up_write(&pcistub_sem);
  586. return result;
  587. }
  588. /*pciback_error_detected: it will send the error_detected request to pcifront
  589. * in case of the device driver could provide this service, and then wait
  590. * for pcifront ack.
  591. * @dev: pointer to PCI devices
  592. * @error: the current PCI connection state
  593. * return value is used by aer_core do_recovery policy
  594. */
  595. static pci_ers_result_t pciback_error_detected(struct pci_dev *dev,
  596. pci_channel_state_t error)
  597. {
  598. struct pcistub_device *psdev;
  599. pci_ers_result_t result;
  600. result = PCI_ERS_RESULT_CAN_RECOVER;
  601. dev_dbg(&dev->dev, "pciback_error_detected(bus:%x,devfn:%x)\n",
  602. dev->bus->number, dev->devfn);
  603. down_write(&pcistub_sem);
  604. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  605. dev->bus->number,
  606. PCI_SLOT(dev->devfn),
  607. PCI_FUNC(dev->devfn));
  608. if (!psdev || !psdev->pdev) {
  609. dev_err(&dev->dev,
  610. "pciback device is not found/assigned\n");
  611. goto end;
  612. }
  613. if (!psdev->pdev->sh_info) {
  614. dev_err(&dev->dev, "pciback device is not connected or owned"
  615. " by HVM, kill it\n");
  616. kill_domain_by_device(psdev);
  617. goto release;
  618. }
  619. /*Guest owns the device yet no aer handler regiested, kill guest*/
  620. if (!test_bit(_XEN_PCIB_AERHANDLER,
  621. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  622. dev_dbg(&dev->dev, "guest may have no aer driver, kill it\n");
  623. kill_domain_by_device(psdev);
  624. goto release;
  625. }
  626. result = common_process(psdev, error, XEN_PCI_OP_aer_detected, result);
  627. if (result == PCI_ERS_RESULT_NONE ||
  628. result == PCI_ERS_RESULT_DISCONNECT) {
  629. dev_dbg(&dev->dev,
  630. "No AER error_detected service or disconnected!\n");
  631. kill_domain_by_device(psdev);
  632. }
  633. release:
  634. pcistub_device_put(psdev);
  635. end:
  636. up_write(&pcistub_sem);
  637. return result;
  638. }
  639. /*pciback_error_resume: it will send the error_resume request to pcifront
  640. * in case of the device driver could provide this service, and then wait
  641. * for pcifront ack.
  642. * @dev: pointer to PCI devices
  643. */
  644. static void pciback_error_resume(struct pci_dev *dev)
  645. {
  646. struct pcistub_device *psdev;
  647. dev_dbg(&dev->dev, "pciback_error_resume(bus:%x,devfn:%x)\n",
  648. dev->bus->number, dev->devfn);
  649. down_write(&pcistub_sem);
  650. psdev = pcistub_device_find(pci_domain_nr(dev->bus),
  651. dev->bus->number,
  652. PCI_SLOT(dev->devfn),
  653. PCI_FUNC(dev->devfn));
  654. if (!psdev || !psdev->pdev) {
  655. dev_err(&dev->dev,
  656. "pciback device is not found/assigned\n");
  657. goto end;
  658. }
  659. if (!psdev->pdev->sh_info) {
  660. dev_err(&dev->dev, "pciback device is not connected or owned"
  661. " by HVM, kill it\n");
  662. kill_domain_by_device(psdev);
  663. goto release;
  664. }
  665. if (!test_bit(_XEN_PCIB_AERHANDLER,
  666. (unsigned long *)&psdev->pdev->sh_info->flags)) {
  667. dev_err(&dev->dev,
  668. "guest with no AER driver should have been killed\n");
  669. kill_domain_by_device(psdev);
  670. goto release;
  671. }
  672. common_process(psdev, 1, XEN_PCI_OP_aer_resume,
  673. PCI_ERS_RESULT_RECOVERED);
  674. release:
  675. pcistub_device_put(psdev);
  676. end:
  677. up_write(&pcistub_sem);
  678. return;
  679. }
  680. /*add pciback AER handling*/
  681. static struct pci_error_handlers pciback_error_handler = {
  682. .error_detected = pciback_error_detected,
  683. .mmio_enabled = pciback_mmio_enabled,
  684. .slot_reset = pciback_slot_reset,
  685. .resume = pciback_error_resume,
  686. };
  687. /*
  688. * Note: There is no MODULE_DEVICE_TABLE entry here because this isn't
  689. * for a normal device. I don't want it to be loaded automatically.
  690. */
  691. static struct pci_driver pciback_pci_driver = {
  692. .name = "pciback",
  693. .id_table = pcistub_ids,
  694. .probe = pcistub_probe,
  695. .remove = pcistub_remove,
  696. .err_handler = &pciback_error_handler,
  697. };
  698. static inline int str_to_slot(const char *buf, int *domain, int *bus,
  699. int *slot, int *func)
  700. {
  701. int err;
  702. err = sscanf(buf, " %x:%x:%x.%x", domain, bus, slot, func);
  703. if (err == 4)
  704. return 0;
  705. else if (err < 0)
  706. return -EINVAL;
  707. /* try again without domain */
  708. *domain = 0;
  709. err = sscanf(buf, " %x:%x.%x", bus, slot, func);
  710. if (err == 3)
  711. return 0;
  712. return -EINVAL;
  713. }
  714. static inline int str_to_quirk(const char *buf, int *domain, int *bus, int
  715. *slot, int *func, int *reg, int *size, int *mask)
  716. {
  717. int err;
  718. err =
  719. sscanf(buf, " %04x:%02x:%02x.%1x-%08x:%1x:%08x", domain, bus, slot,
  720. func, reg, size, mask);
  721. if (err == 7)
  722. return 0;
  723. return -EINVAL;
  724. }
  725. static int pcistub_device_id_add(int domain, int bus, int slot, int func)
  726. {
  727. struct pcistub_device_id *pci_dev_id;
  728. unsigned long flags;
  729. pci_dev_id = kmalloc(sizeof(*pci_dev_id), GFP_KERNEL);
  730. if (!pci_dev_id)
  731. return -ENOMEM;
  732. pci_dev_id->domain = domain;
  733. pci_dev_id->bus = bus;
  734. pci_dev_id->devfn = PCI_DEVFN(slot, func);
  735. pr_debug("pciback: wants to seize %04x:%02x:%02x.%01x\n",
  736. domain, bus, slot, func);
  737. spin_lock_irqsave(&device_ids_lock, flags);
  738. list_add_tail(&pci_dev_id->slot_list, &pcistub_device_ids);
  739. spin_unlock_irqrestore(&device_ids_lock, flags);
  740. return 0;
  741. }
  742. static int pcistub_device_id_remove(int domain, int bus, int slot, int func)
  743. {
  744. struct pcistub_device_id *pci_dev_id, *t;
  745. int devfn = PCI_DEVFN(slot, func);
  746. int err = -ENOENT;
  747. unsigned long flags;
  748. spin_lock_irqsave(&device_ids_lock, flags);
  749. list_for_each_entry_safe(pci_dev_id, t, &pcistub_device_ids,
  750. slot_list) {
  751. if (pci_dev_id->domain == domain
  752. && pci_dev_id->bus == bus && pci_dev_id->devfn == devfn) {
  753. /* Don't break; here because it's possible the same
  754. * slot could be in the list more than once
  755. */
  756. list_del(&pci_dev_id->slot_list);
  757. kfree(pci_dev_id);
  758. err = 0;
  759. pr_debug("pciback: removed %04x:%02x:%02x.%01x from "
  760. "seize list\n", domain, bus, slot, func);
  761. }
  762. }
  763. spin_unlock_irqrestore(&device_ids_lock, flags);
  764. return err;
  765. }
  766. static int pcistub_reg_add(int domain, int bus, int slot, int func, int reg,
  767. int size, int mask)
  768. {
  769. int err = 0;
  770. struct pcistub_device *psdev;
  771. struct pci_dev *dev;
  772. struct config_field *field;
  773. psdev = pcistub_device_find(domain, bus, slot, func);
  774. if (!psdev || !psdev->dev) {
  775. err = -ENODEV;
  776. goto out;
  777. }
  778. dev = psdev->dev;
  779. field = kzalloc(sizeof(*field), GFP_ATOMIC);
  780. if (!field) {
  781. err = -ENOMEM;
  782. goto out;
  783. }
  784. field->offset = reg;
  785. field->size = size;
  786. field->mask = mask;
  787. field->init = NULL;
  788. field->reset = NULL;
  789. field->release = NULL;
  790. field->clean = pciback_config_field_free;
  791. err = pciback_config_quirks_add_field(dev, field);
  792. if (err)
  793. kfree(field);
  794. out:
  795. return err;
  796. }
  797. static ssize_t pcistub_slot_add(struct device_driver *drv, const char *buf,
  798. size_t count)
  799. {
  800. int domain, bus, slot, func;
  801. int err;
  802. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  803. if (err)
  804. goto out;
  805. err = pcistub_device_id_add(domain, bus, slot, func);
  806. out:
  807. if (!err)
  808. err = count;
  809. return err;
  810. }
  811. DRIVER_ATTR(new_slot, S_IWUSR, NULL, pcistub_slot_add);
  812. static ssize_t pcistub_slot_remove(struct device_driver *drv, const char *buf,
  813. size_t count)
  814. {
  815. int domain, bus, slot, func;
  816. int err;
  817. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  818. if (err)
  819. goto out;
  820. err = pcistub_device_id_remove(domain, bus, slot, func);
  821. out:
  822. if (!err)
  823. err = count;
  824. return err;
  825. }
  826. DRIVER_ATTR(remove_slot, S_IWUSR, NULL, pcistub_slot_remove);
  827. static ssize_t pcistub_slot_show(struct device_driver *drv, char *buf)
  828. {
  829. struct pcistub_device_id *pci_dev_id;
  830. size_t count = 0;
  831. unsigned long flags;
  832. spin_lock_irqsave(&device_ids_lock, flags);
  833. list_for_each_entry(pci_dev_id, &pcistub_device_ids, slot_list) {
  834. if (count >= PAGE_SIZE)
  835. break;
  836. count += scnprintf(buf + count, PAGE_SIZE - count,
  837. "%04x:%02x:%02x.%01x\n",
  838. pci_dev_id->domain, pci_dev_id->bus,
  839. PCI_SLOT(pci_dev_id->devfn),
  840. PCI_FUNC(pci_dev_id->devfn));
  841. }
  842. spin_unlock_irqrestore(&device_ids_lock, flags);
  843. return count;
  844. }
  845. DRIVER_ATTR(slots, S_IRUSR, pcistub_slot_show, NULL);
  846. static ssize_t pcistub_quirk_add(struct device_driver *drv, const char *buf,
  847. size_t count)
  848. {
  849. int domain, bus, slot, func, reg, size, mask;
  850. int err;
  851. err = str_to_quirk(buf, &domain, &bus, &slot, &func, &reg, &size,
  852. &mask);
  853. if (err)
  854. goto out;
  855. err = pcistub_reg_add(domain, bus, slot, func, reg, size, mask);
  856. out:
  857. if (!err)
  858. err = count;
  859. return err;
  860. }
  861. static ssize_t pcistub_quirk_show(struct device_driver *drv, char *buf)
  862. {
  863. int count = 0;
  864. unsigned long flags;
  865. struct pciback_config_quirk *quirk;
  866. struct pciback_dev_data *dev_data;
  867. const struct config_field *field;
  868. const struct config_field_entry *cfg_entry;
  869. spin_lock_irqsave(&device_ids_lock, flags);
  870. list_for_each_entry(quirk, &pciback_quirks, quirks_list) {
  871. if (count >= PAGE_SIZE)
  872. goto out;
  873. count += scnprintf(buf + count, PAGE_SIZE - count,
  874. "%02x:%02x.%01x\n\t%04x:%04x:%04x:%04x\n",
  875. quirk->pdev->bus->number,
  876. PCI_SLOT(quirk->pdev->devfn),
  877. PCI_FUNC(quirk->pdev->devfn),
  878. quirk->devid.vendor, quirk->devid.device,
  879. quirk->devid.subvendor,
  880. quirk->devid.subdevice);
  881. dev_data = pci_get_drvdata(quirk->pdev);
  882. list_for_each_entry(cfg_entry, &dev_data->config_fields, list) {
  883. field = cfg_entry->field;
  884. if (count >= PAGE_SIZE)
  885. goto out;
  886. count += scnprintf(buf + count, PAGE_SIZE - count,
  887. "\t\t%08x:%01x:%08x\n",
  888. cfg_entry->base_offset +
  889. field->offset, field->size,
  890. field->mask);
  891. }
  892. }
  893. out:
  894. spin_unlock_irqrestore(&device_ids_lock, flags);
  895. return count;
  896. }
  897. DRIVER_ATTR(quirks, S_IRUSR | S_IWUSR, pcistub_quirk_show, pcistub_quirk_add);
  898. static ssize_t permissive_add(struct device_driver *drv, const char *buf,
  899. size_t count)
  900. {
  901. int domain, bus, slot, func;
  902. int err;
  903. struct pcistub_device *psdev;
  904. struct pciback_dev_data *dev_data;
  905. err = str_to_slot(buf, &domain, &bus, &slot, &func);
  906. if (err)
  907. goto out;
  908. psdev = pcistub_device_find(domain, bus, slot, func);
  909. if (!psdev) {
  910. err = -ENODEV;
  911. goto out;
  912. }
  913. if (!psdev->dev) {
  914. err = -ENODEV;
  915. goto release;
  916. }
  917. dev_data = pci_get_drvdata(psdev->dev);
  918. /* the driver data for a device should never be null at this point */
  919. if (!dev_data) {
  920. err = -ENXIO;
  921. goto release;
  922. }
  923. if (!dev_data->permissive) {
  924. dev_data->permissive = 1;
  925. /* Let user know that what they're doing could be unsafe */
  926. dev_warn(&psdev->dev->dev, "enabling permissive mode "
  927. "configuration space accesses!\n");
  928. dev_warn(&psdev->dev->dev,
  929. "permissive mode is potentially unsafe!\n");
  930. }
  931. release:
  932. pcistub_device_put(psdev);
  933. out:
  934. if (!err)
  935. err = count;
  936. return err;
  937. }
  938. static ssize_t permissive_show(struct device_driver *drv, char *buf)
  939. {
  940. struct pcistub_device *psdev;
  941. struct pciback_dev_data *dev_data;
  942. size_t count = 0;
  943. unsigned long flags;
  944. spin_lock_irqsave(&pcistub_devices_lock, flags);
  945. list_for_each_entry(psdev, &pcistub_devices, dev_list) {
  946. if (count >= PAGE_SIZE)
  947. break;
  948. if (!psdev->dev)
  949. continue;
  950. dev_data = pci_get_drvdata(psdev->dev);
  951. if (!dev_data || !dev_data->permissive)
  952. continue;
  953. count +=
  954. scnprintf(buf + count, PAGE_SIZE - count, "%s\n",
  955. pci_name(psdev->dev));
  956. }
  957. spin_unlock_irqrestore(&pcistub_devices_lock, flags);
  958. return count;
  959. }
  960. DRIVER_ATTR(permissive, S_IRUSR | S_IWUSR, permissive_show, permissive_add);
  961. static void pcistub_exit(void)
  962. {
  963. driver_remove_file(&pciback_pci_driver.driver, &driver_attr_new_slot);
  964. driver_remove_file(&pciback_pci_driver.driver,
  965. &driver_attr_remove_slot);
  966. driver_remove_file(&pciback_pci_driver.driver, &driver_attr_slots);
  967. driver_remove_file(&pciback_pci_driver.driver, &driver_attr_quirks);
  968. driver_remove_file(&pciback_pci_driver.driver, &driver_attr_permissive);
  969. pci_unregister_driver(&pciback_pci_driver);
  970. }
  971. static int __init pcistub_init(void)
  972. {
  973. int pos = 0;
  974. int err = 0;
  975. int domain, bus, slot, func;
  976. int parsed;
  977. if (pci_devs_to_hide && *pci_devs_to_hide) {
  978. do {
  979. parsed = 0;
  980. err = sscanf(pci_devs_to_hide + pos,
  981. " (%x:%x:%x.%x) %n",
  982. &domain, &bus, &slot, &func, &parsed);
  983. if (err != 4) {
  984. domain = 0;
  985. err = sscanf(pci_devs_to_hide + pos,
  986. " (%x:%x.%x) %n",
  987. &bus, &slot, &func, &parsed);
  988. if (err != 3)
  989. goto parse_error;
  990. }
  991. err = pcistub_device_id_add(domain, bus, slot, func);
  992. if (err)
  993. goto out;
  994. /* if parsed<=0, we've reached the end of the string */
  995. pos += parsed;
  996. } while (parsed > 0 && pci_devs_to_hide[pos]);
  997. }
  998. /* If we're the first PCI Device Driver to register, we're the
  999. * first one to get offered PCI devices as they become
  1000. * available (and thus we can be the first to grab them)
  1001. */
  1002. err = pci_register_driver(&pciback_pci_driver);
  1003. if (err < 0)
  1004. goto out;
  1005. err = driver_create_file(&pciback_pci_driver.driver,
  1006. &driver_attr_new_slot);
  1007. if (!err)
  1008. err = driver_create_file(&pciback_pci_driver.driver,
  1009. &driver_attr_remove_slot);
  1010. if (!err)
  1011. err = driver_create_file(&pciback_pci_driver.driver,
  1012. &driver_attr_slots);
  1013. if (!err)
  1014. err = driver_create_file(&pciback_pci_driver.driver,
  1015. &driver_attr_quirks);
  1016. if (!err)
  1017. err = driver_create_file(&pciback_pci_driver.driver,
  1018. &driver_attr_permissive);
  1019. if (err)
  1020. pcistub_exit();
  1021. out:
  1022. return err;
  1023. parse_error:
  1024. printk(KERN_ERR "pciback: Error parsing pci_devs_to_hide at \"%s\"\n",
  1025. pci_devs_to_hide + pos);
  1026. return -EINVAL;
  1027. }
  1028. #ifndef MODULE
  1029. /*
  1030. * fs_initcall happens before device_initcall
  1031. * so pciback *should* get called first (b/c we
  1032. * want to suck up any device before other drivers
  1033. * get a chance by being the first pci device
  1034. * driver to register)
  1035. */
  1036. fs_initcall(pcistub_init);
  1037. #endif
  1038. static int __init pciback_init(void)
  1039. {
  1040. int err;
  1041. if (!xen_initial_domain())
  1042. return -ENODEV;
  1043. err = pciback_config_init();
  1044. if (err)
  1045. return err;
  1046. #ifdef MODULE
  1047. err = pcistub_init();
  1048. if (err < 0)
  1049. return err;
  1050. #endif
  1051. pcistub_init_devices_late();
  1052. err = pciback_xenbus_register();
  1053. if (err)
  1054. pcistub_exit();
  1055. return err;
  1056. }
  1057. static void __exit pciback_cleanup(void)
  1058. {
  1059. pciback_xenbus_unregister();
  1060. pcistub_exit();
  1061. }
  1062. module_init(pciback_init);
  1063. module_exit(pciback_cleanup);
  1064. MODULE_LICENSE("Dual BSD/GPL");