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