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