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