PageRenderTime 2739ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/pci/host/pci-hyperv.c

https://github.com/mturquette/linux
C | 1752 lines | 1094 code | 192 blank | 466 comment | 104 complexity | 9d5a046b9bd81036abcfdc06159964ec MD5 | raw file
  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. #include <linux/kernel.h>
  50. #include <linux/module.h>
  51. #include <linux/pci.h>
  52. #include <linux/semaphore.h>
  53. #include <linux/irqdomain.h>
  54. #include <asm/irqdomain.h>
  55. #include <asm/apic.h>
  56. #include <linux/msi.h>
  57. #include <linux/hyperv.h>
  58. #include <asm/mshyperv.h>
  59. /*
  60. * Protocol versions. The low word is the minor version, the high word the
  61. * major version.
  62. */
  63. #define PCI_MAKE_VERSION(major, minor) ((u32)(((major) << 16) | (major)))
  64. #define PCI_MAJOR_VERSION(version) ((u32)(version) >> 16)
  65. #define PCI_MINOR_VERSION(version) ((u32)(version) & 0xff)
  66. enum {
  67. PCI_PROTOCOL_VERSION_1_1 = PCI_MAKE_VERSION(1, 1),
  68. PCI_PROTOCOL_VERSION_CURRENT = PCI_PROTOCOL_VERSION_1_1
  69. };
  70. #define PCI_CONFIG_MMIO_LENGTH 0x2000
  71. #define CFG_PAGE_OFFSET 0x1000
  72. #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
  73. #define MAX_SUPPORTED_MSI_MESSAGES 0x400
  74. /*
  75. * Message Types
  76. */
  77. enum pci_message_type {
  78. /*
  79. * Version 1.1
  80. */
  81. PCI_MESSAGE_BASE = 0x42490000,
  82. PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
  83. PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
  84. PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
  85. PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
  86. PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
  87. PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
  88. PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
  89. PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
  90. PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
  91. PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
  92. PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
  93. PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
  94. PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
  95. PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
  96. PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
  97. PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
  98. PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
  99. PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
  100. PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
  101. PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
  102. PCI_MESSAGE_MAXIMUM
  103. };
  104. /*
  105. * Structures defining the virtual PCI Express protocol.
  106. */
  107. union pci_version {
  108. struct {
  109. u16 minor_version;
  110. u16 major_version;
  111. } parts;
  112. u32 version;
  113. } __packed;
  114. /*
  115. * Function numbers are 8-bits wide on Express, as interpreted through ARI,
  116. * which is all this driver does. This representation is the one used in
  117. * Windows, which is what is expected when sending this back and forth with
  118. * the Hyper-V parent partition.
  119. */
  120. union win_slot_encoding {
  121. struct {
  122. u32 func:8;
  123. u32 reserved:24;
  124. } bits;
  125. u32 slot;
  126. } __packed;
  127. /*
  128. * Pretty much as defined in the PCI Specifications.
  129. */
  130. struct pci_function_description {
  131. u16 v_id; /* vendor ID */
  132. u16 d_id; /* device ID */
  133. u8 rev;
  134. u8 prog_intf;
  135. u8 subclass;
  136. u8 base_class;
  137. u32 subsystem_id;
  138. union win_slot_encoding win_slot;
  139. u32 ser; /* serial number */
  140. } __packed;
  141. /**
  142. * struct hv_msi_desc
  143. * @vector: IDT entry
  144. * @delivery_mode: As defined in Intel's Programmer's
  145. * Reference Manual, Volume 3, Chapter 8.
  146. * @vector_count: Number of contiguous entries in the
  147. * Interrupt Descriptor Table that are
  148. * occupied by this Message-Signaled
  149. * Interrupt. For "MSI", as first defined
  150. * in PCI 2.2, this can be between 1 and
  151. * 32. For "MSI-X," as first defined in PCI
  152. * 3.0, this must be 1, as each MSI-X table
  153. * entry would have its own descriptor.
  154. * @reserved: Empty space
  155. * @cpu_mask: All the target virtual processors.
  156. */
  157. struct hv_msi_desc {
  158. u8 vector;
  159. u8 delivery_mode;
  160. u16 vector_count;
  161. u32 reserved;
  162. u64 cpu_mask;
  163. } __packed;
  164. /**
  165. * struct tran_int_desc
  166. * @reserved: unused, padding
  167. * @vector_count: same as in hv_msi_desc
  168. * @data: This is the "data payload" value that is
  169. * written by the device when it generates
  170. * a message-signaled interrupt, either MSI
  171. * or MSI-X.
  172. * @address: This is the address to which the data
  173. * payload is written on interrupt
  174. * generation.
  175. */
  176. struct tran_int_desc {
  177. u16 reserved;
  178. u16 vector_count;
  179. u32 data;
  180. u64 address;
  181. } __packed;
  182. /*
  183. * A generic message format for virtual PCI.
  184. * Specific message formats are defined later in the file.
  185. */
  186. struct pci_message {
  187. u32 message_type;
  188. } __packed;
  189. struct pci_child_message {
  190. u32 message_type;
  191. union win_slot_encoding wslot;
  192. } __packed;
  193. struct pci_incoming_message {
  194. struct vmpacket_descriptor hdr;
  195. struct pci_message message_type;
  196. } __packed;
  197. struct pci_response {
  198. struct vmpacket_descriptor hdr;
  199. s32 status; /* negative values are failures */
  200. } __packed;
  201. struct pci_packet {
  202. void (*completion_func)(void *context, struct pci_response *resp,
  203. int resp_packet_size);
  204. void *compl_ctxt;
  205. struct pci_message message;
  206. };
  207. /*
  208. * Specific message types supporting the PCI protocol.
  209. */
  210. /*
  211. * Version negotiation message. Sent from the guest to the host.
  212. * The guest is free to try different versions until the host
  213. * accepts the version.
  214. *
  215. * pci_version: The protocol version requested.
  216. * is_last_attempt: If TRUE, this is the last version guest will request.
  217. * reservedz: Reserved field, set to zero.
  218. */
  219. struct pci_version_request {
  220. struct pci_message message_type;
  221. enum pci_message_type protocol_version;
  222. } __packed;
  223. /*
  224. * Bus D0 Entry. This is sent from the guest to the host when the virtual
  225. * bus (PCI Express port) is ready for action.
  226. */
  227. struct pci_bus_d0_entry {
  228. struct pci_message message_type;
  229. u32 reserved;
  230. u64 mmio_base;
  231. } __packed;
  232. struct pci_bus_relations {
  233. struct pci_incoming_message incoming;
  234. u32 device_count;
  235. struct pci_function_description func[1];
  236. } __packed;
  237. struct pci_q_res_req_response {
  238. struct vmpacket_descriptor hdr;
  239. s32 status; /* negative values are failures */
  240. u32 probed_bar[6];
  241. } __packed;
  242. struct pci_set_power {
  243. struct pci_message message_type;
  244. union win_slot_encoding wslot;
  245. u32 power_state; /* In Windows terms */
  246. u32 reserved;
  247. } __packed;
  248. struct pci_set_power_response {
  249. struct vmpacket_descriptor hdr;
  250. s32 status; /* negative values are failures */
  251. union win_slot_encoding wslot;
  252. u32 resultant_state; /* In Windows terms */
  253. u32 reserved;
  254. } __packed;
  255. struct pci_resources_assigned {
  256. struct pci_message message_type;
  257. union win_slot_encoding wslot;
  258. u8 memory_range[0x14][6]; /* not used here */
  259. u32 msi_descriptors;
  260. u32 reserved[4];
  261. } __packed;
  262. struct pci_create_interrupt {
  263. struct pci_message message_type;
  264. union win_slot_encoding wslot;
  265. struct hv_msi_desc int_desc;
  266. } __packed;
  267. struct pci_create_int_response {
  268. struct pci_response response;
  269. u32 reserved;
  270. struct tran_int_desc int_desc;
  271. } __packed;
  272. struct pci_delete_interrupt {
  273. struct pci_message message_type;
  274. union win_slot_encoding wslot;
  275. struct tran_int_desc int_desc;
  276. } __packed;
  277. struct pci_dev_incoming {
  278. struct pci_incoming_message incoming;
  279. union win_slot_encoding wslot;
  280. } __packed;
  281. struct pci_eject_response {
  282. u32 message_type;
  283. union win_slot_encoding wslot;
  284. u32 status;
  285. } __packed;
  286. static int pci_ring_size = (4 * PAGE_SIZE);
  287. /*
  288. * Definitions or interrupt steering hypercall.
  289. */
  290. #define HV_PARTITION_ID_SELF ((u64)-1)
  291. #define HVCALL_RETARGET_INTERRUPT 0x7e
  292. struct retarget_msi_interrupt {
  293. u64 partition_id; /* use "self" */
  294. u64 device_id;
  295. u32 source; /* 1 for MSI(-X) */
  296. u32 reserved1;
  297. u32 address;
  298. u32 data;
  299. u64 reserved2;
  300. u32 vector;
  301. u32 flags;
  302. u64 vp_mask;
  303. } __packed;
  304. /*
  305. * Driver specific state.
  306. */
  307. enum hv_pcibus_state {
  308. hv_pcibus_init = 0,
  309. hv_pcibus_probed,
  310. hv_pcibus_installed,
  311. hv_pcibus_maximum
  312. };
  313. struct hv_pcibus_device {
  314. struct pci_sysdata sysdata;
  315. enum hv_pcibus_state state;
  316. atomic_t remove_lock;
  317. struct hv_device *hdev;
  318. resource_size_t low_mmio_space;
  319. resource_size_t high_mmio_space;
  320. struct resource *mem_config;
  321. struct resource *low_mmio_res;
  322. struct resource *high_mmio_res;
  323. struct completion *survey_event;
  324. struct completion remove_event;
  325. struct pci_bus *pci_bus;
  326. spinlock_t config_lock; /* Avoid two threads writing index page */
  327. spinlock_t device_list_lock; /* Protect lists below */
  328. void __iomem *cfg_addr;
  329. struct semaphore enum_sem;
  330. struct list_head resources_for_children;
  331. struct list_head children;
  332. struct list_head dr_list;
  333. struct work_struct wrk;
  334. struct msi_domain_info msi_info;
  335. struct msi_controller msi_chip;
  336. struct irq_domain *irq_domain;
  337. };
  338. /*
  339. * Tracks "Device Relations" messages from the host, which must be both
  340. * processed in order and deferred so that they don't run in the context
  341. * of the incoming packet callback.
  342. */
  343. struct hv_dr_work {
  344. struct work_struct wrk;
  345. struct hv_pcibus_device *bus;
  346. };
  347. struct hv_dr_state {
  348. struct list_head list_entry;
  349. u32 device_count;
  350. struct pci_function_description func[1];
  351. };
  352. enum hv_pcichild_state {
  353. hv_pcichild_init = 0,
  354. hv_pcichild_requirements,
  355. hv_pcichild_resourced,
  356. hv_pcichild_ejecting,
  357. hv_pcichild_maximum
  358. };
  359. enum hv_pcidev_ref_reason {
  360. hv_pcidev_ref_invalid = 0,
  361. hv_pcidev_ref_initial,
  362. hv_pcidev_ref_by_slot,
  363. hv_pcidev_ref_packet,
  364. hv_pcidev_ref_pnp,
  365. hv_pcidev_ref_childlist,
  366. hv_pcidev_irqdata,
  367. hv_pcidev_ref_max
  368. };
  369. struct hv_pci_dev {
  370. /* List protected by pci_rescan_remove_lock */
  371. struct list_head list_entry;
  372. atomic_t refs;
  373. enum hv_pcichild_state state;
  374. struct pci_function_description desc;
  375. bool reported_missing;
  376. struct hv_pcibus_device *hbus;
  377. struct work_struct wrk;
  378. /*
  379. * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
  380. * read it back, for each of the BAR offsets within config space.
  381. */
  382. u32 probed_bar[6];
  383. };
  384. struct hv_pci_compl {
  385. struct completion host_event;
  386. s32 completion_status;
  387. };
  388. /**
  389. * hv_pci_generic_compl() - Invoked for a completion packet
  390. * @context: Set up by the sender of the packet.
  391. * @resp: The response packet
  392. * @resp_packet_size: Size in bytes of the packet
  393. *
  394. * This function is used to trigger an event and report status
  395. * for any message for which the completion packet contains a
  396. * status and nothing else.
  397. */
  398. static
  399. void
  400. hv_pci_generic_compl(void *context, struct pci_response *resp,
  401. int resp_packet_size)
  402. {
  403. struct hv_pci_compl *comp_pkt = context;
  404. if (resp_packet_size >= offsetofend(struct pci_response, status))
  405. comp_pkt->completion_status = resp->status;
  406. complete(&comp_pkt->host_event);
  407. }
  408. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  409. u32 wslot);
  410. static void get_pcichild(struct hv_pci_dev *hv_pcidev,
  411. enum hv_pcidev_ref_reason reason);
  412. static void put_pcichild(struct hv_pci_dev *hv_pcidev,
  413. enum hv_pcidev_ref_reason reason);
  414. static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
  415. static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
  416. /**
  417. * devfn_to_wslot() - Convert from Linux PCI slot to Windows
  418. * @devfn: The Linux representation of PCI slot
  419. *
  420. * Windows uses a slightly different representation of PCI slot.
  421. *
  422. * Return: The Windows representation
  423. */
  424. static u32 devfn_to_wslot(int devfn)
  425. {
  426. union win_slot_encoding wslot;
  427. wslot.slot = 0;
  428. wslot.bits.func = PCI_SLOT(devfn) | (PCI_FUNC(devfn) << 5);
  429. return wslot.slot;
  430. }
  431. /**
  432. * wslot_to_devfn() - Convert from Windows PCI slot to Linux
  433. * @wslot: The Windows representation of PCI slot
  434. *
  435. * Windows uses a slightly different representation of PCI slot.
  436. *
  437. * Return: The Linux representation
  438. */
  439. static int wslot_to_devfn(u32 wslot)
  440. {
  441. union win_slot_encoding slot_no;
  442. slot_no.slot = wslot;
  443. return PCI_DEVFN(0, slot_no.bits.func);
  444. }
  445. /*
  446. * PCI Configuration Space for these root PCI buses is implemented as a pair
  447. * of pages in memory-mapped I/O space. Writing to the first page chooses
  448. * the PCI function being written or read. Once the first page has been
  449. * written to, the following page maps in the entire configuration space of
  450. * the function.
  451. */
  452. /**
  453. * _hv_pcifront_read_config() - Internal PCI config read
  454. * @hpdev: The PCI driver's representation of the device
  455. * @where: Offset within config space
  456. * @size: Size of the transfer
  457. * @val: Pointer to the buffer receiving the data
  458. */
  459. static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
  460. int size, u32 *val)
  461. {
  462. unsigned long flags;
  463. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  464. /*
  465. * If the attempt is to read the IDs or the ROM BAR, simulate that.
  466. */
  467. if (where + size <= PCI_COMMAND) {
  468. memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
  469. } else if (where >= PCI_CLASS_REVISION && where + size <=
  470. PCI_CACHE_LINE_SIZE) {
  471. memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
  472. PCI_CLASS_REVISION, size);
  473. } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
  474. PCI_ROM_ADDRESS) {
  475. memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
  476. PCI_SUBSYSTEM_VENDOR_ID, size);
  477. } else if (where >= PCI_ROM_ADDRESS && where + size <=
  478. PCI_CAPABILITY_LIST) {
  479. /* ROM BARs are unimplemented */
  480. *val = 0;
  481. } else if (where >= PCI_INTERRUPT_LINE && where + size <=
  482. PCI_INTERRUPT_PIN) {
  483. /*
  484. * Interrupt Line and Interrupt PIN are hard-wired to zero
  485. * because this front-end only supports message-signaled
  486. * interrupts.
  487. */
  488. *val = 0;
  489. } else if (where + size <= CFG_PAGE_SIZE) {
  490. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  491. /* Choose the function to be read. (See comment above) */
  492. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  493. /* Make sure the function was chosen before we start reading. */
  494. mb();
  495. /* Read from that function's config space. */
  496. switch (size) {
  497. case 1:
  498. *val = readb(addr);
  499. break;
  500. case 2:
  501. *val = readw(addr);
  502. break;
  503. default:
  504. *val = readl(addr);
  505. break;
  506. }
  507. /*
  508. * Make sure the write was done before we release the spinlock
  509. * allowing consecutive reads/writes.
  510. */
  511. mb();
  512. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  513. } else {
  514. dev_err(&hpdev->hbus->hdev->device,
  515. "Attempt to read beyond a function's config space.\n");
  516. }
  517. }
  518. /**
  519. * _hv_pcifront_write_config() - Internal PCI config write
  520. * @hpdev: The PCI driver's representation of the device
  521. * @where: Offset within config space
  522. * @size: Size of the transfer
  523. * @val: The data being transferred
  524. */
  525. static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
  526. int size, u32 val)
  527. {
  528. unsigned long flags;
  529. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  530. if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
  531. where + size <= PCI_CAPABILITY_LIST) {
  532. /* SSIDs and ROM BARs are read-only */
  533. } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
  534. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  535. /* Choose the function to be written. (See comment above) */
  536. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  537. /* Make sure the function was chosen before we start writing. */
  538. wmb();
  539. /* Write to that function's config space. */
  540. switch (size) {
  541. case 1:
  542. writeb(val, addr);
  543. break;
  544. case 2:
  545. writew(val, addr);
  546. break;
  547. default:
  548. writel(val, addr);
  549. break;
  550. }
  551. /*
  552. * Make sure the write was done before we release the spinlock
  553. * allowing consecutive reads/writes.
  554. */
  555. mb();
  556. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  557. } else {
  558. dev_err(&hpdev->hbus->hdev->device,
  559. "Attempt to write beyond a function's config space.\n");
  560. }
  561. }
  562. /**
  563. * hv_pcifront_read_config() - Read configuration space
  564. * @bus: PCI Bus structure
  565. * @devfn: Device/function
  566. * @where: Offset from base
  567. * @size: Byte/word/dword
  568. * @val: Value to be read
  569. *
  570. * Return: PCIBIOS_SUCCESSFUL on success
  571. * PCIBIOS_DEVICE_NOT_FOUND on failure
  572. */
  573. static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
  574. int where, int size, u32 *val)
  575. {
  576. struct hv_pcibus_device *hbus =
  577. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  578. struct hv_pci_dev *hpdev;
  579. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  580. if (!hpdev)
  581. return PCIBIOS_DEVICE_NOT_FOUND;
  582. _hv_pcifront_read_config(hpdev, where, size, val);
  583. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  584. return PCIBIOS_SUCCESSFUL;
  585. }
  586. /**
  587. * hv_pcifront_write_config() - Write configuration space
  588. * @bus: PCI Bus structure
  589. * @devfn: Device/function
  590. * @where: Offset from base
  591. * @size: Byte/word/dword
  592. * @val: Value to be written to device
  593. *
  594. * Return: PCIBIOS_SUCCESSFUL on success
  595. * PCIBIOS_DEVICE_NOT_FOUND on failure
  596. */
  597. static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
  598. int where, int size, u32 val)
  599. {
  600. struct hv_pcibus_device *hbus =
  601. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  602. struct hv_pci_dev *hpdev;
  603. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  604. if (!hpdev)
  605. return PCIBIOS_DEVICE_NOT_FOUND;
  606. _hv_pcifront_write_config(hpdev, where, size, val);
  607. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  608. return PCIBIOS_SUCCESSFUL;
  609. }
  610. /* PCIe operations */
  611. static struct pci_ops hv_pcifront_ops = {
  612. .read = hv_pcifront_read_config,
  613. .write = hv_pcifront_write_config,
  614. };
  615. /* Interrupt management hooks */
  616. static void hv_int_desc_free(struct hv_pci_dev *hpdev,
  617. struct tran_int_desc *int_desc)
  618. {
  619. struct pci_delete_interrupt *int_pkt;
  620. struct {
  621. struct pci_packet pkt;
  622. u8 buffer[sizeof(struct pci_delete_interrupt) -
  623. sizeof(struct pci_message)];
  624. } ctxt;
  625. memset(&ctxt, 0, sizeof(ctxt));
  626. int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
  627. int_pkt->message_type.message_type =
  628. PCI_DELETE_INTERRUPT_MESSAGE;
  629. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  630. int_pkt->int_desc = *int_desc;
  631. vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
  632. (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
  633. kfree(int_desc);
  634. }
  635. /**
  636. * hv_msi_free() - Free the MSI.
  637. * @domain: The interrupt domain pointer
  638. * @info: Extra MSI-related context
  639. * @irq: Identifies the IRQ.
  640. *
  641. * The Hyper-V parent partition and hypervisor are tracking the
  642. * messages that are in use, keeping the interrupt redirection
  643. * table up to date. This callback sends a message that frees
  644. * the IRT entry and related tracking nonsense.
  645. */
  646. static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
  647. unsigned int irq)
  648. {
  649. struct hv_pcibus_device *hbus;
  650. struct hv_pci_dev *hpdev;
  651. struct pci_dev *pdev;
  652. struct tran_int_desc *int_desc;
  653. struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
  654. struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
  655. pdev = msi_desc_to_pci_dev(msi);
  656. hbus = info->data;
  657. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  658. if (!hpdev)
  659. return;
  660. int_desc = irq_data_get_irq_chip_data(irq_data);
  661. if (int_desc) {
  662. irq_data->chip_data = NULL;
  663. hv_int_desc_free(hpdev, int_desc);
  664. }
  665. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  666. }
  667. static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
  668. bool force)
  669. {
  670. struct irq_data *parent = data->parent_data;
  671. return parent->chip->irq_set_affinity(parent, dest, force);
  672. }
  673. void hv_irq_mask(struct irq_data *data)
  674. {
  675. pci_msi_mask_irq(data);
  676. }
  677. /**
  678. * hv_irq_unmask() - "Unmask" the IRQ by setting its current
  679. * affinity.
  680. * @data: Describes the IRQ
  681. *
  682. * Build new a destination for the MSI and make a hypercall to
  683. * update the Interrupt Redirection Table. "Device Logical ID"
  684. * is built out of this PCI bus's instance GUID and the function
  685. * number of the device.
  686. */
  687. void hv_irq_unmask(struct irq_data *data)
  688. {
  689. struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
  690. struct irq_cfg *cfg = irqd_cfg(data);
  691. struct retarget_msi_interrupt params;
  692. struct hv_pcibus_device *hbus;
  693. struct cpumask *dest;
  694. struct pci_bus *pbus;
  695. struct pci_dev *pdev;
  696. int cpu;
  697. dest = irq_data_get_affinity_mask(data);
  698. pdev = msi_desc_to_pci_dev(msi_desc);
  699. pbus = pdev->bus;
  700. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  701. memset(&params, 0, sizeof(params));
  702. params.partition_id = HV_PARTITION_ID_SELF;
  703. params.source = 1; /* MSI(-X) */
  704. params.address = msi_desc->msg.address_lo;
  705. params.data = msi_desc->msg.data;
  706. params.device_id = (hbus->hdev->dev_instance.b[5] << 24) |
  707. (hbus->hdev->dev_instance.b[4] << 16) |
  708. (hbus->hdev->dev_instance.b[7] << 8) |
  709. (hbus->hdev->dev_instance.b[6] & 0xf8) |
  710. PCI_FUNC(pdev->devfn);
  711. params.vector = cfg->vector;
  712. for_each_cpu_and(cpu, dest, cpu_online_mask)
  713. params.vp_mask |= (1ULL << vmbus_cpu_number_to_vp_number(cpu));
  714. hv_do_hypercall(HVCALL_RETARGET_INTERRUPT, &params, NULL);
  715. pci_msi_unmask_irq(data);
  716. }
  717. struct compose_comp_ctxt {
  718. struct hv_pci_compl comp_pkt;
  719. struct tran_int_desc int_desc;
  720. };
  721. static void hv_pci_compose_compl(void *context, struct pci_response *resp,
  722. int resp_packet_size)
  723. {
  724. struct compose_comp_ctxt *comp_pkt = context;
  725. struct pci_create_int_response *int_resp =
  726. (struct pci_create_int_response *)resp;
  727. comp_pkt->comp_pkt.completion_status = resp->status;
  728. comp_pkt->int_desc = int_resp->int_desc;
  729. complete(&comp_pkt->comp_pkt.host_event);
  730. }
  731. /**
  732. * hv_compose_msi_msg() - Supplies a valid MSI address/data
  733. * @data: Everything about this MSI
  734. * @msg: Buffer that is filled in by this function
  735. *
  736. * This function unpacks the IRQ looking for target CPU set, IDT
  737. * vector and mode and sends a message to the parent partition
  738. * asking for a mapping for that tuple in this partition. The
  739. * response supplies a data value and address to which that data
  740. * should be written to trigger that interrupt.
  741. */
  742. static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  743. {
  744. struct irq_cfg *cfg = irqd_cfg(data);
  745. struct hv_pcibus_device *hbus;
  746. struct hv_pci_dev *hpdev;
  747. struct pci_bus *pbus;
  748. struct pci_dev *pdev;
  749. struct pci_create_interrupt *int_pkt;
  750. struct compose_comp_ctxt comp;
  751. struct tran_int_desc *int_desc;
  752. struct cpumask *affinity;
  753. struct {
  754. struct pci_packet pkt;
  755. u8 buffer[sizeof(struct pci_create_interrupt) -
  756. sizeof(struct pci_message)];
  757. } ctxt;
  758. int cpu;
  759. int ret;
  760. pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
  761. pbus = pdev->bus;
  762. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  763. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  764. if (!hpdev)
  765. goto return_null_message;
  766. /* Free any previous message that might have already been composed. */
  767. if (data->chip_data) {
  768. int_desc = data->chip_data;
  769. data->chip_data = NULL;
  770. hv_int_desc_free(hpdev, int_desc);
  771. }
  772. int_desc = kzalloc(sizeof(*int_desc), GFP_KERNEL);
  773. if (!int_desc)
  774. goto drop_reference;
  775. memset(&ctxt, 0, sizeof(ctxt));
  776. init_completion(&comp.comp_pkt.host_event);
  777. ctxt.pkt.completion_func = hv_pci_compose_compl;
  778. ctxt.pkt.compl_ctxt = &comp;
  779. int_pkt = (struct pci_create_interrupt *)&ctxt.pkt.message;
  780. int_pkt->message_type.message_type = PCI_CREATE_INTERRUPT_MESSAGE;
  781. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  782. int_pkt->int_desc.vector = cfg->vector;
  783. int_pkt->int_desc.vector_count = 1;
  784. int_pkt->int_desc.delivery_mode =
  785. (apic->irq_delivery_mode == dest_LowestPrio) ? 1 : 0;
  786. /*
  787. * This bit doesn't have to work on machines with more than 64
  788. * processors because Hyper-V only supports 64 in a guest.
  789. */
  790. affinity = irq_data_get_affinity_mask(data);
  791. for_each_cpu_and(cpu, affinity, cpu_online_mask) {
  792. int_pkt->int_desc.cpu_mask |=
  793. (1ULL << vmbus_cpu_number_to_vp_number(cpu));
  794. }
  795. ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt,
  796. sizeof(*int_pkt), (unsigned long)&ctxt.pkt,
  797. VM_PKT_DATA_INBAND,
  798. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  799. if (!ret)
  800. wait_for_completion(&comp.comp_pkt.host_event);
  801. if (comp.comp_pkt.completion_status < 0) {
  802. dev_err(&hbus->hdev->device,
  803. "Request for interrupt failed: 0x%x",
  804. comp.comp_pkt.completion_status);
  805. goto free_int_desc;
  806. }
  807. /*
  808. * Record the assignment so that this can be unwound later. Using
  809. * irq_set_chip_data() here would be appropriate, but the lock it takes
  810. * is already held.
  811. */
  812. *int_desc = comp.int_desc;
  813. data->chip_data = int_desc;
  814. /* Pass up the result. */
  815. msg->address_hi = comp.int_desc.address >> 32;
  816. msg->address_lo = comp.int_desc.address & 0xffffffff;
  817. msg->data = comp.int_desc.data;
  818. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  819. return;
  820. free_int_desc:
  821. kfree(int_desc);
  822. drop_reference:
  823. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  824. return_null_message:
  825. msg->address_hi = 0;
  826. msg->address_lo = 0;
  827. msg->data = 0;
  828. }
  829. /* HW Interrupt Chip Descriptor */
  830. static struct irq_chip hv_msi_irq_chip = {
  831. .name = "Hyper-V PCIe MSI",
  832. .irq_compose_msi_msg = hv_compose_msi_msg,
  833. .irq_set_affinity = hv_set_affinity,
  834. .irq_ack = irq_chip_ack_parent,
  835. .irq_mask = hv_irq_mask,
  836. .irq_unmask = hv_irq_unmask,
  837. };
  838. static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
  839. msi_alloc_info_t *arg)
  840. {
  841. return arg->msi_hwirq;
  842. }
  843. static struct msi_domain_ops hv_msi_ops = {
  844. .get_hwirq = hv_msi_domain_ops_get_hwirq,
  845. .msi_prepare = pci_msi_prepare,
  846. .set_desc = pci_msi_set_desc,
  847. .msi_free = hv_msi_free,
  848. };
  849. /**
  850. * hv_pcie_init_irq_domain() - Initialize IRQ domain
  851. * @hbus: The root PCI bus
  852. *
  853. * This function creates an IRQ domain which will be used for
  854. * interrupts from devices that have been passed through. These
  855. * devices only support MSI and MSI-X, not line-based interrupts
  856. * or simulations of line-based interrupts through PCIe's
  857. * fabric-layer messages. Because interrupts are remapped, we
  858. * can support multi-message MSI here.
  859. *
  860. * Return: '0' on success and error value on failure
  861. */
  862. static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
  863. {
  864. hbus->msi_info.chip = &hv_msi_irq_chip;
  865. hbus->msi_info.ops = &hv_msi_ops;
  866. hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
  867. MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
  868. MSI_FLAG_PCI_MSIX);
  869. hbus->msi_info.handler = handle_edge_irq;
  870. hbus->msi_info.handler_name = "edge";
  871. hbus->msi_info.data = hbus;
  872. hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
  873. &hbus->msi_info,
  874. x86_vector_domain);
  875. if (!hbus->irq_domain) {
  876. dev_err(&hbus->hdev->device,
  877. "Failed to build an MSI IRQ domain\n");
  878. return -ENODEV;
  879. }
  880. return 0;
  881. }
  882. /**
  883. * get_bar_size() - Get the address space consumed by a BAR
  884. * @bar_val: Value that a BAR returned after -1 was written
  885. * to it.
  886. *
  887. * This function returns the size of the BAR, rounded up to 1
  888. * page. It has to be rounded up because the hypervisor's page
  889. * table entry that maps the BAR into the VM can't specify an
  890. * offset within a page. The invariant is that the hypervisor
  891. * must place any BARs of smaller than page length at the
  892. * beginning of a page.
  893. *
  894. * Return: Size in bytes of the consumed MMIO space.
  895. */
  896. static u64 get_bar_size(u64 bar_val)
  897. {
  898. return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
  899. PAGE_SIZE);
  900. }
  901. /**
  902. * survey_child_resources() - Total all MMIO requirements
  903. * @hbus: Root PCI bus, as understood by this driver
  904. */
  905. static void survey_child_resources(struct hv_pcibus_device *hbus)
  906. {
  907. struct list_head *iter;
  908. struct hv_pci_dev *hpdev;
  909. resource_size_t bar_size = 0;
  910. unsigned long flags;
  911. struct completion *event;
  912. u64 bar_val;
  913. int i;
  914. /* If nobody is waiting on the answer, don't compute it. */
  915. event = xchg(&hbus->survey_event, NULL);
  916. if (!event)
  917. return;
  918. /* If the answer has already been computed, go with it. */
  919. if (hbus->low_mmio_space || hbus->high_mmio_space) {
  920. complete(event);
  921. return;
  922. }
  923. spin_lock_irqsave(&hbus->device_list_lock, flags);
  924. /*
  925. * Due to an interesting quirk of the PCI spec, all memory regions
  926. * for a child device are a power of 2 in size and aligned in memory,
  927. * so it's sufficient to just add them up without tracking alignment.
  928. */
  929. list_for_each(iter, &hbus->children) {
  930. hpdev = container_of(iter, struct hv_pci_dev, list_entry);
  931. for (i = 0; i < 6; i++) {
  932. if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
  933. dev_err(&hbus->hdev->device,
  934. "There's an I/O BAR in this list!\n");
  935. if (hpdev->probed_bar[i] != 0) {
  936. /*
  937. * A probed BAR has all the upper bits set that
  938. * can be changed.
  939. */
  940. bar_val = hpdev->probed_bar[i];
  941. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  942. bar_val |=
  943. ((u64)hpdev->probed_bar[++i] << 32);
  944. else
  945. bar_val |= 0xffffffff00000000ULL;
  946. bar_size = get_bar_size(bar_val);
  947. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  948. hbus->high_mmio_space += bar_size;
  949. else
  950. hbus->low_mmio_space += bar_size;
  951. }
  952. }
  953. }
  954. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  955. complete(event);
  956. }
  957. /**
  958. * prepopulate_bars() - Fill in BARs with defaults
  959. * @hbus: Root PCI bus, as understood by this driver
  960. *
  961. * The core PCI driver code seems much, much happier if the BARs
  962. * for a device have values upon first scan. So fill them in.
  963. * The algorithm below works down from large sizes to small,
  964. * attempting to pack the assignments optimally. The assumption,
  965. * enforced in other parts of the code, is that the beginning of
  966. * the memory-mapped I/O space will be aligned on the largest
  967. * BAR size.
  968. */
  969. static void prepopulate_bars(struct hv_pcibus_device *hbus)
  970. {
  971. resource_size_t high_size = 0;
  972. resource_size_t low_size = 0;
  973. resource_size_t high_base = 0;
  974. resource_size_t low_base = 0;
  975. resource_size_t bar_size;
  976. struct hv_pci_dev *hpdev;
  977. struct list_head *iter;
  978. unsigned long flags;
  979. u64 bar_val;
  980. u32 command;
  981. bool high;
  982. int i;
  983. if (hbus->low_mmio_space) {
  984. low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  985. low_base = hbus->low_mmio_res->start;
  986. }
  987. if (hbus->high_mmio_space) {
  988. high_size = 1ULL <<
  989. (63 - __builtin_clzll(hbus->high_mmio_space));
  990. high_base = hbus->high_mmio_res->start;
  991. }
  992. spin_lock_irqsave(&hbus->device_list_lock, flags);
  993. /* Pick addresses for the BARs. */
  994. do {
  995. list_for_each(iter, &hbus->children) {
  996. hpdev = container_of(iter, struct hv_pci_dev,
  997. list_entry);
  998. for (i = 0; i < 6; i++) {
  999. bar_val = hpdev->probed_bar[i];
  1000. if (bar_val == 0)
  1001. continue;
  1002. high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
  1003. if (high) {
  1004. bar_val |=
  1005. ((u64)hpdev->probed_bar[i + 1]
  1006. << 32);
  1007. } else {
  1008. bar_val |= 0xffffffffULL << 32;
  1009. }
  1010. bar_size = get_bar_size(bar_val);
  1011. if (high) {
  1012. if (high_size != bar_size) {
  1013. i++;
  1014. continue;
  1015. }
  1016. _hv_pcifront_write_config(hpdev,
  1017. PCI_BASE_ADDRESS_0 + (4 * i),
  1018. 4,
  1019. (u32)(high_base & 0xffffff00));
  1020. i++;
  1021. _hv_pcifront_write_config(hpdev,
  1022. PCI_BASE_ADDRESS_0 + (4 * i),
  1023. 4, (u32)(high_base >> 32));
  1024. high_base += bar_size;
  1025. } else {
  1026. if (low_size != bar_size)
  1027. continue;
  1028. _hv_pcifront_write_config(hpdev,
  1029. PCI_BASE_ADDRESS_0 + (4 * i),
  1030. 4,
  1031. (u32)(low_base & 0xffffff00));
  1032. low_base += bar_size;
  1033. }
  1034. }
  1035. if (high_size <= 1 && low_size <= 1) {
  1036. /* Set the memory enable bit. */
  1037. _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
  1038. &command);
  1039. command |= PCI_COMMAND_MEMORY;
  1040. _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
  1041. command);
  1042. break;
  1043. }
  1044. }
  1045. high_size >>= 1;
  1046. low_size >>= 1;
  1047. } while (high_size || low_size);
  1048. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1049. }
  1050. /**
  1051. * create_root_hv_pci_bus() - Expose a new root PCI bus
  1052. * @hbus: Root PCI bus, as understood by this driver
  1053. *
  1054. * Return: 0 on success, -errno on failure
  1055. */
  1056. static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
  1057. {
  1058. /* Register the device */
  1059. hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
  1060. 0, /* bus number is always zero */
  1061. &hv_pcifront_ops,
  1062. &hbus->sysdata,
  1063. &hbus->resources_for_children);
  1064. if (!hbus->pci_bus)
  1065. return -ENODEV;
  1066. hbus->pci_bus->msi = &hbus->msi_chip;
  1067. hbus->pci_bus->msi->dev = &hbus->hdev->device;
  1068. pci_scan_child_bus(hbus->pci_bus);
  1069. pci_bus_assign_resources(hbus->pci_bus);
  1070. pci_bus_add_devices(hbus->pci_bus);
  1071. hbus->state = hv_pcibus_installed;
  1072. return 0;
  1073. }
  1074. struct q_res_req_compl {
  1075. struct completion host_event;
  1076. struct hv_pci_dev *hpdev;
  1077. };
  1078. /**
  1079. * q_resource_requirements() - Query Resource Requirements
  1080. * @context: The completion context.
  1081. * @resp: The response that came from the host.
  1082. * @resp_packet_size: The size in bytes of resp.
  1083. *
  1084. * This function is invoked on completion of a Query Resource
  1085. * Requirements packet.
  1086. */
  1087. static void q_resource_requirements(void *context, struct pci_response *resp,
  1088. int resp_packet_size)
  1089. {
  1090. struct q_res_req_compl *completion = context;
  1091. struct pci_q_res_req_response *q_res_req =
  1092. (struct pci_q_res_req_response *)resp;
  1093. int i;
  1094. if (resp->status < 0) {
  1095. dev_err(&completion->hpdev->hbus->hdev->device,
  1096. "query resource requirements failed: %x\n",
  1097. resp->status);
  1098. } else {
  1099. for (i = 0; i < 6; i++) {
  1100. completion->hpdev->probed_bar[i] =
  1101. q_res_req->probed_bar[i];
  1102. }
  1103. }
  1104. complete(&completion->host_event);
  1105. }
  1106. static void get_pcichild(struct hv_pci_dev *hpdev,
  1107. enum hv_pcidev_ref_reason reason)
  1108. {
  1109. atomic_inc(&hpdev->refs);
  1110. }
  1111. static void put_pcichild(struct hv_pci_dev *hpdev,
  1112. enum hv_pcidev_ref_reason reason)
  1113. {
  1114. if (atomic_dec_and_test(&hpdev->refs))
  1115. kfree(hpdev);
  1116. }
  1117. /**
  1118. * new_pcichild_device() - Create a new child device
  1119. * @hbus: The internal struct tracking this root PCI bus.
  1120. * @desc: The information supplied so far from the host
  1121. * about the device.
  1122. *
  1123. * This function creates the tracking structure for a new child
  1124. * device and kicks off the process of figuring out what it is.
  1125. *
  1126. * Return: Pointer to the new tracking struct
  1127. */
  1128. static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
  1129. struct pci_function_description *desc)
  1130. {
  1131. struct hv_pci_dev *hpdev;
  1132. struct pci_child_message *res_req;
  1133. struct q_res_req_compl comp_pkt;
  1134. union {
  1135. struct pci_packet init_packet;
  1136. u8 buffer[0x100];
  1137. } pkt;
  1138. unsigned long flags;
  1139. int ret;
  1140. hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC);
  1141. if (!hpdev)
  1142. return NULL;
  1143. hpdev->hbus = hbus;
  1144. memset(&pkt, 0, sizeof(pkt));
  1145. init_completion(&comp_pkt.host_event);
  1146. comp_pkt.hpdev = hpdev;
  1147. pkt.init_packet.compl_ctxt = &comp_pkt;
  1148. pkt.init_packet.completion_func = q_resource_requirements;
  1149. res_req = (struct pci_child_message *)&pkt.init_packet.message;
  1150. res_req->message_type = PCI_QUERY_RESOURCE_REQUIREMENTS;
  1151. res_req->wslot.slot = desc->win_slot.slot;
  1152. ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
  1153. sizeof(struct pci_child_message),
  1154. (unsigned long)&pkt.init_packet,
  1155. VM_PKT_DATA_INBAND,
  1156. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1157. if (ret)
  1158. goto error;
  1159. wait_for_completion(&comp_pkt.host_event);
  1160. hpdev->desc = *desc;
  1161. get_pcichild(hpdev, hv_pcidev_ref_initial);
  1162. get_pcichild(hpdev, hv_pcidev_ref_childlist);
  1163. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1164. list_add_tail(&hpdev->list_entry, &hbus->children);
  1165. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1166. return hpdev;
  1167. error:
  1168. kfree(hpdev);
  1169. return NULL;
  1170. }
  1171. /**
  1172. * get_pcichild_wslot() - Find device from slot
  1173. * @hbus: Root PCI bus, as understood by this driver
  1174. * @wslot: Location on the bus
  1175. *
  1176. * This function looks up a PCI device and returns the internal
  1177. * representation of it. It acquires a reference on it, so that
  1178. * the device won't be deleted while somebody is using it. The
  1179. * caller is responsible for calling put_pcichild() to release
  1180. * this reference.
  1181. *
  1182. * Return: Internal representation of a PCI device
  1183. */
  1184. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  1185. u32 wslot)
  1186. {
  1187. unsigned long flags;
  1188. struct hv_pci_dev *iter, *hpdev = NULL;
  1189. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1190. list_for_each_entry(iter, &hbus->children, list_entry) {
  1191. if (iter->desc.win_slot.slot == wslot) {
  1192. hpdev = iter;
  1193. get_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1194. break;
  1195. }
  1196. }
  1197. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1198. return hpdev;
  1199. }
  1200. /**
  1201. * pci_devices_present_work() - Handle new list of child devices
  1202. * @work: Work struct embedded in struct hv_dr_work
  1203. *
  1204. * "Bus Relations" is the Windows term for "children of this
  1205. * bus." The terminology is preserved here for people trying to
  1206. * debug the interaction between Hyper-V and Linux. This
  1207. * function is called when the parent partition reports a list
  1208. * of functions that should be observed under this PCI Express
  1209. * port (bus).
  1210. *
  1211. * This function updates the list, and must tolerate being
  1212. * called multiple times with the same information. The typical
  1213. * number of child devices is one, with very atypical cases
  1214. * involving three or four, so the algorithms used here can be
  1215. * simple and inefficient.
  1216. *
  1217. * It must also treat the omission of a previously observed device as
  1218. * notification that the device no longer exists.
  1219. *
  1220. * Note that this function is a work item, and it may not be
  1221. * invoked in the order that it was queued. Back to back
  1222. * updates of the list of present devices may involve queuing
  1223. * multiple work items, and this one may run before ones that
  1224. * were sent later. As such, this function only does something
  1225. * if is the last one in the queue.
  1226. */
  1227. static void pci_devices_present_work(struct work_struct *work)
  1228. {
  1229. u32 child_no;
  1230. bool found;
  1231. struct list_head *iter;
  1232. struct pci_function_description *new_desc;
  1233. struct hv_pci_dev *hpdev;
  1234. struct hv_pcibus_device *hbus;
  1235. struct list_head removed;
  1236. struct hv_dr_work *dr_wrk;
  1237. struct hv_dr_state *dr = NULL;
  1238. unsigned long flags;
  1239. dr_wrk = container_of(work, struct hv_dr_work, wrk);
  1240. hbus = dr_wrk->bus;
  1241. kfree(dr_wrk);
  1242. INIT_LIST_HEAD(&removed);
  1243. if (down_interruptible(&hbus->enum_sem)) {
  1244. put_hvpcibus(hbus);
  1245. return;
  1246. }
  1247. /* Pull this off the queue and process it if it was the last one. */
  1248. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1249. while (!list_empty(&hbus->dr_list)) {
  1250. dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
  1251. list_entry);
  1252. list_del(&dr->list_entry);
  1253. /* Throw this away if the list still has stuff in it. */
  1254. if (!list_empty(&hbus->dr_list)) {
  1255. kfree(dr);
  1256. continue;
  1257. }
  1258. }
  1259. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1260. if (!dr) {
  1261. up(&hbus->enum_sem);
  1262. put_hvpcibus(hbus);
  1263. return;
  1264. }
  1265. /* First, mark all existing children as reported missing. */
  1266. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1267. list_for_each(iter, &hbus->children) {
  1268. hpdev = container_of(iter, struct hv_pci_dev,
  1269. list_entry);
  1270. hpdev->reported_missing = true;
  1271. }
  1272. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1273. /* Next, add back any reported devices. */
  1274. for (child_no = 0; child_no < dr->device_count; child_no++) {
  1275. found = false;
  1276. new_desc = &dr->func[child_no];
  1277. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1278. list_for_each(iter, &hbus->children) {
  1279. hpdev = container_of(iter, struct hv_pci_dev,
  1280. list_entry);
  1281. if ((hpdev->desc.win_slot.slot ==
  1282. new_desc->win_slot.slot) &&
  1283. (hpdev->desc.v_id == new_desc->v_id) &&
  1284. (hpdev->desc.d_id == new_desc->d_id) &&
  1285. (hpdev->desc.ser == new_desc->ser)) {
  1286. hpdev->reported_missing = false;
  1287. found = true;
  1288. }
  1289. }
  1290. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1291. if (!found) {
  1292. hpdev = new_pcichild_device(hbus, new_desc);
  1293. if (!hpdev)
  1294. dev_err(&hbus->hdev->device,
  1295. "couldn't record a child device.\n");
  1296. }
  1297. }
  1298. /* Move missing children to a list on the stack. */
  1299. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1300. do {
  1301. found = false;
  1302. list_for_each(iter, &hbus->children) {
  1303. hpdev = container_of(iter, struct hv_pci_dev,
  1304. list_entry);
  1305. if (hpdev->reported_missing) {
  1306. found = true;
  1307. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1308. list_del(&hpdev->list_entry);
  1309. list_add_tail(&hpdev->list_entry, &removed);
  1310. break;
  1311. }
  1312. }
  1313. } while (found);
  1314. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1315. /* Delete everything that should no longer exist. */
  1316. while (!list_empty(&removed)) {
  1317. hpdev = list_first_entry(&removed, struct hv_pci_dev,
  1318. list_entry);
  1319. list_del(&hpdev->list_entry);
  1320. put_pcichild(hpdev, hv_pcidev_ref_initial);
  1321. }
  1322. /* Tell the core to rescan bus because there may have been changes. */
  1323. if (hbus->state == hv_pcibus_installed) {
  1324. pci_lock_rescan_remove();
  1325. pci_scan_child_bus(hbus->pci_bus);
  1326. pci_unlock_rescan_remove();
  1327. } else {
  1328. survey_child_resources(hbus);
  1329. }
  1330. up(&hbus->enum_sem);
  1331. put_hvpcibus(hbus);
  1332. kfree(dr);
  1333. }
  1334. /**
  1335. * hv_pci_devices_present() - Handles list of new children
  1336. * @hbus: Root PCI bus, as understood by this driver
  1337. * @relations: Packet from host listing children
  1338. *
  1339. * This function is invoked whenever a new list of devices for
  1340. * this bus appears.
  1341. */
  1342. static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
  1343. struct pci_bus_relations *relations)
  1344. {
  1345. struct hv_dr_state *dr;
  1346. struct hv_dr_work *dr_wrk;
  1347. unsigned long flags;
  1348. dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
  1349. if (!dr_wrk)
  1350. return;
  1351. dr = kzalloc(offsetof(struct hv_dr_state, func) +
  1352. (sizeof(struct pci_function_description) *
  1353. (relations->device_count)), GFP_NOWAIT);
  1354. if (!dr) {
  1355. kfree(dr_wrk);
  1356. return;
  1357. }
  1358. INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
  1359. dr_wrk->bus = hbus;
  1360. dr->device_count = relations->device_count;
  1361. if (dr->device_count != 0) {
  1362. memcpy(dr->func, relations->func,
  1363. sizeof(struct pci_function_description) *
  1364. dr->device_count);
  1365. }
  1366. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1367. list_add_tail(&dr->list_entry, &hbus->dr_list);
  1368. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1369. get_hvpcibus(hbus);
  1370. schedule_work(&dr_wrk->wrk);
  1371. }
  1372. /**
  1373. * hv_eject_device_work() - Asynchronously handles ejection
  1374. * @work: Work struct embedded in internal device struct
  1375. *
  1376. * This function handles ejecting a device. Windows will
  1377. * attempt to gracefully eject a device, waiting 60 seconds to
  1378. * hear back from the guest OS that this completed successfully.
  1379. * If this timer expires, the device will be forcibly removed.
  1380. */
  1381. static void hv_eject_device_work(struct work_struct *work)
  1382. {
  1383. struct pci_eject_response *ejct_pkt;
  1384. struct hv_pci_dev *hpdev;
  1385. struct pci_dev *pdev;
  1386. unsigned long flags;
  1387. int wslot;
  1388. struct {
  1389. struct pci_packet pkt;
  1390. u8 buffer[sizeof(struct pci_eject_response) -
  1391. sizeof(struct pci_message)];
  1392. } ctxt;
  1393. hpdev = container_of(work, struct hv_pci_dev, wrk);
  1394. if (hpdev->state != hv_pcichild_ejecting) {
  1395. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1396. return;
  1397. }
  1398. /*
  1399. * Ejection can come before or after the PCI bus has been set up, so
  1400. * attempt to find it and tear down the bus state, if it exists. This
  1401. * must be done without constructs like pci_domain_nr(hbus->pci_bus)
  1402. * because hbus->pci_bus may not exist yet.
  1403. */
  1404. wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
  1405. pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0,
  1406. wslot);
  1407. if (pdev) {
  1408. pci_stop_and_remove_bus_device(pdev);
  1409. pci_dev_put(pdev);
  1410. }
  1411. memset(&ctxt, 0, sizeof(ctxt));
  1412. ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
  1413. ejct_pkt->message_type = PCI_EJECTION_COMPLETE;
  1414. ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  1415. vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
  1416. sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
  1417. VM_PKT_DATA_INBAND, 0);
  1418. spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
  1419. list_del(&hpdev->list_entry);
  1420. spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
  1421. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1422. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1423. put_hvpcibus(hpdev->hbus);
  1424. }
  1425. /**
  1426. * hv_pci_eject_device() - Handles device ejection
  1427. * @hpdev: Internal device tracking struct
  1428. *
  1429. * This function is invoked when an ejection packet arrives. It
  1430. * just schedules work so that we don't re-enter the packet
  1431. * delivery code handling the ejection.
  1432. */
  1433. static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
  1434. {
  1435. hpdev->state = hv_pcichild_ejecting;
  1436. get_pcichild(hpdev, hv_pcidev_ref_pnp);
  1437. INIT_WORK(&hpdev->wrk, hv_eject_device_work);
  1438. get_hvpcibus(hpdev->hbus);
  1439. schedule_work(&hpdev->wrk);
  1440. }
  1441. /**
  1442. * hv_pci_onchannelcallback() - Handles incoming packets
  1443. * @context: Internal bus tracking struct
  1444. *
  1445. * This function is invoked whenever the host sends a packet to
  1446. * this channel (which is private to this root PCI bus).
  1447. */
  1448. static void hv_pci_onchannelcallback(void *context)
  1449. {
  1450. const int packet_size = 0x100;
  1451. int ret;
  1452. struct hv_pcibus_device *hbus = context;
  1453. u32 bytes_recvd;
  1454. u64 req_id;
  1455. struct vmpacket_descriptor *desc;
  1456. unsigned char *buffer;
  1457. int bufferlen = packet_size;
  1458. struct pci_packet *comp_packet;
  1459. struct pci_response *response;
  1460. struct pci_incoming_message *new_message;
  1461. struct pci_bus_relations *bus_rel;
  1462. struct pci_dev_incoming *dev_message;
  1463. struct hv_pci_dev *hpdev;
  1464. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  1465. if (!buffer)
  1466. return;
  1467. while (1) {
  1468. ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
  1469. bufferlen, &bytes_recvd, &req_id);
  1470. if (ret == -ENOBUFS) {
  1471. kfree(buffer);
  1472. /* Handle large packet */
  1473. bufferlen = bytes_recvd;
  1474. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  1475. if (!buffer)
  1476. return;
  1477. continue;
  1478. }
  1479. /*
  1480. * All incoming packets must be at least as large as a
  1481. * response.
  1482. */
  1483. if (bytes_recvd <= sizeof(struct pci_response)) {
  1484. kfree(buffer);
  1485. return;
  1486. }
  1487. desc = (struct vmpacket_descriptor *)buffer;
  1488. switch (desc->type) {
  1489. case VM_PKT_COMP:
  1490. /*
  1491. * The host is trusted, and thus it's safe to interpret
  1492. * this transaction ID as a pointer.
  1493. */
  1494. comp_packet = (struct pci_packet *)req_id;
  1495. response = (struct pci_response *)buffer;
  1496. comp_packet->completion_func(comp_packet->compl_ctxt,
  1497. response,
  1498. bytes_recvd);
  1499. kfree(buffer);
  1500. return;
  1501. case VM_PKT_DATA_INBAND:
  1502. new_message = (struct pci_incoming_message *)buffer;
  1503. switch (new_message->message_type.message_type) {
  1504. case PCI_BUS_RELATIONS:
  1505. bus_rel = (struct pci_bus_relations *)buffer;
  1506. if (bytes_recvd <
  1507. offsetof(struct pci_bus_relations, func) +
  1508. (sizeof(struct pci_function_description) *
  1509. (bus_rel->device_count))) {
  1510. dev_err(&hbus->hdev->device,
  1511. "bus relations too small\n");
  1512. break;
  1513. }
  1514. hv_pci_devices_present(hbus, bus_rel);
  1515. break;
  1516. case PCI_EJECT:
  1517. dev_message = (struct pci_dev_incoming *)buffer;
  1518. hpdev = get_pcichild_wslot(hbus,
  1519. dev_message->wslot.slot);
  1520. if (hpdev) {
  1521. hv_pci_eject_device(hpdev);
  1522. put_pcichild(hpdev,
  1523. hv_pcidev_ref_by_slot);
  1524. }
  1525. break;
  1526. default:
  1527. dev_warn(&hbus->hdev->device,
  1528. "Unimplemented protocol message %x\n",
  1529. new_message->message_type.message_type);
  1530. break;
  1531. }
  1532. break;
  1533. default:
  1534. dev_err(&hbus->hdev->device,
  1535. "unhandled packet type %d, tid %llx len %d\n",
  1536. desc->type, req_id, bytes_recvd);
  1537. break;
  1538. }
  1539. break;
  1540. }
  1541. }
  1542. /**
  1543. * hv_pci_protocol_negotiation() - Set up protocol
  1544. * @hdev: VMBus's tracking struct for this root PCI bus
  1545. *
  1546. * This driver is intended to support running on Windows 10
  1547. * (server) and later versions. It will not run on earlier
  1548. * versions, as they assume that many of the operations which
  1549. * Linux needs accomplished with a spinlock held were done via
  1550. * asynchronous messaging via VMBus. Windows 10 increases the
  1551. * surface area of PCI emulation so that these actions can take
  1552. * place by suspending a virtual processor for their duration.
  1553. *
  1554. * This function negotiates the channel protocol version,
  1555. * failing if the host doesn't support the necessary protocol
  1556. * level.
  1557. */
  1558. static int hv_pci_protocol_negotiation(struct hv_device *hdev)
  1559. {
  1560. struct pci_version_request *version