1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40#include <linux/kernel.h>
41#include <linux/module.h>
42#include <linux/pci.h>
43#include <linux/delay.h>
44#include <linux/semaphore.h>
45#include <linux/irqdomain.h>
46#include <asm/irqdomain.h>
47#include <asm/apic.h>
48#include <linux/irq.h>
49#include <linux/msi.h>
50#include <linux/hyperv.h>
51#include <linux/refcount.h>
52#include <asm/mshyperv.h>
53
54
55
56
57
58
59#define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (minor)))
60#define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
61#define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
62
63enum pci_protocol_version_t {
64 PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1),
65 PCI_PROTOCOL_VERSION_1_2 = PCI_MAKE_VERSION(1, 2),
66};
67
68#define CPU_AFFINITY_ALL -1ULL
69
70
71
72
73
74static enum pci_protocol_version_t pci_protocol_versions[] = {
75 PCI_PROTOCOL_VERSION_1_2,
76 PCI_PROTOCOL_VERSION_1_1,
77};
78
79
80
81
82static enum pci_protocol_version_t pci_protocol_version;
83
84#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
90#define STATUS_REVISION_MISMATCH 0xC0000059
91
92
93#define SLOT_NAME_SIZE 11
94
95
96
97
98
99enum pci_message_type {
100
101
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,
124 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,
127 PCI_MESSAGE_MAXIMUM
128};
129
130
131
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
144
145
146
147
148union win_slot_encoding {
149 struct {
150 u32 dev:5;
151 u32 func:3;
152 u32 reserved:24;
153 } bits;
154 u32 slot;
155} __packed;
156
157
158
159
160struct pci_function_description {
161 u16 v_id;
162 u16 d_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;
170} __packed;
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188struct hv_msi_desc {
189 u8 vector;
190 u8 delivery_mode;
191 u16 vector_count;
192 u32 reserved;
193 u64 cpu_mask;
194} __packed;
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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
221
222
223
224
225
226
227
228
229
230
231
232struct tran_int_desc {
233 u16 reserved;
234 u16 vector_count;
235 u32 data;
236 u64 address;
237} __packed;
238
239
240
241
242
243
244struct pci_message {
245 u32 type;
246} __packed;
247
248struct pci_child_message {
249 struct pci_message message_type;
250 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;
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;
267
268 struct pci_message message[0];
269};
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285struct pci_version_request {
286 struct pci_message message_type;
287 u32 protocol_version;
288} __packed;
289
290
291
292
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;
304 struct pci_function_description func[0];
305} __packed;
306
307struct pci_q_res_req_response {
308 struct vmpacket_descriptor hdr;
309 s32 status;
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;
317 u32 reserved;
318} __packed;
319
320struct pci_set_power_response {
321 struct vmpacket_descriptor hdr;
322 s32 status;
323 union win_slot_encoding wslot;
324 u32 resultant_state;
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];
332 u32 msi_descriptors;
333 u32 reserved[4];
334} __packed;
335
336struct pci_resources_assigned2 {
337 struct pci_message message_type;
338 union win_slot_encoding wslot;
339 u8 memory_range[0x14][6];
340 u32 msi_descriptor_count;
341 u8 reserved[70];
342} __packed;
343
344struct 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
356struct 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
362struct 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 {
374 struct pci_message message_type;
375 union win_slot_encoding wslot;
376 u32 status;
377} __packed;
378
379static int pci_ring_size = (4 * PAGE_SIZE);
380
381
382
383
384#define HV_PARTITION_ID_SELF ((u64)-1)
385#define HVCALL_RETARGET_INTERRUPT 0x7e
386
387struct hv_interrupt_entry {
388 u32 source;
389 u32 reserved1;
390 u32 address;
391 u32 data;
392};
393
394
395
396
397#define HV_DEVICE_INTERRUPT_TARGET_MULTICAST 1
398#define HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET 2
399
400struct hv_device_interrupt_target {
401 u32 vector;
402 u32 flags;
403 union {
404 u64 vp_mask;
405 struct hv_vpset vp_set;
406 };
407};
408
409struct retarget_msi_interrupt {
410 u64 partition_id;
411 u64 device_id;
412 struct hv_interrupt_entry int_entry;
413 u64 reserved2;
414 struct hv_device_interrupt_target int_target;
415} __packed __aligned(8);
416
417
418
419
420
421enum hv_pcibus_state {
422 hv_pcibus_init = 0,
423 hv_pcibus_probed,
424 hv_pcibus_installed,
425 hv_pcibus_removed,
426 hv_pcibus_maximum
427};
428
429struct hv_pcibus_device {
430 struct pci_sysdata sysdata;
431 enum hv_pcibus_state state;
432 refcount_t remove_lock;
433 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;
443 spinlock_t device_list_lock;
444 void __iomem *cfg_addr;
445
446 struct list_head resources_for_children;
447
448 struct list_head children;
449 struct list_head dr_list;
450
451 struct msi_domain_info msi_info;
452 struct msi_controller msi_chip;
453 struct irq_domain *irq_domain;
454
455 spinlock_t retarget_msi_interrupt_lock;
456
457 struct workqueue_struct *wq;
458
459
460 struct retarget_msi_interrupt retarget_msi_interrupt_params;
461
462
463
464
465};
466
467
468
469
470
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;
480 struct pci_function_description func[0];
481};
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
491struct hv_pci_dev {
492
493 struct list_head list_entry;
494 refcount_t refs;
495 enum hv_pcichild_state state;
496 struct pci_slot *pci_slot;
497 struct pci_function_description desc;
498 bool reported_missing;
499 struct hv_pcibus_device *hbus;
500 struct work_struct wrk;
501
502
503
504
505
506 u32 probed_bar[6];
507};
508
509struct hv_pci_compl {
510 struct completion host_event;
511 s32 completion_status;
512};
513
514static void hv_pci_onchannelcallback(void *context);
515
516
517
518
519
520
521
522
523
524
525
526static void hv_pci_generic_compl(void *context, struct pci_response *resp,
527 int resp_packet_size)
528{
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;
533 else
534 comp_pkt->completion_status = -1;
535
536 complete(&comp_pkt->host_event);
537}
538
539static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
540 u32 wslot);
541
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}
552
553static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
554static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
555
556
557
558
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
576
577
578
579
580
581
582
583
584static u32 devfn_to_wslot(int devfn)
585{
586 union win_slot_encoding wslot;
587
588 wslot.slot = 0;
589 wslot.bits.dev = PCI_SLOT(devfn);
590 wslot.bits.func = PCI_FUNC(devfn);
591
592 return wslot.slot;
593}
594
595
596
597
598
599
600
601
602
603static int wslot_to_devfn(u32 wslot)
604{
605 union win_slot_encoding slot_no;
606
607 slot_no.slot = wslot;
608 return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
609}
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
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
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
648 *val = 0;
649 } else if (where >= PCI_INTERRUPT_LINE && where + size <=
650 PCI_INTERRUPT_PIN) {
651
652
653
654
655
656 *val = 0;
657 } else if (where + size <= CFG_PAGE_SIZE) {
658 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
659
660 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
661
662 mb();
663
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 }
675
676
677
678
679 mb();
680 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
687static 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
697 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
698
699 mb();
700
701 ret = readw(addr);
702
703
704
705
706
707 spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
708
709 return ret;
710}
711
712
713
714
715
716
717
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
728 } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
729 spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
730
731 writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
732
733 wmb();
734
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 }
746
747
748
749
750 mb();
751 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
760
761
762
763
764
765
766
767
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
782 put_pcichild(hpdev);
783 return PCIBIOS_SUCCESSFUL;
784}
785
786
787
788
789
790
791
792
793
794
795
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
810 put_pcichild(hpdev);
811 return PCIBIOS_SUCCESSFUL;
812}
813
814
815static struct pci_ops hv_pcifront_ops = {
816 .read = hv_pcifront_read_config,
817 .write = hv_pcifront_write_config,
818};
819
820
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;
827 u8 buffer[sizeof(struct pci_delete_interrupt)];
828 } ctxt;
829
830 memset(&ctxt, 0, sizeof(ctxt));
831 int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
832 int_pkt->message_type.type =
833 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
843
844
845
846
847
848
849
850
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;
864 int_desc = irq_data_get_irq_chip_data(irq_data);
865 if (!int_desc)
866 return;
867
868 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;
873 }
874
875 hv_int_desc_free(hpdev, int_desc);
876 put_pcichild(hpdev);
877}
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
887static void hv_irq_mask(struct irq_data *data)
888{
889 pci_msi_mask_irq(data);
890}
891
892
893
894
895
896
897
898
899
900
901
902static void hv_irq_unmask(struct irq_data *data)
903{
904 struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
905 struct irq_cfg *cfg = irqd_cfg(data);
906 struct retarget_msi_interrupt *params;
907 struct hv_pcibus_device *hbus;
908 struct cpumask *dest;
909 cpumask_var_t tmp;
910 struct pci_bus *pbus;
911 struct pci_dev *pdev;
912 unsigned long flags;
913 u32 var_size = 0;
914 int cpu, nr_bank;
915 u64 res;
916
917 dest = irq_data_get_effective_affinity_mask(data);
918 pdev = msi_desc_to_pci_dev(msi_desc);
919 pbus = pdev->bus;
920 hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
921
922 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;
927 params->int_entry.source = 1;
928 params->int_entry.address = msi_desc->msg.address_lo;
929 params->int_entry.data = msi_desc->msg.data;
930 params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
931 (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);
935 params->int_target.vector = cfg->vector;
936
937
938
939
940
941
942
943
944 if (pci_protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
945
946
947
948
949
950
951
952 params->int_target.flags |=
953 HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
954
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(¶ms->int_target.vp_set, tmp);
962 free_cpumask_var(tmp);
963
964 if (nr_bank <= 0) {
965 res = 1;
966 goto exit_unlock;
967 }
968
969
970
971
972
973
974 var_size = 1 + nr_bank;
975 } else {
976 for_each_cpu_and(cpu, dest, cpu_online_mask) {
977 params->int_target.vp_mask |=
978 (1ULL << hv_cpu_number_to_vp_number(cpu));
979 }
980 }
981
982 res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
983 params, NULL);
984
985exit_unlock:
986 spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
987
988 if (res) {
989 dev_err(&hbus->hdev->device,
990 "%s() failed: %#llx", __func__, res);
991 return;
992 }
993
994 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
1014static 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;
1022 int_pkt->int_desc.delivery_mode = dest_Fixed;
1023
1024
1025
1026
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;
1043 int_pkt->int_desc.delivery_mode = dest_Fixed;
1044
1045
1046
1047
1048
1049 cpu = cpumask_first_and(affinity, cpu_online_mask);
1050 int_pkt->int_desc.processor_array[0] =
1051 hv_cpu_number_to_vp_number(cpu);
1052 int_pkt->int_desc.processor_count = 1;
1053
1054 return sizeof(*int_pkt);
1055}
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
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;
1075 struct cpumask *dest;
1076 unsigned long flags;
1077 struct compose_comp_ctxt comp;
1078 struct tran_int_desc *int_desc;
1079 struct {
1080 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;
1088 int ret;
1089
1090 pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
1091 dest = irq_data_get_effective_affinity_mask(data);
1092 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
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
1105 int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
1106 if (!int_desc)
1107 goto drop_reference;
1108
1109 memset(&ctxt, 0, sizeof(ctxt));
1110 init_completion(&comp.comp_pkt.host_event);
1111 ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
1112 ctxt.pci_pkt.compl_ctxt = ∁
1113
1114 switch (pci_protocol_version) {
1115 case PCI_PROTOCOL_VERSION_1_1:
1116 size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
1117 dest,
1118 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,
1124 dest,
1125 hpdev->desc.win_slot.slot,
1126 cfg->vector);
1127 break;
1128
1129 default:
1130
1131
1132
1133
1134 dev_err(&hbus->hdev->device,
1135 "Unexpected vPCI protocol, update driver.");
1136 goto free_int_desc;
1137 }
1138
1139 ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
1140 size, (unsigned long)&ctxt.pci_pkt,
1141 VM_PKT_DATA_INBAND,
1142 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1143 if (ret) {
1144 dev_err(&hbus->hdev->device,
1145 "Sending request for interrupt failed: 0x%x",
1146 comp.comp_pkt.completion_status);
1147 goto free_int_desc;
1148 }
1149
1150
1151
1152
1153
1154 while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
1155
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
1164
1165
1166
1167
1168
1169
1170
1171 local_irq_save(flags);
1172
1173 if (hbus->hdev->channel->target_cpu == smp_processor_id())
1174 hv_pci_onchannelcallback(hbus);
1175
1176 local_irq_restore(flags);
1177
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
1184 udelay(100);
1185 }
1186
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
1196
1197
1198
1199 *int_desc = comp.int_desc;
1200 data->chip_data = int_desc;
1201
1202
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
1207 put_pcichild(hpdev);
1208 return;
1209
1210free_int_desc:
1211 kfree(int_desc);
1212drop_reference:
1213 put_pcichild(hpdev);
1214return_null_message:
1215 msg->address_hi = 0;
1216 msg->address_lo = 0;
1217 msg->data = 0;
1218}
1219
1220
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
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
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
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
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
1300
1301
1302static void survey_child_resources(struct hv_pcibus_device *hbus)
1303{
1304 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
1312 event = xchg(&hbus->survey_event, NULL);
1313 if (!event)
1314 return;
1315
1316
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
1326
1327
1328
1329 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1330 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
1338
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
1364
1365
1366
1367
1368
1369
1370
1371
1372
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;
1382 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
1402 do {
1403 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1404 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
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
1459
1460
1461
1462
1463
1464
1465
1466
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);
1482 if (IS_ERR(hpdev->pci_slot)) {
1483 pr_warn("pci_create slot %s failed\n", name);
1484 hpdev->pci_slot = NULL;
1485 }
1486 }
1487}
1488
1489
1490
1491
1492static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
1493{
1494 struct hv_pci_dev *hpdev;
1495
1496 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1497 if (!hpdev->pci_slot)
1498 continue;
1499 pci_destroy_slot(hpdev->pci_slot);
1500 hpdev->pci_slot = NULL;
1501 }
1502}
1503
1504
1505
1506
1507
1508
1509
1510static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
1511{
1512
1513 hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
1514 0,
1515 &hv_pcifront_ops,
1516 &hbus->sysdata,
1517 &hbus->resources_for_children);
1518 if (!hbus->pci_bus)
1519 return -ENODEV;
1520
1521 hbus->pci_bus->msi = &hbus->msi_chip;
1522 hbus->pci_bus->msi->dev = &hbus->hdev->device;
1523
1524 pci_lock_rescan_remove();
1525 pci_scan_child_bus(hbus->pci_bus);
1526 pci_bus_assign_resources(hbus->pci_bus);
1527 hv_pci_assign_slots(hbus);
1528 pci_bus_add_devices(hbus->pci_bus);
1529 pci_unlock_rescan_remove();
1530 hbus->state = hv_pcibus_installed;
1531 return 0;
1532}
1533
1534struct q_res_req_compl {
1535 struct completion host_event;
1536 struct hv_pci_dev *hpdev;
1537};
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548static void q_resource_requirements(void *context, struct pci_response *resp,
1549 int resp_packet_size)
1550{
1551 struct q_res_req_compl *completion = context;
1552 struct pci_q_res_req_response *q_res_req =
1553 (struct pci_q_res_req_response *)resp;
1554 int i;
1555
1556 if (resp->status < 0) {
1557 dev_err(&completion->hpdev->hbus->hdev->device,
1558 "query resource requirements failed: %x\n",
1559 resp->status);
1560 } else {
1561 for (i = 0; i < 6; i++) {
1562 completion->hpdev->probed_bar[i] =
1563 q_res_req->probed_bar[i];
1564 }
1565 }
1566
1567 complete(&completion->host_event);
1568}
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
1582 struct pci_function_description *desc)
1583{
1584 struct hv_pci_dev *hpdev;
1585 struct pci_child_message *res_req;
1586 struct q_res_req_compl comp_pkt;
1587 struct {
1588 struct pci_packet init_packet;
1589 u8 buffer[sizeof(struct pci_child_message)];
1590 } pkt;
1591 unsigned long flags;
1592 int ret;
1593
1594 hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
1595 if (!hpdev)
1596 return NULL;
1597
1598 hpdev->hbus = hbus;
1599
1600 memset(&pkt, 0, sizeof(pkt));
1601 init_completion(&comp_pkt.host_event);
1602 comp_pkt.hpdev = hpdev;
1603 pkt.init_packet.compl_ctxt = &comp_pkt;
1604 pkt.init_packet.completion_func = q_resource_requirements;
1605 res_req = (struct pci_child_message *)&pkt.init_packet.message;
1606 res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
1607 res_req->wslot.slot = desc->win_slot.slot;
1608
1609 ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
1610 sizeof(struct pci_child_message),
1611 (unsigned long)&pkt.init_packet,
1612 VM_PKT_DATA_INBAND,
1613 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
1614 if (ret)
1615 goto error;
1616
1617 if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
1618 goto error;
1619
1620 hpdev->desc = *desc;
1621 refcount_set(&hpdev->refs, 1);
1622 get_pcichild(hpdev);
1623 spin_lock_irqsave(&hbus->device_list_lock, flags);
1624
1625 list_add_tail(&hpdev->list_entry, &hbus->children);
1626 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1627 return hpdev;
1628
1629error:
1630 kfree(hpdev);
1631 return NULL;
1632}
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
1648 u32 wslot)
1649{
1650 unsigned long flags;
1651 struct hv_pci_dev *iter, *hpdev = NULL;
1652
1653 spin_lock_irqsave(&hbus->device_list_lock, flags);
1654 list_for_each_entry(iter, &hbus->children, list_entry) {
1655 if (iter->desc.win_slot.slot == wslot) {
1656 hpdev = iter;
1657 get_pcichild(hpdev);
1658 break;
1659 }
1660 }
1661 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1662
1663 return hpdev;
1664}
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689static void pci_devices_present_work(struct work_struct *work)
1690{
1691 u32 child_no;
1692 bool found;
1693 struct pci_function_description *new_desc;
1694 struct hv_pci_dev *hpdev;
1695 struct hv_pcibus_device *hbus;
1696 struct list_head removed;
1697 struct hv_dr_work *dr_wrk;
1698 struct hv_dr_state *dr = NULL;
1699 unsigned long flags;
1700
1701 dr_wrk = container_of(work, struct hv_dr_work, wrk);
1702 hbus = dr_wrk->bus;
1703 kfree(dr_wrk);
1704
1705 INIT_LIST_HEAD(&removed);
1706
1707
1708 spin_lock_irqsave(&hbus->device_list_lock, flags);
1709 while (!list_empty(&hbus->dr_list)) {
1710 dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
1711 list_entry);
1712 list_del(&dr->list_entry);
1713
1714
1715 if (!list_empty(&hbus->dr_list)) {
1716 kfree(dr);
1717 continue;
1718 }
1719 }
1720 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1721
1722 if (!dr) {
1723 put_hvpcibus(hbus);
1724 return;
1725 }
1726
1727
1728 spin_lock_irqsave(&hbus->device_list_lock, flags);
1729 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1730 hpdev->reported_missing = true;
1731 }
1732 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1733
1734
1735 for (child_no = 0; child_no < dr->device_count; child_no++) {
1736 found = false;
1737 new_desc = &dr->func[child_no];
1738
1739 spin_lock_irqsave(&hbus->device_list_lock, flags);
1740 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1741 if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
1742 (hpdev->desc.v_id == new_desc->v_id) &&
1743 (hpdev->desc.d_id == new_desc->d_id) &&
1744 (hpdev->desc.ser == new_desc->ser)) {
1745 hpdev->reported_missing = false;
1746 found = true;
1747 }
1748 }
1749 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1750
1751 if (!found) {
1752 hpdev = new_pcichild_device(hbus, new_desc);
1753 if (!hpdev)
1754 dev_err(&hbus->hdev->device,
1755 "couldn't record a child device.\n");
1756 }
1757 }
1758
1759
1760 spin_lock_irqsave(&hbus->device_list_lock, flags);
1761 do {
1762 found = false;
1763 list_for_each_entry(hpdev, &hbus->children, list_entry) {
1764 if (hpdev->reported_missing) {
1765 found = true;
1766 put_pcichild(hpdev);
1767 list_move_tail(&hpdev->list_entry, &removed);
1768 break;
1769 }
1770 }
1771 } while (found);
1772 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1773
1774
1775 while (!list_empty(&removed)) {
1776 hpdev = list_first_entry(&removed, struct hv_pci_dev,
1777 list_entry);
1778 list_del(&hpdev->list_entry);
1779
1780 if (hpdev->pci_slot)
1781 pci_destroy_slot(hpdev->pci_slot);
1782
1783 put_pcichild(hpdev);
1784 }
1785
1786 switch (hbus->state) {
1787 case hv_pcibus_installed:
1788
1789
1790
1791
1792 pci_lock_rescan_remove();
1793 pci_scan_child_bus(hbus->pci_bus);
1794 hv_pci_assign_slots(hbus);
1795 pci_unlock_rescan_remove();
1796 break;
1797
1798 case hv_pcibus_init:
1799 case hv_pcibus_probed:
1800 survey_child_resources(hbus);
1801 break;
1802
1803 default:
1804 break;
1805 }
1806
1807 put_hvpcibus(hbus);
1808 kfree(dr);
1809}
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
1820 struct pci_bus_relations *relations)
1821{
1822 struct hv_dr_state *dr;
1823 struct hv_dr_work *dr_wrk;
1824 unsigned long flags;
1825 bool pending_dr;
1826
1827 dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
1828 if (!dr_wrk)
1829 return;
1830
1831 dr = kzalloc(offsetof(struct hv_dr_state, func) +
1832 (sizeof(struct pci_function_description) *
1833 (relations->device_count)), GFP_NOWAIT);
1834 if (!dr) {
1835 kfree(dr_wrk);
1836 return;
1837 }
1838
1839 INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
1840 dr_wrk->bus = hbus;
1841 dr->device_count = relations->device_count;
1842 if (dr->device_count != 0) {
1843 memcpy(dr->func, relations->func,
1844 sizeof(struct pci_function_description) *
1845 dr->device_count);
1846 }
1847
1848 spin_lock_irqsave(&hbus->device_list_lock, flags);
1849
1850
1851
1852
1853
1854 pending_dr = !list_empty(&hbus->dr_list);
1855 list_add_tail(&dr->list_entry, &hbus->dr_list);
1856 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1857
1858 if (pending_dr) {
1859 kfree(dr_wrk);
1860 } else {
1861 get_hvpcibus(hbus);
1862 queue_work(hbus->wq, &dr_wrk->wrk);
1863 }
1864}
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875static void hv_eject_device_work(struct work_struct *work)
1876{
1877 struct pci_eject_response *ejct_pkt;
1878 struct hv_pcibus_device *hbus;
1879 struct hv_pci_dev *hpdev;
1880 struct pci_dev *pdev;
1881 unsigned long flags;
1882 int wslot;
1883 struct {
1884 struct pci_packet pkt;
1885 u8 buffer[sizeof(struct pci_eject_response)];
1886 } ctxt;
1887
1888 hpdev = container_of(work, struct hv_pci_dev, wrk);
1889 hbus = hpdev->hbus;
1890
1891 WARN_ON(hpdev->state != hv_pcichild_ejecting);
1892
1893
1894
1895
1896
1897
1898
1899 wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
1900 pdev = pci_get_domain_bus_and_slot(hbus->sysdata.domain, 0, wslot);
1901 if (pdev) {
1902 pci_lock_rescan_remove();
1903 pci_stop_and_remove_bus_device(pdev);
1904 pci_dev_put(pdev);
1905 pci_unlock_rescan_remove();
1906 }
1907
1908 spin_lock_irqsave(&hbus->device_list_lock, flags);
1909 list_del(&hpdev->list_entry);
1910 spin_unlock_irqrestore(&hbus->device_list_lock, flags);
1911
1912 if (hpdev->pci_slot)
1913 pci_destroy_slot(hpdev->pci_slot);
1914
1915 memset(&ctxt, 0, sizeof(ctxt));
1916 ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
1917 ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
1918 ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
1919 vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
1920 sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
1921 VM_PKT_DATA_INBAND, 0);
1922
1923
1924 put_pcichild(hpdev);
1925
1926 put_pcichild(hpdev);
1927 put_pcichild(hpdev);
1928
1929
1930 put_hvpcibus(hbus);
1931}
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
1942{
1943 hpdev->state = hv_pcichild_ejecting;
1944 get_pcichild(hpdev);
1945 INIT_WORK(&hpdev->wrk, hv_eject_device_work);
1946 get_hvpcibus(hpdev->hbus);
1947 queue_work(hpdev->hbus->wq, &hpdev->wrk);
1948}
1949
1950
1951
1952
1953
1954
1955
1956
1957static void hv_pci_onchannelcallback(void *context)
1958{
1959 const int packet_size = 0x100;
1960 int ret;
1961 struct hv_pcibus_device *hbus = context;
1962 u32 bytes_recvd;
1963 u64 req_id;
1964 struct vmpacket_descriptor *desc;
1965 unsigned char *buffer;
1966 int bufferlen = packet_size;
1967 struct pci_packet *comp_packet;
1968 struct pci_response *response;
1969 struct pci_incoming_message *new_message;
1970 struct pci_bus_relations *bus_rel;
1971 struct pci_dev_incoming *dev_message;
1972 struct hv_pci_dev *hpdev;
1973
1974 buffer = kmalloc(bufferlen, GFP_ATOMIC);
1975 if (!buffer)
1976 return;
1977
1978 while (1) {
1979 ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
1980 bufferlen, &bytes_recvd, &req_id);
1981
1982 if (ret == -ENOBUFS) {
1983 kfree(buffer);
1984
1985 bufferlen = bytes_recvd;
1986 buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
1987 if (!buffer)
1988 return;
1989 continue;
1990 }
1991
1992
1993 if (ret || !bytes_recvd)
1994 break;
1995
1996
1997
1998
1999
2000 if (bytes_recvd <= sizeof(struct pci_response))
2001 continue;
2002 desc = (struct vmpacket_descriptor *)buffer;
2003
2004 switch (desc->type) {
2005 case VM_PKT_COMP:
2006
2007
2008
2009
2010
2011 comp_packet = (struct pci_packet *)req_id;
2012 response = (struct pci_response *)buffer;
2013 comp_packet->completion_func(comp_packet->compl_ctxt,
2014 response,
2015 bytes_recvd);
2016 break;
2017
2018 case VM_PKT_DATA_INBAND:
2019
2020 new_message = (struct pci_incoming_message *)buffer;
2021 switch (new_message->message_type.type) {
2022 case PCI_BUS_RELATIONS:
2023
2024 bus_rel = (struct pci_bus_relations *)buffer;
2025 if (bytes_recvd <
2026 offsetof(struct pci_bus_relations, func) +
2027 (sizeof(struct pci_function_description) *
2028 (bus_rel->device_count))) {
2029 dev_err(&hbus->hdev->device,
2030 "bus relations too small\n");
2031 break;
2032 }
2033
2034 hv_pci_devices_present(hbus, bus_rel);
2035 break;
2036
2037 case PCI_EJECT:
2038
2039 dev_message = (struct pci_dev_incoming *)buffer;
2040 hpdev = get_pcichild_wslot(hbus,
2041 dev_message->wslot.slot);
2042 if (hpdev) {
2043 hv_pci_eject_device(hpdev);
2044 put_pcichild(hpdev);
2045 }
2046 break;
2047
2048 default:
2049 dev_warn(&hbus->hdev->device,
2050 "Unimplemented protocol message %x\n",
2051 new_message->message_type.type);
2052 break;
2053 }
2054 break;
2055
2056 default:
2057 dev_err(&hbus->hdev->device,
2058 "unhandled packet type %d, tid %llx len %d\n",
2059 desc->type, req_id, bytes_recvd);
2060 break;
2061 }
2062 }
2063
2064 kfree(buffer);
2065}
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083static int hv_pci_protocol_negotiation(struct hv_device *hdev)
2084{
2085 struct pci_version_request *version_req;
2086 struct hv_pci_compl comp_pkt;
2087 struct pci_packet *pkt;
2088 int ret;
2089 int i;
2090
2091
2092
2093
2094
2095
2096
2097 pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
2098 if (!pkt)
2099 return -ENOMEM;
2100
2101 init_completion(&comp_pkt.host_event);
2102 pkt->completion_func = hv_pci_generic_compl;
2103 pkt->compl_ctxt = &comp_pkt;
2104 version_req = (struct pci_version_request *)&pkt->message;
2105 version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
2106
2107 for (i = 0; i < ARRAY_SIZE(pci_protocol_versions); i++) {
2108 version_req->protocol_version = pci_protocol_versions[i];
2109 ret = vmbus_sendpacket(hdev->channel, version_req,
2110 sizeof(struct pci_version_request),
2111 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2112 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2113 if (!ret)
2114 ret = wait_for_response(hdev, &comp_pkt.host_event);
2115
2116 if (ret) {
2117 dev_err(&hdev->device,
2118 "PCI Pass-through VSP failed to request version: %d",
2119 ret);
2120 goto exit;
2121 }
2122
2123 if (comp_pkt.completion_status >= 0) {
2124 pci_protocol_version = pci_protocol_versions[i];
2125 dev_info(&hdev->device,
2126 "PCI VMBus probing: Using version %#x\n",
2127 pci_protocol_version);
2128 goto exit;
2129 }
2130
2131 if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
2132 dev_err(&hdev->device,
2133 "PCI Pass-through VSP failed version request: %#x",
2134 comp_pkt.completion_status);
2135 ret = -EPROTO;
2136 goto exit;
2137 }
2138
2139 reinit_completion(&comp_pkt.host_event);
2140 }
2141
2142 dev_err(&hdev->device,
2143 "PCI pass-through VSP failed to find supported version");
2144 ret = -EPROTO;
2145
2146exit:
2147 kfree(pkt);
2148 return ret;
2149}
2150
2151
2152
2153
2154
2155
2156static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
2157{
2158
2159
2160
2161
2162
2163 if (hbus->low_mmio_space && hbus->low_mmio_res) {
2164 hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
2165 vmbus_free_mmio(hbus->low_mmio_res->start,
2166 resource_size(hbus->low_mmio_res));
2167 }
2168
2169 if (hbus->high_mmio_space && hbus->high_mmio_res) {
2170 hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
2171 vmbus_free_mmio(hbus->high_mmio_res->start,
2172 resource_size(hbus->high_mmio_res));
2173 }
2174}
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
2202{
2203 resource_size_t align;
2204 int ret;
2205
2206 if (hbus->low_mmio_space) {
2207 align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
2208 ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
2209 (u64)(u32)0xffffffff,
2210 hbus->low_mmio_space,
2211 align, false);
2212 if (ret) {
2213 dev_err(&hbus->hdev->device,
2214 "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
2215 hbus->low_mmio_space);
2216 return ret;
2217 }
2218
2219
2220 hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
2221 hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
2222 pci_add_resource(&hbus->resources_for_children,
2223 hbus->low_mmio_res);
2224 }
2225
2226 if (hbus->high_mmio_space) {
2227 align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
2228 ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
2229 0x100000000, -1,
2230 hbus->high_mmio_space, align,
2231 false);
2232 if (ret) {
2233 dev_err(&hbus->hdev->device,
2234 "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
2235 hbus->high_mmio_space);
2236 goto release_low_mmio;
2237 }
2238
2239
2240 hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
2241 hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
2242 pci_add_resource(&hbus->resources_for_children,
2243 hbus->high_mmio_res);
2244 }
2245
2246 return 0;
2247
2248release_low_mmio:
2249 if (hbus->low_mmio_res) {
2250 vmbus_free_mmio(hbus->low_mmio_res->start,
2251 resource_size(hbus->low_mmio_res));
2252 }
2253
2254 return ret;
2255}
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
2267{
2268 int ret;
2269
2270
2271
2272
2273
2274 ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
2275 PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
2276 if (ret)
2277 return ret;
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287 hbus->mem_config->flags |= IORESOURCE_BUSY;
2288
2289 return 0;
2290}
2291
2292static void hv_free_config_window(struct hv_pcibus_device *hbus)
2293{
2294 vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
2295}
2296
2297
2298
2299
2300
2301
2302
2303static int hv_pci_enter_d0(struct hv_device *hdev)
2304{
2305 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2306 struct pci_bus_d0_entry *d0_entry;
2307 struct hv_pci_compl comp_pkt;
2308 struct pci_packet *pkt;
2309 int ret;
2310
2311
2312
2313
2314
2315
2316
2317 pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
2318 if (!pkt)
2319 return -ENOMEM;
2320
2321 init_completion(&comp_pkt.host_event);
2322 pkt->completion_func = hv_pci_generic_compl;
2323 pkt->compl_ctxt = &comp_pkt;
2324 d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
2325 d0_entry->message_type.type = PCI_BUS_D0ENTRY;
2326 d0_entry->mmio_base = hbus->mem_config->start;
2327
2328 ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
2329 (unsigned long)pkt, VM_PKT_DATA_INBAND,
2330 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2331 if (!ret)
2332 ret = wait_for_response(hdev, &comp_pkt.host_event);
2333
2334 if (ret)
2335 goto exit;
2336
2337 if (comp_pkt.completion_status < 0) {
2338 dev_err(&hdev->device,
2339 "PCI Pass-through VSP failed D0 Entry with status %x\n",
2340 comp_pkt.completion_status);
2341 ret = -EPROTO;
2342 goto exit;
2343 }
2344
2345 ret = 0;
2346
2347exit:
2348 kfree(pkt);
2349 return ret;
2350}
2351
2352
2353
2354
2355
2356
2357
2358
2359static int hv_pci_query_relations(struct hv_device *hdev)
2360{
2361 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2362 struct pci_message message;
2363 struct completion comp;
2364 int ret;
2365
2366
2367 init_completion(&comp);
2368 if (cmpxchg(&hbus->survey_event, NULL, &comp))
2369 return -ENOTEMPTY;
2370
2371 memset(&message, 0, sizeof(message));
2372 message.type = PCI_QUERY_BUS_RELATIONS;
2373
2374 ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
2375 0, VM_PKT_DATA_INBAND, 0);
2376 if (!ret)
2377 ret = wait_for_response(hdev, &comp);
2378
2379 return ret;
2380}
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399static int hv_send_resources_allocated(struct hv_device *hdev)
2400{
2401 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2402 struct pci_resources_assigned *res_assigned;
2403 struct pci_resources_assigned2 *res_assigned2;
2404 struct hv_pci_compl comp_pkt;
2405 struct hv_pci_dev *hpdev;
2406 struct pci_packet *pkt;
2407 size_t size_res;
2408 u32 wslot;
2409 int ret;
2410
2411 size_res = (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2)
2412 ? sizeof(*res_assigned) : sizeof(*res_assigned2);
2413
2414 pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
2415 if (!pkt)
2416 return -ENOMEM;
2417
2418 ret = 0;
2419
2420 for (wslot = 0; wslot < 256; wslot++) {
2421 hpdev = get_pcichild_wslot(hbus, wslot);
2422 if (!hpdev)
2423 continue;
2424
2425 memset(pkt, 0, sizeof(*pkt) + size_res);
2426 init_completion(&comp_pkt.host_event);
2427 pkt->completion_func = hv_pci_generic_compl;
2428 pkt->compl_ctxt = &comp_pkt;
2429
2430 if (pci_protocol_version < PCI_PROTOCOL_VERSION_1_2) {
2431 res_assigned =
2432 (struct pci_resources_assigned *)&pkt->message;
2433 res_assigned->message_type.type =
2434 PCI_RESOURCES_ASSIGNED;
2435 res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
2436 } else {
2437 res_assigned2 =
2438 (struct pci_resources_assigned2 *)&pkt->message;
2439 res_assigned2->message_type.type =
2440 PCI_RESOURCES_ASSIGNED2;
2441 res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
2442 }
2443 put_pcichild(hpdev);
2444
2445 ret = vmbus_sendpacket(hdev->channel, &pkt->message,
2446 size_res, (unsigned long)pkt,
2447 VM_PKT_DATA_INBAND,
2448 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2449 if (!ret)
2450 ret = wait_for_response(hdev, &comp_pkt.host_event);
2451 if (ret)
2452 break;
2453
2454 if (comp_pkt.completion_status < 0) {
2455 ret = -EPROTO;
2456 dev_err(&hdev->device,
2457 "resource allocated returned 0x%x",
2458 comp_pkt.completion_status);
2459 break;
2460 }
2461 }
2462
2463 kfree(pkt);
2464 return ret;
2465}
2466
2467
2468
2469
2470
2471
2472
2473
2474static int hv_send_resources_released(struct hv_device *hdev)
2475{
2476 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2477 struct pci_child_message pkt;
2478 struct hv_pci_dev *hpdev;
2479 u32 wslot;
2480 int ret;
2481
2482 for (wslot = 0; wslot < 256; wslot++) {
2483 hpdev = get_pcichild_wslot(hbus, wslot);
2484 if (!hpdev)
2485 continue;
2486
2487 memset(&pkt, 0, sizeof(pkt));
2488 pkt.message_type.type = PCI_RESOURCES_RELEASED;
2489 pkt.wslot.slot = hpdev->desc.win_slot.slot;
2490
2491 put_pcichild(hpdev);
2492
2493 ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
2494 VM_PKT_DATA_INBAND, 0);
2495 if (ret)
2496 return ret;
2497 }
2498
2499 return 0;
2500}
2501
2502static void get_hvpcibus(struct hv_pcibus_device *hbus)
2503{
2504 refcount_inc(&hbus->remove_lock);
2505}
2506
2507static void put_hvpcibus(struct hv_pcibus_device *hbus)
2508{
2509 if (refcount_dec_and_test(&hbus->remove_lock))
2510 complete(&hbus->remove_event);
2511}
2512
2513
2514
2515
2516
2517
2518
2519
2520static int hv_pci_probe(struct hv_device *hdev,
2521 const struct hv_vmbus_device_id *dev_id)
2522{
2523 struct hv_pcibus_device *hbus;
2524 int ret;
2525
2526
2527
2528
2529
2530 BUILD_BUG_ON(sizeof(*hbus) > PAGE_SIZE);
2531
2532 hbus = (struct hv_pcibus_device *)get_zeroed_page(GFP_KERNEL);
2533 if (!hbus)
2534 return -ENOMEM;
2535 hbus->state = hv_pcibus_init;
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549 hbus->sysdata.domain = hdev->dev_instance.b[9] |
2550 hdev->dev_instance.b[8] << 8;
2551
2552 hbus->hdev = hdev;
2553 refcount_set(&hbus->remove_lock, 1);
2554 INIT_LIST_HEAD(&hbus->children);
2555 INIT_LIST_HEAD(&hbus->dr_list);
2556 INIT_LIST_HEAD(&hbus->resources_for_children);
2557 spin_lock_init(&hbus->config_lock);
2558 spin_lock_init(&hbus->device_list_lock);
2559 spin_lock_init(&hbus->retarget_msi_interrupt_lock);
2560 init_completion(&hbus->remove_event);
2561 hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
2562 hbus->sysdata.domain);
2563 if (!hbus->wq) {
2564 ret = -ENOMEM;
2565 goto free_bus;
2566 }
2567
2568 ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
2569 hv_pci_onchannelcallback, hbus);
2570 if (ret)
2571 goto destroy_wq;
2572
2573 hv_set_drvdata(hdev, hbus);
2574
2575 ret = hv_pci_protocol_negotiation(hdev);
2576 if (ret)
2577 goto close;
2578
2579 ret = hv_allocate_config_window(hbus);
2580 if (ret)
2581 goto close;
2582
2583 hbus->cfg_addr = ioremap(hbus->mem_config->start,
2584 PCI_CONFIG_MMIO_LENGTH);
2585 if (!hbus->cfg_addr) {
2586 dev_err(&hdev->device,
2587 "Unable to map a virtual address for config space\n");
2588 ret = -ENOMEM;
2589 goto free_config;
2590 }
2591
2592 hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
2593 if (!hbus->sysdata.fwnode) {
2594 ret = -ENOMEM;
2595 goto unmap;
2596 }
2597
2598 ret = hv_pcie_init_irq_domain(hbus);
2599 if (ret)
2600 goto free_fwnode;
2601
2602 ret = hv_pci_query_relations(hdev);
2603 if (ret)
2604 goto free_irq_domain;
2605
2606 ret = hv_pci_enter_d0(hdev);
2607 if (ret)
2608 goto free_irq_domain;
2609
2610 ret = hv_pci_allocate_bridge_windows(hbus);
2611 if (ret)
2612 goto free_irq_domain;
2613
2614 ret = hv_send_resources_allocated(hdev);
2615 if (ret)
2616 goto free_windows;
2617
2618 prepopulate_bars(hbus);
2619
2620 hbus->state = hv_pcibus_probed;
2621
2622 ret = create_root_hv_pci_bus(hbus);
2623 if (ret)
2624 goto free_windows;
2625
2626 return 0;
2627
2628free_windows:
2629 hv_pci_free_bridge_windows(hbus);
2630free_irq_domain:
2631 irq_domain_remove(hbus->irq_domain);
2632free_fwnode:
2633 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2634unmap:
2635 iounmap(hbus->cfg_addr);
2636free_config:
2637 hv_free_config_window(hbus);
2638close:
2639 vmbus_close(hdev->channel);
2640destroy_wq:
2641 destroy_workqueue(hbus->wq);
2642free_bus:
2643 free_page((unsigned long)hbus);
2644 return ret;
2645}
2646
2647static void hv_pci_bus_exit(struct hv_device *hdev)
2648{
2649 struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
2650 struct {
2651 struct pci_packet teardown_packet;
2652 u8 buffer[sizeof(struct pci_message)];
2653 } pkt;
2654 struct pci_bus_relations relations;
2655 struct hv_pci_compl comp_pkt;
2656 int ret;
2657
2658
2659
2660
2661
2662 if (hdev->channel->rescind)
2663 return;
2664
2665
2666 memset(&relations, 0, sizeof(relations));
2667 hv_pci_devices_present(hbus, &relations);
2668
2669 ret = hv_send_resources_released(hdev);
2670 if (ret)
2671 dev_err(&hdev->device,
2672 "Couldn't send resources released packet(s)\n");
2673
2674 memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
2675 init_completion(&comp_pkt.host_event);
2676 pkt.teardown_packet.completion_func = hv_pci_generic_compl;
2677 pkt.teardown_packet.compl_ctxt = &comp_pkt;
2678 pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
2679
2680 ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
2681 sizeof(struct pci_message),
2682 (unsigned long)&pkt.teardown_packet,
2683 VM_PKT_DATA_INBAND,
2684 VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
2685 if (!ret)
2686 wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
2687}
2688
2689
2690
2691
2692
2693
2694
2695static int hv_pci_remove(struct hv_device *hdev)
2696{
2697 struct hv_pcibus_device *hbus;
2698
2699 hbus = hv_get_drvdata(hdev);
2700 if (hbus->state == hv_pcibus_installed) {
2701
2702 pci_lock_rescan_remove();
2703 pci_stop_root_bus(hbus->pci_bus);
2704 pci_remove_root_bus(hbus->pci_bus);
2705 hv_pci_remove_slots(hbus);
2706 pci_unlock_rescan_remove();
2707 hbus->state = hv_pcibus_removed;
2708 }
2709
2710 hv_pci_bus_exit(hdev);
2711
2712 vmbus_close(hdev->channel);
2713
2714 iounmap(hbus->cfg_addr);
2715 hv_free_config_window(hbus);
2716 pci_free_resource_list(&hbus->resources_for_children);
2717 hv_pci_free_bridge_windows(hbus);
2718 irq_domain_remove(hbus->irq_domain);
2719 irq_domain_free_fwnode(hbus->sysdata.fwnode);
2720 put_hvpcibus(hbus);
2721 wait_for_completion(&hbus->remove_event);
2722 destroy_workqueue(hbus->wq);
2723 free_page((unsigned long)hbus);
2724 return 0;
2725}
2726
2727static const struct hv_vmbus_device_id hv_pci_id_table[] = {
2728
2729
2730 { HV_PCIE_GUID, },
2731 { },
2732};
2733
2734MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
2735
2736static struct hv_driver hv_pci_drv = {
2737 .name = "hv_pci",
2738 .id_table = hv_pci_id_table,
2739 .probe = hv_pci_probe,
2740 .remove = hv_pci_remove,
2741};
2742
2743static void __exit exit_hv_pci_drv(void)
2744{
2745 vmbus_driver_unregister(&hv_pci_drv);
2746}
2747
2748static int __init init_hv_pci_drv(void)
2749{
2750 return vmbus_driver_register(&hv_pci_drv);
2751}
2752
2753module_init(init_hv_pci_drv);
2754module_exit(exit_hv_pci_drv);
2755
2756MODULE_DESCRIPTION("Hyper-V PCI");
2757MODULE_LICENSE("GPL v2");
2758