blob: 95441a35ecebbcdee6e12ffe3be17cff963dcb3f [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
Stephen Hemmingera15f2c082018-09-14 12:54:56 -070092/* space for 32bit serial number as string */
93#define SLOT_NAME_SIZE 11
94
Jake Oshins4daace02016-02-16 21:56:23 +000095/*
96 * Message Types
97 */
98
99enum pci_message_type {
100 /*
101 * Version 1.1
102 */
103 PCI_MESSAGE_BASE = 0x42490000,
104 PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
105 PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
106 PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
107 PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
108 PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
109 PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
110 PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
111 PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
112 PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
113 PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
114 PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
115 PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
116 PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
117 PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
118 PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
119 PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
120 PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
121 PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
122 PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
123 PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700124 PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
125 PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
126 PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
Jake Oshins4daace02016-02-16 21:56:23 +0000127 PCI_MESSAGE_MAXIMUM
128};
129
130/*
131 * Structures defining the virtual PCI Express protocol.
132 */
133
134union pci_version {
135 struct {
136 u16 minor_version;
137 u16 major_version;
138 } parts;
139 u32 version;
140} __packed;
141
142/*
143 * Function numbers are 8-bits wide on Express, as interpreted through ARI,
144 * which is all this driver does. This representation is the one used in
145 * Windows, which is what is expected when sending this back and forth with
146 * the Hyper-V parent partition.
147 */
148union win_slot_encoding {
149 struct {
Dexuan Cui60e2e2f2017-02-10 15:18:46 -0600150 u32 dev:5;
151 u32 func:3;
Jake Oshins4daace02016-02-16 21:56:23 +0000152 u32 reserved:24;
153 } bits;
154 u32 slot;
155} __packed;
156
157/*
158 * Pretty much as defined in the PCI Specifications.
159 */
160struct pci_function_description {
161 u16 v_id; /* vendor ID */
162 u16 d_id; /* device ID */
163 u8 rev;
164 u8 prog_intf;
165 u8 subclass;
166 u8 base_class;
167 u32 subsystem_id;
168 union win_slot_encoding win_slot;
169 u32 ser; /* serial number */
170} __packed;
171
172/**
173 * struct hv_msi_desc
174 * @vector: IDT entry
175 * @delivery_mode: As defined in Intel's Programmer's
176 * Reference Manual, Volume 3, Chapter 8.
177 * @vector_count: Number of contiguous entries in the
178 * Interrupt Descriptor Table that are
179 * occupied by this Message-Signaled
180 * Interrupt. For "MSI", as first defined
181 * in PCI 2.2, this can be between 1 and
182 * 32. For "MSI-X," as first defined in PCI
183 * 3.0, this must be 1, as each MSI-X table
184 * entry would have its own descriptor.
185 * @reserved: Empty space
186 * @cpu_mask: All the target virtual processors.
187 */
188struct hv_msi_desc {
189 u8 vector;
190 u8 delivery_mode;
191 u16 vector_count;
192 u32 reserved;
193 u64 cpu_mask;
194} __packed;
195
196/**
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700197 * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
198 * @vector: IDT entry
199 * @delivery_mode: As defined in Intel's Programmer's
200 * Reference Manual, Volume 3, Chapter 8.
201 * @vector_count: Number of contiguous entries in the
202 * Interrupt Descriptor Table that are
203 * occupied by this Message-Signaled
204 * Interrupt. For "MSI", as first defined
205 * in PCI 2.2, this can be between 1 and
206 * 32. For "MSI-X," as first defined in PCI
207 * 3.0, this must be 1, as each MSI-X table
208 * entry would have its own descriptor.
209 * @processor_count: number of bits enabled in array.
210 * @processor_array: All the target virtual processors.
211 */
212struct hv_msi_desc2 {
213 u8 vector;
214 u8 delivery_mode;
215 u16 vector_count;
216 u16 processor_count;
217 u16 processor_array[32];
218} __packed;
219
220/**
Jake Oshins4daace02016-02-16 21:56:23 +0000221 * struct tran_int_desc
222 * @reserved: unused, padding
223 * @vector_count: same as in hv_msi_desc
224 * @data: This is the "data payload" value that is
225 * written by the device when it generates
226 * a message-signaled interrupt, either MSI
227 * or MSI-X.
228 * @address: This is the address to which the data
229 * payload is written on interrupt
230 * generation.
231 */
232struct tran_int_desc {
233 u16 reserved;
234 u16 vector_count;
235 u32 data;
236 u64 address;
237} __packed;
238
239/*
240 * A generic message format for virtual PCI.
241 * Specific message formats are defined later in the file.
242 */
243
244struct pci_message {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000245 u32 type;
Jake Oshins4daace02016-02-16 21:56:23 +0000246} __packed;
247
248struct pci_child_message {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000249 struct pci_message message_type;
Jake Oshins4daace02016-02-16 21:56:23 +0000250 union win_slot_encoding wslot;
251} __packed;
252
253struct pci_incoming_message {
254 struct vmpacket_descriptor hdr;
255 struct pci_message message_type;
256} __packed;
257
258struct pci_response {
259 struct vmpacket_descriptor hdr;
260 s32 status; /* negative values are failures */
261} __packed;
262
263struct pci_packet {
264 void (*completion_func)(void *context, struct pci_response *resp,
265 int resp_packet_size);
266 void *compl_ctxt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000267
268 struct pci_message message[0];
Jake Oshins4daace02016-02-16 21:56:23 +0000269};
270
271/*
272 * Specific message types supporting the PCI protocol.
273 */
274
275/*
276 * Version negotiation message. Sent from the guest to the host.
277 * The guest is free to try different versions until the host
278 * accepts the version.
279 *
280 * pci_version: The protocol version requested.
281 * is_last_attempt: If TRUE, this is the last version guest will request.
282 * reservedz: Reserved field, set to zero.
283 */
284
285struct pci_version_request {
286 struct pci_message message_type;
Jork Loeser691ac1d2017-05-24 13:41:24 -0700287 u32 protocol_version;
Jake Oshins4daace02016-02-16 21:56:23 +0000288} __packed;
289
290/*
291 * Bus D0 Entry. This is sent from the guest to the host when the virtual
292 * bus (PCI Express port) is ready for action.
293 */
294
295struct pci_bus_d0_entry {
296 struct pci_message message_type;
297 u32 reserved;
298 u64 mmio_base;
299} __packed;
300
301struct pci_bus_relations {
302 struct pci_incoming_message incoming;
303 u32 device_count;
Dexuan Cui7d0f8ee2016-08-23 04:46:39 +0000304 struct pci_function_description func[0];
Jake Oshins4daace02016-02-16 21:56:23 +0000305} __packed;
306
307struct pci_q_res_req_response {
308 struct vmpacket_descriptor hdr;
309 s32 status; /* negative values are failures */
310 u32 probed_bar[6];
311} __packed;
312
313struct pci_set_power {
314 struct pci_message message_type;
315 union win_slot_encoding wslot;
316 u32 power_state; /* In Windows terms */
317 u32 reserved;
318} __packed;
319
320struct pci_set_power_response {
321 struct vmpacket_descriptor hdr;
322 s32 status; /* negative values are failures */
323 union win_slot_encoding wslot;
324 u32 resultant_state; /* In Windows terms */
325 u32 reserved;
326} __packed;
327
328struct pci_resources_assigned {
329 struct pci_message message_type;
330 union win_slot_encoding wslot;
331 u8 memory_range[0x14][6]; /* not used here */
332 u32 msi_descriptors;
333 u32 reserved[4];
334} __packed;
335
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700336struct pci_resources_assigned2 {
337 struct pci_message message_type;
338 union win_slot_encoding wslot;
339 u8 memory_range[0x14][6]; /* not used here */
340 u32 msi_descriptor_count;
341 u8 reserved[70];
342} __packed;
343
Jake Oshins4daace02016-02-16 21:56:23 +0000344struct pci_create_interrupt {
345 struct pci_message message_type;
346 union win_slot_encoding wslot;
347 struct hv_msi_desc int_desc;
348} __packed;
349
350struct pci_create_int_response {
351 struct pci_response response;
352 u32 reserved;
353 struct tran_int_desc int_desc;
354} __packed;
355
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700356struct pci_create_interrupt2 {
357 struct pci_message message_type;
358 union win_slot_encoding wslot;
359 struct hv_msi_desc2 int_desc;
360} __packed;
361
Jake Oshins4daace02016-02-16 21:56:23 +0000362struct pci_delete_interrupt {
363 struct pci_message message_type;
364 union win_slot_encoding wslot;
365 struct tran_int_desc int_desc;
366} __packed;
367
368struct pci_dev_incoming {
369 struct pci_incoming_message incoming;
370 union win_slot_encoding wslot;
371} __packed;
372
373struct pci_eject_response {
Dexuan Cui0c6045d2016-08-23 04:45:51 +0000374 struct pci_message message_type;
Jake Oshins4daace02016-02-16 21:56:23 +0000375 union win_slot_encoding wslot;
376 u32 status;
377} __packed;
378
379static int pci_ring_size = (4 * PAGE_SIZE);
380
381/*
382 * Definitions or interrupt steering hypercall.
383 */
384#define HV_PARTITION_ID_SELF ((u64)-1)
385#define HVCALL_RETARGET_INTERRUPT 0x7e
386
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700387struct hv_interrupt_entry {
Jake Oshins4daace02016-02-16 21:56:23 +0000388 u32 source; /* 1 for MSI(-X) */
389 u32 reserved1;
390 u32 address;
391 u32 data;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700392};
393
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700394/*
395 * flags for hv_device_interrupt_target.flags
396 */
397#define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
398#define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
399
400struct hv_device_interrupt_target {
Jake Oshins4daace02016-02-16 21:56:23 +0000401 u32 vector;
402 u32 flags;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700403 union {
404 u64 vp_mask;
Maya Nakamura9bc11742019-03-01 06:59:02 +0000405 struct hv_vpset vp_set;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700406 };
407};
408
409struct retarget_msi_interrupt {
410 u64 partition_id; /* use "self" */
411 u64 device_id;
412 struct hv_interrupt_entry int_entry;
413 u64 reserved2;
414 struct hv_device_interrupt_target int_target;
Maya Nakamura6ae91572019-03-01 06:56:24 +0000415} __packed __aligned(8);
Jake Oshins4daace02016-02-16 21:56:23 +0000416
417/*
418 * Driver specific state.
419 */
420
421enum hv_pcibus_state {
422 hv_pcibus_init = 0,
423 hv_pcibus_probed,
424 hv_pcibus_installed,
Long Lid3a78d82017-03-23 14:58:10 -0700425 hv_pcibus_removed,
Jake Oshins4daace02016-02-16 21:56:23 +0000426 hv_pcibus_maximum
427};
428
429struct hv_pcibus_device {
430 struct pci_sysdata sysdata;
431 enum hv_pcibus_state state;
Stephen Hemminger6708be92018-05-23 10:11:13 -0700432 refcount_t remove_lock;
Jake Oshins4daace02016-02-16 21:56:23 +0000433 struct hv_device *hdev;
434 resource_size_t low_mmio_space;
435 resource_size_t high_mmio_space;
436 struct resource *mem_config;
437 struct resource *low_mmio_res;
438 struct resource *high_mmio_res;
439 struct completion *survey_event;
440 struct completion remove_event;
441 struct pci_bus *pci_bus;
442 spinlock_t config_lock; /* Avoid two threads writing index page */
443 spinlock_t device_list_lock; /* Protect lists below */
444 void __iomem *cfg_addr;
445
Jake Oshins4daace02016-02-16 21:56:23 +0000446 struct list_head resources_for_children;
447
448 struct list_head children;
449 struct list_head dr_list;
Jake Oshins4daace02016-02-16 21:56:23 +0000450
451 struct msi_domain_info msi_info;
452 struct msi_controller msi_chip;
453 struct irq_domain *irq_domain;
Jork Loeserbe66b672017-05-24 13:41:25 -0700454
Long Li0de8ce32016-11-08 14:04:38 -0800455 spinlock_t retarget_msi_interrupt_lock;
Dexuan Cui021ad272018-03-15 14:20:53 +0000456
457 struct workqueue_struct *wq;
Maya Nakamura9bc11742019-03-01 06:59:02 +0000458
459 /* hypercall arg, must not cross page boundary */
460 struct retarget_msi_interrupt retarget_msi_interrupt_params;
461
462 /*
463 * Don't put anything here: retarget_msi_interrupt_params must be last
464 */
Jake Oshins4daace02016-02-16 21:56:23 +0000465};
466
467/*
468 * Tracks "Device Relations" messages from the host, which must be both
469 * processed in order and deferred so that they don't run in the context
470 * of the incoming packet callback.
471 */
472struct hv_dr_work {
473 struct work_struct wrk;
474 struct hv_pcibus_device *bus;
475};
476
477struct hv_dr_state {
478 struct list_head list_entry;
479 u32 device_count;
Dexuan Cui7d0f8ee2016-08-23 04:46:39 +0000480 struct pci_function_description func[0];
Jake Oshins4daace02016-02-16 21:56:23 +0000481};
482
483enum hv_pcichild_state {
484 hv_pcichild_init = 0,
485 hv_pcichild_requirements,
486 hv_pcichild_resourced,
487 hv_pcichild_ejecting,
488 hv_pcichild_maximum
489};
490
Jake Oshins4daace02016-02-16 21:56:23 +0000491struct hv_pci_dev {
492 /* List protected by pci_rescan_remove_lock */
493 struct list_head list_entry;
Elena Reshetova24196f02017-04-18 09:02:48 -0500494 refcount_t refs;
Jake Oshins4daace02016-02-16 21:56:23 +0000495 enum hv_pcichild_state state;
Stephen Hemmingera15f2c082018-09-14 12:54:56 -0700496 struct pci_slot *pci_slot;
Jake Oshins4daace02016-02-16 21:56:23 +0000497 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;
Maya Nakamurac8ccf752019-03-01 07:04:17 +0000909 cpumask_var_t tmp;
Jake Oshins4daace02016-02-16 21:56:23 +0000910 struct pci_bus *pbus;
911 struct pci_dev *pdev;
Long Li0de8ce32016-11-08 14:04:38 -0800912 unsigned long flags;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700913 u32 var_size = 0;
Maya Nakamurac8ccf752019-03-01 07:04:17 +0000914 int cpu, nr_bank;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700915 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;
Maya Nakamurac8ccf752019-03-01 07:04:17 +0000954
955 if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
956 res = 1;
957 goto exit_unlock;
958 }
959
960 cpumask_and(tmp, dest, cpu_online_mask);
961 nr_bank = cpumask_to_vpset(&params->int_target.vp_set, tmp);
962 free_cpumask_var(tmp);
963
964 if (nr_bank <= 0) {
965 res = 1;
966 goto exit_unlock;
967 }
Long Li0de8ce32016-11-08 14:04:38 -0800968
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700969 /*
970 * var-sized hypercall, var-size starts after vp_mask (thus
Maya Nakamura9bc11742019-03-01 06:59:02 +0000971 * vp_set.format does not count, but vp_set.valid_bank_mask
972 * does).
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700973 */
Maya Nakamurac8ccf752019-03-01 07:04:17 +0000974 var_size = 1 + nr_bank;
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700975 } else {
976 for_each_cpu_and(cpu, dest, cpu_online_mask) {
977 params->int_target.vp_mask |=
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200978 (1ULL << hv_cpu_number_to_vp_number(cpu));
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700979 }
980 }
981
982 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
983 params, NULL);
984
985exit_unlock:
Long Li0de8ce32016-11-08 14:04:38 -0800986 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
Jake Oshins4daace02016-02-16 21:56:23 +0000987
Jork Loeser7dcf90e2017-05-24 13:41:28 -0700988 if (res) {
989 dev_err(&hbus->hdev->device,
990 "%s() failed: %#llx", __func__, res);
991 return;
992 }
993
Jake Oshins4daace02016-02-16 21:56:23 +0000994 pci_msi_unmask_irq(data);
995}
996
997struct compose_comp_ctxt {
998 struct hv_pci_compl comp_pkt;
999 struct tran_int_desc int_desc;
1000};
1001
1002static void hv_pci_compose_compl(void *context, struct pci_response *resp,
1003 int resp_packet_size)
1004{
1005 struct compose_comp_ctxt *comp_pkt = context;
1006 struct pci_create_int_response *int_resp =
1007 (struct pci_create_int_response *)resp;
1008
1009 comp_pkt->comp_pkt.completion_status = resp->status;
1010 comp_pkt->int_desc = int_resp->int_desc;
1011 complete(&comp_pkt->comp_pkt.host_event);
1012}
1013
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001014static u32 hv_compose_msi_req_v1(
1015 struct pci_create_interrupt *int_pkt, struct cpumask *affinity,
1016 u32 slot, u8 vector)
1017{
1018 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
1019 int_pkt->wslot.slot = slot;
1020 int_pkt->int_desc.vector = vector;
1021 int_pkt->int_desc.vector_count = 1;
Thomas Gleixnera31e58e2017-12-28 11:33:33 +01001022 int_pkt->int_desc.delivery_mode = dest_Fixed;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001023
1024 /*
1025 * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
1026 * hv_irq_unmask().
1027 */
1028 int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
1029
1030 return sizeof(*int_pkt);
1031}
1032
1033static u32 hv_compose_msi_req_v2(
1034 struct pci_create_interrupt2 *int_pkt, struct cpumask *affinity,
1035 u32 slot, u8 vector)
1036{
1037 int cpu;
1038
1039 int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
1040 int_pkt->wslot.slot = slot;
1041 int_pkt->int_desc.vector = vector;
1042 int_pkt->int_desc.vector_count = 1;
Thomas Gleixnera31e58e2017-12-28 11:33:33 +01001043 int_pkt->int_desc.delivery_mode = dest_Fixed;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001044
1045 /*
1046 * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
1047 * by subsequent retarget in hv_irq_unmask().
1048 */
1049 cpu = cpumask_first_and(affinity, cpu_online_mask);
1050 int_pkt->int_desc.processor_array[0] =
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +02001051 hv_cpu_number_to_vp_number(cpu);
Jork Loeser7dcf90e2017-05-24 13:41:28 -07001052 int_pkt->int_desc.processor_count = 1;
1053
1054 return sizeof(*int_pkt);
1055}
1056
Jake Oshins4daace02016-02-16 21:56:23 +00001057/**
1058 * hv_compose_msi_msg() - Supplies a valid MSI address/data
1059 * @data: Everything about this MSI
1060 * @msg: Buffer that is filled in by this function
1061 *
1062 * This function unpacks the IRQ looking for target CPU set, IDT
1063 * vector and mode and sends a message to the parent partition
1064 * asking for a mapping for that tuple in this partition. The
1065 * response supplies a data value and address to which that data
1066 * should be written to trigger that interrupt.
1067 */
1068static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
1069{
1070 struct irq_cfg *cfg = irqd_cfg(data);
1071 struct hv_pcibus_device *hbus;
1072 struct hv_pci_dev *hpdev;
1073 struct pci_bus *pbus;
1074 struct pci_dev *pdev;
Dexuan Cui79aa8012017-11-01 20:30:53 +00001075 struct cpumask *dest;
Dexuan Cui35a88a12018-07-09 13:16:07 -05001076 unsigned long flags;
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
Dexuan Cui35a88a12018-07-09 13:16:07 -05001168 * local_irq_save()/restore() to avoid race:
1169 * hv_pci_onchannelcallback() can also run in tasklet.
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001170 */
Dexuan Cui35a88a12018-07-09 13:16:07 -05001171 local_irq_save(flags);
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001172
1173 if (hbus->hdev->channel->target_cpu == smp_processor_id())
1174 hv_pci_onchannelcallback(hbus);
1175
Dexuan Cui35a88a12018-07-09 13:16:07 -05001176 local_irq_restore(flags);
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001177
1178 if (hpdev->state == hv_pcichild_ejecting) {
1179 dev_err_once(&hbus->hdev->device,
1180 "the device is being ejected\n");
1181 goto free_int_desc;
1182 }
1183
Stephen Hemminger80bfeeb2017-07-31 16:48:29 -07001184 udelay(100);
Dexuan Cuide0aa7b2018-03-15 14:21:08 +00001185 }
Jake Oshins4daace02016-02-16 21:56:23 +00001186
1187 if (comp.comp_pkt.completion_status < 0) {
1188 dev_err(&hbus->hdev->device,
1189 "Request for interrupt failed: 0x%x",
1190 comp.comp_pkt.completion_status);
1191 goto free_int_desc;
1192 }
1193
1194 /*
1195 * Record the assignment so that this can be unwound later. Using
1196 * irq_set_chip_data() here would be appropriate, but the lock it takes
1197 * is already held.
1198 */
1199 *int_desc = comp.int_desc;
1200 data->chip_data = int_desc;
1201
1202 /* Pass up the result. */
1203 msg->address_hi = comp.int_desc.address >> 32;
1204 msg->address_lo = comp.int_desc.address & 0xffffffff;
1205 msg->data = comp.int_desc.data;
1206
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001207 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001208 return;
1209
1210free_int_desc:
1211 kfree(int_desc);
1212drop_reference:
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001213 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001214return_null_message:
1215 msg->address_hi = 0;
1216 msg->address_lo = 0;
1217 msg->data = 0;
1218}
1219
1220/* HW Interrupt Chip Descriptor */
1221static struct irq_chip hv_msi_irq_chip = {
1222 .name = "Hyper-V PCIe MSI",
1223 .irq_compose_msi_msg = hv_compose_msi_msg,
1224 .irq_set_affinity = hv_set_affinity,
1225 .irq_ack = irq_chip_ack_parent,
1226 .irq_mask = hv_irq_mask,
1227 .irq_unmask = hv_irq_unmask,
1228};
1229
1230static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
1231 msi_alloc_info_t *arg)
1232{
1233 return arg->msi_hwirq;
1234}
1235
1236static struct msi_domain_ops hv_msi_ops = {
1237 .get_hwirq = hv_msi_domain_ops_get_hwirq,
1238 .msi_prepare = pci_msi_prepare,
1239 .set_desc = pci_msi_set_desc,
1240 .msi_free = hv_msi_free,
1241};
1242
1243/**
1244 * hv_pcie_init_irq_domain() - Initialize IRQ domain
1245 * @hbus: The root PCI bus
1246 *
1247 * This function creates an IRQ domain which will be used for
1248 * interrupts from devices that have been passed through. These
1249 * devices only support MSI and MSI-X, not line-based interrupts
1250 * or simulations of line-based interrupts through PCIe's
1251 * fabric-layer messages. Because interrupts are remapped, we
1252 * can support multi-message MSI here.
1253 *
1254 * Return: '0' on success and error value on failure
1255 */
1256static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
1257{
1258 hbus->msi_info.chip = &hv_msi_irq_chip;
1259 hbus->msi_info.ops = &hv_msi_ops;
1260 hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
1261 MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
1262 MSI_FLAG_PCI_MSIX);
1263 hbus->msi_info.handler = handle_edge_irq;
1264 hbus->msi_info.handler_name = "edge";
1265 hbus->msi_info.data = hbus;
1266 hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
1267 &hbus->msi_info,
1268 x86_vector_domain);
1269 if (!hbus->irq_domain) {
1270 dev_err(&hbus->hdev->device,
1271 "Failed to build an MSI IRQ domain\n");
1272 return -ENODEV;
1273 }
1274
1275 return 0;
1276}
1277
1278/**
1279 * get_bar_size() - Get the address space consumed by a BAR
1280 * @bar_val: Value that a BAR returned after -1 was written
1281 * to it.
1282 *
1283 * This function returns the size of the BAR, rounded up to 1
1284 * page. It has to be rounded up because the hypervisor's page
1285 * table entry that maps the BAR into the VM can't specify an
1286 * offset within a page. The invariant is that the hypervisor
1287 * must place any BARs of smaller than page length at the
1288 * beginning of a page.
1289 *
1290 * Return: Size in bytes of the consumed MMIO space.
1291 */
1292static u64 get_bar_size(u64 bar_val)
1293{
1294 return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
1295 PAGE_SIZE);
1296}
1297
1298/**
1299 * survey_child_resources() - Total all MMIO requirements
1300 * @hbus: Root PCI bus, as understood by this driver
1301 */
1302static void survey_child_resources(struct hv_pcibus_device *hbus)
1303{
Jake Oshins4daace02016-02-16 21:56:23 +00001304 struct hv_pci_dev *hpdev;
1305 resource_size_t bar_size = 0;
1306 unsigned long flags;
1307 struct completion *event;
1308 u64 bar_val;
1309 int i;
1310
1311 /* If nobody is waiting on the answer, don't compute it. */
1312 event = xchg(&hbus->survey_event, NULL);
1313 if (!event)
1314 return;
1315
1316 /* If the answer has already been computed, go with it. */
1317 if (hbus->low_mmio_space || hbus->high_mmio_space) {
1318 complete(event);
1319 return;
1320 }
1321
1322 spin_lock_irqsave(&hbus->device_list_lock, flags);
1323
1324 /*
1325 * Due to an interesting quirk of the PCI spec, all memory regions
1326 * for a child device are a power of 2 in size and aligned in memory,
1327 * so it's sufficient to just add them up without tracking alignment.
1328 */
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001329 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Jake Oshins4daace02016-02-16 21:56:23 +00001330 for (i = 0; i < 6; i++) {
1331 if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
1332 dev_err(&hbus->hdev->device,
1333 "There's an I/O BAR in this list!\n");
1334
1335 if (hpdev->probed_bar[i] != 0) {
1336 /*
1337 * A probed BAR has all the upper bits set that
1338 * can be changed.
1339 */
1340
1341 bar_val = hpdev->probed_bar[i];
1342 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1343 bar_val |=
1344 ((u64)hpdev->probed_bar[++i] << 32);
1345 else
1346 bar_val |= 0xffffffff00000000ULL;
1347
1348 bar_size = get_bar_size(bar_val);
1349
1350 if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
1351 hbus->high_mmio_space += bar_size;
1352 else
1353 hbus->low_mmio_space += bar_size;
1354 }
1355 }
1356 }
1357
1358 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1359 complete(event);
1360}
1361
1362/**
1363 * prepopulate_bars() - Fill in BARs with defaults
1364 * @hbus: Root PCI bus, as understood by this driver
1365 *
1366 * The core PCI driver code seems much, much happier if the BARs
1367 * for a device have values upon first scan. So fill them in.
1368 * The algorithm below works down from large sizes to small,
1369 * attempting to pack the assignments optimally. The assumption,
1370 * enforced in other parts of the code, is that the beginning of
1371 * the memory-mapped I/O space will be aligned on the largest
1372 * BAR size.
1373 */
1374static void prepopulate_bars(struct hv_pcibus_device *hbus)
1375{
1376 resource_size_t high_size = 0;
1377 resource_size_t low_size = 0;
1378 resource_size_t high_base = 0;
1379 resource_size_t low_base = 0;
1380 resource_size_t bar_size;
1381 struct hv_pci_dev *hpdev;
Jake Oshins4daace02016-02-16 21:56:23 +00001382 unsigned long flags;
1383 u64 bar_val;
1384 u32 command;
1385 bool high;
1386 int i;
1387
1388 if (hbus->low_mmio_space) {
1389 low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
1390 low_base = hbus->low_mmio_res->start;
1391 }
1392
1393 if (hbus->high_mmio_space) {
1394 high_size = 1ULL <<
1395 (63 - __builtin_clzll(hbus->high_mmio_space));
1396 high_base = hbus->high_mmio_res->start;
1397 }
1398
1399 spin_lock_irqsave(&hbus->device_list_lock, flags);
1400
1401 /* Pick addresses for the BARs. */
1402 do {
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001403 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Jake Oshins4daace02016-02-16 21:56:23 +00001404 for (i = 0; i < 6; i++) {
1405 bar_val = hpdev->probed_bar[i];
1406 if (bar_val == 0)
1407 continue;
1408 high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
1409 if (high) {
1410 bar_val |=
1411 ((u64)hpdev->probed_bar[i + 1]
1412 << 32);
1413 } else {
1414 bar_val |= 0xffffffffULL << 32;
1415 }
1416 bar_size = get_bar_size(bar_val);
1417 if (high) {
1418 if (high_size != bar_size) {
1419 i++;
1420 continue;
1421 }
1422 _hv_pcifront_write_config(hpdev,
1423 PCI_BASE_ADDRESS_0 + (4 * i),
1424 4,
1425 (u32)(high_base & 0xffffff00));
1426 i++;
1427 _hv_pcifront_write_config(hpdev,
1428 PCI_BASE_ADDRESS_0 + (4 * i),
1429 4, (u32)(high_base >> 32));
1430 high_base += bar_size;
1431 } else {
1432 if (low_size != bar_size)
1433 continue;
1434 _hv_pcifront_write_config(hpdev,
1435 PCI_BASE_ADDRESS_0 + (4 * i),
1436 4,
1437 (u32)(low_base & 0xffffff00));
1438 low_base += bar_size;
1439 }
1440 }
1441 if (high_size <= 1 && low_size <= 1) {
1442 /* Set the memory enable bit. */
1443 _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
1444 &command);
1445 command |= PCI_COMMAND_MEMORY;
1446 _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
1447 command);
1448 break;
1449 }
1450 }
1451
1452 high_size >>= 1;
1453 low_size >>= 1;
1454 } while (high_size || low_size);
1455
1456 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1457}
1458
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001459/*
1460 * Assign entries in sysfs pci slot directory.
1461 *
1462 * Note that this function does not need to lock the children list
1463 * because it is called from pci_devices_present_work which
1464 * is serialized with hv_eject_device_work because they are on the
1465 * same ordered workqueue. Therefore hbus->children list will not change
1466 * even when pci_create_slot sleeps.
1467 */
1468static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
1469{
1470 struct hv_pci_dev *hpdev;
1471 char name[SLOT_NAME_SIZE];
1472 int slot_nr;
1473
1474 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1475 if (hpdev->pci_slot)
1476 continue;
1477
1478 slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
1479 snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
1480 hpdev->pci_slot = pci_create_slot(hbus->pci_bus, slot_nr,
1481 name, NULL);
Wei Yongjun54be5b82018-09-21 02:53:17 +00001482 if (IS_ERR(hpdev->pci_slot)) {
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001483 pr_warn("pci_create slot %s failed\n", name);
Wei Yongjun54be5b82018-09-21 02:53:17 +00001484 hpdev->pci_slot = NULL;
1485 }
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001486 }
1487}
1488
Jake Oshins4daace02016-02-16 21:56:23 +00001489/**
1490 * create_root_hv_pci_bus() - Expose a new root PCI bus
1491 * @hbus: Root PCI bus, as understood by this driver
1492 *
1493 * Return: 0 on success, -errno on failure
1494 */
1495static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
1496{
1497 /* Register the device */
1498 hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
1499 0, /* bus number is always zero */
1500 &hv_pcifront_ops,
1501 &hbus->sysdata,
1502 &hbus->resources_for_children);
1503 if (!hbus->pci_bus)
1504 return -ENODEV;
1505
1506 hbus->pci_bus->msi = &hbus->msi_chip;
1507 hbus->pci_bus->msi->dev = &hbus->hdev->device;
1508
Long Li414428c2017-03-23 14:58:32 -07001509 pci_lock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001510 pci_scan_child_bus(hbus->pci_bus);
1511 pci_bus_assign_resources(hbus->pci_bus);
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001512 hv_pci_assign_slots(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00001513 pci_bus_add_devices(hbus->pci_bus);
Long Li414428c2017-03-23 14:58:32 -07001514 pci_unlock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001515 hbus->state = hv_pcibus_installed;
1516 return 0;
1517}
1518
1519struct q_res_req_compl {
1520 struct completion host_event;
1521 struct hv_pci_dev *hpdev;
1522};
1523
1524/**
1525 * q_resource_requirements() - Query Resource Requirements
1526 * @context: The completion context.
1527 * @resp: The response that came from the host.
1528 * @resp_packet_size: The size in bytes of resp.
1529 *
1530 * This function is invoked on completion of a Query Resource
1531 * Requirements packet.
1532 */
1533static void q_resource_requirements(void *context, struct pci_response *resp,
1534 int resp_packet_size)
1535{
1536 struct q_res_req_compl *completion = context;
1537 struct pci_q_res_req_response *q_res_req =
1538 (struct pci_q_res_req_response *)resp;
1539 int i;
1540
1541 if (resp->status < 0) {
1542 dev_err(&completion->hpdev->hbus->hdev->device,
1543 "query resource requirements failed: %x\n",
1544 resp->status);
1545 } else {
1546 for (i = 0; i < 6; i++) {
1547 completion->hpdev->probed_bar[i] =
1548 q_res_req->probed_bar[i];
1549 }
1550 }
1551
1552 complete(&completion->host_event);
1553}
1554
Jake Oshins4daace02016-02-16 21:56:23 +00001555/**
1556 * new_pcichild_device() - Create a new child device
1557 * @hbus: The internal struct tracking this root PCI bus.
1558 * @desc: The information supplied so far from the host
1559 * about the device.
1560 *
1561 * This function creates the tracking structure for a new child
1562 * device and kicks off the process of figuring out what it is.
1563 *
1564 * Return: Pointer to the new tracking struct
1565 */
1566static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
1567 struct pci_function_description *desc)
1568{
1569 struct hv_pci_dev *hpdev;
1570 struct pci_child_message *res_req;
1571 struct q_res_req_compl comp_pkt;
Dexuan Cui8286e962016-11-10 07:17:48 +00001572 struct {
1573 struct pci_packet init_packet;
1574 u8 buffer[sizeof(struct pci_child_message)];
Jake Oshins4daace02016-02-16 21:56:23 +00001575 } pkt;
1576 unsigned long flags;
1577 int ret;
1578
Jia-Ju Bai7403bd12018-03-18 22:53:28 +08001579 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00001580 if (!hpdev)
1581 return NULL;
1582
1583 hpdev->hbus = hbus;
1584
1585 memset(&pkt, 0, sizeof(pkt));
1586 init_completion(&comp_pkt.host_event);
1587 comp_pkt.hpdev = hpdev;
1588 pkt.init_packet.compl_ctxt = &comp_pkt;
1589 pkt.init_packet.completion_func = q_resource_requirements;
1590 res_req = (struct pci_child_message *)&pkt.init_packet.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001591 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
Jake Oshins4daace02016-02-16 21:56:23 +00001592 res_req->wslot.slot = desc->win_slot.slot;
1593
1594 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
1595 sizeof(struct pci_child_message),
1596 (unsigned long)&pkt.init_packet,
1597 VM_PKT_DATA_INBAND,
1598 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1599 if (ret)
1600 goto error;
1601
Dexuan Cuic3635da2018-05-23 21:12:01 +00001602 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
1603 goto error;
Jake Oshins4daace02016-02-16 21:56:23 +00001604
1605 hpdev->desc = *desc;
Elena Reshetova24196f02017-04-18 09:02:48 -05001606 refcount_set(&hpdev->refs, 1);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001607 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001608 spin_lock_irqsave(&hbus->device_list_lock, flags);
Haiyang Zhang4a9b0932017-02-13 18:10:11 +00001609
Jake Oshins4daace02016-02-16 21:56:23 +00001610 list_add_tail(&hpdev->list_entry, &hbus->children);
1611 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1612 return hpdev;
1613
1614error:
1615 kfree(hpdev);
1616 return NULL;
1617}
1618
1619/**
1620 * get_pcichild_wslot() - Find device from slot
1621 * @hbus: Root PCI bus, as understood by this driver
1622 * @wslot: Location on the bus
1623 *
1624 * This function looks up a PCI device and returns the internal
1625 * representation of it. It acquires a reference on it, so that
1626 * the device won't be deleted while somebody is using it. The
1627 * caller is responsible for calling put_pcichild() to release
1628 * this reference.
1629 *
1630 * Return: Internal representation of a PCI device
1631 */
1632static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
1633 u32 wslot)
1634{
1635 unsigned long flags;
1636 struct hv_pci_dev *iter, *hpdev = NULL;
1637
1638 spin_lock_irqsave(&hbus->device_list_lock, flags);
1639 list_for_each_entry(iter, &hbus->children, list_entry) {
1640 if (iter->desc.win_slot.slot == wslot) {
1641 hpdev = iter;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001642 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001643 break;
1644 }
1645 }
1646 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1647
1648 return hpdev;
1649}
1650
1651/**
1652 * pci_devices_present_work() - Handle new list of child devices
1653 * @work: Work struct embedded in struct hv_dr_work
1654 *
1655 * "Bus Relations" is the Windows term for "children of this
1656 * bus." The terminology is preserved here for people trying to
1657 * debug the interaction between Hyper-V and Linux. This
1658 * function is called when the parent partition reports a list
1659 * of functions that should be observed under this PCI Express
1660 * port (bus).
1661 *
1662 * This function updates the list, and must tolerate being
1663 * called multiple times with the same information. The typical
1664 * number of child devices is one, with very atypical cases
1665 * involving three or four, so the algorithms used here can be
1666 * simple and inefficient.
1667 *
1668 * It must also treat the omission of a previously observed device as
1669 * notification that the device no longer exists.
1670 *
Dexuan Cui021ad272018-03-15 14:20:53 +00001671 * Note that this function is serialized with hv_eject_device_work(),
1672 * because both are pushed to the ordered workqueue hbus->wq.
Jake Oshins4daace02016-02-16 21:56:23 +00001673 */
1674static void pci_devices_present_work(struct work_struct *work)
1675{
1676 u32 child_no;
1677 bool found;
Jake Oshins4daace02016-02-16 21:56:23 +00001678 struct pci_function_description *new_desc;
1679 struct hv_pci_dev *hpdev;
1680 struct hv_pcibus_device *hbus;
1681 struct list_head removed;
1682 struct hv_dr_work *dr_wrk;
1683 struct hv_dr_state *dr = NULL;
1684 unsigned long flags;
1685
1686 dr_wrk = container_of(work, struct hv_dr_work, wrk);
1687 hbus = dr_wrk->bus;
1688 kfree(dr_wrk);
1689
1690 INIT_LIST_HEAD(&removed);
1691
Jake Oshins4daace02016-02-16 21:56:23 +00001692 /* Pull this off the queue and process it if it was the last one. */
1693 spin_lock_irqsave(&hbus->device_list_lock, flags);
1694 while (!list_empty(&hbus->dr_list)) {
1695 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
1696 list_entry);
1697 list_del(&dr->list_entry);
1698
1699 /* Throw this away if the list still has stuff in it. */
1700 if (!list_empty(&hbus->dr_list)) {
1701 kfree(dr);
1702 continue;
1703 }
1704 }
1705 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1706
1707 if (!dr) {
Jake Oshins4daace02016-02-16 21:56:23 +00001708 put_hvpcibus(hbus);
1709 return;
1710 }
1711
1712 /* First, mark all existing children as reported missing. */
1713 spin_lock_irqsave(&hbus->device_list_lock, flags);
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001714 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1715 hpdev->reported_missing = true;
Jake Oshins4daace02016-02-16 21:56:23 +00001716 }
1717 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1718
1719 /* Next, add back any reported devices. */
1720 for (child_no = 0; child_no < dr->device_count; child_no++) {
1721 found = false;
1722 new_desc = &dr->func[child_no];
1723
1724 spin_lock_irqsave(&hbus->device_list_lock, flags);
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001725 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1726 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
Jake Oshins4daace02016-02-16 21:56:23 +00001727 (hpdev->desc.v_id == new_desc->v_id) &&
1728 (hpdev->desc.d_id == new_desc->d_id) &&
1729 (hpdev->desc.ser == new_desc->ser)) {
1730 hpdev->reported_missing = false;
1731 found = true;
1732 }
1733 }
1734 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1735
1736 if (!found) {
1737 hpdev = new_pcichild_device(hbus, new_desc);
1738 if (!hpdev)
1739 dev_err(&hbus->hdev->device,
1740 "couldn't record a child device.\n");
1741 }
1742 }
1743
1744 /* Move missing children to a list on the stack. */
1745 spin_lock_irqsave(&hbus->device_list_lock, flags);
1746 do {
1747 found = false;
Stephen Hemminger5b8db8f2018-05-23 10:11:14 -07001748 list_for_each_entry(hpdev, &hbus->children, list_entry) {
Jake Oshins4daace02016-02-16 21:56:23 +00001749 if (hpdev->reported_missing) {
1750 found = true;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001751 put_pcichild(hpdev);
Wei Yongjun4f1cb012016-07-28 16:16:48 +00001752 list_move_tail(&hpdev->list_entry, &removed);
Jake Oshins4daace02016-02-16 21:56:23 +00001753 break;
1754 }
1755 }
1756 } while (found);
1757 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1758
1759 /* Delete everything that should no longer exist. */
1760 while (!list_empty(&removed)) {
1761 hpdev = list_first_entry(&removed, struct hv_pci_dev,
1762 list_entry);
1763 list_del(&hpdev->list_entry);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001764 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001765 }
1766
Jork Loeser691ac1d2017-05-24 13:41:24 -07001767 switch (hbus->state) {
Long Lid3a78d82017-03-23 14:58:10 -07001768 case hv_pcibus_installed:
1769 /*
Jork Loeser691ac1d2017-05-24 13:41:24 -07001770 * Tell the core to rescan bus
1771 * because there may have been changes.
1772 */
Jake Oshins4daace02016-02-16 21:56:23 +00001773 pci_lock_rescan_remove();
1774 pci_scan_child_bus(hbus->pci_bus);
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001775 hv_pci_assign_slots(hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00001776 pci_unlock_rescan_remove();
Long Lid3a78d82017-03-23 14:58:10 -07001777 break;
1778
1779 case hv_pcibus_init:
1780 case hv_pcibus_probed:
Jake Oshins4daace02016-02-16 21:56:23 +00001781 survey_child_resources(hbus);
Long Lid3a78d82017-03-23 14:58:10 -07001782 break;
1783
1784 default:
1785 break;
Jake Oshins4daace02016-02-16 21:56:23 +00001786 }
1787
Jake Oshins4daace02016-02-16 21:56:23 +00001788 put_hvpcibus(hbus);
1789 kfree(dr);
1790}
1791
1792/**
1793 * hv_pci_devices_present() - Handles list of new children
1794 * @hbus: Root PCI bus, as understood by this driver
1795 * @relations: Packet from host listing children
1796 *
1797 * This function is invoked whenever a new list of devices for
1798 * this bus appears.
1799 */
1800static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
1801 struct pci_bus_relations *relations)
1802{
1803 struct hv_dr_state *dr;
1804 struct hv_dr_work *dr_wrk;
1805 unsigned long flags;
Dexuan Cui948373b2018-03-15 14:22:00 +00001806 bool pending_dr;
Jake Oshins4daace02016-02-16 21:56:23 +00001807
1808 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
1809 if (!dr_wrk)
1810 return;
1811
1812 dr = kzalloc(offsetof(struct hv_dr_state, func) +
1813 (sizeof(struct pci_function_description) *
1814 (relations->device_count)), GFP_NOWAIT);
1815 if (!dr) {
1816 kfree(dr_wrk);
1817 return;
1818 }
1819
1820 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
1821 dr_wrk->bus = hbus;
1822 dr->device_count = relations->device_count;
1823 if (dr->device_count != 0) {
1824 memcpy(dr->func, relations->func,
1825 sizeof(struct pci_function_description) *
1826 dr->device_count);
1827 }
1828
1829 spin_lock_irqsave(&hbus->device_list_lock, flags);
Dexuan Cui948373b2018-03-15 14:22:00 +00001830 /*
1831 * If pending_dr is true, we have already queued a work,
1832 * which will see the new dr. Otherwise, we need to
1833 * queue a new work.
1834 */
1835 pending_dr = !list_empty(&hbus->dr_list);
Jake Oshins4daace02016-02-16 21:56:23 +00001836 list_add_tail(&dr->list_entry, &hbus->dr_list);
1837 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1838
Dexuan Cui948373b2018-03-15 14:22:00 +00001839 if (pending_dr) {
1840 kfree(dr_wrk);
1841 } else {
1842 get_hvpcibus(hbus);
1843 queue_work(hbus->wq, &dr_wrk->wrk);
1844 }
Jake Oshins4daace02016-02-16 21:56:23 +00001845}
1846
1847/**
1848 * hv_eject_device_work() - Asynchronously handles ejection
1849 * @work: Work struct embedded in internal device struct
1850 *
1851 * This function handles ejecting a device. Windows will
1852 * attempt to gracefully eject a device, waiting 60 seconds to
1853 * hear back from the guest OS that this completed successfully.
1854 * If this timer expires, the device will be forcibly removed.
1855 */
1856static void hv_eject_device_work(struct work_struct *work)
1857{
1858 struct pci_eject_response *ejct_pkt;
1859 struct hv_pci_dev *hpdev;
1860 struct pci_dev *pdev;
1861 unsigned long flags;
1862 int wslot;
1863 struct {
1864 struct pci_packet pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001865 u8 buffer[sizeof(struct pci_eject_response)];
Jake Oshins4daace02016-02-16 21:56:23 +00001866 } ctxt;
1867
1868 hpdev = container_of(work, struct hv_pci_dev, wrk);
1869
Dexuan Cuifca288c2018-03-15 14:21:43 +00001870 WARN_ON(hpdev->state != hv_pcichild_ejecting);
Jake Oshins4daace02016-02-16 21:56:23 +00001871
1872 /*
1873 * Ejection can come before or after the PCI bus has been set up, so
1874 * attempt to find it and tear down the bus state, if it exists. This
1875 * must be done without constructs like pci_domain_nr(hbus->pci_bus)
1876 * because hbus->pci_bus may not exist yet.
1877 */
1878 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
1879 pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0,
1880 wslot);
1881 if (pdev) {
Long Li414428c2017-03-23 14:58:32 -07001882 pci_lock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001883 pci_stop_and_remove_bus_device(pdev);
1884 pci_dev_put(pdev);
Long Li414428c2017-03-23 14:58:32 -07001885 pci_unlock_rescan_remove();
Jake Oshins4daace02016-02-16 21:56:23 +00001886 }
1887
Dexuan Cuie74d2eb2016-11-10 07:19:52 +00001888 spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
1889 list_del(&hpdev->list_entry);
1890 spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
1891
Stephen Hemmingera15f2c082018-09-14 12:54:56 -07001892 if (hpdev->pci_slot)
1893 pci_destroy_slot(hpdev->pci_slot);
1894
Jake Oshins4daace02016-02-16 21:56:23 +00001895 memset(&ctxt, 0, sizeof(ctxt));
1896 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001897 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
Jake Oshins4daace02016-02-16 21:56:23 +00001898 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1899 vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
1900 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
1901 VM_PKT_DATA_INBAND, 0);
1902
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001903 put_pcichild(hpdev);
1904 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001905 put_hvpcibus(hpdev->hbus);
1906}
1907
1908/**
1909 * hv_pci_eject_device() - Handles device ejection
1910 * @hpdev: Internal device tracking struct
1911 *
1912 * This function is invoked when an ejection packet arrives. It
1913 * just schedules work so that we don't re-enter the packet
1914 * delivery code handling the ejection.
1915 */
1916static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
1917{
1918 hpdev->state = hv_pcichild_ejecting;
Stephen Hemminger8c99e122018-05-23 10:11:12 -07001919 get_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00001920 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
1921 get_hvpcibus(hpdev->hbus);
Dexuan Cui021ad272018-03-15 14:20:53 +00001922 queue_work(hpdev->hbus->wq, &hpdev->wrk);
Jake Oshins4daace02016-02-16 21:56:23 +00001923}
1924
1925/**
1926 * hv_pci_onchannelcallback() - Handles incoming packets
1927 * @context: Internal bus tracking struct
1928 *
1929 * This function is invoked whenever the host sends a packet to
1930 * this channel (which is private to this root PCI bus).
1931 */
1932static void hv_pci_onchannelcallback(void *context)
1933{
1934 const int packet_size = 0x100;
1935 int ret;
1936 struct hv_pcibus_device *hbus = context;
1937 u32 bytes_recvd;
1938 u64 req_id;
1939 struct vmpacket_descriptor *desc;
1940 unsigned char *buffer;
1941 int bufferlen = packet_size;
1942 struct pci_packet *comp_packet;
1943 struct pci_response *response;
1944 struct pci_incoming_message *new_message;
1945 struct pci_bus_relations *bus_rel;
1946 struct pci_dev_incoming *dev_message;
1947 struct hv_pci_dev *hpdev;
1948
1949 buffer = kmalloc(bufferlen, GFP_ATOMIC);
1950 if (!buffer)
1951 return;
1952
1953 while (1) {
1954 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
1955 bufferlen, &bytes_recvd, &req_id);
1956
1957 if (ret == -ENOBUFS) {
1958 kfree(buffer);
1959 /* Handle large packet */
1960 bufferlen = bytes_recvd;
1961 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1962 if (!buffer)
1963 return;
1964 continue;
1965 }
1966
Vitaly Kuznetsov837d7412016-06-17 12:45:30 -05001967 /* Zero length indicates there are no more packets. */
1968 if (ret || !bytes_recvd)
1969 break;
1970
Jake Oshins4daace02016-02-16 21:56:23 +00001971 /*
1972 * All incoming packets must be at least as large as a
1973 * response.
1974 */
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02001975 if (bytes_recvd <= sizeof(struct pci_response))
Vitaly Kuznetsov837d7412016-06-17 12:45:30 -05001976 continue;
Jake Oshins4daace02016-02-16 21:56:23 +00001977 desc = (struct vmpacket_descriptor *)buffer;
1978
1979 switch (desc->type) {
1980 case VM_PKT_COMP:
1981
1982 /*
1983 * The host is trusted, and thus it's safe to interpret
1984 * this transaction ID as a pointer.
1985 */
1986 comp_packet = (struct pci_packet *)req_id;
1987 response = (struct pci_response *)buffer;
1988 comp_packet->completion_func(comp_packet->compl_ctxt,
1989 response,
1990 bytes_recvd);
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02001991 break;
Jake Oshins4daace02016-02-16 21:56:23 +00001992
1993 case VM_PKT_DATA_INBAND:
1994
1995 new_message = (struct pci_incoming_message *)buffer;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00001996 switch (new_message->message_type.type) {
Jake Oshins4daace02016-02-16 21:56:23 +00001997 case PCI_BUS_RELATIONS:
1998
1999 bus_rel = (struct pci_bus_relations *)buffer;
2000 if (bytes_recvd <
2001 offsetof(struct pci_bus_relations, func) +
2002 (sizeof(struct pci_function_description) *
2003 (bus_rel->device_count))) {
2004 dev_err(&hbus->hdev->device,
2005 "bus relations too small\n");
2006 break;
2007 }
2008
2009 hv_pci_devices_present(hbus, bus_rel);
2010 break;
2011
2012 case PCI_EJECT:
2013
2014 dev_message = (struct pci_dev_incoming *)buffer;
2015 hpdev = get_pcichild_wslot(hbus,
2016 dev_message->wslot.slot);
2017 if (hpdev) {
2018 hv_pci_eject_device(hpdev);
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002019 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002020 }
2021 break;
2022
2023 default:
2024 dev_warn(&hbus->hdev->device,
2025 "Unimplemented protocol message %x\n",
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002026 new_message->message_type.type);
Jake Oshins4daace02016-02-16 21:56:23 +00002027 break;
2028 }
2029 break;
2030
2031 default:
2032 dev_err(&hbus->hdev->device,
2033 "unhandled packet type %d, tid %llx len %d\n",
2034 desc->type, req_id, bytes_recvd);
2035 break;
2036 }
Jake Oshins4daace02016-02-16 21:56:23 +00002037 }
Vitaly Kuznetsov60fcdac2016-05-30 16:17:58 +02002038
2039 kfree(buffer);
Jake Oshins4daace02016-02-16 21:56:23 +00002040}
2041
2042/**
2043 * hv_pci_protocol_negotiation() - Set up protocol
2044 * @hdev: VMBus's tracking struct for this root PCI bus
2045 *
2046 * This driver is intended to support running on Windows 10
2047 * (server) and later versions. It will not run on earlier
2048 * versions, as they assume that many of the operations which
2049 * Linux needs accomplished with a spinlock held were done via
2050 * asynchronous messaging via VMBus. Windows 10 increases the
2051 * surface area of PCI emulation so that these actions can take
2052 * place by suspending a virtual processor for their duration.
2053 *
2054 * This function negotiates the channel protocol version,
2055 * failing if the host doesn't support the necessary protocol
2056 * level.
2057 */
2058static int hv_pci_protocol_negotiation(struct hv_device *hdev)
2059{
2060 struct pci_version_request *version_req;
2061 struct hv_pci_compl comp_pkt;
2062 struct pci_packet *pkt;
2063 int ret;
Jork Loeserb1db7e72017-05-24 13:41:27 -07002064 int i;
Jake Oshins4daace02016-02-16 21:56:23 +00002065
2066 /*
2067 * Initiate the handshake with the host and negotiate
2068 * a version that the host can support. We start with the
2069 * highest version number and go down if the host cannot
2070 * support it.
2071 */
2072 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2073 if (!pkt)
2074 return -ENOMEM;
2075
2076 init_completion(&comp_pkt.host_event);
2077 pkt->completion_func = hv_pci_generic_compl;
2078 pkt->compl_ctxt = &comp_pkt;
2079 version_req = (struct pci_version_request *)&pkt->message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002080 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
Jake Oshins4daace02016-02-16 21:56:23 +00002081
Jork Loeserb1db7e72017-05-24 13:41:27 -07002082 for (i = 0; i < ARRAY_SIZE(pci_protocol_versions); i++) {
2083 version_req->protocol_version = pci_protocol_versions[i];
2084 ret = vmbus_sendpacket(hdev->channel, version_req,
2085 sizeof(struct pci_version_request),
2086 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2087 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002088 if (!ret)
2089 ret = wait_for_response(hdev, &comp_pkt.host_event);
2090
Jork Loeserb1db7e72017-05-24 13:41:27 -07002091 if (ret) {
2092 dev_err(&hdev->device,
Dexuan Cuic3635da2018-05-23 21:12:01 +00002093 "PCI Pass-through VSP failed to request version: %d",
Jork Loeserb1db7e72017-05-24 13:41:27 -07002094 ret);
2095 goto exit;
2096 }
Jake Oshins4daace02016-02-16 21:56:23 +00002097
Jork Loeserb1db7e72017-05-24 13:41:27 -07002098 if (comp_pkt.completion_status >= 0) {
2099 pci_protocol_version = pci_protocol_versions[i];
2100 dev_info(&hdev->device,
2101 "PCI VMBus probing: Using version %#x\n",
2102 pci_protocol_version);
2103 goto exit;
2104 }
2105
2106 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2107 dev_err(&hdev->device,
2108 "PCI Pass-through VSP failed version request: %#x",
2109 comp_pkt.completion_status);
2110 ret = -EPROTO;
2111 goto exit;
2112 }
2113
2114 reinit_completion(&comp_pkt.host_event);
Jake Oshins4daace02016-02-16 21:56:23 +00002115 }
2116
Jork Loeserb1db7e72017-05-24 13:41:27 -07002117 dev_err(&hdev->device,
2118 "PCI pass-through VSP failed to find supported version");
2119 ret = -EPROTO;
Jake Oshins4daace02016-02-16 21:56:23 +00002120
2121exit:
2122 kfree(pkt);
2123 return ret;
2124}
2125
2126/**
2127 * hv_pci_free_bridge_windows() - Release memory regions for the
2128 * bus
2129 * @hbus: Root PCI bus, as understood by this driver
2130 */
2131static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2132{
2133 /*
2134 * Set the resources back to the way they looked when they
2135 * were allocated by setting IORESOURCE_BUSY again.
2136 */
2137
2138 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2139 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
Jake Oshins696ca5e2016-04-05 10:22:52 -07002140 vmbus_free_mmio(hbus->low_mmio_res->start,
2141 resource_size(hbus->low_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002142 }
2143
2144 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2145 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
Jake Oshins696ca5e2016-04-05 10:22:52 -07002146 vmbus_free_mmio(hbus->high_mmio_res->start,
2147 resource_size(hbus->high_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002148 }
2149}
2150
2151/**
2152 * hv_pci_allocate_bridge_windows() - Allocate memory regions
2153 * for the bus
2154 * @hbus: Root PCI bus, as understood by this driver
2155 *
2156 * This function calls vmbus_allocate_mmio(), which is itself a
2157 * bit of a compromise. Ideally, we might change the pnp layer
2158 * in the kernel such that it comprehends either PCI devices
2159 * which are "grandchildren of ACPI," with some intermediate bus
2160 * node (in this case, VMBus) or change it such that it
2161 * understands VMBus. The pnp layer, however, has been declared
2162 * deprecated, and not subject to change.
2163 *
2164 * The workaround, implemented here, is to ask VMBus to allocate
2165 * MMIO space for this bus. VMBus itself knows which ranges are
2166 * appropriate by looking at its own ACPI objects. Then, after
2167 * these ranges are claimed, they're modified to look like they
2168 * would have looked if the ACPI and pnp code had allocated
2169 * bridge windows. These descriptors have to exist in this form
2170 * in order to satisfy the code which will get invoked when the
2171 * endpoint PCI function driver calls request_mem_region() or
2172 * request_mem_region_exclusive().
2173 *
2174 * Return: 0 on success, -errno on failure
2175 */
2176static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2177{
2178 resource_size_t align;
2179 int ret;
2180
2181 if (hbus->low_mmio_space) {
2182 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2183 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2184 (u64)(u32)0xffffffff,
2185 hbus->low_mmio_space,
2186 align, false);
2187 if (ret) {
2188 dev_err(&hbus->hdev->device,
2189 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2190 hbus->low_mmio_space);
2191 return ret;
2192 }
2193
2194 /* Modify this resource to become a bridge window. */
2195 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2196 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
2197 pci_add_resource(&hbus->resources_for_children,
2198 hbus->low_mmio_res);
2199 }
2200
2201 if (hbus->high_mmio_space) {
2202 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2203 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2204 0x100000000, -1,
2205 hbus->high_mmio_space, align,
2206 false);
2207 if (ret) {
2208 dev_err(&hbus->hdev->device,
2209 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2210 hbus->high_mmio_space);
2211 goto release_low_mmio;
2212 }
2213
2214 /* Modify this resource to become a bridge window. */
2215 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
2216 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
2217 pci_add_resource(&hbus->resources_for_children,
2218 hbus->high_mmio_res);
2219 }
2220
2221 return 0;
2222
2223release_low_mmio:
2224 if (hbus->low_mmio_res) {
Jake Oshins696ca5e2016-04-05 10:22:52 -07002225 vmbus_free_mmio(hbus->low_mmio_res->start,
2226 resource_size(hbus->low_mmio_res));
Jake Oshins4daace02016-02-16 21:56:23 +00002227 }
2228
2229 return ret;
2230}
2231
2232/**
2233 * hv_allocate_config_window() - Find MMIO space for PCI Config
2234 * @hbus: Root PCI bus, as understood by this driver
2235 *
2236 * This function claims memory-mapped I/O space for accessing
2237 * configuration space for the functions on this bus.
2238 *
2239 * Return: 0 on success, -errno on failure
2240 */
2241static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
2242{
2243 int ret;
2244
2245 /*
2246 * Set up a region of MMIO space to use for accessing configuration
2247 * space.
2248 */
2249 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
2250 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
2251 if (ret)
2252 return ret;
2253
2254 /*
2255 * vmbus_allocate_mmio() gets used for allocating both device endpoint
2256 * resource claims (those which cannot be overlapped) and the ranges
2257 * which are valid for the children of this bus, which are intended
2258 * to be overlapped by those children. Set the flag on this claim
2259 * meaning that this region can't be overlapped.
2260 */
2261
2262 hbus->mem_config->flags |= IORESOURCE_BUSY;
2263
2264 return 0;
2265}
2266
2267static void hv_free_config_window(struct hv_pcibus_device *hbus)
2268{
Jake Oshins696ca5e2016-04-05 10:22:52 -07002269 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
Jake Oshins4daace02016-02-16 21:56:23 +00002270}
2271
2272/**
2273 * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
2274 * @hdev: VMBus's tracking struct for this root PCI bus
2275 *
2276 * Return: 0 on success, -errno on failure
2277 */
2278static int hv_pci_enter_d0(struct hv_device *hdev)
2279{
2280 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2281 struct pci_bus_d0_entry *d0_entry;
2282 struct hv_pci_compl comp_pkt;
2283 struct pci_packet *pkt;
2284 int ret;
2285
2286 /*
2287 * Tell the host that the bus is ready to use, and moved into the
2288 * powered-on state. This includes telling the host which region
2289 * of memory-mapped I/O space has been chosen for configuration space
2290 * access.
2291 */
2292 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
2293 if (!pkt)
2294 return -ENOMEM;
2295
2296 init_completion(&comp_pkt.host_event);
2297 pkt->completion_func = hv_pci_generic_compl;
2298 pkt->compl_ctxt = &comp_pkt;
2299 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002300 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
Jake Oshins4daace02016-02-16 21:56:23 +00002301 d0_entry->mmio_base = hbus->mem_config->start;
2302
2303 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
2304 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2305 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002306 if (!ret)
2307 ret = wait_for_response(hdev, &comp_pkt.host_event);
2308
Jake Oshins4daace02016-02-16 21:56:23 +00002309 if (ret)
2310 goto exit;
2311
Jake Oshins4daace02016-02-16 21:56:23 +00002312 if (comp_pkt.completion_status < 0) {
2313 dev_err(&hdev->device,
2314 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2315 comp_pkt.completion_status);
2316 ret = -EPROTO;
2317 goto exit;
2318 }
2319
2320 ret = 0;
2321
2322exit:
2323 kfree(pkt);
2324 return ret;
2325}
2326
2327/**
2328 * hv_pci_query_relations() - Ask host to send list of child
2329 * devices
2330 * @hdev: VMBus's tracking struct for this root PCI bus
2331 *
2332 * Return: 0 on success, -errno on failure
2333 */
2334static int hv_pci_query_relations(struct hv_device *hdev)
2335{
2336 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2337 struct pci_message message;
2338 struct completion comp;
2339 int ret;
2340
2341 /* Ask the host to send along the list of child devices */
2342 init_completion(&comp);
2343 if (cmpxchg(&hbus->survey_event, NULL, &comp))
2344 return -ENOTEMPTY;
2345
2346 memset(&message, 0, sizeof(message));
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002347 message.type = PCI_QUERY_BUS_RELATIONS;
Jake Oshins4daace02016-02-16 21:56:23 +00002348
2349 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
2350 0, VM_PKT_DATA_INBAND, 0);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002351 if (!ret)
2352 ret = wait_for_response(hdev, &comp);
Jake Oshins4daace02016-02-16 21:56:23 +00002353
Dexuan Cuic3635da2018-05-23 21:12:01 +00002354 return ret;
Jake Oshins4daace02016-02-16 21:56:23 +00002355}
2356
2357/**
2358 * hv_send_resources_allocated() - Report local resource choices
2359 * @hdev: VMBus's tracking struct for this root PCI bus
2360 *
2361 * The host OS is expecting to be sent a request as a message
2362 * which contains all the resources that the device will use.
2363 * The response contains those same resources, "translated"
2364 * which is to say, the values which should be used by the
2365 * hardware, when it delivers an interrupt. (MMIO resources are
2366 * used in local terms.) This is nice for Windows, and lines up
2367 * with the FDO/PDO split, which doesn't exist in Linux. Linux
2368 * is deeply expecting to scan an emulated PCI configuration
2369 * space. So this message is sent here only to drive the state
2370 * machine on the host forward.
2371 *
2372 * Return: 0 on success, -errno on failure
2373 */
2374static int hv_send_resources_allocated(struct hv_device *hdev)
2375{
2376 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2377 struct pci_resources_assigned *res_assigned;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002378 struct pci_resources_assigned2 *res_assigned2;
Jake Oshins4daace02016-02-16 21:56:23 +00002379 struct hv_pci_compl comp_pkt;
2380 struct hv_pci_dev *hpdev;
2381 struct pci_packet *pkt;
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002382 size_t size_res;
Jake Oshins4daace02016-02-16 21:56:23 +00002383 u32 wslot;
2384 int ret;
2385
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002386 size_res = (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2)
2387 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
2388
2389 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00002390 if (!pkt)
2391 return -ENOMEM;
2392
2393 ret = 0;
2394
2395 for (wslot = 0; wslot < 256; wslot++) {
2396 hpdev = get_pcichild_wslot(hbus, wslot);
2397 if (!hpdev)
2398 continue;
2399
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002400 memset(pkt, 0, sizeof(*pkt) + size_res);
Jake Oshins4daace02016-02-16 21:56:23 +00002401 init_completion(&comp_pkt.host_event);
2402 pkt->completion_func = hv_pci_generic_compl;
2403 pkt->compl_ctxt = &comp_pkt;
Jake Oshins4daace02016-02-16 21:56:23 +00002404
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002405 if (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2) {
2406 res_assigned =
2407 (struct pci_resources_assigned *)&pkt->message;
2408 res_assigned->message_type.type =
2409 PCI_RESOURCES_ASSIGNED;
2410 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
2411 } else {
2412 res_assigned2 =
2413 (struct pci_resources_assigned2 *)&pkt->message;
2414 res_assigned2->message_type.type =
2415 PCI_RESOURCES_ASSIGNED2;
2416 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
2417 }
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002418 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002419
Jork Loeser7dcf90e2017-05-24 13:41:28 -07002420 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
2421 size_res, (unsigned long)pkt,
2422 VM_PKT_DATA_INBAND,
2423 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
Dexuan Cuic3635da2018-05-23 21:12:01 +00002424 if (!ret)
2425 ret = wait_for_response(hdev, &comp_pkt.host_event);
Jake Oshins4daace02016-02-16 21:56:23 +00002426 if (ret)
2427 break;
2428
Jake Oshins4daace02016-02-16 21:56:23 +00002429 if (comp_pkt.completion_status < 0) {
2430 ret = -EPROTO;
2431 dev_err(&hdev->device,
2432 "resource allocated returned 0x%x",
2433 comp_pkt.completion_status);
2434 break;
2435 }
2436 }
2437
2438 kfree(pkt);
2439 return ret;
2440}
2441
2442/**
2443 * hv_send_resources_released() - Report local resources
2444 * released
2445 * @hdev: VMBus's tracking struct for this root PCI bus
2446 *
2447 * Return: 0 on success, -errno on failure
2448 */
2449static int hv_send_resources_released(struct hv_device *hdev)
2450{
2451 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2452 struct pci_child_message pkt;
2453 struct hv_pci_dev *hpdev;
2454 u32 wslot;
2455 int ret;
2456
2457 for (wslot = 0; wslot < 256; wslot++) {
2458 hpdev = get_pcichild_wslot(hbus, wslot);
2459 if (!hpdev)
2460 continue;
2461
2462 memset(&pkt, 0, sizeof(pkt));
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002463 pkt.message_type.type = PCI_RESOURCES_RELEASED;
Jake Oshins4daace02016-02-16 21:56:23 +00002464 pkt.wslot.slot = hpdev->desc.win_slot.slot;
2465
Stephen Hemminger8c99e122018-05-23 10:11:12 -07002466 put_pcichild(hpdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002467
2468 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
2469 VM_PKT_DATA_INBAND, 0);
2470 if (ret)
2471 return ret;
2472 }
2473
2474 return 0;
2475}
2476
2477static void get_hvpcibus(struct hv_pcibus_device *hbus)
2478{
Stephen Hemminger6708be92018-05-23 10:11:13 -07002479 refcount_inc(&hbus->remove_lock);
Jake Oshins4daace02016-02-16 21:56:23 +00002480}
2481
2482static void put_hvpcibus(struct hv_pcibus_device *hbus)
2483{
Stephen Hemminger6708be92018-05-23 10:11:13 -07002484 if (refcount_dec_and_test(&hbus->remove_lock))
Jake Oshins4daace02016-02-16 21:56:23 +00002485 complete(&hbus->remove_event);
2486}
2487
2488/**
2489 * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
2490 * @hdev: VMBus's tracking struct for this root PCI bus
2491 * @dev_id: Identifies the device itself
2492 *
2493 * Return: 0 on success, -errno on failure
2494 */
2495static int hv_pci_probe(struct hv_device *hdev,
2496 const struct hv_vmbus_device_id *dev_id)
2497{
2498 struct hv_pcibus_device *hbus;
2499 int ret;
2500
Jork Loeserbe66b672017-05-24 13:41:25 -07002501 /*
2502 * hv_pcibus_device contains the hypercall arguments for retargeting in
2503 * hv_irq_unmask(). Those must not cross a page boundary.
2504 */
2505 BUILD_BUG_ON(sizeof(*hbus) > PAGE_SIZE);
2506
2507 hbus = (struct hv_pcibus_device *)get_zeroed_page(GFP_KERNEL);
Jake Oshins4daace02016-02-16 21:56:23 +00002508 if (!hbus)
2509 return -ENOMEM;
Long Lid3a78d82017-03-23 14:58:10 -07002510 hbus->state = hv_pcibus_init;
Jake Oshins4daace02016-02-16 21:56:23 +00002511
2512 /*
2513 * The PCI bus "domain" is what is called "segment" in ACPI and
2514 * other specs. Pull it from the instance ID, to get something
2515 * unique. Bytes 8 and 9 are what is used in Windows guests, so
2516 * do the same thing for consistency. Note that, since this code
2517 * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
2518 * that (1) the only domain in use for something that looks like
2519 * a physical PCI bus (which is actually emulated by the
2520 * hypervisor) is domain 0 and (2) there will be no overlap
2521 * between domains derived from these instance IDs in the same
2522 * VM.
2523 */
2524 hbus->sysdata.domain = hdev->dev_instance.b[9] |
2525 hdev->dev_instance.b[8] << 8;
2526
2527 hbus->hdev = hdev;
Stephen Hemminger6708be92018-05-23 10:11:13 -07002528 refcount_set(&hbus->remove_lock, 1);
Jake Oshins4daace02016-02-16 21:56:23 +00002529 INIT_LIST_HEAD(&hbus->children);
2530 INIT_LIST_HEAD(&hbus->dr_list);
2531 INIT_LIST_HEAD(&hbus->resources_for_children);
2532 spin_lock_init(&hbus->config_lock);
2533 spin_lock_init(&hbus->device_list_lock);
Long Li0de8ce32016-11-08 14:04:38 -08002534 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
Jake Oshins4daace02016-02-16 21:56:23 +00002535 init_completion(&hbus->remove_event);
Dexuan Cui021ad272018-03-15 14:20:53 +00002536 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
2537 hbus->sysdata.domain);
2538 if (!hbus->wq) {
2539 ret = -ENOMEM;
2540 goto free_bus;
2541 }
Jake Oshins4daace02016-02-16 21:56:23 +00002542
2543 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
2544 hv_pci_onchannelcallback, hbus);
2545 if (ret)
Dexuan Cui021ad272018-03-15 14:20:53 +00002546 goto destroy_wq;
Jake Oshins4daace02016-02-16 21:56:23 +00002547
2548 hv_set_drvdata(hdev, hbus);
2549
2550 ret = hv_pci_protocol_negotiation(hdev);
2551 if (ret)
2552 goto close;
2553
2554 ret = hv_allocate_config_window(hbus);
2555 if (ret)
2556 goto close;
2557
2558 hbus->cfg_addr = ioremap(hbus->mem_config->start,
2559 PCI_CONFIG_MMIO_LENGTH);
2560 if (!hbus->cfg_addr) {
2561 dev_err(&hdev->device,
2562 "Unable to map a virtual address for config space\n");
2563 ret = -ENOMEM;
2564 goto free_config;
2565 }
2566
2567 hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
2568 if (!hbus->sysdata.fwnode) {
2569 ret = -ENOMEM;
2570 goto unmap;
2571 }
2572
2573 ret = hv_pcie_init_irq_domain(hbus);
2574 if (ret)
2575 goto free_fwnode;
2576
2577 ret = hv_pci_query_relations(hdev);
2578 if (ret)
2579 goto free_irq_domain;
2580
2581 ret = hv_pci_enter_d0(hdev);
2582 if (ret)
2583 goto free_irq_domain;
2584
2585 ret = hv_pci_allocate_bridge_windows(hbus);
2586 if (ret)
2587 goto free_irq_domain;
2588
2589 ret = hv_send_resources_allocated(hdev);
2590 if (ret)
2591 goto free_windows;
2592
2593 prepopulate_bars(hbus);
2594
2595 hbus->state = hv_pcibus_probed;
2596
2597 ret = create_root_hv_pci_bus(hbus);
2598 if (ret)
2599 goto free_windows;
2600
2601 return 0;
2602
2603free_windows:
2604 hv_pci_free_bridge_windows(hbus);
2605free_irq_domain:
2606 irq_domain_remove(hbus->irq_domain);
2607free_fwnode:
2608 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2609unmap:
2610 iounmap(hbus->cfg_addr);
2611free_config:
2612 hv_free_config_window(hbus);
2613close:
2614 vmbus_close(hdev->channel);
Dexuan Cui021ad272018-03-15 14:20:53 +00002615destroy_wq:
2616 destroy_workqueue(hbus->wq);
Jake Oshins4daace02016-02-16 21:56:23 +00002617free_bus:
Jork Loeserbe66b672017-05-24 13:41:25 -07002618 free_page((unsigned long)hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00002619 return ret;
2620}
2621
Dexuan Cui179785242016-11-10 07:18:47 +00002622static void hv_pci_bus_exit(struct hv_device *hdev)
Jake Oshins4daace02016-02-16 21:56:23 +00002623{
Dexuan Cui179785242016-11-10 07:18:47 +00002624 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2625 struct {
Jake Oshins4daace02016-02-16 21:56:23 +00002626 struct pci_packet teardown_packet;
Dexuan Cui179785242016-11-10 07:18:47 +00002627 u8 buffer[sizeof(struct pci_message)];
Jake Oshins4daace02016-02-16 21:56:23 +00002628 } pkt;
2629 struct pci_bus_relations relations;
2630 struct hv_pci_compl comp_pkt;
Dexuan Cui179785242016-11-10 07:18:47 +00002631 int ret;
Jake Oshins4daace02016-02-16 21:56:23 +00002632
Dexuan Cui179785242016-11-10 07:18:47 +00002633 /*
2634 * After the host sends the RESCIND_CHANNEL message, it doesn't
2635 * access the per-channel ringbuffer any longer.
2636 */
2637 if (hdev->channel->rescind)
2638 return;
2639
2640 /* Delete any children which might still exist. */
2641 memset(&relations, 0, sizeof(relations));
2642 hv_pci_devices_present(hbus, &relations);
2643
2644 ret = hv_send_resources_released(hdev);
2645 if (ret)
2646 dev_err(&hdev->device,
2647 "Couldn't send resources released packet(s)\n");
Jake Oshins4daace02016-02-16 21:56:23 +00002648
Jake Oshins4daace02016-02-16 21:56:23 +00002649 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
2650 init_completion(&comp_pkt.host_event);
2651 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
2652 pkt.teardown_packet.compl_ctxt = &comp_pkt;
Dexuan Cui0c6045d2016-08-23 04:45:51 +00002653 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
Jake Oshins4daace02016-02-16 21:56:23 +00002654
2655 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
2656 sizeof(struct pci_message),
2657 (unsigned long)&pkt.teardown_packet,
2658 VM_PKT_DATA_INBAND,
2659 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2660 if (!ret)
2661 wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
Dexuan Cui179785242016-11-10 07:18:47 +00002662}
Jake Oshins4daace02016-02-16 21:56:23 +00002663
Dexuan Cui179785242016-11-10 07:18:47 +00002664/**
2665 * hv_pci_remove() - Remove routine for this VMBus channel
2666 * @hdev: VMBus's tracking struct for this root PCI bus
2667 *
2668 * Return: 0 on success, -errno on failure
2669 */
2670static int hv_pci_remove(struct hv_device *hdev)
2671{
2672 struct hv_pcibus_device *hbus;
2673
2674 hbus = hv_get_drvdata(hdev);
Jake Oshins4daace02016-02-16 21:56:23 +00002675 if (hbus->state == hv_pcibus_installed) {
2676 /* Remove the bus from PCI's point of view. */
2677 pci_lock_rescan_remove();
2678 pci_stop_root_bus(hbus->pci_bus);
2679 pci_remove_root_bus(hbus->pci_bus);
2680 pci_unlock_rescan_remove();
Long Lid3a78d82017-03-23 14:58:10 -07002681 hbus->state = hv_pcibus_removed;
Jake Oshins4daace02016-02-16 21:56:23 +00002682 }
2683
Dexuan Cui179785242016-11-10 07:18:47 +00002684 hv_pci_bus_exit(hdev);
Vitaly Kuznetsovdeb22e52016-04-29 11:39:10 +02002685
Jake Oshins4daace02016-02-16 21:56:23 +00002686 vmbus_close(hdev->channel);
2687
Jake Oshins4daace02016-02-16 21:56:23 +00002688 iounmap(hbus->cfg_addr);
2689 hv_free_config_window(hbus);
2690 pci_free_resource_list(&hbus->resources_for_children);
2691 hv_pci_free_bridge_windows(hbus);
2692 irq_domain_remove(hbus->irq_domain);
2693 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2694 put_hvpcibus(hbus);
2695 wait_for_completion(&hbus->remove_event);
Dexuan Cui021ad272018-03-15 14:20:53 +00002696 destroy_workqueue(hbus->wq);
Jork Loeserbe66b672017-05-24 13:41:25 -07002697 free_page((unsigned long)hbus);
Jake Oshins4daace02016-02-16 21:56:23 +00002698 return 0;
2699}
2700
2701static const struct hv_vmbus_device_id hv_pci_id_table[] = {
2702 /* PCI Pass-through Class ID */
2703 /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
2704 { HV_PCIE_GUID, },
2705 { },
2706};
2707
2708MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
2709
2710static struct hv_driver hv_pci_drv = {
2711 .name = "hv_pci",
2712 .id_table = hv_pci_id_table,
2713 .probe = hv_pci_probe,
2714 .remove = hv_pci_remove,
2715};
2716
2717static void __exit exit_hv_pci_drv(void)
2718{
2719 vmbus_driver_unregister(&hv_pci_drv);
2720}
2721
2722static int __init init_hv_pci_drv(void)
2723{
2724 return vmbus_driver_register(&hv_pci_drv);
2725}
2726
2727module_init(init_hv_pci_drv);
2728module_exit(exit_hv_pci_drv);
2729
2730MODULE_DESCRIPTION("Hyper-V PCI");
2731MODULE_LICENSE("GPL v2");