pci-hyperv.c 73 KB

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