pci_stub.c 34 KB

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