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