pci-hyperv.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389
  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 CPU_AFFINITY_ALL -1ULL
  71. #define PCI_CONFIG_MMIO_LENGTH 0x2000
  72. #define CFG_PAGE_OFFSET 0x1000
  73. #define CFG_PAGE_SIZE (PCI_CONFIG_MMIO_LENGTH - CFG_PAGE_OFFSET)
  74. #define MAX_SUPPORTED_MSI_MESSAGES 0x400
  75. /*
  76. * Message Types
  77. */
  78. enum pci_message_type {
  79. /*
  80. * Version 1.1
  81. */
  82. PCI_MESSAGE_BASE = 0x42490000,
  83. PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
  84. PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
  85. PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
  86. PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
  87. PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
  88. PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
  89. PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
  90. PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
  91. PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
  92. PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
  93. PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
  94. PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
  95. PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
  96. PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
  97. PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
  98. PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
  99. PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
  100. PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
  101. PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
  102. PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
  103. PCI_MESSAGE_MAXIMUM
  104. };
  105. /*
  106. * Structures defining the virtual PCI Express protocol.
  107. */
  108. union pci_version {
  109. struct {
  110. u16 minor_version;
  111. u16 major_version;
  112. } parts;
  113. u32 version;
  114. } __packed;
  115. /*
  116. * Function numbers are 8-bits wide on Express, as interpreted through ARI,
  117. * which is all this driver does. This representation is the one used in
  118. * Windows, which is what is expected when sending this back and forth with
  119. * the Hyper-V parent partition.
  120. */
  121. union win_slot_encoding {
  122. struct {
  123. u32 dev:5;
  124. u32 func:3;
  125. u32 reserved:24;
  126. } bits;
  127. u32 slot;
  128. } __packed;
  129. /*
  130. * Pretty much as defined in the PCI Specifications.
  131. */
  132. struct pci_function_description {
  133. u16 v_id; /* vendor ID */
  134. u16 d_id; /* device ID */
  135. u8 rev;
  136. u8 prog_intf;
  137. u8 subclass;
  138. u8 base_class;
  139. u32 subsystem_id;
  140. union win_slot_encoding win_slot;
  141. u32 ser; /* serial number */
  142. } __packed;
  143. /**
  144. * struct hv_msi_desc
  145. * @vector: IDT entry
  146. * @delivery_mode: As defined in Intel's Programmer's
  147. * Reference Manual, Volume 3, Chapter 8.
  148. * @vector_count: Number of contiguous entries in the
  149. * Interrupt Descriptor Table that are
  150. * occupied by this Message-Signaled
  151. * Interrupt. For "MSI", as first defined
  152. * in PCI 2.2, this can be between 1 and
  153. * 32. For "MSI-X," as first defined in PCI
  154. * 3.0, this must be 1, as each MSI-X table
  155. * entry would have its own descriptor.
  156. * @reserved: Empty space
  157. * @cpu_mask: All the target virtual processors.
  158. */
  159. struct hv_msi_desc {
  160. u8 vector;
  161. u8 delivery_mode;
  162. u16 vector_count;
  163. u32 reserved;
  164. u64 cpu_mask;
  165. } __packed;
  166. /**
  167. * struct tran_int_desc
  168. * @reserved: unused, padding
  169. * @vector_count: same as in hv_msi_desc
  170. * @data: This is the "data payload" value that is
  171. * written by the device when it generates
  172. * a message-signaled interrupt, either MSI
  173. * or MSI-X.
  174. * @address: This is the address to which the data
  175. * payload is written on interrupt
  176. * generation.
  177. */
  178. struct tran_int_desc {
  179. u16 reserved;
  180. u16 vector_count;
  181. u32 data;
  182. u64 address;
  183. } __packed;
  184. /*
  185. * A generic message format for virtual PCI.
  186. * Specific message formats are defined later in the file.
  187. */
  188. struct pci_message {
  189. u32 type;
  190. } __packed;
  191. struct pci_child_message {
  192. struct pci_message message_type;
  193. union win_slot_encoding wslot;
  194. } __packed;
  195. struct pci_incoming_message {
  196. struct vmpacket_descriptor hdr;
  197. struct pci_message message_type;
  198. } __packed;
  199. struct pci_response {
  200. struct vmpacket_descriptor hdr;
  201. s32 status; /* negative values are failures */
  202. } __packed;
  203. struct pci_packet {
  204. void (*completion_func)(void *context, struct pci_response *resp,
  205. int resp_packet_size);
  206. void *compl_ctxt;
  207. struct pci_message message[0];
  208. };
  209. /*
  210. * Specific message types supporting the PCI protocol.
  211. */
  212. /*
  213. * Version negotiation message. Sent from the guest to the host.
  214. * The guest is free to try different versions until the host
  215. * accepts the version.
  216. *
  217. * pci_version: The protocol version requested.
  218. * is_last_attempt: If TRUE, this is the last version guest will request.
  219. * reservedz: Reserved field, set to zero.
  220. */
  221. struct pci_version_request {
  222. struct pci_message message_type;
  223. enum pci_message_type protocol_version;
  224. } __packed;
  225. /*
  226. * Bus D0 Entry. This is sent from the guest to the host when the virtual
  227. * bus (PCI Express port) is ready for action.
  228. */
  229. struct pci_bus_d0_entry {
  230. struct pci_message message_type;
  231. u32 reserved;
  232. u64 mmio_base;
  233. } __packed;
  234. struct pci_bus_relations {
  235. struct pci_incoming_message incoming;
  236. u32 device_count;
  237. struct pci_function_description func[0];
  238. } __packed;
  239. struct pci_q_res_req_response {
  240. struct vmpacket_descriptor hdr;
  241. s32 status; /* negative values are failures */
  242. u32 probed_bar[6];
  243. } __packed;
  244. struct pci_set_power {
  245. struct pci_message message_type;
  246. union win_slot_encoding wslot;
  247. u32 power_state; /* In Windows terms */
  248. u32 reserved;
  249. } __packed;
  250. struct pci_set_power_response {
  251. struct vmpacket_descriptor hdr;
  252. s32 status; /* negative values are failures */
  253. union win_slot_encoding wslot;
  254. u32 resultant_state; /* In Windows terms */
  255. u32 reserved;
  256. } __packed;
  257. struct pci_resources_assigned {
  258. struct pci_message message_type;
  259. union win_slot_encoding wslot;
  260. u8 memory_range[0x14][6]; /* not used here */
  261. u32 msi_descriptors;
  262. u32 reserved[4];
  263. } __packed;
  264. struct pci_create_interrupt {
  265. struct pci_message message_type;
  266. union win_slot_encoding wslot;
  267. struct hv_msi_desc int_desc;
  268. } __packed;
  269. struct pci_create_int_response {
  270. struct pci_response response;
  271. u32 reserved;
  272. struct tran_int_desc int_desc;
  273. } __packed;
  274. struct pci_delete_interrupt {
  275. struct pci_message message_type;
  276. union win_slot_encoding wslot;
  277. struct tran_int_desc int_desc;
  278. } __packed;
  279. struct pci_dev_incoming {
  280. struct pci_incoming_message incoming;
  281. union win_slot_encoding wslot;
  282. } __packed;
  283. struct pci_eject_response {
  284. struct pci_message message_type;
  285. union win_slot_encoding wslot;
  286. u32 status;
  287. } __packed;
  288. static int pci_ring_size = (4 * PAGE_SIZE);
  289. /*
  290. * Definitions or interrupt steering hypercall.
  291. */
  292. #define HV_PARTITION_ID_SELF ((u64)-1)
  293. #define HVCALL_RETARGET_INTERRUPT 0x7e
  294. struct retarget_msi_interrupt {
  295. u64 partition_id; /* use "self" */
  296. u64 device_id;
  297. u32 source; /* 1 for MSI(-X) */
  298. u32 reserved1;
  299. u32 address;
  300. u32 data;
  301. u64 reserved2;
  302. u32 vector;
  303. u32 flags;
  304. u64 vp_mask;
  305. } __packed;
  306. /*
  307. * Driver specific state.
  308. */
  309. enum hv_pcibus_state {
  310. hv_pcibus_init = 0,
  311. hv_pcibus_probed,
  312. hv_pcibus_installed,
  313. hv_pcibus_removed,
  314. hv_pcibus_maximum
  315. };
  316. struct hv_pcibus_device {
  317. struct pci_sysdata sysdata;
  318. enum hv_pcibus_state state;
  319. atomic_t remove_lock;
  320. struct hv_device *hdev;
  321. resource_size_t low_mmio_space;
  322. resource_size_t high_mmio_space;
  323. struct resource *mem_config;
  324. struct resource *low_mmio_res;
  325. struct resource *high_mmio_res;
  326. struct completion *survey_event;
  327. struct completion remove_event;
  328. struct pci_bus *pci_bus;
  329. spinlock_t config_lock; /* Avoid two threads writing index page */
  330. spinlock_t device_list_lock; /* Protect lists below */
  331. void __iomem *cfg_addr;
  332. struct semaphore enum_sem;
  333. struct list_head resources_for_children;
  334. struct list_head children;
  335. struct list_head dr_list;
  336. struct msi_domain_info msi_info;
  337. struct msi_controller msi_chip;
  338. struct irq_domain *irq_domain;
  339. };
  340. /*
  341. * Tracks "Device Relations" messages from the host, which must be both
  342. * processed in order and deferred so that they don't run in the context
  343. * of the incoming packet callback.
  344. */
  345. struct hv_dr_work {
  346. struct work_struct wrk;
  347. struct hv_pcibus_device *bus;
  348. };
  349. struct hv_dr_state {
  350. struct list_head list_entry;
  351. u32 device_count;
  352. struct pci_function_description func[0];
  353. };
  354. enum hv_pcichild_state {
  355. hv_pcichild_init = 0,
  356. hv_pcichild_requirements,
  357. hv_pcichild_resourced,
  358. hv_pcichild_ejecting,
  359. hv_pcichild_maximum
  360. };
  361. enum hv_pcidev_ref_reason {
  362. hv_pcidev_ref_invalid = 0,
  363. hv_pcidev_ref_initial,
  364. hv_pcidev_ref_by_slot,
  365. hv_pcidev_ref_packet,
  366. hv_pcidev_ref_pnp,
  367. hv_pcidev_ref_childlist,
  368. hv_pcidev_irqdata,
  369. hv_pcidev_ref_max
  370. };
  371. struct hv_pci_dev {
  372. /* List protected by pci_rescan_remove_lock */
  373. struct list_head list_entry;
  374. atomic_t refs;
  375. enum hv_pcichild_state state;
  376. struct pci_function_description desc;
  377. bool reported_missing;
  378. struct hv_pcibus_device *hbus;
  379. struct work_struct wrk;
  380. /*
  381. * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
  382. * read it back, for each of the BAR offsets within config space.
  383. */
  384. u32 probed_bar[6];
  385. };
  386. struct hv_pci_compl {
  387. struct completion host_event;
  388. s32 completion_status;
  389. };
  390. /**
  391. * hv_pci_generic_compl() - Invoked for a completion packet
  392. * @context: Set up by the sender of the packet.
  393. * @resp: The response packet
  394. * @resp_packet_size: Size in bytes of the packet
  395. *
  396. * This function is used to trigger an event and report status
  397. * for any message for which the completion packet contains a
  398. * status and nothing else.
  399. */
  400. static void 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. else
  407. comp_pkt->completion_status = -1;
  408. complete(&comp_pkt->host_event);
  409. }
  410. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  411. u32 wslot);
  412. static void get_pcichild(struct hv_pci_dev *hv_pcidev,
  413. enum hv_pcidev_ref_reason reason);
  414. static void put_pcichild(struct hv_pci_dev *hv_pcidev,
  415. enum hv_pcidev_ref_reason reason);
  416. static void get_hvpcibus(struct hv_pcibus_device *hv_pcibus);
  417. static void put_hvpcibus(struct hv_pcibus_device *hv_pcibus);
  418. /**
  419. * devfn_to_wslot() - Convert from Linux PCI slot to Windows
  420. * @devfn: The Linux representation of PCI slot
  421. *
  422. * Windows uses a slightly different representation of PCI slot.
  423. *
  424. * Return: The Windows representation
  425. */
  426. static u32 devfn_to_wslot(int devfn)
  427. {
  428. union win_slot_encoding wslot;
  429. wslot.slot = 0;
  430. wslot.bits.dev = PCI_SLOT(devfn);
  431. wslot.bits.func = PCI_FUNC(devfn);
  432. return wslot.slot;
  433. }
  434. /**
  435. * wslot_to_devfn() - Convert from Windows PCI slot to Linux
  436. * @wslot: The Windows representation of PCI slot
  437. *
  438. * Windows uses a slightly different representation of PCI slot.
  439. *
  440. * Return: The Linux representation
  441. */
  442. static int wslot_to_devfn(u32 wslot)
  443. {
  444. union win_slot_encoding slot_no;
  445. slot_no.slot = wslot;
  446. return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
  447. }
  448. /*
  449. * PCI Configuration Space for these root PCI buses is implemented as a pair
  450. * of pages in memory-mapped I/O space. Writing to the first page chooses
  451. * the PCI function being written or read. Once the first page has been
  452. * written to, the following page maps in the entire configuration space of
  453. * the function.
  454. */
  455. /**
  456. * _hv_pcifront_read_config() - Internal PCI config read
  457. * @hpdev: The PCI driver's representation of the device
  458. * @where: Offset within config space
  459. * @size: Size of the transfer
  460. * @val: Pointer to the buffer receiving the data
  461. */
  462. static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
  463. int size, u32 *val)
  464. {
  465. unsigned long flags;
  466. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  467. /*
  468. * If the attempt is to read the IDs or the ROM BAR, simulate that.
  469. */
  470. if (where + size <= PCI_COMMAND) {
  471. memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
  472. } else if (where >= PCI_CLASS_REVISION && where + size <=
  473. PCI_CACHE_LINE_SIZE) {
  474. memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
  475. PCI_CLASS_REVISION, size);
  476. } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
  477. PCI_ROM_ADDRESS) {
  478. memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
  479. PCI_SUBSYSTEM_VENDOR_ID, size);
  480. } else if (where >= PCI_ROM_ADDRESS && where + size <=
  481. PCI_CAPABILITY_LIST) {
  482. /* ROM BARs are unimplemented */
  483. *val = 0;
  484. } else if (where >= PCI_INTERRUPT_LINE && where + size <=
  485. PCI_INTERRUPT_PIN) {
  486. /*
  487. * Interrupt Line and Interrupt PIN are hard-wired to zero
  488. * because this front-end only supports message-signaled
  489. * interrupts.
  490. */
  491. *val = 0;
  492. } else if (where + size <= CFG_PAGE_SIZE) {
  493. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  494. /* Choose the function to be read. (See comment above) */
  495. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  496. /* Make sure the function was chosen before we start reading. */
  497. mb();
  498. /* Read from that function's config space. */
  499. switch (size) {
  500. case 1:
  501. *val = readb(addr);
  502. break;
  503. case 2:
  504. *val = readw(addr);
  505. break;
  506. default:
  507. *val = readl(addr);
  508. break;
  509. }
  510. /*
  511. * Make sure the write was done before we release the spinlock
  512. * allowing consecutive reads/writes.
  513. */
  514. mb();
  515. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  516. } else {
  517. dev_err(&hpdev->hbus->hdev->device,
  518. "Attempt to read beyond a function's config space.\n");
  519. }
  520. }
  521. /**
  522. * _hv_pcifront_write_config() - Internal PCI config write
  523. * @hpdev: The PCI driver's representation of the device
  524. * @where: Offset within config space
  525. * @size: Size of the transfer
  526. * @val: The data being transferred
  527. */
  528. static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
  529. int size, u32 val)
  530. {
  531. unsigned long flags;
  532. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  533. if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
  534. where + size <= PCI_CAPABILITY_LIST) {
  535. /* SSIDs and ROM BARs are read-only */
  536. } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
  537. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  538. /* Choose the function to be written. (See comment above) */
  539. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  540. /* Make sure the function was chosen before we start writing. */
  541. wmb();
  542. /* Write to that function's config space. */
  543. switch (size) {
  544. case 1:
  545. writeb(val, addr);
  546. break;
  547. case 2:
  548. writew(val, addr);
  549. break;
  550. default:
  551. writel(val, addr);
  552. break;
  553. }
  554. /*
  555. * Make sure the write was done before we release the spinlock
  556. * allowing consecutive reads/writes.
  557. */
  558. mb();
  559. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  560. } else {
  561. dev_err(&hpdev->hbus->hdev->device,
  562. "Attempt to write beyond a function's config space.\n");
  563. }
  564. }
  565. /**
  566. * hv_pcifront_read_config() - Read configuration space
  567. * @bus: PCI Bus structure
  568. * @devfn: Device/function
  569. * @where: Offset from base
  570. * @size: Byte/word/dword
  571. * @val: Value to be read
  572. *
  573. * Return: PCIBIOS_SUCCESSFUL on success
  574. * PCIBIOS_DEVICE_NOT_FOUND on failure
  575. */
  576. static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
  577. int where, int size, u32 *val)
  578. {
  579. struct hv_pcibus_device *hbus =
  580. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  581. struct hv_pci_dev *hpdev;
  582. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  583. if (!hpdev)
  584. return PCIBIOS_DEVICE_NOT_FOUND;
  585. _hv_pcifront_read_config(hpdev, where, size, val);
  586. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  587. return PCIBIOS_SUCCESSFUL;
  588. }
  589. /**
  590. * hv_pcifront_write_config() - Write configuration space
  591. * @bus: PCI Bus structure
  592. * @devfn: Device/function
  593. * @where: Offset from base
  594. * @size: Byte/word/dword
  595. * @val: Value to be written to device
  596. *
  597. * Return: PCIBIOS_SUCCESSFUL on success
  598. * PCIBIOS_DEVICE_NOT_FOUND on failure
  599. */
  600. static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
  601. int where, int size, u32 val)
  602. {
  603. struct hv_pcibus_device *hbus =
  604. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  605. struct hv_pci_dev *hpdev;
  606. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  607. if (!hpdev)
  608. return PCIBIOS_DEVICE_NOT_FOUND;
  609. _hv_pcifront_write_config(hpdev, where, size, val);
  610. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  611. return PCIBIOS_SUCCESSFUL;
  612. }
  613. /* PCIe operations */
  614. static struct pci_ops hv_pcifront_ops = {
  615. .read = hv_pcifront_read_config,
  616. .write = hv_pcifront_write_config,
  617. };
  618. /* Interrupt management hooks */
  619. static void hv_int_desc_free(struct hv_pci_dev *hpdev,
  620. struct tran_int_desc *int_desc)
  621. {
  622. struct pci_delete_interrupt *int_pkt;
  623. struct {
  624. struct pci_packet pkt;
  625. u8 buffer[sizeof(struct pci_delete_interrupt)];
  626. } ctxt;
  627. memset(&ctxt, 0, sizeof(ctxt));
  628. int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
  629. int_pkt->message_type.type =
  630. PCI_DELETE_INTERRUPT_MESSAGE;
  631. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  632. int_pkt->int_desc = *int_desc;
  633. vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
  634. (unsigned long)&ctxt.pkt, VM_PKT_DATA_INBAND, 0);
  635. kfree(int_desc);
  636. }
  637. /**
  638. * hv_msi_free() - Free the MSI.
  639. * @domain: The interrupt domain pointer
  640. * @info: Extra MSI-related context
  641. * @irq: Identifies the IRQ.
  642. *
  643. * The Hyper-V parent partition and hypervisor are tracking the
  644. * messages that are in use, keeping the interrupt redirection
  645. * table up to date. This callback sends a message that frees
  646. * the IRT entry and related tracking nonsense.
  647. */
  648. static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
  649. unsigned int irq)
  650. {
  651. struct hv_pcibus_device *hbus;
  652. struct hv_pci_dev *hpdev;
  653. struct pci_dev *pdev;
  654. struct tran_int_desc *int_desc;
  655. struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
  656. struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
  657. pdev = msi_desc_to_pci_dev(msi);
  658. hbus = info->data;
  659. int_desc = irq_data_get_irq_chip_data(irq_data);
  660. if (!int_desc)
  661. return;
  662. irq_data->chip_data = NULL;
  663. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  664. if (!hpdev) {
  665. kfree(int_desc);
  666. return;
  667. }
  668. hv_int_desc_free(hpdev, int_desc);
  669. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  670. }
  671. static int hv_set_affinity(struct irq_data *data, const struct cpumask *dest,
  672. bool force)
  673. {
  674. struct irq_data *parent = data->parent_data;
  675. return parent->chip->irq_set_affinity(parent, dest, force);
  676. }
  677. void hv_irq_mask(struct irq_data *data)
  678. {
  679. pci_msi_mask_irq(data);
  680. }
  681. /**
  682. * hv_irq_unmask() - "Unmask" the IRQ by setting its current
  683. * affinity.
  684. * @data: Describes the IRQ
  685. *
  686. * Build new a destination for the MSI and make a hypercall to
  687. * update the Interrupt Redirection Table. "Device Logical ID"
  688. * is built out of this PCI bus's instance GUID and the function
  689. * number of the device.
  690. */
  691. void hv_irq_unmask(struct irq_data *data)
  692. {
  693. struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
  694. struct irq_cfg *cfg = irqd_cfg(data);
  695. struct retarget_msi_interrupt params;
  696. struct hv_pcibus_device *hbus;
  697. struct cpumask *dest;
  698. struct pci_bus *pbus;
  699. struct pci_dev *pdev;
  700. int cpu;
  701. dest = irq_data_get_affinity_mask(data);
  702. pdev = msi_desc_to_pci_dev(msi_desc);
  703. pbus = pdev->bus;
  704. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  705. memset(&params, 0, sizeof(params));
  706. params.partition_id = HV_PARTITION_ID_SELF;
  707. params.source = 1; /* MSI(-X) */
  708. params.address = msi_desc->msg.address_lo;
  709. params.data = msi_desc->msg.data;
  710. params.device_id = (hbus->hdev->dev_instance.b[5] << 24) |
  711. (hbus->hdev->dev_instance.b[4] << 16) |
  712. (hbus->hdev->dev_instance.b[7] << 8) |
  713. (hbus->hdev->dev_instance.b[6] & 0xf8) |
  714. PCI_FUNC(pdev->devfn);
  715. params.vector = cfg->vector;
  716. for_each_cpu_and(cpu, dest, cpu_online_mask)
  717. params.vp_mask |= (1ULL << vmbus_cpu_number_to_vp_number(cpu));
  718. hv_do_hypercall(HVCALL_RETARGET_INTERRUPT, &params, NULL);
  719. pci_msi_unmask_irq(data);
  720. }
  721. struct compose_comp_ctxt {
  722. struct hv_pci_compl comp_pkt;
  723. struct tran_int_desc int_desc;
  724. };
  725. static void hv_pci_compose_compl(void *context, struct pci_response *resp,
  726. int resp_packet_size)
  727. {
  728. struct compose_comp_ctxt *comp_pkt = context;
  729. struct pci_create_int_response *int_resp =
  730. (struct pci_create_int_response *)resp;
  731. comp_pkt->comp_pkt.completion_status = resp->status;
  732. comp_pkt->int_desc = int_resp->int_desc;
  733. complete(&comp_pkt->comp_pkt.host_event);
  734. }
  735. /**
  736. * hv_compose_msi_msg() - Supplies a valid MSI address/data
  737. * @data: Everything about this MSI
  738. * @msg: Buffer that is filled in by this function
  739. *
  740. * This function unpacks the IRQ looking for target CPU set, IDT
  741. * vector and mode and sends a message to the parent partition
  742. * asking for a mapping for that tuple in this partition. The
  743. * response supplies a data value and address to which that data
  744. * should be written to trigger that interrupt.
  745. */
  746. static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  747. {
  748. struct irq_cfg *cfg = irqd_cfg(data);
  749. struct hv_pcibus_device *hbus;
  750. struct hv_pci_dev *hpdev;
  751. struct pci_bus *pbus;
  752. struct pci_dev *pdev;
  753. struct pci_create_interrupt *int_pkt;
  754. struct compose_comp_ctxt comp;
  755. struct tran_int_desc *int_desc;
  756. struct cpumask *affinity;
  757. struct {
  758. struct pci_packet pkt;
  759. u8 buffer[sizeof(struct pci_create_interrupt)];
  760. } ctxt;
  761. int cpu;
  762. int ret;
  763. pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
  764. pbus = pdev->bus;
  765. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  766. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  767. if (!hpdev)
  768. goto return_null_message;
  769. /* Free any previous message that might have already been composed. */
  770. if (data->chip_data) {
  771. int_desc = data->chip_data;
  772. data->chip_data = NULL;
  773. hv_int_desc_free(hpdev, int_desc);
  774. }
  775. int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
  776. if (!int_desc)
  777. goto drop_reference;
  778. memset(&ctxt, 0, sizeof(ctxt));
  779. init_completion(&comp.comp_pkt.host_event);
  780. ctxt.pkt.completion_func = hv_pci_compose_compl;
  781. ctxt.pkt.compl_ctxt = &comp;
  782. int_pkt = (struct pci_create_interrupt *)&ctxt.pkt.message;
  783. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
  784. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  785. int_pkt->int_desc.vector = cfg->vector;
  786. int_pkt->int_desc.vector_count = 1;
  787. int_pkt->int_desc.delivery_mode =
  788. (apic->irq_delivery_mode == dest_LowestPrio) ? 1 : 0;
  789. /*
  790. * This bit doesn't have to work on machines with more than 64
  791. * processors because Hyper-V only supports 64 in a guest.
  792. */
  793. affinity = irq_data_get_affinity_mask(data);
  794. if (cpumask_weight(affinity) >= 32) {
  795. int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
  796. } else {
  797. for_each_cpu_and(cpu, affinity, cpu_online_mask) {
  798. int_pkt->int_desc.cpu_mask |=
  799. (1ULL << vmbus_cpu_number_to_vp_number(cpu));
  800. }
  801. }
  802. ret = vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt,
  803. sizeof(*int_pkt), (unsigned long)&ctxt.pkt,
  804. VM_PKT_DATA_INBAND,
  805. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  806. if (ret)
  807. goto free_int_desc;
  808. wait_for_completion(&comp.comp_pkt.host_event);
  809. if (comp.comp_pkt.completion_status < 0) {
  810. dev_err(&hbus->hdev->device,
  811. "Request for interrupt failed: 0x%x",
  812. comp.comp_pkt.completion_status);
  813. goto free_int_desc;
  814. }
  815. /*
  816. * Record the assignment so that this can be unwound later. Using
  817. * irq_set_chip_data() here would be appropriate, but the lock it takes
  818. * is already held.
  819. */
  820. *int_desc = comp.int_desc;
  821. data->chip_data = int_desc;
  822. /* Pass up the result. */
  823. msg->address_hi = comp.int_desc.address >> 32;
  824. msg->address_lo = comp.int_desc.address & 0xffffffff;
  825. msg->data = comp.int_desc.data;
  826. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  827. return;
  828. free_int_desc:
  829. kfree(int_desc);
  830. drop_reference:
  831. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  832. return_null_message:
  833. msg->address_hi = 0;
  834. msg->address_lo = 0;
  835. msg->data = 0;
  836. }
  837. /* HW Interrupt Chip Descriptor */
  838. static struct irq_chip hv_msi_irq_chip = {
  839. .name = "Hyper-V PCIe MSI",
  840. .irq_compose_msi_msg = hv_compose_msi_msg,
  841. .irq_set_affinity = hv_set_affinity,
  842. .irq_ack = irq_chip_ack_parent,
  843. .irq_mask = hv_irq_mask,
  844. .irq_unmask = hv_irq_unmask,
  845. };
  846. static irq_hw_number_t hv_msi_domain_ops_get_hwirq(struct msi_domain_info *info,
  847. msi_alloc_info_t *arg)
  848. {
  849. return arg->msi_hwirq;
  850. }
  851. static struct msi_domain_ops hv_msi_ops = {
  852. .get_hwirq = hv_msi_domain_ops_get_hwirq,
  853. .msi_prepare = pci_msi_prepare,
  854. .set_desc = pci_msi_set_desc,
  855. .msi_free = hv_msi_free,
  856. };
  857. /**
  858. * hv_pcie_init_irq_domain() - Initialize IRQ domain
  859. * @hbus: The root PCI bus
  860. *
  861. * This function creates an IRQ domain which will be used for
  862. * interrupts from devices that have been passed through. These
  863. * devices only support MSI and MSI-X, not line-based interrupts
  864. * or simulations of line-based interrupts through PCIe's
  865. * fabric-layer messages. Because interrupts are remapped, we
  866. * can support multi-message MSI here.
  867. *
  868. * Return: '0' on success and error value on failure
  869. */
  870. static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
  871. {
  872. hbus->msi_info.chip = &hv_msi_irq_chip;
  873. hbus->msi_info.ops = &hv_msi_ops;
  874. hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
  875. MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
  876. MSI_FLAG_PCI_MSIX);
  877. hbus->msi_info.handler = handle_edge_irq;
  878. hbus->msi_info.handler_name = "edge";
  879. hbus->msi_info.data = hbus;
  880. hbus->irq_domain = pci_msi_create_irq_domain(hbus->sysdata.fwnode,
  881. &hbus->msi_info,
  882. x86_vector_domain);
  883. if (!hbus->irq_domain) {
  884. dev_err(&hbus->hdev->device,
  885. "Failed to build an MSI IRQ domain\n");
  886. return -ENODEV;
  887. }
  888. return 0;
  889. }
  890. /**
  891. * get_bar_size() - Get the address space consumed by a BAR
  892. * @bar_val: Value that a BAR returned after -1 was written
  893. * to it.
  894. *
  895. * This function returns the size of the BAR, rounded up to 1
  896. * page. It has to be rounded up because the hypervisor's page
  897. * table entry that maps the BAR into the VM can't specify an
  898. * offset within a page. The invariant is that the hypervisor
  899. * must place any BARs of smaller than page length at the
  900. * beginning of a page.
  901. *
  902. * Return: Size in bytes of the consumed MMIO space.
  903. */
  904. static u64 get_bar_size(u64 bar_val)
  905. {
  906. return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
  907. PAGE_SIZE);
  908. }
  909. /**
  910. * survey_child_resources() - Total all MMIO requirements
  911. * @hbus: Root PCI bus, as understood by this driver
  912. */
  913. static void survey_child_resources(struct hv_pcibus_device *hbus)
  914. {
  915. struct list_head *iter;
  916. struct hv_pci_dev *hpdev;
  917. resource_size_t bar_size = 0;
  918. unsigned long flags;
  919. struct completion *event;
  920. u64 bar_val;
  921. int i;
  922. /* If nobody is waiting on the answer, don't compute it. */
  923. event = xchg(&hbus->survey_event, NULL);
  924. if (!event)
  925. return;
  926. /* If the answer has already been computed, go with it. */
  927. if (hbus->low_mmio_space || hbus->high_mmio_space) {
  928. complete(event);
  929. return;
  930. }
  931. spin_lock_irqsave(&hbus->device_list_lock, flags);
  932. /*
  933. * Due to an interesting quirk of the PCI spec, all memory regions
  934. * for a child device are a power of 2 in size and aligned in memory,
  935. * so it's sufficient to just add them up without tracking alignment.
  936. */
  937. list_for_each(iter, &hbus->children) {
  938. hpdev = container_of(iter, struct hv_pci_dev, list_entry);
  939. for (i = 0; i < 6; i++) {
  940. if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
  941. dev_err(&hbus->hdev->device,
  942. "There's an I/O BAR in this list!\n");
  943. if (hpdev->probed_bar[i] != 0) {
  944. /*
  945. * A probed BAR has all the upper bits set that
  946. * can be changed.
  947. */
  948. bar_val = hpdev->probed_bar[i];
  949. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  950. bar_val |=
  951. ((u64)hpdev->probed_bar[++i] << 32);
  952. else
  953. bar_val |= 0xffffffff00000000ULL;
  954. bar_size = get_bar_size(bar_val);
  955. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  956. hbus->high_mmio_space += bar_size;
  957. else
  958. hbus->low_mmio_space += bar_size;
  959. }
  960. }
  961. }
  962. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  963. complete(event);
  964. }
  965. /**
  966. * prepopulate_bars() - Fill in BARs with defaults
  967. * @hbus: Root PCI bus, as understood by this driver
  968. *
  969. * The core PCI driver code seems much, much happier if the BARs
  970. * for a device have values upon first scan. So fill them in.
  971. * The algorithm below works down from large sizes to small,
  972. * attempting to pack the assignments optimally. The assumption,
  973. * enforced in other parts of the code, is that the beginning of
  974. * the memory-mapped I/O space will be aligned on the largest
  975. * BAR size.
  976. */
  977. static void prepopulate_bars(struct hv_pcibus_device *hbus)
  978. {
  979. resource_size_t high_size = 0;
  980. resource_size_t low_size = 0;
  981. resource_size_t high_base = 0;
  982. resource_size_t low_base = 0;
  983. resource_size_t bar_size;
  984. struct hv_pci_dev *hpdev;
  985. struct list_head *iter;
  986. unsigned long flags;
  987. u64 bar_val;
  988. u32 command;
  989. bool high;
  990. int i;
  991. if (hbus->low_mmio_space) {
  992. low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  993. low_base = hbus->low_mmio_res->start;
  994. }
  995. if (hbus->high_mmio_space) {
  996. high_size = 1ULL <<
  997. (63 - __builtin_clzll(hbus->high_mmio_space));
  998. high_base = hbus->high_mmio_res->start;
  999. }
  1000. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1001. /* Pick addresses for the BARs. */
  1002. do {
  1003. list_for_each(iter, &hbus->children) {
  1004. hpdev = container_of(iter, struct hv_pci_dev,
  1005. list_entry);
  1006. for (i = 0; i < 6; i++) {
  1007. bar_val = hpdev->probed_bar[i];
  1008. if (bar_val == 0)
  1009. continue;
  1010. high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
  1011. if (high) {
  1012. bar_val |=
  1013. ((u64)hpdev->probed_bar[i + 1]
  1014. << 32);
  1015. } else {
  1016. bar_val |= 0xffffffffULL << 32;
  1017. }
  1018. bar_size = get_bar_size(bar_val);
  1019. if (high) {
  1020. if (high_size != bar_size) {
  1021. i++;
  1022. continue;
  1023. }
  1024. _hv_pcifront_write_config(hpdev,
  1025. PCI_BASE_ADDRESS_0 + (4 * i),
  1026. 4,
  1027. (u32)(high_base & 0xffffff00));
  1028. i++;
  1029. _hv_pcifront_write_config(hpdev,
  1030. PCI_BASE_ADDRESS_0 + (4 * i),
  1031. 4, (u32)(high_base >> 32));
  1032. high_base += bar_size;
  1033. } else {
  1034. if (low_size != bar_size)
  1035. continue;
  1036. _hv_pcifront_write_config(hpdev,
  1037. PCI_BASE_ADDRESS_0 + (4 * i),
  1038. 4,
  1039. (u32)(low_base & 0xffffff00));
  1040. low_base += bar_size;
  1041. }
  1042. }
  1043. if (high_size <= 1 && low_size <= 1) {
  1044. /* Set the memory enable bit. */
  1045. _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2,
  1046. &command);
  1047. command |= PCI_COMMAND_MEMORY;
  1048. _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2,
  1049. command);
  1050. break;
  1051. }
  1052. }
  1053. high_size >>= 1;
  1054. low_size >>= 1;
  1055. } while (high_size || low_size);
  1056. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1057. }
  1058. /**
  1059. * create_root_hv_pci_bus() - Expose a new root PCI bus
  1060. * @hbus: Root PCI bus, as understood by this driver
  1061. *
  1062. * Return: 0 on success, -errno on failure
  1063. */
  1064. static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
  1065. {
  1066. /* Register the device */
  1067. hbus->pci_bus = pci_create_root_bus(&hbus->hdev->device,
  1068. 0, /* bus number is always zero */
  1069. &hv_pcifront_ops,
  1070. &hbus->sysdata,
  1071. &hbus->resources_for_children);
  1072. if (!hbus->pci_bus)
  1073. return -ENODEV;
  1074. hbus->pci_bus->msi = &hbus->msi_chip;
  1075. hbus->pci_bus->msi->dev = &hbus->hdev->device;
  1076. pci_lock_rescan_remove();
  1077. pci_scan_child_bus(hbus->pci_bus);
  1078. pci_bus_assign_resources(hbus->pci_bus);
  1079. pci_bus_add_devices(hbus->pci_bus);
  1080. pci_unlock_rescan_remove();
  1081. hbus->state = hv_pcibus_installed;
  1082. return 0;
  1083. }
  1084. struct q_res_req_compl {
  1085. struct completion host_event;
  1086. struct hv_pci_dev *hpdev;
  1087. };
  1088. /**
  1089. * q_resource_requirements() - Query Resource Requirements
  1090. * @context: The completion context.
  1091. * @resp: The response that came from the host.
  1092. * @resp_packet_size: The size in bytes of resp.
  1093. *
  1094. * This function is invoked on completion of a Query Resource
  1095. * Requirements packet.
  1096. */
  1097. static void q_resource_requirements(void *context, struct pci_response *resp,
  1098. int resp_packet_size)
  1099. {
  1100. struct q_res_req_compl *completion = context;
  1101. struct pci_q_res_req_response *q_res_req =
  1102. (struct pci_q_res_req_response *)resp;
  1103. int i;
  1104. if (resp->status < 0) {
  1105. dev_err(&completion->hpdev->hbus->hdev->device,
  1106. "query resource requirements failed: %x\n",
  1107. resp->status);
  1108. } else {
  1109. for (i = 0; i < 6; i++) {
  1110. completion->hpdev->probed_bar[i] =
  1111. q_res_req->probed_bar[i];
  1112. }
  1113. }
  1114. complete(&completion->host_event);
  1115. }
  1116. static void get_pcichild(struct hv_pci_dev *hpdev,
  1117. enum hv_pcidev_ref_reason reason)
  1118. {
  1119. atomic_inc(&hpdev->refs);
  1120. }
  1121. static void put_pcichild(struct hv_pci_dev *hpdev,
  1122. enum hv_pcidev_ref_reason reason)
  1123. {
  1124. if (atomic_dec_and_test(&hpdev->refs))
  1125. kfree(hpdev);
  1126. }
  1127. /**
  1128. * new_pcichild_device() - Create a new child device
  1129. * @hbus: The internal struct tracking this root PCI bus.
  1130. * @desc: The information supplied so far from the host
  1131. * about the device.
  1132. *
  1133. * This function creates the tracking structure for a new child
  1134. * device and kicks off the process of figuring out what it is.
  1135. *
  1136. * Return: Pointer to the new tracking struct
  1137. */
  1138. static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
  1139. struct pci_function_description *desc)
  1140. {
  1141. struct hv_pci_dev *hpdev;
  1142. struct pci_child_message *res_req;
  1143. struct q_res_req_compl comp_pkt;
  1144. union {
  1145. struct pci_packet init_packet;
  1146. u8 buffer[0x100];
  1147. } pkt;
  1148. unsigned long flags;
  1149. int ret;
  1150. hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC);
  1151. if (!hpdev)
  1152. return NULL;
  1153. hpdev->hbus = hbus;
  1154. memset(&pkt, 0, sizeof(pkt));
  1155. init_completion(&comp_pkt.host_event);
  1156. comp_pkt.hpdev = hpdev;
  1157. pkt.init_packet.compl_ctxt = &comp_pkt;
  1158. pkt.init_packet.completion_func = q_resource_requirements;
  1159. res_req = (struct pci_child_message *)&pkt.init_packet.message;
  1160. res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
  1161. res_req->wslot.slot = desc->win_slot.slot;
  1162. ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
  1163. sizeof(struct pci_child_message),
  1164. (unsigned long)&pkt.init_packet,
  1165. VM_PKT_DATA_INBAND,
  1166. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1167. if (ret)
  1168. goto error;
  1169. wait_for_completion(&comp_pkt.host_event);
  1170. hpdev->desc = *desc;
  1171. get_pcichild(hpdev, hv_pcidev_ref_initial);
  1172. get_pcichild(hpdev, hv_pcidev_ref_childlist);
  1173. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1174. list_add_tail(&hpdev->list_entry, &hbus->children);
  1175. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1176. return hpdev;
  1177. error:
  1178. kfree(hpdev);
  1179. return NULL;
  1180. }
  1181. /**
  1182. * get_pcichild_wslot() - Find device from slot
  1183. * @hbus: Root PCI bus, as understood by this driver
  1184. * @wslot: Location on the bus
  1185. *
  1186. * This function looks up a PCI device and returns the internal
  1187. * representation of it. It acquires a reference on it, so that
  1188. * the device won't be deleted while somebody is using it. The
  1189. * caller is responsible for calling put_pcichild() to release
  1190. * this reference.
  1191. *
  1192. * Return: Internal representation of a PCI device
  1193. */
  1194. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  1195. u32 wslot)
  1196. {
  1197. unsigned long flags;
  1198. struct hv_pci_dev *iter, *hpdev = NULL;
  1199. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1200. list_for_each_entry(iter, &hbus->children, list_entry) {
  1201. if (iter->desc.win_slot.slot == wslot) {
  1202. hpdev = iter;
  1203. get_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1204. break;
  1205. }
  1206. }
  1207. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1208. return hpdev;
  1209. }
  1210. /**
  1211. * pci_devices_present_work() - Handle new list of child devices
  1212. * @work: Work struct embedded in struct hv_dr_work
  1213. *
  1214. * "Bus Relations" is the Windows term for "children of this
  1215. * bus." The terminology is preserved here for people trying to
  1216. * debug the interaction between Hyper-V and Linux. This
  1217. * function is called when the parent partition reports a list
  1218. * of functions that should be observed under this PCI Express
  1219. * port (bus).
  1220. *
  1221. * This function updates the list, and must tolerate being
  1222. * called multiple times with the same information. The typical
  1223. * number of child devices is one, with very atypical cases
  1224. * involving three or four, so the algorithms used here can be
  1225. * simple and inefficient.
  1226. *
  1227. * It must also treat the omission of a previously observed device as
  1228. * notification that the device no longer exists.
  1229. *
  1230. * Note that this function is a work item, and it may not be
  1231. * invoked in the order that it was queued. Back to back
  1232. * updates of the list of present devices may involve queuing
  1233. * multiple work items, and this one may run before ones that
  1234. * were sent later. As such, this function only does something
  1235. * if is the last one in the queue.
  1236. */
  1237. static void pci_devices_present_work(struct work_struct *work)
  1238. {
  1239. u32 child_no;
  1240. bool found;
  1241. struct list_head *iter;
  1242. struct pci_function_description *new_desc;
  1243. struct hv_pci_dev *hpdev;
  1244. struct hv_pcibus_device *hbus;
  1245. struct list_head removed;
  1246. struct hv_dr_work *dr_wrk;
  1247. struct hv_dr_state *dr = NULL;
  1248. unsigned long flags;
  1249. dr_wrk = container_of(work, struct hv_dr_work, wrk);
  1250. hbus = dr_wrk->bus;
  1251. kfree(dr_wrk);
  1252. INIT_LIST_HEAD(&removed);
  1253. if (down_interruptible(&hbus->enum_sem)) {
  1254. put_hvpcibus(hbus);
  1255. return;
  1256. }
  1257. /* Pull this off the queue and process it if it was the last one. */
  1258. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1259. while (!list_empty(&hbus->dr_list)) {
  1260. dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
  1261. list_entry);
  1262. list_del(&dr->list_entry);
  1263. /* Throw this away if the list still has stuff in it. */
  1264. if (!list_empty(&hbus->dr_list)) {
  1265. kfree(dr);
  1266. continue;
  1267. }
  1268. }
  1269. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1270. if (!dr) {
  1271. up(&hbus->enum_sem);
  1272. put_hvpcibus(hbus);
  1273. return;
  1274. }
  1275. /* First, mark all existing children as reported missing. */
  1276. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1277. list_for_each(iter, &hbus->children) {
  1278. hpdev = container_of(iter, struct hv_pci_dev,
  1279. list_entry);
  1280. hpdev->reported_missing = true;
  1281. }
  1282. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1283. /* Next, add back any reported devices. */
  1284. for (child_no = 0; child_no < dr->device_count; child_no++) {
  1285. found = false;
  1286. new_desc = &dr->func[child_no];
  1287. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1288. list_for_each(iter, &hbus->children) {
  1289. hpdev = container_of(iter, struct hv_pci_dev,
  1290. list_entry);
  1291. if ((hpdev->desc.win_slot.slot ==
  1292. new_desc->win_slot.slot) &&
  1293. (hpdev->desc.v_id == new_desc->v_id) &&
  1294. (hpdev->desc.d_id == new_desc->d_id) &&
  1295. (hpdev->desc.ser == new_desc->ser)) {
  1296. hpdev->reported_missing = false;
  1297. found = true;
  1298. }
  1299. }
  1300. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1301. if (!found) {
  1302. hpdev = new_pcichild_device(hbus, new_desc);
  1303. if (!hpdev)
  1304. dev_err(&hbus->hdev->device,
  1305. "couldn't record a child device.\n");
  1306. }
  1307. }
  1308. /* Move missing children to a list on the stack. */
  1309. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1310. do {
  1311. found = false;
  1312. list_for_each(iter, &hbus->children) {
  1313. hpdev = container_of(iter, struct hv_pci_dev,
  1314. list_entry);
  1315. if (hpdev->reported_missing) {
  1316. found = true;
  1317. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1318. list_move_tail(&hpdev->list_entry, &removed);
  1319. break;
  1320. }
  1321. }
  1322. } while (found);
  1323. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1324. /* Delete everything that should no longer exist. */
  1325. while (!list_empty(&removed)) {
  1326. hpdev = list_first_entry(&removed, struct hv_pci_dev,
  1327. list_entry);
  1328. list_del(&hpdev->list_entry);
  1329. put_pcichild(hpdev, hv_pcidev_ref_initial);
  1330. }
  1331. switch(hbus->state) {
  1332. case hv_pcibus_installed:
  1333. /*
  1334. * Tell the core to rescan bus
  1335. * because there may have been changes.
  1336. */
  1337. pci_lock_rescan_remove();
  1338. pci_scan_child_bus(hbus->pci_bus);
  1339. pci_unlock_rescan_remove();
  1340. break;
  1341. case hv_pcibus_init:
  1342. case hv_pcibus_probed:
  1343. survey_child_resources(hbus);
  1344. break;
  1345. default:
  1346. break;
  1347. }
  1348. up(&hbus->enum_sem);
  1349. put_hvpcibus(hbus);
  1350. kfree(dr);
  1351. }
  1352. /**
  1353. * hv_pci_devices_present() - Handles list of new children
  1354. * @hbus: Root PCI bus, as understood by this driver
  1355. * @relations: Packet from host listing children
  1356. *
  1357. * This function is invoked whenever a new list of devices for
  1358. * this bus appears.
  1359. */
  1360. static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
  1361. struct pci_bus_relations *relations)
  1362. {
  1363. struct hv_dr_state *dr;
  1364. struct hv_dr_work *dr_wrk;
  1365. unsigned long flags;
  1366. dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
  1367. if (!dr_wrk)
  1368. return;
  1369. dr = kzalloc(offsetof(struct hv_dr_state, func) +
  1370. (sizeof(struct pci_function_description) *
  1371. (relations->device_count)), GFP_NOWAIT);
  1372. if (!dr) {
  1373. kfree(dr_wrk);
  1374. return;
  1375. }
  1376. INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
  1377. dr_wrk->bus = hbus;
  1378. dr->device_count = relations->device_count;
  1379. if (dr->device_count != 0) {
  1380. memcpy(dr->func, relations->func,
  1381. sizeof(struct pci_function_description) *
  1382. dr->device_count);
  1383. }
  1384. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1385. list_add_tail(&dr->list_entry, &hbus->dr_list);
  1386. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1387. get_hvpcibus(hbus);
  1388. schedule_work(&dr_wrk->wrk);
  1389. }
  1390. /**
  1391. * hv_eject_device_work() - Asynchronously handles ejection
  1392. * @work: Work struct embedded in internal device struct
  1393. *
  1394. * This function handles ejecting a device. Windows will
  1395. * attempt to gracefully eject a device, waiting 60 seconds to
  1396. * hear back from the guest OS that this completed successfully.
  1397. * If this timer expires, the device will be forcibly removed.
  1398. */
  1399. static void hv_eject_device_work(struct work_struct *work)
  1400. {
  1401. struct pci_eject_response *ejct_pkt;
  1402. struct hv_pci_dev *hpdev;
  1403. struct pci_dev *pdev;
  1404. unsigned long flags;
  1405. int wslot;
  1406. struct {
  1407. struct pci_packet pkt;
  1408. u8 buffer[sizeof(struct pci_eject_response)];
  1409. } ctxt;
  1410. hpdev = container_of(work, struct hv_pci_dev, wrk);
  1411. if (hpdev->state != hv_pcichild_ejecting) {
  1412. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1413. return;
  1414. }
  1415. /*
  1416. * Ejection can come before or after the PCI bus has been set up, so
  1417. * attempt to find it and tear down the bus state, if it exists. This
  1418. * must be done without constructs like pci_domain_nr(hbus->pci_bus)
  1419. * because hbus->pci_bus may not exist yet.
  1420. */
  1421. wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
  1422. pdev = pci_get_domain_bus_and_slot(hpdev->hbus->sysdata.domain, 0,
  1423. wslot);
  1424. if (pdev) {
  1425. pci_lock_rescan_remove();
  1426. pci_stop_and_remove_bus_device(pdev);
  1427. pci_dev_put(pdev);
  1428. pci_unlock_rescan_remove();
  1429. }
  1430. memset(&ctxt, 0, sizeof(ctxt));
  1431. ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
  1432. ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
  1433. ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  1434. vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
  1435. sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
  1436. VM_PKT_DATA_INBAND, 0);
  1437. spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
  1438. list_del(&hpdev->list_entry);
  1439. spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
  1440. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1441. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1442. put_hvpcibus(hpdev->hbus);
  1443. }
  1444. /**
  1445. * hv_pci_eject_device() - Handles device ejection
  1446. * @hpdev: Internal device tracking struct
  1447. *
  1448. * This function is invoked when an ejection packet arrives. It
  1449. * just schedules work so that we don't re-enter the packet
  1450. * delivery code handling the ejection.
  1451. */
  1452. static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
  1453. {
  1454. hpdev->state = hv_pcichild_ejecting;
  1455. get_pcichild(hpdev, hv_pcidev_ref_pnp);
  1456. INIT_WORK(&hpdev->wrk, hv_eject_device_work);
  1457. get_hvpcibus(hpdev->hbus);
  1458. schedule_work(&hpdev->wrk);
  1459. }
  1460. /**
  1461. * hv_pci_onchannelcallback() - Handles incoming packets
  1462. * @context: Internal bus tracking struct
  1463. *
  1464. * This function is invoked whenever the host sends a packet to
  1465. * this channel (which is private to this root PCI bus).
  1466. */
  1467. static void hv_pci_onchannelcallback(void *context)
  1468. {
  1469. const int packet_size = 0x100;
  1470. int ret;
  1471. struct hv_pcibus_device *hbus = context;
  1472. u32 bytes_recvd;
  1473. u64 req_id;
  1474. struct vmpacket_descriptor *desc;
  1475. unsigned char *buffer;
  1476. int bufferlen = packet_size;
  1477. struct pci_packet *comp_packet;
  1478. struct pci_response *response;
  1479. struct pci_incoming_message *new_message;
  1480. struct pci_bus_relations *bus_rel;
  1481. struct pci_dev_incoming *dev_message;
  1482. struct hv_pci_dev *hpdev;
  1483. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  1484. if (!buffer)
  1485. return;
  1486. while (1) {
  1487. ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
  1488. bufferlen, &bytes_recvd, &req_id);
  1489. if (ret == -ENOBUFS) {
  1490. kfree(buffer);
  1491. /* Handle large packet */
  1492. bufferlen = bytes_recvd;
  1493. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  1494. if (!buffer)
  1495. return;
  1496. continue;
  1497. }
  1498. /* Zero length indicates there are no more packets. */
  1499. if (ret || !bytes_recvd)
  1500. break;
  1501. /*
  1502. * All incoming packets must be at least as large as a
  1503. * response.
  1504. */
  1505. if (bytes_recvd <= sizeof(struct pci_response))
  1506. continue;
  1507. desc = (struct vmpacket_descriptor *)buffer;
  1508. switch (desc->type) {
  1509. case VM_PKT_COMP:
  1510. /*
  1511. * The host is trusted, and thus it's safe to interpret
  1512. * this transaction ID as a pointer.
  1513. */
  1514. comp_packet = (struct pci_packet *)req_id;
  1515. response = (struct pci_response *)buffer;
  1516. comp_packet->completion_func(comp_packet->compl_ctxt,
  1517. response,
  1518. bytes_recvd);
  1519. break;
  1520. case VM_PKT_DATA_INBAND:
  1521. new_message = (struct pci_incoming_message *)buffer;
  1522. switch (new_message->message_type.type) {
  1523. case PCI_BUS_RELATIONS:
  1524. bus_rel = (struct pci_bus_relations *)buffer;
  1525. if (bytes_recvd <
  1526. offsetof(struct pci_bus_relations, func) +
  1527. (sizeof(struct pci_function_description) *
  1528. (bus_rel->device_count))) {
  1529. dev_err(&hbus->hdev->device,
  1530. "bus relations too small\n");
  1531. break;
  1532. }
  1533. hv_pci_devices_present(hbus, bus_rel);
  1534. break;
  1535. case PCI_EJECT:
  1536. dev_message = (struct pci_dev_incoming *)buffer;
  1537. hpdev = get_pcichild_wslot(hbus,
  1538. dev_message->wslot.slot);
  1539. if (hpdev) {
  1540. hv_pci_eject_device(hpdev);
  1541. put_pcichild(hpdev,
  1542. hv_pcidev_ref_by_slot);
  1543. }
  1544. break;
  1545. default:
  1546. dev_warn(&hbus->hdev->device,
  1547. "Unimplemented protocol message %x\n",
  1548. new_message->message_type.type);
  1549. break;
  1550. }
  1551. break;
  1552. default:
  1553. dev_err(&hbus->hdev->device,
  1554. "unhandled packet type %d, tid %llx len %d\n",
  1555. desc->type, req_id, bytes_recvd);
  1556. break;
  1557. }
  1558. }
  1559. kfree(buffer);
  1560. }
  1561. /**
  1562. * hv_pci_protocol_negotiation() - Set up protocol
  1563. * @hdev: VMBus's tracking struct for this root PCI bus
  1564. *
  1565. * This driver is intended to support running on Windows 10
  1566. * (server) and later versions. It will not run on earlier
  1567. * versions, as they assume that many of the operations which
  1568. * Linux needs accomplished with a spinlock held were done via
  1569. * asynchronous messaging via VMBus. Windows 10 increases the
  1570. * surface area of PCI emulation so that these actions can take
  1571. * place by suspending a virtual processor for their duration.
  1572. *
  1573. * This function negotiates the channel protocol version,
  1574. * failing if the host doesn't support the necessary protocol
  1575. * level.
  1576. */
  1577. static int hv_pci_protocol_negotiation(struct hv_device *hdev)
  1578. {
  1579. struct pci_version_request *version_req;
  1580. struct hv_pci_compl comp_pkt;
  1581. struct pci_packet *pkt;
  1582. int ret;
  1583. /*
  1584. * Initiate the handshake with the host and negotiate
  1585. * a version that the host can support. We start with the
  1586. * highest version number and go down if the host cannot
  1587. * support it.
  1588. */
  1589. pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
  1590. if (!pkt)
  1591. return -ENOMEM;
  1592. init_completion(&comp_pkt.host_event);
  1593. pkt->completion_func = hv_pci_generic_compl;
  1594. pkt->compl_ctxt = &comp_pkt;
  1595. version_req = (struct pci_version_request *)&pkt->message;
  1596. version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
  1597. version_req->protocol_version = PCI_PROTOCOL_VERSION_CURRENT;
  1598. ret = vmbus_sendpacket(hdev->channel, version_req,
  1599. sizeof(struct pci_version_request),
  1600. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  1601. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1602. if (ret)
  1603. goto exit;
  1604. wait_for_completion(&comp_pkt.host_event);
  1605. if (comp_pkt.completion_status < 0) {
  1606. dev_err(&hdev->device,
  1607. "PCI Pass-through VSP failed version request %x\n",
  1608. comp_pkt.completion_status);
  1609. ret = -EPROTO;
  1610. goto exit;
  1611. }
  1612. ret = 0;
  1613. exit:
  1614. kfree(pkt);
  1615. return ret;
  1616. }
  1617. /**
  1618. * hv_pci_free_bridge_windows() - Release memory regions for the
  1619. * bus
  1620. * @hbus: Root PCI bus, as understood by this driver
  1621. */
  1622. static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
  1623. {
  1624. /*
  1625. * Set the resources back to the way they looked when they
  1626. * were allocated by setting IORESOURCE_BUSY again.
  1627. */
  1628. if (hbus->low_mmio_space && hbus->low_mmio_res) {
  1629. hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
  1630. vmbus_free_mmio(hbus->low_mmio_res->start,
  1631. resource_size(hbus->low_mmio_res));
  1632. }
  1633. if (hbus->high_mmio_space && hbus->high_mmio_res) {
  1634. hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
  1635. vmbus_free_mmio(hbus->high_mmio_res->start,
  1636. resource_size(hbus->high_mmio_res));
  1637. }
  1638. }
  1639. /**
  1640. * hv_pci_allocate_bridge_windows() - Allocate memory regions
  1641. * for the bus
  1642. * @hbus: Root PCI bus, as understood by this driver
  1643. *
  1644. * This function calls vmbus_allocate_mmio(), which is itself a
  1645. * bit of a compromise. Ideally, we might change the pnp layer
  1646. * in the kernel such that it comprehends either PCI devices
  1647. * which are "grandchildren of ACPI," with some intermediate bus
  1648. * node (in this case, VMBus) or change it such that it
  1649. * understands VMBus. The pnp layer, however, has been declared
  1650. * deprecated, and not subject to change.
  1651. *
  1652. * The workaround, implemented here, is to ask VMBus to allocate
  1653. * MMIO space for this bus. VMBus itself knows which ranges are
  1654. * appropriate by looking at its own ACPI objects. Then, after
  1655. * these ranges are claimed, they're modified to look like they
  1656. * would have looked if the ACPI and pnp code had allocated
  1657. * bridge windows. These descriptors have to exist in this form
  1658. * in order to satisfy the code which will get invoked when the
  1659. * endpoint PCI function driver calls request_mem_region() or
  1660. * request_mem_region_exclusive().
  1661. *
  1662. * Return: 0 on success, -errno on failure
  1663. */
  1664. static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
  1665. {
  1666. resource_size_t align;
  1667. int ret;
  1668. if (hbus->low_mmio_space) {
  1669. align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  1670. ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
  1671. (u64)(u32)0xffffffff,
  1672. hbus->low_mmio_space,
  1673. align, false);
  1674. if (ret) {
  1675. dev_err(&hbus->hdev->device,
  1676. "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
  1677. hbus->low_mmio_space);
  1678. return ret;
  1679. }
  1680. /* Modify this resource to become a bridge window. */
  1681. hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
  1682. hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
  1683. pci_add_resource(&hbus->resources_for_children,
  1684. hbus->low_mmio_res);
  1685. }
  1686. if (hbus->high_mmio_space) {
  1687. align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
  1688. ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
  1689. 0x100000000, -1,
  1690. hbus->high_mmio_space, align,
  1691. false);
  1692. if (ret) {
  1693. dev_err(&hbus->hdev->device,
  1694. "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
  1695. hbus->high_mmio_space);
  1696. goto release_low_mmio;
  1697. }
  1698. /* Modify this resource to become a bridge window. */
  1699. hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
  1700. hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
  1701. pci_add_resource(&hbus->resources_for_children,
  1702. hbus->high_mmio_res);
  1703. }
  1704. return 0;
  1705. release_low_mmio:
  1706. if (hbus->low_mmio_res) {
  1707. vmbus_free_mmio(hbus->low_mmio_res->start,
  1708. resource_size(hbus->low_mmio_res));
  1709. }
  1710. return ret;
  1711. }
  1712. /**
  1713. * hv_allocate_config_window() - Find MMIO space for PCI Config
  1714. * @hbus: Root PCI bus, as understood by this driver
  1715. *
  1716. * This function claims memory-mapped I/O space for accessing
  1717. * configuration space for the functions on this bus.
  1718. *
  1719. * Return: 0 on success, -errno on failure
  1720. */
  1721. static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
  1722. {
  1723. int ret;
  1724. /*
  1725. * Set up a region of MMIO space to use for accessing configuration
  1726. * space.
  1727. */
  1728. ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
  1729. PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
  1730. if (ret)
  1731. return ret;
  1732. /*
  1733. * vmbus_allocate_mmio() gets used for allocating both device endpoint
  1734. * resource claims (those which cannot be overlapped) and the ranges
  1735. * which are valid for the children of this bus, which are intended
  1736. * to be overlapped by those children. Set the flag on this claim
  1737. * meaning that this region can't be overlapped.
  1738. */
  1739. hbus->mem_config->flags |= IORESOURCE_BUSY;
  1740. return 0;
  1741. }
  1742. static void hv_free_config_window(struct hv_pcibus_device *hbus)
  1743. {
  1744. vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
  1745. }
  1746. /**
  1747. * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
  1748. * @hdev: VMBus's tracking struct for this root PCI bus
  1749. *
  1750. * Return: 0 on success, -errno on failure
  1751. */
  1752. static int hv_pci_enter_d0(struct hv_device *hdev)
  1753. {
  1754. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1755. struct pci_bus_d0_entry *d0_entry;
  1756. struct hv_pci_compl comp_pkt;
  1757. struct pci_packet *pkt;
  1758. int ret;
  1759. /*
  1760. * Tell the host that the bus is ready to use, and moved into the
  1761. * powered-on state. This includes telling the host which region
  1762. * of memory-mapped I/O space has been chosen for configuration space
  1763. * access.
  1764. */
  1765. pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
  1766. if (!pkt)
  1767. return -ENOMEM;
  1768. init_completion(&comp_pkt.host_event);
  1769. pkt->completion_func = hv_pci_generic_compl;
  1770. pkt->compl_ctxt = &comp_pkt;
  1771. d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
  1772. d0_entry->message_type.type = PCI_BUS_D0ENTRY;
  1773. d0_entry->mmio_base = hbus->mem_config->start;
  1774. ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
  1775. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  1776. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1777. if (ret)
  1778. goto exit;
  1779. wait_for_completion(&comp_pkt.host_event);
  1780. if (comp_pkt.completion_status < 0) {
  1781. dev_err(&hdev->device,
  1782. "PCI Pass-through VSP failed D0 Entry with status %x\n",
  1783. comp_pkt.completion_status);
  1784. ret = -EPROTO;
  1785. goto exit;
  1786. }
  1787. ret = 0;
  1788. exit:
  1789. kfree(pkt);
  1790. return ret;
  1791. }
  1792. /**
  1793. * hv_pci_query_relations() - Ask host to send list of child
  1794. * devices
  1795. * @hdev: VMBus's tracking struct for this root PCI bus
  1796. *
  1797. * Return: 0 on success, -errno on failure
  1798. */
  1799. static int hv_pci_query_relations(struct hv_device *hdev)
  1800. {
  1801. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1802. struct pci_message message;
  1803. struct completion comp;
  1804. int ret;
  1805. /* Ask the host to send along the list of child devices */
  1806. init_completion(&comp);
  1807. if (cmpxchg(&hbus->survey_event, NULL, &comp))
  1808. return -ENOTEMPTY;
  1809. memset(&message, 0, sizeof(message));
  1810. message.type = PCI_QUERY_BUS_RELATIONS;
  1811. ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
  1812. 0, VM_PKT_DATA_INBAND, 0);
  1813. if (ret)
  1814. return ret;
  1815. wait_for_completion(&comp);
  1816. return 0;
  1817. }
  1818. /**
  1819. * hv_send_resources_allocated() - Report local resource choices
  1820. * @hdev: VMBus's tracking struct for this root PCI bus
  1821. *
  1822. * The host OS is expecting to be sent a request as a message
  1823. * which contains all the resources that the device will use.
  1824. * The response contains those same resources, "translated"
  1825. * which is to say, the values which should be used by the
  1826. * hardware, when it delivers an interrupt. (MMIO resources are
  1827. * used in local terms.) This is nice for Windows, and lines up
  1828. * with the FDO/PDO split, which doesn't exist in Linux. Linux
  1829. * is deeply expecting to scan an emulated PCI configuration
  1830. * space. So this message is sent here only to drive the state
  1831. * machine on the host forward.
  1832. *
  1833. * Return: 0 on success, -errno on failure
  1834. */
  1835. static int hv_send_resources_allocated(struct hv_device *hdev)
  1836. {
  1837. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1838. struct pci_resources_assigned *res_assigned;
  1839. struct hv_pci_compl comp_pkt;
  1840. struct hv_pci_dev *hpdev;
  1841. struct pci_packet *pkt;
  1842. u32 wslot;
  1843. int ret;
  1844. pkt = kmalloc(sizeof(*pkt) + sizeof(*res_assigned), GFP_KERNEL);
  1845. if (!pkt)
  1846. return -ENOMEM;
  1847. ret = 0;
  1848. for (wslot = 0; wslot < 256; wslot++) {
  1849. hpdev = get_pcichild_wslot(hbus, wslot);
  1850. if (!hpdev)
  1851. continue;
  1852. memset(pkt, 0, sizeof(*pkt) + sizeof(*res_assigned));
  1853. init_completion(&comp_pkt.host_event);
  1854. pkt->completion_func = hv_pci_generic_compl;
  1855. pkt->compl_ctxt = &comp_pkt;
  1856. res_assigned = (struct pci_resources_assigned *)&pkt->message;
  1857. res_assigned->message_type.type = PCI_RESOURCES_ASSIGNED;
  1858. res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
  1859. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1860. ret = vmbus_sendpacket(
  1861. hdev->channel, &pkt->message,
  1862. sizeof(*res_assigned),
  1863. (unsigned long)pkt,
  1864. VM_PKT_DATA_INBAND,
  1865. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1866. if (ret)
  1867. break;
  1868. wait_for_completion(&comp_pkt.host_event);
  1869. if (comp_pkt.completion_status < 0) {
  1870. ret = -EPROTO;
  1871. dev_err(&hdev->device,
  1872. "resource allocated returned 0x%x",
  1873. comp_pkt.completion_status);
  1874. break;
  1875. }
  1876. }
  1877. kfree(pkt);
  1878. return ret;
  1879. }
  1880. /**
  1881. * hv_send_resources_released() - Report local resources
  1882. * released
  1883. * @hdev: VMBus's tracking struct for this root PCI bus
  1884. *
  1885. * Return: 0 on success, -errno on failure
  1886. */
  1887. static int hv_send_resources_released(struct hv_device *hdev)
  1888. {
  1889. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1890. struct pci_child_message pkt;
  1891. struct hv_pci_dev *hpdev;
  1892. u32 wslot;
  1893. int ret;
  1894. for (wslot = 0; wslot < 256; wslot++) {
  1895. hpdev = get_pcichild_wslot(hbus, wslot);
  1896. if (!hpdev)
  1897. continue;
  1898. memset(&pkt, 0, sizeof(pkt));
  1899. pkt.message_type.type = PCI_RESOURCES_RELEASED;
  1900. pkt.wslot.slot = hpdev->desc.win_slot.slot;
  1901. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1902. ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
  1903. VM_PKT_DATA_INBAND, 0);
  1904. if (ret)
  1905. return ret;
  1906. }
  1907. return 0;
  1908. }
  1909. static void get_hvpcibus(struct hv_pcibus_device *hbus)
  1910. {
  1911. atomic_inc(&hbus->remove_lock);
  1912. }
  1913. static void put_hvpcibus(struct hv_pcibus_device *hbus)
  1914. {
  1915. if (atomic_dec_and_test(&hbus->remove_lock))
  1916. complete(&hbus->remove_event);
  1917. }
  1918. /**
  1919. * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
  1920. * @hdev: VMBus's tracking struct for this root PCI bus
  1921. * @dev_id: Identifies the device itself
  1922. *
  1923. * Return: 0 on success, -errno on failure
  1924. */
  1925. static int hv_pci_probe(struct hv_device *hdev,
  1926. const struct hv_vmbus_device_id *dev_id)
  1927. {
  1928. struct hv_pcibus_device *hbus;
  1929. int ret;
  1930. hbus = kzalloc(sizeof(*hbus), GFP_KERNEL);
  1931. if (!hbus)
  1932. return -ENOMEM;
  1933. hbus->state = hv_pcibus_init;
  1934. /*
  1935. * The PCI bus "domain" is what is called "segment" in ACPI and
  1936. * other specs. Pull it from the instance ID, to get something
  1937. * unique. Bytes 8 and 9 are what is used in Windows guests, so
  1938. * do the same thing for consistency. Note that, since this code
  1939. * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
  1940. * that (1) the only domain in use for something that looks like
  1941. * a physical PCI bus (which is actually emulated by the
  1942. * hypervisor) is domain 0 and (2) there will be no overlap
  1943. * between domains derived from these instance IDs in the same
  1944. * VM.
  1945. */
  1946. hbus->sysdata.domain = hdev->dev_instance.b[9] |
  1947. hdev->dev_instance.b[8] << 8;
  1948. hbus->hdev = hdev;
  1949. atomic_inc(&hbus->remove_lock);
  1950. INIT_LIST_HEAD(&hbus->children);
  1951. INIT_LIST_HEAD(&hbus->dr_list);
  1952. INIT_LIST_HEAD(&hbus->resources_for_children);
  1953. spin_lock_init(&hbus->config_lock);
  1954. spin_lock_init(&hbus->device_list_lock);
  1955. sema_init(&hbus->enum_sem, 1);
  1956. init_completion(&hbus->remove_event);
  1957. ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
  1958. hv_pci_onchannelcallback, hbus);
  1959. if (ret)
  1960. goto free_bus;
  1961. hv_set_drvdata(hdev, hbus);
  1962. ret = hv_pci_protocol_negotiation(hdev);
  1963. if (ret)
  1964. goto close;
  1965. ret = hv_allocate_config_window(hbus);
  1966. if (ret)
  1967. goto close;
  1968. hbus->cfg_addr = ioremap(hbus->mem_config->start,
  1969. PCI_CONFIG_MMIO_LENGTH);
  1970. if (!hbus->cfg_addr) {
  1971. dev_err(&hdev->device,
  1972. "Unable to map a virtual address for config space\n");
  1973. ret = -ENOMEM;
  1974. goto free_config;
  1975. }
  1976. hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
  1977. if (!hbus->sysdata.fwnode) {
  1978. ret = -ENOMEM;
  1979. goto unmap;
  1980. }
  1981. ret = hv_pcie_init_irq_domain(hbus);
  1982. if (ret)
  1983. goto free_fwnode;
  1984. ret = hv_pci_query_relations(hdev);
  1985. if (ret)
  1986. goto free_irq_domain;
  1987. ret = hv_pci_enter_d0(hdev);
  1988. if (ret)
  1989. goto free_irq_domain;
  1990. ret = hv_pci_allocate_bridge_windows(hbus);
  1991. if (ret)
  1992. goto free_irq_domain;
  1993. ret = hv_send_resources_allocated(hdev);
  1994. if (ret)
  1995. goto free_windows;
  1996. prepopulate_bars(hbus);
  1997. hbus->state = hv_pcibus_probed;
  1998. ret = create_root_hv_pci_bus(hbus);
  1999. if (ret)
  2000. goto free_windows;
  2001. return 0;
  2002. free_windows:
  2003. hv_pci_free_bridge_windows(hbus);
  2004. free_irq_domain:
  2005. irq_domain_remove(hbus->irq_domain);
  2006. free_fwnode:
  2007. irq_domain_free_fwnode(hbus->sysdata.fwnode);
  2008. unmap:
  2009. iounmap(hbus->cfg_addr);
  2010. free_config:
  2011. hv_free_config_window(hbus);
  2012. close:
  2013. vmbus_close(hdev->channel);
  2014. free_bus:
  2015. kfree(hbus);
  2016. return ret;
  2017. }
  2018. /**
  2019. * hv_pci_remove() - Remove routine for this VMBus channel
  2020. * @hdev: VMBus's tracking struct for this root PCI bus
  2021. *
  2022. * Return: 0 on success, -errno on failure
  2023. */
  2024. static int hv_pci_remove(struct hv_device *hdev)
  2025. {
  2026. int ret;
  2027. struct hv_pcibus_device *hbus;
  2028. union {
  2029. struct pci_packet teardown_packet;
  2030. u8 buffer[0x100];
  2031. } pkt;
  2032. struct pci_bus_relations relations;
  2033. struct hv_pci_compl comp_pkt;
  2034. hbus = hv_get_drvdata(hdev);
  2035. memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
  2036. init_completion(&comp_pkt.host_event);
  2037. pkt.teardown_packet.completion_func = hv_pci_generic_compl;
  2038. pkt.teardown_packet.compl_ctxt = &comp_pkt;
  2039. pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
  2040. ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
  2041. sizeof(struct pci_message),
  2042. (unsigned long)&pkt.teardown_packet,
  2043. VM_PKT_DATA_INBAND,
  2044. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2045. if (!ret)
  2046. wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
  2047. if (hbus->state == hv_pcibus_installed) {
  2048. /* Remove the bus from PCI's point of view. */
  2049. pci_lock_rescan_remove();
  2050. pci_stop_root_bus(hbus->pci_bus);
  2051. pci_remove_root_bus(hbus->pci_bus);
  2052. pci_unlock_rescan_remove();
  2053. hbus->state = hv_pcibus_removed;
  2054. }
  2055. ret = hv_send_resources_released(hdev);
  2056. if (ret)
  2057. dev_err(&hdev->device,
  2058. "Couldn't send resources released packet(s)\n");
  2059. vmbus_close(hdev->channel);
  2060. /* Delete any children which might still exist. */
  2061. memset(&relations, 0, sizeof(relations));
  2062. hv_pci_devices_present(hbus, &relations);
  2063. iounmap(hbus->cfg_addr);
  2064. hv_free_config_window(hbus);
  2065. pci_free_resource_list(&hbus->resources_for_children);
  2066. hv_pci_free_bridge_windows(hbus);
  2067. irq_domain_remove(hbus->irq_domain);
  2068. irq_domain_free_fwnode(hbus->sysdata.fwnode);
  2069. put_hvpcibus(hbus);
  2070. wait_for_completion(&hbus->remove_event);
  2071. kfree(hbus);
  2072. return 0;
  2073. }
  2074. static const struct hv_vmbus_device_id hv_pci_id_table[] = {
  2075. /* PCI Pass-through Class ID */
  2076. /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
  2077. { HV_PCIE_GUID, },
  2078. { },
  2079. };
  2080. MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
  2081. static struct hv_driver hv_pci_drv = {
  2082. .name = "hv_pci",
  2083. .id_table = hv_pci_id_table,
  2084. .probe = hv_pci_probe,
  2085. .remove = hv_pci_remove,
  2086. };
  2087. static void __exit exit_hv_pci_drv(void)
  2088. {
  2089. vmbus_driver_unregister(&hv_pci_drv);
  2090. }
  2091. static int __init init_hv_pci_drv(void)
  2092. {
  2093. return vmbus_driver_register(&hv_pci_drv);
  2094. }
  2095. module_init(init_hv_pci_drv);
  2096. module_exit(exit_hv_pci_drv);
  2097. MODULE_DESCRIPTION("Hyper-V PCI");
  2098. MODULE_LICENSE("GPL v2");