xref: /freebsd/sys/dev/hyperv/pcib/vmbus_pcib.c (revision 93b4e111bb02ce42ba82e25c9c049e03ded19062)
18c582c7cSDexuan Cui /*-
2*93b4e111SSepherosa Ziehau  * Copyright (c) 2016-2017 Microsoft Corp.
38c582c7cSDexuan Cui  * All rights reserved.
48c582c7cSDexuan Cui  *
58c582c7cSDexuan Cui  * Redistribution and use in source and binary forms, with or without
68c582c7cSDexuan Cui  * modification, are permitted provided that the following conditions
78c582c7cSDexuan Cui  * are met:
88c582c7cSDexuan Cui  * 1. Redistributions of source code must retain the above copyright
98c582c7cSDexuan Cui  *    notice, this list of conditions and the following disclaimer.
108c582c7cSDexuan Cui  * 2. Redistributions in binary form must reproduce the above copyright
118c582c7cSDexuan Cui  *    notice, this list of conditions and the following disclaimer in the
128c582c7cSDexuan Cui  *    documentation and/or other materials provided with the distribution.
138c582c7cSDexuan Cui  *
148c582c7cSDexuan Cui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
158c582c7cSDexuan Cui  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168c582c7cSDexuan Cui  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
178c582c7cSDexuan Cui  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
188c582c7cSDexuan Cui  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
198c582c7cSDexuan Cui  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
208c582c7cSDexuan Cui  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
218c582c7cSDexuan Cui  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
228c582c7cSDexuan Cui  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
238c582c7cSDexuan Cui  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
248c582c7cSDexuan Cui  * SUCH DAMAGE.
258c582c7cSDexuan Cui  */
268c582c7cSDexuan Cui 
278c582c7cSDexuan Cui #include <sys/cdefs.h>
288c582c7cSDexuan Cui __FBSDID("$FreeBSD$");
298c582c7cSDexuan Cui 
30cdb316eeSDexuan Cui #ifdef NEW_PCIB
31cdb316eeSDexuan Cui 
328c582c7cSDexuan Cui #include <sys/param.h>
338c582c7cSDexuan Cui #include <sys/systm.h>
348c582c7cSDexuan Cui #include <sys/types.h>
358c582c7cSDexuan Cui #include <sys/malloc.h>
368c582c7cSDexuan Cui #include <sys/module.h>
378c582c7cSDexuan Cui #include <sys/kernel.h>
388c582c7cSDexuan Cui #include <sys/queue.h>
398c582c7cSDexuan Cui #include <sys/lock.h>
408c582c7cSDexuan Cui #include <sys/sx.h>
418c582c7cSDexuan Cui #include <sys/smp.h>
428c582c7cSDexuan Cui #include <sys/sysctl.h>
438c582c7cSDexuan Cui #include <sys/bus.h>
448c582c7cSDexuan Cui #include <sys/rman.h>
458c582c7cSDexuan Cui #include <sys/mutex.h>
468c582c7cSDexuan Cui #include <sys/errno.h>
478c582c7cSDexuan Cui 
488c582c7cSDexuan Cui #include <vm/vm.h>
498c582c7cSDexuan Cui #include <vm/vm_param.h>
508c582c7cSDexuan Cui #include <vm/vm_kern.h>
518c582c7cSDexuan Cui #include <vm/pmap.h>
528c582c7cSDexuan Cui 
538c582c7cSDexuan Cui #include <machine/atomic.h>
548c582c7cSDexuan Cui #include <machine/bus.h>
558c582c7cSDexuan Cui #include <machine/frame.h>
568c582c7cSDexuan Cui #include <machine/pci_cfgreg.h>
578c582c7cSDexuan Cui #include <machine/resource.h>
588c582c7cSDexuan Cui 
598c582c7cSDexuan Cui #include <sys/pciio.h>
608c582c7cSDexuan Cui #include <dev/pci/pcireg.h>
618c582c7cSDexuan Cui #include <dev/pci/pcivar.h>
628c582c7cSDexuan Cui #include <dev/pci/pci_private.h>
638c582c7cSDexuan Cui #include <dev/pci/pcib_private.h>
648c582c7cSDexuan Cui #include "pcib_if.h"
658c582c7cSDexuan Cui 
668c582c7cSDexuan Cui #include <machine/intr_machdep.h>
678c582c7cSDexuan Cui #include <x86/apicreg.h>
688c582c7cSDexuan Cui 
698c582c7cSDexuan Cui #include <dev/hyperv/include/hyperv.h>
708c582c7cSDexuan Cui #include <dev/hyperv/include/hyperv_busdma.h>
718c582c7cSDexuan Cui #include <dev/hyperv/include/vmbus_xact.h>
728c582c7cSDexuan Cui #include <dev/hyperv/vmbus/vmbus_reg.h>
738c582c7cSDexuan Cui #include <dev/hyperv/vmbus/vmbus_chanvar.h>
748c582c7cSDexuan Cui 
758c582c7cSDexuan Cui #include "vmbus_if.h"
768c582c7cSDexuan Cui 
778c582c7cSDexuan Cui #if __FreeBSD_version < 1100000
788c582c7cSDexuan Cui typedef u_long rman_res_t;
798c582c7cSDexuan Cui #define RM_MAX_END	(~(rman_res_t)0)
808c582c7cSDexuan Cui #endif
818c582c7cSDexuan Cui 
828c582c7cSDexuan Cui struct completion {
838c582c7cSDexuan Cui 	unsigned int done;
848c582c7cSDexuan Cui 	struct mtx lock;
858c582c7cSDexuan Cui };
868c582c7cSDexuan Cui 
878c582c7cSDexuan Cui static void
888c582c7cSDexuan Cui init_completion(struct completion *c)
898c582c7cSDexuan Cui {
908c582c7cSDexuan Cui 	memset(c, 0, sizeof(*c));
918c582c7cSDexuan Cui 	mtx_init(&c->lock, "hvcmpl", NULL, MTX_DEF);
928c582c7cSDexuan Cui 	c->done = 0;
938c582c7cSDexuan Cui }
948c582c7cSDexuan Cui 
958c582c7cSDexuan Cui static void
968c582c7cSDexuan Cui free_completion(struct completion *c)
978c582c7cSDexuan Cui {
988c582c7cSDexuan Cui 	mtx_destroy(&c->lock);
998c582c7cSDexuan Cui }
1008c582c7cSDexuan Cui 
1018c582c7cSDexuan Cui static void
1028c582c7cSDexuan Cui complete(struct completion *c)
1038c582c7cSDexuan Cui {
1048c582c7cSDexuan Cui 	mtx_lock(&c->lock);
1058c582c7cSDexuan Cui 	c->done++;
1068c582c7cSDexuan Cui 	mtx_unlock(&c->lock);
1078c582c7cSDexuan Cui 	wakeup(c);
1088c582c7cSDexuan Cui }
1098c582c7cSDexuan Cui 
1108c582c7cSDexuan Cui static void
1118c582c7cSDexuan Cui wait_for_completion(struct completion *c)
1128c582c7cSDexuan Cui {
1138c582c7cSDexuan Cui 	mtx_lock(&c->lock);
1148c582c7cSDexuan Cui 	while (c->done == 0)
1158c582c7cSDexuan Cui 		mtx_sleep(c, &c->lock, 0, "hvwfc", 0);
1168c582c7cSDexuan Cui 	c->done--;
1178c582c7cSDexuan Cui 	mtx_unlock(&c->lock);
1188c582c7cSDexuan Cui }
1198c582c7cSDexuan Cui 
1208c582c7cSDexuan Cui #define PCI_MAKE_VERSION(major, minor) ((uint32_t)(((major) << 16) | (major)))
1218c582c7cSDexuan Cui 
1228c582c7cSDexuan Cui enum {
1238c582c7cSDexuan Cui 	PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1),
1248c582c7cSDexuan Cui 	PCI_PROTOCOL_VERSION_CURRENT = PCI_PROTOCOL_VERSION_1_1
1258c582c7cSDexuan Cui };
1268c582c7cSDexuan Cui 
1278c582c7cSDexuan Cui #define PCI_CONFIG_MMIO_LENGTH	0x2000
1288c582c7cSDexuan Cui #define CFG_PAGE_OFFSET 0x1000
1298c582c7cSDexuan Cui #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
1308c582c7cSDexuan Cui 
1318c582c7cSDexuan Cui /*
1328c582c7cSDexuan Cui  * Message Types
1338c582c7cSDexuan Cui  */
1348c582c7cSDexuan Cui 
1358c582c7cSDexuan Cui enum pci_message_type {
1368c582c7cSDexuan Cui 	/*
1378c582c7cSDexuan Cui 	 * Version 1.1
1388c582c7cSDexuan Cui 	 */
1398c582c7cSDexuan Cui 	PCI_MESSAGE_BASE                = 0x42490000,
1408c582c7cSDexuan Cui 	PCI_BUS_RELATIONS               = PCI_MESSAGE_BASE + 0,
1418c582c7cSDexuan Cui 	PCI_QUERY_BUS_RELATIONS         = PCI_MESSAGE_BASE + 1,
1428c582c7cSDexuan Cui 	PCI_POWER_STATE_CHANGE          = PCI_MESSAGE_BASE + 4,
1438c582c7cSDexuan Cui 	PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
1448c582c7cSDexuan Cui 	PCI_QUERY_RESOURCE_RESOURCES    = PCI_MESSAGE_BASE + 6,
1458c582c7cSDexuan Cui 	PCI_BUS_D0ENTRY                 = PCI_MESSAGE_BASE + 7,
1468c582c7cSDexuan Cui 	PCI_BUS_D0EXIT                  = PCI_MESSAGE_BASE + 8,
1478c582c7cSDexuan Cui 	PCI_READ_BLOCK                  = PCI_MESSAGE_BASE + 9,
1488c582c7cSDexuan Cui 	PCI_WRITE_BLOCK                 = PCI_MESSAGE_BASE + 0xA,
1498c582c7cSDexuan Cui 	PCI_EJECT                       = PCI_MESSAGE_BASE + 0xB,
1508c582c7cSDexuan Cui 	PCI_QUERY_STOP                  = PCI_MESSAGE_BASE + 0xC,
1518c582c7cSDexuan Cui 	PCI_REENABLE                    = PCI_MESSAGE_BASE + 0xD,
1528c582c7cSDexuan Cui 	PCI_QUERY_STOP_FAILED           = PCI_MESSAGE_BASE + 0xE,
1538c582c7cSDexuan Cui 	PCI_EJECTION_COMPLETE           = PCI_MESSAGE_BASE + 0xF,
1548c582c7cSDexuan Cui 	PCI_RESOURCES_ASSIGNED          = PCI_MESSAGE_BASE + 0x10,
1558c582c7cSDexuan Cui 	PCI_RESOURCES_RELEASED          = PCI_MESSAGE_BASE + 0x11,
1568c582c7cSDexuan Cui 	PCI_INVALIDATE_BLOCK            = PCI_MESSAGE_BASE + 0x12,
1578c582c7cSDexuan Cui 	PCI_QUERY_PROTOCOL_VERSION      = PCI_MESSAGE_BASE + 0x13,
1588c582c7cSDexuan Cui 	PCI_CREATE_INTERRUPT_MESSAGE    = PCI_MESSAGE_BASE + 0x14,
1598c582c7cSDexuan Cui 	PCI_DELETE_INTERRUPT_MESSAGE    = PCI_MESSAGE_BASE + 0x15,
1608c582c7cSDexuan Cui 	PCI_MESSAGE_MAXIMUM
1618c582c7cSDexuan Cui };
1628c582c7cSDexuan Cui 
1638c582c7cSDexuan Cui /*
1648c582c7cSDexuan Cui  * Structures defining the virtual PCI Express protocol.
1658c582c7cSDexuan Cui  */
1668c582c7cSDexuan Cui 
1678c582c7cSDexuan Cui union pci_version {
1688c582c7cSDexuan Cui 	struct {
1698c582c7cSDexuan Cui 		uint16_t minor_version;
1708c582c7cSDexuan Cui 		uint16_t major_version;
1718c582c7cSDexuan Cui 	} parts;
1728c582c7cSDexuan Cui 	uint32_t version;
1738c582c7cSDexuan Cui } __packed;
1748c582c7cSDexuan Cui 
1758c582c7cSDexuan Cui /*
1768c582c7cSDexuan Cui  * This representation is the one used in Windows, which is
1778c582c7cSDexuan Cui  * what is expected when sending this back and forth with
1788c582c7cSDexuan Cui  * the Hyper-V parent partition.
1798c582c7cSDexuan Cui  */
1808c582c7cSDexuan Cui union win_slot_encoding {
1818c582c7cSDexuan Cui 	struct {
1828c582c7cSDexuan Cui 		uint32_t	slot:5;
1838c582c7cSDexuan Cui 		uint32_t	func:3;
1848c582c7cSDexuan Cui 		uint32_t	reserved:24;
1858c582c7cSDexuan Cui 	} bits;
1868c582c7cSDexuan Cui 	uint32_t val;
1878c582c7cSDexuan Cui } __packed;
1888c582c7cSDexuan Cui 
1898c582c7cSDexuan Cui struct pci_func_desc {
1908c582c7cSDexuan Cui 	uint16_t	v_id;	/* vendor ID */
1918c582c7cSDexuan Cui 	uint16_t	d_id;	/* device ID */
1928c582c7cSDexuan Cui 	uint8_t		rev;
1938c582c7cSDexuan Cui 	uint8_t		prog_intf;
1948c582c7cSDexuan Cui 	uint8_t		subclass;
1958c582c7cSDexuan Cui 	uint8_t		base_class;
1968c582c7cSDexuan Cui 	uint32_t	subsystem_id;
1978c582c7cSDexuan Cui 	union win_slot_encoding wslot;
1988c582c7cSDexuan Cui 	uint32_t	ser;	/* serial number */
1998c582c7cSDexuan Cui } __packed;
2008c582c7cSDexuan Cui 
2018c582c7cSDexuan Cui struct hv_msi_desc {
2028c582c7cSDexuan Cui 	uint8_t		vector;
2038c582c7cSDexuan Cui 	uint8_t		delivery_mode;
2048c582c7cSDexuan Cui 	uint16_t	vector_count;
2058c582c7cSDexuan Cui 	uint32_t	reserved;
2068c582c7cSDexuan Cui 	uint64_t	cpu_mask;
2078c582c7cSDexuan Cui } __packed;
2088c582c7cSDexuan Cui 
2098c582c7cSDexuan Cui struct tran_int_desc {
2108c582c7cSDexuan Cui 	uint16_t	reserved;
2118c582c7cSDexuan Cui 	uint16_t	vector_count;
2128c582c7cSDexuan Cui 	uint32_t	data;
2138c582c7cSDexuan Cui 	uint64_t	address;
2148c582c7cSDexuan Cui } __packed;
2158c582c7cSDexuan Cui 
2168c582c7cSDexuan Cui struct pci_message {
2178c582c7cSDexuan Cui 	uint32_t type;
2188c582c7cSDexuan Cui } __packed;
2198c582c7cSDexuan Cui 
2208c582c7cSDexuan Cui struct pci_child_message {
2218c582c7cSDexuan Cui 	struct pci_message message_type;
2228c582c7cSDexuan Cui 	union win_slot_encoding wslot;
2238c582c7cSDexuan Cui } __packed;
2248c582c7cSDexuan Cui 
2258c582c7cSDexuan Cui struct pci_incoming_message {
2268c582c7cSDexuan Cui 	struct vmbus_chanpkt_hdr hdr;
2278c582c7cSDexuan Cui 	struct pci_message message_type;
2288c582c7cSDexuan Cui } __packed;
2298c582c7cSDexuan Cui 
2308c582c7cSDexuan Cui struct pci_response {
2318c582c7cSDexuan Cui 	struct vmbus_chanpkt_hdr hdr;
2328c582c7cSDexuan Cui 	int32_t status;	/* negative values are failures */
2338c582c7cSDexuan Cui } __packed;
2348c582c7cSDexuan Cui 
2358c582c7cSDexuan Cui struct pci_packet {
2368c582c7cSDexuan Cui 	void (*completion_func)(void *context, struct pci_response *resp,
2378c582c7cSDexuan Cui 	    int resp_packet_size);
2388c582c7cSDexuan Cui 	void *compl_ctxt;
2398c582c7cSDexuan Cui 
2408c582c7cSDexuan Cui 	struct pci_message message[0];
2418c582c7cSDexuan Cui };
2428c582c7cSDexuan Cui 
2438c582c7cSDexuan Cui /*
2448c582c7cSDexuan Cui  * Specific message types supporting the PCI protocol.
2458c582c7cSDexuan Cui  */
2468c582c7cSDexuan Cui 
2478c582c7cSDexuan Cui struct pci_version_request {
2488c582c7cSDexuan Cui 	struct pci_message message_type;
2498c582c7cSDexuan Cui 	uint32_t protocol_version;
2508c582c7cSDexuan Cui 	uint32_t is_last_attempt:1;
2518c582c7cSDexuan Cui 	uint32_t reservedz:31;
2528c582c7cSDexuan Cui } __packed;
2538c582c7cSDexuan Cui 
2548c582c7cSDexuan Cui struct pci_bus_d0_entry {
2558c582c7cSDexuan Cui 	struct pci_message message_type;
2568c582c7cSDexuan Cui 	uint32_t reserved;
2578c582c7cSDexuan Cui 	uint64_t mmio_base;
2588c582c7cSDexuan Cui } __packed;
2598c582c7cSDexuan Cui 
2608c582c7cSDexuan Cui struct pci_bus_relations {
2618c582c7cSDexuan Cui 	struct pci_incoming_message incoming;
2628c582c7cSDexuan Cui 	uint32_t device_count;
2638c582c7cSDexuan Cui 	struct pci_func_desc func[0];
2648c582c7cSDexuan Cui } __packed;
2658c582c7cSDexuan Cui 
2668c582c7cSDexuan Cui #define MAX_NUM_BARS	(PCIR_MAX_BAR_0 + 1)
2678c582c7cSDexuan Cui struct pci_q_res_req_response {
2688c582c7cSDexuan Cui 	struct vmbus_chanpkt_hdr hdr;
2698c582c7cSDexuan Cui 	int32_t status; /* negative values are failures */
2708c582c7cSDexuan Cui 	uint32_t probed_bar[MAX_NUM_BARS];
2718c582c7cSDexuan Cui } __packed;
2728c582c7cSDexuan Cui 
2738c582c7cSDexuan Cui struct pci_resources_assigned {
2748c582c7cSDexuan Cui 	struct pci_message message_type;
2758c582c7cSDexuan Cui 	union win_slot_encoding wslot;
2768c582c7cSDexuan Cui 	uint8_t memory_range[0x14][MAX_NUM_BARS]; /* unused here */
2778c582c7cSDexuan Cui 	uint32_t msi_descriptors;
2788c582c7cSDexuan Cui 	uint32_t reserved[4];
2798c582c7cSDexuan Cui } __packed;
2808c582c7cSDexuan Cui 
2818c582c7cSDexuan Cui struct pci_create_interrupt {
2828c582c7cSDexuan Cui 	struct pci_message message_type;
2838c582c7cSDexuan Cui 	union win_slot_encoding wslot;
2848c582c7cSDexuan Cui 	struct hv_msi_desc int_desc;
2858c582c7cSDexuan Cui } __packed;
2868c582c7cSDexuan Cui 
2878c582c7cSDexuan Cui struct pci_create_int_response {
2888c582c7cSDexuan Cui 	struct pci_response response;
2898c582c7cSDexuan Cui 	uint32_t reserved;
2908c582c7cSDexuan Cui 	struct tran_int_desc int_desc;
2918c582c7cSDexuan Cui } __packed;
2928c582c7cSDexuan Cui 
2938c582c7cSDexuan Cui struct pci_delete_interrupt {
2948c582c7cSDexuan Cui 	struct pci_message message_type;
2958c582c7cSDexuan Cui 	union win_slot_encoding wslot;
2968c582c7cSDexuan Cui 	struct tran_int_desc int_desc;
2978c582c7cSDexuan Cui } __packed;
2988c582c7cSDexuan Cui 
2998c582c7cSDexuan Cui struct pci_dev_incoming {
3008c582c7cSDexuan Cui 	struct pci_incoming_message incoming;
3018c582c7cSDexuan Cui 	union win_slot_encoding wslot;
3028c582c7cSDexuan Cui } __packed;
3038c582c7cSDexuan Cui 
3048c582c7cSDexuan Cui struct pci_eject_response {
3058c582c7cSDexuan Cui 	struct pci_message message_type;
3068c582c7cSDexuan Cui 	union win_slot_encoding wslot;
3078c582c7cSDexuan Cui 	uint32_t status;
3088c582c7cSDexuan Cui } __packed;
3098c582c7cSDexuan Cui 
3108c582c7cSDexuan Cui /*
3118c582c7cSDexuan Cui  * Driver specific state.
3128c582c7cSDexuan Cui  */
3138c582c7cSDexuan Cui 
3148c582c7cSDexuan Cui enum hv_pcibus_state {
3158c582c7cSDexuan Cui 	hv_pcibus_init = 0,
3168c582c7cSDexuan Cui 	hv_pcibus_installed,
3178c582c7cSDexuan Cui };
3188c582c7cSDexuan Cui 
3198c582c7cSDexuan Cui struct hv_pcibus {
3208c582c7cSDexuan Cui 	device_t pcib;
3218c582c7cSDexuan Cui 	device_t pci_bus;
3228c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc;
3238c582c7cSDexuan Cui 
3248c582c7cSDexuan Cui 	uint16_t pci_domain;
3258c582c7cSDexuan Cui 
3268c582c7cSDexuan Cui 	enum hv_pcibus_state state;
3278c582c7cSDexuan Cui 
3288c582c7cSDexuan Cui 	struct resource *cfg_res;
3298c582c7cSDexuan Cui 
3308c582c7cSDexuan Cui 	struct completion query_completion, *query_comp;
3318c582c7cSDexuan Cui 
3328c582c7cSDexuan Cui 	struct mtx config_lock; /* Avoid two threads writing index page */
3338c582c7cSDexuan Cui 	struct mtx device_list_lock;    /* Protect lists below */
3348c582c7cSDexuan Cui 	TAILQ_HEAD(, hv_pci_dev) children;
3358c582c7cSDexuan Cui 	TAILQ_HEAD(, hv_dr_state) dr_list;
3368c582c7cSDexuan Cui 
3378c582c7cSDexuan Cui 	volatile int detaching;
3388c582c7cSDexuan Cui };
3398c582c7cSDexuan Cui 
3408c582c7cSDexuan Cui struct hv_pci_dev {
3418c582c7cSDexuan Cui 	TAILQ_ENTRY(hv_pci_dev) link;
3428c582c7cSDexuan Cui 
3438c582c7cSDexuan Cui 	struct pci_func_desc desc;
3448c582c7cSDexuan Cui 
3458c582c7cSDexuan Cui 	bool reported_missing;
3468c582c7cSDexuan Cui 
3478c582c7cSDexuan Cui 	struct hv_pcibus *hbus;
3488c582c7cSDexuan Cui 	struct task eject_task;
3498c582c7cSDexuan Cui 
3508c582c7cSDexuan Cui 	TAILQ_HEAD(, hv_irq_desc) irq_desc_list;
3518c582c7cSDexuan Cui 
3528c582c7cSDexuan Cui 	/*
3538c582c7cSDexuan Cui 	 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
3548c582c7cSDexuan Cui 	 * read it back, for each of the BAR offsets within config space.
3558c582c7cSDexuan Cui 	 */
3568c582c7cSDexuan Cui 	uint32_t probed_bar[MAX_NUM_BARS];
3578c582c7cSDexuan Cui };
3588c582c7cSDexuan Cui 
3598c582c7cSDexuan Cui /*
3608c582c7cSDexuan Cui  * Tracks "Device Relations" messages from the host, which must be both
3618c582c7cSDexuan Cui  * processed in order.
3628c582c7cSDexuan Cui  */
3638c582c7cSDexuan Cui struct hv_dr_work {
3648c582c7cSDexuan Cui 	struct task task;
3658c582c7cSDexuan Cui 	struct hv_pcibus *bus;
3668c582c7cSDexuan Cui };
3678c582c7cSDexuan Cui 
3688c582c7cSDexuan Cui struct hv_dr_state {
3698c582c7cSDexuan Cui 	TAILQ_ENTRY(hv_dr_state) link;
3708c582c7cSDexuan Cui 	uint32_t device_count;
3718c582c7cSDexuan Cui 	struct pci_func_desc func[0];
3728c582c7cSDexuan Cui };
3738c582c7cSDexuan Cui 
3748c582c7cSDexuan Cui struct hv_irq_desc {
3758c582c7cSDexuan Cui 	TAILQ_ENTRY(hv_irq_desc) link;
3768c582c7cSDexuan Cui 	struct tran_int_desc desc;
3778c582c7cSDexuan Cui 	int irq;
3788c582c7cSDexuan Cui };
3798c582c7cSDexuan Cui 
3808c582c7cSDexuan Cui #define PCI_DEVFN(slot, func)   ((((slot) & 0x1f) << 3) | ((func) & 0x07))
3818c582c7cSDexuan Cui #define PCI_SLOT(devfn)         (((devfn) >> 3) & 0x1f)
3828c582c7cSDexuan Cui #define PCI_FUNC(devfn)         ((devfn) & 0x07)
3838c582c7cSDexuan Cui 
3848c582c7cSDexuan Cui static uint32_t
3858c582c7cSDexuan Cui devfn_to_wslot(unsigned int devfn)
3868c582c7cSDexuan Cui {
3878c582c7cSDexuan Cui 	union win_slot_encoding wslot;
3888c582c7cSDexuan Cui 
3898c582c7cSDexuan Cui 	wslot.val = 0;
3908c582c7cSDexuan Cui 	wslot.bits.slot = PCI_SLOT(devfn);
3918c582c7cSDexuan Cui 	wslot.bits.func = PCI_FUNC(devfn);
3928c582c7cSDexuan Cui 
3938c582c7cSDexuan Cui 	return (wslot.val);
3948c582c7cSDexuan Cui }
3958c582c7cSDexuan Cui 
3968c582c7cSDexuan Cui static unsigned int
3978c582c7cSDexuan Cui wslot_to_devfn(uint32_t wslot)
3988c582c7cSDexuan Cui {
3998c582c7cSDexuan Cui 	union win_slot_encoding encoding;
4008c582c7cSDexuan Cui 	unsigned int slot;
4018c582c7cSDexuan Cui 	unsigned int func;
4028c582c7cSDexuan Cui 
4038c582c7cSDexuan Cui 	encoding.val = wslot;
4048c582c7cSDexuan Cui 
4058c582c7cSDexuan Cui 	slot = encoding.bits.slot;
4068c582c7cSDexuan Cui 	func = encoding.bits.func;
4078c582c7cSDexuan Cui 
4088c582c7cSDexuan Cui 	return (PCI_DEVFN(slot, func));
4098c582c7cSDexuan Cui }
4108c582c7cSDexuan Cui 
4118c582c7cSDexuan Cui struct vmbus_pcib_softc {
4128c582c7cSDexuan Cui 	struct vmbus_channel	*chan;
4138c582c7cSDexuan Cui 	void *rx_buf;
4148c582c7cSDexuan Cui 
4158c582c7cSDexuan Cui 	struct taskqueue	*taskq;
4168c582c7cSDexuan Cui 
4178c582c7cSDexuan Cui 	struct hv_pcibus	*hbus;
4188c582c7cSDexuan Cui };
4198c582c7cSDexuan Cui 
4208c582c7cSDexuan Cui /* {44C4F61D-4444-4400-9D52-802E27EDE19F} */
4218c582c7cSDexuan Cui static const struct hyperv_guid g_pass_through_dev_type = {
4228c582c7cSDexuan Cui 	.hv_guid = {0x1D, 0xF6, 0xC4, 0x44, 0x44, 0x44, 0x00, 0x44,
4238c582c7cSDexuan Cui 	    0x9D, 0x52, 0x80, 0x2E, 0x27, 0xED, 0xE1, 0x9F}
4248c582c7cSDexuan Cui };
4258c582c7cSDexuan Cui 
4268c582c7cSDexuan Cui struct hv_pci_compl {
4278c582c7cSDexuan Cui 	struct completion host_event;
4288c582c7cSDexuan Cui 	int32_t completion_status;
4298c582c7cSDexuan Cui };
4308c582c7cSDexuan Cui 
4318c582c7cSDexuan Cui struct q_res_req_compl {
4328c582c7cSDexuan Cui 	struct completion host_event;
4338c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
4348c582c7cSDexuan Cui };
4358c582c7cSDexuan Cui 
4368c582c7cSDexuan Cui struct compose_comp_ctxt {
4378c582c7cSDexuan Cui 	struct hv_pci_compl comp_pkt;
4388c582c7cSDexuan Cui 	struct tran_int_desc int_desc;
4398c582c7cSDexuan Cui };
4408c582c7cSDexuan Cui 
4418c582c7cSDexuan Cui static void
4428c582c7cSDexuan Cui hv_pci_generic_compl(void *context, struct pci_response *resp,
4438c582c7cSDexuan Cui     int resp_packet_size)
4448c582c7cSDexuan Cui {
4458c582c7cSDexuan Cui 	struct hv_pci_compl *comp_pkt = context;
4468c582c7cSDexuan Cui 
4478c582c7cSDexuan Cui 	if (resp_packet_size >= sizeof(struct pci_response))
4488c582c7cSDexuan Cui 		comp_pkt->completion_status = resp->status;
4498c582c7cSDexuan Cui 	else
4508c582c7cSDexuan Cui 		comp_pkt->completion_status = -1;
4518c582c7cSDexuan Cui 
4528c582c7cSDexuan Cui 	complete(&comp_pkt->host_event);
4538c582c7cSDexuan Cui }
4548c582c7cSDexuan Cui 
4558c582c7cSDexuan Cui static void
4568c582c7cSDexuan Cui q_resource_requirements(void *context, struct pci_response *resp,
4578c582c7cSDexuan Cui     int resp_packet_size)
4588c582c7cSDexuan Cui {
4598c582c7cSDexuan Cui 	struct q_res_req_compl *completion = context;
4608c582c7cSDexuan Cui 	struct pci_q_res_req_response *q_res_req =
4618c582c7cSDexuan Cui 	    (struct pci_q_res_req_response *)resp;
4628c582c7cSDexuan Cui 	int i;
4638c582c7cSDexuan Cui 
4648c582c7cSDexuan Cui 	if (resp->status < 0) {
4658c582c7cSDexuan Cui 		printf("vmbus_pcib: failed to query resource requirements\n");
4668c582c7cSDexuan Cui 	} else {
4678c582c7cSDexuan Cui 		for (i = 0; i < MAX_NUM_BARS; i++)
4688c582c7cSDexuan Cui 			completion->hpdev->probed_bar[i] =
4698c582c7cSDexuan Cui 			    q_res_req->probed_bar[i];
4708c582c7cSDexuan Cui 	}
4718c582c7cSDexuan Cui 
4728c582c7cSDexuan Cui 	complete(&completion->host_event);
4738c582c7cSDexuan Cui }
4748c582c7cSDexuan Cui 
4758c582c7cSDexuan Cui static void
4768c582c7cSDexuan Cui hv_pci_compose_compl(void *context, struct pci_response *resp,
4778c582c7cSDexuan Cui     int resp_packet_size)
4788c582c7cSDexuan Cui {
4798c582c7cSDexuan Cui 	struct compose_comp_ctxt *comp_pkt = context;
4808c582c7cSDexuan Cui 	struct pci_create_int_response *int_resp =
4818c582c7cSDexuan Cui 	    (struct pci_create_int_response *)resp;
4828c582c7cSDexuan Cui 
4838c582c7cSDexuan Cui 	comp_pkt->comp_pkt.completion_status = resp->status;
4848c582c7cSDexuan Cui 	comp_pkt->int_desc = int_resp->int_desc;
4858c582c7cSDexuan Cui 	complete(&comp_pkt->comp_pkt.host_event);
4868c582c7cSDexuan Cui }
4878c582c7cSDexuan Cui 
4888c582c7cSDexuan Cui static void
4898c582c7cSDexuan Cui hv_int_desc_free(struct hv_pci_dev *hpdev, struct hv_irq_desc *hid)
4908c582c7cSDexuan Cui {
4918c582c7cSDexuan Cui 	struct pci_delete_interrupt *int_pkt;
4928c582c7cSDexuan Cui 	struct {
4938c582c7cSDexuan Cui 		struct pci_packet pkt;
4948c582c7cSDexuan Cui 		uint8_t buffer[sizeof(struct pci_delete_interrupt)];
4958c582c7cSDexuan Cui 	} ctxt;
4968c582c7cSDexuan Cui 
4978c582c7cSDexuan Cui 	memset(&ctxt, 0, sizeof(ctxt));
4988c582c7cSDexuan Cui 	int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
4998c582c7cSDexuan Cui 	int_pkt->message_type.type = PCI_DELETE_INTERRUPT_MESSAGE;
5008c582c7cSDexuan Cui 	int_pkt->wslot.val = hpdev->desc.wslot.val;
5018c582c7cSDexuan Cui 	int_pkt->int_desc = hid->desc;
5028c582c7cSDexuan Cui 
5038c582c7cSDexuan Cui 	vmbus_chan_send(hpdev->hbus->sc->chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
5048c582c7cSDexuan Cui 	    int_pkt, sizeof(*int_pkt), 0);
5058c582c7cSDexuan Cui 
5068c582c7cSDexuan Cui 	free(hid, M_DEVBUF);
5078c582c7cSDexuan Cui }
5088c582c7cSDexuan Cui 
5098c582c7cSDexuan Cui static void
5108c582c7cSDexuan Cui hv_pci_delete_device(struct hv_pci_dev *hpdev)
5118c582c7cSDexuan Cui {
5128c582c7cSDexuan Cui 	struct hv_pcibus *hbus = hpdev->hbus;
5138c582c7cSDexuan Cui 	struct hv_irq_desc *hid, *tmp_hid;
5148c582c7cSDexuan Cui 	device_t pci_dev;
5158c582c7cSDexuan Cui 	int devfn;
5168c582c7cSDexuan Cui 
5178c582c7cSDexuan Cui 	devfn = wslot_to_devfn(hpdev->desc.wslot.val);
5188c582c7cSDexuan Cui 
5198c582c7cSDexuan Cui 	mtx_lock(&Giant);
5208c582c7cSDexuan Cui 
5218c582c7cSDexuan Cui 	pci_dev = pci_find_dbsf(hbus->pci_domain,
5228c582c7cSDexuan Cui 	    0, PCI_SLOT(devfn), PCI_FUNC(devfn));
5238c582c7cSDexuan Cui 	if (pci_dev)
5248c582c7cSDexuan Cui 		device_delete_child(hbus->pci_bus, pci_dev);
5258c582c7cSDexuan Cui 
5268c582c7cSDexuan Cui 	mtx_unlock(&Giant);
5278c582c7cSDexuan Cui 
5288c582c7cSDexuan Cui 	mtx_lock(&hbus->device_list_lock);
5298c582c7cSDexuan Cui 	TAILQ_REMOVE(&hbus->children, hpdev, link);
5308c582c7cSDexuan Cui 	mtx_unlock(&hbus->device_list_lock);
5318c582c7cSDexuan Cui 
5328c582c7cSDexuan Cui 	TAILQ_FOREACH_SAFE(hid, &hpdev->irq_desc_list, link, tmp_hid)
5338c582c7cSDexuan Cui 		hv_int_desc_free(hpdev, hid);
5348c582c7cSDexuan Cui 
5358c582c7cSDexuan Cui 	free(hpdev, M_DEVBUF);
5368c582c7cSDexuan Cui }
5378c582c7cSDexuan Cui 
5388c582c7cSDexuan Cui static struct hv_pci_dev *
5398c582c7cSDexuan Cui new_pcichild_device(struct hv_pcibus *hbus, struct pci_func_desc *desc)
5408c582c7cSDexuan Cui {
5418c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
5428c582c7cSDexuan Cui 	struct pci_child_message *res_req;
5438c582c7cSDexuan Cui 	struct q_res_req_compl comp_pkt;
5448c582c7cSDexuan Cui 	struct {
5458c582c7cSDexuan Cui 		struct pci_packet pkt;
5468c582c7cSDexuan Cui 		uint8_t buffer[sizeof(struct pci_child_message)];
5478c582c7cSDexuan Cui 	} ctxt;
5488c582c7cSDexuan Cui 	int ret;
5498c582c7cSDexuan Cui 
5508c582c7cSDexuan Cui 	hpdev = malloc(sizeof(*hpdev), M_DEVBUF, M_WAITOK | M_ZERO);
5518c582c7cSDexuan Cui 	hpdev->hbus = hbus;
5528c582c7cSDexuan Cui 
5538c582c7cSDexuan Cui 	TAILQ_INIT(&hpdev->irq_desc_list);
5548c582c7cSDexuan Cui 
5558c582c7cSDexuan Cui 	init_completion(&comp_pkt.host_event);
5568c582c7cSDexuan Cui 	comp_pkt.hpdev = hpdev;
5578c582c7cSDexuan Cui 
5588c582c7cSDexuan Cui 	ctxt.pkt.compl_ctxt = &comp_pkt;
5598c582c7cSDexuan Cui 	ctxt.pkt.completion_func = q_resource_requirements;
5608c582c7cSDexuan Cui 
5618c582c7cSDexuan Cui 	res_req = (struct pci_child_message *)&ctxt.pkt.message;
5628c582c7cSDexuan Cui 	res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
5638c582c7cSDexuan Cui 	res_req->wslot.val = desc->wslot.val;
5648c582c7cSDexuan Cui 
5658c582c7cSDexuan Cui 	ret = vmbus_chan_send(hbus->sc->chan,
5668c582c7cSDexuan Cui 	    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
5678c582c7cSDexuan Cui 	    res_req, sizeof(*res_req), (uint64_t)&ctxt.pkt);
5688c582c7cSDexuan Cui 	if (ret)
5698c582c7cSDexuan Cui 		goto err;
5708c582c7cSDexuan Cui 
5718c582c7cSDexuan Cui 	wait_for_completion(&comp_pkt.host_event);
5728c582c7cSDexuan Cui 	free_completion(&comp_pkt.host_event);
5738c582c7cSDexuan Cui 
5748c582c7cSDexuan Cui 	hpdev->desc = *desc;
5758c582c7cSDexuan Cui 
5768c582c7cSDexuan Cui 	mtx_lock(&hbus->device_list_lock);
5776944b2e6SDexuan Cui 	if (TAILQ_EMPTY(&hbus->children))
5786944b2e6SDexuan Cui 		hbus->pci_domain = desc->ser & 0xFFFF;
5798c582c7cSDexuan Cui 	TAILQ_INSERT_TAIL(&hbus->children, hpdev, link);
5808c582c7cSDexuan Cui 	mtx_unlock(&hbus->device_list_lock);
5818c582c7cSDexuan Cui 	return (hpdev);
5828c582c7cSDexuan Cui err:
5838c582c7cSDexuan Cui 	free_completion(&comp_pkt.host_event);
5848c582c7cSDexuan Cui 	free(hpdev, M_DEVBUF);
5858c582c7cSDexuan Cui 	return (NULL);
5868c582c7cSDexuan Cui }
5878c582c7cSDexuan Cui 
5888c582c7cSDexuan Cui #if __FreeBSD_version < 1100000
5898c582c7cSDexuan Cui 
5908c582c7cSDexuan Cui /* Old versions don't have BUS_RESCAN(). Let's copy it from FreeBSD 11. */
5918c582c7cSDexuan Cui 
5928c582c7cSDexuan Cui static struct pci_devinfo *
5938c582c7cSDexuan Cui pci_identify_function(device_t pcib, device_t dev, int domain, int busno,
5948c582c7cSDexuan Cui     int slot, int func, size_t dinfo_size)
5958c582c7cSDexuan Cui {
5968c582c7cSDexuan Cui 	struct pci_devinfo *dinfo;
5978c582c7cSDexuan Cui 
5988c582c7cSDexuan Cui 	dinfo = pci_read_device(pcib, domain, busno, slot, func, dinfo_size);
5998c582c7cSDexuan Cui 	if (dinfo != NULL)
6008c582c7cSDexuan Cui 		pci_add_child(dev, dinfo);
6018c582c7cSDexuan Cui 
6028c582c7cSDexuan Cui 	return (dinfo);
6038c582c7cSDexuan Cui }
6048c582c7cSDexuan Cui 
6058c582c7cSDexuan Cui static int
6068c582c7cSDexuan Cui pci_rescan(device_t dev)
6078c582c7cSDexuan Cui {
6088c582c7cSDexuan Cui #define	REG(n, w)	PCIB_READ_CONFIG(pcib, busno, s, f, n, w)
6098c582c7cSDexuan Cui 	device_t pcib = device_get_parent(dev);
6108c582c7cSDexuan Cui 	struct pci_softc *sc;
6118c582c7cSDexuan Cui 	device_t child, *devlist, *unchanged;
6128c582c7cSDexuan Cui 	int devcount, error, i, j, maxslots, oldcount;
6138c582c7cSDexuan Cui 	int busno, domain, s, f, pcifunchigh;
6148c582c7cSDexuan Cui 	uint8_t hdrtype;
6158c582c7cSDexuan Cui 
6168c582c7cSDexuan Cui 	/* No need to check for ARI on a rescan. */
6178c582c7cSDexuan Cui 	error = device_get_children(dev, &devlist, &devcount);
6188c582c7cSDexuan Cui 	if (error)
6198c582c7cSDexuan Cui 		return (error);
6208c582c7cSDexuan Cui 	if (devcount != 0) {
6218c582c7cSDexuan Cui 		unchanged = malloc(devcount * sizeof(device_t), M_TEMP,
6228c582c7cSDexuan Cui 		    M_NOWAIT | M_ZERO);
6238c582c7cSDexuan Cui 		if (unchanged == NULL) {
6248c582c7cSDexuan Cui 			free(devlist, M_TEMP);
6258c582c7cSDexuan Cui 			return (ENOMEM);
6268c582c7cSDexuan Cui 		}
6278c582c7cSDexuan Cui 	} else
6288c582c7cSDexuan Cui 		unchanged = NULL;
6298c582c7cSDexuan Cui 
6308c582c7cSDexuan Cui 	sc = device_get_softc(dev);
6318c582c7cSDexuan Cui 	domain = pcib_get_domain(dev);
6328c582c7cSDexuan Cui 	busno = pcib_get_bus(dev);
6338c582c7cSDexuan Cui 	maxslots = PCIB_MAXSLOTS(pcib);
6348c582c7cSDexuan Cui 	for (s = 0; s <= maxslots; s++) {
6358c582c7cSDexuan Cui 		/* If function 0 is not present, skip to the next slot. */
6368c582c7cSDexuan Cui 		f = 0;
6378c582c7cSDexuan Cui 		if (REG(PCIR_VENDOR, 2) == 0xffff)
6388c582c7cSDexuan Cui 			continue;
6398c582c7cSDexuan Cui 		pcifunchigh = 0;
6408c582c7cSDexuan Cui 		hdrtype = REG(PCIR_HDRTYPE, 1);
6418c582c7cSDexuan Cui 		if ((hdrtype & PCIM_HDRTYPE) > PCI_MAXHDRTYPE)
6428c582c7cSDexuan Cui 			continue;
6438c582c7cSDexuan Cui 		if (hdrtype & PCIM_MFDEV)
6448c582c7cSDexuan Cui 			pcifunchigh = PCIB_MAXFUNCS(pcib);
6458c582c7cSDexuan Cui 		for (f = 0; f <= pcifunchigh; f++) {
6468c582c7cSDexuan Cui 			if (REG(PCIR_VENDOR, 2) == 0xffff)
6478c582c7cSDexuan Cui 				continue;
6488c582c7cSDexuan Cui 
6498c582c7cSDexuan Cui 			/*
6508c582c7cSDexuan Cui 			 * Found a valid function.  Check if a
6518c582c7cSDexuan Cui 			 * device_t for this device already exists.
6528c582c7cSDexuan Cui 			 */
6538c582c7cSDexuan Cui 			for (i = 0; i < devcount; i++) {
6548c582c7cSDexuan Cui 				child = devlist[i];
6558c582c7cSDexuan Cui 				if (child == NULL)
6568c582c7cSDexuan Cui 					continue;
6578c582c7cSDexuan Cui 				if (pci_get_slot(child) == s &&
6588c582c7cSDexuan Cui 				    pci_get_function(child) == f) {
6598c582c7cSDexuan Cui 					unchanged[i] = child;
6608c582c7cSDexuan Cui 					goto next_func;
6618c582c7cSDexuan Cui 				}
6628c582c7cSDexuan Cui 			}
6638c582c7cSDexuan Cui 
6648c582c7cSDexuan Cui 			pci_identify_function(pcib, dev, domain, busno, s, f,
6658c582c7cSDexuan Cui 			    sizeof(struct pci_devinfo));
6668c582c7cSDexuan Cui 		next_func:;
6678c582c7cSDexuan Cui 		}
6688c582c7cSDexuan Cui 	}
6698c582c7cSDexuan Cui 
6708c582c7cSDexuan Cui 	/* Remove devices that are no longer present. */
6718c582c7cSDexuan Cui 	for (i = 0; i < devcount; i++) {
6728c582c7cSDexuan Cui 		if (unchanged[i] != NULL)
6738c582c7cSDexuan Cui 			continue;
6748c582c7cSDexuan Cui 		device_delete_child(dev, devlist[i]);
6758c582c7cSDexuan Cui 	}
6768c582c7cSDexuan Cui 
6778c582c7cSDexuan Cui 	free(devlist, M_TEMP);
6788c582c7cSDexuan Cui 	oldcount = devcount;
6798c582c7cSDexuan Cui 
6808c582c7cSDexuan Cui 	/* Try to attach the devices just added. */
6818c582c7cSDexuan Cui 	error = device_get_children(dev, &devlist, &devcount);
6828c582c7cSDexuan Cui 	if (error) {
6838c582c7cSDexuan Cui 		free(unchanged, M_TEMP);
6848c582c7cSDexuan Cui 		return (error);
6858c582c7cSDexuan Cui 	}
6868c582c7cSDexuan Cui 
6878c582c7cSDexuan Cui 	for (i = 0; i < devcount; i++) {
6888c582c7cSDexuan Cui 		for (j = 0; j < oldcount; j++) {
6898c582c7cSDexuan Cui 			if (devlist[i] == unchanged[j])
6908c582c7cSDexuan Cui 				goto next_device;
6918c582c7cSDexuan Cui 		}
6928c582c7cSDexuan Cui 
6938c582c7cSDexuan Cui 		device_probe_and_attach(devlist[i]);
6948c582c7cSDexuan Cui 	next_device:;
6958c582c7cSDexuan Cui 	}
6968c582c7cSDexuan Cui 
6978c582c7cSDexuan Cui 	free(unchanged, M_TEMP);
6988c582c7cSDexuan Cui 	free(devlist, M_TEMP);
6998c582c7cSDexuan Cui 	return (0);
7008c582c7cSDexuan Cui #undef REG
7018c582c7cSDexuan Cui }
7028c582c7cSDexuan Cui 
7038c582c7cSDexuan Cui #else
7048c582c7cSDexuan Cui 
7058c582c7cSDexuan Cui static int
7068c582c7cSDexuan Cui pci_rescan(device_t dev)
7078c582c7cSDexuan Cui {
7088c582c7cSDexuan Cui 	return (BUS_RESCAN(dev));
7098c582c7cSDexuan Cui }
7108c582c7cSDexuan Cui 
7118c582c7cSDexuan Cui #endif
7128c582c7cSDexuan Cui 
7138c582c7cSDexuan Cui static void
7148c582c7cSDexuan Cui pci_devices_present_work(void *arg, int pending __unused)
7158c582c7cSDexuan Cui {
7168c582c7cSDexuan Cui 	struct hv_dr_work *dr_wrk = arg;
7178c582c7cSDexuan Cui 	struct hv_dr_state *dr = NULL;
7188c582c7cSDexuan Cui 	struct hv_pcibus *hbus;
7198c582c7cSDexuan Cui 	uint32_t child_no;
7208c582c7cSDexuan Cui 	bool found;
7218c582c7cSDexuan Cui 	struct pci_func_desc *new_desc;
7228c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev, *tmp_hpdev;
7238c582c7cSDexuan Cui 	struct completion *query_comp;
7248c582c7cSDexuan Cui 	bool need_rescan = false;
7258c582c7cSDexuan Cui 
7268c582c7cSDexuan Cui 	hbus = dr_wrk->bus;
7278c582c7cSDexuan Cui 	free(dr_wrk, M_DEVBUF);
7288c582c7cSDexuan Cui 
7298c582c7cSDexuan Cui 	/* Pull this off the queue and process it if it was the last one. */
7308c582c7cSDexuan Cui 	mtx_lock(&hbus->device_list_lock);
7318c582c7cSDexuan Cui 	while (!TAILQ_EMPTY(&hbus->dr_list)) {
7328c582c7cSDexuan Cui 		dr = TAILQ_FIRST(&hbus->dr_list);
7338c582c7cSDexuan Cui 		TAILQ_REMOVE(&hbus->dr_list, dr, link);
7348c582c7cSDexuan Cui 
7358c582c7cSDexuan Cui 		/* Throw this away if the list still has stuff in it. */
7368c582c7cSDexuan Cui 		if (!TAILQ_EMPTY(&hbus->dr_list)) {
7378c582c7cSDexuan Cui 			free(dr, M_DEVBUF);
7388c582c7cSDexuan Cui 			continue;
7398c582c7cSDexuan Cui 		}
7408c582c7cSDexuan Cui 	}
7418c582c7cSDexuan Cui 	mtx_unlock(&hbus->device_list_lock);
7428c582c7cSDexuan Cui 
7438c582c7cSDexuan Cui 	if (!dr)
7448c582c7cSDexuan Cui 		return;
7458c582c7cSDexuan Cui 
7468c582c7cSDexuan Cui 	/* First, mark all existing children as reported missing. */
7478c582c7cSDexuan Cui 	mtx_lock(&hbus->device_list_lock);
7488c582c7cSDexuan Cui 	TAILQ_FOREACH(hpdev, &hbus->children, link)
7498c582c7cSDexuan Cui 		hpdev->reported_missing = true;
7508c582c7cSDexuan Cui 	mtx_unlock(&hbus->device_list_lock);
7518c582c7cSDexuan Cui 
7528c582c7cSDexuan Cui 	/* Next, add back any reported devices. */
7538c582c7cSDexuan Cui 	for (child_no = 0; child_no < dr->device_count; child_no++) {
7548c582c7cSDexuan Cui 		found = false;
7558c582c7cSDexuan Cui 		new_desc = &dr->func[child_no];
7568c582c7cSDexuan Cui 
7578c582c7cSDexuan Cui 		mtx_lock(&hbus->device_list_lock);
7588c582c7cSDexuan Cui 		TAILQ_FOREACH(hpdev, &hbus->children, link) {
7598c582c7cSDexuan Cui 			if ((hpdev->desc.wslot.val ==
7608c582c7cSDexuan Cui 			    new_desc->wslot.val) &&
7618c582c7cSDexuan Cui 			    (hpdev->desc.v_id == new_desc->v_id) &&
7628c582c7cSDexuan Cui 			    (hpdev->desc.d_id == new_desc->d_id) &&
7638c582c7cSDexuan Cui 			    (hpdev->desc.ser == new_desc->ser)) {
7648c582c7cSDexuan Cui 				hpdev->reported_missing = false;
7658c582c7cSDexuan Cui 				found = true;
7668c582c7cSDexuan Cui 				break;
7678c582c7cSDexuan Cui 			}
7688c582c7cSDexuan Cui 		}
7698c582c7cSDexuan Cui 		mtx_unlock(&hbus->device_list_lock);
7708c582c7cSDexuan Cui 
7718c582c7cSDexuan Cui 		if (!found) {
7728c582c7cSDexuan Cui 			if (!need_rescan)
7738c582c7cSDexuan Cui 				need_rescan = true;
7748c582c7cSDexuan Cui 
7758c582c7cSDexuan Cui 			hpdev = new_pcichild_device(hbus, new_desc);
7768c582c7cSDexuan Cui 			if (!hpdev)
7778c582c7cSDexuan Cui 				printf("vmbus_pcib: failed to add a child\n");
7788c582c7cSDexuan Cui 		}
7798c582c7cSDexuan Cui 	}
7808c582c7cSDexuan Cui 
7818c582c7cSDexuan Cui 	/* Remove missing device(s), if any */
7828c582c7cSDexuan Cui 	TAILQ_FOREACH_SAFE(hpdev, &hbus->children, link, tmp_hpdev) {
7838c582c7cSDexuan Cui 		if (hpdev->reported_missing)
7848c582c7cSDexuan Cui 			hv_pci_delete_device(hpdev);
7858c582c7cSDexuan Cui 	}
7868c582c7cSDexuan Cui 
7878c582c7cSDexuan Cui 	/* Rescan the bus to find any new device, if necessary. */
7888c582c7cSDexuan Cui 	if (hbus->state == hv_pcibus_installed && need_rescan)
7898c582c7cSDexuan Cui 		pci_rescan(hbus->pci_bus);
7908c582c7cSDexuan Cui 
7918c582c7cSDexuan Cui 	/* Wake up hv_pci_query_relations(), if it's waiting. */
7928c582c7cSDexuan Cui 	query_comp = hbus->query_comp;
7938c582c7cSDexuan Cui 	if (query_comp) {
7948c582c7cSDexuan Cui 		hbus->query_comp = NULL;
7958c582c7cSDexuan Cui 		complete(query_comp);
7968c582c7cSDexuan Cui 	}
7978c582c7cSDexuan Cui 
7988c582c7cSDexuan Cui 	free(dr, M_DEVBUF);
7998c582c7cSDexuan Cui }
8008c582c7cSDexuan Cui 
8018c582c7cSDexuan Cui static struct hv_pci_dev *
8028c582c7cSDexuan Cui get_pcichild_wslot(struct hv_pcibus *hbus, uint32_t wslot)
8038c582c7cSDexuan Cui {
8048c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev, *ret = NULL;
8058c582c7cSDexuan Cui 
8068c582c7cSDexuan Cui 	mtx_lock(&hbus->device_list_lock);
8078c582c7cSDexuan Cui 	TAILQ_FOREACH(hpdev, &hbus->children, link) {
8088c582c7cSDexuan Cui 		if (hpdev->desc.wslot.val == wslot) {
8098c582c7cSDexuan Cui 			ret = hpdev;
8108c582c7cSDexuan Cui 			break;
8118c582c7cSDexuan Cui 		}
8128c582c7cSDexuan Cui 	}
8138c582c7cSDexuan Cui 	mtx_unlock(&hbus->device_list_lock);
8148c582c7cSDexuan Cui 
8158c582c7cSDexuan Cui 	return (ret);
8168c582c7cSDexuan Cui }
8178c582c7cSDexuan Cui 
8188c582c7cSDexuan Cui static void
8198c582c7cSDexuan Cui hv_pci_devices_present(struct hv_pcibus *hbus,
8208c582c7cSDexuan Cui     struct pci_bus_relations *relations)
8218c582c7cSDexuan Cui {
8228c582c7cSDexuan Cui 	struct hv_dr_state *dr;
8238c582c7cSDexuan Cui 	struct hv_dr_work *dr_wrk;
8248c582c7cSDexuan Cui 	unsigned long dr_size;
8258c582c7cSDexuan Cui 
8268c582c7cSDexuan Cui 	if (hbus->detaching && relations->device_count > 0)
8278c582c7cSDexuan Cui 		return;
8288c582c7cSDexuan Cui 
8298c582c7cSDexuan Cui 	dr_size = offsetof(struct hv_dr_state, func) +
8308c582c7cSDexuan Cui 	    (sizeof(struct pci_func_desc) * relations->device_count);
8318c582c7cSDexuan Cui 	dr = malloc(dr_size, M_DEVBUF, M_WAITOK | M_ZERO);
8328c582c7cSDexuan Cui 
8338c582c7cSDexuan Cui 	dr->device_count = relations->device_count;
8348c582c7cSDexuan Cui 	if (dr->device_count != 0)
8358c582c7cSDexuan Cui 		memcpy(dr->func, relations->func,
8368c582c7cSDexuan Cui 		    sizeof(struct pci_func_desc) * dr->device_count);
8378c582c7cSDexuan Cui 
8388c582c7cSDexuan Cui 	mtx_lock(&hbus->device_list_lock);
8398c582c7cSDexuan Cui 	TAILQ_INSERT_TAIL(&hbus->dr_list, dr, link);
8408c582c7cSDexuan Cui 	mtx_unlock(&hbus->device_list_lock);
8418c582c7cSDexuan Cui 
8428c582c7cSDexuan Cui 	dr_wrk = malloc(sizeof(*dr_wrk), M_DEVBUF, M_WAITOK | M_ZERO);
8438c582c7cSDexuan Cui 	dr_wrk->bus = hbus;
8448c582c7cSDexuan Cui 	TASK_INIT(&dr_wrk->task, 0, pci_devices_present_work, dr_wrk);
8458c582c7cSDexuan Cui 	taskqueue_enqueue(hbus->sc->taskq, &dr_wrk->task);
8468c582c7cSDexuan Cui }
8478c582c7cSDexuan Cui 
8488c582c7cSDexuan Cui static void
8498c582c7cSDexuan Cui hv_eject_device_work(void *arg, int pending __unused)
8508c582c7cSDexuan Cui {
8518c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev = arg;
8528c582c7cSDexuan Cui 	union win_slot_encoding wslot = hpdev->desc.wslot;
8538c582c7cSDexuan Cui 	struct hv_pcibus *hbus = hpdev->hbus;
8548c582c7cSDexuan Cui 	struct pci_eject_response *eject_pkt;
8558c582c7cSDexuan Cui 	struct {
8568c582c7cSDexuan Cui 		struct pci_packet pkt;
8578c582c7cSDexuan Cui 		uint8_t buffer[sizeof(struct pci_eject_response)];
8588c582c7cSDexuan Cui 	} ctxt;
8598c582c7cSDexuan Cui 
8608c582c7cSDexuan Cui 	hv_pci_delete_device(hpdev);
8618c582c7cSDexuan Cui 
8628c582c7cSDexuan Cui 	memset(&ctxt, 0, sizeof(ctxt));
8638c582c7cSDexuan Cui 	eject_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
8648c582c7cSDexuan Cui 	eject_pkt->message_type.type = PCI_EJECTION_COMPLETE;
8658c582c7cSDexuan Cui 	eject_pkt->wslot.val = wslot.val;
8668c582c7cSDexuan Cui 	vmbus_chan_send(hbus->sc->chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
8678c582c7cSDexuan Cui 	    eject_pkt, sizeof(*eject_pkt), 0);
8688c582c7cSDexuan Cui }
8698c582c7cSDexuan Cui 
8708c582c7cSDexuan Cui static void
8718c582c7cSDexuan Cui hv_pci_eject_device(struct hv_pci_dev *hpdev)
8728c582c7cSDexuan Cui {
8738c582c7cSDexuan Cui 	struct hv_pcibus *hbus = hpdev->hbus;
8748c582c7cSDexuan Cui 	struct taskqueue *taskq;
8758c582c7cSDexuan Cui 
8768c582c7cSDexuan Cui 	if (hbus->detaching)
8778c582c7cSDexuan Cui 		return;
8788c582c7cSDexuan Cui 
8798c582c7cSDexuan Cui 	/*
8808c582c7cSDexuan Cui 	 * Push this task into the same taskqueue on which
8818c582c7cSDexuan Cui 	 * vmbus_pcib_attach() runs, so we're sure this task can't run
8828c582c7cSDexuan Cui 	 * concurrently with vmbus_pcib_attach().
8838c582c7cSDexuan Cui 	 */
8848c582c7cSDexuan Cui 	TASK_INIT(&hpdev->eject_task, 0, hv_eject_device_work, hpdev);
8858c582c7cSDexuan Cui 	taskq = vmbus_chan_mgmt_tq(hbus->sc->chan);
8868c582c7cSDexuan Cui 	taskqueue_enqueue(taskq, &hpdev->eject_task);
8878c582c7cSDexuan Cui }
8888c582c7cSDexuan Cui 
8898c582c7cSDexuan Cui #define PCIB_PACKET_SIZE	0x100
8908c582c7cSDexuan Cui 
8918c582c7cSDexuan Cui static void
8928c582c7cSDexuan Cui vmbus_pcib_on_channel_callback(struct vmbus_channel *chan, void *arg)
8938c582c7cSDexuan Cui {
8948c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc = arg;
8958c582c7cSDexuan Cui 	struct hv_pcibus *hbus = sc->hbus;
8968c582c7cSDexuan Cui 
8978c582c7cSDexuan Cui 	void *buffer;
8988c582c7cSDexuan Cui 	int bufferlen = PCIB_PACKET_SIZE;
8998c582c7cSDexuan Cui 
9008c582c7cSDexuan Cui 	struct pci_packet *comp_packet;
9018c582c7cSDexuan Cui 	struct pci_response *response;
9028c582c7cSDexuan Cui 	struct pci_incoming_message *new_msg;
9038c582c7cSDexuan Cui 	struct pci_bus_relations *bus_rel;
9048c582c7cSDexuan Cui 	struct pci_dev_incoming *dev_msg;
9058c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
9068c582c7cSDexuan Cui 
9078c582c7cSDexuan Cui 	buffer = sc->rx_buf;
9088c582c7cSDexuan Cui 	do {
9098c582c7cSDexuan Cui 		struct vmbus_chanpkt_hdr *pkt = buffer;
9108c582c7cSDexuan Cui 		uint32_t bytes_rxed;
9118c582c7cSDexuan Cui 		int ret;
9128c582c7cSDexuan Cui 
9138c582c7cSDexuan Cui 		bytes_rxed = bufferlen;
9148c582c7cSDexuan Cui 		ret = vmbus_chan_recv_pkt(chan, pkt, &bytes_rxed);
9158c582c7cSDexuan Cui 
9168c582c7cSDexuan Cui 		if (ret == ENOBUFS) {
9178c582c7cSDexuan Cui 			/* Handle large packet */
9188c582c7cSDexuan Cui 			if (bufferlen > PCIB_PACKET_SIZE) {
9198c582c7cSDexuan Cui 				free(buffer, M_DEVBUF);
9208c582c7cSDexuan Cui 				buffer = NULL;
9218c582c7cSDexuan Cui 			}
9228c582c7cSDexuan Cui 
9238c582c7cSDexuan Cui 			/* alloc new buffer */
9248c582c7cSDexuan Cui 			buffer = malloc(bytes_rxed, M_DEVBUF, M_WAITOK | M_ZERO);
9258c582c7cSDexuan Cui 			bufferlen = bytes_rxed;
9268c582c7cSDexuan Cui 
9278c582c7cSDexuan Cui 			continue;
9288c582c7cSDexuan Cui 		}
9298c582c7cSDexuan Cui 
9308c582c7cSDexuan Cui 		if (ret != 0) {
9318c582c7cSDexuan Cui 			/* ignore EIO or EAGAIN */
9328c582c7cSDexuan Cui 			break;
9338c582c7cSDexuan Cui 		}
9348c582c7cSDexuan Cui 
9358c582c7cSDexuan Cui 		if (bytes_rxed <= sizeof(struct pci_response))
9368c582c7cSDexuan Cui 			continue;
9378c582c7cSDexuan Cui 
9388c582c7cSDexuan Cui 		switch (pkt->cph_type) {
9398c582c7cSDexuan Cui 		case VMBUS_CHANPKT_TYPE_COMP:
9408c582c7cSDexuan Cui 			comp_packet = (struct pci_packet *)pkt->cph_xactid;
9418c582c7cSDexuan Cui 			response = (struct pci_response *)pkt;
9428c582c7cSDexuan Cui 			comp_packet->completion_func(comp_packet->compl_ctxt,
9438c582c7cSDexuan Cui 			    response, bytes_rxed);
9448c582c7cSDexuan Cui 			break;
9458c582c7cSDexuan Cui 		case VMBUS_CHANPKT_TYPE_INBAND:
9468c582c7cSDexuan Cui 			new_msg = (struct pci_incoming_message *)buffer;
9478c582c7cSDexuan Cui 
9488c582c7cSDexuan Cui 			switch (new_msg->message_type.type) {
9498c582c7cSDexuan Cui 			case PCI_BUS_RELATIONS:
9508c582c7cSDexuan Cui 				bus_rel = (struct pci_bus_relations *)buffer;
9518c582c7cSDexuan Cui 
9528c582c7cSDexuan Cui 				if (bus_rel->device_count == 0)
9538c582c7cSDexuan Cui 					break;
9548c582c7cSDexuan Cui 
9558c582c7cSDexuan Cui 				if (bytes_rxed <
9568c582c7cSDexuan Cui 				    offsetof(struct pci_bus_relations, func) +
9578c582c7cSDexuan Cui 				        (sizeof(struct pci_func_desc) *
9588c582c7cSDexuan Cui 				            (bus_rel->device_count)))
9598c582c7cSDexuan Cui 					break;
9608c582c7cSDexuan Cui 
9618c582c7cSDexuan Cui 				hv_pci_devices_present(hbus, bus_rel);
9628c582c7cSDexuan Cui 				break;
9638c582c7cSDexuan Cui 
9648c582c7cSDexuan Cui 			case PCI_EJECT:
9658c582c7cSDexuan Cui 				dev_msg = (struct pci_dev_incoming *)buffer;
9668c582c7cSDexuan Cui 				hpdev = get_pcichild_wslot(hbus,
9678c582c7cSDexuan Cui 				    dev_msg->wslot.val);
9688c582c7cSDexuan Cui 
9698c582c7cSDexuan Cui 				if (hpdev)
9708c582c7cSDexuan Cui 					hv_pci_eject_device(hpdev);
9718c582c7cSDexuan Cui 
9728c582c7cSDexuan Cui 				break;
9738c582c7cSDexuan Cui 			default:
9748c582c7cSDexuan Cui 				printf("vmbus_pcib: Unknown msg type 0x%x\n",
9758c582c7cSDexuan Cui 				    new_msg->message_type.type);
9768c582c7cSDexuan Cui 				break;
9778c582c7cSDexuan Cui 			}
9788c582c7cSDexuan Cui 			break;
9798c582c7cSDexuan Cui 		default:
9808c582c7cSDexuan Cui 			printf("vmbus_pcib: Unknown VMBus msg type %hd\n",
9818c582c7cSDexuan Cui 			    pkt->cph_type);
9828c582c7cSDexuan Cui 			break;
9838c582c7cSDexuan Cui 		}
9848c582c7cSDexuan Cui 	} while (1);
9858c582c7cSDexuan Cui 
9868c582c7cSDexuan Cui 	if (bufferlen > PCIB_PACKET_SIZE)
9878c582c7cSDexuan Cui 		free(buffer, M_DEVBUF);
9888c582c7cSDexuan Cui }
9898c582c7cSDexuan Cui 
9908c582c7cSDexuan Cui static int
9918c582c7cSDexuan Cui hv_pci_protocol_negotiation(struct hv_pcibus *hbus)
9928c582c7cSDexuan Cui {
9938c582c7cSDexuan Cui 	struct pci_version_request *version_req;
9948c582c7cSDexuan Cui 	struct hv_pci_compl comp_pkt;
9958c582c7cSDexuan Cui 	struct {
9968c582c7cSDexuan Cui 		struct pci_packet pkt;
9978c582c7cSDexuan Cui 		uint8_t buffer[sizeof(struct pci_version_request)];
9988c582c7cSDexuan Cui 	} ctxt;
9998c582c7cSDexuan Cui 	int ret;
10008c582c7cSDexuan Cui 
10018c582c7cSDexuan Cui 	init_completion(&comp_pkt.host_event);
10028c582c7cSDexuan Cui 
10038c582c7cSDexuan Cui 	ctxt.pkt.completion_func = hv_pci_generic_compl;
10048c582c7cSDexuan Cui 	ctxt.pkt.compl_ctxt = &comp_pkt;
10058c582c7cSDexuan Cui 	version_req = (struct pci_version_request *)&ctxt.pkt.message;
10068c582c7cSDexuan Cui 	version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
10078c582c7cSDexuan Cui 	version_req->protocol_version = PCI_PROTOCOL_VERSION_CURRENT;
10088c582c7cSDexuan Cui 	version_req->is_last_attempt = 1;
10098c582c7cSDexuan Cui 
10108c582c7cSDexuan Cui 	ret = vmbus_chan_send(hbus->sc->chan, VMBUS_CHANPKT_TYPE_INBAND,
10118c582c7cSDexuan Cui 	    VMBUS_CHANPKT_FLAG_RC, version_req, sizeof(*version_req),
10128c582c7cSDexuan Cui 	    (uint64_t)&ctxt.pkt);
10138c582c7cSDexuan Cui 	if (ret)
10148c582c7cSDexuan Cui 		goto out;
10158c582c7cSDexuan Cui 
10168c582c7cSDexuan Cui 	wait_for_completion(&comp_pkt.host_event);
10178c582c7cSDexuan Cui 
10188c582c7cSDexuan Cui 	if (comp_pkt.completion_status < 0) {
10198c582c7cSDexuan Cui 		device_printf(hbus->pcib,
10208c582c7cSDexuan Cui 		    "vmbus_pcib version negotiation failed: %x\n",
10218c582c7cSDexuan Cui 		    comp_pkt.completion_status);
10228c582c7cSDexuan Cui 		ret = EPROTO;
10238c582c7cSDexuan Cui 	} else {
10248c582c7cSDexuan Cui 		ret = 0;
10258c582c7cSDexuan Cui 	}
10268c582c7cSDexuan Cui out:
10278c582c7cSDexuan Cui 	free_completion(&comp_pkt.host_event);
10288c582c7cSDexuan Cui 	return (ret);
10298c582c7cSDexuan Cui }
10308c582c7cSDexuan Cui 
10318c582c7cSDexuan Cui /* Ask the host to send along the list of child devices */
10328c582c7cSDexuan Cui static int
10338c582c7cSDexuan Cui hv_pci_query_relations(struct hv_pcibus *hbus)
10348c582c7cSDexuan Cui {
10358c582c7cSDexuan Cui 	struct pci_message message;
10368c582c7cSDexuan Cui 	int ret;
10378c582c7cSDexuan Cui 
10388c582c7cSDexuan Cui 	message.type = PCI_QUERY_BUS_RELATIONS;
10398c582c7cSDexuan Cui 	ret = vmbus_chan_send(hbus->sc->chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
10408c582c7cSDexuan Cui 	    &message, sizeof(message), 0);
10418c582c7cSDexuan Cui 	return (ret);
10428c582c7cSDexuan Cui }
10438c582c7cSDexuan Cui 
10448c582c7cSDexuan Cui static int
10458c582c7cSDexuan Cui hv_pci_enter_d0(struct hv_pcibus *hbus)
10468c582c7cSDexuan Cui {
10478c582c7cSDexuan Cui 	struct pci_bus_d0_entry *d0_entry;
10488c582c7cSDexuan Cui 	struct hv_pci_compl comp_pkt;
10498c582c7cSDexuan Cui 	struct {
10508c582c7cSDexuan Cui 		struct pci_packet pkt;
10518c582c7cSDexuan Cui 		uint8_t buffer[sizeof(struct pci_bus_d0_entry)];
10528c582c7cSDexuan Cui 	} ctxt;
10538c582c7cSDexuan Cui 	int ret;
10548c582c7cSDexuan Cui 
10558c582c7cSDexuan Cui 	/*
10568c582c7cSDexuan Cui 	 * Tell the host that the bus is ready to use, and moved into the
10578c582c7cSDexuan Cui 	 * powered-on state.  This includes telling the host which region
10588c582c7cSDexuan Cui 	 * of memory-mapped I/O space has been chosen for configuration space
10598c582c7cSDexuan Cui 	 * access.
10608c582c7cSDexuan Cui 	 */
10618c582c7cSDexuan Cui 	init_completion(&comp_pkt.host_event);
10628c582c7cSDexuan Cui 
10638c582c7cSDexuan Cui 	ctxt.pkt.completion_func = hv_pci_generic_compl;
10648c582c7cSDexuan Cui 	ctxt.pkt.compl_ctxt = &comp_pkt;
10658c582c7cSDexuan Cui 
10668c582c7cSDexuan Cui 	d0_entry = (struct pci_bus_d0_entry *)&ctxt.pkt.message;
10678c582c7cSDexuan Cui 	memset(d0_entry, 0, sizeof(*d0_entry));
10688c582c7cSDexuan Cui 	d0_entry->message_type.type = PCI_BUS_D0ENTRY;
10698c582c7cSDexuan Cui 	d0_entry->mmio_base = rman_get_start(hbus->cfg_res);
10708c582c7cSDexuan Cui 
10718c582c7cSDexuan Cui 	ret = vmbus_chan_send(hbus->sc->chan, VMBUS_CHANPKT_TYPE_INBAND,
10728c582c7cSDexuan Cui 	    VMBUS_CHANPKT_FLAG_RC, d0_entry, sizeof(*d0_entry),
10738c582c7cSDexuan Cui 	    (uint64_t)&ctxt.pkt);
10748c582c7cSDexuan Cui 	if (ret)
10758c582c7cSDexuan Cui 		goto out;
10768c582c7cSDexuan Cui 
10778c582c7cSDexuan Cui 	wait_for_completion(&comp_pkt.host_event);
10788c582c7cSDexuan Cui 
10798c582c7cSDexuan Cui 	if (comp_pkt.completion_status < 0) {
10808c582c7cSDexuan Cui 		device_printf(hbus->pcib, "vmbus_pcib failed to enable D0\n");
10818c582c7cSDexuan Cui 		ret = EPROTO;
10828c582c7cSDexuan Cui 	} else {
10838c582c7cSDexuan Cui 		ret = 0;
10848c582c7cSDexuan Cui 	}
10858c582c7cSDexuan Cui 
10868c582c7cSDexuan Cui out:
10878c582c7cSDexuan Cui 	free_completion(&comp_pkt.host_event);
10888c582c7cSDexuan Cui 	return (ret);
10898c582c7cSDexuan Cui }
10908c582c7cSDexuan Cui 
10918c582c7cSDexuan Cui /*
10928c582c7cSDexuan Cui  * It looks this is only needed by Windows VM, but let's send the message too
10938c582c7cSDexuan Cui  * just to make the host happy.
10948c582c7cSDexuan Cui  */
10958c582c7cSDexuan Cui static int
10968c582c7cSDexuan Cui hv_send_resources_allocated(struct hv_pcibus *hbus)
10978c582c7cSDexuan Cui {
10988c582c7cSDexuan Cui 	struct pci_resources_assigned *res_assigned;
10998c582c7cSDexuan Cui 	struct hv_pci_compl comp_pkt;
11008c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
11018c582c7cSDexuan Cui 	struct pci_packet *pkt;
11028c582c7cSDexuan Cui 	uint32_t wslot;
11038c582c7cSDexuan Cui 	int ret = 0;
11048c582c7cSDexuan Cui 
11058c582c7cSDexuan Cui 	pkt = malloc(sizeof(*pkt) + sizeof(*res_assigned),
11068c582c7cSDexuan Cui 	    M_DEVBUF, M_WAITOK | M_ZERO);
11078c582c7cSDexuan Cui 
11088c582c7cSDexuan Cui 	for (wslot = 0; wslot < 256; wslot++) {
11098c582c7cSDexuan Cui 		hpdev = get_pcichild_wslot(hbus, wslot);
11108c582c7cSDexuan Cui 		if (!hpdev)
11118c582c7cSDexuan Cui 			continue;
11128c582c7cSDexuan Cui 
11138c582c7cSDexuan Cui 		init_completion(&comp_pkt.host_event);
11148c582c7cSDexuan Cui 
11158c582c7cSDexuan Cui 		memset(pkt, 0, sizeof(*pkt) + sizeof(*res_assigned));
11168c582c7cSDexuan Cui 		pkt->completion_func = hv_pci_generic_compl;
11178c582c7cSDexuan Cui 		pkt->compl_ctxt = &comp_pkt;
11188c582c7cSDexuan Cui 
11198c582c7cSDexuan Cui 		res_assigned = (struct pci_resources_assigned *)&pkt->message;
11208c582c7cSDexuan Cui 		res_assigned->message_type.type = PCI_RESOURCES_ASSIGNED;
11218c582c7cSDexuan Cui 		res_assigned->wslot.val = hpdev->desc.wslot.val;
11228c582c7cSDexuan Cui 
11238c582c7cSDexuan Cui 		ret = vmbus_chan_send(hbus->sc->chan,
11248c582c7cSDexuan Cui 		    VMBUS_CHANPKT_TYPE_INBAND, VMBUS_CHANPKT_FLAG_RC,
11258c582c7cSDexuan Cui 		    &pkt->message, sizeof(*res_assigned), (uint64_t)pkt);
11268c582c7cSDexuan Cui 		if (ret) {
11278c582c7cSDexuan Cui 			free_completion(&comp_pkt.host_event);
11288c582c7cSDexuan Cui 			break;
11298c582c7cSDexuan Cui 		}
11308c582c7cSDexuan Cui 
11318c582c7cSDexuan Cui 		wait_for_completion(&comp_pkt.host_event);
11328c582c7cSDexuan Cui 		free_completion(&comp_pkt.host_event);
11338c582c7cSDexuan Cui 
11348c582c7cSDexuan Cui 		if (comp_pkt.completion_status < 0) {
11358c582c7cSDexuan Cui 			ret = EPROTO;
11368c582c7cSDexuan Cui 			device_printf(hbus->pcib,
11378c582c7cSDexuan Cui 			    "failed to send PCI_RESOURCES_ASSIGNED\n");
11388c582c7cSDexuan Cui 			break;
11398c582c7cSDexuan Cui 		}
11408c582c7cSDexuan Cui 	}
11418c582c7cSDexuan Cui 
11428c582c7cSDexuan Cui 	free(pkt, M_DEVBUF);
11438c582c7cSDexuan Cui 	return (ret);
11448c582c7cSDexuan Cui }
11458c582c7cSDexuan Cui 
11468c582c7cSDexuan Cui static int
11478c582c7cSDexuan Cui hv_send_resources_released(struct hv_pcibus *hbus)
11488c582c7cSDexuan Cui {
11498c582c7cSDexuan Cui 	struct pci_child_message pkt;
11508c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
11518c582c7cSDexuan Cui 	uint32_t wslot;
11528c582c7cSDexuan Cui 	int ret;
11538c582c7cSDexuan Cui 
11548c582c7cSDexuan Cui 	for (wslot = 0; wslot < 256; wslot++) {
11558c582c7cSDexuan Cui 		hpdev = get_pcichild_wslot(hbus, wslot);
11568c582c7cSDexuan Cui 		if (!hpdev)
11578c582c7cSDexuan Cui 			continue;
11588c582c7cSDexuan Cui 
11598c582c7cSDexuan Cui 		pkt.message_type.type = PCI_RESOURCES_RELEASED;
11608c582c7cSDexuan Cui 		pkt.wslot.val = hpdev->desc.wslot.val;
11618c582c7cSDexuan Cui 
11628c582c7cSDexuan Cui 		ret = vmbus_chan_send(hbus->sc->chan,
11638c582c7cSDexuan Cui 		    VMBUS_CHANPKT_TYPE_INBAND, 0, &pkt, sizeof(pkt), 0);
11648c582c7cSDexuan Cui 		if (ret)
11658c582c7cSDexuan Cui 			return (ret);
11668c582c7cSDexuan Cui 	}
11678c582c7cSDexuan Cui 
11688c582c7cSDexuan Cui 	return (0);
11698c582c7cSDexuan Cui }
11708c582c7cSDexuan Cui 
11718c582c7cSDexuan Cui #define hv_cfg_read(x, s)						\
11728c582c7cSDexuan Cui static inline uint##x##_t hv_cfg_read_##s(struct hv_pcibus *bus,	\
11738c582c7cSDexuan Cui     bus_size_t offset)							\
11748c582c7cSDexuan Cui {									\
11758c582c7cSDexuan Cui 	return (bus_read_##s(bus->cfg_res, offset));			\
11768c582c7cSDexuan Cui }
11778c582c7cSDexuan Cui 
11788c582c7cSDexuan Cui #define hv_cfg_write(x, s)						\
11798c582c7cSDexuan Cui static inline void hv_cfg_write_##s(struct hv_pcibus *bus,		\
11808c582c7cSDexuan Cui     bus_size_t offset, uint##x##_t val)					\
11818c582c7cSDexuan Cui {									\
11828c582c7cSDexuan Cui 	return (bus_write_##s(bus->cfg_res, offset, val));		\
11838c582c7cSDexuan Cui }
11848c582c7cSDexuan Cui 
11858c582c7cSDexuan Cui hv_cfg_read(8, 1)
11868c582c7cSDexuan Cui hv_cfg_read(16, 2)
11878c582c7cSDexuan Cui hv_cfg_read(32, 4)
11888c582c7cSDexuan Cui 
11898c582c7cSDexuan Cui hv_cfg_write(8, 1)
11908c582c7cSDexuan Cui hv_cfg_write(16, 2)
11918c582c7cSDexuan Cui hv_cfg_write(32, 4)
11928c582c7cSDexuan Cui 
11938c582c7cSDexuan Cui static void
11948c582c7cSDexuan Cui _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where, int size,
11958c582c7cSDexuan Cui     uint32_t *val)
11968c582c7cSDexuan Cui {
11978c582c7cSDexuan Cui 	struct hv_pcibus *hbus = hpdev->hbus;
11988c582c7cSDexuan Cui 	bus_size_t addr = CFG_PAGE_OFFSET + where;
11998c582c7cSDexuan Cui 
12008c582c7cSDexuan Cui 	/*
12018c582c7cSDexuan Cui 	 * If the attempt is to read the IDs or the ROM BAR, simulate that.
12028c582c7cSDexuan Cui 	 */
12038c582c7cSDexuan Cui 	if (where + size <= PCIR_COMMAND) {
12048c582c7cSDexuan Cui 		memcpy(val, ((uint8_t *)&hpdev->desc.v_id) + where, size);
12058c582c7cSDexuan Cui 	} else if (where >= PCIR_REVID && where + size <=
12068c582c7cSDexuan Cui 		   PCIR_CACHELNSZ) {
12078c582c7cSDexuan Cui 		memcpy(val, ((uint8_t *)&hpdev->desc.rev) + where -
12088c582c7cSDexuan Cui 		       PCIR_REVID, size);
12098c582c7cSDexuan Cui 	} else if (where >= PCIR_SUBVEND_0 && where + size <=
12108c582c7cSDexuan Cui 		   PCIR_BIOS) {
12118c582c7cSDexuan Cui 		memcpy(val, (uint8_t *)&hpdev->desc.subsystem_id + where -
12128c582c7cSDexuan Cui 		       PCIR_SUBVEND_0, size);
12138c582c7cSDexuan Cui 	} else if (where >= PCIR_BIOS && where + size <=
12148c582c7cSDexuan Cui 		   PCIR_CAP_PTR) {
12158c582c7cSDexuan Cui 		/* ROM BARs are unimplemented */
12168c582c7cSDexuan Cui 		*val = 0;
12178c582c7cSDexuan Cui 	} else if ((where >= PCIR_INTLINE && where + size <=
12188c582c7cSDexuan Cui 		   PCIR_INTPIN) ||(where == PCIR_INTPIN && size == 1)) {
12198c582c7cSDexuan Cui 		/*
12208c582c7cSDexuan Cui 		 * Interrupt Line and Interrupt PIN are hard-wired to zero
12218c582c7cSDexuan Cui 		 * because this front-end only supports message-signaled
12228c582c7cSDexuan Cui 		 * interrupts.
12238c582c7cSDexuan Cui 		 */
12248c582c7cSDexuan Cui 		*val = 0;
12258c582c7cSDexuan Cui 	} else if (where + size <= CFG_PAGE_SIZE) {
12268c582c7cSDexuan Cui 		mtx_lock(&hbus->config_lock);
12278c582c7cSDexuan Cui 
12288c582c7cSDexuan Cui 		/* Choose the function to be read. */
12298c582c7cSDexuan Cui 		hv_cfg_write_4(hbus, 0, hpdev->desc.wslot.val);
12308c582c7cSDexuan Cui 
12318c582c7cSDexuan Cui 		/* Make sure the function was chosen before we start reading.*/
12328c582c7cSDexuan Cui 		mb();
12338c582c7cSDexuan Cui 
12348c582c7cSDexuan Cui 		/* Read from that function's config space. */
12358c582c7cSDexuan Cui 		switch (size) {
12368c582c7cSDexuan Cui 		case 1:
12378c582c7cSDexuan Cui 			*((uint8_t *)val) = hv_cfg_read_1(hbus, addr);
12388c582c7cSDexuan Cui 			break;
12398c582c7cSDexuan Cui 		case 2:
12408c582c7cSDexuan Cui 			*((uint16_t *)val) = hv_cfg_read_2(hbus, addr);
12418c582c7cSDexuan Cui 			break;
12428c582c7cSDexuan Cui 		default:
12438c582c7cSDexuan Cui 			*((uint32_t *)val) = hv_cfg_read_4(hbus, addr);
12448c582c7cSDexuan Cui 			break;
12458c582c7cSDexuan Cui 		}
12468c582c7cSDexuan Cui 		/*
12478c582c7cSDexuan Cui 		 * Make sure the write was done before we release the lock,
12488c582c7cSDexuan Cui 		 * allowing consecutive reads/writes.
12498c582c7cSDexuan Cui 		 */
12508c582c7cSDexuan Cui 		mb();
12518c582c7cSDexuan Cui 
12528c582c7cSDexuan Cui 		mtx_unlock(&hbus->config_lock);
12538c582c7cSDexuan Cui 	} else {
12548c582c7cSDexuan Cui 		/* Invalid config read: it's unlikely to reach here. */
12558c582c7cSDexuan Cui 		memset(val, 0, size);
12568c582c7cSDexuan Cui 	}
12578c582c7cSDexuan Cui }
12588c582c7cSDexuan Cui 
12598c582c7cSDexuan Cui static void
12608c582c7cSDexuan Cui _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where, int size,
12618c582c7cSDexuan Cui     uint32_t val)
12628c582c7cSDexuan Cui {
12638c582c7cSDexuan Cui 	struct hv_pcibus *hbus = hpdev->hbus;
12648c582c7cSDexuan Cui 	bus_size_t addr = CFG_PAGE_OFFSET + where;
12658c582c7cSDexuan Cui 
12668c582c7cSDexuan Cui 	/* SSIDs and ROM BARs are read-only */
12678c582c7cSDexuan Cui 	if (where >= PCIR_SUBVEND_0 && where + size <= PCIR_CAP_PTR)
12688c582c7cSDexuan Cui 		return;
12698c582c7cSDexuan Cui 
12708c582c7cSDexuan Cui 	if (where >= PCIR_COMMAND && where + size <= CFG_PAGE_SIZE) {
12718c582c7cSDexuan Cui 		mtx_lock(&hbus->config_lock);
12728c582c7cSDexuan Cui 
12738c582c7cSDexuan Cui 		/* Choose the function to be written. */
12748c582c7cSDexuan Cui 		hv_cfg_write_4(hbus, 0, hpdev->desc.wslot.val);
12758c582c7cSDexuan Cui 
12768c582c7cSDexuan Cui 		/* Make sure the function was chosen before we start writing.*/
12778c582c7cSDexuan Cui 		wmb();
12788c582c7cSDexuan Cui 
12798c582c7cSDexuan Cui 		/* Write to that function's config space. */
12808c582c7cSDexuan Cui 		switch (size) {
12818c582c7cSDexuan Cui 		case 1:
12828c582c7cSDexuan Cui 			hv_cfg_write_1(hbus, addr, (uint8_t)val);
12838c582c7cSDexuan Cui 			break;
12848c582c7cSDexuan Cui 		case 2:
12858c582c7cSDexuan Cui 			hv_cfg_write_2(hbus, addr, (uint16_t)val);
12868c582c7cSDexuan Cui 			break;
12878c582c7cSDexuan Cui 		default:
12888c582c7cSDexuan Cui 			hv_cfg_write_4(hbus, addr, (uint32_t)val);
12898c582c7cSDexuan Cui 			break;
12908c582c7cSDexuan Cui 		}
12918c582c7cSDexuan Cui 
12928c582c7cSDexuan Cui 		/*
12938c582c7cSDexuan Cui 		 * Make sure the write was done before we release the lock,
12948c582c7cSDexuan Cui 		 * allowing consecutive reads/writes.
12958c582c7cSDexuan Cui 		 */
12968c582c7cSDexuan Cui 		mb();
12978c582c7cSDexuan Cui 
12988c582c7cSDexuan Cui 		mtx_unlock(&hbus->config_lock);
12998c582c7cSDexuan Cui 	} else {
13008c582c7cSDexuan Cui 		/* Invalid config write: it's unlikely to reach here. */
13018c582c7cSDexuan Cui 		return;
13028c582c7cSDexuan Cui 	}
13038c582c7cSDexuan Cui }
13048c582c7cSDexuan Cui 
13058c582c7cSDexuan Cui static void
13068c582c7cSDexuan Cui vmbus_pcib_set_detaching(void *arg, int pending __unused)
13078c582c7cSDexuan Cui {
13088c582c7cSDexuan Cui 	struct hv_pcibus *hbus = arg;
13098c582c7cSDexuan Cui 
13108c582c7cSDexuan Cui 	atomic_set_int(&hbus->detaching, 1);
13118c582c7cSDexuan Cui }
13128c582c7cSDexuan Cui 
13138c582c7cSDexuan Cui static void
13148c582c7cSDexuan Cui vmbus_pcib_pre_detach(struct hv_pcibus *hbus)
13158c582c7cSDexuan Cui {
13168c582c7cSDexuan Cui 	struct task task;
13178c582c7cSDexuan Cui 
13188c582c7cSDexuan Cui 	TASK_INIT(&task, 0, vmbus_pcib_set_detaching, hbus);
13198c582c7cSDexuan Cui 
13208c582c7cSDexuan Cui 	/*
13218c582c7cSDexuan Cui 	 * Make sure the channel callback won't push any possible new
13228c582c7cSDexuan Cui 	 * PCI_BUS_RELATIONS and PCI_EJECT tasks to sc->taskq.
13238c582c7cSDexuan Cui 	 */
13248c582c7cSDexuan Cui 	vmbus_chan_run_task(hbus->sc->chan, &task);
13258c582c7cSDexuan Cui 
13268c582c7cSDexuan Cui 	taskqueue_drain_all(hbus->sc->taskq);
13278c582c7cSDexuan Cui }
13288c582c7cSDexuan Cui 
13298c582c7cSDexuan Cui 
13308c582c7cSDexuan Cui /*
13318c582c7cSDexuan Cui  * Standard probe entry point.
13328c582c7cSDexuan Cui  *
13338c582c7cSDexuan Cui  */
13348c582c7cSDexuan Cui static int
13358c582c7cSDexuan Cui vmbus_pcib_probe(device_t dev)
13368c582c7cSDexuan Cui {
13378c582c7cSDexuan Cui 	if (VMBUS_PROBE_GUID(device_get_parent(dev), dev,
13388c582c7cSDexuan Cui 	    &g_pass_through_dev_type) == 0) {
13398c582c7cSDexuan Cui 		device_set_desc(dev, "Hyper-V PCI Express Pass Through");
13408c582c7cSDexuan Cui 		return (BUS_PROBE_DEFAULT);
13418c582c7cSDexuan Cui 	}
13428c582c7cSDexuan Cui 	return (ENXIO);
13438c582c7cSDexuan Cui }
13448c582c7cSDexuan Cui 
13458c582c7cSDexuan Cui /*
13468c582c7cSDexuan Cui  * Standard attach entry point.
13478c582c7cSDexuan Cui  *
13488c582c7cSDexuan Cui  */
13498c582c7cSDexuan Cui static int
13508c582c7cSDexuan Cui vmbus_pcib_attach(device_t dev)
13518c582c7cSDexuan Cui {
13528c582c7cSDexuan Cui 	const int pci_ring_size = (4 * PAGE_SIZE);
13538c582c7cSDexuan Cui 	const struct hyperv_guid *inst_guid;
13548c582c7cSDexuan Cui 	struct vmbus_channel *channel;
13558c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc;
13568c582c7cSDexuan Cui 	struct hv_pcibus *hbus;
13578c582c7cSDexuan Cui 	int rid = 0;
13588c582c7cSDexuan Cui 	int ret;
13598c582c7cSDexuan Cui 
13608c582c7cSDexuan Cui 	hbus = malloc(sizeof(*hbus), M_DEVBUF, M_WAITOK | M_ZERO);
13618c582c7cSDexuan Cui 	hbus->pcib = dev;
13628c582c7cSDexuan Cui 
13638c582c7cSDexuan Cui 	channel = vmbus_get_channel(dev);
13648c582c7cSDexuan Cui 	inst_guid = vmbus_chan_guid_inst(channel);
13658c582c7cSDexuan Cui 	hbus->pci_domain = inst_guid->hv_guid[9] |
13668c582c7cSDexuan Cui 			  (inst_guid->hv_guid[8] << 8);
13678c582c7cSDexuan Cui 
13688c582c7cSDexuan Cui 	mtx_init(&hbus->config_lock, "hbcfg", NULL, MTX_DEF);
13698c582c7cSDexuan Cui 	mtx_init(&hbus->device_list_lock, "hbdl", NULL, MTX_DEF);
13708c582c7cSDexuan Cui 	TAILQ_INIT(&hbus->children);
13718c582c7cSDexuan Cui 	TAILQ_INIT(&hbus->dr_list);
13728c582c7cSDexuan Cui 
13738c582c7cSDexuan Cui 	hbus->cfg_res = bus_alloc_resource(dev, SYS_RES_MEMORY, &rid,
13748c582c7cSDexuan Cui 	    0, RM_MAX_END, PCI_CONFIG_MMIO_LENGTH,
13758c582c7cSDexuan Cui 	    RF_ACTIVE | rman_make_alignment_flags(PAGE_SIZE));
13768c582c7cSDexuan Cui 
13778c582c7cSDexuan Cui 	if (!hbus->cfg_res) {
13788c582c7cSDexuan Cui 		device_printf(dev, "failed to get resource for cfg window\n");
13798c582c7cSDexuan Cui 		ret = ENXIO;
13808c582c7cSDexuan Cui 		goto free_bus;
13818c582c7cSDexuan Cui 	}
13828c582c7cSDexuan Cui 
13838c582c7cSDexuan Cui 	sc = device_get_softc(dev);
13848c582c7cSDexuan Cui 	sc->chan = channel;
13858c582c7cSDexuan Cui 	sc->rx_buf = malloc(PCIB_PACKET_SIZE, M_DEVBUF, M_WAITOK | M_ZERO);
13868c582c7cSDexuan Cui 	sc->hbus = hbus;
13878c582c7cSDexuan Cui 
13888c582c7cSDexuan Cui 	/*
13898c582c7cSDexuan Cui 	 * The taskq is used to handle PCI_BUS_RELATIONS and PCI_EJECT
13908c582c7cSDexuan Cui 	 * messages. NB: we can't handle the messages in the channel callback
13918c582c7cSDexuan Cui 	 * directly, because the message handlers need to send new messages
13928c582c7cSDexuan Cui 	 * to the host and waits for the host's completion messages, which
13938c582c7cSDexuan Cui 	 * must also be handled by the channel callback.
13948c582c7cSDexuan Cui 	 */
13958c582c7cSDexuan Cui 	sc->taskq = taskqueue_create("vmbus_pcib_tq", M_WAITOK,
13968c582c7cSDexuan Cui 	    taskqueue_thread_enqueue, &sc->taskq);
13978c582c7cSDexuan Cui 	taskqueue_start_threads(&sc->taskq, 1, PI_NET, "vmbus_pcib_tq");
13988c582c7cSDexuan Cui 
13998c582c7cSDexuan Cui 	hbus->sc = sc;
14008c582c7cSDexuan Cui 
14018c582c7cSDexuan Cui 	init_completion(&hbus->query_completion);
14028c582c7cSDexuan Cui 	hbus->query_comp = &hbus->query_completion;
14038c582c7cSDexuan Cui 
14048c582c7cSDexuan Cui 	ret = vmbus_chan_open(sc->chan, pci_ring_size, pci_ring_size,
14058c582c7cSDexuan Cui 		NULL, 0, vmbus_pcib_on_channel_callback, sc);
14068c582c7cSDexuan Cui 	if (ret)
14078c582c7cSDexuan Cui 		goto free_res;
14088c582c7cSDexuan Cui 
14098c582c7cSDexuan Cui 	ret = hv_pci_protocol_negotiation(hbus);
14108c582c7cSDexuan Cui 	if (ret)
14118c582c7cSDexuan Cui 		goto vmbus_close;
14128c582c7cSDexuan Cui 
14138c582c7cSDexuan Cui 	ret = hv_pci_query_relations(hbus);
14148c582c7cSDexuan Cui 	if (ret)
14158c582c7cSDexuan Cui 		goto vmbus_close;
14168c582c7cSDexuan Cui 	wait_for_completion(hbus->query_comp);
14178c582c7cSDexuan Cui 
14188c582c7cSDexuan Cui 	ret = hv_pci_enter_d0(hbus);
14198c582c7cSDexuan Cui 	if (ret)
14208c582c7cSDexuan Cui 		goto vmbus_close;
14218c582c7cSDexuan Cui 
14228c582c7cSDexuan Cui 	ret = hv_send_resources_allocated(hbus);
14238c582c7cSDexuan Cui 	if (ret)
14248c582c7cSDexuan Cui 		goto vmbus_close;
14258c582c7cSDexuan Cui 
14268c582c7cSDexuan Cui 	hbus->pci_bus = device_add_child(dev, "pci", -1);
14278c582c7cSDexuan Cui 	if (!hbus->pci_bus) {
14288c582c7cSDexuan Cui 		device_printf(dev, "failed to create pci bus\n");
14298c582c7cSDexuan Cui 		ret = ENXIO;
14308c582c7cSDexuan Cui 		goto vmbus_close;
14318c582c7cSDexuan Cui 	}
14328c582c7cSDexuan Cui 
14338c582c7cSDexuan Cui 	bus_generic_attach(dev);
14348c582c7cSDexuan Cui 
14358c582c7cSDexuan Cui 	hbus->state = hv_pcibus_installed;
14368c582c7cSDexuan Cui 
14378c582c7cSDexuan Cui 	return (0);
14388c582c7cSDexuan Cui 
14398c582c7cSDexuan Cui vmbus_close:
14408c582c7cSDexuan Cui 	vmbus_pcib_pre_detach(hbus);
14418c582c7cSDexuan Cui 	vmbus_chan_close(sc->chan);
14428c582c7cSDexuan Cui free_res:
14438c582c7cSDexuan Cui 	taskqueue_free(sc->taskq);
14448c582c7cSDexuan Cui 	free_completion(&hbus->query_completion);
14458c582c7cSDexuan Cui 	free(sc->rx_buf, M_DEVBUF);
14468c582c7cSDexuan Cui 	bus_release_resource(dev, SYS_RES_MEMORY, 0, hbus->cfg_res);
14478c582c7cSDexuan Cui free_bus:
14488c582c7cSDexuan Cui 	mtx_destroy(&hbus->device_list_lock);
14498c582c7cSDexuan Cui 	mtx_destroy(&hbus->config_lock);
14508c582c7cSDexuan Cui 	free(hbus, M_DEVBUF);
14518c582c7cSDexuan Cui 	return (ret);
14528c582c7cSDexuan Cui }
14538c582c7cSDexuan Cui 
14548c582c7cSDexuan Cui /*
14558c582c7cSDexuan Cui  * Standard detach entry point
14568c582c7cSDexuan Cui  */
14578c582c7cSDexuan Cui static int
14588c582c7cSDexuan Cui vmbus_pcib_detach(device_t dev)
14598c582c7cSDexuan Cui {
14608c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc = device_get_softc(dev);
14618c582c7cSDexuan Cui 	struct hv_pcibus *hbus = sc->hbus;
14628c582c7cSDexuan Cui 	struct pci_message teardown_packet;
14638c582c7cSDexuan Cui 	struct pci_bus_relations relations;
14648c582c7cSDexuan Cui 	int ret;
14658c582c7cSDexuan Cui 
14668c582c7cSDexuan Cui 	vmbus_pcib_pre_detach(hbus);
14678c582c7cSDexuan Cui 
14688c582c7cSDexuan Cui 	if (hbus->state == hv_pcibus_installed)
14698c582c7cSDexuan Cui 		bus_generic_detach(dev);
14708c582c7cSDexuan Cui 
14718c582c7cSDexuan Cui 	/* Delete any children which might still exist. */
14728c582c7cSDexuan Cui 	memset(&relations, 0, sizeof(relations));
14738c582c7cSDexuan Cui 	hv_pci_devices_present(hbus, &relations);
14748c582c7cSDexuan Cui 
14758c582c7cSDexuan Cui 	ret = hv_send_resources_released(hbus);
14768c582c7cSDexuan Cui 	if (ret)
14778c582c7cSDexuan Cui 		device_printf(dev, "failed to send PCI_RESOURCES_RELEASED\n");
14788c582c7cSDexuan Cui 
14798c582c7cSDexuan Cui 	teardown_packet.type = PCI_BUS_D0EXIT;
14808c582c7cSDexuan Cui 	ret = vmbus_chan_send(sc->chan, VMBUS_CHANPKT_TYPE_INBAND, 0,
14818c582c7cSDexuan Cui 	    &teardown_packet, sizeof(struct pci_message), 0);
14828c582c7cSDexuan Cui 	if (ret)
14838c582c7cSDexuan Cui 		device_printf(dev, "failed to send PCI_BUS_D0EXIT\n");
14848c582c7cSDexuan Cui 
14858c582c7cSDexuan Cui 	taskqueue_drain_all(hbus->sc->taskq);
14868c582c7cSDexuan Cui 	vmbus_chan_close(sc->chan);
14878c582c7cSDexuan Cui 	taskqueue_free(sc->taskq);
14888c582c7cSDexuan Cui 
14898c582c7cSDexuan Cui 	free_completion(&hbus->query_completion);
14908c582c7cSDexuan Cui 	free(sc->rx_buf, M_DEVBUF);
14918c582c7cSDexuan Cui 	bus_release_resource(dev, SYS_RES_MEMORY, 0, hbus->cfg_res);
14928c582c7cSDexuan Cui 
14938c582c7cSDexuan Cui 	mtx_destroy(&hbus->device_list_lock);
14948c582c7cSDexuan Cui 	mtx_destroy(&hbus->config_lock);
14958c582c7cSDexuan Cui 	free(hbus, M_DEVBUF);
14968c582c7cSDexuan Cui 
14978c582c7cSDexuan Cui 	return (0);
14988c582c7cSDexuan Cui }
14998c582c7cSDexuan Cui 
15008c582c7cSDexuan Cui static int
15018c582c7cSDexuan Cui vmbus_pcib_read_ivar(device_t dev, device_t child, int which, uintptr_t *val)
15028c582c7cSDexuan Cui {
15038c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc = device_get_softc(dev);
15048c582c7cSDexuan Cui 
15058c582c7cSDexuan Cui 	switch (which) {
15068c582c7cSDexuan Cui 	case PCIB_IVAR_DOMAIN:
15078c582c7cSDexuan Cui 		*val = sc->hbus->pci_domain;
15088c582c7cSDexuan Cui 		return (0);
15098c582c7cSDexuan Cui 
15108c582c7cSDexuan Cui 	case PCIB_IVAR_BUS:
15118c582c7cSDexuan Cui 		/* There is only bus 0. */
15128c582c7cSDexuan Cui 		*val = 0;
15138c582c7cSDexuan Cui 		return (0);
15148c582c7cSDexuan Cui 	}
15158c582c7cSDexuan Cui 	return (ENOENT);
15168c582c7cSDexuan Cui }
15178c582c7cSDexuan Cui 
15188c582c7cSDexuan Cui static int
15198c582c7cSDexuan Cui vmbus_pcib_write_ivar(device_t dev, device_t child, int which, uintptr_t val)
15208c582c7cSDexuan Cui {
15218c582c7cSDexuan Cui 	return (ENOENT);
15228c582c7cSDexuan Cui }
15238c582c7cSDexuan Cui 
15248c582c7cSDexuan Cui static struct resource *
15258c582c7cSDexuan Cui vmbus_pcib_alloc_resource(device_t dev, device_t child, int type, int *rid,
15268c582c7cSDexuan Cui 	rman_res_t start, rman_res_t end, rman_res_t count, u_int flags)
15278c582c7cSDexuan Cui {
15288c582c7cSDexuan Cui 	unsigned int bar_no;
15298c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
15308c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc = device_get_softc(dev);
15318c582c7cSDexuan Cui 	struct resource *res;
15328c582c7cSDexuan Cui 	unsigned int devfn;
15338c582c7cSDexuan Cui 
15348c582c7cSDexuan Cui 	if (type == PCI_RES_BUS)
15358c582c7cSDexuan Cui 		return (pci_domain_alloc_bus(sc->hbus->pci_domain, child, rid,
15368c582c7cSDexuan Cui 		    start, end, count, flags));
15378c582c7cSDexuan Cui 
15388c582c7cSDexuan Cui 	/* Devices with port I/O BAR are not supported. */
15398c582c7cSDexuan Cui 	if (type == SYS_RES_IOPORT)
15408c582c7cSDexuan Cui 		return (NULL);
15418c582c7cSDexuan Cui 
15428c582c7cSDexuan Cui 	if (type == SYS_RES_MEMORY) {
15438c582c7cSDexuan Cui 		devfn = PCI_DEVFN(pci_get_slot(child),
15448c582c7cSDexuan Cui 		    pci_get_function(child));
15458c582c7cSDexuan Cui 		hpdev = get_pcichild_wslot(sc->hbus, devfn_to_wslot(devfn));
15468c582c7cSDexuan Cui 		if (!hpdev)
15478c582c7cSDexuan Cui 			return (NULL);
15488c582c7cSDexuan Cui 
15498c582c7cSDexuan Cui 		bar_no = PCI_RID2BAR(*rid);
15508c582c7cSDexuan Cui 		if (bar_no >= MAX_NUM_BARS)
15518c582c7cSDexuan Cui 			return (NULL);
15528c582c7cSDexuan Cui 
15538c582c7cSDexuan Cui 		/* Make sure a 32-bit BAR gets a 32-bit address */
15548c582c7cSDexuan Cui 		if (!(hpdev->probed_bar[bar_no] & PCIM_BAR_MEM_64))
15558c582c7cSDexuan Cui 			end = ulmin(end, 0xFFFFFFFF);
15568c582c7cSDexuan Cui 	}
15578c582c7cSDexuan Cui 
15588c582c7cSDexuan Cui 	res = bus_generic_alloc_resource(dev, child, type, rid,
15598c582c7cSDexuan Cui 		start, end, count, flags);
15608c582c7cSDexuan Cui 	/*
15618c582c7cSDexuan Cui 	 * If this is a request for a specific range, assume it is
15628c582c7cSDexuan Cui 	 * correct and pass it up to the parent.
15638c582c7cSDexuan Cui 	 */
15648c582c7cSDexuan Cui 	if (res == NULL && start + count - 1 == end)
15658c582c7cSDexuan Cui 		res = bus_generic_alloc_resource(dev, child, type, rid,
15668c582c7cSDexuan Cui 		    start, end, count, flags);
15678c582c7cSDexuan Cui 	return (res);
15688c582c7cSDexuan Cui }
15698c582c7cSDexuan Cui 
15708c582c7cSDexuan Cui static int
15718c582c7cSDexuan Cui vmbus_pcib_release_resource(device_t dev, device_t child, int type, int rid,
15728c582c7cSDexuan Cui     struct resource *r)
15738c582c7cSDexuan Cui {
15748c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc = device_get_softc(dev);
15758c582c7cSDexuan Cui 
15768c582c7cSDexuan Cui 	if (type == PCI_RES_BUS)
15778c582c7cSDexuan Cui 		return (pci_domain_release_bus(sc->hbus->pci_domain, child,
15788c582c7cSDexuan Cui 		    rid, r));
15798c582c7cSDexuan Cui 
15808c582c7cSDexuan Cui 	if (type == SYS_RES_IOPORT)
15818c582c7cSDexuan Cui 		return (EINVAL);
15828c582c7cSDexuan Cui 
15838c582c7cSDexuan Cui 	return (bus_generic_release_resource(dev, child, type, rid, r));
15848c582c7cSDexuan Cui }
15858c582c7cSDexuan Cui 
15868c582c7cSDexuan Cui #if __FreeBSD_version >= 1100000
15878c582c7cSDexuan Cui static int
15888c582c7cSDexuan Cui vmbus_pcib_get_cpus(device_t pcib, device_t dev, enum cpu_sets op,
15898c582c7cSDexuan Cui     size_t setsize, cpuset_t *cpuset)
15908c582c7cSDexuan Cui {
15918c582c7cSDexuan Cui 	return (bus_get_cpus(pcib, op, setsize, cpuset));
15928c582c7cSDexuan Cui }
15938c582c7cSDexuan Cui #endif
15948c582c7cSDexuan Cui 
15958c582c7cSDexuan Cui static uint32_t
15968c582c7cSDexuan Cui vmbus_pcib_read_config(device_t dev, u_int bus, u_int slot, u_int func,
15978c582c7cSDexuan Cui     u_int reg, int bytes)
15988c582c7cSDexuan Cui {
15998c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc = device_get_softc(dev);
16008c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
16018c582c7cSDexuan Cui 	unsigned int devfn = PCI_DEVFN(slot, func);
16028c582c7cSDexuan Cui 	uint32_t data = 0;
16038c582c7cSDexuan Cui 
16048c582c7cSDexuan Cui 	KASSERT(bus == 0, ("bus should be 0, but is %u", bus));
16058c582c7cSDexuan Cui 
16068c582c7cSDexuan Cui 	hpdev = get_pcichild_wslot(sc->hbus, devfn_to_wslot(devfn));
16078c582c7cSDexuan Cui 	if (!hpdev)
16088c582c7cSDexuan Cui 		return (~0);
16098c582c7cSDexuan Cui 
16108c582c7cSDexuan Cui 	_hv_pcifront_read_config(hpdev, reg, bytes, &data);
16118c582c7cSDexuan Cui 
16128c582c7cSDexuan Cui 	return (data);
16138c582c7cSDexuan Cui }
16148c582c7cSDexuan Cui 
16158c582c7cSDexuan Cui static void
16168c582c7cSDexuan Cui vmbus_pcib_write_config(device_t dev, u_int bus, u_int slot, u_int func,
16178c582c7cSDexuan Cui     u_int reg, uint32_t data, int bytes)
16188c582c7cSDexuan Cui {
16198c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc = device_get_softc(dev);
16208c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
16218c582c7cSDexuan Cui 	unsigned int devfn = PCI_DEVFN(slot, func);
16228c582c7cSDexuan Cui 
16238c582c7cSDexuan Cui 	KASSERT(bus == 0, ("bus should be 0, but is %u", bus));
16248c582c7cSDexuan Cui 
16258c582c7cSDexuan Cui 	hpdev = get_pcichild_wslot(sc->hbus, devfn_to_wslot(devfn));
16268c582c7cSDexuan Cui 	if (!hpdev)
16278c582c7cSDexuan Cui 		return;
16288c582c7cSDexuan Cui 
16298c582c7cSDexuan Cui 	_hv_pcifront_write_config(hpdev, reg, bytes, data);
16308c582c7cSDexuan Cui }
16318c582c7cSDexuan Cui 
16328c582c7cSDexuan Cui static int
16338c582c7cSDexuan Cui vmbus_pcib_route_intr(device_t pcib, device_t dev, int pin)
16348c582c7cSDexuan Cui {
16358c582c7cSDexuan Cui 	/* We only support MSI/MSI-X and don't support INTx interrupt. */
16368c582c7cSDexuan Cui 	return (PCI_INVALID_IRQ);
16378c582c7cSDexuan Cui }
16388c582c7cSDexuan Cui 
16398c582c7cSDexuan Cui static int
16408c582c7cSDexuan Cui vmbus_pcib_alloc_msi(device_t pcib, device_t dev, int count,
16418c582c7cSDexuan Cui     int maxcount, int *irqs)
16428c582c7cSDexuan Cui {
16438c582c7cSDexuan Cui 	return (PCIB_ALLOC_MSI(device_get_parent(pcib), dev, count, maxcount,
16448c582c7cSDexuan Cui 	    irqs));
16458c582c7cSDexuan Cui }
16468c582c7cSDexuan Cui 
16478c582c7cSDexuan Cui static int
16488c582c7cSDexuan Cui vmbus_pcib_release_msi(device_t pcib, device_t dev, int count, int *irqs)
16498c582c7cSDexuan Cui {
16508c582c7cSDexuan Cui 	return (PCIB_RELEASE_MSI(device_get_parent(pcib), dev, count, irqs));
16518c582c7cSDexuan Cui }
16528c582c7cSDexuan Cui 
16538c582c7cSDexuan Cui static int
16548c582c7cSDexuan Cui vmbus_pcib_alloc_msix(device_t pcib, device_t dev, int *irq)
16558c582c7cSDexuan Cui {
16568c582c7cSDexuan Cui 	return (PCIB_ALLOC_MSIX(device_get_parent(pcib), dev, irq));
16578c582c7cSDexuan Cui }
16588c582c7cSDexuan Cui 
16598c582c7cSDexuan Cui static int
16608c582c7cSDexuan Cui vmbus_pcib_release_msix(device_t pcib, device_t dev, int irq)
16618c582c7cSDexuan Cui {
16628c582c7cSDexuan Cui 	return (PCIB_RELEASE_MSIX(device_get_parent(pcib), dev, irq));
16638c582c7cSDexuan Cui }
16648c582c7cSDexuan Cui 
16658c582c7cSDexuan Cui #define	MSI_INTEL_ADDR_DEST	0x000ff000
16668c582c7cSDexuan Cui #define	MSI_INTEL_DATA_INTVEC	IOART_INTVEC	/* Interrupt vector. */
16678c582c7cSDexuan Cui #define	MSI_INTEL_DATA_DELFIXED	IOART_DELFIXED
16688c582c7cSDexuan Cui 
16698c582c7cSDexuan Cui static int
16708c582c7cSDexuan Cui vmbus_pcib_map_msi(device_t pcib, device_t child, int irq,
16718c582c7cSDexuan Cui     uint64_t *addr, uint32_t *data)
16728c582c7cSDexuan Cui {
16738c582c7cSDexuan Cui 	unsigned int devfn;
16748c582c7cSDexuan Cui 	struct hv_pci_dev *hpdev;
16758c582c7cSDexuan Cui 
16768c582c7cSDexuan Cui 	uint64_t v_addr;
16778c582c7cSDexuan Cui 	uint32_t v_data;
16788c582c7cSDexuan Cui 	struct hv_irq_desc *hid, *tmp_hid;
16798c582c7cSDexuan Cui 	unsigned int cpu, vcpu_id;
16808c582c7cSDexuan Cui 	unsigned int vector;
16818c582c7cSDexuan Cui 
16828c582c7cSDexuan Cui 	struct vmbus_pcib_softc *sc = device_get_softc(pcib);
16838c582c7cSDexuan Cui 	struct pci_create_interrupt *int_pkt;
16848c582c7cSDexuan Cui 	struct compose_comp_ctxt comp;
16858c582c7cSDexuan Cui 	struct {
16868c582c7cSDexuan Cui 		struct pci_packet pkt;
16878c582c7cSDexuan Cui 		uint8_t buffer[sizeof(struct pci_create_interrupt)];
16888c582c7cSDexuan Cui 	} ctxt;
16898c582c7cSDexuan Cui 
16908c582c7cSDexuan Cui 	int ret;
16918c582c7cSDexuan Cui 
16928c582c7cSDexuan Cui 	devfn = PCI_DEVFN(pci_get_slot(child), pci_get_function(child));
16938c582c7cSDexuan Cui 	hpdev = get_pcichild_wslot(sc->hbus, devfn_to_wslot(devfn));
16948c582c7cSDexuan Cui 	if (!hpdev)
16958c582c7cSDexuan Cui 		return (ENOENT);
16968c582c7cSDexuan Cui 
16978c582c7cSDexuan Cui 	ret = PCIB_MAP_MSI(device_get_parent(pcib), child, irq,
16988c582c7cSDexuan Cui 	    &v_addr, &v_data);
16998c582c7cSDexuan Cui 	if (ret)
17008c582c7cSDexuan Cui 		return (ret);
17018c582c7cSDexuan Cui 
17028c582c7cSDexuan Cui 	TAILQ_FOREACH_SAFE(hid, &hpdev->irq_desc_list, link, tmp_hid) {
17038c582c7cSDexuan Cui 		if (hid->irq == irq) {
17048c582c7cSDexuan Cui 			TAILQ_REMOVE(&hpdev->irq_desc_list, hid, link);
17058c582c7cSDexuan Cui 			hv_int_desc_free(hpdev, hid);
17068c582c7cSDexuan Cui 			break;
17078c582c7cSDexuan Cui 		}
17088c582c7cSDexuan Cui 	}
17098c582c7cSDexuan Cui 
17108c582c7cSDexuan Cui 	cpu = (v_addr & MSI_INTEL_ADDR_DEST) >> 12;
17118c582c7cSDexuan Cui 	vcpu_id = VMBUS_GET_VCPU_ID(device_get_parent(pcib), pcib, cpu);
17128c582c7cSDexuan Cui 	vector = v_data & MSI_INTEL_DATA_INTVEC;
17138c582c7cSDexuan Cui 
17148c582c7cSDexuan Cui 	init_completion(&comp.comp_pkt.host_event);
17158c582c7cSDexuan Cui 
17168c582c7cSDexuan Cui 	memset(&ctxt, 0, sizeof(ctxt));
17178c582c7cSDexuan Cui 	ctxt.pkt.completion_func = hv_pci_compose_compl;
17188c582c7cSDexuan Cui 	ctxt.pkt.compl_ctxt = &comp;
17198c582c7cSDexuan Cui 
17208c582c7cSDexuan Cui 	int_pkt = (struct pci_create_interrupt *)&ctxt.pkt.message;
17218c582c7cSDexuan Cui 	int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
17228c582c7cSDexuan Cui 	int_pkt->wslot.val = hpdev->desc.wslot.val;
17238c582c7cSDexuan Cui 	int_pkt->int_desc.vector = vector;
17248c582c7cSDexuan Cui 	int_pkt->int_desc.vector_count = 1;
17258c582c7cSDexuan Cui 	int_pkt->int_desc.delivery_mode = MSI_INTEL_DATA_DELFIXED;
17268c582c7cSDexuan Cui 	int_pkt->int_desc.cpu_mask = 1ULL << vcpu_id;
17278c582c7cSDexuan Cui 
17288c582c7cSDexuan Cui 	ret = vmbus_chan_send(sc->chan,	VMBUS_CHANPKT_TYPE_INBAND,
17298c582c7cSDexuan Cui 	    VMBUS_CHANPKT_FLAG_RC, int_pkt, sizeof(*int_pkt),
17308c582c7cSDexuan Cui 	    (uint64_t)&ctxt.pkt);
17318c582c7cSDexuan Cui 	if (ret) {
17328c582c7cSDexuan Cui 		free_completion(&comp.comp_pkt.host_event);
17338c582c7cSDexuan Cui 		return (ret);
17348c582c7cSDexuan Cui 	}
17358c582c7cSDexuan Cui 
17368c582c7cSDexuan Cui 	wait_for_completion(&comp.comp_pkt.host_event);
17378c582c7cSDexuan Cui 	free_completion(&comp.comp_pkt.host_event);
17388c582c7cSDexuan Cui 
17398c582c7cSDexuan Cui 	if (comp.comp_pkt.completion_status < 0)
17408c582c7cSDexuan Cui 		return (EPROTO);
17418c582c7cSDexuan Cui 
17428c582c7cSDexuan Cui 	*addr = comp.int_desc.address;
17438c582c7cSDexuan Cui 	*data = comp.int_desc.data;
17448c582c7cSDexuan Cui 
17458c582c7cSDexuan Cui 	hid = malloc(sizeof(struct hv_irq_desc), M_DEVBUF, M_WAITOK | M_ZERO);
17468c582c7cSDexuan Cui 	hid->irq = irq;
17478c582c7cSDexuan Cui 	hid->desc = comp.int_desc;
17488c582c7cSDexuan Cui 	TAILQ_INSERT_TAIL(&hpdev->irq_desc_list, hid, link);
17498c582c7cSDexuan Cui 
17508c582c7cSDexuan Cui 	return (0);
17518c582c7cSDexuan Cui }
17528c582c7cSDexuan Cui 
17538c582c7cSDexuan Cui static device_method_t vmbus_pcib_methods[] = {
17548c582c7cSDexuan Cui 	/* Device interface */
17558c582c7cSDexuan Cui 	DEVMETHOD(device_probe,         vmbus_pcib_probe),
17568c582c7cSDexuan Cui 	DEVMETHOD(device_attach,        vmbus_pcib_attach),
17578c582c7cSDexuan Cui 	DEVMETHOD(device_detach,        vmbus_pcib_detach),
17588c582c7cSDexuan Cui 	DEVMETHOD(device_shutdown,	bus_generic_shutdown),
17598c582c7cSDexuan Cui 	DEVMETHOD(device_suspend,	bus_generic_suspend),
17608c582c7cSDexuan Cui 	DEVMETHOD(device_resume,	bus_generic_resume),
17618c582c7cSDexuan Cui 
17628c582c7cSDexuan Cui 	/* Bus interface */
17638c582c7cSDexuan Cui 	DEVMETHOD(bus_read_ivar,		vmbus_pcib_read_ivar),
17648c582c7cSDexuan Cui 	DEVMETHOD(bus_write_ivar,		vmbus_pcib_write_ivar),
17658c582c7cSDexuan Cui 	DEVMETHOD(bus_alloc_resource,		vmbus_pcib_alloc_resource),
17668c582c7cSDexuan Cui 	DEVMETHOD(bus_release_resource,		vmbus_pcib_release_resource),
17678c582c7cSDexuan Cui 	DEVMETHOD(bus_activate_resource,   bus_generic_activate_resource),
17688c582c7cSDexuan Cui 	DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource),
17698c582c7cSDexuan Cui 	DEVMETHOD(bus_setup_intr,	   bus_generic_setup_intr),
17708c582c7cSDexuan Cui 	DEVMETHOD(bus_teardown_intr,	   bus_generic_teardown_intr),
17718c582c7cSDexuan Cui #if __FreeBSD_version >= 1100000
17728c582c7cSDexuan Cui 	DEVMETHOD(bus_get_cpus,			vmbus_pcib_get_cpus),
17738c582c7cSDexuan Cui #endif
17748c582c7cSDexuan Cui 
17758c582c7cSDexuan Cui 	/* pcib interface */
17768c582c7cSDexuan Cui 	DEVMETHOD(pcib_maxslots,		pcib_maxslots),
17778c582c7cSDexuan Cui 	DEVMETHOD(pcib_read_config,		vmbus_pcib_read_config),
17788c582c7cSDexuan Cui 	DEVMETHOD(pcib_write_config,		vmbus_pcib_write_config),
17798c582c7cSDexuan Cui 	DEVMETHOD(pcib_route_interrupt,		vmbus_pcib_route_intr),
17808c582c7cSDexuan Cui 	DEVMETHOD(pcib_alloc_msi,		vmbus_pcib_alloc_msi),
17818c582c7cSDexuan Cui 	DEVMETHOD(pcib_release_msi,		vmbus_pcib_release_msi),
17828c582c7cSDexuan Cui 	DEVMETHOD(pcib_alloc_msix,		vmbus_pcib_alloc_msix),
17838c582c7cSDexuan Cui 	DEVMETHOD(pcib_release_msix,		vmbus_pcib_release_msix),
17848c582c7cSDexuan Cui 	DEVMETHOD(pcib_map_msi,			vmbus_pcib_map_msi),
178528586889SWarner Losh 	DEVMETHOD(pcib_request_feature,		pcib_request_feature_allow),
17868c582c7cSDexuan Cui 
17878c582c7cSDexuan Cui 	DEVMETHOD_END
17888c582c7cSDexuan Cui };
17898c582c7cSDexuan Cui 
17908c582c7cSDexuan Cui static devclass_t pcib_devclass;
17918c582c7cSDexuan Cui 
17928c582c7cSDexuan Cui DEFINE_CLASS_0(pcib, vmbus_pcib_driver, vmbus_pcib_methods,
17938c582c7cSDexuan Cui 		sizeof(struct vmbus_pcib_softc));
17948c582c7cSDexuan Cui DRIVER_MODULE(vmbus_pcib, vmbus, vmbus_pcib_driver, pcib_devclass, 0, 0);
17958c582c7cSDexuan Cui MODULE_DEPEND(vmbus_pcib, vmbus, 1, 1, 1);
17968c582c7cSDexuan Cui MODULE_DEPEND(vmbus_pcib, pci, 1, 1, 1);
1797cdb316eeSDexuan Cui 
1798cdb316eeSDexuan Cui #endif /* NEW_PCIB */
1799