blob: fadc305533d93447692b409f63e6c099df532a87 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Jake Oshins4daace02016-02-16 21:56:23 +00002/*
3 * Copyright (c) Microsoft Corporation.
4 *
5 * Author:
6 * Jake Oshins <jakeo@microsoft.com>
7 *
8 * This driver acts as a paravirtual front-end for PCI Express root buses.
9 * When a PCI Express function (either an entire device or an SR-IOV
10 * Virtual Function) is being passed through to the VM, this driver exposes
11 * a new bus to the guest VM. This is modeled as a root PCI bus because
12 * no bridges are being exposed to the VM. In fact, with a "Generation 2"
13 * VM within Hyper-V, there may seem to be no PCI bus at all in the VM
14 * until a device as been exposed using this driver.
15 *
16 * Each root PCI bus has its own PCI domain, which is called "Segment" in
17 * the PCI Firmware Specifications. Thus while each device passed through
18 * to the VM using this front-end will appear at "device 0", the domain will
19 * be unique. Typically, each bus will have one PCI function on it, though
20 * this driver does support more than one.
21 *
22 * In order to map the interrupts from the device through to the guest VM,
23 * this driver also implements an IRQ Domain, which handles interrupts (either
24 * MSI or MSI-X) associated with the functions on the bus. As interrupts are
25 * set up, torn down, or reaffined, this driver communicates with the
26 * underlying hypervisor to adjust the mappings in the I/O MMU so that each
27 * interrupt will be delivered to the correct virtual processor at the right
28 * vector. This driver does not support level-triggered (line-based)
29 * interrupts, and will report that the Interrupt Line register in the
30 * function's configuration space is zero.
31 *
32 * The rest of this driver mostly maps PCI concepts onto underlying Hyper-V
33 * facilities. For instance, the configuration space of a function exposed
34 * by Hyper-V is mapped into a single page of memory space, and the
35 * read and write handlers for config space must be aware of this mechanism.
36 * Similarly, device setup and teardown involves messages sent to and from
37 * the PCI back-end driver in Hyper-V.
Jake Oshins4daace02016-02-16 21:56:23 +000038 */
39
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/pci.h>
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -070043#include <linux/delay.h>
Jake Oshins4daace02016-02-16 21:56:23 +000044#include <linux/semaphore.h>
45#include <linux/irqdomain.h>
46#include <asm/irqdomain.h>
47#include <asm/apic.h>
Nicolai Stange447ae312018-07-29 12:15:33 +020048#include <linux/irq.h>
Jake Oshins4daace02016-02-16 21:56:23 +000049#include <linux/msi.h>
50#include <linux/hyperv.h>
Elena Reshetova24196f02017-04-18 09:02:48 -050051#include <linux/refcount.h>
Jake Oshins4daace02016-02-16 21:56:23 +000052#include <asm/mshyperv.h>
53
54/*
55 * Protocol versions. The low word is the minor version, the high word the
56 * major version.
57 */
58
Jork Loeserb1db7e72017-05-24 13:41:27 -070059#define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
Jake Oshins4daace02016-02-16 21:56:23 +000060#define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
61#define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
62
Jork Loeserb1db7e72017-05-24 13:41:27 -070063enum pci_protocol_version_t {
64 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1), /* Win10 */
Jork Loeser7dcf90e2017-05-24 13:41:28 -070065 PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2), /* RS1 */
Jake Oshins4daace02016-02-16 21:56:23 +000066};
67
K. Y. Srinivasan433fcf62017-03-24 11:07:21 -070068#define CPU_AFFINITY_ALL -1ULL
Jork Loeserb1db7e72017-05-24 13:41:27 -070069
70/*
71 * Supported protocol versions in the order of probing - highest go
72 * first.
73 */
74static enum pci_protocol_version_t pci_protocol_versions[] = {
Jork Loeser7dcf90e2017-05-24 13:41:28 -070075 PCI_PROTOCOL_VERSION_1_2,
Jork Loeserb1db7e72017-05-24 13:41:27 -070076 PCI_PROTOCOL_VERSION_1_1,
77};
78
79/*
80 * Protocol version negotiated by hv_pci_protocol_negotiation().
81 */
82static enum pci_protocol_version_t pci_protocol_version;
83
Jake Oshins4daace02016-02-16 21:56:23 +000084#define PCI_CONFIG_MMIO_LENGTH 0x2000
85#define CFG_PAGE_OFFSET 0x1000
86#define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
87
88#define MAX_SUPPORTED_MSI_MESSAGES 0x400
89
Jork Loeserb1db7e72017-05-24 13:41:27 -070090#define STATUS_REVISION_MISMATCH 0xC0000059
91
Jake Oshins4daace02016-02-16 21:56:23 +000092/*
93 * Message Types
94 */
95
96enum pci_message_type {
97 /*
98 * Version 1.1
99 */
100 PCI_MESSAGE_BASE = 0x42490000,
101 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
102 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
103 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
104 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
105 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
106 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
107 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
108 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
109 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
110 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
111 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
112 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
113 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
114 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
115 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
116 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
117 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
118 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
119 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
120 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700121 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
122 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
123 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
Jake Oshins4daace02016-02-16 21:56:23 +0000124 PCI_MESSAGE_MAXIMUM
125};
126
127/*
128 * Structures defining the virtual PCI Express protocol.
129 */
130
131union pci_version {
132 struct {
133 u16 minor_version;
134 u16 major_version;
135 } parts;
136 u32 version;
137} __packed;
138
139/*
140 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
141 * which is all this driver does. This representation is the one used in
142 * Windows, which is what is expected when sending this back and forth with
143 * the Hyper-V parent partition.
144 */
145union win_slot_encoding {
146 struct {
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600147 u32 dev:5;
148 u32 func:3;
Jake Oshins4daace02016-02-16 21:56:23 +0000149 u32 reserved:24;
150 } bits;
151 u32 slot;
152} __packed;
153
154/*
155 * Pretty much as defined in the PCI Specifications.
156 */
157struct pci_function_description {
158 u16 v_id; /* vendor ID */
159 u16 d_id; /* device ID */
160 u8 rev;
161 u8 prog_intf;
162 u8 subclass;
163 u8 base_class;
164 u32 subsystem_id;
165 union win_slot_encoding win_slot;
166 u32 ser; /* serial number */
167} __packed;
168
169/**
170 * struct hv_msi_desc
171 * @vector: IDT entry
172 * @delivery_mode: As defined in Intel's Programmer's
173 * Reference Manual, Volume 3, Chapter 8.
174 * @vector_count: Number of contiguous entries in the
175 * Interrupt Descriptor Table that are
176 * occupied by this Message-Signaled
177 * Interrupt. For "MSI", as first defined
178 * in PCI 2.2, this can be between 1 and
179 * 32. For "MSI-X," as first defined in PCI
180 * 3.0, this must be 1, as each MSI-X table
181 * entry would have its own descriptor.
182 * @reserved: Empty space
183 * @cpu_mask: All the target virtual processors.
184 */
185struct hv_msi_desc {
186 u8 vector;
187 u8 delivery_mode;
188 u16 vector_count;
189 u32 reserved;
190 u64 cpu_mask;
191} __packed;
192
193/**
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700194 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
195 * @vector: IDT entry
196 * @delivery_mode: As defined in Intel's Programmer's
197 * Reference Manual, Volume 3, Chapter 8.
198 * @vector_count: Number of contiguous entries in the
199 * Interrupt Descriptor Table that are
200 * occupied by this Message-Signaled
201 * Interrupt. For "MSI", as first defined
202 * in PCI 2.2, this can be between 1 and
203 * 32. For "MSI-X," as first defined in PCI
204 * 3.0, this must be 1, as each MSI-X table
205 * entry would have its own descriptor.
206 * @processor_count: number of bits enabled in array.
207 * @processor_array: All the target virtual processors.
208 */
209struct hv_msi_desc2 {
210 u8 vector;
211 u8 delivery_mode;
212 u16 vector_count;
213 u16 processor_count;
214 u16 processor_array[32];
215} __packed;
216
217/**
Jake Oshins4daace02016-02-16 21:56:23 +0000218 * struct tran_int_desc
219 * @reserved: unused, padding
220 * @vector_count: same as in hv_msi_desc
221 * @data: This is the "data payload" value that is
222 * written by the device when it generates
223 * a message-signaled interrupt, either MSI
224 * or MSI-X.
225 * @address: This is the address to which the data
226 * payload is written on interrupt
227 * generation.
228 */
229struct tran_int_desc {
230 u16 reserved;
231 u16 vector_count;
232 u32 data;
233 u64 address;
234} __packed;
235
236/*
237 * A generic message format for virtual PCI.
238 * Specific message formats are defined later in the file.
239 */
240
241struct pci_message {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000242 u32 type;
Jake Oshins4daace02016-02-16 21:56:23 +0000243} __packed;
244
245struct pci_child_message {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000246 struct pci_message message_type;
Jake Oshins4daace02016-02-16 21:56:23 +0000247 union win_slot_encoding wslot;
248} __packed;
249
250struct pci_incoming_message {
251 struct vmpacket_descriptor hdr;
252 struct pci_message message_type;
253} __packed;
254
255struct pci_response {
256 struct vmpacket_descriptor hdr;
257 s32 status; /* negative values are failures */
258} __packed;
259
260struct pci_packet {
261 void (*completion_func)(void *context, struct pci_response *resp,
262 int resp_packet_size);
263 void *compl_ctxt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000264
265 struct pci_message message[0];
Jake Oshins4daace02016-02-16 21:56:23 +0000266};
267
268/*
269 * Specific message types supporting the PCI protocol.
270 */
271
272/*
273 * Version negotiation message. Sent from the guest to the host.
274 * The guest is free to try different versions until the host
275 * accepts the version.
276 *
277 * pci_version: The protocol version requested.
278 * is_last_attempt: If TRUE, this is the last version guest will request.
279 * reservedz: Reserved field, set to zero.
280 */
281
282struct pci_version_request {
283 struct pci_message message_type;
Jork Loeser691ac1d2017-05-24 13:41:24 -0700284 u32 protocol_version;
Jake Oshins4daace02016-02-16 21:56:23 +0000285} __packed;
286
287/*
288 * Bus D0 Entry. This is sent from the guest to the host when the virtual
289 * bus (PCI Express port) is ready for action.
290 */
291
292struct pci_bus_d0_entry {
293 struct pci_message message_type;
294 u32 reserved;
295 u64 mmio_base;
296} __packed;
297
298struct pci_bus_relations {
299 struct pci_incoming_message incoming;
300 u32 device_count;
Dexuan Cui7d0f8ee2016-08-23 04:46:39 +0000301 struct pci_function_description func[0];
Jake Oshins4daace02016-02-16 21:56:23 +0000302} __packed;
303
304struct pci_q_res_req_response {
305 struct vmpacket_descriptor hdr;
306 s32 status; /* negative values are failures */
307 u32 probed_bar[6];
308} __packed;
309
310struct pci_set_power {
311 struct pci_message message_type;
312 union win_slot_encoding wslot;
313 u32 power_state; /* In Windows terms */
314 u32 reserved;
315} __packed;
316
317struct pci_set_power_response {
318 struct vmpacket_descriptor hdr;
319 s32 status; /* negative values are failures */
320 union win_slot_encoding wslot;
321 u32 resultant_state; /* In Windows terms */
322 u32 reserved;
323} __packed;
324
325struct pci_resources_assigned {
326 struct pci_message message_type;
327 union win_slot_encoding wslot;
328 u8 memory_range[0x14][6]; /* not used here */
329 u32 msi_descriptors;
330 u32 reserved[4];
331} __packed;
332
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700333struct pci_resources_assigned2 {
334 struct pci_message message_type;
335 union win_slot_encoding wslot;
336 u8 memory_range[0x14][6]; /* not used here */
337 u32 msi_descriptor_count;
338 u8 reserved[70];
339} __packed;
340
Jake Oshins4daace02016-02-16 21:56:23 +0000341struct pci_create_interrupt {
342 struct pci_message message_type;
343 union win_slot_encoding wslot;
344 struct hv_msi_desc int_desc;
345} __packed;
346
347struct pci_create_int_response {
348 struct pci_response response;
349 u32 reserved;
350 struct tran_int_desc int_desc;
351} __packed;
352
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700353struct pci_create_interrupt2 {
354 struct pci_message message_type;
355 union win_slot_encoding wslot;
356 struct hv_msi_desc2 int_desc;
357} __packed;
358
Jake Oshins4daace02016-02-16 21:56:23 +0000359struct pci_delete_interrupt {
360 struct pci_message message_type;
361 union win_slot_encoding wslot;
362 struct tran_int_desc int_desc;
363} __packed;
364
365struct pci_dev_incoming {
366 struct pci_incoming_message incoming;
367 union win_slot_encoding wslot;
368} __packed;
369
370struct pci_eject_response {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000371 struct pci_message message_type;
Jake Oshins4daace02016-02-16 21:56:23 +0000372 union win_slot_encoding wslot;
373 u32 status;
374} __packed;
375
376static int pci_ring_size = (4 * PAGE_SIZE);
377
378/*
379 * Definitions or interrupt steering hypercall.
380 */
381#define HV_PARTITION_ID_SELF ((u64)-1)
382#define HVCALL_RETARGET_INTERRUPT 0x7e
383
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700384struct hv_interrupt_entry {
Jake Oshins4daace02016-02-16 21:56:23 +0000385 u32 source; /* 1 for MSI(-X) */
386 u32 reserved1;
387 u32 address;
388 u32 data;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700389};
390
391#define HV_VP_SET_BANK_COUNT_MAX 5 /* current implementation limit */
392
393struct hv_vp_set {
394 u64 format; /* 0 (HvGenericSetSparse4k) */
395 u64 valid_banks;
396 u64 masks[HV_VP_SET_BANK_COUNT_MAX];
397};
398
399/*
400 * flags for hv_device_interrupt_target.flags
401 */
402#define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
403#define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
404
405struct hv_device_interrupt_target {
Jake Oshins4daace02016-02-16 21:56:23 +0000406 u32 vector;
407 u32 flags;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700408 union {
409 u64 vp_mask;
410 struct hv_vp_set vp_set;
411 };
412};
413
414struct retarget_msi_interrupt {
415 u64 partition_id; /* use "self" */
416 u64 device_id;
417 struct hv_interrupt_entry int_entry;
418 u64 reserved2;
419 struct hv_device_interrupt_target int_target;
Jake Oshins4daace02016-02-16 21:56:23 +0000420} __packed;
421
422/*
423 * Driver specific state.
424 */
425
426enum hv_pcibus_state {
427 hv_pcibus_init = 0,
428 hv_pcibus_probed,
429 hv_pcibus_installed,
Long Lid3a78d82017-03-23 14:58:10 -0700430 hv_pcibus_removed,
Jake Oshins4daace02016-02-16 21:56:23 +0000431 hv_pcibus_maximum
432};
433
434struct hv_pcibus_device {
435 struct pci_sysdata sysdata;
436 enum hv_pcibus_state state;
Stephen Hemminger6708be92018-05-23 10:11:13 -0700437 refcount_t remove_lock;
Jake Oshins4daace02016-02-16 21:56:23 +0000438 struct hv_device *hdev;
439 resource_size_t low_mmio_space;
440 resource_size_t high_mmio_space;
441 struct resource *mem_config;
442 struct resource *low_mmio_res;
443 struct resource *high_mmio_res;
444 struct completion *survey_event;
445 struct completion remove_event;
446 struct pci_bus *pci_bus;
447 spinlock_t config_lock; /* Avoid two threads writing index page */
448 spinlock_t device_list_lock; /* Protect lists below */
449 void __iomem *cfg_addr;
450
Jake Oshins4daace02016-02-16 21:56:23 +0000451 struct list_head resources_for_children;
452
453 struct list_head children;
454 struct list_head dr_list;
Jake Oshins4daace02016-02-16 21:56:23 +0000455
456 struct msi_domain_info msi_info;
457 struct msi_controller msi_chip;
458 struct irq_domain *irq_domain;
Jork Loeserbe66b672017-05-24 13:41:25 -0700459
460 /* hypercall arg, must not cross page boundary */
Long Li0de8ce32016-11-08 14:04:38 -0800461 struct retarget_msi_interrupt retarget_msi_interrupt_params;
Jork Loeserbe66b672017-05-24 13:41:25 -0700462
Long Li0de8ce32016-11-08 14:04:38 -0800463 spinlock_t retarget_msi_interrupt_lock;
Dexuan Cui021ad272018-03-15 14:20:53 +0000464
465 struct workqueue_struct *wq;
Jake Oshins4daace02016-02-16 21:56:23 +0000466};
467
468/*
469 * Tracks "Device Relations" messages from the host, which must be both
470 * processed in order and deferred so that they don't run in the context
471 * of the incoming packet callback.
472 */
473struct hv_dr_work {
474 struct work_struct wrk;
475 struct hv_pcibus_device *bus;
476};
477
478struct hv_dr_state {
479 struct list_head list_entry;
480 u32 device_count;
Dexuan Cui7d0f8ee2016-08-23 04:46:39 +0000481 struct pci_function_description func[0];
Jake Oshins4daace02016-02-16 21:56:23 +0000482};
483
484enum hv_pcichild_state {
485 hv_pcichild_init = 0,
486 hv_pcichild_requirements,
487 hv_pcichild_resourced,
488 hv_pcichild_ejecting,
489 hv_pcichild_maximum
490};
491
Jake Oshins4daace02016-02-16 21:56:23 +0000492struct hv_pci_dev {
493 /* List protected by pci_rescan_remove_lock */
494 struct list_head list_entry;
Elena Reshetova24196f02017-04-18 09:02:48 -0500495 refcount_t refs;
Jake Oshins4daace02016-02-16 21:56:23 +0000496 enum hv_pcichild_state state;
497 struct pci_function_description desc;
498 bool reported_missing;
499 struct hv_pcibus_device *hbus;
500 struct work_struct wrk;
501
502 /*
503 * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
504 * read it back, for each of the BAR offsets within config space.
505 */
506 u32 probed_bar[6];
507};
508
509struct hv_pci_compl {
510 struct completion host_event;
511 s32 completion_status;
512};
513
Dexuan Cuide0aa7b2018-03-15 14:21:08 +0000514static void hv_pci_onchannelcallback(void *context);
515
Jake Oshins4daace02016-02-16 21:56:23 +0000516/**
517 * hv_pci_generic_compl() - Invoked for a completion packet
518 * @context: Set up by the sender of the packet.
519 * @resp: The response packet
520 * @resp_packet_size: Size in bytes of the packet
521 *
522 * This function is used to trigger an event and report status
523 * for any message for which the completion packet contains a
524 * status and nothing else.
525 */
Dexuan Cuia5b45b72016-08-23 04:49:22 +0000526static void hv_pci_generic_compl(void *context, struct pci_response *resp,
527 int resp_packet_size)
Jake Oshins4daace02016-02-16 21:56:23 +0000528{
529 struct hv_pci_compl *comp_pkt = context;
530
531 if (resp_packet_size >= offsetofend(struct pci_response, status))
532 comp_pkt->completion_status = resp->status;
Dexuan Cuia5b45b72016-08-23 04:49:22 +0000533 else
534 comp_pkt->completion_status = -1;
535
Jake Oshins4daace02016-02-16 21:56:23 +0000536 complete(&comp_pkt->host_event);
537}
538
539static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
540 u32 wslot);
Stephen Hemminger8c99e122018-05-23 10:11:12 -0700541
542static void get_pcichild(struct hv_pci_dev *hpdev)
543{
544 refcount_inc(&hpdev->refs);
545}
546
547static void put_pcichild(struct hv_pci_dev *hpdev)
548{
549 if (refcount_dec_and_test(&hpdev->refs))
550 kfree(hpdev);
551}
Jake Oshins4daace02016-02-16 21:56:23 +0000552
553static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
554static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
555
Dexuan Cuic3635da2018-05-23 21:12:01 +0000556/*
557 * There is no good way to get notified from vmbus_onoffer_rescind(),
558 * so let's use polling here, since this is not a hot path.
559 */
560static int wait_for_response(struct hv_device *hdev,
561 struct completion *comp)
562{
563 while (true) {
564 if (hdev->channel->rescind) {
565 dev_warn_once(&hdev->device, "The device is gone.\n");
566 return -ENODEV;
567 }
568
569 if (wait_for_completion_timeout(comp, HZ / 10))
570 break;
571 }
572
573 return 0;
574}
575
Jake Oshins4daace02016-02-16 21:56:23 +0000576/**
577 * devfn_to_wslot() - Convert from Linux PCI slot to Windows
578 * @devfn: The Linux representation of PCI slot
579 *
580 * Windows uses a slightly different representation of PCI slot.
581 *
582 * Return: The Windows representation
583 */
584static u32 devfn_to_wslot(int devfn)
585{
586 union win_slot_encoding wslot;
587
588 wslot.slot = 0;
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600589 wslot.bits.dev = PCI_SLOT(devfn);
590 wslot.bits.func = PCI_FUNC(devfn);
Jake Oshins4daace02016-02-16 21:56:23 +0000591
592 return wslot.slot;
593}
594
595/**
596 * wslot_to_devfn() - Convert from Windows PCI slot to Linux
597 * @wslot: The Windows representation of PCI slot
598 *
599 * Windows uses a slightly different representation of PCI slot.
600 *
601 * Return: The Linux representation
602 */
603static int wslot_to_devfn(u32 wslot)
604{
605 union win_slot_encoding slot_no;
606
607 slot_no.slot = wslot;
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600608 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
Jake Oshins4daace02016-02-16 21:56:23 +0000609}
610
611/*
612 * PCI Configuration Space for these root PCI buses is implemented as a pair
613 * of pages in memory-mapped I/O space. Writing to the first page chooses
614 * the PCI function being written or read. Once the first page has been
615 * written to, the following page maps in the entire configuration space of
616 * the function.
617 */
618
619/**
620 * _hv_pcifront_read_config() - Internal PCI config read
621 * @hpdev: The PCI driver's representation of the device
622 * @where: Offset within config space
623 * @size: Size of the transfer
624 * @val: Pointer to the buffer receiving the data
625 */
626static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
627 int size, u32 *val)
628{
629 unsigned long flags;
630 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
631
632 /*
633 * If the attempt is to read the IDs or the ROM BAR, simulate that.
634 */
635 if (where + size <= PCI_COMMAND) {
636 memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
637 } else if (where >= PCI_CLASS_REVISION && where + size <=
638 PCI_CACHE_LINE_SIZE) {
639 memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
640 PCI_CLASS_REVISION, size);
641 } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
642 PCI_ROM_ADDRESS) {
643 memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
644 PCI_SUBSYSTEM_VENDOR_ID, size);
645 } else if (where >= PCI_ROM_ADDRESS && where + size <=
646 PCI_CAPABILITY_LIST) {
647 /* ROM BARs are unimplemented */
648 *val = 0;
649 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
650 PCI_INTERRUPT_PIN) {
651 /*
652 * Interrupt Line and Interrupt PIN are hard-wired to zero
653 * because this front-end only supports message-signaled
654 * interrupts.
655 */
656 *val = 0;
657 } else if (where + size <= CFG_PAGE_SIZE) {
658 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
659 /* Choose the function to be read. (See comment above) */
660 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200661 /* Make sure the function was chosen before we start reading. */
662 mb();
Jake Oshins4daace02016-02-16 21:56:23 +0000663 /* Read from that function's config space. */
664 switch (size) {
665 case 1:
666 *val = readb(addr);
667 break;
668 case 2:
669 *val = readw(addr);
670 break;
671 default:
672 *val = readl(addr);
673 break;
674 }
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200675 /*
Dexuan Cuidf3f2152018-03-15 14:21:35 +0000676 * Make sure the read was done before we release the spinlock
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200677 * allowing consecutive reads/writes.
678 */
679 mb();
Jake Oshins4daace02016-02-16 21:56:23 +0000680 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
681 } else {
682 dev_err(&hpdev->hbus->hdev->device,
683 "Attempt to read beyond a function's config space.\n");
684 }
685}
686
Dexuan Cuide0aa7b2018-03-15 14:21:08 +0000687static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
688{
689 u16 ret;
690 unsigned long flags;
691 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
692 PCI_VENDOR_ID;
693
694 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
695
696 /* Choose the function to be read. (See comment above) */
697 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
698 /* Make sure the function was chosen before we start reading. */
699 mb();
700 /* Read from that function's config space. */
701 ret = readw(addr);
702 /*
703 * mb() is not required here, because the spin_unlock_irqrestore()
704 * is a barrier.
705 */
706
707 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
708
709 return ret;
710}
711
Jake Oshins4daace02016-02-16 21:56:23 +0000712/**
713 * _hv_pcifront_write_config() - Internal PCI config write
714 * @hpdev: The PCI driver's representation of the device
715 * @where: Offset within config space
716 * @size: Size of the transfer
717 * @val: The data being transferred
718 */
719static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
720 int size, u32 val)
721{
722 unsigned long flags;
723 void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
724
725 if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
726 where + size <= PCI_CAPABILITY_LIST) {
727 /* SSIDs and ROM BARs are read-only */
728 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
729 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
730 /* Choose the function to be written. (See comment above) */
731 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200732 /* Make sure the function was chosen before we start writing. */
733 wmb();
Jake Oshins4daace02016-02-16 21:56:23 +0000734 /* Write to that function's config space. */
735 switch (size) {
736 case 1:
737 writeb(val, addr);
738 break;
739 case 2:
740 writew(val, addr);
741 break;
742 default:
743 writel(val, addr);
744 break;
745 }
Vitaly Kuznetsovbdd74442016-05-03 14:22:00 +0200746 /*
747 * Make sure the write was done before we release the spinlock
748 * allowing consecutive reads/writes.
749 */
750 mb();
Jake Oshins4daace02016-02-16 21:56:23 +0000751 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
752 } else {
753 dev_err(&hpdev->hbus->hdev->device,
754 "Attempt to write beyond a function's config space.\n");
755 }
756}
757
758/**
759 * hv_pcifront_read_config() - Read configuration space
760 * @bus: PCI Bus structure
761 * @devfn: Device/function
762 * @where: Offset from base
763 * @size: Byte/word/dword
764 * @val: Value to be read
765 *
766 * Return: PCIBIOS_SUCCESSFUL on success
767 * PCIBIOS_DEVICE_NOT_FOUND on failure
768 */
769static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
770 int where, int size, u32 *val)
771{
772 struct hv_pcibus_device *hbus =
773 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
774 struct hv_pci_dev *hpdev;
775
776 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
777 if (!hpdev)
778 return PCIBIOS_DEVICE_NOT_FOUND;
779
780 _hv_pcifront_read_config(hpdev, where, size, val);
781
Stephen Hemminger8c99e122018-05-23 10:11:12 -0700782 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +0000783 return PCIBIOS_SUCCESSFUL;
784}
785
786/**
787 * hv_pcifront_write_config() - Write configuration space
788 * @bus: PCI Bus structure
789 * @devfn: Device/function
790 * @where: Offset from base
791 * @size: Byte/word/dword
792 * @val: Value to be written to device
793 *
794 * Return: PCIBIOS_SUCCESSFUL on success
795 * PCIBIOS_DEVICE_NOT_FOUND on failure
796 */
797static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
798 int where, int size, u32 val)
799{
800 struct hv_pcibus_device *hbus =
801 container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
802 struct hv_pci_dev *hpdev;
803
804 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
805 if (!hpdev)
806 return PCIBIOS_DEVICE_NOT_FOUND;
807
808 _hv_pcifront_write_config(hpdev, where, size, val);
809
Stephen Hemminger8c99e122018-05-23 10:11:12 -0700810 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +0000811 return PCIBIOS_SUCCESSFUL;
812}
813
814/* PCIe operations */
815static struct pci_ops hv_pcifront_ops = {
816 .read = hv_pcifront_read_config,
817 .write = hv_pcifront_write_config,
818};
819
820/* Interrupt management hooks */
821static void hv_int_desc_free(struct hv_pci_dev *hpdev,
822 struct tran_int_desc *int_desc)
823{
824 struct pci_delete_interrupt *int_pkt;
825 struct {
826 struct pci_packet pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000827 u8 buffer[sizeof(struct pci_delete_interrupt)];
Jake Oshins4daace02016-02-16 21:56:23 +0000828 } ctxt;
829
830 memset(&ctxt, 0, sizeof(ctxt));
831 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000832 int_pkt->message_type.type =
Jake Oshins4daace02016-02-16 21:56:23 +0000833 PCI_DELETE_INTERRUPT_MESSAGE;
834 int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
835 int_pkt->int_desc = *int_desc;
836 vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
837 (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
838 kfree(int_desc);
839}
840
841/**
842 * hv_msi_free() - Free the MSI.
843 * @domain: The interrupt domain pointer
844 * @info: Extra MSI-related context
845 * @irq: Identifies the IRQ.
846 *
847 * The Hyper-V parent partition and hypervisor are tracking the
848 * messages that are in use, keeping the interrupt redirection
849 * table up to date. This callback sends a message that frees
850 * the IRT entry and related tracking nonsense.
851 */
852static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
853 unsigned int irq)
854{
855 struct hv_pcibus_device *hbus;
856 struct hv_pci_dev *hpdev;
857 struct pci_dev *pdev;
858 struct tran_int_desc *int_desc;
859 struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
860 struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
861
862 pdev = msi_desc_to_pci_dev(msi);
863 hbus = info->data;
Cathy Avery0c6e6172016-07-12 11:31:24 -0400864 int_desc = irq_data_get_irq_chip_data(irq_data);
865 if (!int_desc)
Jake Oshins4daace02016-02-16 21:56:23 +0000866 return;
867
Cathy Avery0c6e6172016-07-12 11:31:24 -0400868 irq_data->chip_data = NULL;
869 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
870 if (!hpdev) {
871 kfree(int_desc);
872 return;
Jake Oshins4daace02016-02-16 21:56:23 +0000873 }
874
Cathy Avery0c6e6172016-07-12 11:31:24 -0400875 hv_int_desc_free(hpdev, int_desc);
Stephen Hemminger8c99e122018-05-23 10:11:12 -0700876 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +0000877}
878
879static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
880 bool force)
881{
882 struct irq_data *parent = data->parent_data;
883
884 return parent->chip->irq_set_affinity(parent, dest, force);
885}
886
Tobias Klauser542ccf42016-10-31 12:04:09 +0100887static void hv_irq_mask(struct irq_data *data)
Jake Oshins4daace02016-02-16 21:56:23 +0000888{
889 pci_msi_mask_irq(data);
890}
891
892/**
893 * hv_irq_unmask() - "Unmask" the IRQ by setting its current
894 * affinity.
895 * @data: Describes the IRQ
896 *
897 * Build new a destination for the MSI and make a hypercall to
898 * update the Interrupt Redirection Table. "Device Logical ID"
899 * is built out of this PCI bus's instance GUID and the function
900 * number of the device.
901 */
Tobias Klauser542ccf42016-10-31 12:04:09 +0100902static void hv_irq_unmask(struct irq_data *data)
Jake Oshins4daace02016-02-16 21:56:23 +0000903{
904 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
905 struct irq_cfg *cfg = irqd_cfg(data);
Long Li0de8ce32016-11-08 14:04:38 -0800906 struct retarget_msi_interrupt *params;
Jake Oshins4daace02016-02-16 21:56:23 +0000907 struct hv_pcibus_device *hbus;
908 struct cpumask *dest;
909 struct pci_bus *pbus;
910 struct pci_dev *pdev;
Long Li0de8ce32016-11-08 14:04:38 -0800911 unsigned long flags;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700912 u32 var_size = 0;
913 int cpu_vmbus;
914 int cpu;
915 u64 res;
Jake Oshins4daace02016-02-16 21:56:23 +0000916
Dexuan Cui79aa8012017-11-01 20:30:53 +0000917 dest = irq_data_get_effective_affinity_mask(data);
Jake Oshins4daace02016-02-16 21:56:23 +0000918 pdev = msi_desc_to_pci_dev(msi_desc);
919 pbus = pdev->bus;
920 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
921
Long Li0de8ce32016-11-08 14:04:38 -0800922 spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
923
924 params = &hbus->retarget_msi_interrupt_params;
925 memset(params, 0, sizeof(*params));
926 params->partition_id = HV_PARTITION_ID_SELF;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700927 params->int_entry.source = 1; /* MSI(-X) */
928 params->int_entry.address = msi_desc->msg.address_lo;
929 params->int_entry.data = msi_desc->msg.data;
Long Li0de8ce32016-11-08 14:04:38 -0800930 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
Jake Oshins4daace02016-02-16 21:56:23 +0000931 (hbus->hdev->dev_instance.b[4] << 16) |
932 (hbus->hdev->dev_instance.b[7] << 8) |
933 (hbus->hdev->dev_instance.b[6] & 0xf8) |
934 PCI_FUNC(pdev->devfn);
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700935 params->int_target.vector = cfg->vector;
Jake Oshins4daace02016-02-16 21:56:23 +0000936
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700937 /*
938 * Honoring apic->irq_delivery_mode set to dest_Fixed by
939 * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
940 * spurious interrupt storm. Not doing so does not seem to have a
941 * negative effect (yet?).
942 */
Jake Oshins4daace02016-02-16 21:56:23 +0000943
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700944 if (pci_protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
945 /*
946 * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
947 * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
948 * with >64 VP support.
949 * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
950 * is not sufficient for this hypercall.
951 */
952 params->int_target.flags |=
953 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
954 params->int_target.vp_set.valid_banks =
955 (1ull << HV_VP_SET_BANK_COUNT_MAX) - 1;
Long Li0de8ce32016-11-08 14:04:38 -0800956
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700957 /*
958 * var-sized hypercall, var-size starts after vp_mask (thus
959 * vp_set.format does not count, but vp_set.valid_banks does).
960 */
961 var_size = 1 + HV_VP_SET_BANK_COUNT_MAX;
962
963 for_each_cpu_and(cpu, dest, cpu_online_mask) {
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200964 cpu_vmbus = hv_cpu_number_to_vp_number(cpu);
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700965
966 if (cpu_vmbus >= HV_VP_SET_BANK_COUNT_MAX * 64) {
967 dev_err(&hbus->hdev->device,
968 "too high CPU %d", cpu_vmbus);
969 res = 1;
970 goto exit_unlock;
971 }
972
973 params->int_target.vp_set.masks[cpu_vmbus / 64] |=
974 (1ULL << (cpu_vmbus & 63));
975 }
976 } else {
977 for_each_cpu_and(cpu, dest, cpu_online_mask) {
978 params->int_target.vp_mask |=
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200979 (1ULL << hv_cpu_number_to_vp_number(cpu));
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700980 }
981 }
982
983 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
984 params, NULL);
985
986exit_unlock:
Long Li0de8ce32016-11-08 14:04:38 -0800987 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
Jake Oshins4daace02016-02-16 21:56:23 +0000988
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700989 if (res) {
990 dev_err(&hbus->hdev->device,
991 "%s() failed: %#llx", __func__, res);
992 return;
993 }
994
Jake Oshins4daace02016-02-16 21:56:23 +0000995 pci_msi_unmask_irq(data);
996}
997
998struct compose_comp_ctxt {
999 struct hv_pci_compl comp_pkt;
1000 struct tran_int_desc int_desc;
1001};
1002
1003static void hv_pci_compose_compl(void *context, struct pci_response *resp,
1004 int resp_packet_size)
1005{
1006 struct compose_comp_ctxt *comp_pkt = context;
1007 struct pci_create_int_response *int_resp =
1008 (struct pci_create_int_response *)resp;
1009
1010 comp_pkt->comp_pkt.completion_status = resp->status;
1011 comp_pkt->int_desc = int_resp->int_desc;
1012 complete(&comp_pkt->comp_pkt.host_event);
1013}
1014
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001015static u32 hv_compose_msi_req_v1(
1016 struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
1017 u32 slot, u8 vector)
1018{
1019 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
1020 int_pkt->wslot.slot = slot;
1021 int_pkt->int_desc.vector = vector;
1022 int_pkt->int_desc.vector_count = 1;
Thomas Gleixnera31e58e2017-12-28 11:33:33 +01001023 int_pkt->int_desc.delivery_mode = dest_Fixed;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001024
1025 /*
1026 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
1027 * hv_irq_unmask().
1028 */
1029 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
1030
1031 return sizeof(*int_pkt);
1032}
1033
1034static u32 hv_compose_msi_req_v2(
1035 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
1036 u32 slot, u8 vector)
1037{
1038 int cpu;
1039
1040 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1041 int_pkt->wslot.slot = slot;
1042 int_pkt->int_desc.vector = vector;
1043 int_pkt->int_desc.vector_count = 1;
Thomas Gleixnera31e58e2017-12-28 11:33:33 +01001044 int_pkt->int_desc.delivery_mode = dest_Fixed;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001045
1046 /*
1047 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1048 * by subsequent retarget in hv_irq_unmask().
1049 */
1050 cpu = cpumask_first_and(affinity, cpu_online_mask);
1051 int_pkt->int_desc.processor_array[0] =
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +02001052 hv_cpu_number_to_vp_number(cpu);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001053 int_pkt->int_desc.processor_count = 1;
1054
1055 return sizeof(*int_pkt);
1056}
1057
Jake Oshins4daace02016-02-16 21:56:23 +00001058/**
1059 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1060 * @data: Everything about this MSI
1061 * @msg: Buffer that is filled in by this function
1062 *
1063 * This function unpacks the IRQ looking for target CPU set, IDT
1064 * vector and mode and sends a message to the parent partition
1065 * asking for a mapping for that tuple in this partition. The
1066 * response supplies a data value and address to which that data
1067 * should be written to trigger that interrupt.
1068 */
1069static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1070{
1071 struct irq_cfg *cfg = irqd_cfg(data);
1072 struct hv_pcibus_device *hbus;
1073 struct hv_pci_dev *hpdev;
1074 struct pci_bus *pbus;
1075 struct pci_dev *pdev;
Dexuan Cui79aa8012017-11-01 20:30:53 +00001076 struct cpumask *dest;
Jake Oshins4daace02016-02-16 21:56:23 +00001077 struct compose_comp_ctxt comp;
1078 struct tran_int_desc *int_desc;
Jake Oshins4daace02016-02-16 21:56:23 +00001079 struct {
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001080 struct pci_packet pci_pkt;
1081 union {
1082 struct pci_create_interrupt v1;
1083 struct pci_create_interrupt2 v2;
1084 } int_pkts;
1085 } __packed ctxt;
1086
1087 u32 size;
Jake Oshins4daace02016-02-16 21:56:23 +00001088 int ret;
1089
1090 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
Dexuan Cui79aa8012017-11-01 20:30:53 +00001091 dest = irq_data_get_effective_affinity_mask(data);
Jake Oshins4daace02016-02-16 21:56:23 +00001092 pbus = pdev->bus;
1093 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
1094 hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
1095 if (!hpdev)
1096 goto return_null_message;
1097
1098 /* Free any previous message that might have already been composed. */
1099 if (data->chip_data) {
1100 int_desc = data->chip_data;
1101 data->chip_data = NULL;
1102 hv_int_desc_free(hpdev, int_desc);
1103 }
1104
K. Y. Srinivasan59c58cee2017-03-24 11:07:22 -07001105 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
Jake Oshins4daace02016-02-16 21:56:23 +00001106 if (!int_desc)
1107 goto drop_reference;
1108
1109 memset(&ctxt, 0, sizeof(ctxt));
1110 init_completion(&comp.comp_pkt.host_event);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001111 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1112 ctxt.pci_pkt.compl_ctxt = &comp;
Jake Oshins4daace02016-02-16 21:56:23 +00001113
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001114 switch (pci_protocol_version) {
1115 case PCI_PROTOCOL_VERSION_1_1:
1116 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
Dexuan Cui79aa8012017-11-01 20:30:53 +00001117 dest,
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001118 hpdev->desc.win_slot.slot,
1119 cfg->vector);
1120 break;
1121
1122 case PCI_PROTOCOL_VERSION_1_2:
1123 size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
Dexuan Cui79aa8012017-11-01 20:30:53 +00001124 dest,
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001125 hpdev->desc.win_slot.slot,
1126 cfg->vector);
1127 break;
1128
1129 default:
1130 /* As we only negotiate protocol versions known to this driver,
1131 * this path should never hit. However, this is it not a hot
1132 * path so we print a message to aid future updates.
1133 */
1134 dev_err(&hbus->hdev->device,
1135 "Unexpected vPCI protocol, update driver.");
1136 goto free_int_desc;
Jake Oshins4daace02016-02-16 21:56:23 +00001137 }
1138
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001139 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1140 size, (unsigned long)&ctxt.pci_pkt,
Jake Oshins4daace02016-02-16 21:56:23 +00001141 VM_PKT_DATA_INBAND,
1142 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001143 if (ret) {
1144 dev_err(&hbus->hdev->device,
1145 "Sending request for interrupt failed: 0x%x",
1146 comp.comp_pkt.completion_status);
Dexuan Cui665e2242016-08-23 04:48:11 +00001147 goto free_int_desc;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001148 }
Dexuan Cui665e2242016-08-23 04:48:11 +00001149
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001150 /*
1151 * Since this function is called with IRQ locks held, can't
1152 * do normal wait for completion; instead poll.
1153 */
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001154 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
1155 /* 0xFFFF means an invalid PCI VENDOR ID. */
1156 if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
1157 dev_err_once(&hbus->hdev->device,
1158 "the device has gone\n");
1159 goto free_int_desc;
1160 }
1161
1162 /*
1163 * When the higher level interrupt code calls us with
1164 * interrupt disabled, we must poll the channel by calling
1165 * the channel callback directly when channel->target_cpu is
1166 * the current CPU. When the higher level interrupt code
1167 * calls us with interrupt enabled, let's add the
1168 * local_bh_disable()/enable() to avoid race.
1169 */
1170 local_bh_disable();
1171
1172 if (hbus->hdev->channel->target_cpu == smp_processor_id())
1173 hv_pci_onchannelcallback(hbus);
1174
1175 local_bh_enable();
1176
1177 if (hpdev->state == hv_pcichild_ejecting) {
1178 dev_err_once(&hbus->hdev->device,
1179 "the device is being ejected\n");
1180 goto free_int_desc;
1181 }
1182
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001183 udelay(100);
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001184 }
Jake Oshins4daace02016-02-16 21:56:23 +00001185
1186 if (comp.comp_pkt.completion_status < 0) {
1187 dev_err(&hbus->hdev->device,
1188 "Request for interrupt failed: 0x%x",
1189 comp.comp_pkt.completion_status);
1190 goto free_int_desc;
1191 }
1192
1193 /*
1194 * Record the assignment so that this can be unwound later. Using
1195 * irq_set_chip_data() here would be appropriate, but the lock it takes
1196 * is already held.
1197 */
1198 *int_desc = comp.int_desc;
1199 data->chip_data = int_desc;
1200
1201 /* Pass up the result. */
1202 msg->address_hi = comp.int_desc.address >> 32;
1203 msg->address_lo = comp.int_desc.address & 0xffffffff;
1204 msg->data = comp.int_desc.data;
1205
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001206 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001207 return;
1208
1209free_int_desc:
1210 kfree(int_desc);
1211drop_reference:
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001212 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001213return_null_message:
1214 msg->address_hi = 0;
1215 msg->address_lo = 0;
1216 msg->data = 0;
1217}
1218
1219/* HW Interrupt Chip Descriptor */
1220static struct irq_chip hv_msi_irq_chip = {
1221 .name = "Hyper-V PCIe MSI",
1222 .irq_compose_msi_msg = hv_compose_msi_msg,
1223 .irq_set_affinity = hv_set_affinity,
1224 .irq_ack = irq_chip_ack_parent,
1225 .irq_mask = hv_irq_mask,
1226 .irq_unmask = hv_irq_unmask,
1227};
1228
1229static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
1230 msi_alloc_info_t *arg)
1231{
1232 return arg->msi_hwirq;
1233}
1234
1235static struct msi_domain_ops hv_msi_ops = {
1236 .get_hwirq = hv_msi_domain_ops_get_hwirq,
1237 .msi_prepare = pci_msi_prepare,
1238 .set_desc = pci_msi_set_desc,
1239 .msi_free = hv_msi_free,
1240};
1241
1242/**
1243 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1244 * @hbus: The root PCI bus
1245 *
1246 * This function creates an IRQ domain which will be used for
1247 * interrupts from devices that have been passed through. These
1248 * devices only support MSI and MSI-X, not line-based interrupts
1249 * or simulations of line-based interrupts through PCIe's
1250 * fabric-layer messages. Because interrupts are remapped, we
1251 * can support multi-message MSI here.
1252 *
1253 * Return: '0' on success and error value on failure
1254 */
1255static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1256{
1257 hbus->msi_info.chip = &hv_msi_irq_chip;
1258 hbus->msi_info.ops = &hv_msi_ops;
1259 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1260 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1261 MSI_FLAG_PCI_MSIX);
1262 hbus->msi_info.handler = handle_edge_irq;
1263 hbus->msi_info.handler_name = "edge";
1264 hbus->msi_info.data = hbus;
1265 hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
1266 &hbus->msi_info,
1267 x86_vector_domain);
1268 if (!hbus->irq_domain) {
1269 dev_err(&hbus->hdev->device,
1270 "Failed to build an MSI IRQ domain\n");
1271 return -ENODEV;
1272 }
1273
1274 return 0;
1275}
1276
1277/**
1278 * get_bar_size() - Get the address space consumed by a BAR
1279 * @bar_val: Value that a BAR returned after -1 was written
1280 * to it.
1281 *
1282 * This function returns the size of the BAR, rounded up to 1
1283 * page. It has to be rounded up because the hypervisor's page
1284 * table entry that maps the BAR into the VM can't specify an
1285 * offset within a page. The invariant is that the hypervisor
1286 * must place any BARs of smaller than page length at the
1287 * beginning of a page.
1288 *
1289 * Return: Size in bytes of the consumed MMIO space.
1290 */
1291static u64 get_bar_size(u64 bar_val)
1292{
1293 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1294 PAGE_SIZE);
1295}
1296
1297/**
1298 * survey_child_resources() - Total all MMIO requirements
1299 * @hbus: Root PCI bus, as understood by this driver
1300 */
1301static void survey_child_resources(struct hv_pcibus_device *hbus)
1302{
Jake Oshins4daace02016-02-16 21:56:23 +00001303 struct hv_pci_dev *hpdev;
1304 resource_size_t bar_size = 0;
1305 unsigned long flags;
1306 struct completion *event;
1307 u64 bar_val;
1308 int i;
1309
1310 /* If nobody is waiting on the answer, don't compute it. */
1311 event = xchg(&hbus->survey_event, NULL);
1312 if (!event)
1313 return;
1314
1315 /* If the answer has already been computed, go with it. */
1316 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1317 complete(event);
1318 return;
1319 }
1320
1321 spin_lock_irqsave(&hbus->device_list_lock, flags);
1322
1323 /*
1324 * Due to an interesting quirk of the PCI spec, all memory regions
1325 * for a child device are a power of 2 in size and aligned in memory,
1326 * so it's sufficient to just add them up without tracking alignment.
1327 */
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001328 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Jake Oshins4daace02016-02-16 21:56:23 +00001329 for (i = 0; i < 6; i++) {
1330 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1331 dev_err(&hbus->hdev->device,
1332 "There's an I/O BAR in this list!\n");
1333
1334 if (hpdev->probed_bar[i] != 0) {
1335 /*
1336 * A probed BAR has all the upper bits set that
1337 * can be changed.
1338 */
1339
1340 bar_val = hpdev->probed_bar[i];
1341 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1342 bar_val |=
1343 ((u64)hpdev->probed_bar[++i] << 32);
1344 else
1345 bar_val |= 0xffffffff00000000ULL;
1346
1347 bar_size = get_bar_size(bar_val);
1348
1349 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1350 hbus->high_mmio_space += bar_size;
1351 else
1352 hbus->low_mmio_space += bar_size;
1353 }
1354 }
1355 }
1356
1357 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1358 complete(event);
1359}
1360
1361/**
1362 * prepopulate_bars() - Fill in BARs with defaults
1363 * @hbus: Root PCI bus, as understood by this driver
1364 *
1365 * The core PCI driver code seems much, much happier if the BARs
1366 * for a device have values upon first scan. So fill them in.
1367 * The algorithm below works down from large sizes to small,
1368 * attempting to pack the assignments optimally. The assumption,
1369 * enforced in other parts of the code, is that the beginning of
1370 * the memory-mapped I/O space will be aligned on the largest
1371 * BAR size.
1372 */
1373static void prepopulate_bars(struct hv_pcibus_device *hbus)
1374{
1375 resource_size_t high_size = 0;
1376 resource_size_t low_size = 0;
1377 resource_size_t high_base = 0;
1378 resource_size_t low_base = 0;
1379 resource_size_t bar_size;
1380 struct hv_pci_dev *hpdev;
Jake Oshins4daace02016-02-16 21:56:23 +00001381 unsigned long flags;
1382 u64 bar_val;
1383 u32 command;
1384 bool high;
1385 int i;
1386
1387 if (hbus->low_mmio_space) {
1388 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
1389 low_base = hbus->low_mmio_res->start;
1390 }
1391
1392 if (hbus->high_mmio_space) {
1393 high_size = 1ULL <<
1394 (63 - __builtin_clzll(hbus->high_mmio_space));
1395 high_base = hbus->high_mmio_res->start;
1396 }
1397
1398 spin_lock_irqsave(&hbus->device_list_lock, flags);
1399
1400 /* Pick addresses for the BARs. */
1401 do {
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001402 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Jake Oshins4daace02016-02-16 21:56:23 +00001403 for (i = 0; i < 6; i++) {
1404 bar_val = hpdev->probed_bar[i];
1405 if (bar_val == 0)
1406 continue;
1407 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
1408 if (high) {
1409 bar_val |=
1410 ((u64)hpdev->probed_bar[i + 1]
1411 << 32);
1412 } else {
1413 bar_val |= 0xffffffffULL << 32;
1414 }
1415 bar_size = get_bar_size(bar_val);
1416 if (high) {
1417 if (high_size != bar_size) {
1418 i++;
1419 continue;
1420 }
1421 _hv_pcifront_write_config(hpdev,
1422 PCI_BASE_ADDRESS_0 + (4 * i),
1423 4,
1424 (u32)(high_base & 0xffffff00));
1425 i++;
1426 _hv_pcifront_write_config(hpdev,
1427 PCI_BASE_ADDRESS_0 + (4 * i),
1428 4, (u32)(high_base >> 32));
1429 high_base += bar_size;
1430 } else {
1431 if (low_size != bar_size)
1432 continue;
1433 _hv_pcifront_write_config(hpdev,
1434 PCI_BASE_ADDRESS_0 + (4 * i),
1435 4,
1436 (u32)(low_base & 0xffffff00));
1437 low_base += bar_size;
1438 }
1439 }
1440 if (high_size <= 1 && low_size <= 1) {
1441 /* Set the memory enable bit. */
1442 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
1443 &command);
1444 command |= PCI_COMMAND_MEMORY;
1445 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
1446 command);
1447 break;
1448 }
1449 }
1450
1451 high_size >>= 1;
1452 low_size >>= 1;
1453 } while (high_size || low_size);
1454
1455 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1456}
1457
1458/**
1459 * create_root_hv_pci_bus() - Expose a new root PCI bus
1460 * @hbus: Root PCI bus, as understood by this driver
1461 *
1462 * Return: 0 on success, -errno on failure
1463 */
1464static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
1465{
1466 /* Register the device */
1467 hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
1468 0, /* bus number is always zero */
1469 &hv_pcifront_ops,
1470 &hbus->sysdata,
1471 &hbus->resources_for_children);
1472 if (!hbus->pci_bus)
1473 return -ENODEV;
1474
1475 hbus->pci_bus->msi = &hbus->msi_chip;
1476 hbus->pci_bus->msi->dev = &hbus->hdev->device;
1477
Long Li414428c2017-03-23 14:58:32 -07001478 pci_lock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001479 pci_scan_child_bus(hbus->pci_bus);
1480 pci_bus_assign_resources(hbus->pci_bus);
1481 pci_bus_add_devices(hbus->pci_bus);
Long Li414428c2017-03-23 14:58:32 -07001482 pci_unlock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001483 hbus->state = hv_pcibus_installed;
1484 return 0;
1485}
1486
1487struct q_res_req_compl {
1488 struct completion host_event;
1489 struct hv_pci_dev *hpdev;
1490};
1491
1492/**
1493 * q_resource_requirements() - Query Resource Requirements
1494 * @context: The completion context.
1495 * @resp: The response that came from the host.
1496 * @resp_packet_size: The size in bytes of resp.
1497 *
1498 * This function is invoked on completion of a Query Resource
1499 * Requirements packet.
1500 */
1501static void q_resource_requirements(void *context, struct pci_response *resp,
1502 int resp_packet_size)
1503{
1504 struct q_res_req_compl *completion = context;
1505 struct pci_q_res_req_response *q_res_req =
1506 (struct pci_q_res_req_response *)resp;
1507 int i;
1508
1509 if (resp->status < 0) {
1510 dev_err(&completion->hpdev->hbus->hdev->device,
1511 "query resource requirements failed: %x\n",
1512 resp->status);
1513 } else {
1514 for (i = 0; i < 6; i++) {
1515 completion->hpdev->probed_bar[i] =
1516 q_res_req->probed_bar[i];
1517 }
1518 }
1519
1520 complete(&completion->host_event);
1521}
1522
Jake Oshins4daace02016-02-16 21:56:23 +00001523/**
1524 * new_pcichild_device() - Create a new child device
1525 * @hbus: The internal struct tracking this root PCI bus.
1526 * @desc: The information supplied so far from the host
1527 * about the device.
1528 *
1529 * This function creates the tracking structure for a new child
1530 * device and kicks off the process of figuring out what it is.
1531 *
1532 * Return: Pointer to the new tracking struct
1533 */
1534static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
1535 struct pci_function_description *desc)
1536{
1537 struct hv_pci_dev *hpdev;
1538 struct pci_child_message *res_req;
1539 struct q_res_req_compl comp_pkt;
Dexuan Cui8286e962016-11-10 07:17:48 +00001540 struct {
1541 struct pci_packet init_packet;
1542 u8 buffer[sizeof(struct pci_child_message)];
Jake Oshins4daace02016-02-16 21:56:23 +00001543 } pkt;
1544 unsigned long flags;
1545 int ret;
1546
1547 hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC);
1548 if (!hpdev)
1549 return NULL;
1550
1551 hpdev->hbus = hbus;
1552
1553 memset(&pkt, 0, sizeof(pkt));
1554 init_completion(&comp_pkt.host_event);
1555 comp_pkt.hpdev = hpdev;
1556 pkt.init_packet.compl_ctxt = &comp_pkt;
1557 pkt.init_packet.completion_func = q_resource_requirements;
1558 res_req = (struct pci_child_message *)&pkt.init_packet.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001559 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
Jake Oshins4daace02016-02-16 21:56:23 +00001560 res_req->wslot.slot = desc->win_slot.slot;
1561
1562 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
1563 sizeof(struct pci_child_message),
1564 (unsigned long)&pkt.init_packet,
1565 VM_PKT_DATA_INBAND,
1566 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1567 if (ret)
1568 goto error;
1569
Dexuan Cuic3635da2018-05-23 21:12:01 +00001570 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
1571 goto error;
Jake Oshins4daace02016-02-16 21:56:23 +00001572
1573 hpdev->desc = *desc;
Elena Reshetova24196f02017-04-18 09:02:48 -05001574 refcount_set(&hpdev->refs, 1);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001575 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001576 spin_lock_irqsave(&hbus->device_list_lock, flags);
Haiyang Zhang4a9b0932017-02-13 18:10:11 +00001577
Jake Oshins4daace02016-02-16 21:56:23 +00001578 list_add_tail(&hpdev->list_entry, &hbus->children);
1579 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1580 return hpdev;
1581
1582error:
1583 kfree(hpdev);
1584 return NULL;
1585}
1586
1587/**
1588 * get_pcichild_wslot() - Find device from slot
1589 * @hbus: Root PCI bus, as understood by this driver
1590 * @wslot: Location on the bus
1591 *
1592 * This function looks up a PCI device and returns the internal
1593 * representation of it. It acquires a reference on it, so that
1594 * the device won't be deleted while somebody is using it. The
1595 * caller is responsible for calling put_pcichild() to release
1596 * this reference.
1597 *
1598 * Return: Internal representation of a PCI device
1599 */
1600static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
1601 u32 wslot)
1602{
1603 unsigned long flags;
1604 struct hv_pci_dev *iter, *hpdev = NULL;
1605
1606 spin_lock_irqsave(&hbus->device_list_lock, flags);
1607 list_for_each_entry(iter, &hbus->children, list_entry) {
1608 if (iter->desc.win_slot.slot == wslot) {
1609 hpdev = iter;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001610 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001611 break;
1612 }
1613 }
1614 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1615
1616 return hpdev;
1617}
1618
1619/**
1620 * pci_devices_present_work() - Handle new list of child devices
1621 * @work: Work struct embedded in struct hv_dr_work
1622 *
1623 * "Bus Relations" is the Windows term for "children of this
1624 * bus." The terminology is preserved here for people trying to
1625 * debug the interaction between Hyper-V and Linux. This
1626 * function is called when the parent partition reports a list
1627 * of functions that should be observed under this PCI Express
1628 * port (bus).
1629 *
1630 * This function updates the list, and must tolerate being
1631 * called multiple times with the same information. The typical
1632 * number of child devices is one, with very atypical cases
1633 * involving three or four, so the algorithms used here can be
1634 * simple and inefficient.
1635 *
1636 * It must also treat the omission of a previously observed device as
1637 * notification that the device no longer exists.
1638 *
Dexuan Cui021ad272018-03-15 14:20:53 +00001639 * Note that this function is serialized with hv_eject_device_work(),
1640 * because both are pushed to the ordered workqueue hbus->wq.
Jake Oshins4daace02016-02-16 21:56:23 +00001641 */
1642static void pci_devices_present_work(struct work_struct *work)
1643{
1644 u32 child_no;
1645 bool found;
Jake Oshins4daace02016-02-16 21:56:23 +00001646 struct pci_function_description *new_desc;
1647 struct hv_pci_dev *hpdev;
1648 struct hv_pcibus_device *hbus;
1649 struct list_head removed;
1650 struct hv_dr_work *dr_wrk;
1651 struct hv_dr_state *dr = NULL;
1652 unsigned long flags;
1653
1654 dr_wrk = container_of(work, struct hv_dr_work, wrk);
1655 hbus = dr_wrk->bus;
1656 kfree(dr_wrk);
1657
1658 INIT_LIST_HEAD(&removed);
1659
Jake Oshins4daace02016-02-16 21:56:23 +00001660 /* Pull this off the queue and process it if it was the last one. */
1661 spin_lock_irqsave(&hbus->device_list_lock, flags);
1662 while (!list_empty(&hbus->dr_list)) {
1663 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
1664 list_entry);
1665 list_del(&dr->list_entry);
1666
1667 /* Throw this away if the list still has stuff in it. */
1668 if (!list_empty(&hbus->dr_list)) {
1669 kfree(dr);
1670 continue;
1671 }
1672 }
1673 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1674
1675 if (!dr) {
Jake Oshins4daace02016-02-16 21:56:23 +00001676 put_hvpcibus(hbus);
1677 return;
1678 }
1679
1680 /* First, mark all existing children as reported missing. */
1681 spin_lock_irqsave(&hbus->device_list_lock, flags);
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001682 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1683 hpdev->reported_missing = true;
Jake Oshins4daace02016-02-16 21:56:23 +00001684 }
1685 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1686
1687 /* Next, add back any reported devices. */
1688 for (child_no = 0; child_no < dr->device_count; child_no++) {
1689 found = false;
1690 new_desc = &dr->func[child_no];
1691
1692 spin_lock_irqsave(&hbus->device_list_lock, flags);
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001693 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1694 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
Jake Oshins4daace02016-02-16 21:56:23 +00001695 (hpdev->desc.v_id == new_desc->v_id) &&
1696 (hpdev->desc.d_id == new_desc->d_id) &&
1697 (hpdev->desc.ser == new_desc->ser)) {
1698 hpdev->reported_missing = false;
1699 found = true;
1700 }
1701 }
1702 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1703
1704 if (!found) {
1705 hpdev = new_pcichild_device(hbus, new_desc);
1706 if (!hpdev)
1707 dev_err(&hbus->hdev->device,
1708 "couldn't record a child device.\n");
1709 }
1710 }
1711
1712 /* Move missing children to a list on the stack. */
1713 spin_lock_irqsave(&hbus->device_list_lock, flags);
1714 do {
1715 found = false;
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001716 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Jake Oshins4daace02016-02-16 21:56:23 +00001717 if (hpdev->reported_missing) {
1718 found = true;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001719 put_pcichild(hpdev);
Wei Yongjun4f1cb012016-07-28 16:16:48 +00001720 list_move_tail(&hpdev->list_entry, &removed);
Jake Oshins4daace02016-02-16 21:56:23 +00001721 break;
1722 }
1723 }
1724 } while (found);
1725 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1726
1727 /* Delete everything that should no longer exist. */
1728 while (!list_empty(&removed)) {
1729 hpdev = list_first_entry(&removed, struct hv_pci_dev,
1730 list_entry);
1731 list_del(&hpdev->list_entry);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001732 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001733 }
1734
Jork Loeser691ac1d2017-05-24 13:41:24 -07001735 switch (hbus->state) {
Long Lid3a78d82017-03-23 14:58:10 -07001736 case hv_pcibus_installed:
1737 /*
Jork Loeser691ac1d2017-05-24 13:41:24 -07001738 * Tell the core to rescan bus
1739 * because there may have been changes.
1740 */
Jake Oshins4daace02016-02-16 21:56:23 +00001741 pci_lock_rescan_remove();
1742 pci_scan_child_bus(hbus->pci_bus);
1743 pci_unlock_rescan_remove();
Long Lid3a78d82017-03-23 14:58:10 -07001744 break;
1745
1746 case hv_pcibus_init:
1747 case hv_pcibus_probed:
Jake Oshins4daace02016-02-16 21:56:23 +00001748 survey_child_resources(hbus);
Long Lid3a78d82017-03-23 14:58:10 -07001749 break;
1750
1751 default:
1752 break;
Jake Oshins4daace02016-02-16 21:56:23 +00001753 }
1754
Jake Oshins4daace02016-02-16 21:56:23 +00001755 put_hvpcibus(hbus);
1756 kfree(dr);
1757}
1758
1759/**
1760 * hv_pci_devices_present() - Handles list of new children
1761 * @hbus: Root PCI bus, as understood by this driver
1762 * @relations: Packet from host listing children
1763 *
1764 * This function is invoked whenever a new list of devices for
1765 * this bus appears.
1766 */
1767static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
1768 struct pci_bus_relations *relations)
1769{
1770 struct hv_dr_state *dr;
1771 struct hv_dr_work *dr_wrk;
1772 unsigned long flags;
Dexuan Cui948373b2018-03-15 14:22:00 +00001773 bool pending_dr;
Jake Oshins4daace02016-02-16 21:56:23 +00001774
1775 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
1776 if (!dr_wrk)
1777 return;
1778
1779 dr = kzalloc(offsetof(struct hv_dr_state, func) +
1780 (sizeof(struct pci_function_description) *
1781 (relations->device_count)), GFP_NOWAIT);
1782 if (!dr) {
1783 kfree(dr_wrk);
1784 return;
1785 }
1786
1787 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
1788 dr_wrk->bus = hbus;
1789 dr->device_count = relations->device_count;
1790 if (dr->device_count != 0) {
1791 memcpy(dr->func, relations->func,
1792 sizeof(struct pci_function_description) *
1793 dr->device_count);
1794 }
1795
1796 spin_lock_irqsave(&hbus->device_list_lock, flags);
Dexuan Cui948373b2018-03-15 14:22:00 +00001797 /*
1798 * If pending_dr is true, we have already queued a work,
1799 * which will see the new dr. Otherwise, we need to
1800 * queue a new work.
1801 */
1802 pending_dr = !list_empty(&hbus->dr_list);
Jake Oshins4daace02016-02-16 21:56:23 +00001803 list_add_tail(&dr->list_entry, &hbus->dr_list);
1804 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1805
Dexuan Cui948373b2018-03-15 14:22:00 +00001806 if (pending_dr) {
1807 kfree(dr_wrk);
1808 } else {
1809 get_hvpcibus(hbus);
1810 queue_work(hbus->wq, &dr_wrk->wrk);
1811 }
Jake Oshins4daace02016-02-16 21:56:23 +00001812}
1813
1814/**
1815 * hv_eject_device_work() - Asynchronously handles ejection
1816 * @work: Work struct embedded in internal device struct
1817 *
1818 * This function handles ejecting a device. Windows will
1819 * attempt to gracefully eject a device, waiting 60 seconds to
1820 * hear back from the guest OS that this completed successfully.
1821 * If this timer expires, the device will be forcibly removed.
1822 */
1823static void hv_eject_device_work(struct work_struct *work)
1824{
1825 struct pci_eject_response *ejct_pkt;
1826 struct hv_pci_dev *hpdev;
1827 struct pci_dev *pdev;
1828 unsigned long flags;
1829 int wslot;
1830 struct {
1831 struct pci_packet pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001832 u8 buffer[sizeof(struct pci_eject_response)];
Jake Oshins4daace02016-02-16 21:56:23 +00001833 } ctxt;
1834
1835 hpdev = container_of(work, struct hv_pci_dev, wrk);
1836
Dexuan Cuifca288c2018-03-15 14:21:43 +00001837 WARN_ON(hpdev->state != hv_pcichild_ejecting);
Jake Oshins4daace02016-02-16 21:56:23 +00001838
1839 /*
1840 * Ejection can come before or after the PCI bus has been set up, so
1841 * attempt to find it and tear down the bus state, if it exists. This
1842 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
1843 * because hbus->pci_bus may not exist yet.
1844 */
1845 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
1846 pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0,
1847 wslot);
1848 if (pdev) {
Long Li414428c2017-03-23 14:58:32 -07001849 pci_lock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001850 pci_stop_and_remove_bus_device(pdev);
1851 pci_dev_put(pdev);
Long Li414428c2017-03-23 14:58:32 -07001852 pci_unlock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001853 }
1854
Dexuan Cuie74d2eb2016-11-10 07:19:52 +00001855 spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
1856 list_del(&hpdev->list_entry);
1857 spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
1858
Jake Oshins4daace02016-02-16 21:56:23 +00001859 memset(&ctxt, 0, sizeof(ctxt));
1860 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001861 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
Jake Oshins4daace02016-02-16 21:56:23 +00001862 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1863 vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
1864 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
1865 VM_PKT_DATA_INBAND, 0);
1866
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001867 put_pcichild(hpdev);
1868 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001869 put_hvpcibus(hpdev->hbus);
1870}
1871
1872/**
1873 * hv_pci_eject_device() - Handles device ejection
1874 * @hpdev: Internal device tracking struct
1875 *
1876 * This function is invoked when an ejection packet arrives. It
1877 * just schedules work so that we don't re-enter the packet
1878 * delivery code handling the ejection.
1879 */
1880static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
1881{
1882 hpdev->state = hv_pcichild_ejecting;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001883 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001884 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
1885 get_hvpcibus(hpdev->hbus);
Dexuan Cui021ad272018-03-15 14:20:53 +00001886 queue_work(hpdev->hbus->wq, &hpdev->wrk);
Jake Oshins4daace02016-02-16 21:56:23 +00001887}
1888
1889/**
1890 * hv_pci_onchannelcallback() - Handles incoming packets
1891 * @context: Internal bus tracking struct
1892 *
1893 * This function is invoked whenever the host sends a packet to
1894 * this channel (which is private to this root PCI bus).
1895 */
1896static void hv_pci_onchannelcallback(void *context)
1897{
1898 const int packet_size = 0x100;
1899 int ret;
1900 struct hv_pcibus_device *hbus = context;
1901 u32 bytes_recvd;
1902 u64 req_id;
1903 struct vmpacket_descriptor *desc;
1904 unsigned char *buffer;
1905 int bufferlen = packet_size;
1906 struct pci_packet *comp_packet;
1907 struct pci_response *response;
1908 struct pci_incoming_message *new_message;
1909 struct pci_bus_relations *bus_rel;
1910 struct pci_dev_incoming *dev_message;
1911 struct hv_pci_dev *hpdev;
1912
1913 buffer = kmalloc(bufferlen, GFP_ATOMIC);
1914 if (!buffer)
1915 return;
1916
1917 while (1) {
1918 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
1919 bufferlen, &bytes_recvd, &req_id);
1920
1921 if (ret == -ENOBUFS) {
1922 kfree(buffer);
1923 /* Handle large packet */
1924 bufferlen = bytes_recvd;
1925 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1926 if (!buffer)
1927 return;
1928 continue;
1929 }
1930
Vitaly Kuznetsov837d7412016-06-17 12:45:30 -05001931 /* Zero length indicates there are no more packets. */
1932 if (ret || !bytes_recvd)
1933 break;
1934
Jake Oshins4daace02016-02-16 21:56:23 +00001935 /*
1936 * All incoming packets must be at least as large as a
1937 * response.
1938 */
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02001939 if (bytes_recvd <= sizeof(struct pci_response))
Vitaly Kuznetsov837d7412016-06-17 12:45:30 -05001940 continue;
Jake Oshins4daace02016-02-16 21:56:23 +00001941 desc = (struct vmpacket_descriptor *)buffer;
1942
1943 switch (desc->type) {
1944 case VM_PKT_COMP:
1945
1946 /*
1947 * The host is trusted, and thus it's safe to interpret
1948 * this transaction ID as a pointer.
1949 */
1950 comp_packet = (struct pci_packet *)req_id;
1951 response = (struct pci_response *)buffer;
1952 comp_packet->completion_func(comp_packet->compl_ctxt,
1953 response,
1954 bytes_recvd);
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02001955 break;
Jake Oshins4daace02016-02-16 21:56:23 +00001956
1957 case VM_PKT_DATA_INBAND:
1958
1959 new_message = (struct pci_incoming_message *)buffer;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001960 switch (new_message->message_type.type) {
Jake Oshins4daace02016-02-16 21:56:23 +00001961 case PCI_BUS_RELATIONS:
1962
1963 bus_rel = (struct pci_bus_relations *)buffer;
1964 if (bytes_recvd <
1965 offsetof(struct pci_bus_relations, func) +
1966 (sizeof(struct pci_function_description) *
1967 (bus_rel->device_count))) {
1968 dev_err(&hbus->hdev->device,
1969 "bus relations too small\n");
1970 break;
1971 }
1972
1973 hv_pci_devices_present(hbus, bus_rel);
1974 break;
1975
1976 case PCI_EJECT:
1977
1978 dev_message = (struct pci_dev_incoming *)buffer;
1979 hpdev = get_pcichild_wslot(hbus,
1980 dev_message->wslot.slot);
1981 if (hpdev) {
1982 hv_pci_eject_device(hpdev);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001983 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001984 }
1985 break;
1986
1987 default:
1988 dev_warn(&hbus->hdev->device,
1989 "Unimplemented protocol message %x\n",
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001990 new_message->message_type.type);
Jake Oshins4daace02016-02-16 21:56:23 +00001991 break;
1992 }
1993 break;
1994
1995 default:
1996 dev_err(&hbus->hdev->device,
1997 "unhandled packet type %d, tid %llx len %d\n",
1998 desc->type, req_id, bytes_recvd);
1999 break;
2000 }
Jake Oshins4daace02016-02-16 21:56:23 +00002001 }
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02002002
2003 kfree(buffer);
Jake Oshins4daace02016-02-16 21:56:23 +00002004}
2005
2006/**
2007 * hv_pci_protocol_negotiation() - Set up protocol
2008 * @hdev: VMBus's tracking struct for this root PCI bus
2009 *
2010 * This driver is intended to support running on Windows 10
2011 * (server) and later versions. It will not run on earlier
2012 * versions, as they assume that many of the operations which
2013 * Linux needs accomplished with a spinlock held were done via
2014 * asynchronous messaging via VMBus. Windows 10 increases the
2015 * surface area of PCI emulation so that these actions can take
2016 * place by suspending a virtual processor for their duration.
2017 *
2018 * This function negotiates the channel protocol version,
2019 * failing if the host doesn't support the necessary protocol
2020 * level.
2021 */
2022static int hv_pci_protocol_negotiation(struct hv_device *hdev)
2023{
2024 struct pci_version_request *version_req;
2025 struct hv_pci_compl comp_pkt;
2026 struct pci_packet *pkt;
2027 int ret;
Jork Loeserb1db7e72017-05-24 13:41:27 -07002028 int i;
Jake Oshins4daace02016-02-16 21:56:23 +00002029
2030 /*
2031 * Initiate the handshake with the host and negotiate
2032 * a version that the host can support. We start with the
2033 * highest version number and go down if the host cannot
2034 * support it.
2035 */
2036 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2037 if (!pkt)
2038 return -ENOMEM;
2039
2040 init_completion(&comp_pkt.host_event);
2041 pkt->completion_func = hv_pci_generic_compl;
2042 pkt->compl_ctxt = &comp_pkt;
2043 version_req = (struct pci_version_request *)&pkt->message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002044 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
Jake Oshins4daace02016-02-16 21:56:23 +00002045
Jork Loeserb1db7e72017-05-24 13:41:27 -07002046 for (i = 0; i < ARRAY_SIZE(pci_protocol_versions); i++) {
2047 version_req->protocol_version = pci_protocol_versions[i];
2048 ret = vmbus_sendpacket(hdev->channel, version_req,
2049 sizeof(struct pci_version_request),
2050 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2051 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002052 if (!ret)
2053 ret = wait_for_response(hdev, &comp_pkt.host_event);
2054
Jork Loeserb1db7e72017-05-24 13:41:27 -07002055 if (ret) {
2056 dev_err(&hdev->device,
Dexuan Cuic3635da2018-05-23 21:12:01 +00002057 "PCI Pass-through VSP failed to request version: %d",
Jork Loeserb1db7e72017-05-24 13:41:27 -07002058 ret);
2059 goto exit;
2060 }
Jake Oshins4daace02016-02-16 21:56:23 +00002061
Jork Loeserb1db7e72017-05-24 13:41:27 -07002062 if (comp_pkt.completion_status >= 0) {
2063 pci_protocol_version = pci_protocol_versions[i];
2064 dev_info(&hdev->device,
2065 "PCI VMBus probing: Using version %#x\n",
2066 pci_protocol_version);
2067 goto exit;
2068 }
2069
2070 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2071 dev_err(&hdev->device,
2072 "PCI Pass-through VSP failed version request: %#x",
2073 comp_pkt.completion_status);
2074 ret = -EPROTO;
2075 goto exit;
2076 }
2077
2078 reinit_completion(&comp_pkt.host_event);
Jake Oshins4daace02016-02-16 21:56:23 +00002079 }
2080
Jork Loeserb1db7e72017-05-24 13:41:27 -07002081 dev_err(&hdev->device,
2082 "PCI pass-through VSP failed to find supported version");
2083 ret = -EPROTO;
Jake Oshins4daace02016-02-16 21:56:23 +00002084
2085exit:
2086 kfree(pkt);
2087 return ret;
2088}
2089
2090/**
2091 * hv_pci_free_bridge_windows() - Release memory regions for the
2092 * bus
2093 * @hbus: Root PCI bus, as understood by this driver
2094 */
2095static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2096{
2097 /*
2098 * Set the resources back to the way they looked when they
2099 * were allocated by setting IORESOURCE_BUSY again.
2100 */
2101
2102 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2103 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
Jake Oshins696ca5e2016-04-05 10:22:52 -07002104 vmbus_free_mmio(hbus->low_mmio_res->start,
2105 resource_size(hbus->low_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002106 }
2107
2108 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2109 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
Jake Oshins696ca5e2016-04-05 10:22:52 -07002110 vmbus_free_mmio(hbus->high_mmio_res->start,
2111 resource_size(hbus->high_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002112 }
2113}
2114
2115/**
2116 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2117 * for the bus
2118 * @hbus: Root PCI bus, as understood by this driver
2119 *
2120 * This function calls vmbus_allocate_mmio(), which is itself a
2121 * bit of a compromise. Ideally, we might change the pnp layer
2122 * in the kernel such that it comprehends either PCI devices
2123 * which are "grandchildren of ACPI," with some intermediate bus
2124 * node (in this case, VMBus) or change it such that it
2125 * understands VMBus. The pnp layer, however, has been declared
2126 * deprecated, and not subject to change.
2127 *
2128 * The workaround, implemented here, is to ask VMBus to allocate
2129 * MMIO space for this bus. VMBus itself knows which ranges are
2130 * appropriate by looking at its own ACPI objects. Then, after
2131 * these ranges are claimed, they're modified to look like they
2132 * would have looked if the ACPI and pnp code had allocated
2133 * bridge windows. These descriptors have to exist in this form
2134 * in order to satisfy the code which will get invoked when the
2135 * endpoint PCI function driver calls request_mem_region() or
2136 * request_mem_region_exclusive().
2137 *
2138 * Return: 0 on success, -errno on failure
2139 */
2140static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2141{
2142 resource_size_t align;
2143 int ret;
2144
2145 if (hbus->low_mmio_space) {
2146 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2147 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2148 (u64)(u32)0xffffffff,
2149 hbus->low_mmio_space,
2150 align, false);
2151 if (ret) {
2152 dev_err(&hbus->hdev->device,
2153 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2154 hbus->low_mmio_space);
2155 return ret;
2156 }
2157
2158 /* Modify this resource to become a bridge window. */
2159 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2160 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
2161 pci_add_resource(&hbus->resources_for_children,
2162 hbus->low_mmio_res);
2163 }
2164
2165 if (hbus->high_mmio_space) {
2166 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2167 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2168 0x100000000, -1,
2169 hbus->high_mmio_space, align,
2170 false);
2171 if (ret) {
2172 dev_err(&hbus->hdev->device,
2173 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2174 hbus->high_mmio_space);
2175 goto release_low_mmio;
2176 }
2177
2178 /* Modify this resource to become a bridge window. */
2179 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
2180 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
2181 pci_add_resource(&hbus->resources_for_children,
2182 hbus->high_mmio_res);
2183 }
2184
2185 return 0;
2186
2187release_low_mmio:
2188 if (hbus->low_mmio_res) {
Jake Oshins696ca5e2016-04-05 10:22:52 -07002189 vmbus_free_mmio(hbus->low_mmio_res->start,
2190 resource_size(hbus->low_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002191 }
2192
2193 return ret;
2194}
2195
2196/**
2197 * hv_allocate_config_window() - Find MMIO space for PCI Config
2198 * @hbus: Root PCI bus, as understood by this driver
2199 *
2200 * This function claims memory-mapped I/O space for accessing
2201 * configuration space for the functions on this bus.
2202 *
2203 * Return: 0 on success, -errno on failure
2204 */
2205static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
2206{
2207 int ret;
2208
2209 /*
2210 * Set up a region of MMIO space to use for accessing configuration
2211 * space.
2212 */
2213 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
2214 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
2215 if (ret)
2216 return ret;
2217
2218 /*
2219 * vmbus_allocate_mmio() gets used for allocating both device endpoint
2220 * resource claims (those which cannot be overlapped) and the ranges
2221 * which are valid for the children of this bus, which are intended
2222 * to be overlapped by those children. Set the flag on this claim
2223 * meaning that this region can't be overlapped.
2224 */
2225
2226 hbus->mem_config->flags |= IORESOURCE_BUSY;
2227
2228 return 0;
2229}
2230
2231static void hv_free_config_window(struct hv_pcibus_device *hbus)
2232{
Jake Oshins696ca5e2016-04-05 10:22:52 -07002233 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
Jake Oshins4daace02016-02-16 21:56:23 +00002234}
2235
2236/**
2237 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
2238 * @hdev: VMBus's tracking struct for this root PCI bus
2239 *
2240 * Return: 0 on success, -errno on failure
2241 */
2242static int hv_pci_enter_d0(struct hv_device *hdev)
2243{
2244 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2245 struct pci_bus_d0_entry *d0_entry;
2246 struct hv_pci_compl comp_pkt;
2247 struct pci_packet *pkt;
2248 int ret;
2249
2250 /*
2251 * Tell the host that the bus is ready to use, and moved into the
2252 * powered-on state. This includes telling the host which region
2253 * of memory-mapped I/O space has been chosen for configuration space
2254 * access.
2255 */
2256 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
2257 if (!pkt)
2258 return -ENOMEM;
2259
2260 init_completion(&comp_pkt.host_event);
2261 pkt->completion_func = hv_pci_generic_compl;
2262 pkt->compl_ctxt = &comp_pkt;
2263 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002264 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
Jake Oshins4daace02016-02-16 21:56:23 +00002265 d0_entry->mmio_base = hbus->mem_config->start;
2266
2267 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
2268 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2269 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002270 if (!ret)
2271 ret = wait_for_response(hdev, &comp_pkt.host_event);
2272
Jake Oshins4daace02016-02-16 21:56:23 +00002273 if (ret)
2274 goto exit;
2275
Jake Oshins4daace02016-02-16 21:56:23 +00002276 if (comp_pkt.completion_status < 0) {
2277 dev_err(&hdev->device,
2278 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2279 comp_pkt.completion_status);
2280 ret = -EPROTO;
2281 goto exit;
2282 }
2283
2284 ret = 0;
2285
2286exit:
2287 kfree(pkt);
2288 return ret;
2289}
2290
2291/**
2292 * hv_pci_query_relations() - Ask host to send list of child
2293 * devices
2294 * @hdev: VMBus's tracking struct for this root PCI bus
2295 *
2296 * Return: 0 on success, -errno on failure
2297 */
2298static int hv_pci_query_relations(struct hv_device *hdev)
2299{
2300 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2301 struct pci_message message;
2302 struct completion comp;
2303 int ret;
2304
2305 /* Ask the host to send along the list of child devices */
2306 init_completion(&comp);
2307 if (cmpxchg(&hbus->survey_event, NULL, &comp))
2308 return -ENOTEMPTY;
2309
2310 memset(&message, 0, sizeof(message));
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002311 message.type = PCI_QUERY_BUS_RELATIONS;
Jake Oshins4daace02016-02-16 21:56:23 +00002312
2313 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
2314 0, VM_PKT_DATA_INBAND, 0);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002315 if (!ret)
2316 ret = wait_for_response(hdev, &comp);
Jake Oshins4daace02016-02-16 21:56:23 +00002317
Dexuan Cuic3635da2018-05-23 21:12:01 +00002318 return ret;
Jake Oshins4daace02016-02-16 21:56:23 +00002319}
2320
2321/**
2322 * hv_send_resources_allocated() - Report local resource choices
2323 * @hdev: VMBus's tracking struct for this root PCI bus
2324 *
2325 * The host OS is expecting to be sent a request as a message
2326 * which contains all the resources that the device will use.
2327 * The response contains those same resources, "translated"
2328 * which is to say, the values which should be used by the
2329 * hardware, when it delivers an interrupt. (MMIO resources are
2330 * used in local terms.) This is nice for Windows, and lines up
2331 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2332 * is deeply expecting to scan an emulated PCI configuration
2333 * space. So this message is sent here only to drive the state
2334 * machine on the host forward.
2335 *
2336 * Return: 0 on success, -errno on failure
2337 */
2338static int hv_send_resources_allocated(struct hv_device *hdev)
2339{
2340 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2341 struct pci_resources_assigned *res_assigned;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002342 struct pci_resources_assigned2 *res_assigned2;
Jake Oshins4daace02016-02-16 21:56:23 +00002343 struct hv_pci_compl comp_pkt;
2344 struct hv_pci_dev *hpdev;
2345 struct pci_packet *pkt;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002346 size_t size_res;
Jake Oshins4daace02016-02-16 21:56:23 +00002347 u32 wslot;
2348 int ret;
2349
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002350 size_res = (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2)
2351 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
2352
2353 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00002354 if (!pkt)
2355 return -ENOMEM;
2356
2357 ret = 0;
2358
2359 for (wslot = 0; wslot < 256; wslot++) {
2360 hpdev = get_pcichild_wslot(hbus, wslot);
2361 if (!hpdev)
2362 continue;
2363
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002364 memset(pkt, 0, sizeof(*pkt) + size_res);
Jake Oshins4daace02016-02-16 21:56:23 +00002365 init_completion(&comp_pkt.host_event);
2366 pkt->completion_func = hv_pci_generic_compl;
2367 pkt->compl_ctxt = &comp_pkt;
Jake Oshins4daace02016-02-16 21:56:23 +00002368
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002369 if (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2) {
2370 res_assigned =
2371 (struct pci_resources_assigned *)&pkt->message;
2372 res_assigned->message_type.type =
2373 PCI_RESOURCES_ASSIGNED;
2374 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
2375 } else {
2376 res_assigned2 =
2377 (struct pci_resources_assigned2 *)&pkt->message;
2378 res_assigned2->message_type.type =
2379 PCI_RESOURCES_ASSIGNED2;
2380 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
2381 }
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002382 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002383
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002384 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
2385 size_res, (unsigned long)pkt,
2386 VM_PKT_DATA_INBAND,
2387 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002388 if (!ret)
2389 ret = wait_for_response(hdev, &comp_pkt.host_event);
Jake Oshins4daace02016-02-16 21:56:23 +00002390 if (ret)
2391 break;
2392
Jake Oshins4daace02016-02-16 21:56:23 +00002393 if (comp_pkt.completion_status < 0) {
2394 ret = -EPROTO;
2395 dev_err(&hdev->device,
2396 "resource allocated returned 0x%x",
2397 comp_pkt.completion_status);
2398 break;
2399 }
2400 }
2401
2402 kfree(pkt);
2403 return ret;
2404}
2405
2406/**
2407 * hv_send_resources_released() - Report local resources
2408 * released
2409 * @hdev: VMBus's tracking struct for this root PCI bus
2410 *
2411 * Return: 0 on success, -errno on failure
2412 */
2413static int hv_send_resources_released(struct hv_device *hdev)
2414{
2415 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2416 struct pci_child_message pkt;
2417 struct hv_pci_dev *hpdev;
2418 u32 wslot;
2419 int ret;
2420
2421 for (wslot = 0; wslot < 256; wslot++) {
2422 hpdev = get_pcichild_wslot(hbus, wslot);
2423 if (!hpdev)
2424 continue;
2425
2426 memset(&pkt, 0, sizeof(pkt));
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002427 pkt.message_type.type = PCI_RESOURCES_RELEASED;
Jake Oshins4daace02016-02-16 21:56:23 +00002428 pkt.wslot.slot = hpdev->desc.win_slot.slot;
2429
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002430 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002431
2432 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
2433 VM_PKT_DATA_INBAND, 0);
2434 if (ret)
2435 return ret;
2436 }
2437
2438 return 0;
2439}
2440
2441static void get_hvpcibus(struct hv_pcibus_device *hbus)
2442{
Stephen Hemminger6708be92018-05-23 10:11:13 -07002443 refcount_inc(&hbus->remove_lock);
Jake Oshins4daace02016-02-16 21:56:23 +00002444}
2445
2446static void put_hvpcibus(struct hv_pcibus_device *hbus)
2447{
Stephen Hemminger6708be92018-05-23 10:11:13 -07002448 if (refcount_dec_and_test(&hbus->remove_lock))
Jake Oshins4daace02016-02-16 21:56:23 +00002449 complete(&hbus->remove_event);
2450}
2451
2452/**
2453 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
2454 * @hdev: VMBus's tracking struct for this root PCI bus
2455 * @dev_id: Identifies the device itself
2456 *
2457 * Return: 0 on success, -errno on failure
2458 */
2459static int hv_pci_probe(struct hv_device *hdev,
2460 const struct hv_vmbus_device_id *dev_id)
2461{
2462 struct hv_pcibus_device *hbus;
2463 int ret;
2464
Jork Loeserbe66b672017-05-24 13:41:25 -07002465 /*
2466 * hv_pcibus_device contains the hypercall arguments for retargeting in
2467 * hv_irq_unmask(). Those must not cross a page boundary.
2468 */
2469 BUILD_BUG_ON(sizeof(*hbus) > PAGE_SIZE);
2470
2471 hbus = (struct hv_pcibus_device *)get_zeroed_page(GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00002472 if (!hbus)
2473 return -ENOMEM;
Long Lid3a78d82017-03-23 14:58:10 -07002474 hbus->state = hv_pcibus_init;
Jake Oshins4daace02016-02-16 21:56:23 +00002475
2476 /*
2477 * The PCI bus "domain" is what is called "segment" in ACPI and
2478 * other specs. Pull it from the instance ID, to get something
2479 * unique. Bytes 8 and 9 are what is used in Windows guests, so
2480 * do the same thing for consistency. Note that, since this code
2481 * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
2482 * that (1) the only domain in use for something that looks like
2483 * a physical PCI bus (which is actually emulated by the
2484 * hypervisor) is domain 0 and (2) there will be no overlap
2485 * between domains derived from these instance IDs in the same
2486 * VM.
2487 */
2488 hbus->sysdata.domain = hdev->dev_instance.b[9] |
2489 hdev->dev_instance.b[8] << 8;
2490
2491 hbus->hdev = hdev;
Stephen Hemminger6708be92018-05-23 10:11:13 -07002492 refcount_set(&hbus->remove_lock, 1);
Jake Oshins4daace02016-02-16 21:56:23 +00002493 INIT_LIST_HEAD(&hbus->children);
2494 INIT_LIST_HEAD(&hbus->dr_list);
2495 INIT_LIST_HEAD(&hbus->resources_for_children);
2496 spin_lock_init(&hbus->config_lock);
2497 spin_lock_init(&hbus->device_list_lock);
Long Li0de8ce32016-11-08 14:04:38 -08002498 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
Jake Oshins4daace02016-02-16 21:56:23 +00002499 init_completion(&hbus->remove_event);
Dexuan Cui021ad272018-03-15 14:20:53 +00002500 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
2501 hbus->sysdata.domain);
2502 if (!hbus->wq) {
2503 ret = -ENOMEM;
2504 goto free_bus;
2505 }
Jake Oshins4daace02016-02-16 21:56:23 +00002506
2507 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
2508 hv_pci_onchannelcallback, hbus);
2509 if (ret)
Dexuan Cui021ad272018-03-15 14:20:53 +00002510 goto destroy_wq;
Jake Oshins4daace02016-02-16 21:56:23 +00002511
2512 hv_set_drvdata(hdev, hbus);
2513
2514 ret = hv_pci_protocol_negotiation(hdev);
2515 if (ret)
2516 goto close;
2517
2518 ret = hv_allocate_config_window(hbus);
2519 if (ret)
2520 goto close;
2521
2522 hbus->cfg_addr = ioremap(hbus->mem_config->start,
2523 PCI_CONFIG_MMIO_LENGTH);
2524 if (!hbus->cfg_addr) {
2525 dev_err(&hdev->device,
2526 "Unable to map a virtual address for config space\n");
2527 ret = -ENOMEM;
2528 goto free_config;
2529 }
2530
2531 hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
2532 if (!hbus->sysdata.fwnode) {
2533 ret = -ENOMEM;
2534 goto unmap;
2535 }
2536
2537 ret = hv_pcie_init_irq_domain(hbus);
2538 if (ret)
2539 goto free_fwnode;
2540
2541 ret = hv_pci_query_relations(hdev);
2542 if (ret)
2543 goto free_irq_domain;
2544
2545 ret = hv_pci_enter_d0(hdev);
2546 if (ret)
2547 goto free_irq_domain;
2548
2549 ret = hv_pci_allocate_bridge_windows(hbus);
2550 if (ret)
2551 goto free_irq_domain;
2552
2553 ret = hv_send_resources_allocated(hdev);
2554 if (ret)
2555 goto free_windows;
2556
2557 prepopulate_bars(hbus);
2558
2559 hbus->state = hv_pcibus_probed;
2560
2561 ret = create_root_hv_pci_bus(hbus);
2562 if (ret)
2563 goto free_windows;
2564
2565 return 0;
2566
2567free_windows:
2568 hv_pci_free_bridge_windows(hbus);
2569free_irq_domain:
2570 irq_domain_remove(hbus->irq_domain);
2571free_fwnode:
2572 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2573unmap:
2574 iounmap(hbus->cfg_addr);
2575free_config:
2576 hv_free_config_window(hbus);
2577close:
2578 vmbus_close(hdev->channel);
Dexuan Cui021ad272018-03-15 14:20:53 +00002579destroy_wq:
2580 destroy_workqueue(hbus->wq);
Jake Oshins4daace02016-02-16 21:56:23 +00002581free_bus:
Jork Loeserbe66b672017-05-24 13:41:25 -07002582 free_page((unsigned long)hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00002583 return ret;
2584}
2585
Dexuan Cui179785242016-11-10 07:18:47 +00002586static void hv_pci_bus_exit(struct hv_device *hdev)
Jake Oshins4daace02016-02-16 21:56:23 +00002587{
Dexuan Cui179785242016-11-10 07:18:47 +00002588 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2589 struct {
Jake Oshins4daace02016-02-16 21:56:23 +00002590 struct pci_packet teardown_packet;
Dexuan Cui179785242016-11-10 07:18:47 +00002591 u8 buffer[sizeof(struct pci_message)];
Jake Oshins4daace02016-02-16 21:56:23 +00002592 } pkt;
2593 struct pci_bus_relations relations;
2594 struct hv_pci_compl comp_pkt;
Dexuan Cui179785242016-11-10 07:18:47 +00002595 int ret;
Jake Oshins4daace02016-02-16 21:56:23 +00002596
Dexuan Cui179785242016-11-10 07:18:47 +00002597 /*
2598 * After the host sends the RESCIND_CHANNEL message, it doesn't
2599 * access the per-channel ringbuffer any longer.
2600 */
2601 if (hdev->channel->rescind)
2602 return;
2603
2604 /* Delete any children which might still exist. */
2605 memset(&relations, 0, sizeof(relations));
2606 hv_pci_devices_present(hbus, &relations);
2607
2608 ret = hv_send_resources_released(hdev);
2609 if (ret)
2610 dev_err(&hdev->device,
2611 "Couldn't send resources released packet(s)\n");
Jake Oshins4daace02016-02-16 21:56:23 +00002612
Jake Oshins4daace02016-02-16 21:56:23 +00002613 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
2614 init_completion(&comp_pkt.host_event);
2615 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
2616 pkt.teardown_packet.compl_ctxt = &comp_pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002617 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
Jake Oshins4daace02016-02-16 21:56:23 +00002618
2619 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
2620 sizeof(struct pci_message),
2621 (unsigned long)&pkt.teardown_packet,
2622 VM_PKT_DATA_INBAND,
2623 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2624 if (!ret)
2625 wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
Dexuan Cui179785242016-11-10 07:18:47 +00002626}
Jake Oshins4daace02016-02-16 21:56:23 +00002627
Dexuan Cui179785242016-11-10 07:18:47 +00002628/**
2629 * hv_pci_remove() - Remove routine for this VMBus channel
2630 * @hdev: VMBus's tracking struct for this root PCI bus
2631 *
2632 * Return: 0 on success, -errno on failure
2633 */
2634static int hv_pci_remove(struct hv_device *hdev)
2635{
2636 struct hv_pcibus_device *hbus;
2637
2638 hbus = hv_get_drvdata(hdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002639 if (hbus->state == hv_pcibus_installed) {
2640 /* Remove the bus from PCI's point of view. */
2641 pci_lock_rescan_remove();
2642 pci_stop_root_bus(hbus->pci_bus);
2643 pci_remove_root_bus(hbus->pci_bus);
2644 pci_unlock_rescan_remove();
Long Lid3a78d82017-03-23 14:58:10 -07002645 hbus->state = hv_pcibus_removed;
Jake Oshins4daace02016-02-16 21:56:23 +00002646 }
2647
Dexuan Cui179785242016-11-10 07:18:47 +00002648 hv_pci_bus_exit(hdev);
Vitaly Kuznetsovdeb22e52016-04-29 11:39:10 +02002649
Jake Oshins4daace02016-02-16 21:56:23 +00002650 vmbus_close(hdev->channel);
2651
Jake Oshins4daace02016-02-16 21:56:23 +00002652 iounmap(hbus->cfg_addr);
2653 hv_free_config_window(hbus);
2654 pci_free_resource_list(&hbus->resources_for_children);
2655 hv_pci_free_bridge_windows(hbus);
2656 irq_domain_remove(hbus->irq_domain);
2657 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2658 put_hvpcibus(hbus);
2659 wait_for_completion(&hbus->remove_event);
Dexuan Cui021ad272018-03-15 14:20:53 +00002660 destroy_workqueue(hbus->wq);
Jork Loeserbe66b672017-05-24 13:41:25 -07002661 free_page((unsigned long)hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00002662 return 0;
2663}
2664
2665static const struct hv_vmbus_device_id hv_pci_id_table[] = {
2666 /* PCI Pass-through Class ID */
2667 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
2668 { HV_PCIE_GUID, },
2669 { },
2670};
2671
2672MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
2673
2674static struct hv_driver hv_pci_drv = {
2675 .name = "hv_pci",
2676 .id_table = hv_pci_id_table,
2677 .probe = hv_pci_probe,
2678 .remove = hv_pci_remove,
2679};
2680
2681static void __exit exit_hv_pci_drv(void)
2682{
2683 vmbus_driver_unregister(&hv_pci_drv);
2684}
2685
2686static int __init init_hv_pci_drv(void)
2687{
2688 return vmbus_driver_register(&hv_pci_drv);
2689}
2690
2691module_init(init_hv_pci_drv);
2692module_exit(exit_hv_pci_drv);
2693
2694MODULE_DESCRIPTION("Hyper-V PCI");
2695MODULE_LICENSE("GPL v2");