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