pci-hyperv.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399
  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 dev:5;
  123. u32 func:3;
  124. u32 reserved:24;
  125. } bits;
  126. u32 slot;
  127. } __packed;
  128. /*
  129. * Pretty much as defined in the PCI Specifications.
  130. */
  131. struct pci_function_description {
  132. u16 v_id; /* vendor ID */
  133. u16 d_id; /* device ID */
  134. u8 rev;
  135. u8 prog_intf;
  136. u8 subclass;
  137. u8 base_class;
  138. u32 subsystem_id;
  139. union win_slot_encoding win_slot;
  140. u32 ser; /* serial number */
  141. } __packed;
  142. /**
  143. * struct hv_msi_desc
  144. * @vector: IDT entry
  145. * @delivery_mode: As defined in Intel's Programmer's
  146. * Reference Manual, Volume 3, Chapter 8.
  147. * @vector_count: Number of contiguous entries in the
  148. * Interrupt Descriptor Table that are
  149. * occupied by this Message-Signaled
  150. * Interrupt. For "MSI", as first defined
  151. * in PCI 2.2, this can be between 1 and
  152. * 32. For "MSI-X," as first defined in PCI
  153. * 3.0, this must be 1, as each MSI-X table
  154. * entry would have its own descriptor.
  155. * @reserved: Empty space
  156. * @cpu_mask: All the target virtual processors.
  157. */
  158. struct hv_msi_desc {
  159. u8 vector;
  160. u8 delivery_mode;
  161. u16 vector_count;
  162. u32 reserved;
  163. u64 cpu_mask;
  164. } __packed;
  165. /**
  166. * struct tran_int_desc
  167. * @reserved: unused, padding
  168. * @vector_count: same as in hv_msi_desc
  169. * @data: This is the "data payload" value that is
  170. * written by the device when it generates
  171. * a message-signaled interrupt, either MSI
  172. * or MSI-X.
  173. * @address: This is the address to which the data
  174. * payload is written on interrupt
  175. * generation.
  176. */
  177. struct tran_int_desc {
  178. u16 reserved;
  179. u16 vector_count;
  180. u32 data;
  181. u64 address;
  182. } __packed;
  183. /*
  184. * A generic message format for virtual PCI.
  185. * Specific message formats are defined later in the file.
  186. */
  187. struct pci_message {
  188. u32 type;
  189. } __packed;
  190. struct pci_child_message {
  191. struct pci_message message_type;
  192. union win_slot_encoding wslot;
  193. } __packed;
  194. struct pci_incoming_message {
  195. struct vmpacket_descriptor hdr;
  196. struct pci_message message_type;
  197. } __packed;
  198. struct pci_response {
  199. struct vmpacket_descriptor hdr;
  200. s32 status; /* negative values are failures */
  201. } __packed;
  202. struct pci_packet {
  203. void (*completion_func)(void *context, struct pci_response *resp,
  204. int resp_packet_size);
  205. void *compl_ctxt;
  206. struct pci_message message[0];
  207. };
  208. /*
  209. * Specific message types supporting the PCI protocol.
  210. */
  211. /*
  212. * Version negotiation message. Sent from the guest to the host.
  213. * The guest is free to try different versions until the host
  214. * accepts the version.
  215. *
  216. * pci_version: The protocol version requested.
  217. * is_last_attempt: If TRUE, this is the last version guest will request.
  218. * reservedz: Reserved field, set to zero.
  219. */
  220. struct pci_version_request {
  221. struct pci_message message_type;
  222. enum pci_message_type protocol_version;
  223. } __packed;
  224. /*
  225. * Bus D0 Entry. This is sent from the guest to the host when the virtual
  226. * bus (PCI Express port) is ready for action.
  227. */
  228. struct pci_bus_d0_entry {
  229. struct pci_message message_type;
  230. u32 reserved;
  231. u64 mmio_base;
  232. } __packed;
  233. struct pci_bus_relations {
  234. struct pci_incoming_message incoming;
  235. u32 device_count;
  236. struct pci_function_description func[0];
  237. } __packed;
  238. struct pci_q_res_req_response {
  239. struct vmpacket_descriptor hdr;
  240. s32 status; /* negative values are failures */
  241. u32 probed_bar[6];
  242. } __packed;
  243. struct pci_set_power {
  244. struct pci_message message_type;
  245. union win_slot_encoding wslot;
  246. u32 power_state; /* In Windows terms */
  247. u32 reserved;
  248. } __packed;
  249. struct pci_set_power_response {
  250. struct vmpacket_descriptor hdr;
  251. s32 status; /* negative values are failures */
  252. union win_slot_encoding wslot;
  253. u32 resultant_state; /* In Windows terms */
  254. u32 reserved;
  255. } __packed;
  256. struct pci_resources_assigned {
  257. struct pci_message message_type;
  258. union win_slot_encoding wslot;
  259. u8 memory_range[0x14][6]; /* not used here */
  260. u32 msi_descriptors;
  261. u32 reserved[4];
  262. } __packed;
  263. struct pci_create_interrupt {
  264. struct pci_message message_type;
  265. union win_slot_encoding wslot;
  266. struct hv_msi_desc int_desc;
  267. } __packed;
  268. struct pci_create_int_response {
  269. struct pci_response response;
  270. u32 reserved;
  271. struct tran_int_desc int_desc;
  272. } __packed;
  273. struct pci_delete_interrupt {
  274. struct pci_message message_type;
  275. union win_slot_encoding wslot;
  276. struct tran_int_desc int_desc;
  277. } __packed;
  278. struct pci_dev_incoming {
  279. struct pci_incoming_message incoming;
  280. union win_slot_encoding wslot;
  281. } __packed;
  282. struct pci_eject_response {
  283. struct pci_message message_type;
  284. union win_slot_encoding wslot;
  285. u32 status;
  286. } __packed;
  287. static int pci_ring_size = (4 * PAGE_SIZE);
  288. /*
  289. * Definitions or interrupt steering hypercall.
  290. */
  291. #define HV_PARTITION_ID_SELF ((u64)-1)
  292. #define HVCALL_RETARGET_INTERRUPT 0x7e
  293. struct retarget_msi_interrupt {
  294. u64 partition_id; /* use "self" */
  295. u64 device_id;
  296. u32 source; /* 1 for MSI(-X) */
  297. u32 reserved1;
  298. u32 address;
  299. u32 data;
  300. u64 reserved2;
  301. u32 vector;
  302. u32 flags;
  303. u64 vp_mask;
  304. } __packed;
  305. /*
  306. * Driver specific state.
  307. */
  308. enum hv_pcibus_state {
  309. hv_pcibus_init = 0,
  310. hv_pcibus_probed,
  311. hv_pcibus_installed,
  312. hv_pcibus_maximum
  313. };
  314. struct hv_pcibus_device {
  315. struct pci_sysdata sysdata;
  316. enum hv_pcibus_state state;
  317. atomic_t remove_lock;
  318. struct hv_device *hdev;
  319. resource_size_t low_mmio_space;
  320. resource_size_t high_mmio_space;
  321. struct resource *mem_config;
  322. struct resource *low_mmio_res;
  323. struct resource *high_mmio_res;
  324. struct completion *survey_event;
  325. struct completion remove_event;
  326. struct pci_bus *pci_bus;
  327. spinlock_t config_lock; /* Avoid two threads writing index page */
  328. spinlock_t device_list_lock; /* Protect lists below */
  329. void __iomem *cfg_addr;
  330. struct semaphore enum_sem;
  331. struct list_head resources_for_children;
  332. struct list_head children;
  333. struct list_head dr_list;
  334. struct msi_domain_info msi_info;
  335. struct msi_controller msi_chip;
  336. struct irq_domain *irq_domain;
  337. struct retarget_msi_interrupt retarget_msi_interrupt_params;
  338. spinlock_t retarget_msi_interrupt_lock;
  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. static 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. static 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. unsigned long flags;
  702. dest = irq_data_get_affinity_mask(data);
  703. pdev = msi_desc_to_pci_dev(msi_desc);
  704. pbus = pdev->bus;
  705. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  706. spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
  707. params = &hbus->retarget_msi_interrupt_params;
  708. memset(params, 0, sizeof(*params));
  709. params->partition_id = HV_PARTITION_ID_SELF;
  710. params->source = 1; /* MSI(-X) */
  711. params->address = msi_desc->msg.address_lo;
  712. params->data = msi_desc->msg.data;
  713. params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
  714. (hbus->hdev->dev_instance.b[4] << 16) |
  715. (hbus->hdev->dev_instance.b[7] << 8) |
  716. (hbus->hdev->dev_instance.b[6] & 0xf8) |
  717. PCI_FUNC(pdev->devfn);
  718. params->vector = cfg->vector;
  719. for_each_cpu_and(cpu, dest, cpu_online_mask)
  720. params->vp_mask |= (1ULL << vmbus_cpu_number_to_vp_number(cpu));
  721. hv_do_hypercall(HVCALL_RETARGET_INTERRUPT, params, NULL);
  722. spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
  723. pci_msi_unmask_irq(data);
  724. }
  725. struct compose_comp_ctxt {
  726. struct hv_pci_compl comp_pkt;
  727. struct tran_int_desc int_desc;
  728. };
  729. static void hv_pci_compose_compl(void *context, struct pci_response *resp,
  730. int resp_packet_size)
  731. {
  732. struct compose_comp_ctxt *comp_pkt = context;
  733. struct pci_create_int_response *int_resp =
  734. (struct pci_create_int_response *)resp;
  735. comp_pkt->comp_pkt.completion_status = resp->status;
  736. comp_pkt->int_desc = int_resp->int_desc;
  737. complete(&comp_pkt->comp_pkt.host_event);
  738. }
  739. /**
  740. * hv_compose_msi_msg() - Supplies a valid MSI address/data
  741. * @data: Everything about this MSI
  742. * @msg: Buffer that is filled in by this function
  743. *
  744. * This function unpacks the IRQ looking for target CPU set, IDT
  745. * vector and mode and sends a message to the parent partition
  746. * asking for a mapping for that tuple in this partition. The
  747. * response supplies a data value and address to which that data
  748. * should be written to trigger that interrupt.
  749. */
  750. static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  751. {
  752. struct irq_cfg *cfg = irqd_cfg(data);
  753. struct hv_pcibus_device *hbus;
  754. struct hv_pci_dev *hpdev;
  755. struct pci_bus *pbus;
  756. struct pci_dev *pdev;
  757. struct pci_create_interrupt *int_pkt;
  758. struct compose_comp_ctxt comp;
  759. struct tran_int_desc *int_desc;
  760. struct cpumask *affinity;
  761. struct {
  762. struct pci_packet pkt;
  763. u8 buffer[sizeof(struct pci_create_interrupt)];
  764. } ctxt;
  765. int cpu;
  766. int ret;
  767. pdev = msi_desc_to_pci_dev(irq_data_get_msi_desc(data));
  768. pbus = pdev->bus;
  769. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  770. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  771. if (!hpdev)
  772. goto return_null_message;
  773. /* Free any previous message that might have already been composed. */
  774. if (data->chip_data) {
  775. int_desc = data->chip_data;
  776. data->chip_data = NULL;
  777. hv_int_desc_free(hpdev, int_desc);
  778. }
  779. int_desc = kzalloc(sizeof(*int_desc), GFP_KERNEL);
  780. if (!int_desc)
  781. goto drop_reference;
  782. memset(&ctxt, 0, sizeof(ctxt));
  783. init_completion(&comp.comp_pkt.host_event);
  784. ctxt.pkt.completion_func = hv_pci_compose_compl;
  785. ctxt.pkt.compl_ctxt = &comp;
  786. int_pkt = (struct pci_create_interrupt *)&ctxt.pkt.message;
  787. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
  788. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  789. int_pkt->int_desc.vector = cfg->vector;
  790. int_pkt->int_desc.vector_count = 1;
  791. int_pkt->int_desc.delivery_mode =
  792. (apic->irq_delivery_mode == dest_LowestPrio) ? 1 : 0;
  793. /*
  794. * This bit doesn't have to work on machines with more than 64
  795. * processors because Hyper-V only supports 64 in a guest.
  796. */
  797. affinity = irq_data_get_affinity_mask(data);
  798. for_each_cpu_and(cpu, affinity, cpu_online_mask) {
  799. int_pkt->int_desc.cpu_mask |=
  800. (1ULL << vmbus_cpu_number_to_vp_number(cpu));
  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_scan_child_bus(hbus->pci_bus);
  1077. pci_bus_assign_resources(hbus->pci_bus);
  1078. pci_bus_add_devices(hbus->pci_bus);
  1079. hbus->state = hv_pcibus_installed;
  1080. return 0;
  1081. }
  1082. struct q_res_req_compl {
  1083. struct completion host_event;
  1084. struct hv_pci_dev *hpdev;
  1085. };
  1086. /**
  1087. * q_resource_requirements() - Query Resource Requirements
  1088. * @context: The completion context.
  1089. * @resp: The response that came from the host.
  1090. * @resp_packet_size: The size in bytes of resp.
  1091. *
  1092. * This function is invoked on completion of a Query Resource
  1093. * Requirements packet.
  1094. */
  1095. static void q_resource_requirements(void *context, struct pci_response *resp,
  1096. int resp_packet_size)
  1097. {
  1098. struct q_res_req_compl *completion = context;
  1099. struct pci_q_res_req_response *q_res_req =
  1100. (struct pci_q_res_req_response *)resp;
  1101. int i;
  1102. if (resp->status < 0) {
  1103. dev_err(&completion->hpdev->hbus->hdev->device,
  1104. "query resource requirements failed: %x\n",
  1105. resp->status);
  1106. } else {
  1107. for (i = 0; i < 6; i++) {
  1108. completion->hpdev->probed_bar[i] =
  1109. q_res_req->probed_bar[i];
  1110. }
  1111. }
  1112. complete(&completion->host_event);
  1113. }
  1114. static void get_pcichild(struct hv_pci_dev *hpdev,
  1115. enum hv_pcidev_ref_reason reason)
  1116. {
  1117. atomic_inc(&hpdev->refs);
  1118. }
  1119. static void put_pcichild(struct hv_pci_dev *hpdev,
  1120. enum hv_pcidev_ref_reason reason)
  1121. {
  1122. if (atomic_dec_and_test(&hpdev->refs))
  1123. kfree(hpdev);
  1124. }
  1125. /**
  1126. * new_pcichild_device() - Create a new child device
  1127. * @hbus: The internal struct tracking this root PCI bus.
  1128. * @desc: The information supplied so far from the host
  1129. * about the device.
  1130. *
  1131. * This function creates the tracking structure for a new child
  1132. * device and kicks off the process of figuring out what it is.
  1133. *
  1134. * Return: Pointer to the new tracking struct
  1135. */
  1136. static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
  1137. struct pci_function_description *desc)
  1138. {
  1139. struct hv_pci_dev *hpdev;
  1140. struct pci_child_message *res_req;
  1141. struct q_res_req_compl comp_pkt;
  1142. struct {
  1143. struct pci_packet init_packet;
  1144. u8 buffer[sizeof(struct pci_child_message)];
  1145. } pkt;
  1146. unsigned long flags;
  1147. int ret;
  1148. hpdev = kzalloc(sizeof(*hpdev), GFP_ATOMIC);
  1149. if (!hpdev)
  1150. return NULL;
  1151. hpdev->hbus = hbus;
  1152. memset(&pkt, 0, sizeof(pkt));
  1153. init_completion(&comp_pkt.host_event);
  1154. comp_pkt.hpdev = hpdev;
  1155. pkt.init_packet.compl_ctxt = &comp_pkt;
  1156. pkt.init_packet.completion_func = q_resource_requirements;
  1157. res_req = (struct pci_child_message *)&pkt.init_packet.message;
  1158. res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
  1159. res_req->wslot.slot = desc->win_slot.slot;
  1160. ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
  1161. sizeof(struct pci_child_message),
  1162. (unsigned long)&pkt.init_packet,
  1163. VM_PKT_DATA_INBAND,
  1164. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1165. if (ret)
  1166. goto error;
  1167. wait_for_completion(&comp_pkt.host_event);
  1168. hpdev->desc = *desc;
  1169. get_pcichild(hpdev, hv_pcidev_ref_initial);
  1170. get_pcichild(hpdev, hv_pcidev_ref_childlist);
  1171. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1172. /*
  1173. * When a device is being added to the bus, we set the PCI domain
  1174. * number to be the device serial number, which is non-zero and
  1175. * unique on the same VM. The serial numbers start with 1, and
  1176. * increase by 1 for each device. So device names including this
  1177. * can have shorter names than based on the bus instance UUID.
  1178. * Only the first device serial number is used for domain, so the
  1179. * domain number will not change after the first device is added.
  1180. */
  1181. if (list_empty(&hbus->children))
  1182. hbus->sysdata.domain = desc->ser;
  1183. list_add_tail(&hpdev->list_entry, &hbus->children);
  1184. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1185. return hpdev;
  1186. error:
  1187. kfree(hpdev);
  1188. return NULL;
  1189. }
  1190. /**
  1191. * get_pcichild_wslot() - Find device from slot
  1192. * @hbus: Root PCI bus, as understood by this driver
  1193. * @wslot: Location on the bus
  1194. *
  1195. * This function looks up a PCI device and returns the internal
  1196. * representation of it. It acquires a reference on it, so that
  1197. * the device won't be deleted while somebody is using it. The
  1198. * caller is responsible for calling put_pcichild() to release
  1199. * this reference.
  1200. *
  1201. * Return: Internal representation of a PCI device
  1202. */
  1203. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  1204. u32 wslot)
  1205. {
  1206. unsigned long flags;
  1207. struct hv_pci_dev *iter, *hpdev = NULL;
  1208. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1209. list_for_each_entry(iter, &hbus->children, list_entry) {
  1210. if (iter->desc.win_slot.slot == wslot) {
  1211. hpdev = iter;
  1212. get_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1213. break;
  1214. }
  1215. }
  1216. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1217. return hpdev;
  1218. }
  1219. /**
  1220. * pci_devices_present_work() - Handle new list of child devices
  1221. * @work: Work struct embedded in struct hv_dr_work
  1222. *
  1223. * "Bus Relations" is the Windows term for "children of this
  1224. * bus." The terminology is preserved here for people trying to
  1225. * debug the interaction between Hyper-V and Linux. This
  1226. * function is called when the parent partition reports a list
  1227. * of functions that should be observed under this PCI Express
  1228. * port (bus).
  1229. *
  1230. * This function updates the list, and must tolerate being
  1231. * called multiple times with the same information. The typical
  1232. * number of child devices is one, with very atypical cases
  1233. * involving three or four, so the algorithms used here can be
  1234. * simple and inefficient.
  1235. *
  1236. * It must also treat the omission of a previously observed device as
  1237. * notification that the device no longer exists.
  1238. *
  1239. * Note that this function is a work item, and it may not be
  1240. * invoked in the order that it was queued. Back to back
  1241. * updates of the list of present devices may involve queuing
  1242. * multiple work items, and this one may run before ones that
  1243. * were sent later. As such, this function only does something
  1244. * if is the last one in the queue.
  1245. */
  1246. static void pci_devices_present_work(struct work_struct *work)
  1247. {
  1248. u32 child_no;
  1249. bool found;
  1250. struct list_head *iter;
  1251. struct pci_function_description *new_desc;
  1252. struct hv_pci_dev *hpdev;
  1253. struct hv_pcibus_device *hbus;
  1254. struct list_head removed;
  1255. struct hv_dr_work *dr_wrk;
  1256. struct hv_dr_state *dr = NULL;
  1257. unsigned long flags;
  1258. dr_wrk = container_of(work, struct hv_dr_work, wrk);
  1259. hbus = dr_wrk->bus;
  1260. kfree(dr_wrk);
  1261. INIT_LIST_HEAD(&removed);
  1262. if (down_interruptible(&hbus->enum_sem)) {
  1263. put_hvpcibus(hbus);
  1264. return;
  1265. }
  1266. /* Pull this off the queue and process it if it was the last one. */
  1267. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1268. while (!list_empty(&hbus->dr_list)) {
  1269. dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
  1270. list_entry);
  1271. list_del(&dr->list_entry);
  1272. /* Throw this away if the list still has stuff in it. */
  1273. if (!list_empty(&hbus->dr_list)) {
  1274. kfree(dr);
  1275. continue;
  1276. }
  1277. }
  1278. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1279. if (!dr) {
  1280. up(&hbus->enum_sem);
  1281. put_hvpcibus(hbus);
  1282. return;
  1283. }
  1284. /* First, mark all existing children as reported missing. */
  1285. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1286. list_for_each(iter, &hbus->children) {
  1287. hpdev = container_of(iter, struct hv_pci_dev,
  1288. list_entry);
  1289. hpdev->reported_missing = true;
  1290. }
  1291. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1292. /* Next, add back any reported devices. */
  1293. for (child_no = 0; child_no < dr->device_count; child_no++) {
  1294. found = false;
  1295. new_desc = &dr->func[child_no];
  1296. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1297. list_for_each(iter, &hbus->children) {
  1298. hpdev = container_of(iter, struct hv_pci_dev,
  1299. list_entry);
  1300. if ((hpdev->desc.win_slot.slot ==
  1301. new_desc->win_slot.slot) &&
  1302. (hpdev->desc.v_id == new_desc->v_id) &&
  1303. (hpdev->desc.d_id == new_desc->d_id) &&
  1304. (hpdev->desc.ser == new_desc->ser)) {
  1305. hpdev->reported_missing = false;
  1306. found = true;
  1307. }
  1308. }
  1309. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1310. if (!found) {
  1311. hpdev = new_pcichild_device(hbus, new_desc);
  1312. if (!hpdev)
  1313. dev_err(&hbus->hdev->device,
  1314. "couldn't record a child device.\n");
  1315. }
  1316. }
  1317. /* Move missing children to a list on the stack. */
  1318. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1319. do {
  1320. found = false;
  1321. list_for_each(iter, &hbus->children) {
  1322. hpdev = container_of(iter, struct hv_pci_dev,
  1323. list_entry);
  1324. if (hpdev->reported_missing) {
  1325. found = true;
  1326. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1327. list_move_tail(&hpdev->list_entry, &removed);
  1328. break;
  1329. }
  1330. }
  1331. } while (found);
  1332. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1333. /* Delete everything that should no longer exist. */
  1334. while (!list_empty(&removed)) {
  1335. hpdev = list_first_entry(&removed, struct hv_pci_dev,
  1336. list_entry);
  1337. list_del(&hpdev->list_entry);
  1338. put_pcichild(hpdev, hv_pcidev_ref_initial);
  1339. }
  1340. /* Tell the core to rescan bus because there may have been changes. */
  1341. if (hbus->state == hv_pcibus_installed) {
  1342. pci_lock_rescan_remove();
  1343. pci_scan_child_bus(hbus->pci_bus);
  1344. pci_unlock_rescan_remove();
  1345. } else {
  1346. survey_child_resources(hbus);
  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_stop_and_remove_bus_device(pdev);
  1426. pci_dev_put(pdev);
  1427. }
  1428. spin_lock_irqsave(&hpdev->hbus->device_list_lock, flags);
  1429. list_del(&hpdev->list_entry);
  1430. spin_unlock_irqrestore(&hpdev->hbus->device_list_lock, flags);
  1431. memset(&ctxt, 0, sizeof(ctxt));
  1432. ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
  1433. ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
  1434. ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  1435. vmbus_sendpacket(hpdev->hbus->hdev->channel, ejct_pkt,
  1436. sizeof(*ejct_pkt), (unsigned long)&ctxt.pkt,
  1437. VM_PKT_DATA_INBAND, 0);
  1438. put_pcichild(hpdev, hv_pcidev_ref_childlist);
  1439. put_pcichild(hpdev, hv_pcidev_ref_pnp);
  1440. put_hvpcibus(hpdev->hbus);
  1441. }
  1442. /**
  1443. * hv_pci_eject_device() - Handles device ejection
  1444. * @hpdev: Internal device tracking struct
  1445. *
  1446. * This function is invoked when an ejection packet arrives. It
  1447. * just schedules work so that we don't re-enter the packet
  1448. * delivery code handling the ejection.
  1449. */
  1450. static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
  1451. {
  1452. hpdev->state = hv_pcichild_ejecting;
  1453. get_pcichild(hpdev, hv_pcidev_ref_pnp);
  1454. INIT_WORK(&hpdev->wrk, hv_eject_device_work);
  1455. get_hvpcibus(hpdev->hbus);
  1456. schedule_work(&hpdev->wrk);
  1457. }
  1458. /**
  1459. * hv_pci_onchannelcallback() - Handles incoming packets
  1460. * @context: Internal bus tracking struct
  1461. *
  1462. * This function is invoked whenever the host sends a packet to
  1463. * this channel (which is private to this root PCI bus).
  1464. */
  1465. static void hv_pci_onchannelcallback(void *context)
  1466. {
  1467. const int packet_size = 0x100;
  1468. int ret;
  1469. struct hv_pcibus_device *hbus = context;
  1470. u32 bytes_recvd;
  1471. u64 req_id;
  1472. struct vmpacket_descriptor *desc;
  1473. unsigned char *buffer;
  1474. int bufferlen = packet_size;
  1475. struct pci_packet *comp_packet;
  1476. struct pci_response *response;
  1477. struct pci_incoming_message *new_message;
  1478. struct pci_bus_relations *bus_rel;
  1479. struct pci_dev_incoming *dev_message;
  1480. struct hv_pci_dev *hpdev;
  1481. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  1482. if (!buffer)
  1483. return;
  1484. while (1) {
  1485. ret = vmbus_recvpacket_raw(hbus->hdev->channel, buffer,
  1486. bufferlen, &bytes_recvd, &req_id);
  1487. if (ret == -ENOBUFS) {
  1488. kfree(buffer);
  1489. /* Handle large packet */
  1490. bufferlen = bytes_recvd;
  1491. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  1492. if (!buffer)
  1493. return;
  1494. continue;
  1495. }
  1496. /* Zero length indicates there are no more packets. */
  1497. if (ret || !bytes_recvd)
  1498. break;
  1499. /*
  1500. * All incoming packets must be at least as large as a
  1501. * response.
  1502. */
  1503. if (bytes_recvd <= sizeof(struct pci_response))
  1504. continue;
  1505. desc = (struct vmpacket_descriptor *)buffer;
  1506. switch (desc->type) {
  1507. case VM_PKT_COMP:
  1508. /*
  1509. * The host is trusted, and thus it's safe to interpret
  1510. * this transaction ID as a pointer.
  1511. */
  1512. comp_packet = (struct pci_packet *)req_id;
  1513. response = (struct pci_response *)buffer;
  1514. comp_packet->completion_func(comp_packet->compl_ctxt,
  1515. response,
  1516. bytes_recvd);
  1517. break;
  1518. case VM_PKT_DATA_INBAND:
  1519. new_message = (struct pci_incoming_message *)buffer;
  1520. switch (new_message->message_type.type) {
  1521. case PCI_BUS_RELATIONS:
  1522. bus_rel = (struct pci_bus_relations *)buffer;
  1523. if (bytes_recvd <
  1524. offsetof(struct pci_bus_relations, func) +
  1525. (sizeof(struct pci_function_description) *
  1526. (bus_rel->device_count))) {
  1527. dev_err(&hbus->hdev->device,
  1528. "bus relations too small\n");
  1529. break;
  1530. }
  1531. hv_pci_devices_present(hbus, bus_rel);
  1532. break;
  1533. case PCI_EJECT:
  1534. dev_message = (struct pci_dev_incoming *)buffer;
  1535. hpdev = get_pcichild_wslot(hbus,
  1536. dev_message->wslot.slot);
  1537. if (hpdev) {
  1538. hv_pci_eject_device(hpdev);
  1539. put_pcichild(hpdev,
  1540. hv_pcidev_ref_by_slot);
  1541. }
  1542. break;
  1543. default:
  1544. dev_warn(&hbus->hdev->device,
  1545. "Unimplemented protocol message %x\n",
  1546. new_message->message_type.type);
  1547. break;
  1548. }
  1549. break;
  1550. default:
  1551. dev_err(&hbus->hdev->device,
  1552. "unhandled packet type %d, tid %llx len %d\n",
  1553. desc->type, req_id, bytes_recvd);
  1554. break;
  1555. }
  1556. }
  1557. kfree(buffer);
  1558. }
  1559. /**
  1560. * hv_pci_protocol_negotiation() - Set up protocol
  1561. * @hdev: VMBus's tracking struct for this root PCI bus
  1562. *
  1563. * This driver is intended to support running on Windows 10
  1564. * (server) and later versions. It will not run on earlier
  1565. * versions, as they assume that many of the operations which
  1566. * Linux needs accomplished with a spinlock held were done via
  1567. * asynchronous messaging via VMBus. Windows 10 increases the
  1568. * surface area of PCI emulation so that these actions can take
  1569. * place by suspending a virtual processor for their duration.
  1570. *
  1571. * This function negotiates the channel protocol version,
  1572. * failing if the host doesn't support the necessary protocol
  1573. * level.
  1574. */
  1575. static int hv_pci_protocol_negotiation(struct hv_device *hdev)
  1576. {
  1577. struct pci_version_request *version_req;
  1578. struct hv_pci_compl comp_pkt;
  1579. struct pci_packet *pkt;
  1580. int ret;
  1581. /*
  1582. * Initiate the handshake with the host and negotiate
  1583. * a version that the host can support. We start with the
  1584. * highest version number and go down if the host cannot
  1585. * support it.
  1586. */
  1587. pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
  1588. if (!pkt)
  1589. return -ENOMEM;
  1590. init_completion(&comp_pkt.host_event);
  1591. pkt->completion_func = hv_pci_generic_compl;
  1592. pkt->compl_ctxt = &comp_pkt;
  1593. version_req = (struct pci_version_request *)&pkt->message;
  1594. version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
  1595. version_req->protocol_version = PCI_PROTOCOL_VERSION_CURRENT;
  1596. ret = vmbus_sendpacket(hdev->channel, version_req,
  1597. sizeof(struct pci_version_request),
  1598. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  1599. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1600. if (ret)
  1601. goto exit;
  1602. wait_for_completion(&comp_pkt.host_event);
  1603. if (comp_pkt.completion_status < 0) {
  1604. dev_err(&hdev->device,
  1605. "PCI Pass-through VSP failed version request %x\n",
  1606. comp_pkt.completion_status);
  1607. ret = -EPROTO;
  1608. goto exit;
  1609. }
  1610. ret = 0;
  1611. exit:
  1612. kfree(pkt);
  1613. return ret;
  1614. }
  1615. /**
  1616. * hv_pci_free_bridge_windows() - Release memory regions for the
  1617. * bus
  1618. * @hbus: Root PCI bus, as understood by this driver
  1619. */
  1620. static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
  1621. {
  1622. /*
  1623. * Set the resources back to the way they looked when they
  1624. * were allocated by setting IORESOURCE_BUSY again.
  1625. */
  1626. if (hbus->low_mmio_space && hbus->low_mmio_res) {
  1627. hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
  1628. vmbus_free_mmio(hbus->low_mmio_res->start,
  1629. resource_size(hbus->low_mmio_res));
  1630. }
  1631. if (hbus->high_mmio_space && hbus->high_mmio_res) {
  1632. hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
  1633. vmbus_free_mmio(hbus->high_mmio_res->start,
  1634. resource_size(hbus->high_mmio_res));
  1635. }
  1636. }
  1637. /**
  1638. * hv_pci_allocate_bridge_windows() - Allocate memory regions
  1639. * for the bus
  1640. * @hbus: Root PCI bus, as understood by this driver
  1641. *
  1642. * This function calls vmbus_allocate_mmio(), which is itself a
  1643. * bit of a compromise. Ideally, we might change the pnp layer
  1644. * in the kernel such that it comprehends either PCI devices
  1645. * which are "grandchildren of ACPI," with some intermediate bus
  1646. * node (in this case, VMBus) or change it such that it
  1647. * understands VMBus. The pnp layer, however, has been declared
  1648. * deprecated, and not subject to change.
  1649. *
  1650. * The workaround, implemented here, is to ask VMBus to allocate
  1651. * MMIO space for this bus. VMBus itself knows which ranges are
  1652. * appropriate by looking at its own ACPI objects. Then, after
  1653. * these ranges are claimed, they're modified to look like they
  1654. * would have looked if the ACPI and pnp code had allocated
  1655. * bridge windows. These descriptors have to exist in this form
  1656. * in order to satisfy the code which will get invoked when the
  1657. * endpoint PCI function driver calls request_mem_region() or
  1658. * request_mem_region_exclusive().
  1659. *
  1660. * Return: 0 on success, -errno on failure
  1661. */
  1662. static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
  1663. {
  1664. resource_size_t align;
  1665. int ret;
  1666. if (hbus->low_mmio_space) {
  1667. align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  1668. ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
  1669. (u64)(u32)0xffffffff,
  1670. hbus->low_mmio_space,
  1671. align, false);
  1672. if (ret) {
  1673. dev_err(&hbus->hdev->device,
  1674. "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
  1675. hbus->low_mmio_space);
  1676. return ret;
  1677. }
  1678. /* Modify this resource to become a bridge window. */
  1679. hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
  1680. hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
  1681. pci_add_resource(&hbus->resources_for_children,
  1682. hbus->low_mmio_res);
  1683. }
  1684. if (hbus->high_mmio_space) {
  1685. align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
  1686. ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
  1687. 0x100000000, -1,
  1688. hbus->high_mmio_space, align,
  1689. false);
  1690. if (ret) {
  1691. dev_err(&hbus->hdev->device,
  1692. "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
  1693. hbus->high_mmio_space);
  1694. goto release_low_mmio;
  1695. }
  1696. /* Modify this resource to become a bridge window. */
  1697. hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
  1698. hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
  1699. pci_add_resource(&hbus->resources_for_children,
  1700. hbus->high_mmio_res);
  1701. }
  1702. return 0;
  1703. release_low_mmio:
  1704. if (hbus->low_mmio_res) {
  1705. vmbus_free_mmio(hbus->low_mmio_res->start,
  1706. resource_size(hbus->low_mmio_res));
  1707. }
  1708. return ret;
  1709. }
  1710. /**
  1711. * hv_allocate_config_window() - Find MMIO space for PCI Config
  1712. * @hbus: Root PCI bus, as understood by this driver
  1713. *
  1714. * This function claims memory-mapped I/O space for accessing
  1715. * configuration space for the functions on this bus.
  1716. *
  1717. * Return: 0 on success, -errno on failure
  1718. */
  1719. static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
  1720. {
  1721. int ret;
  1722. /*
  1723. * Set up a region of MMIO space to use for accessing configuration
  1724. * space.
  1725. */
  1726. ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
  1727. PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
  1728. if (ret)
  1729. return ret;
  1730. /*
  1731. * vmbus_allocate_mmio() gets used for allocating both device endpoint
  1732. * resource claims (those which cannot be overlapped) and the ranges
  1733. * which are valid for the children of this bus, which are intended
  1734. * to be overlapped by those children. Set the flag on this claim
  1735. * meaning that this region can't be overlapped.
  1736. */
  1737. hbus->mem_config->flags |= IORESOURCE_BUSY;
  1738. return 0;
  1739. }
  1740. static void hv_free_config_window(struct hv_pcibus_device *hbus)
  1741. {
  1742. vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
  1743. }
  1744. /**
  1745. * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
  1746. * @hdev: VMBus's tracking struct for this root PCI bus
  1747. *
  1748. * Return: 0 on success, -errno on failure
  1749. */
  1750. static int hv_pci_enter_d0(struct hv_device *hdev)
  1751. {
  1752. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1753. struct pci_bus_d0_entry *d0_entry;
  1754. struct hv_pci_compl comp_pkt;
  1755. struct pci_packet *pkt;
  1756. int ret;
  1757. /*
  1758. * Tell the host that the bus is ready to use, and moved into the
  1759. * powered-on state. This includes telling the host which region
  1760. * of memory-mapped I/O space has been chosen for configuration space
  1761. * access.
  1762. */
  1763. pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
  1764. if (!pkt)
  1765. return -ENOMEM;
  1766. init_completion(&comp_pkt.host_event);
  1767. pkt->completion_func = hv_pci_generic_compl;
  1768. pkt->compl_ctxt = &comp_pkt;
  1769. d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
  1770. d0_entry->message_type.type = PCI_BUS_D0ENTRY;
  1771. d0_entry->mmio_base = hbus->mem_config->start;
  1772. ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
  1773. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  1774. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1775. if (ret)
  1776. goto exit;
  1777. wait_for_completion(&comp_pkt.host_event);
  1778. if (comp_pkt.completion_status < 0) {
  1779. dev_err(&hdev->device,
  1780. "PCI Pass-through VSP failed D0 Entry with status %x\n",
  1781. comp_pkt.completion_status);
  1782. ret = -EPROTO;
  1783. goto exit;
  1784. }
  1785. ret = 0;
  1786. exit:
  1787. kfree(pkt);
  1788. return ret;
  1789. }
  1790. /**
  1791. * hv_pci_query_relations() - Ask host to send list of child
  1792. * devices
  1793. * @hdev: VMBus's tracking struct for this root PCI bus
  1794. *
  1795. * Return: 0 on success, -errno on failure
  1796. */
  1797. static int hv_pci_query_relations(struct hv_device *hdev)
  1798. {
  1799. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1800. struct pci_message message;
  1801. struct completion comp;
  1802. int ret;
  1803. /* Ask the host to send along the list of child devices */
  1804. init_completion(&comp);
  1805. if (cmpxchg(&hbus->survey_event, NULL, &comp))
  1806. return -ENOTEMPTY;
  1807. memset(&message, 0, sizeof(message));
  1808. message.type = PCI_QUERY_BUS_RELATIONS;
  1809. ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
  1810. 0, VM_PKT_DATA_INBAND, 0);
  1811. if (ret)
  1812. return ret;
  1813. wait_for_completion(&comp);
  1814. return 0;
  1815. }
  1816. /**
  1817. * hv_send_resources_allocated() - Report local resource choices
  1818. * @hdev: VMBus's tracking struct for this root PCI bus
  1819. *
  1820. * The host OS is expecting to be sent a request as a message
  1821. * which contains all the resources that the device will use.
  1822. * The response contains those same resources, "translated"
  1823. * which is to say, the values which should be used by the
  1824. * hardware, when it delivers an interrupt. (MMIO resources are
  1825. * used in local terms.) This is nice for Windows, and lines up
  1826. * with the FDO/PDO split, which doesn't exist in Linux. Linux
  1827. * is deeply expecting to scan an emulated PCI configuration
  1828. * space. So this message is sent here only to drive the state
  1829. * machine on the host forward.
  1830. *
  1831. * Return: 0 on success, -errno on failure
  1832. */
  1833. static int hv_send_resources_allocated(struct hv_device *hdev)
  1834. {
  1835. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1836. struct pci_resources_assigned *res_assigned;
  1837. struct hv_pci_compl comp_pkt;
  1838. struct hv_pci_dev *hpdev;
  1839. struct pci_packet *pkt;
  1840. u32 wslot;
  1841. int ret;
  1842. pkt = kmalloc(sizeof(*pkt) + sizeof(*res_assigned), GFP_KERNEL);
  1843. if (!pkt)
  1844. return -ENOMEM;
  1845. ret = 0;
  1846. for (wslot = 0; wslot < 256; wslot++) {
  1847. hpdev = get_pcichild_wslot(hbus, wslot);
  1848. if (!hpdev)
  1849. continue;
  1850. memset(pkt, 0, sizeof(*pkt) + sizeof(*res_assigned));
  1851. init_completion(&comp_pkt.host_event);
  1852. pkt->completion_func = hv_pci_generic_compl;
  1853. pkt->compl_ctxt = &comp_pkt;
  1854. res_assigned = (struct pci_resources_assigned *)&pkt->message;
  1855. res_assigned->message_type.type = PCI_RESOURCES_ASSIGNED;
  1856. res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
  1857. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1858. ret = vmbus_sendpacket(
  1859. hdev->channel, &pkt->message,
  1860. sizeof(*res_assigned),
  1861. (unsigned long)pkt,
  1862. VM_PKT_DATA_INBAND,
  1863. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1864. if (ret)
  1865. break;
  1866. wait_for_completion(&comp_pkt.host_event);
  1867. if (comp_pkt.completion_status < 0) {
  1868. ret = -EPROTO;
  1869. dev_err(&hdev->device,
  1870. "resource allocated returned 0x%x",
  1871. comp_pkt.completion_status);
  1872. break;
  1873. }
  1874. }
  1875. kfree(pkt);
  1876. return ret;
  1877. }
  1878. /**
  1879. * hv_send_resources_released() - Report local resources
  1880. * released
  1881. * @hdev: VMBus's tracking struct for this root PCI bus
  1882. *
  1883. * Return: 0 on success, -errno on failure
  1884. */
  1885. static int hv_send_resources_released(struct hv_device *hdev)
  1886. {
  1887. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  1888. struct pci_child_message pkt;
  1889. struct hv_pci_dev *hpdev;
  1890. u32 wslot;
  1891. int ret;
  1892. for (wslot = 0; wslot < 256; wslot++) {
  1893. hpdev = get_pcichild_wslot(hbus, wslot);
  1894. if (!hpdev)
  1895. continue;
  1896. memset(&pkt, 0, sizeof(pkt));
  1897. pkt.message_type.type = PCI_RESOURCES_RELEASED;
  1898. pkt.wslot.slot = hpdev->desc.win_slot.slot;
  1899. put_pcichild(hpdev, hv_pcidev_ref_by_slot);
  1900. ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
  1901. VM_PKT_DATA_INBAND, 0);
  1902. if (ret)
  1903. return ret;
  1904. }
  1905. return 0;
  1906. }
  1907. static void get_hvpcibus(struct hv_pcibus_device *hbus)
  1908. {
  1909. atomic_inc(&hbus->remove_lock);
  1910. }
  1911. static void put_hvpcibus(struct hv_pcibus_device *hbus)
  1912. {
  1913. if (atomic_dec_and_test(&hbus->remove_lock))
  1914. complete(&hbus->remove_event);
  1915. }
  1916. /**
  1917. * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
  1918. * @hdev: VMBus's tracking struct for this root PCI bus
  1919. * @dev_id: Identifies the device itself
  1920. *
  1921. * Return: 0 on success, -errno on failure
  1922. */
  1923. static int hv_pci_probe(struct hv_device *hdev,
  1924. const struct hv_vmbus_device_id *dev_id)
  1925. {
  1926. struct hv_pcibus_device *hbus;
  1927. int ret;
  1928. hbus = kzalloc(sizeof(*hbus), GFP_KERNEL);
  1929. if (!hbus)
  1930. return -ENOMEM;
  1931. /*
  1932. * The PCI bus "domain" is what is called "segment" in ACPI and
  1933. * other specs. Pull it from the instance ID, to get something
  1934. * unique. Bytes 8 and 9 are what is used in Windows guests, so
  1935. * do the same thing for consistency. Note that, since this code
  1936. * only runs in a Hyper-V VM, Hyper-V can (and does) guarantee
  1937. * that (1) the only domain in use for something that looks like
  1938. * a physical PCI bus (which is actually emulated by the
  1939. * hypervisor) is domain 0 and (2) there will be no overlap
  1940. * between domains derived from these instance IDs in the same
  1941. * VM.
  1942. */
  1943. hbus->sysdata.domain = hdev->dev_instance.b[9] |
  1944. hdev->dev_instance.b[8] << 8;
  1945. hbus->hdev = hdev;
  1946. atomic_inc(&hbus->remove_lock);
  1947. INIT_LIST_HEAD(&hbus->children);
  1948. INIT_LIST_HEAD(&hbus->dr_list);
  1949. INIT_LIST_HEAD(&hbus->resources_for_children);
  1950. spin_lock_init(&hbus->config_lock);
  1951. spin_lock_init(&hbus->device_list_lock);
  1952. spin_lock_init(&hbus->retarget_msi_interrupt_lock);
  1953. sema_init(&hbus->enum_sem, 1);
  1954. init_completion(&hbus->remove_event);
  1955. ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
  1956. hv_pci_onchannelcallback, hbus);
  1957. if (ret)
  1958. goto free_bus;
  1959. hv_set_drvdata(hdev, hbus);
  1960. ret = hv_pci_protocol_negotiation(hdev);
  1961. if (ret)
  1962. goto close;
  1963. ret = hv_allocate_config_window(hbus);
  1964. if (ret)
  1965. goto close;
  1966. hbus->cfg_addr = ioremap(hbus->mem_config->start,
  1967. PCI_CONFIG_MMIO_LENGTH);
  1968. if (!hbus->cfg_addr) {
  1969. dev_err(&hdev->device,
  1970. "Unable to map a virtual address for config space\n");
  1971. ret = -ENOMEM;
  1972. goto free_config;
  1973. }
  1974. hbus->sysdata.fwnode = irq_domain_alloc_fwnode(hbus);
  1975. if (!hbus->sysdata.fwnode) {
  1976. ret = -ENOMEM;
  1977. goto unmap;
  1978. }
  1979. ret = hv_pcie_init_irq_domain(hbus);
  1980. if (ret)
  1981. goto free_fwnode;
  1982. ret = hv_pci_query_relations(hdev);
  1983. if (ret)
  1984. goto free_irq_domain;
  1985. ret = hv_pci_enter_d0(hdev);
  1986. if (ret)
  1987. goto free_irq_domain;
  1988. ret = hv_pci_allocate_bridge_windows(hbus);
  1989. if (ret)
  1990. goto free_irq_domain;
  1991. ret = hv_send_resources_allocated(hdev);
  1992. if (ret)
  1993. goto free_windows;
  1994. prepopulate_bars(hbus);
  1995. hbus->state = hv_pcibus_probed;
  1996. ret = create_root_hv_pci_bus(hbus);
  1997. if (ret)
  1998. goto free_windows;
  1999. return 0;
  2000. free_windows:
  2001. hv_pci_free_bridge_windows(hbus);
  2002. free_irq_domain:
  2003. irq_domain_remove(hbus->irq_domain);
  2004. free_fwnode:
  2005. irq_domain_free_fwnode(hbus->sysdata.fwnode);
  2006. unmap:
  2007. iounmap(hbus->cfg_addr);
  2008. free_config:
  2009. hv_free_config_window(hbus);
  2010. close:
  2011. vmbus_close(hdev->channel);
  2012. free_bus:
  2013. kfree(hbus);
  2014. return ret;
  2015. }
  2016. static void hv_pci_bus_exit(struct hv_device *hdev)
  2017. {
  2018. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2019. struct {
  2020. struct pci_packet teardown_packet;
  2021. u8 buffer[sizeof(struct pci_message)];
  2022. } pkt;
  2023. struct pci_bus_relations relations;
  2024. struct hv_pci_compl comp_pkt;
  2025. int ret;
  2026. /*
  2027. * After the host sends the RESCIND_CHANNEL message, it doesn't
  2028. * access the per-channel ringbuffer any longer.
  2029. */
  2030. if (hdev->channel->rescind)
  2031. return;
  2032. /* Delete any children which might still exist. */
  2033. memset(&relations, 0, sizeof(relations));
  2034. hv_pci_devices_present(hbus, &relations);
  2035. ret = hv_send_resources_released(hdev);
  2036. if (ret)
  2037. dev_err(&hdev->device,
  2038. "Couldn't send resources released packet(s)\n");
  2039. memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
  2040. init_completion(&comp_pkt.host_event);
  2041. pkt.teardown_packet.completion_func = hv_pci_generic_compl;
  2042. pkt.teardown_packet.compl_ctxt = &comp_pkt;
  2043. pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
  2044. ret = vmbus_sendpacket(hdev->channel, &pkt.teardown_packet.message,
  2045. sizeof(struct pci_message),
  2046. (unsigned long)&pkt.teardown_packet,
  2047. VM_PKT_DATA_INBAND,
  2048. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2049. if (!ret)
  2050. wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ);
  2051. }
  2052. /**
  2053. * hv_pci_remove() - Remove routine for this VMBus channel
  2054. * @hdev: VMBus's tracking struct for this root PCI bus
  2055. *
  2056. * Return: 0 on success, -errno on failure
  2057. */
  2058. static int hv_pci_remove(struct hv_device *hdev)
  2059. {
  2060. struct hv_pcibus_device *hbus;
  2061. hbus = hv_get_drvdata(hdev);
  2062. if (hbus->state == hv_pcibus_installed) {
  2063. /* Remove the bus from PCI's point of view. */
  2064. pci_lock_rescan_remove();
  2065. pci_stop_root_bus(hbus->pci_bus);
  2066. pci_remove_root_bus(hbus->pci_bus);
  2067. pci_unlock_rescan_remove();
  2068. }
  2069. hv_pci_bus_exit(hdev);
  2070. vmbus_close(hdev->channel);
  2071. iounmap(hbus->cfg_addr);
  2072. hv_free_config_window(hbus);
  2073. pci_free_resource_list(&hbus->resources_for_children);
  2074. hv_pci_free_bridge_windows(hbus);
  2075. irq_domain_remove(hbus->irq_domain);
  2076. irq_domain_free_fwnode(hbus->sysdata.fwnode);
  2077. put_hvpcibus(hbus);
  2078. wait_for_completion(&hbus->remove_event);
  2079. kfree(hbus);
  2080. return 0;
  2081. }
  2082. static const struct hv_vmbus_device_id hv_pci_id_table[] = {
  2083. /* PCI Pass-through Class ID */
  2084. /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
  2085. { HV_PCIE_GUID, },
  2086. { },
  2087. };
  2088. MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
  2089. static struct hv_driver hv_pci_drv = {
  2090. .name = "hv_pci",
  2091. .id_table = hv_pci_id_table,
  2092. .probe = hv_pci_probe,
  2093. .remove = hv_pci_remove,
  2094. };
  2095. static void __exit exit_hv_pci_drv(void)
  2096. {
  2097. vmbus_driver_unregister(&hv_pci_drv);
  2098. }
  2099. static int __init init_hv_pci_drv(void)
  2100. {
  2101. return vmbus_driver_register(&hv_pci_drv);
  2102. }
  2103. module_init(init_hv_pci_drv);
  2104. module_exit(exit_hv_pci_drv);
  2105. MODULE_DESCRIPTION("Hyper-V PCI");
  2106. MODULE_LICENSE("GPL v2");