]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/pci/host/pci-hyperv.c
PCI: hv: Serialize the present and eject work items
[mirror_ubuntu-bionic-kernel.git] / drivers / pci / host / pci-hyperv.c
CommitLineData
4daace0d
JO
1/*
2 * Copyright (c) Microsoft Corporation.
3 *
4 * Author:
5 * Jake Oshins <jakeo@microsoft.com>
6 *
7 * This driver acts as a paravirtual front-end for PCI Express root buses.
8 * When a PCI Express function (either an entire device or an SR-IOV
9 * Virtual Function) is being passed through to the VM, this driver exposes
10 * a new bus to the guest VM. This is modeled as a root PCI bus because
11 * no bridges are being exposed to the VM. In fact, with a "Generation 2"
12 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
13 * until a device as been exposed using this driver.
14 *
15 * Each root PCI bus has its own PCI domain, which is called "Segment" in
16 * the PCI Firmware Specifications. Thus while each device passed through
17 * to the VM using this front-end will appear at "device 0", the domain will
18 * be unique. Typically, each bus will have one PCI function on it, though
19 * this driver does support more than one.
20 *
21 * In order to map the interrupts from the device through to the guest VM,
22 * this driver also implements an IRQ Domain, which handles interrupts (either
23 * MSI or MSI-X) associated with the functions on the bus. As interrupts are
24 * set up, torn down, or reaffined, this driver communicates with the
25 * underlying hypervisor to adjust the mappings in the I/O MMU so that each
26 * interrupt will be delivered to the correct virtual processor at the right
27 * vector. This driver does not support level-triggered (line-based)
28 * interrupts, and will report that the Interrupt Line register in the
29 * function's configuration space is zero.
30 *
31 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
32 * facilities. For instance, the configuration space of a function exposed
33 * by Hyper-V is mapped into a single page of memory space, and the
34 * read and write handlers for config space must be aware of this mechanism.
35 * Similarly, device setup and teardown involves messages sent to and from
36 * the PCI back-end driver in Hyper-V.
37 *
38 * This program is free software; you can redistribute it and/or modify it
39 * under the terms of the GNU General Public License version 2 as published
40 * by the Free Software Foundation.
41 *
42 * This program is distributed in the hope that it will be useful, but
43 * WITHOUT ANY WARRANTY; without even the implied warranty of
44 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
45 * NON INFRINGEMENT. See the GNU General Public License for more
46 * details.
47 *
48 */
49
50#include <linux/kernel.h>
51#include <linux/module.h>
52#include <linux/pci.h>
80bfeeb9 53#include <linux/delay.h>
4daace0d
JO
54#include <linux/semaphore.h>
55#include <linux/irqdomain.h>
56#include <asm/irqdomain.h>
57#include <asm/apic.h>
58#include <linux/msi.h>
59#include <linux/hyperv.h>
24196f0c 60#include <linux/refcount.h>
4daace0d
JO
61#include <asm/mshyperv.h>
62
63/*
64 * Protocol versions. The low word is the minor version, the high word the
65 * major version.
66 */
67
b1db7e7e 68#define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
4daace0d
JO
69#define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
70#define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
71
b1db7e7e
JL
72enum pci_protocol_version_t {
73 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */
7dcf90e9 74 PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */
4daace0d
JO
75};
76
433fcf6b 77#define CPU_AFFINITY_ALL -1ULL
b1db7e7e
JL
78
79/*
80 * Supported protocol versions in the order of probing - highest go
81 * first.
82 */
83static enum pci_protocol_version_t pci_protocol_versions[] = {
7dcf90e9 84 PCI_PROTOCOL_VERSION_1_2,
b1db7e7e
JL
85 PCI_PROTOCOL_VERSION_1_1,
86};
87
88/*
89 * Protocol version negotiated by hv_pci_protocol_negotiation().
90 */
91static enum pci_protocol_version_t pci_protocol_version;
92
4daace0d
JO
93#define PCI_CONFIG_MMIO_LENGTH 0x2000
94#define CFG_PAGE_OFFSET 0x1000
95#define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
96
97#define MAX_SUPPORTED_MSI_MESSAGES 0x400
98
b1db7e7e
JL
99#define STATUS_REVISION_MISMATCH 0xC0000059
100
4daace0d
JO
101/*
102 * Message Types
103 */
104
105enum pci_message_type {
106 /*
107 * Version 1.1
108 */
109 PCI_MESSAGE_BASE = 0x42490000,
110 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
111 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
112 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
113 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
114 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
115 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
116 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
117 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
118 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
119 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
120 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
121 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
122 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
123 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
124 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
125 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
126 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
127 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
128 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
129 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
7dcf90e9
JL
130 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
131 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
132 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
4daace0d
JO
133 PCI_MESSAGE_MAXIMUM
134};
135
136/*
137 * Structures defining the virtual PCI Express protocol.
138 */
139
140union pci_version {
141 struct {
142 u16 minor_version;
143 u16 major_version;
144 } parts;
145 u32 version;
146} __packed;
147
148/*
149 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
150 * which is all this driver does. This representation is the one used in
151 * Windows, which is what is expected when sending this back and forth with
152 * the Hyper-V parent partition.
153 */
154union win_slot_encoding {
155 struct {
60e2e2fb
DC
156 u32 dev:5;
157 u32 func:3;
4daace0d
JO
158 u32 reserved:24;
159 } bits;
160 u32 slot;
161} __packed;
162
163/*
164 * Pretty much as defined in the PCI Specifications.
165 */
166struct pci_function_description {
167 u16 v_id; /* vendor ID */
168 u16 d_id; /* device ID */
169 u8 rev;
170 u8 prog_intf;
171 u8 subclass;
172 u8 base_class;
173 u32 subsystem_id;
174 union win_slot_encoding win_slot;
175 u32 ser; /* serial number */
176} __packed;
177
178/**
179 * struct hv_msi_desc
180 * @vector: IDT entry
181 * @delivery_mode: As defined in Intel's Programmer's
182 * Reference Manual, Volume 3, Chapter 8.
183 * @vector_count: Number of contiguous entries in the
184 * Interrupt Descriptor Table that are
185 * occupied by this Message-Signaled
186 * Interrupt. For "MSI", as first defined
187 * in PCI 2.2, this can be between 1 and
188 * 32. For "MSI-X," as first defined in PCI
189 * 3.0, this must be 1, as each MSI-X table
190 * entry would have its own descriptor.
191 * @reserved: Empty space
192 * @cpu_mask: All the target virtual processors.
193 */
194struct hv_msi_desc {
195 u8 vector;
196 u8 delivery_mode;
197 u16 vector_count;
198 u32 reserved;
199 u64 cpu_mask;
200} __packed;
201
7dcf90e9
JL
202/**
203 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
204 * @vector: IDT entry
205 * @delivery_mode: As defined in Intel's Programmer's
206 * Reference Manual, Volume 3, Chapter 8.
207 * @vector_count: Number of contiguous entries in the
208 * Interrupt Descriptor Table that are
209 * occupied by this Message-Signaled
210 * Interrupt. For "MSI", as first defined
211 * in PCI 2.2, this can be between 1 and
212 * 32. For "MSI-X," as first defined in PCI
213 * 3.0, this must be 1, as each MSI-X table
214 * entry would have its own descriptor.
215 * @processor_count: number of bits enabled in array.
216 * @processor_array: All the target virtual processors.
217 */
218struct hv_msi_desc2 {
219 u8 vector;
220 u8 delivery_mode;
221 u16 vector_count;
222 u16 processor_count;
223 u16 processor_array[32];
224} __packed;
225
4daace0d
JO
226/**
227 * struct tran_int_desc
228 * @reserved: unused, padding
229 * @vector_count: same as in hv_msi_desc
230 * @data: This is the "data payload" value that is
231 * written by the device when it generates
232 * a message-signaled interrupt, either MSI
233 * or MSI-X.
234 * @address: This is the address to which the data
235 * payload is written on interrupt
236 * generation.
237 */
238struct tran_int_desc {
239 u16 reserved;
240 u16 vector_count;
241 u32 data;
242 u64 address;
243} __packed;
244
245/*
246 * A generic message format for virtual PCI.
247 * Specific message formats are defined later in the file.
248 */
249
250struct pci_message {
0c6045d8 251 u32 type;
4daace0d
JO
252} __packed;
253
254struct pci_child_message {
0c6045d8 255 struct pci_message message_type;
4daace0d
JO
256 union win_slot_encoding wslot;
257} __packed;
258
259struct pci_incoming_message {
260 struct vmpacket_descriptor hdr;
261 struct pci_message message_type;
262} __packed;
263
264struct pci_response {
265 struct vmpacket_descriptor hdr;
266 s32 status; /* negative values are failures */
267} __packed;
268
269struct pci_packet {
270 void (*completion_func)(void *context, struct pci_response *resp,
271 int resp_packet_size);
272 void *compl_ctxt;
0c6045d8
DC
273
274 struct pci_message message[0];
4daace0d
JO
275};
276
277/*
278 * Specific message types supporting the PCI protocol.
279 */
280
281/*
282 * Version negotiation message. Sent from the guest to the host.
283 * The guest is free to try different versions until the host
284 * accepts the version.
285 *
286 * pci_version: The protocol version requested.
287 * is_last_attempt: If TRUE, this is the last version guest will request.
288 * reservedz: Reserved field, set to zero.
289 */
290
291struct pci_version_request {
292 struct pci_message message_type;
691ac1dc 293 u32 protocol_version;
4daace0d
JO
294} __packed;
295
296/*
297 * Bus D0 Entry. This is sent from the guest to the host when the virtual
298 * bus (PCI Express port) is ready for action.
299 */
300
301struct pci_bus_d0_entry {
302 struct pci_message message_type;
303 u32 reserved;
304 u64 mmio_base;
305} __packed;
306
307struct pci_bus_relations {
308 struct pci_incoming_message incoming;
309 u32 device_count;
7d0f8eec 310 struct pci_function_description func[0];
4daace0d
JO
311} __packed;
312
313struct pci_q_res_req_response {
314 struct vmpacket_descriptor hdr;
315 s32 status; /* negative values are failures */
316 u32 probed_bar[6];
317} __packed;
318
319struct pci_set_power {
320 struct pci_message message_type;
321 union win_slot_encoding wslot;
322 u32 power_state; /* In Windows terms */
323 u32 reserved;
324} __packed;
325
326struct pci_set_power_response {
327 struct vmpacket_descriptor hdr;
328 s32 status; /* negative values are failures */
329 union win_slot_encoding wslot;
330 u32 resultant_state; /* In Windows terms */
331 u32 reserved;
332} __packed;
333
334struct pci_resources_assigned {
335 struct pci_message message_type;
336 union win_slot_encoding wslot;
337 u8 memory_range[0x14][6]; /* not used here */
338 u32 msi_descriptors;
339 u32 reserved[4];
340} __packed;
341
7dcf90e9
JL
342struct pci_resources_assigned2 {
343 struct pci_message message_type;
344 union win_slot_encoding wslot;
345 u8 memory_range[0x14][6]; /* not used here */
346 u32 msi_descriptor_count;
347 u8 reserved[70];
348} __packed;
349
4daace0d
JO
350struct pci_create_interrupt {
351 struct pci_message message_type;
352 union win_slot_encoding wslot;
353 struct hv_msi_desc int_desc;
354} __packed;
355
356struct pci_create_int_response {
357 struct pci_response response;
358 u32 reserved;
359 struct tran_int_desc int_desc;
360} __packed;
361
7dcf90e9
JL
362struct pci_create_interrupt2 {
363 struct pci_message message_type;
364 union win_slot_encoding wslot;
365 struct hv_msi_desc2 int_desc;
366} __packed;
367
4daace0d
JO
368struct pci_delete_interrupt {
369 struct pci_message message_type;
370 union win_slot_encoding wslot;
371 struct tran_int_desc int_desc;
372} __packed;
373
374struct pci_dev_incoming {
375 struct pci_incoming_message incoming;
376 union win_slot_encoding wslot;
377} __packed;
378
379struct pci_eject_response {
0c6045d8 380 struct pci_message message_type;
4daace0d
JO
381 union win_slot_encoding wslot;
382 u32 status;
383} __packed;
384
385static int pci_ring_size = (4 * PAGE_SIZE);
386
387/*
388 * Definitions or interrupt steering hypercall.
389 */
390#define HV_PARTITION_ID_SELF ((u64)-1)
391#define HVCALL_RETARGET_INTERRUPT 0x7e
392
7dcf90e9 393struct hv_interrupt_entry {
4daace0d
JO
394 u32 source; /* 1 for MSI(-X) */
395 u32 reserved1;
396 u32 address;
397 u32 data;
7dcf90e9
JL
398};
399
400#define HV_VP_SET_BANK_COUNT_MAX 5 /* current implementation limit */
401
402struct hv_vp_set {
403 u64 format; /* 0 (HvGenericSetSparse4k) */
404 u64 valid_banks;
405 u64 masks[HV_VP_SET_BANK_COUNT_MAX];
406};
407
408/*
409 * flags for hv_device_interrupt_target.flags
410 */
411#define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
412#define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
413
414struct hv_device_interrupt_target {
4daace0d
JO
415 u32 vector;
416 u32 flags;
7dcf90e9
JL
417 union {
418 u64 vp_mask;
419 struct hv_vp_set vp_set;
420 };
421};
422
423struct retarget_msi_interrupt {
424 u64 partition_id; /* use "self" */
425 u64 device_id;
426 struct hv_interrupt_entry int_entry;
427 u64 reserved2;
428 struct hv_device_interrupt_target int_target;
4daace0d
JO
429} __packed;
430
431/*
432 * Driver specific state.
433 */
434
435enum hv_pcibus_state {
436 hv_pcibus_init = 0,
437 hv_pcibus_probed,
438 hv_pcibus_installed,
d3a78d8b 439 hv_pcibus_removed,
4daace0d
JO
440 hv_pcibus_maximum
441};
442
443struct hv_pcibus_device {
444 struct pci_sysdata sysdata;
445 enum hv_pcibus_state state;
446 atomic_t remove_lock;
447 struct hv_device *hdev;
448 resource_size_t low_mmio_space;
449 resource_size_t high_mmio_space;
450 struct resource *mem_config;
451 struct resource *low_mmio_res;
452 struct resource *high_mmio_res;
453 struct completion *survey_event;
454 struct completion remove_event;
455 struct pci_bus *pci_bus;
456 spinlock_t config_lock; /* Avoid two threads writing index page */
457 spinlock_t device_list_lock; /* Protect lists below */
458 void __iomem *cfg_addr;
459
4daace0d
JO
460 struct list_head resources_for_children;
461
462 struct list_head children;
463 struct list_head dr_list;
4daace0d
JO
464
465 struct msi_domain_info msi_info;
466 struct msi_controller msi_chip;
467 struct irq_domain *irq_domain;
be66b673
JL
468
469 /* hypercall arg, must not cross page boundary */
0de8ce3e 470 struct retarget_msi_interrupt retarget_msi_interrupt_params;
be66b673 471
0de8ce3e 472 spinlock_t retarget_msi_interrupt_lock;
9053ead2
DC
473
474 struct workqueue_struct *wq;
4daace0d
JO
475};
476
477/*
478 * Tracks "Device Relations" messages from the host, which must be both
479 * processed in order and deferred so that they don't run in the context
480 * of the incoming packet callback.
481 */
482struct hv_dr_work {
483 struct work_struct wrk;
484 struct hv_pcibus_device *bus;
485};
486
487struct hv_dr_state {
488 struct list_head list_entry;
489 u32 device_count;
7d0f8eec 490 struct pci_function_description func[0];
4daace0d
JO
491};
492
493enum hv_pcichild_state {
494 hv_pcichild_init = 0,
495 hv_pcichild_requirements,
496 hv_pcichild_resourced,
497 hv_pcichild_ejecting,
498 hv_pcichild_maximum
499};
500
501enum hv_pcidev_ref_reason {
502 hv_pcidev_ref_invalid = 0,
503 hv_pcidev_ref_initial,
504 hv_pcidev_ref_by_slot,
505 hv_pcidev_ref_packet,
506 hv_pcidev_ref_pnp,
507 hv_pcidev_ref_childlist,
508 hv_pcidev_irqdata,
509 hv_pcidev_ref_max
510};
511
512struct hv_pci_dev {
513 /* List protected by pci_rescan_remove_lock */
514 struct list_head list_entry;
24196f0c 515 refcount_t refs;
4daace0d
JO
516 enum hv_pcichild_state state;
517 struct pci_function_description desc;
518 bool reported_missing;
519 struct hv_pcibus_device *hbus;
520 struct work_struct wrk;
521
522 /*
523 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
524 * read it back, for each of the BAR offsets within config space.
525 */
526 u32 probed_bar[6];
527};
528
529struct hv_pci_compl {
530 struct completion host_event;
531 s32 completion_status;
532};
533
534/**
535 * hv_pci_generic_compl() - Invoked for a completion packet
536 * @context: Set up by the sender of the packet.
537 * @resp: The response packet
538 * @resp_packet_size: Size in bytes of the packet
539 *
540 * This function is used to trigger an event and report status
541 * for any message for which the completion packet contains a
542 * status and nothing else.
543 */
a5b45b7b
DC
544static void hv_pci_generic_compl(void *context, struct pci_response *resp,
545 int resp_packet_size)
4daace0d
JO
546{
547 struct hv_pci_compl *comp_pkt = context;
548
549 if (resp_packet_size >= offsetofend(struct pci_response, status))
550 comp_pkt->completion_status = resp->status;
a5b45b7b
DC
551 else
552 comp_pkt->completion_status = -1;
553
4daace0d
JO
554 complete(&comp_pkt->host_event);
555}
556
557static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
558 u32 wslot);
559static void get_pcichild(struct hv_pci_dev *hv_pcidev,
560 enum hv_pcidev_ref_reason reason);
561static void put_pcichild(struct hv_pci_dev *hv_pcidev,
562 enum hv_pcidev_ref_reason reason);
563
564static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
565static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
566
567/**
568 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
569 * @devfn: The Linux representation of PCI slot
570 *
571 * Windows uses a slightly different representation of PCI slot.
572 *
573 * Return: The Windows representation
574 */
575static u32 devfn_to_wslot(int devfn)
576{
577 union win_slot_encoding wslot;
578
579 wslot.slot = 0;
60e2e2fb
DC
580 wslot.bits.dev = PCI_SLOT(devfn);
581 wslot.bits.func = PCI_FUNC(devfn);
4daace0d
JO
582
583 return wslot.slot;
584}
585
586/**
587 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
588 * @wslot: The Windows representation of PCI slot
589 *
590 * Windows uses a slightly different representation of PCI slot.
591 *
592 * Return: The Linux representation
593 */
594static int wslot_to_devfn(u32 wslot)
595{
596 union win_slot_encoding slot_no;
597
598 slot_no.slot = wslot;
60e2e2fb 599 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
4daace0d
JO
600}
601
602/*
603 * PCI Configuration Space for these root PCI buses is implemented as a pair
604 * of pages in memory-mapped I/O space. Writing to the first page chooses
605 * the PCI function being written or read. Once the first page has been
606 * written to, the following page maps in the entire configuration space of
607 * the function.
608 */
609
610/**
611 * _hv_pcifront_read_config() - Internal PCI config read
612 * @hpdev: The PCI driver's representation of the device
613 * @where: Offset within config space
614 * @size: Size of the transfer
615 * @val: Pointer to the buffer receiving the data
616 */
617static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
618 int size, u32 *val)
619{
620 unsigned long flags;
621 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
622
623 /*
624 * If the attempt is to read the IDs or the ROM BAR, simulate that.
625 */
626 if (where + size <= PCI_COMMAND) {
627 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
628 } else if (where >= PCI_CLASS_REVISION && where + size <=
629 PCI_CACHE_LINE_SIZE) {
630 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
631 PCI_CLASS_REVISION, size);
632 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
633 PCI_ROM_ADDRESS) {
634 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
635 PCI_SUBSYSTEM_VENDOR_ID, size);
636 } else if (where >= PCI_ROM_ADDRESS && where + size <=
637 PCI_CAPABILITY_LIST) {
638 /* ROM BARs are unimplemented */
639 *val = 0;
640 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
641 PCI_INTERRUPT_PIN) {
642 /*
643 * Interrupt Line and Interrupt PIN are hard-wired to zero
644 * because this front-end only supports message-signaled
645 * interrupts.
646 */
647 *val = 0;
648 } else if (where + size <= CFG_PAGE_SIZE) {
649 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
650 /* Choose the function to be read. (See comment above) */
651 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
bdd74440
VK
652 /* Make sure the function was chosen before we start reading. */
653 mb();
4daace0d
JO
654 /* Read from that function's config space. */
655 switch (size) {
656 case 1:
657 *val = readb(addr);
658 break;
659 case 2:
660 *val = readw(addr);
661 break;
662 default:
663 *val = readl(addr);
664 break;
665 }
bdd74440
VK
666 /*
667 * Make sure the write was done before we release the spinlock
668 * allowing consecutive reads/writes.
669 */
670 mb();
4daace0d
JO
671 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
672 } else {
673 dev_err(&hpdev->hbus->hdev->device,
674 "Attempt to read beyond a function's config space.\n");
675 }
676}
677
678/**
679 * _hv_pcifront_write_config() - Internal PCI config write
680 * @hpdev: The PCI driver's representation of the device
681 * @where: Offset within config space
682 * @size: Size of the transfer
683 * @val: The data being transferred
684 */
685static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
686 int size, u32 val)
687{
688 unsigned long flags;
689 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
690
691 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
692 where + size <= PCI_CAPABILITY_LIST) {
693 /* SSIDs and ROM BARs are read-only */
694 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
695 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
696 /* Choose the function to be written. (See comment above) */
697 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
bdd74440
VK
698 /* Make sure the function was chosen before we start writing. */
699 wmb();
4daace0d
JO
700 /* Write to that function's config space. */
701 switch (size) {
702 case 1:
703 writeb(val, addr);
704 break;
705 case 2:
706 writew(val, addr);
707 break;
708 default:
709 writel(val, addr);
710 break;
711 }
bdd74440
VK
712 /*
713 * Make sure the write was done before we release the spinlock
714 * allowing consecutive reads/writes.
715 */
716 mb();
4daace0d
JO
717 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
718 } else {
719 dev_err(&hpdev->hbus->hdev->device,
720 "Attempt to write beyond a function's config space.\n");
721 }
722}
723
724/**
725 * hv_pcifront_read_config() - Read configuration space
726 * @bus: PCI Bus structure
727 * @devfn: Device/function
728 * @where: Offset from base
729 * @size: Byte/word/dword
730 * @val: Value to be read
731 *
732 * Return: PCIBIOS_SUCCESSFUL on success
733 * PCIBIOS_DEVICE_NOT_FOUND on failure
734 */
735static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
736 int where, int size, u32 *val)
737{
738 struct hv_pcibus_device *hbus =
739 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
740 struct hv_pci_dev *hpdev;
741
742 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
743 if (!hpdev)
744 return PCIBIOS_DEVICE_NOT_FOUND;
745
746 _hv_pcifront_read_config(hpdev, where, size, val);
747
748 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
749 return PCIBIOS_SUCCESSFUL;
750}
751
752/**
753 * hv_pcifront_write_config() - Write configuration space
754 * @bus: PCI Bus structure
755 * @devfn: Device/function
756 * @where: Offset from base
757 * @size: Byte/word/dword
758 * @val: Value to be written to device
759 *
760 * Return: PCIBIOS_SUCCESSFUL on success
761 * PCIBIOS_DEVICE_NOT_FOUND on failure
762 */
763static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
764 int where, int size, u32 val)
765{
766 struct hv_pcibus_device *hbus =
767 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
768 struct hv_pci_dev *hpdev;
769
770 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
771 if (!hpdev)
772 return PCIBIOS_DEVICE_NOT_FOUND;
773
774 _hv_pcifront_write_config(hpdev, where, size, val);
775
776 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
777 return PCIBIOS_SUCCESSFUL;
778}
779
780/* PCIe operations */
781static struct pci_ops hv_pcifront_ops = {
782 .read = hv_pcifront_read_config,
783 .write = hv_pcifront_write_config,
784};
785
786/* Interrupt management hooks */
787static void hv_int_desc_free(struct hv_pci_dev *hpdev,
788 struct tran_int_desc *int_desc)
789{
790 struct pci_delete_interrupt *int_pkt;
791 struct {
792 struct pci_packet pkt;
0c6045d8 793 u8 buffer[sizeof(struct pci_delete_interrupt)];
4daace0d
JO
794 } ctxt;
795
796 memset(&ctxt, 0, sizeof(ctxt));
797 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
0c6045d8 798 int_pkt->message_type.type =
4daace0d
JO
799 PCI_DELETE_INTERRUPT_MESSAGE;
800 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
801 int_pkt->int_desc = *int_desc;
802 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
803 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
804 kfree(int_desc);
805}
806
807/**
808 * hv_msi_free() - Free the MSI.
809 * @domain: The interrupt domain pointer
810 * @info: Extra MSI-related context
811 * @irq: Identifies the IRQ.
812 *
813 * The Hyper-V parent partition and hypervisor are tracking the
814 * messages that are in use, keeping the interrupt redirection
815 * table up to date. This callback sends a message that frees
816 * the IRT entry and related tracking nonsense.
817 */
818static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
819 unsigned int irq)
820{
821 struct hv_pcibus_device *hbus;
822 struct hv_pci_dev *hpdev;
823 struct pci_dev *pdev;
824 struct tran_int_desc *int_desc;
825 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
826 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
827
828 pdev = msi_desc_to_pci_dev(msi);
829 hbus = info->data;
0c6e617f
CA
830 int_desc = irq_data_get_irq_chip_data(irq_data);
831 if (!int_desc)
4daace0d
JO
832 return;
833
0c6e617f
CA
834 irq_data->chip_data = NULL;
835 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
836 if (!hpdev) {
837 kfree(int_desc);
838 return;
4daace0d
JO
839 }
840
0c6e617f 841 hv_int_desc_free(hpdev, int_desc);
4daace0d
JO
842 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
843}
844
845static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
846 bool force)
847{
848 struct irq_data *parent = data->parent_data;
849
850 return parent->chip->irq_set_affinity(parent, dest, force);
851}
852
542ccf45 853static void hv_irq_mask(struct irq_data *data)
4daace0d
JO
854{
855 pci_msi_mask_irq(data);
856}
857
858/**
859 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
860 * affinity.
861 * @data: Describes the IRQ
862 *
863 * Build new a destination for the MSI and make a hypercall to
864 * update the Interrupt Redirection Table. "Device Logical ID"
865 * is built out of this PCI bus's instance GUID and the function
866 * number of the device.
867 */
542ccf45 868static void hv_irq_unmask(struct irq_data *data)
4daace0d
JO
869{
870 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
871 struct irq_cfg *cfg = irqd_cfg(data);
0de8ce3e 872 struct retarget_msi_interrupt *params;
4daace0d
JO
873 struct hv_pcibus_device *hbus;
874 struct cpumask *dest;
875 struct pci_bus *pbus;
876 struct pci_dev *pdev;
0de8ce3e 877 unsigned long flags;
7dcf90e9
JL
878 u32 var_size = 0;
879 int cpu_vmbus;
880 int cpu;
881 u64 res;
4daace0d 882
79aa801e 883 dest = irq_data_get_effective_affinity_mask(data);
4daace0d
JO
884 pdev = msi_desc_to_pci_dev(msi_desc);
885 pbus = pdev->bus;
886 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
887
0de8ce3e
LL
888 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
889
890 params = &hbus->retarget_msi_interrupt_params;
891 memset(params, 0, sizeof(*params));
892 params->partition_id = HV_PARTITION_ID_SELF;
7dcf90e9
JL
893 params->int_entry.source = 1; /* MSI(-X) */
894 params->int_entry.address = msi_desc->msg.address_lo;
895 params->int_entry.data = msi_desc->msg.data;
0de8ce3e 896 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
4daace0d
JO
897 (hbus->hdev->dev_instance.b[4] << 16) |
898 (hbus->hdev->dev_instance.b[7] << 8) |
899 (hbus->hdev->dev_instance.b[6] & 0xf8) |
900 PCI_FUNC(pdev->devfn);
7dcf90e9
JL
901 params->int_target.vector = cfg->vector;
902
903 /*
904 * Honoring apic->irq_delivery_mode set to dest_Fixed by
905 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
906 * spurious interrupt storm. Not doing so does not seem to have a
907 * negative effect (yet?).
908 */
909
910 if (pci_protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
911 /*
912 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
913 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
914 * with >64 VP support.
915 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
916 * is not sufficient for this hypercall.
917 */
918 params->int_target.flags |=
919 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
920 params->int_target.vp_set.valid_banks =
921 (1ull << HV_VP_SET_BANK_COUNT_MAX) - 1;
922
923 /*
924 * var-sized hypercall, var-size starts after vp_mask (thus
925 * vp_set.format does not count, but vp_set.valid_banks does).
926 */
927 var_size = 1 + HV_VP_SET_BANK_COUNT_MAX;
928
929 for_each_cpu_and(cpu, dest, cpu_online_mask) {
7415aea6 930 cpu_vmbus = hv_cpu_number_to_vp_number(cpu);
7dcf90e9
JL
931
932 if (cpu_vmbus >= HV_VP_SET_BANK_COUNT_MAX * 64) {
933 dev_err(&hbus->hdev->device,
934 "too high CPU %d", cpu_vmbus);
935 res = 1;
936 goto exit_unlock;
937 }
4daace0d 938
7dcf90e9
JL
939 params->int_target.vp_set.masks[cpu_vmbus / 64] |=
940 (1ULL << (cpu_vmbus & 63));
941 }
942 } else {
943 for_each_cpu_and(cpu, dest, cpu_online_mask) {
944 params->int_target.vp_mask |=
7415aea6 945 (1ULL << hv_cpu_number_to_vp_number(cpu));
7dcf90e9
JL
946 }
947 }
0de8ce3e 948
7dcf90e9
JL
949 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
950 params, NULL);
4daace0d 951
7dcf90e9 952exit_unlock:
0de8ce3e 953 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
4daace0d 954
7dcf90e9
JL
955 if (res) {
956 dev_err(&hbus->hdev->device,
957 "%s() failed: %#llx", __func__, res);
958 return;
959 }
960
4daace0d
JO
961 pci_msi_unmask_irq(data);
962}
963
964struct compose_comp_ctxt {
965 struct hv_pci_compl comp_pkt;
966 struct tran_int_desc int_desc;
967};
968
969static void hv_pci_compose_compl(void *context, struct pci_response *resp,
970 int resp_packet_size)
971{
972 struct compose_comp_ctxt *comp_pkt = context;
973 struct pci_create_int_response *int_resp =
974 (struct pci_create_int_response *)resp;
975
976 comp_pkt->comp_pkt.completion_status = resp->status;
977 comp_pkt->int_desc = int_resp->int_desc;
978 complete(&comp_pkt->comp_pkt.host_event);
979}
980
7dcf90e9
JL
981static u32 hv_compose_msi_req_v1(
982 struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
983 u32 slot, u8 vector)
984{
985 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
986 int_pkt->wslot.slot = slot;
987 int_pkt->int_desc.vector = vector;
988 int_pkt->int_desc.vector_count = 1;
a31e58e1 989 int_pkt->int_desc.delivery_mode = dest_Fixed;
7dcf90e9
JL
990
991 /*
992 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
993 * hv_irq_unmask().
994 */
995 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
996
997 return sizeof(*int_pkt);
998}
999
1000static u32 hv_compose_msi_req_v2(
1001 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
1002 u32 slot, u8 vector)
1003{
1004 int cpu;
1005
1006 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1007 int_pkt->wslot.slot = slot;
1008 int_pkt->int_desc.vector = vector;
1009 int_pkt->int_desc.vector_count = 1;
a31e58e1 1010 int_pkt->int_desc.delivery_mode = dest_Fixed;
7dcf90e9
JL
1011
1012 /*
1013 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1014 * by subsequent retarget in hv_irq_unmask().
1015 */
1016 cpu = cpumask_first_and(affinity, cpu_online_mask);
1017 int_pkt->int_desc.processor_array[0] =
7415aea6 1018 hv_cpu_number_to_vp_number(cpu);
7dcf90e9
JL
1019 int_pkt->int_desc.processor_count = 1;
1020
1021 return sizeof(*int_pkt);
1022}
1023
4daace0d
JO
1024/**
1025 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1026 * @data: Everything about this MSI
1027 * @msg: Buffer that is filled in by this function
1028 *
1029 * This function unpacks the IRQ looking for target CPU set, IDT
1030 * vector and mode and sends a message to the parent partition
1031 * asking for a mapping for that tuple in this partition. The
1032 * response supplies a data value and address to which that data
1033 * should be written to trigger that interrupt.
1034 */
1035static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1036{
1037 struct irq_cfg *cfg = irqd_cfg(data);
1038 struct hv_pcibus_device *hbus;
1039 struct hv_pci_dev *hpdev;
1040 struct pci_bus *pbus;
1041 struct pci_dev *pdev;
79aa801e 1042 struct cpumask *dest;
4daace0d
JO
1043 struct compose_comp_ctxt comp;
1044 struct tran_int_desc *int_desc;
4daace0d 1045 struct {
7dcf90e9
JL
1046 struct pci_packet pci_pkt;
1047 union {
1048 struct pci_create_interrupt v1;
1049 struct pci_create_interrupt2 v2;
1050 } int_pkts;
1051 } __packed ctxt;
1052
1053 u32 size;
4daace0d
JO
1054 int ret;
1055
1056 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
79aa801e 1057 dest = irq_data_get_effective_affinity_mask(data);
4daace0d
JO
1058 pbus = pdev->bus;
1059 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1060 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1061 if (!hpdev)
1062 goto return_null_message;
1063
1064 /* Free any previous message that might have already been composed. */
1065 if (data->chip_data) {
1066 int_desc = data->chip_data;
1067 data->chip_data = NULL;
1068 hv_int_desc_free(hpdev, int_desc);
1069 }
1070
59c58cee 1071 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
4daace0d
JO
1072 if (!int_desc)
1073 goto drop_reference;
1074
1075 memset(&ctxt, 0, sizeof(ctxt));
1076 init_completion(&comp.comp_pkt.host_event);
7dcf90e9
JL
1077 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1078 ctxt.pci_pkt.compl_ctxt = &comp;
1079
1080 switch (pci_protocol_version) {
1081 case PCI_PROTOCOL_VERSION_1_1:
1082 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
79aa801e 1083 dest,
7dcf90e9
JL
1084 hpdev->desc.win_slot.slot,
1085 cfg->vector);
1086 break;
4daace0d 1087
7dcf90e9
JL
1088 case PCI_PROTOCOL_VERSION_1_2:
1089 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
79aa801e 1090 dest,
7dcf90e9
JL
1091 hpdev->desc.win_slot.slot,
1092 cfg->vector);
1093 break;
1094
1095 default:
1096 /* As we only negotiate protocol versions known to this driver,
1097 * this path should never hit. However, this is it not a hot
1098 * path so we print a message to aid future updates.
1099 */
1100 dev_err(&hbus->hdev->device,
1101 "Unexpected vPCI protocol, update driver.");
1102 goto free_int_desc;
4daace0d
JO
1103 }
1104
7dcf90e9
JL
1105 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1106 size, (unsigned long)&ctxt.pci_pkt,
4daace0d
JO
1107 VM_PKT_DATA_INBAND,
1108 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
7dcf90e9
JL
1109 if (ret) {
1110 dev_err(&hbus->hdev->device,
1111 "Sending request for interrupt failed: 0x%x",
1112 comp.comp_pkt.completion_status);
665e2245 1113 goto free_int_desc;
7dcf90e9 1114 }
665e2245 1115
80bfeeb9
SH
1116 /*
1117 * Since this function is called with IRQ locks held, can't
1118 * do normal wait for completion; instead poll.
1119 */
1120 while (!try_wait_for_completion(&comp.comp_pkt.host_event))
1121 udelay(100);
4daace0d
JO
1122
1123 if (comp.comp_pkt.completion_status < 0) {
1124 dev_err(&hbus->hdev->device,
1125 "Request for interrupt failed: 0x%x",
1126 comp.comp_pkt.completion_status);
1127 goto free_int_desc;
1128 }
1129
1130 /*
1131 * Record the assignment so that this can be unwound later. Using
1132 * irq_set_chip_data() here would be appropriate, but the lock it takes
1133 * is already held.
1134 */
1135 *int_desc = comp.int_desc;
1136 data->chip_data = int_desc;
1137
1138 /* Pass up the result. */
1139 msg->address_hi = comp.int_desc.address >> 32;
1140 msg->address_lo = comp.int_desc.address & 0xffffffff;
1141 msg->data = comp.int_desc.data;
1142
1143 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
1144 return;
1145
1146free_int_desc:
1147 kfree(int_desc);
1148drop_reference:
1149 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
1150return_null_message:
1151 msg->address_hi = 0;
1152 msg->address_lo = 0;
1153 msg->data = 0;
1154}
1155
1156/* HW Interrupt Chip Descriptor */
1157static struct irq_chip hv_msi_irq_chip = {
1158 .name = "Hyper-V PCIe MSI",
1159 .irq_compose_msi_msg = hv_compose_msi_msg,
1160 .irq_set_affinity = hv_set_affinity,
1161 .irq_ack = irq_chip_ack_parent,
1162 .irq_mask = hv_irq_mask,
1163 .irq_unmask = hv_irq_unmask,
1164};
1165
1166static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
1167 msi_alloc_info_t *arg)
1168{
1169 return arg->msi_hwirq;
1170}
1171
1172static struct msi_domain_ops hv_msi_ops = {
1173 .get_hwirq = hv_msi_domain_ops_get_hwirq,
1174 .msi_prepare = pci_msi_prepare,
1175 .set_desc = pci_msi_set_desc,
1176 .msi_free = hv_msi_free,
1177};
1178
1179/**
1180 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1181 * @hbus: The root PCI bus
1182 *
1183 * This function creates an IRQ domain which will be used for
1184 * interrupts from devices that have been passed through. These
1185 * devices only support MSI and MSI-X, not line-based interrupts
1186 * or simulations of line-based interrupts through PCIe's
1187 * fabric-layer messages. Because interrupts are remapped, we
1188 * can support multi-message MSI here.
1189 *
1190 * Return: '0' on success and error value on failure
1191 */
1192static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1193{
1194 hbus->msi_info.chip = &hv_msi_irq_chip;
1195 hbus->msi_info.ops = &hv_msi_ops;
1196 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1197 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1198 MSI_FLAG_PCI_MSIX);
1199 hbus->msi_info.handler = handle_edge_irq;
1200 hbus->msi_info.handler_name = "edge";
1201 hbus->msi_info.data = hbus;
1202 hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
1203 &hbus->msi_info,
1204 x86_vector_domain);
1205 if (!hbus->irq_domain) {
1206 dev_err(&hbus->hdev->device,
1207 "Failed to build an MSI IRQ domain\n");
1208 return -ENODEV;
1209 }
1210
1211 return 0;
1212}
1213
1214/**
1215 * get_bar_size() - Get the address space consumed by a BAR
1216 * @bar_val: Value that a BAR returned after -1 was written
1217 * to it.
1218 *
1219 * This function returns the size of the BAR, rounded up to 1
1220 * page. It has to be rounded up because the hypervisor's page
1221 * table entry that maps the BAR into the VM can't specify an
1222 * offset within a page. The invariant is that the hypervisor
1223 * must place any BARs of smaller than page length at the
1224 * beginning of a page.
1225 *
1226 * Return: Size in bytes of the consumed MMIO space.
1227 */
1228static u64 get_bar_size(u64 bar_val)
1229{
1230 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1231 PAGE_SIZE);
1232}
1233
1234/**
1235 * survey_child_resources() - Total all MMIO requirements
1236 * @hbus: Root PCI bus, as understood by this driver
1237 */
1238static void survey_child_resources(struct hv_pcibus_device *hbus)
1239{
1240 struct list_head *iter;
1241 struct hv_pci_dev *hpdev;
1242 resource_size_t bar_size = 0;
1243 unsigned long flags;
1244 struct completion *event;
1245 u64 bar_val;
1246 int i;
1247
1248 /* If nobody is waiting on the answer, don't compute it. */
1249 event = xchg(&hbus->survey_event, NULL);
1250 if (!event)
1251 return;
1252
1253 /* If the answer has already been computed, go with it. */
1254 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1255 complete(event);
1256 return;
1257 }
1258
1259 spin_lock_irqsave(&hbus->device_list_lock, flags);
1260
1261 /*
1262 * Due to an interesting quirk of the PCI spec, all memory regions
1263 * for a child device are a power of 2 in size and aligned in memory,
1264 * so it's sufficient to just add them up without tracking alignment.
1265 */
1266 list_for_each(iter, &hbus->children) {
1267 hpdev = container_of(iter, struct hv_pci_dev, list_entry);
1268 for (i = 0; i < 6; i++) {
1269 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1270 dev_err(&hbus->hdev->device,
1271 "There's an I/O BAR in this list!\n");
1272
1273 if (hpdev->probed_bar[i] != 0) {
1274 /*
1275 * A probed BAR has all the upper bits set that
1276 * can be changed.
1277 */
1278
1279 bar_val = hpdev->probed_bar[i];
1280 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1281 bar_val |=
1282 ((u64)hpdev->probed_bar[++i] << 32);
1283 else
1284 bar_val |= 0xffffffff00000000ULL;
1285
1286 bar_size = get_bar_size(bar_val);
1287
1288 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1289 hbus->high_mmio_space += bar_size;
1290 else
1291 hbus->low_mmio_space += bar_size;
1292 }
1293 }
1294 }
1295
1296 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1297 complete(event);
1298}
1299
1300/**
1301 * prepopulate_bars() - Fill in BARs with defaults
1302 * @hbus: Root PCI bus, as understood by this driver
1303 *
1304 * The core PCI driver code seems much, much happier if the BARs
1305 * for a device have values upon first scan. So fill them in.
1306 * The algorithm below works down from large sizes to small,
1307 * attempting to pack the assignments optimally. The assumption,
1308 * enforced in other parts of the code, is that the beginning of
1309 * the memory-mapped I/O space will be aligned on the largest
1310 * BAR size.
1311 */
1312static void prepopulate_bars(struct hv_pcibus_device *hbus)
1313{
1314 resource_size_t high_size = 0;
1315 resource_size_t low_size = 0;
1316 resource_size_t high_base = 0;
1317 resource_size_t low_base = 0;
1318 resource_size_t bar_size;
1319 struct hv_pci_dev *hpdev;
1320 struct list_head *iter;
1321 unsigned long flags;
1322 u64 bar_val;
1323 u32 command;
1324 bool high;
1325 int i;
1326
1327 if (hbus->low_mmio_space) {
1328 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
1329 low_base = hbus->low_mmio_res->start;
1330 }
1331
1332 if (hbus->high_mmio_space) {
1333 high_size = 1ULL <<
1334 (63 - __builtin_clzll(hbus->high_mmio_space));
1335 high_base = hbus->high_mmio_res->start;
1336 }
1337
1338 spin_lock_irqsave(&hbus->device_list_lock, flags);
1339
1340 /* Pick addresses for the BARs. */
1341 do {
1342 list_for_each(iter, &hbus->children) {
1343 hpdev = container_of(iter, struct hv_pci_dev,
1344 list_entry);
1345 for (i = 0; i < 6; i++) {
1346 bar_val = hpdev->probed_bar[i];
1347 if (bar_val == 0)
1348 continue;
1349 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
1350 if (high) {
1351 bar_val |=
1352 ((u64)hpdev->probed_bar[i + 1]
1353 << 32);
1354 } else {
1355 bar_val |= 0xffffffffULL << 32;
1356 }
1357 bar_size = get_bar_size(bar_val);
1358 if (high) {
1359 if (high_size != bar_size) {
1360 i++;
1361 continue;
1362 }
1363 _hv_pcifront_write_config(hpdev,
1364 PCI_BASE_ADDRESS_0 + (4 * i),
1365 4,
1366 (u32)(high_base & 0xffffff00));
1367 i++;
1368 _hv_pcifront_write_config(hpdev,
1369 PCI_BASE_ADDRESS_0 + (4 * i),
1370 4, (u32)(high_base >> 32));
1371 high_base += bar_size;
1372 } else {
1373 if (low_size != bar_size)
1374 continue;
1375 _hv_pcifront_write_config(hpdev,
1376 PCI_BASE_ADDRESS_0 + (4 * i),
1377 4,
1378 (u32)(low_base & 0xffffff00));
1379 low_base += bar_size;
1380 }
1381 }
1382 if (high_size <= 1 && low_size <= 1) {
1383 /* Set the memory enable bit. */
1384 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
1385 &command);
1386 command |= PCI_COMMAND_MEMORY;
1387 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
1388 command);
1389 break;
1390 }
1391 }
1392
1393 high_size >>= 1;
1394 low_size >>= 1;
1395 } while (high_size || low_size);
1396
1397 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1398}
1399
1400/**
1401 * create_root_hv_pci_bus() - Expose a new root PCI bus
1402 * @hbus: Root PCI bus, as understood by this driver
1403 *
1404 * Return: 0 on success, -errno on failure
1405 */
1406static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
1407{
1408 /* Register the device */
1409 hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
1410 0, /* bus number is always zero */
1411 &hv_pcifront_ops,
1412 &hbus->sysdata,
1413 &hbus->resources_for_children);
1414 if (!hbus->pci_bus)
1415 return -ENODEV;
1416
1417 hbus->pci_bus->msi = &hbus->msi_chip;
1418 hbus->pci_bus->msi->dev = &hbus->hdev->device;
1419
414428c5 1420 pci_lock_rescan_remove();
4daace0d
JO
1421 pci_scan_child_bus(hbus->pci_bus);
1422 pci_bus_assign_resources(hbus->pci_bus);
1423 pci_bus_add_devices(hbus->pci_bus);
414428c5 1424 pci_unlock_rescan_remove();
4daace0d
JO
1425 hbus->state = hv_pcibus_installed;
1426 return 0;
1427}
1428
1429struct q_res_req_compl {
1430 struct completion host_event;
1431 struct hv_pci_dev *hpdev;
1432};
1433
1434/**
1435 * q_resource_requirements() - Query Resource Requirements
1436 * @context: The completion context.
1437 * @resp: The response that came from the host.
1438 * @resp_packet_size: The size in bytes of resp.
1439 *
1440 * This function is invoked on completion of a Query Resource
1441 * Requirements packet.
1442 */
1443static void q_resource_requirements(void *context, struct pci_response *resp,
1444 int resp_packet_size)
1445{
1446 struct q_res_req_compl *completion = context;
1447 struct pci_q_res_req_response *q_res_req =
1448 (struct pci_q_res_req_response *)resp;
1449 int i;
1450
1451 if (resp->status < 0) {
1452 dev_err(&completion->hpdev->hbus->hdev->device,
1453 "query resource requirements failed: %x\n",
1454 resp->status);
1455 } else {
1456 for (i = 0; i < 6; i++) {
1457 completion->hpdev->probed_bar[i] =
1458 q_res_req->probed_bar[i];
1459 }
1460 }
1461
1462 complete(&completion->host_event);
1463}
1464
1465static void get_pcichild(struct hv_pci_dev *hpdev,
1466 enum hv_pcidev_ref_reason reason)
1467{
24196f0c 1468 refcount_inc(&hpdev->refs);
4daace0d
JO
1469}
1470
1471static void put_pcichild(struct hv_pci_dev *hpdev,
1472 enum hv_pcidev_ref_reason reason)
1473{
24196f0c 1474 if (refcount_dec_and_test(&hpdev->refs))
4daace0d
JO
1475 kfree(hpdev);
1476}
1477
1478/**
1479 * new_pcichild_device() - Create a new child device
1480 * @hbus: The internal struct tracking this root PCI bus.
1481 * @desc: The information supplied so far from the host
1482 * about the device.
1483 *
1484 * This function creates the tracking structure for a new child
1485 * device and kicks off the process of figuring out what it is.
1486 *
1487 * Return: Pointer to the new tracking struct
1488 */
1489static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
1490 struct pci_function_description *desc)
1491{
1492 struct hv_pci_dev *hpdev;
1493 struct pci_child_message *res_req;
1494 struct q_res_req_compl comp_pkt;
8286e96d
DC
1495 struct {
1496 struct pci_packet init_packet;
1497 u8 buffer[sizeof(struct pci_child_message)];
4daace0d
JO
1498 } pkt;
1499 unsigned long flags;
1500 int ret;
1501
1502 hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC);
1503 if (!hpdev)
1504 return NULL;
1505
1506 hpdev->hbus = hbus;
1507
1508 memset(&pkt, 0, sizeof(pkt));
1509 init_completion(&comp_pkt.host_event);
1510 comp_pkt.hpdev = hpdev;
1511 pkt.init_packet.compl_ctxt = &comp_pkt;
1512 pkt.init_packet.completion_func = q_resource_requirements;
1513 res_req = (struct pci_child_message *)&pkt.init_packet.message;
0c6045d8 1514 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
4daace0d
JO
1515 res_req->wslot.slot = desc->win_slot.slot;
1516
1517 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
1518 sizeof(struct pci_child_message),
1519 (unsigned long)&pkt.init_packet,
1520 VM_PKT_DATA_INBAND,
1521 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1522 if (ret)
1523 goto error;
1524
1525 wait_for_completion(&comp_pkt.host_event);
1526
1527 hpdev->desc = *desc;
24196f0c 1528 refcount_set(&hpdev->refs, 1);
4daace0d
JO
1529 get_pcichild(hpdev, hv_pcidev_ref_childlist);
1530 spin_lock_irqsave(&hbus->device_list_lock, flags);
4a9b0933
HZ
1531
1532 /*
1533 * When a device is being added to the bus, we set the PCI domain
1534 * number to be the device serial number, which is non-zero and
1535 * unique on the same VM. The serial numbers start with 1, and
1536 * increase by 1 for each device. So device names including this
1537 * can have shorter names than based on the bus instance UUID.
1538 * Only the first device serial number is used for domain, so the
1539 * domain number will not change after the first device is added.
3cb6972e
HZ
1540 * The lower 16 bits of the serial number is used, otherwise some
1541 * drivers may not be able to handle it.
4a9b0933
HZ
1542 */
1543 if (list_empty(&hbus->children))
3cb6972e 1544 hbus->sysdata.domain = desc->ser & 0xFFFF;
4daace0d
JO
1545 list_add_tail(&hpdev->list_entry, &hbus->children);
1546 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1547 return hpdev;
1548
1549error:
1550 kfree(hpdev);
1551 return NULL;
1552}
1553
1554/**
1555 * get_pcichild_wslot() - Find device from slot
1556 * @hbus: Root PCI bus, as understood by this driver
1557 * @wslot: Location on the bus
1558 *
1559 * This function looks up a PCI device and returns the internal
1560 * representation of it. It acquires a reference on it, so that
1561 * the device won't be deleted while somebody is using it. The
1562 * caller is responsible for calling put_pcichild() to release
1563 * this reference.
1564 *
1565 * Return: Internal representation of a PCI device
1566 */
1567static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
1568 u32 wslot)
1569{
1570 unsigned long flags;
1571 struct hv_pci_dev *iter, *hpdev = NULL;
1572
1573 spin_lock_irqsave(&hbus->device_list_lock, flags);
1574 list_for_each_entry(iter, &hbus->children, list_entry) {
1575 if (iter->desc.win_slot.slot == wslot) {
1576 hpdev = iter;
1577 get_pcichild(hpdev, hv_pcidev_ref_by_slot);
1578 break;
1579 }
1580 }
1581 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1582
1583 return hpdev;
1584}
1585
1586/**
1587 * pci_devices_present_work() - Handle new list of child devices
1588 * @work: Work struct embedded in struct hv_dr_work
1589 *
1590 * "Bus Relations" is the Windows term for "children of this
1591 * bus." The terminology is preserved here for people trying to
1592 * debug the interaction between Hyper-V and Linux. This
1593 * function is called when the parent partition reports a list
1594 * of functions that should be observed under this PCI Express
1595 * port (bus).
1596 *
1597 * This function updates the list, and must tolerate being
1598 * called multiple times with the same information. The typical
1599 * number of child devices is one, with very atypical cases
1600 * involving three or four, so the algorithms used here can be
1601 * simple and inefficient.
1602 *
1603 * It must also treat the omission of a previously observed device as
1604 * notification that the device no longer exists.
1605 *
9053ead2
DC
1606 * Note that this function is serialized with hv_eject_device_work(),
1607 * because both are pushed to the ordered workqueue hbus->wq.
4daace0d
JO
1608 */
1609static void pci_devices_present_work(struct work_struct *work)
1610{
1611 u32 child_no;
1612 bool found;
1613 struct list_head *iter;
1614 struct pci_function_description *new_desc;
1615 struct hv_pci_dev *hpdev;
1616 struct hv_pcibus_device *hbus;
1617 struct list_head removed;
1618 struct hv_dr_work *dr_wrk;
1619 struct hv_dr_state *dr = NULL;
1620 unsigned long flags;
1621
1622 dr_wrk = container_of(work, struct hv_dr_work, wrk);
1623 hbus = dr_wrk->bus;
1624 kfree(dr_wrk);
1625
1626 INIT_LIST_HEAD(&removed);
1627
4daace0d
JO
1628 /* Pull this off the queue and process it if it was the last one. */
1629 spin_lock_irqsave(&hbus->device_list_lock, flags);
1630 while (!list_empty(&hbus->dr_list)) {
1631 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
1632 list_entry);
1633 list_del(&dr->list_entry);
1634
1635 /* Throw this away if the list still has stuff in it. */
1636 if (!list_empty(&hbus->dr_list)) {
1637 kfree(dr);
1638 continue;
1639 }
1640 }
1641 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1642
1643 if (!dr) {
4daace0d
JO
1644 put_hvpcibus(hbus);
1645 return;
1646 }
1647
1648 /* First, mark all existing children as reported missing. */
1649 spin_lock_irqsave(&hbus->device_list_lock, flags);
1650 list_for_each(iter, &hbus->children) {
1651 hpdev = container_of(iter, struct hv_pci_dev,
1652 list_entry);
1653 hpdev->reported_missing = true;
1654 }
1655 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1656
1657 /* Next, add back any reported devices. */
1658 for (child_no = 0; child_no < dr->device_count; child_no++) {
1659 found = false;
1660 new_desc = &dr->func[child_no];
1661
1662 spin_lock_irqsave(&hbus->device_list_lock, flags);
1663 list_for_each(iter, &hbus->children) {
1664 hpdev = container_of(iter, struct hv_pci_dev,
1665 list_entry);
1666 if ((hpdev->desc.win_slot.slot ==
1667 new_desc->win_slot.slot) &&
1668 (hpdev->desc.v_id == new_desc->v_id) &&
1669 (hpdev->desc.d_id == new_desc->d_id) &&
1670 (hpdev->desc.ser == new_desc->ser)) {
1671 hpdev->reported_missing = false;
1672 found = true;
1673 }
1674 }
1675 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1676
1677 if (!found) {
1678 hpdev = new_pcichild_device(hbus, new_desc);
1679 if (!hpdev)
1680 dev_err(&hbus->hdev->device,
1681 "couldn't record a child device.\n");
1682 }
1683 }
1684
1685 /* Move missing children to a list on the stack. */
1686 spin_lock_irqsave(&hbus->device_list_lock, flags);
1687 do {
1688 found = false;
1689 list_for_each(iter, &hbus->children) {
1690 hpdev = container_of(iter, struct hv_pci_dev,
1691 list_entry);
1692 if (hpdev->reported_missing) {
1693 found = true;
1694 put_pcichild(hpdev, hv_pcidev_ref_childlist);
4f1cb01a 1695 list_move_tail(&hpdev->list_entry, &removed);
4daace0d
JO
1696 break;
1697 }
1698 }
1699 } while (found);
1700 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1701
1702 /* Delete everything that should no longer exist. */
1703 while (!list_empty(&removed)) {
1704 hpdev = list_first_entry(&removed, struct hv_pci_dev,
1705 list_entry);
1706 list_del(&hpdev->list_entry);
1707 put_pcichild(hpdev, hv_pcidev_ref_initial);
1708 }
1709
691ac1dc 1710 switch (hbus->state) {
d3a78d8b
LL
1711 case hv_pcibus_installed:
1712 /*
691ac1dc
JL
1713 * Tell the core to rescan bus
1714 * because there may have been changes.
1715 */
4daace0d
JO
1716 pci_lock_rescan_remove();
1717 pci_scan_child_bus(hbus->pci_bus);
1718 pci_unlock_rescan_remove();
d3a78d8b
LL
1719 break;
1720
1721 case hv_pcibus_init:
1722 case hv_pcibus_probed:
4daace0d 1723 survey_child_resources(hbus);
d3a78d8b
LL
1724 break;
1725
1726 default:
1727 break;
4daace0d
JO
1728 }
1729
4daace0d
JO
1730 put_hvpcibus(hbus);
1731 kfree(dr);
1732}
1733
1734/**
1735 * hv_pci_devices_present() - Handles list of new children
1736 * @hbus: Root PCI bus, as understood by this driver
1737 * @relations: Packet from host listing children
1738 *
1739 * This function is invoked whenever a new list of devices for
1740 * this bus appears.
1741 */
1742static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
1743 struct pci_bus_relations *relations)
1744{
1745 struct hv_dr_state *dr;
1746 struct hv_dr_work *dr_wrk;
1747 unsigned long flags;
1748
1749 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
1750 if (!dr_wrk)
1751 return;
1752
1753 dr = kzalloc(offsetof(struct hv_dr_state, func) +
1754 (sizeof(struct pci_function_description) *
1755 (relations->device_count)), GFP_NOWAIT);
1756 if (!dr) {
1757 kfree(dr_wrk);
1758 return;
1759 }
1760
1761 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
1762 dr_wrk->bus = hbus;
1763 dr->device_count = relations->device_count;
1764 if (dr->device_count != 0) {
1765 memcpy(dr->func, relations->func,
1766 sizeof(struct pci_function_description) *
1767 dr->device_count);
1768 }
1769
1770 spin_lock_irqsave(&hbus->device_list_lock, flags);
1771 list_add_tail(&dr->list_entry, &hbus->dr_list);
1772 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1773
1774 get_hvpcibus(hbus);
9053ead2 1775 queue_work(hbus->wq, &dr_wrk->wrk);
4daace0d
JO
1776}
1777
1778/**
1779 * hv_eject_device_work() - Asynchronously handles ejection
1780 * @work: Work struct embedded in internal device struct
1781 *
1782 * This function handles ejecting a device. Windows will
1783 * attempt to gracefully eject a device, waiting 60 seconds to
1784 * hear back from the guest OS that this completed successfully.
1785 * If this timer expires, the device will be forcibly removed.
1786 */
1787static void hv_eject_device_work(struct work_struct *work)
1788{
1789 struct pci_eject_response *ejct_pkt;
1790 struct hv_pci_dev *hpdev;
1791 struct pci_dev *pdev;
1792 unsigned long flags;
1793 int wslot;
1794 struct {
1795 struct pci_packet pkt;
0c6045d8 1796 u8 buffer[sizeof(struct pci_eject_response)];
4daace0d
JO
1797 } ctxt;
1798
1799 hpdev = container_of(work, struct hv_pci_dev, wrk);
1800
1801 if (hpdev->state != hv_pcichild_ejecting) {
1802 put_pcichild(hpdev, hv_pcidev_ref_pnp);
1803 return;
1804 }
1805
1806 /*
1807 * Ejection can come before or after the PCI bus has been set up, so
1808 * attempt to find it and tear down the bus state, if it exists. This
1809 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
1810 * because hbus->pci_bus may not exist yet.
1811 */
1812 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
1813 pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0,
1814 wslot);
1815 if (pdev) {
414428c5 1816 pci_lock_rescan_remove();
4daace0d
JO
1817 pci_stop_and_remove_bus_device(pdev);
1818 pci_dev_put(pdev);
414428c5 1819 pci_unlock_rescan_remove();
4daace0d
JO
1820 }
1821
e74d2ebd
DC
1822 spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
1823 list_del(&hpdev->list_entry);
1824 spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
1825
4daace0d
JO
1826 memset(&ctxt, 0, sizeof(ctxt));
1827 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
0c6045d8 1828 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
4daace0d
JO
1829 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1830 vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
1831 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
1832 VM_PKT_DATA_INBAND, 0);
1833
4daace0d
JO
1834 put_pcichild(hpdev, hv_pcidev_ref_childlist);
1835 put_pcichild(hpdev, hv_pcidev_ref_pnp);
1836 put_hvpcibus(hpdev->hbus);
1837}
1838
1839/**
1840 * hv_pci_eject_device() - Handles device ejection
1841 * @hpdev: Internal device tracking struct
1842 *
1843 * This function is invoked when an ejection packet arrives. It
1844 * just schedules work so that we don't re-enter the packet
1845 * delivery code handling the ejection.
1846 */
1847static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
1848{
1849 hpdev->state = hv_pcichild_ejecting;
1850 get_pcichild(hpdev, hv_pcidev_ref_pnp);
1851 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
1852 get_hvpcibus(hpdev->hbus);
9053ead2 1853 queue_work(hpdev->hbus->wq, &hpdev->wrk);
4daace0d
JO
1854}
1855
1856/**
1857 * hv_pci_onchannelcallback() - Handles incoming packets
1858 * @context: Internal bus tracking struct
1859 *
1860 * This function is invoked whenever the host sends a packet to
1861 * this channel (which is private to this root PCI bus).
1862 */
1863static void hv_pci_onchannelcallback(void *context)
1864{
1865 const int packet_size = 0x100;
1866 int ret;
1867 struct hv_pcibus_device *hbus = context;
1868 u32 bytes_recvd;
1869 u64 req_id;
1870 struct vmpacket_descriptor *desc;
1871 unsigned char *buffer;
1872 int bufferlen = packet_size;
1873 struct pci_packet *comp_packet;
1874 struct pci_response *response;
1875 struct pci_incoming_message *new_message;
1876 struct pci_bus_relations *bus_rel;
1877 struct pci_dev_incoming *dev_message;
1878 struct hv_pci_dev *hpdev;
1879
1880 buffer = kmalloc(bufferlen, GFP_ATOMIC);
1881 if (!buffer)
1882 return;
1883
1884 while (1) {
1885 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
1886 bufferlen, &bytes_recvd, &req_id);
1887
1888 if (ret == -ENOBUFS) {
1889 kfree(buffer);
1890 /* Handle large packet */
1891 bufferlen = bytes_recvd;
1892 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1893 if (!buffer)
1894 return;
1895 continue;
1896 }
1897
837d741e
VK
1898 /* Zero length indicates there are no more packets. */
1899 if (ret || !bytes_recvd)
1900 break;
1901
4daace0d
JO
1902 /*
1903 * All incoming packets must be at least as large as a
1904 * response.
1905 */
60fcdac8 1906 if (bytes_recvd <= sizeof(struct pci_response))
837d741e 1907 continue;
4daace0d
JO
1908 desc = (struct vmpacket_descriptor *)buffer;
1909
1910 switch (desc->type) {
1911 case VM_PKT_COMP:
1912
1913 /*
1914 * The host is trusted, and thus it's safe to interpret
1915 * this transaction ID as a pointer.
1916 */
1917 comp_packet = (struct pci_packet *)req_id;
1918 response = (struct pci_response *)buffer;
1919 comp_packet->completion_func(comp_packet->compl_ctxt,
1920 response,
1921 bytes_recvd);
60fcdac8 1922 break;
4daace0d
JO
1923
1924 case VM_PKT_DATA_INBAND:
1925
1926 new_message = (struct pci_incoming_message *)buffer;
0c6045d8 1927 switch (new_message->message_type.type) {
4daace0d
JO
1928 case PCI_BUS_RELATIONS:
1929
1930 bus_rel = (struct pci_bus_relations *)buffer;
1931 if (bytes_recvd <
1932 offsetof(struct pci_bus_relations, func) +
1933 (sizeof(struct pci_function_description) *
1934 (bus_rel->device_count))) {
1935 dev_err(&hbus->hdev->device,
1936 "bus relations too small\n");
1937 break;
1938 }
1939
1940 hv_pci_devices_present(hbus, bus_rel);
1941 break;
1942
1943 case PCI_EJECT:
1944
1945 dev_message = (struct pci_dev_incoming *)buffer;
1946 hpdev = get_pcichild_wslot(hbus,
1947 dev_message->wslot.slot);
1948 if (hpdev) {
1949 hv_pci_eject_device(hpdev);
1950 put_pcichild(hpdev,
1951 hv_pcidev_ref_by_slot);
1952 }
1953 break;
1954
1955 default:
1956 dev_warn(&hbus->hdev->device,
1957 "Unimplemented protocol message %x\n",
0c6045d8 1958 new_message->message_type.type);
4daace0d
JO
1959 break;
1960 }
1961 break;
1962
1963 default:
1964 dev_err(&hbus->hdev->device,
1965 "unhandled packet type %d, tid %llx len %d\n",
1966 desc->type, req_id, bytes_recvd);
1967 break;
1968 }
4daace0d 1969 }
60fcdac8
VK
1970
1971 kfree(buffer);
4daace0d
JO
1972}
1973
1974/**
1975 * hv_pci_protocol_negotiation() - Set up protocol
1976 * @hdev: VMBus's tracking struct for this root PCI bus
1977 *
1978 * This driver is intended to support running on Windows 10
1979 * (server) and later versions. It will not run on earlier
1980 * versions, as they assume that many of the operations which
1981 * Linux needs accomplished with a spinlock held were done via
1982 * asynchronous messaging via VMBus. Windows 10 increases the
1983 * surface area of PCI emulation so that these actions can take
1984 * place by suspending a virtual processor for their duration.
1985 *
1986 * This function negotiates the channel protocol version,
1987 * failing if the host doesn't support the necessary protocol
1988 * level.
1989 */
1990static int hv_pci_protocol_negotiation(struct hv_device *hdev)
1991{
1992 struct pci_version_request *version_req;
1993 struct hv_pci_compl comp_pkt;
1994 struct pci_packet *pkt;
1995 int ret;
b1db7e7e 1996 int i;
4daace0d
JO
1997
1998 /*
1999 * Initiate the handshake with the host and negotiate
2000 * a version that the host can support. We start with the
2001 * highest version number and go down if the host cannot
2002 * support it.
2003 */
2004 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2005 if (!pkt)
2006 return -ENOMEM;
2007
2008 init_completion(&comp_pkt.host_event);
2009 pkt->completion_func = hv_pci_generic_compl;
2010 pkt->compl_ctxt = &comp_pkt;
2011 version_req = (struct pci_version_request *)&pkt->message;
0c6045d8 2012 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
4daace0d 2013
b1db7e7e
JL
2014 for (i = 0; i < ARRAY_SIZE(pci_protocol_versions); i++) {
2015 version_req->protocol_version = pci_protocol_versions[i];
2016 ret = vmbus_sendpacket(hdev->channel, version_req,
2017 sizeof(struct pci_version_request),
2018 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2019 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2020 if (ret) {
2021 dev_err(&hdev->device,
2022 "PCI Pass-through VSP failed sending version reqquest: %#x",
2023 ret);
2024 goto exit;
2025 }
4daace0d 2026
b1db7e7e
JL
2027 wait_for_completion(&comp_pkt.host_event);
2028
2029 if (comp_pkt.completion_status >= 0) {
2030 pci_protocol_version = pci_protocol_versions[i];
2031 dev_info(&hdev->device,
2032 "PCI VMBus probing: Using version %#x\n",
2033 pci_protocol_version);
2034 goto exit;
2035 }
2036
2037 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2038 dev_err(&hdev->device,
2039 "PCI Pass-through VSP failed version request: %#x",
2040 comp_pkt.completion_status);
2041 ret = -EPROTO;
2042 goto exit;
2043 }
4daace0d 2044
b1db7e7e 2045 reinit_completion(&comp_pkt.host_event);
4daace0d
JO
2046 }
2047
b1db7e7e
JL
2048 dev_err(&hdev->device,
2049 "PCI pass-through VSP failed to find supported version");
2050 ret = -EPROTO;
4daace0d
JO
2051
2052exit:
2053 kfree(pkt);
2054 return ret;
2055}
2056
2057/**
2058 * hv_pci_free_bridge_windows() - Release memory regions for the
2059 * bus
2060 * @hbus: Root PCI bus, as understood by this driver
2061 */
2062static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2063{
2064 /*
2065 * Set the resources back to the way they looked when they
2066 * were allocated by setting IORESOURCE_BUSY again.
2067 */
2068
2069 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2070 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
696ca5e8
JO
2071 vmbus_free_mmio(hbus->low_mmio_res->start,
2072 resource_size(hbus->low_mmio_res));
4daace0d
JO
2073 }
2074
2075 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2076 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
696ca5e8
JO
2077 vmbus_free_mmio(hbus->high_mmio_res->start,
2078 resource_size(hbus->high_mmio_res));
4daace0d
JO
2079 }
2080}
2081
2082/**
2083 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2084 * for the bus
2085 * @hbus: Root PCI bus, as understood by this driver
2086 *
2087 * This function calls vmbus_allocate_mmio(), which is itself a
2088 * bit of a compromise. Ideally, we might change the pnp layer
2089 * in the kernel such that it comprehends either PCI devices
2090 * which are "grandchildren of ACPI," with some intermediate bus
2091 * node (in this case, VMBus) or change it such that it
2092 * understands VMBus. The pnp layer, however, has been declared
2093 * deprecated, and not subject to change.
2094 *
2095 * The workaround, implemented here, is to ask VMBus to allocate
2096 * MMIO space for this bus. VMBus itself knows which ranges are
2097 * appropriate by looking at its own ACPI objects. Then, after
2098 * these ranges are claimed, they're modified to look like they
2099 * would have looked if the ACPI and pnp code had allocated
2100 * bridge windows. These descriptors have to exist in this form
2101 * in order to satisfy the code which will get invoked when the
2102 * endpoint PCI function driver calls request_mem_region() or
2103 * request_mem_region_exclusive().
2104 *
2105 * Return: 0 on success, -errno on failure
2106 */
2107static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2108{
2109 resource_size_t align;
2110 int ret;
2111
2112 if (hbus->low_mmio_space) {
2113 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2114 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2115 (u64)(u32)0xffffffff,
2116 hbus->low_mmio_space,
2117 align, false);
2118 if (ret) {
2119 dev_err(&hbus->hdev->device,
2120 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2121 hbus->low_mmio_space);
2122 return ret;
2123 }
2124
2125 /* Modify this resource to become a bridge window. */
2126 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2127 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
2128 pci_add_resource(&hbus->resources_for_children,
2129 hbus->low_mmio_res);
2130 }
2131
2132 if (hbus->high_mmio_space) {
2133 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2134 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2135 0x100000000, -1,
2136 hbus->high_mmio_space, align,
2137 false);
2138 if (ret) {
2139 dev_err(&hbus->hdev->device,
2140 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2141 hbus->high_mmio_space);
2142 goto release_low_mmio;
2143 }
2144
2145 /* Modify this resource to become a bridge window. */
2146 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
2147 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
2148 pci_add_resource(&hbus->resources_for_children,
2149 hbus->high_mmio_res);
2150 }
2151
2152 return 0;
2153
2154release_low_mmio:
2155 if (hbus->low_mmio_res) {
696ca5e8
JO
2156 vmbus_free_mmio(hbus->low_mmio_res->start,
2157 resource_size(hbus->low_mmio_res));
4daace0d
JO
2158 }
2159
2160 return ret;
2161}
2162
2163/**
2164 * hv_allocate_config_window() - Find MMIO space for PCI Config
2165 * @hbus: Root PCI bus, as understood by this driver
2166 *
2167 * This function claims memory-mapped I/O space for accessing
2168 * configuration space for the functions on this bus.
2169 *
2170 * Return: 0 on success, -errno on failure
2171 */
2172static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
2173{
2174 int ret;
2175
2176 /*
2177 * Set up a region of MMIO space to use for accessing configuration
2178 * space.
2179 */
2180 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
2181 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
2182 if (ret)
2183 return ret;
2184
2185 /*
2186 * vmbus_allocate_mmio() gets used for allocating both device endpoint
2187 * resource claims (those which cannot be overlapped) and the ranges
2188 * which are valid for the children of this bus, which are intended
2189 * to be overlapped by those children. Set the flag on this claim
2190 * meaning that this region can't be overlapped.
2191 */
2192
2193 hbus->mem_config->flags |= IORESOURCE_BUSY;
2194
2195 return 0;
2196}
2197
2198static void hv_free_config_window(struct hv_pcibus_device *hbus)
2199{
696ca5e8 2200 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
4daace0d
JO
2201}
2202
2203/**
2204 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
2205 * @hdev: VMBus's tracking struct for this root PCI bus
2206 *
2207 * Return: 0 on success, -errno on failure
2208 */
2209static int hv_pci_enter_d0(struct hv_device *hdev)
2210{
2211 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2212 struct pci_bus_d0_entry *d0_entry;
2213 struct hv_pci_compl comp_pkt;
2214 struct pci_packet *pkt;
2215 int ret;
2216
2217 /*
2218 * Tell the host that the bus is ready to use, and moved into the
2219 * powered-on state. This includes telling the host which region
2220 * of memory-mapped I/O space has been chosen for configuration space
2221 * access.
2222 */
2223 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
2224 if (!pkt)
2225 return -ENOMEM;
2226
2227 init_completion(&comp_pkt.host_event);
2228 pkt->completion_func = hv_pci_generic_compl;
2229 pkt->compl_ctxt = &comp_pkt;
2230 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
0c6045d8 2231 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
4daace0d
JO
2232 d0_entry->mmio_base = hbus->mem_config->start;
2233
2234 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
2235 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2236 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2237 if (ret)
2238 goto exit;
2239
2240 wait_for_completion(&comp_pkt.host_event);
2241
2242 if (comp_pkt.completion_status < 0) {
2243 dev_err(&hdev->device,
2244 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2245 comp_pkt.completion_status);
2246 ret = -EPROTO;
2247 goto exit;
2248 }
2249
2250 ret = 0;
2251
2252exit:
2253 kfree(pkt);
2254 return ret;
2255}
2256
2257/**
2258 * hv_pci_query_relations() - Ask host to send list of child
2259 * devices
2260 * @hdev: VMBus's tracking struct for this root PCI bus
2261 *
2262 * Return: 0 on success, -errno on failure
2263 */
2264static int hv_pci_query_relations(struct hv_device *hdev)
2265{
2266 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2267 struct pci_message message;
2268 struct completion comp;
2269 int ret;
2270
2271 /* Ask the host to send along the list of child devices */
2272 init_completion(&comp);
2273 if (cmpxchg(&hbus->survey_event, NULL, &comp))
2274 return -ENOTEMPTY;
2275
2276 memset(&message, 0, sizeof(message));
0c6045d8 2277 message.type = PCI_QUERY_BUS_RELATIONS;
4daace0d
JO
2278
2279 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
2280 0, VM_PKT_DATA_INBAND, 0);
2281 if (ret)
2282 return ret;
2283
2284 wait_for_completion(&comp);
2285 return 0;
2286}
2287
2288/**
2289 * hv_send_resources_allocated() - Report local resource choices
2290 * @hdev: VMBus's tracking struct for this root PCI bus
2291 *
2292 * The host OS is expecting to be sent a request as a message
2293 * which contains all the resources that the device will use.
2294 * The response contains those same resources, "translated"
2295 * which is to say, the values which should be used by the
2296 * hardware, when it delivers an interrupt. (MMIO resources are
2297 * used in local terms.) This is nice for Windows, and lines up
2298 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2299 * is deeply expecting to scan an emulated PCI configuration
2300 * space. So this message is sent here only to drive the state
2301 * machine on the host forward.
2302 *
2303 * Return: 0 on success, -errno on failure
2304 */
2305static int hv_send_resources_allocated(struct hv_device *hdev)
2306{
2307 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2308 struct pci_resources_assigned *res_assigned;
7dcf90e9 2309 struct pci_resources_assigned2 *res_assigned2;
4daace0d
JO
2310 struct hv_pci_compl comp_pkt;
2311 struct hv_pci_dev *hpdev;
2312 struct pci_packet *pkt;
7dcf90e9 2313 size_t size_res;
4daace0d
JO
2314 u32 wslot;
2315 int ret;
2316
7dcf90e9
JL
2317 size_res = (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2)
2318 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
2319
2320 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
4daace0d
JO
2321 if (!pkt)
2322 return -ENOMEM;
2323
2324 ret = 0;
2325
2326 for (wslot = 0; wslot < 256; wslot++) {
2327 hpdev = get_pcichild_wslot(hbus, wslot);
2328 if (!hpdev)
2329 continue;
2330
7dcf90e9 2331 memset(pkt, 0, sizeof(*pkt) + size_res);
4daace0d
JO
2332 init_completion(&comp_pkt.host_event);
2333 pkt->completion_func = hv_pci_generic_compl;
2334 pkt->compl_ctxt = &comp_pkt;
4daace0d 2335
7dcf90e9
JL
2336 if (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2) {
2337 res_assigned =
2338 (struct pci_resources_assigned *)&pkt->message;
2339 res_assigned->message_type.type =
2340 PCI_RESOURCES_ASSIGNED;
2341 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
2342 } else {
2343 res_assigned2 =
2344 (struct pci_resources_assigned2 *)&pkt->message;
2345 res_assigned2->message_type.type =
2346 PCI_RESOURCES_ASSIGNED2;
2347 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
2348 }
4daace0d
JO
2349 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
2350
7dcf90e9
JL
2351 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
2352 size_res, (unsigned long)pkt,
2353 VM_PKT_DATA_INBAND,
2354 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
4daace0d
JO
2355 if (ret)
2356 break;
2357
2358 wait_for_completion(&comp_pkt.host_event);
2359
2360 if (comp_pkt.completion_status < 0) {
2361 ret = -EPROTO;
2362 dev_err(&hdev->device,
2363 "resource allocated returned 0x%x",
2364 comp_pkt.completion_status);
2365 break;
2366 }
2367 }
2368
2369 kfree(pkt);
2370 return ret;
2371}
2372
2373/**
2374 * hv_send_resources_released() - Report local resources
2375 * released
2376 * @hdev: VMBus's tracking struct for this root PCI bus
2377 *
2378 * Return: 0 on success, -errno on failure
2379 */
2380static int hv_send_resources_released(struct hv_device *hdev)
2381{
2382 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2383 struct pci_child_message pkt;
2384 struct hv_pci_dev *hpdev;
2385 u32 wslot;
2386 int ret;
2387
2388 for (wslot = 0; wslot < 256; wslot++) {
2389 hpdev = get_pcichild_wslot(hbus, wslot);
2390 if (!hpdev)
2391 continue;
2392
2393 memset(&pkt, 0, sizeof(pkt));
0c6045d8 2394 pkt.message_type.type = PCI_RESOURCES_RELEASED;
4daace0d
JO
2395 pkt.wslot.slot = hpdev->desc.win_slot.slot;
2396
2397 put_pcichild(hpdev, hv_pcidev_ref_by_slot);
2398
2399 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
2400 VM_PKT_DATA_INBAND, 0);
2401 if (ret)
2402 return ret;
2403 }
2404
2405 return 0;
2406}
2407
2408static void get_hvpcibus(struct hv_pcibus_device *hbus)
2409{
2410 atomic_inc(&hbus->remove_lock);
2411}
2412
2413static void put_hvpcibus(struct hv_pcibus_device *hbus)
2414{
2415 if (atomic_dec_and_test(&hbus->remove_lock))
2416 complete(&hbus->remove_event);
2417}
2418
2419/**
2420 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
2421 * @hdev: VMBus's tracking struct for this root PCI bus
2422 * @dev_id: Identifies the device itself
2423 *
2424 * Return: 0 on success, -errno on failure
2425 */
2426static int hv_pci_probe(struct hv_device *hdev,
2427 const struct hv_vmbus_device_id *dev_id)
2428{
2429 struct hv_pcibus_device *hbus;
2430 int ret;
2431
be66b673
JL
2432 /*
2433 * hv_pcibus_device contains the hypercall arguments for retargeting in
2434 * hv_irq_unmask(). Those must not cross a page boundary.
2435 */
2436 BUILD_BUG_ON(sizeof(*hbus) > PAGE_SIZE);
2437
2438 hbus = (struct hv_pcibus_device *)get_zeroed_page(GFP_KERNEL);
4daace0d
JO
2439 if (!hbus)
2440 return -ENOMEM;
d3a78d8b 2441 hbus->state = hv_pcibus_init;
4daace0d
JO
2442
2443 /*
2444 * The PCI bus "domain" is what is called "segment" in ACPI and
2445 * other specs. Pull it from the instance ID, to get something
2446 * unique. Bytes 8 and 9 are what is used in Windows guests, so
2447 * do the same thing for consistency. Note that, since this code
2448 * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
2449 * that (1) the only domain in use for something that looks like
2450 * a physical PCI bus (which is actually emulated by the
2451 * hypervisor) is domain 0 and (2) there will be no overlap
2452 * between domains derived from these instance IDs in the same
2453 * VM.
2454 */
2455 hbus->sysdata.domain = hdev->dev_instance.b[9] |
2456 hdev->dev_instance.b[8] << 8;
2457
2458 hbus->hdev = hdev;
2459 atomic_inc(&hbus->remove_lock);
2460 INIT_LIST_HEAD(&hbus->children);
2461 INIT_LIST_HEAD(&hbus->dr_list);
2462 INIT_LIST_HEAD(&hbus->resources_for_children);
2463 spin_lock_init(&hbus->config_lock);
2464 spin_lock_init(&hbus->device_list_lock);
0de8ce3e 2465 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
4daace0d 2466 init_completion(&hbus->remove_event);
9053ead2
DC
2467 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
2468 hbus->sysdata.domain);
2469 if (!hbus->wq) {
2470 ret = -ENOMEM;
2471 goto free_bus;
2472 }
4daace0d
JO
2473
2474 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
2475 hv_pci_onchannelcallback, hbus);
2476 if (ret)
9053ead2 2477 goto destroy_wq;
4daace0d
JO
2478
2479 hv_set_drvdata(hdev, hbus);
2480
2481 ret = hv_pci_protocol_negotiation(hdev);
2482 if (ret)
2483 goto close;
2484
2485 ret = hv_allocate_config_window(hbus);
2486 if (ret)
2487 goto close;
2488
2489 hbus->cfg_addr = ioremap(hbus->mem_config->start,
2490 PCI_CONFIG_MMIO_LENGTH);
2491 if (!hbus->cfg_addr) {
2492 dev_err(&hdev->device,
2493 "Unable to map a virtual address for config space\n");
2494 ret = -ENOMEM;
2495 goto free_config;
2496 }
2497
2498 hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
2499 if (!hbus->sysdata.fwnode) {
2500 ret = -ENOMEM;
2501 goto unmap;
2502 }
2503
2504 ret = hv_pcie_init_irq_domain(hbus);
2505 if (ret)
2506 goto free_fwnode;
2507
2508 ret = hv_pci_query_relations(hdev);
2509 if (ret)
2510 goto free_irq_domain;
2511
2512 ret = hv_pci_enter_d0(hdev);
2513 if (ret)
2514 goto free_irq_domain;
2515
2516 ret = hv_pci_allocate_bridge_windows(hbus);
2517 if (ret)
2518 goto free_irq_domain;
2519
2520 ret = hv_send_resources_allocated(hdev);
2521 if (ret)
2522 goto free_windows;
2523
2524 prepopulate_bars(hbus);
2525
2526 hbus->state = hv_pcibus_probed;
2527
2528 ret = create_root_hv_pci_bus(hbus);
2529 if (ret)
2530 goto free_windows;
2531
2532 return 0;
2533
2534free_windows:
2535 hv_pci_free_bridge_windows(hbus);
2536free_irq_domain:
2537 irq_domain_remove(hbus->irq_domain);
2538free_fwnode:
2539 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2540unmap:
2541 iounmap(hbus->cfg_addr);
2542free_config:
2543 hv_free_config_window(hbus);
2544close:
2545 vmbus_close(hdev->channel);
9053ead2
DC
2546destroy_wq:
2547 destroy_workqueue(hbus->wq);
4daace0d 2548free_bus:
be66b673 2549 free_page((unsigned long)hbus);
4daace0d
JO
2550 return ret;
2551}
2552
17978524 2553static void hv_pci_bus_exit(struct hv_device *hdev)
4daace0d 2554{
17978524
DC
2555 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2556 struct {
4daace0d 2557 struct pci_packet teardown_packet;
17978524 2558 u8 buffer[sizeof(struct pci_message)];
4daace0d
JO
2559 } pkt;
2560 struct pci_bus_relations relations;
2561 struct hv_pci_compl comp_pkt;
17978524 2562 int ret;
4daace0d 2563
17978524
DC
2564 /*
2565 * After the host sends the RESCIND_CHANNEL message, it doesn't
2566 * access the per-channel ringbuffer any longer.
2567 */
2568 if (hdev->channel->rescind)
2569 return;
2570
2571 /* Delete any children which might still exist. */
2572 memset(&relations, 0, sizeof(relations));
2573 hv_pci_devices_present(hbus, &relations);
2574
2575 ret = hv_send_resources_released(hdev);
2576 if (ret)
2577 dev_err(&hdev->device,
2578 "Couldn't send resources released packet(s)\n");
4daace0d 2579
4daace0d
JO
2580 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
2581 init_completion(&comp_pkt.host_event);
2582 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
2583 pkt.teardown_packet.compl_ctxt = &comp_pkt;
0c6045d8 2584 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
4daace0d
JO
2585
2586 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
2587 sizeof(struct pci_message),
2588 (unsigned long)&pkt.teardown_packet,
2589 VM_PKT_DATA_INBAND,
2590 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2591 if (!ret)
2592 wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
17978524
DC
2593}
2594
2595/**
2596 * hv_pci_remove() - Remove routine for this VMBus channel
2597 * @hdev: VMBus's tracking struct for this root PCI bus
2598 *
2599 * Return: 0 on success, -errno on failure
2600 */
2601static int hv_pci_remove(struct hv_device *hdev)
2602{
2603 struct hv_pcibus_device *hbus;
4daace0d 2604
17978524 2605 hbus = hv_get_drvdata(hdev);
4daace0d
JO
2606 if (hbus->state == hv_pcibus_installed) {
2607 /* Remove the bus from PCI's point of view. */
2608 pci_lock_rescan_remove();
2609 pci_stop_root_bus(hbus->pci_bus);
2610 pci_remove_root_bus(hbus->pci_bus);
2611 pci_unlock_rescan_remove();
d3a78d8b 2612 hbus->state = hv_pcibus_removed;
4daace0d
JO
2613 }
2614
17978524 2615 hv_pci_bus_exit(hdev);
deb22e5c 2616
4daace0d
JO
2617 vmbus_close(hdev->channel);
2618
4daace0d
JO
2619 iounmap(hbus->cfg_addr);
2620 hv_free_config_window(hbus);
2621 pci_free_resource_list(&hbus->resources_for_children);
2622 hv_pci_free_bridge_windows(hbus);
2623 irq_domain_remove(hbus->irq_domain);
2624 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2625 put_hvpcibus(hbus);
2626 wait_for_completion(&hbus->remove_event);
9053ead2 2627 destroy_workqueue(hbus->wq);
be66b673 2628 free_page((unsigned long)hbus);
4daace0d
JO
2629 return 0;
2630}
2631
2632static const struct hv_vmbus_device_id hv_pci_id_table[] = {
2633 /* PCI Pass-through Class ID */
2634 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
2635 { HV_PCIE_GUID, },
2636 { },
2637};
2638
2639MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
2640
2641static struct hv_driver hv_pci_drv = {
2642 .name = "hv_pci",
2643 .id_table = hv_pci_id_table,
2644 .probe = hv_pci_probe,
2645 .remove = hv_pci_remove,
2646};
2647
2648static void __exit exit_hv_pci_drv(void)
2649{
2650 vmbus_driver_unregister(&hv_pci_drv);
2651}
2652
2653static int __init init_hv_pci_drv(void)
2654{
2655 return vmbus_driver_register(&hv_pci_drv);
2656}
2657
2658module_init(init_hv_pci_drv);
2659module_exit(exit_hv_pci_drv);
2660
2661MODULE_DESCRIPTION("Hyper-V PCI");
2662MODULE_LICENSE("GPL v2");