pci-hyperv.c 74 KB

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