pci-hyperv.c 67 KB

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