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