pci-hyperv.c 75 KB

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