pci_emul_init(struct vmctx *ctx, struct pci_vdev_ops *ops, int bus, int slot,
	      int func, struct funcinfo *fi)
{
	struct pci_vdev *pdi;
	int err;

	pdi = calloc(1, sizeof(struct pci_vdev));
	if (!pdi) {
		fprintf(stderr, "%s: calloc returns NULL\n", __func__);
		return -1;
	}

	pdi->vmctx = ctx;
	pdi->bus = bus;
	pdi->slot = slot;
	pdi->func = func;
	pthread_mutex_init(&pdi->lintr.lock, NULL);
	pdi->lintr.pin = 0;
	pdi->lintr.state = IDLE;
	pdi->lintr.pirq_pin = 0;
	pdi->lintr.ioapic_irq = 0;
	pdi->dev_ops = ops;
	snprintf(pdi->name, PI_NAMESZ, "%s-pci-%d", ops->class_name, slot);

	/* Disable legacy interrupts */
	pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
	pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);

	pci_set_cfgdata8(pdi, PCIR_COMMAND,
		    PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);

	if (fi->fi_param_saved)
		fi->fi_param = strdup(fi->fi_param_saved);
	else
		fi->fi_param = NULL;
	err = (*ops->vdev_init)(ctx, pdi, fi->fi_param);
	if (err == 0)
		fi->fi_devi = pdi;
	else
		free(pdi);

	return err;
}
pci_emul_init(struct vmctx *ctx, struct pci_vdev_ops *ops, int bus, int slot,
	      int func, struct funcinfo *fi)
{
	struct pci_vdev *pdi;
	int err;

	pdi = calloc(1, sizeof(struct pci_vdev));
	if (!pdi) {
		fprintf(stderr, "%s: calloc returns NULL\n", __func__);
		return -1;
	}

	pdi->vmctx = ctx;
	pdi->bus = bus;
	pdi->slot = slot;
	pdi->func = func;
	pthread_mutex_init(&pdi->lintr.lock, NULL);
	pdi->lintr.pin = 0;
	pdi->lintr.state = IDLE;
	pdi->lintr.pirq_pin = 0;
	pdi->lintr.ioapic_irq = 0;
	pdi->dev_ops = ops;
	snprintf(pdi->name, PI_NAMESZ, "%s-pci-%d", ops->class_name, slot);

	 
	pci_set_cfgdata8(pdi, PCIR_INTLINE, 255);
	pci_set_cfgdata8(pdi, PCIR_INTPIN, 0);

	pci_set_cfgdata8(pdi, PCIR_COMMAND,
		    PCIM_CMD_PORTEN | PCIM_CMD_MEMEN | PCIM_CMD_BUSMASTEREN);

	if (fi->fi_param_saved)
		fi->fi_param = strdup(fi->fi_param_saved);
	else
		fi->fi_param = NULL;
	err = (*ops->vdev_init)(ctx, pdi, fi->fi_param);
	if (err == 0)
		fi->fi_devi = pdi;
	else
		free(pdi);

	return err;
}