pci-hyperv.c 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * Author:
  6. * Jake Oshins <[email protected]>
  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/pci-ecam.h>
  43. #include <linux/delay.h>
  44. #include <linux/semaphore.h>
  45. #include <linux/irq.h>
  46. #include <linux/msi.h>
  47. #include <linux/hyperv.h>
  48. #include <linux/refcount.h>
  49. #include <linux/irqdomain.h>
  50. #include <linux/acpi.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. PCI_PROTOCOL_VERSION_1_3 = PCI_MAKE_VERSION(1, 3), /* Vibranium */
  63. PCI_PROTOCOL_VERSION_1_4 = PCI_MAKE_VERSION(1, 4), /* WS2022 */
  64. };
  65. #define CPU_AFFINITY_ALL -1ULL
  66. /*
  67. * Supported protocol versions in the order of probing - highest go
  68. * first.
  69. */
  70. static enum pci_protocol_version_t pci_protocol_versions[] = {
  71. PCI_PROTOCOL_VERSION_1_4,
  72. PCI_PROTOCOL_VERSION_1_3,
  73. PCI_PROTOCOL_VERSION_1_2,
  74. PCI_PROTOCOL_VERSION_1_1,
  75. };
  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. * Size of requestor for VMbus; the value is based on the observation
  85. * that having more than one request outstanding is 'rare', and so 64
  86. * should be generous in ensuring that we don't ever run out.
  87. */
  88. #define HV_PCI_RQSTOR_SIZE 64
  89. /*
  90. * Message Types
  91. */
  92. enum pci_message_type {
  93. /*
  94. * Version 1.1
  95. */
  96. PCI_MESSAGE_BASE = 0x42490000,
  97. PCI_BUS_RELATIONS = PCI_MESSAGE_BASE + 0,
  98. PCI_QUERY_BUS_RELATIONS = PCI_MESSAGE_BASE + 1,
  99. PCI_POWER_STATE_CHANGE = PCI_MESSAGE_BASE + 4,
  100. PCI_QUERY_RESOURCE_REQUIREMENTS = PCI_MESSAGE_BASE + 5,
  101. PCI_QUERY_RESOURCE_RESOURCES = PCI_MESSAGE_BASE + 6,
  102. PCI_BUS_D0ENTRY = PCI_MESSAGE_BASE + 7,
  103. PCI_BUS_D0EXIT = PCI_MESSAGE_BASE + 8,
  104. PCI_READ_BLOCK = PCI_MESSAGE_BASE + 9,
  105. PCI_WRITE_BLOCK = PCI_MESSAGE_BASE + 0xA,
  106. PCI_EJECT = PCI_MESSAGE_BASE + 0xB,
  107. PCI_QUERY_STOP = PCI_MESSAGE_BASE + 0xC,
  108. PCI_REENABLE = PCI_MESSAGE_BASE + 0xD,
  109. PCI_QUERY_STOP_FAILED = PCI_MESSAGE_BASE + 0xE,
  110. PCI_EJECTION_COMPLETE = PCI_MESSAGE_BASE + 0xF,
  111. PCI_RESOURCES_ASSIGNED = PCI_MESSAGE_BASE + 0x10,
  112. PCI_RESOURCES_RELEASED = PCI_MESSAGE_BASE + 0x11,
  113. PCI_INVALIDATE_BLOCK = PCI_MESSAGE_BASE + 0x12,
  114. PCI_QUERY_PROTOCOL_VERSION = PCI_MESSAGE_BASE + 0x13,
  115. PCI_CREATE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x14,
  116. PCI_DELETE_INTERRUPT_MESSAGE = PCI_MESSAGE_BASE + 0x15,
  117. PCI_RESOURCES_ASSIGNED2 = PCI_MESSAGE_BASE + 0x16,
  118. PCI_CREATE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x17,
  119. PCI_DELETE_INTERRUPT_MESSAGE2 = PCI_MESSAGE_BASE + 0x18, /* unused */
  120. PCI_BUS_RELATIONS2 = PCI_MESSAGE_BASE + 0x19,
  121. PCI_RESOURCES_ASSIGNED3 = PCI_MESSAGE_BASE + 0x1A,
  122. PCI_CREATE_INTERRUPT_MESSAGE3 = PCI_MESSAGE_BASE + 0x1B,
  123. PCI_MESSAGE_MAXIMUM
  124. };
  125. /*
  126. * Structures defining the virtual PCI Express protocol.
  127. */
  128. union pci_version {
  129. struct {
  130. u16 minor_version;
  131. u16 major_version;
  132. } parts;
  133. u32 version;
  134. } __packed;
  135. /*
  136. * Function numbers are 8-bits wide on Express, as interpreted through ARI,
  137. * which is all this driver does. This representation is the one used in
  138. * Windows, which is what is expected when sending this back and forth with
  139. * the Hyper-V parent partition.
  140. */
  141. union win_slot_encoding {
  142. struct {
  143. u32 dev:5;
  144. u32 func:3;
  145. u32 reserved:24;
  146. } bits;
  147. u32 slot;
  148. } __packed;
  149. /*
  150. * Pretty much as defined in the PCI Specifications.
  151. */
  152. struct pci_function_description {
  153. u16 v_id; /* vendor ID */
  154. u16 d_id; /* device ID */
  155. u8 rev;
  156. u8 prog_intf;
  157. u8 subclass;
  158. u8 base_class;
  159. u32 subsystem_id;
  160. union win_slot_encoding win_slot;
  161. u32 ser; /* serial number */
  162. } __packed;
  163. enum pci_device_description_flags {
  164. HV_PCI_DEVICE_FLAG_NONE = 0x0,
  165. HV_PCI_DEVICE_FLAG_NUMA_AFFINITY = 0x1,
  166. };
  167. struct pci_function_description2 {
  168. u16 v_id; /* vendor ID */
  169. u16 d_id; /* device ID */
  170. u8 rev;
  171. u8 prog_intf;
  172. u8 subclass;
  173. u8 base_class;
  174. u32 subsystem_id;
  175. union win_slot_encoding win_slot;
  176. u32 ser; /* serial number */
  177. u32 flags;
  178. u16 virtual_numa_node;
  179. u16 reserved;
  180. } __packed;
  181. /**
  182. * struct hv_msi_desc
  183. * @vector: IDT entry
  184. * @delivery_mode: As defined in Intel's Programmer's
  185. * Reference Manual, Volume 3, Chapter 8.
  186. * @vector_count: Number of contiguous entries in the
  187. * Interrupt Descriptor Table that are
  188. * occupied by this Message-Signaled
  189. * Interrupt. For "MSI", as first defined
  190. * in PCI 2.2, this can be between 1 and
  191. * 32. For "MSI-X," as first defined in PCI
  192. * 3.0, this must be 1, as each MSI-X table
  193. * entry would have its own descriptor.
  194. * @reserved: Empty space
  195. * @cpu_mask: All the target virtual processors.
  196. */
  197. struct hv_msi_desc {
  198. u8 vector;
  199. u8 delivery_mode;
  200. u16 vector_count;
  201. u32 reserved;
  202. u64 cpu_mask;
  203. } __packed;
  204. /**
  205. * struct hv_msi_desc2 - 1.2 version of hv_msi_desc
  206. * @vector: IDT entry
  207. * @delivery_mode: As defined in Intel's Programmer's
  208. * Reference Manual, Volume 3, Chapter 8.
  209. * @vector_count: Number of contiguous entries in the
  210. * Interrupt Descriptor Table that are
  211. * occupied by this Message-Signaled
  212. * Interrupt. For "MSI", as first defined
  213. * in PCI 2.2, this can be between 1 and
  214. * 32. For "MSI-X," as first defined in PCI
  215. * 3.0, this must be 1, as each MSI-X table
  216. * entry would have its own descriptor.
  217. * @processor_count: number of bits enabled in array.
  218. * @processor_array: All the target virtual processors.
  219. */
  220. struct hv_msi_desc2 {
  221. u8 vector;
  222. u8 delivery_mode;
  223. u16 vector_count;
  224. u16 processor_count;
  225. u16 processor_array[32];
  226. } __packed;
  227. /*
  228. * struct hv_msi_desc3 - 1.3 version of hv_msi_desc
  229. * Everything is the same as in 'hv_msi_desc2' except that the size of the
  230. * 'vector' field is larger to support bigger vector values. For ex: LPI
  231. * vectors on ARM.
  232. */
  233. struct hv_msi_desc3 {
  234. u32 vector;
  235. u8 delivery_mode;
  236. u8 reserved;
  237. u16 vector_count;
  238. u16 processor_count;
  239. u16 processor_array[32];
  240. } __packed;
  241. /**
  242. * struct tran_int_desc
  243. * @reserved: unused, padding
  244. * @vector_count: same as in hv_msi_desc
  245. * @data: This is the "data payload" value that is
  246. * written by the device when it generates
  247. * a message-signaled interrupt, either MSI
  248. * or MSI-X.
  249. * @address: This is the address to which the data
  250. * payload is written on interrupt
  251. * generation.
  252. */
  253. struct tran_int_desc {
  254. u16 reserved;
  255. u16 vector_count;
  256. u32 data;
  257. u64 address;
  258. } __packed;
  259. /*
  260. * A generic message format for virtual PCI.
  261. * Specific message formats are defined later in the file.
  262. */
  263. struct pci_message {
  264. u32 type;
  265. } __packed;
  266. struct pci_child_message {
  267. struct pci_message message_type;
  268. union win_slot_encoding wslot;
  269. } __packed;
  270. struct pci_incoming_message {
  271. struct vmpacket_descriptor hdr;
  272. struct pci_message message_type;
  273. } __packed;
  274. struct pci_response {
  275. struct vmpacket_descriptor hdr;
  276. s32 status; /* negative values are failures */
  277. } __packed;
  278. struct pci_packet {
  279. void (*completion_func)(void *context, struct pci_response *resp,
  280. int resp_packet_size);
  281. void *compl_ctxt;
  282. struct pci_message message[];
  283. };
  284. /*
  285. * Specific message types supporting the PCI protocol.
  286. */
  287. /*
  288. * Version negotiation message. Sent from the guest to the host.
  289. * The guest is free to try different versions until the host
  290. * accepts the version.
  291. *
  292. * pci_version: The protocol version requested.
  293. * is_last_attempt: If TRUE, this is the last version guest will request.
  294. * reservedz: Reserved field, set to zero.
  295. */
  296. struct pci_version_request {
  297. struct pci_message message_type;
  298. u32 protocol_version;
  299. } __packed;
  300. /*
  301. * Bus D0 Entry. This is sent from the guest to the host when the virtual
  302. * bus (PCI Express port) is ready for action.
  303. */
  304. struct pci_bus_d0_entry {
  305. struct pci_message message_type;
  306. u32 reserved;
  307. u64 mmio_base;
  308. } __packed;
  309. struct pci_bus_relations {
  310. struct pci_incoming_message incoming;
  311. u32 device_count;
  312. struct pci_function_description func[];
  313. } __packed;
  314. struct pci_bus_relations2 {
  315. struct pci_incoming_message incoming;
  316. u32 device_count;
  317. struct pci_function_description2 func[];
  318. } __packed;
  319. struct pci_q_res_req_response {
  320. struct vmpacket_descriptor hdr;
  321. s32 status; /* negative values are failures */
  322. u32 probed_bar[PCI_STD_NUM_BARS];
  323. } __packed;
  324. struct pci_set_power {
  325. struct pci_message message_type;
  326. union win_slot_encoding wslot;
  327. u32 power_state; /* In Windows terms */
  328. u32 reserved;
  329. } __packed;
  330. struct pci_set_power_response {
  331. struct vmpacket_descriptor hdr;
  332. s32 status; /* negative values are failures */
  333. union win_slot_encoding wslot;
  334. u32 resultant_state; /* In Windows terms */
  335. u32 reserved;
  336. } __packed;
  337. struct pci_resources_assigned {
  338. struct pci_message message_type;
  339. union win_slot_encoding wslot;
  340. u8 memory_range[0x14][6]; /* not used here */
  341. u32 msi_descriptors;
  342. u32 reserved[4];
  343. } __packed;
  344. struct pci_resources_assigned2 {
  345. struct pci_message message_type;
  346. union win_slot_encoding wslot;
  347. u8 memory_range[0x14][6]; /* not used here */
  348. u32 msi_descriptor_count;
  349. u8 reserved[70];
  350. } __packed;
  351. struct pci_create_interrupt {
  352. struct pci_message message_type;
  353. union win_slot_encoding wslot;
  354. struct hv_msi_desc int_desc;
  355. } __packed;
  356. struct pci_create_int_response {
  357. struct pci_response response;
  358. u32 reserved;
  359. struct tran_int_desc int_desc;
  360. } __packed;
  361. struct pci_create_interrupt2 {
  362. struct pci_message message_type;
  363. union win_slot_encoding wslot;
  364. struct hv_msi_desc2 int_desc;
  365. } __packed;
  366. struct pci_create_interrupt3 {
  367. struct pci_message message_type;
  368. union win_slot_encoding wslot;
  369. struct hv_msi_desc3 int_desc;
  370. } __packed;
  371. struct pci_delete_interrupt {
  372. struct pci_message message_type;
  373. union win_slot_encoding wslot;
  374. struct tran_int_desc int_desc;
  375. } __packed;
  376. /*
  377. * Note: the VM must pass a valid block id, wslot and bytes_requested.
  378. */
  379. struct pci_read_block {
  380. struct pci_message message_type;
  381. u32 block_id;
  382. union win_slot_encoding wslot;
  383. u32 bytes_requested;
  384. } __packed;
  385. struct pci_read_block_response {
  386. struct vmpacket_descriptor hdr;
  387. u32 status;
  388. u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
  389. } __packed;
  390. /*
  391. * Note: the VM must pass a valid block id, wslot and byte_count.
  392. */
  393. struct pci_write_block {
  394. struct pci_message message_type;
  395. u32 block_id;
  396. union win_slot_encoding wslot;
  397. u32 byte_count;
  398. u8 bytes[HV_CONFIG_BLOCK_SIZE_MAX];
  399. } __packed;
  400. struct pci_dev_inval_block {
  401. struct pci_incoming_message incoming;
  402. union win_slot_encoding wslot;
  403. u64 block_mask;
  404. } __packed;
  405. struct pci_dev_incoming {
  406. struct pci_incoming_message incoming;
  407. union win_slot_encoding wslot;
  408. } __packed;
  409. struct pci_eject_response {
  410. struct pci_message message_type;
  411. union win_slot_encoding wslot;
  412. u32 status;
  413. } __packed;
  414. static int pci_ring_size = (4 * PAGE_SIZE);
  415. /*
  416. * Driver specific state.
  417. */
  418. enum hv_pcibus_state {
  419. hv_pcibus_init = 0,
  420. hv_pcibus_probed,
  421. hv_pcibus_installed,
  422. hv_pcibus_removing,
  423. hv_pcibus_maximum
  424. };
  425. struct hv_pcibus_device {
  426. #ifdef CONFIG_X86
  427. struct pci_sysdata sysdata;
  428. #elif defined(CONFIG_ARM64)
  429. struct pci_config_window sysdata;
  430. #endif
  431. struct pci_host_bridge *bridge;
  432. struct fwnode_handle *fwnode;
  433. /* Protocol version negotiated with the host */
  434. enum pci_protocol_version_t protocol_version;
  435. struct mutex state_lock;
  436. enum hv_pcibus_state state;
  437. struct hv_device *hdev;
  438. resource_size_t low_mmio_space;
  439. resource_size_t high_mmio_space;
  440. struct resource *mem_config;
  441. struct resource *low_mmio_res;
  442. struct resource *high_mmio_res;
  443. struct completion *survey_event;
  444. struct pci_bus *pci_bus;
  445. spinlock_t config_lock; /* Avoid two threads writing index page */
  446. spinlock_t device_list_lock; /* Protect lists below */
  447. void __iomem *cfg_addr;
  448. struct list_head children;
  449. struct list_head dr_list;
  450. struct msi_domain_info msi_info;
  451. struct irq_domain *irq_domain;
  452. spinlock_t retarget_msi_interrupt_lock;
  453. struct workqueue_struct *wq;
  454. /* Highest slot of child device with resources allocated */
  455. int wslot_res_allocated;
  456. /* hypercall arg, must not cross page boundary */
  457. struct hv_retarget_device_interrupt retarget_msi_interrupt_params;
  458. /*
  459. * Don't put anything here: retarget_msi_interrupt_params must be last
  460. */
  461. };
  462. /*
  463. * Tracks "Device Relations" messages from the host, which must be both
  464. * processed in order and deferred so that they don't run in the context
  465. * of the incoming packet callback.
  466. */
  467. struct hv_dr_work {
  468. struct work_struct wrk;
  469. struct hv_pcibus_device *bus;
  470. };
  471. struct hv_pcidev_description {
  472. u16 v_id; /* vendor ID */
  473. u16 d_id; /* device ID */
  474. u8 rev;
  475. u8 prog_intf;
  476. u8 subclass;
  477. u8 base_class;
  478. u32 subsystem_id;
  479. union win_slot_encoding win_slot;
  480. u32 ser; /* serial number */
  481. u32 flags;
  482. u16 virtual_numa_node;
  483. };
  484. struct hv_dr_state {
  485. struct list_head list_entry;
  486. u32 device_count;
  487. struct hv_pcidev_description func[];
  488. };
  489. struct hv_pci_dev {
  490. /* List protected by pci_rescan_remove_lock */
  491. struct list_head list_entry;
  492. refcount_t refs;
  493. struct pci_slot *pci_slot;
  494. struct hv_pcidev_description desc;
  495. bool reported_missing;
  496. struct hv_pcibus_device *hbus;
  497. struct work_struct wrk;
  498. void (*block_invalidate)(void *context, u64 block_mask);
  499. void *invalidate_context;
  500. /*
  501. * What would be observed if one wrote 0xFFFFFFFF to a BAR and then
  502. * read it back, for each of the BAR offsets within config space.
  503. */
  504. u32 probed_bar[PCI_STD_NUM_BARS];
  505. };
  506. struct hv_pci_compl {
  507. struct completion host_event;
  508. s32 completion_status;
  509. };
  510. static void hv_pci_onchannelcallback(void *context);
  511. #ifdef CONFIG_X86
  512. #define DELIVERY_MODE APIC_DELIVERY_MODE_FIXED
  513. #define FLOW_HANDLER handle_edge_irq
  514. #define FLOW_NAME "edge"
  515. static int hv_pci_irqchip_init(void)
  516. {
  517. return 0;
  518. }
  519. static struct irq_domain *hv_pci_get_root_domain(void)
  520. {
  521. return x86_vector_domain;
  522. }
  523. static unsigned int hv_msi_get_int_vector(struct irq_data *data)
  524. {
  525. struct irq_cfg *cfg = irqd_cfg(data);
  526. return cfg->vector;
  527. }
  528. static int hv_msi_prepare(struct irq_domain *domain, struct device *dev,
  529. int nvec, msi_alloc_info_t *info)
  530. {
  531. int ret = pci_msi_prepare(domain, dev, nvec, info);
  532. /*
  533. * By using the interrupt remapper in the hypervisor IOMMU, contiguous
  534. * CPU vectors is not needed for multi-MSI
  535. */
  536. if (info->type == X86_IRQ_ALLOC_TYPE_PCI_MSI)
  537. info->flags &= ~X86_IRQ_ALLOC_CONTIGUOUS_VECTORS;
  538. return ret;
  539. }
  540. /**
  541. * hv_arch_irq_unmask() - "Unmask" the IRQ by setting its current
  542. * affinity.
  543. * @data: Describes the IRQ
  544. *
  545. * Build new a destination for the MSI and make a hypercall to
  546. * update the Interrupt Redirection Table. "Device Logical ID"
  547. * is built out of this PCI bus's instance GUID and the function
  548. * number of the device.
  549. */
  550. static void hv_arch_irq_unmask(struct irq_data *data)
  551. {
  552. struct msi_desc *msi_desc = irq_data_get_msi_desc(data);
  553. struct hv_retarget_device_interrupt *params;
  554. struct tran_int_desc *int_desc;
  555. struct hv_pcibus_device *hbus;
  556. const struct cpumask *dest;
  557. cpumask_var_t tmp;
  558. struct pci_bus *pbus;
  559. struct pci_dev *pdev;
  560. unsigned long flags;
  561. u32 var_size = 0;
  562. int cpu, nr_bank;
  563. u64 res;
  564. dest = irq_data_get_effective_affinity_mask(data);
  565. pdev = msi_desc_to_pci_dev(msi_desc);
  566. pbus = pdev->bus;
  567. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  568. int_desc = data->chip_data;
  569. if (!int_desc) {
  570. dev_warn(&hbus->hdev->device, "%s() can not unmask irq %u\n",
  571. __func__, data->irq);
  572. return;
  573. }
  574. spin_lock_irqsave(&hbus->retarget_msi_interrupt_lock, flags);
  575. params = &hbus->retarget_msi_interrupt_params;
  576. memset(params, 0, sizeof(*params));
  577. params->partition_id = HV_PARTITION_ID_SELF;
  578. params->int_entry.source = HV_INTERRUPT_SOURCE_MSI;
  579. params->int_entry.msi_entry.address.as_uint32 = int_desc->address & 0xffffffff;
  580. params->int_entry.msi_entry.data.as_uint32 = int_desc->data;
  581. params->device_id = (hbus->hdev->dev_instance.b[5] << 24) |
  582. (hbus->hdev->dev_instance.b[4] << 16) |
  583. (hbus->hdev->dev_instance.b[7] << 8) |
  584. (hbus->hdev->dev_instance.b[6] & 0xf8) |
  585. PCI_FUNC(pdev->devfn);
  586. params->int_target.vector = hv_msi_get_int_vector(data);
  587. /*
  588. * Honoring apic->delivery_mode set to APIC_DELIVERY_MODE_FIXED by
  589. * setting the HV_DEVICE_INTERRUPT_TARGET_MULTICAST flag results in a
  590. * spurious interrupt storm. Not doing so does not seem to have a
  591. * negative effect (yet?).
  592. */
  593. if (hbus->protocol_version >= PCI_PROTOCOL_VERSION_1_2) {
  594. /*
  595. * PCI_PROTOCOL_VERSION_1_2 supports the VP_SET version of the
  596. * HVCALL_RETARGET_INTERRUPT hypercall, which also coincides
  597. * with >64 VP support.
  598. * ms_hyperv.hints & HV_X64_EX_PROCESSOR_MASKS_RECOMMENDED
  599. * is not sufficient for this hypercall.
  600. */
  601. params->int_target.flags |=
  602. HV_DEVICE_INTERRUPT_TARGET_PROCESSOR_SET;
  603. if (!alloc_cpumask_var(&tmp, GFP_ATOMIC)) {
  604. res = 1;
  605. goto exit_unlock;
  606. }
  607. cpumask_and(tmp, dest, cpu_online_mask);
  608. nr_bank = cpumask_to_vpset(&params->int_target.vp_set, tmp);
  609. free_cpumask_var(tmp);
  610. if (nr_bank <= 0) {
  611. res = 1;
  612. goto exit_unlock;
  613. }
  614. /*
  615. * var-sized hypercall, var-size starts after vp_mask (thus
  616. * vp_set.format does not count, but vp_set.valid_bank_mask
  617. * does).
  618. */
  619. var_size = 1 + nr_bank;
  620. } else {
  621. for_each_cpu_and(cpu, dest, cpu_online_mask) {
  622. params->int_target.vp_mask |=
  623. (1ULL << hv_cpu_number_to_vp_number(cpu));
  624. }
  625. }
  626. res = hv_do_hypercall(HVCALL_RETARGET_INTERRUPT | (var_size << 17),
  627. params, NULL);
  628. exit_unlock:
  629. spin_unlock_irqrestore(&hbus->retarget_msi_interrupt_lock, flags);
  630. /*
  631. * During hibernation, when a CPU is offlined, the kernel tries
  632. * to move the interrupt to the remaining CPUs that haven't
  633. * been offlined yet. In this case, the below hv_do_hypercall()
  634. * always fails since the vmbus channel has been closed:
  635. * refer to cpu_disable_common() -> fixup_irqs() ->
  636. * irq_migrate_all_off_this_cpu() -> migrate_one_irq().
  637. *
  638. * Suppress the error message for hibernation because the failure
  639. * during hibernation does not matter (at this time all the devices
  640. * have been frozen). Note: the correct affinity info is still updated
  641. * into the irqdata data structure in migrate_one_irq() ->
  642. * irq_do_set_affinity() -> hv_set_affinity(), so later when the VM
  643. * resumes, hv_pci_restore_msi_state() is able to correctly restore
  644. * the interrupt with the correct affinity.
  645. */
  646. if (!hv_result_success(res) && hbus->state != hv_pcibus_removing)
  647. dev_err(&hbus->hdev->device,
  648. "%s() failed: %#llx", __func__, res);
  649. }
  650. #elif defined(CONFIG_ARM64)
  651. /*
  652. * SPI vectors to use for vPCI; arch SPIs range is [32, 1019], but leaving a bit
  653. * of room at the start to allow for SPIs to be specified through ACPI and
  654. * starting with a power of two to satisfy power of 2 multi-MSI requirement.
  655. */
  656. #define HV_PCI_MSI_SPI_START 64
  657. #define HV_PCI_MSI_SPI_NR (1020 - HV_PCI_MSI_SPI_START)
  658. #define DELIVERY_MODE 0
  659. #define FLOW_HANDLER NULL
  660. #define FLOW_NAME NULL
  661. #define hv_msi_prepare NULL
  662. struct hv_pci_chip_data {
  663. DECLARE_BITMAP(spi_map, HV_PCI_MSI_SPI_NR);
  664. struct mutex map_lock;
  665. };
  666. /* Hyper-V vPCI MSI GIC IRQ domain */
  667. static struct irq_domain *hv_msi_gic_irq_domain;
  668. /* Hyper-V PCI MSI IRQ chip */
  669. static struct irq_chip hv_arm64_msi_irq_chip = {
  670. .name = "MSI",
  671. .irq_set_affinity = irq_chip_set_affinity_parent,
  672. .irq_eoi = irq_chip_eoi_parent,
  673. .irq_mask = irq_chip_mask_parent,
  674. .irq_unmask = irq_chip_unmask_parent
  675. };
  676. static unsigned int hv_msi_get_int_vector(struct irq_data *irqd)
  677. {
  678. return irqd->parent_data->hwirq;
  679. }
  680. /*
  681. * @nr_bm_irqs: Indicates the number of IRQs that were allocated from
  682. * the bitmap.
  683. * @nr_dom_irqs: Indicates the number of IRQs that were allocated from
  684. * the parent domain.
  685. */
  686. static void hv_pci_vec_irq_free(struct irq_domain *domain,
  687. unsigned int virq,
  688. unsigned int nr_bm_irqs,
  689. unsigned int nr_dom_irqs)
  690. {
  691. struct hv_pci_chip_data *chip_data = domain->host_data;
  692. struct irq_data *d = irq_domain_get_irq_data(domain, virq);
  693. int first = d->hwirq - HV_PCI_MSI_SPI_START;
  694. int i;
  695. mutex_lock(&chip_data->map_lock);
  696. bitmap_release_region(chip_data->spi_map,
  697. first,
  698. get_count_order(nr_bm_irqs));
  699. mutex_unlock(&chip_data->map_lock);
  700. for (i = 0; i < nr_dom_irqs; i++) {
  701. if (i)
  702. d = irq_domain_get_irq_data(domain, virq + i);
  703. irq_domain_reset_irq_data(d);
  704. }
  705. irq_domain_free_irqs_parent(domain, virq, nr_dom_irqs);
  706. }
  707. static void hv_pci_vec_irq_domain_free(struct irq_domain *domain,
  708. unsigned int virq,
  709. unsigned int nr_irqs)
  710. {
  711. hv_pci_vec_irq_free(domain, virq, nr_irqs, nr_irqs);
  712. }
  713. static int hv_pci_vec_alloc_device_irq(struct irq_domain *domain,
  714. unsigned int nr_irqs,
  715. irq_hw_number_t *hwirq)
  716. {
  717. struct hv_pci_chip_data *chip_data = domain->host_data;
  718. int index;
  719. /* Find and allocate region from the SPI bitmap */
  720. mutex_lock(&chip_data->map_lock);
  721. index = bitmap_find_free_region(chip_data->spi_map,
  722. HV_PCI_MSI_SPI_NR,
  723. get_count_order(nr_irqs));
  724. mutex_unlock(&chip_data->map_lock);
  725. if (index < 0)
  726. return -ENOSPC;
  727. *hwirq = index + HV_PCI_MSI_SPI_START;
  728. return 0;
  729. }
  730. static int hv_pci_vec_irq_gic_domain_alloc(struct irq_domain *domain,
  731. unsigned int virq,
  732. irq_hw_number_t hwirq)
  733. {
  734. struct irq_fwspec fwspec;
  735. struct irq_data *d;
  736. int ret;
  737. fwspec.fwnode = domain->parent->fwnode;
  738. fwspec.param_count = 2;
  739. fwspec.param[0] = hwirq;
  740. fwspec.param[1] = IRQ_TYPE_EDGE_RISING;
  741. ret = irq_domain_alloc_irqs_parent(domain, virq, 1, &fwspec);
  742. if (ret)
  743. return ret;
  744. /*
  745. * Since the interrupt specifier is not coming from ACPI or DT, the
  746. * trigger type will need to be set explicitly. Otherwise, it will be
  747. * set to whatever is in the GIC configuration.
  748. */
  749. d = irq_domain_get_irq_data(domain->parent, virq);
  750. return d->chip->irq_set_type(d, IRQ_TYPE_EDGE_RISING);
  751. }
  752. static int hv_pci_vec_irq_domain_alloc(struct irq_domain *domain,
  753. unsigned int virq, unsigned int nr_irqs,
  754. void *args)
  755. {
  756. irq_hw_number_t hwirq;
  757. unsigned int i;
  758. int ret;
  759. ret = hv_pci_vec_alloc_device_irq(domain, nr_irqs, &hwirq);
  760. if (ret)
  761. return ret;
  762. for (i = 0; i < nr_irqs; i++) {
  763. ret = hv_pci_vec_irq_gic_domain_alloc(domain, virq + i,
  764. hwirq + i);
  765. if (ret) {
  766. hv_pci_vec_irq_free(domain, virq, nr_irqs, i);
  767. return ret;
  768. }
  769. irq_domain_set_hwirq_and_chip(domain, virq + i,
  770. hwirq + i,
  771. &hv_arm64_msi_irq_chip,
  772. domain->host_data);
  773. pr_debug("pID:%d vID:%u\n", (int)(hwirq + i), virq + i);
  774. }
  775. return 0;
  776. }
  777. /*
  778. * Pick the first cpu as the irq affinity that can be temporarily used for
  779. * composing MSI from the hypervisor. GIC will eventually set the right
  780. * affinity for the irq and the 'unmask' will retarget the interrupt to that
  781. * cpu.
  782. */
  783. static int hv_pci_vec_irq_domain_activate(struct irq_domain *domain,
  784. struct irq_data *irqd, bool reserve)
  785. {
  786. int cpu = cpumask_first(cpu_present_mask);
  787. irq_data_update_effective_affinity(irqd, cpumask_of(cpu));
  788. return 0;
  789. }
  790. static const struct irq_domain_ops hv_pci_domain_ops = {
  791. .alloc = hv_pci_vec_irq_domain_alloc,
  792. .free = hv_pci_vec_irq_domain_free,
  793. .activate = hv_pci_vec_irq_domain_activate,
  794. };
  795. static int hv_pci_irqchip_init(void)
  796. {
  797. static struct hv_pci_chip_data *chip_data;
  798. struct fwnode_handle *fn = NULL;
  799. int ret = -ENOMEM;
  800. chip_data = kzalloc(sizeof(*chip_data), GFP_KERNEL);
  801. if (!chip_data)
  802. return ret;
  803. mutex_init(&chip_data->map_lock);
  804. fn = irq_domain_alloc_named_fwnode("hv_vpci_arm64");
  805. if (!fn)
  806. goto free_chip;
  807. /*
  808. * IRQ domain once enabled, should not be removed since there is no
  809. * way to ensure that all the corresponding devices are also gone and
  810. * no interrupts will be generated.
  811. */
  812. hv_msi_gic_irq_domain = acpi_irq_create_hierarchy(0, HV_PCI_MSI_SPI_NR,
  813. fn, &hv_pci_domain_ops,
  814. chip_data);
  815. if (!hv_msi_gic_irq_domain) {
  816. pr_err("Failed to create Hyper-V arm64 vPCI MSI IRQ domain\n");
  817. goto free_chip;
  818. }
  819. return 0;
  820. free_chip:
  821. kfree(chip_data);
  822. if (fn)
  823. irq_domain_free_fwnode(fn);
  824. return ret;
  825. }
  826. static struct irq_domain *hv_pci_get_root_domain(void)
  827. {
  828. return hv_msi_gic_irq_domain;
  829. }
  830. /*
  831. * SPIs are used for interrupts of PCI devices and SPIs is managed via GICD
  832. * registers which Hyper-V already supports, so no hypercall needed.
  833. */
  834. static void hv_arch_irq_unmask(struct irq_data *data) { }
  835. #endif /* CONFIG_ARM64 */
  836. /**
  837. * hv_pci_generic_compl() - Invoked for a completion packet
  838. * @context: Set up by the sender of the packet.
  839. * @resp: The response packet
  840. * @resp_packet_size: Size in bytes of the packet
  841. *
  842. * This function is used to trigger an event and report status
  843. * for any message for which the completion packet contains a
  844. * status and nothing else.
  845. */
  846. static void hv_pci_generic_compl(void *context, struct pci_response *resp,
  847. int resp_packet_size)
  848. {
  849. struct hv_pci_compl *comp_pkt = context;
  850. comp_pkt->completion_status = resp->status;
  851. complete(&comp_pkt->host_event);
  852. }
  853. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  854. u32 wslot);
  855. static void get_pcichild(struct hv_pci_dev *hpdev)
  856. {
  857. refcount_inc(&hpdev->refs);
  858. }
  859. static void put_pcichild(struct hv_pci_dev *hpdev)
  860. {
  861. if (refcount_dec_and_test(&hpdev->refs))
  862. kfree(hpdev);
  863. }
  864. /*
  865. * There is no good way to get notified from vmbus_onoffer_rescind(),
  866. * so let's use polling here, since this is not a hot path.
  867. */
  868. static int wait_for_response(struct hv_device *hdev,
  869. struct completion *comp)
  870. {
  871. while (true) {
  872. if (hdev->channel->rescind) {
  873. dev_warn_once(&hdev->device, "The device is gone.\n");
  874. return -ENODEV;
  875. }
  876. if (wait_for_completion_timeout(comp, HZ / 10))
  877. break;
  878. }
  879. return 0;
  880. }
  881. /**
  882. * devfn_to_wslot() - Convert from Linux PCI slot to Windows
  883. * @devfn: The Linux representation of PCI slot
  884. *
  885. * Windows uses a slightly different representation of PCI slot.
  886. *
  887. * Return: The Windows representation
  888. */
  889. static u32 devfn_to_wslot(int devfn)
  890. {
  891. union win_slot_encoding wslot;
  892. wslot.slot = 0;
  893. wslot.bits.dev = PCI_SLOT(devfn);
  894. wslot.bits.func = PCI_FUNC(devfn);
  895. return wslot.slot;
  896. }
  897. /**
  898. * wslot_to_devfn() - Convert from Windows PCI slot to Linux
  899. * @wslot: The Windows representation of PCI slot
  900. *
  901. * Windows uses a slightly different representation of PCI slot.
  902. *
  903. * Return: The Linux representation
  904. */
  905. static int wslot_to_devfn(u32 wslot)
  906. {
  907. union win_slot_encoding slot_no;
  908. slot_no.slot = wslot;
  909. return PCI_DEVFN(slot_no.bits.dev, slot_no.bits.func);
  910. }
  911. /*
  912. * PCI Configuration Space for these root PCI buses is implemented as a pair
  913. * of pages in memory-mapped I/O space. Writing to the first page chooses
  914. * the PCI function being written or read. Once the first page has been
  915. * written to, the following page maps in the entire configuration space of
  916. * the function.
  917. */
  918. /**
  919. * _hv_pcifront_read_config() - Internal PCI config read
  920. * @hpdev: The PCI driver's representation of the device
  921. * @where: Offset within config space
  922. * @size: Size of the transfer
  923. * @val: Pointer to the buffer receiving the data
  924. */
  925. static void _hv_pcifront_read_config(struct hv_pci_dev *hpdev, int where,
  926. int size, u32 *val)
  927. {
  928. unsigned long flags;
  929. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  930. /*
  931. * If the attempt is to read the IDs or the ROM BAR, simulate that.
  932. */
  933. if (where + size <= PCI_COMMAND) {
  934. memcpy(val, ((u8 *)&hpdev->desc.v_id) + where, size);
  935. } else if (where >= PCI_CLASS_REVISION && where + size <=
  936. PCI_CACHE_LINE_SIZE) {
  937. memcpy(val, ((u8 *)&hpdev->desc.rev) + where -
  938. PCI_CLASS_REVISION, size);
  939. } else if (where >= PCI_SUBSYSTEM_VENDOR_ID && where + size <=
  940. PCI_ROM_ADDRESS) {
  941. memcpy(val, (u8 *)&hpdev->desc.subsystem_id + where -
  942. PCI_SUBSYSTEM_VENDOR_ID, size);
  943. } else if (where >= PCI_ROM_ADDRESS && where + size <=
  944. PCI_CAPABILITY_LIST) {
  945. /* ROM BARs are unimplemented */
  946. *val = 0;
  947. } else if (where >= PCI_INTERRUPT_LINE && where + size <=
  948. PCI_INTERRUPT_PIN) {
  949. /*
  950. * Interrupt Line and Interrupt PIN are hard-wired to zero
  951. * because this front-end only supports message-signaled
  952. * interrupts.
  953. */
  954. *val = 0;
  955. } else if (where + size <= CFG_PAGE_SIZE) {
  956. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  957. /* Choose the function to be read. (See comment above) */
  958. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  959. /* Make sure the function was chosen before we start reading. */
  960. mb();
  961. /* Read from that function's config space. */
  962. switch (size) {
  963. case 1:
  964. *val = readb(addr);
  965. break;
  966. case 2:
  967. *val = readw(addr);
  968. break;
  969. default:
  970. *val = readl(addr);
  971. break;
  972. }
  973. /*
  974. * Make sure the read was done before we release the spinlock
  975. * allowing consecutive reads/writes.
  976. */
  977. mb();
  978. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  979. } else {
  980. dev_err(&hpdev->hbus->hdev->device,
  981. "Attempt to read beyond a function's config space.\n");
  982. }
  983. }
  984. static u16 hv_pcifront_get_vendor_id(struct hv_pci_dev *hpdev)
  985. {
  986. u16 ret;
  987. unsigned long flags;
  988. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET +
  989. PCI_VENDOR_ID;
  990. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  991. /* Choose the function to be read. (See comment above) */
  992. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  993. /* Make sure the function was chosen before we start reading. */
  994. mb();
  995. /* Read from that function's config space. */
  996. ret = readw(addr);
  997. /*
  998. * mb() is not required here, because the spin_unlock_irqrestore()
  999. * is a barrier.
  1000. */
  1001. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  1002. return ret;
  1003. }
  1004. /**
  1005. * _hv_pcifront_write_config() - Internal PCI config write
  1006. * @hpdev: The PCI driver's representation of the device
  1007. * @where: Offset within config space
  1008. * @size: Size of the transfer
  1009. * @val: The data being transferred
  1010. */
  1011. static void _hv_pcifront_write_config(struct hv_pci_dev *hpdev, int where,
  1012. int size, u32 val)
  1013. {
  1014. unsigned long flags;
  1015. void __iomem *addr = hpdev->hbus->cfg_addr + CFG_PAGE_OFFSET + where;
  1016. if (where >= PCI_SUBSYSTEM_VENDOR_ID &&
  1017. where + size <= PCI_CAPABILITY_LIST) {
  1018. /* SSIDs and ROM BARs are read-only */
  1019. } else if (where >= PCI_COMMAND && where + size <= CFG_PAGE_SIZE) {
  1020. spin_lock_irqsave(&hpdev->hbus->config_lock, flags);
  1021. /* Choose the function to be written. (See comment above) */
  1022. writel(hpdev->desc.win_slot.slot, hpdev->hbus->cfg_addr);
  1023. /* Make sure the function was chosen before we start writing. */
  1024. wmb();
  1025. /* Write to that function's config space. */
  1026. switch (size) {
  1027. case 1:
  1028. writeb(val, addr);
  1029. break;
  1030. case 2:
  1031. writew(val, addr);
  1032. break;
  1033. default:
  1034. writel(val, addr);
  1035. break;
  1036. }
  1037. /*
  1038. * Make sure the write was done before we release the spinlock
  1039. * allowing consecutive reads/writes.
  1040. */
  1041. mb();
  1042. spin_unlock_irqrestore(&hpdev->hbus->config_lock, flags);
  1043. } else {
  1044. dev_err(&hpdev->hbus->hdev->device,
  1045. "Attempt to write beyond a function's config space.\n");
  1046. }
  1047. }
  1048. /**
  1049. * hv_pcifront_read_config() - Read configuration space
  1050. * @bus: PCI Bus structure
  1051. * @devfn: Device/function
  1052. * @where: Offset from base
  1053. * @size: Byte/word/dword
  1054. * @val: Value to be read
  1055. *
  1056. * Return: PCIBIOS_SUCCESSFUL on success
  1057. * PCIBIOS_DEVICE_NOT_FOUND on failure
  1058. */
  1059. static int hv_pcifront_read_config(struct pci_bus *bus, unsigned int devfn,
  1060. int where, int size, u32 *val)
  1061. {
  1062. struct hv_pcibus_device *hbus =
  1063. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  1064. struct hv_pci_dev *hpdev;
  1065. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  1066. if (!hpdev)
  1067. return PCIBIOS_DEVICE_NOT_FOUND;
  1068. _hv_pcifront_read_config(hpdev, where, size, val);
  1069. put_pcichild(hpdev);
  1070. return PCIBIOS_SUCCESSFUL;
  1071. }
  1072. /**
  1073. * hv_pcifront_write_config() - Write configuration space
  1074. * @bus: PCI Bus structure
  1075. * @devfn: Device/function
  1076. * @where: Offset from base
  1077. * @size: Byte/word/dword
  1078. * @val: Value to be written to device
  1079. *
  1080. * Return: PCIBIOS_SUCCESSFUL on success
  1081. * PCIBIOS_DEVICE_NOT_FOUND on failure
  1082. */
  1083. static int hv_pcifront_write_config(struct pci_bus *bus, unsigned int devfn,
  1084. int where, int size, u32 val)
  1085. {
  1086. struct hv_pcibus_device *hbus =
  1087. container_of(bus->sysdata, struct hv_pcibus_device, sysdata);
  1088. struct hv_pci_dev *hpdev;
  1089. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(devfn));
  1090. if (!hpdev)
  1091. return PCIBIOS_DEVICE_NOT_FOUND;
  1092. _hv_pcifront_write_config(hpdev, where, size, val);
  1093. put_pcichild(hpdev);
  1094. return PCIBIOS_SUCCESSFUL;
  1095. }
  1096. /* PCIe operations */
  1097. static struct pci_ops hv_pcifront_ops = {
  1098. .read = hv_pcifront_read_config,
  1099. .write = hv_pcifront_write_config,
  1100. };
  1101. /*
  1102. * Paravirtual backchannel
  1103. *
  1104. * Hyper-V SR-IOV provides a backchannel mechanism in software for
  1105. * communication between a VF driver and a PF driver. These
  1106. * "configuration blocks" are similar in concept to PCI configuration space,
  1107. * but instead of doing reads and writes in 32-bit chunks through a very slow
  1108. * path, packets of up to 128 bytes can be sent or received asynchronously.
  1109. *
  1110. * Nearly every SR-IOV device contains just such a communications channel in
  1111. * hardware, so using this one in software is usually optional. Using the
  1112. * software channel, however, allows driver implementers to leverage software
  1113. * tools that fuzz the communications channel looking for vulnerabilities.
  1114. *
  1115. * The usage model for these packets puts the responsibility for reading or
  1116. * writing on the VF driver. The VF driver sends a read or a write packet,
  1117. * indicating which "block" is being referred to by number.
  1118. *
  1119. * If the PF driver wishes to initiate communication, it can "invalidate" one or
  1120. * more of the first 64 blocks. This invalidation is delivered via a callback
  1121. * supplied by the VF driver by this driver.
  1122. *
  1123. * No protocol is implied, except that supplied by the PF and VF drivers.
  1124. */
  1125. struct hv_read_config_compl {
  1126. struct hv_pci_compl comp_pkt;
  1127. void *buf;
  1128. unsigned int len;
  1129. unsigned int bytes_returned;
  1130. };
  1131. /**
  1132. * hv_pci_read_config_compl() - Invoked when a response packet
  1133. * for a read config block operation arrives.
  1134. * @context: Identifies the read config operation
  1135. * @resp: The response packet itself
  1136. * @resp_packet_size: Size in bytes of the response packet
  1137. */
  1138. static void hv_pci_read_config_compl(void *context, struct pci_response *resp,
  1139. int resp_packet_size)
  1140. {
  1141. struct hv_read_config_compl *comp = context;
  1142. struct pci_read_block_response *read_resp =
  1143. (struct pci_read_block_response *)resp;
  1144. unsigned int data_len, hdr_len;
  1145. hdr_len = offsetof(struct pci_read_block_response, bytes);
  1146. if (resp_packet_size < hdr_len) {
  1147. comp->comp_pkt.completion_status = -1;
  1148. goto out;
  1149. }
  1150. data_len = resp_packet_size - hdr_len;
  1151. if (data_len > 0 && read_resp->status == 0) {
  1152. comp->bytes_returned = min(comp->len, data_len);
  1153. memcpy(comp->buf, read_resp->bytes, comp->bytes_returned);
  1154. } else {
  1155. comp->bytes_returned = 0;
  1156. }
  1157. comp->comp_pkt.completion_status = read_resp->status;
  1158. out:
  1159. complete(&comp->comp_pkt.host_event);
  1160. }
  1161. /**
  1162. * hv_read_config_block() - Sends a read config block request to
  1163. * the back-end driver running in the Hyper-V parent partition.
  1164. * @pdev: The PCI driver's representation for this device.
  1165. * @buf: Buffer into which the config block will be copied.
  1166. * @len: Size in bytes of buf.
  1167. * @block_id: Identifies the config block which has been requested.
  1168. * @bytes_returned: Size which came back from the back-end driver.
  1169. *
  1170. * Return: 0 on success, -errno on failure
  1171. */
  1172. static int hv_read_config_block(struct pci_dev *pdev, void *buf,
  1173. unsigned int len, unsigned int block_id,
  1174. unsigned int *bytes_returned)
  1175. {
  1176. struct hv_pcibus_device *hbus =
  1177. container_of(pdev->bus->sysdata, struct hv_pcibus_device,
  1178. sysdata);
  1179. struct {
  1180. struct pci_packet pkt;
  1181. char buf[sizeof(struct pci_read_block)];
  1182. } pkt;
  1183. struct hv_read_config_compl comp_pkt;
  1184. struct pci_read_block *read_blk;
  1185. int ret;
  1186. if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
  1187. return -EINVAL;
  1188. init_completion(&comp_pkt.comp_pkt.host_event);
  1189. comp_pkt.buf = buf;
  1190. comp_pkt.len = len;
  1191. memset(&pkt, 0, sizeof(pkt));
  1192. pkt.pkt.completion_func = hv_pci_read_config_compl;
  1193. pkt.pkt.compl_ctxt = &comp_pkt;
  1194. read_blk = (struct pci_read_block *)&pkt.pkt.message;
  1195. read_blk->message_type.type = PCI_READ_BLOCK;
  1196. read_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
  1197. read_blk->block_id = block_id;
  1198. read_blk->bytes_requested = len;
  1199. ret = vmbus_sendpacket(hbus->hdev->channel, read_blk,
  1200. sizeof(*read_blk), (unsigned long)&pkt.pkt,
  1201. VM_PKT_DATA_INBAND,
  1202. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1203. if (ret)
  1204. return ret;
  1205. ret = wait_for_response(hbus->hdev, &comp_pkt.comp_pkt.host_event);
  1206. if (ret)
  1207. return ret;
  1208. if (comp_pkt.comp_pkt.completion_status != 0 ||
  1209. comp_pkt.bytes_returned == 0) {
  1210. dev_err(&hbus->hdev->device,
  1211. "Read Config Block failed: 0x%x, bytes_returned=%d\n",
  1212. comp_pkt.comp_pkt.completion_status,
  1213. comp_pkt.bytes_returned);
  1214. return -EIO;
  1215. }
  1216. *bytes_returned = comp_pkt.bytes_returned;
  1217. return 0;
  1218. }
  1219. /**
  1220. * hv_pci_write_config_compl() - Invoked when a response packet for a write
  1221. * config block operation arrives.
  1222. * @context: Identifies the write config operation
  1223. * @resp: The response packet itself
  1224. * @resp_packet_size: Size in bytes of the response packet
  1225. */
  1226. static void hv_pci_write_config_compl(void *context, struct pci_response *resp,
  1227. int resp_packet_size)
  1228. {
  1229. struct hv_pci_compl *comp_pkt = context;
  1230. comp_pkt->completion_status = resp->status;
  1231. complete(&comp_pkt->host_event);
  1232. }
  1233. /**
  1234. * hv_write_config_block() - Sends a write config block request to the
  1235. * back-end driver running in the Hyper-V parent partition.
  1236. * @pdev: The PCI driver's representation for this device.
  1237. * @buf: Buffer from which the config block will be copied.
  1238. * @len: Size in bytes of buf.
  1239. * @block_id: Identifies the config block which is being written.
  1240. *
  1241. * Return: 0 on success, -errno on failure
  1242. */
  1243. static int hv_write_config_block(struct pci_dev *pdev, void *buf,
  1244. unsigned int len, unsigned int block_id)
  1245. {
  1246. struct hv_pcibus_device *hbus =
  1247. container_of(pdev->bus->sysdata, struct hv_pcibus_device,
  1248. sysdata);
  1249. struct {
  1250. struct pci_packet pkt;
  1251. char buf[sizeof(struct pci_write_block)];
  1252. u32 reserved;
  1253. } pkt;
  1254. struct hv_pci_compl comp_pkt;
  1255. struct pci_write_block *write_blk;
  1256. u32 pkt_size;
  1257. int ret;
  1258. if (len == 0 || len > HV_CONFIG_BLOCK_SIZE_MAX)
  1259. return -EINVAL;
  1260. init_completion(&comp_pkt.host_event);
  1261. memset(&pkt, 0, sizeof(pkt));
  1262. pkt.pkt.completion_func = hv_pci_write_config_compl;
  1263. pkt.pkt.compl_ctxt = &comp_pkt;
  1264. write_blk = (struct pci_write_block *)&pkt.pkt.message;
  1265. write_blk->message_type.type = PCI_WRITE_BLOCK;
  1266. write_blk->wslot.slot = devfn_to_wslot(pdev->devfn);
  1267. write_blk->block_id = block_id;
  1268. write_blk->byte_count = len;
  1269. memcpy(write_blk->bytes, buf, len);
  1270. pkt_size = offsetof(struct pci_write_block, bytes) + len;
  1271. /*
  1272. * This quirk is required on some hosts shipped around 2018, because
  1273. * these hosts don't check the pkt_size correctly (new hosts have been
  1274. * fixed since early 2019). The quirk is also safe on very old hosts
  1275. * and new hosts, because, on them, what really matters is the length
  1276. * specified in write_blk->byte_count.
  1277. */
  1278. pkt_size += sizeof(pkt.reserved);
  1279. ret = vmbus_sendpacket(hbus->hdev->channel, write_blk, pkt_size,
  1280. (unsigned long)&pkt.pkt, VM_PKT_DATA_INBAND,
  1281. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1282. if (ret)
  1283. return ret;
  1284. ret = wait_for_response(hbus->hdev, &comp_pkt.host_event);
  1285. if (ret)
  1286. return ret;
  1287. if (comp_pkt.completion_status != 0) {
  1288. dev_err(&hbus->hdev->device,
  1289. "Write Config Block failed: 0x%x\n",
  1290. comp_pkt.completion_status);
  1291. return -EIO;
  1292. }
  1293. return 0;
  1294. }
  1295. /**
  1296. * hv_register_block_invalidate() - Invoked when a config block invalidation
  1297. * arrives from the back-end driver.
  1298. * @pdev: The PCI driver's representation for this device.
  1299. * @context: Identifies the device.
  1300. * @block_invalidate: Identifies all of the blocks being invalidated.
  1301. *
  1302. * Return: 0 on success, -errno on failure
  1303. */
  1304. static int hv_register_block_invalidate(struct pci_dev *pdev, void *context,
  1305. void (*block_invalidate)(void *context,
  1306. u64 block_mask))
  1307. {
  1308. struct hv_pcibus_device *hbus =
  1309. container_of(pdev->bus->sysdata, struct hv_pcibus_device,
  1310. sysdata);
  1311. struct hv_pci_dev *hpdev;
  1312. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  1313. if (!hpdev)
  1314. return -ENODEV;
  1315. hpdev->block_invalidate = block_invalidate;
  1316. hpdev->invalidate_context = context;
  1317. put_pcichild(hpdev);
  1318. return 0;
  1319. }
  1320. /* Interrupt management hooks */
  1321. static void hv_int_desc_free(struct hv_pci_dev *hpdev,
  1322. struct tran_int_desc *int_desc)
  1323. {
  1324. struct pci_delete_interrupt *int_pkt;
  1325. struct {
  1326. struct pci_packet pkt;
  1327. u8 buffer[sizeof(struct pci_delete_interrupt)];
  1328. } ctxt;
  1329. if (!int_desc->vector_count) {
  1330. kfree(int_desc);
  1331. return;
  1332. }
  1333. memset(&ctxt, 0, sizeof(ctxt));
  1334. int_pkt = (struct pci_delete_interrupt *)&ctxt.pkt.message;
  1335. int_pkt->message_type.type =
  1336. PCI_DELETE_INTERRUPT_MESSAGE;
  1337. int_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  1338. int_pkt->int_desc = *int_desc;
  1339. vmbus_sendpacket(hpdev->hbus->hdev->channel, int_pkt, sizeof(*int_pkt),
  1340. 0, VM_PKT_DATA_INBAND, 0);
  1341. kfree(int_desc);
  1342. }
  1343. /**
  1344. * hv_msi_free() - Free the MSI.
  1345. * @domain: The interrupt domain pointer
  1346. * @info: Extra MSI-related context
  1347. * @irq: Identifies the IRQ.
  1348. *
  1349. * The Hyper-V parent partition and hypervisor are tracking the
  1350. * messages that are in use, keeping the interrupt redirection
  1351. * table up to date. This callback sends a message that frees
  1352. * the IRT entry and related tracking nonsense.
  1353. */
  1354. static void hv_msi_free(struct irq_domain *domain, struct msi_domain_info *info,
  1355. unsigned int irq)
  1356. {
  1357. struct hv_pcibus_device *hbus;
  1358. struct hv_pci_dev *hpdev;
  1359. struct pci_dev *pdev;
  1360. struct tran_int_desc *int_desc;
  1361. struct irq_data *irq_data = irq_domain_get_irq_data(domain, irq);
  1362. struct msi_desc *msi = irq_data_get_msi_desc(irq_data);
  1363. pdev = msi_desc_to_pci_dev(msi);
  1364. hbus = info->data;
  1365. int_desc = irq_data_get_irq_chip_data(irq_data);
  1366. if (!int_desc)
  1367. return;
  1368. irq_data->chip_data = NULL;
  1369. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  1370. if (!hpdev) {
  1371. kfree(int_desc);
  1372. return;
  1373. }
  1374. hv_int_desc_free(hpdev, int_desc);
  1375. put_pcichild(hpdev);
  1376. }
  1377. static void hv_irq_mask(struct irq_data *data)
  1378. {
  1379. pci_msi_mask_irq(data);
  1380. if (data->parent_data->chip->irq_mask)
  1381. irq_chip_mask_parent(data);
  1382. }
  1383. static void hv_irq_unmask(struct irq_data *data)
  1384. {
  1385. hv_arch_irq_unmask(data);
  1386. if (data->parent_data->chip->irq_unmask)
  1387. irq_chip_unmask_parent(data);
  1388. pci_msi_unmask_irq(data);
  1389. }
  1390. struct compose_comp_ctxt {
  1391. struct hv_pci_compl comp_pkt;
  1392. struct tran_int_desc int_desc;
  1393. };
  1394. static void hv_pci_compose_compl(void *context, struct pci_response *resp,
  1395. int resp_packet_size)
  1396. {
  1397. struct compose_comp_ctxt *comp_pkt = context;
  1398. struct pci_create_int_response *int_resp =
  1399. (struct pci_create_int_response *)resp;
  1400. if (resp_packet_size < sizeof(*int_resp)) {
  1401. comp_pkt->comp_pkt.completion_status = -1;
  1402. goto out;
  1403. }
  1404. comp_pkt->comp_pkt.completion_status = resp->status;
  1405. comp_pkt->int_desc = int_resp->int_desc;
  1406. out:
  1407. complete(&comp_pkt->comp_pkt.host_event);
  1408. }
  1409. static u32 hv_compose_msi_req_v1(
  1410. struct pci_create_interrupt *int_pkt,
  1411. u32 slot, u8 vector, u16 vector_count)
  1412. {
  1413. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE;
  1414. int_pkt->wslot.slot = slot;
  1415. int_pkt->int_desc.vector = vector;
  1416. int_pkt->int_desc.vector_count = vector_count;
  1417. int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
  1418. /*
  1419. * Create MSI w/ dummy vCPU set, overwritten by subsequent retarget in
  1420. * hv_irq_unmask().
  1421. */
  1422. int_pkt->int_desc.cpu_mask = CPU_AFFINITY_ALL;
  1423. return sizeof(*int_pkt);
  1424. }
  1425. /*
  1426. * The vCPU selected by hv_compose_multi_msi_req_get_cpu() and
  1427. * hv_compose_msi_req_get_cpu() is a "dummy" vCPU because the final vCPU to be
  1428. * interrupted is specified later in hv_irq_unmask() and communicated to Hyper-V
  1429. * via the HVCALL_RETARGET_INTERRUPT hypercall. But the choice of dummy vCPU is
  1430. * not irrelevant because Hyper-V chooses the physical CPU to handle the
  1431. * interrupts based on the vCPU specified in message sent to the vPCI VSP in
  1432. * hv_compose_msi_msg(). Hyper-V's choice of pCPU is not visible to the guest,
  1433. * but assigning too many vPCI device interrupts to the same pCPU can cause a
  1434. * performance bottleneck. So we spread out the dummy vCPUs to influence Hyper-V
  1435. * to spread out the pCPUs that it selects.
  1436. *
  1437. * For the single-MSI and MSI-X cases, it's OK for hv_compose_msi_req_get_cpu()
  1438. * to always return the same dummy vCPU, because a second call to
  1439. * hv_compose_msi_msg() contains the "real" vCPU, causing Hyper-V to choose a
  1440. * new pCPU for the interrupt. But for the multi-MSI case, the second call to
  1441. * hv_compose_msi_msg() exits without sending a message to the vPCI VSP, so the
  1442. * original dummy vCPU is used. This dummy vCPU must be round-robin'ed so that
  1443. * the pCPUs are spread out. All interrupts for a multi-MSI device end up using
  1444. * the same pCPU, even though the vCPUs will be spread out by later calls
  1445. * to hv_irq_unmask(), but that is the best we can do now.
  1446. *
  1447. * With Hyper-V in Nov 2022, the HVCALL_RETARGET_INTERRUPT hypercall does *not*
  1448. * cause Hyper-V to reselect the pCPU based on the specified vCPU. Such an
  1449. * enhancement is planned for a future version. With that enhancement, the
  1450. * dummy vCPU selection won't matter, and interrupts for the same multi-MSI
  1451. * device will be spread across multiple pCPUs.
  1452. */
  1453. /*
  1454. * Create MSI w/ dummy vCPU set targeting just one vCPU, overwritten
  1455. * by subsequent retarget in hv_irq_unmask().
  1456. */
  1457. static int hv_compose_msi_req_get_cpu(const struct cpumask *affinity)
  1458. {
  1459. return cpumask_first_and(affinity, cpu_online_mask);
  1460. }
  1461. /*
  1462. * Make sure the dummy vCPU values for multi-MSI don't all point to vCPU0.
  1463. */
  1464. static int hv_compose_multi_msi_req_get_cpu(void)
  1465. {
  1466. static DEFINE_SPINLOCK(multi_msi_cpu_lock);
  1467. /* -1 means starting with CPU 0 */
  1468. static int cpu_next = -1;
  1469. unsigned long flags;
  1470. int cpu;
  1471. spin_lock_irqsave(&multi_msi_cpu_lock, flags);
  1472. cpu_next = cpumask_next_wrap(cpu_next, cpu_online_mask, nr_cpu_ids,
  1473. false);
  1474. cpu = cpu_next;
  1475. spin_unlock_irqrestore(&multi_msi_cpu_lock, flags);
  1476. return cpu;
  1477. }
  1478. static u32 hv_compose_msi_req_v2(
  1479. struct pci_create_interrupt2 *int_pkt, int cpu,
  1480. u32 slot, u8 vector, u16 vector_count)
  1481. {
  1482. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE2;
  1483. int_pkt->wslot.slot = slot;
  1484. int_pkt->int_desc.vector = vector;
  1485. int_pkt->int_desc.vector_count = vector_count;
  1486. int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
  1487. int_pkt->int_desc.processor_array[0] =
  1488. hv_cpu_number_to_vp_number(cpu);
  1489. int_pkt->int_desc.processor_count = 1;
  1490. return sizeof(*int_pkt);
  1491. }
  1492. static u32 hv_compose_msi_req_v3(
  1493. struct pci_create_interrupt3 *int_pkt, int cpu,
  1494. u32 slot, u32 vector, u16 vector_count)
  1495. {
  1496. int_pkt->message_type.type = PCI_CREATE_INTERRUPT_MESSAGE3;
  1497. int_pkt->wslot.slot = slot;
  1498. int_pkt->int_desc.vector = vector;
  1499. int_pkt->int_desc.reserved = 0;
  1500. int_pkt->int_desc.vector_count = vector_count;
  1501. int_pkt->int_desc.delivery_mode = DELIVERY_MODE;
  1502. int_pkt->int_desc.processor_array[0] =
  1503. hv_cpu_number_to_vp_number(cpu);
  1504. int_pkt->int_desc.processor_count = 1;
  1505. return sizeof(*int_pkt);
  1506. }
  1507. /**
  1508. * hv_compose_msi_msg() - Supplies a valid MSI address/data
  1509. * @data: Everything about this MSI
  1510. * @msg: Buffer that is filled in by this function
  1511. *
  1512. * This function unpacks the IRQ looking for target CPU set, IDT
  1513. * vector and mode and sends a message to the parent partition
  1514. * asking for a mapping for that tuple in this partition. The
  1515. * response supplies a data value and address to which that data
  1516. * should be written to trigger that interrupt.
  1517. */
  1518. static void hv_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
  1519. {
  1520. struct hv_pcibus_device *hbus;
  1521. struct vmbus_channel *channel;
  1522. struct hv_pci_dev *hpdev;
  1523. struct pci_bus *pbus;
  1524. struct pci_dev *pdev;
  1525. const struct cpumask *dest;
  1526. struct compose_comp_ctxt comp;
  1527. struct tran_int_desc *int_desc;
  1528. struct msi_desc *msi_desc;
  1529. /*
  1530. * vector_count should be u16: see hv_msi_desc, hv_msi_desc2
  1531. * and hv_msi_desc3. vector must be u32: see hv_msi_desc3.
  1532. */
  1533. u16 vector_count;
  1534. u32 vector;
  1535. struct {
  1536. struct pci_packet pci_pkt;
  1537. union {
  1538. struct pci_create_interrupt v1;
  1539. struct pci_create_interrupt2 v2;
  1540. struct pci_create_interrupt3 v3;
  1541. } int_pkts;
  1542. } __packed ctxt;
  1543. bool multi_msi;
  1544. u64 trans_id;
  1545. u32 size;
  1546. int ret;
  1547. int cpu;
  1548. msi_desc = irq_data_get_msi_desc(data);
  1549. multi_msi = !msi_desc->pci.msi_attrib.is_msix &&
  1550. msi_desc->nvec_used > 1;
  1551. /* Reuse the previous allocation */
  1552. if (data->chip_data && multi_msi) {
  1553. int_desc = data->chip_data;
  1554. msg->address_hi = int_desc->address >> 32;
  1555. msg->address_lo = int_desc->address & 0xffffffff;
  1556. msg->data = int_desc->data;
  1557. return;
  1558. }
  1559. pdev = msi_desc_to_pci_dev(msi_desc);
  1560. dest = irq_data_get_effective_affinity_mask(data);
  1561. pbus = pdev->bus;
  1562. hbus = container_of(pbus->sysdata, struct hv_pcibus_device, sysdata);
  1563. channel = hbus->hdev->channel;
  1564. hpdev = get_pcichild_wslot(hbus, devfn_to_wslot(pdev->devfn));
  1565. if (!hpdev)
  1566. goto return_null_message;
  1567. /* Free any previous message that might have already been composed. */
  1568. if (data->chip_data && !multi_msi) {
  1569. int_desc = data->chip_data;
  1570. data->chip_data = NULL;
  1571. hv_int_desc_free(hpdev, int_desc);
  1572. }
  1573. int_desc = kzalloc(sizeof(*int_desc), GFP_ATOMIC);
  1574. if (!int_desc)
  1575. goto drop_reference;
  1576. if (multi_msi) {
  1577. /*
  1578. * If this is not the first MSI of Multi MSI, we already have
  1579. * a mapping. Can exit early.
  1580. */
  1581. if (msi_desc->irq != data->irq) {
  1582. data->chip_data = int_desc;
  1583. int_desc->address = msi_desc->msg.address_lo |
  1584. (u64)msi_desc->msg.address_hi << 32;
  1585. int_desc->data = msi_desc->msg.data +
  1586. (data->irq - msi_desc->irq);
  1587. msg->address_hi = msi_desc->msg.address_hi;
  1588. msg->address_lo = msi_desc->msg.address_lo;
  1589. msg->data = int_desc->data;
  1590. put_pcichild(hpdev);
  1591. return;
  1592. }
  1593. /*
  1594. * The vector we select here is a dummy value. The correct
  1595. * value gets sent to the hypervisor in unmask(). This needs
  1596. * to be aligned with the count, and also not zero. Multi-msi
  1597. * is powers of 2 up to 32, so 32 will always work here.
  1598. */
  1599. vector = 32;
  1600. vector_count = msi_desc->nvec_used;
  1601. cpu = hv_compose_multi_msi_req_get_cpu();
  1602. } else {
  1603. vector = hv_msi_get_int_vector(data);
  1604. vector_count = 1;
  1605. cpu = hv_compose_msi_req_get_cpu(dest);
  1606. }
  1607. /*
  1608. * hv_compose_msi_req_v1 and v2 are for x86 only, meaning 'vector'
  1609. * can't exceed u8. Cast 'vector' down to u8 for v1/v2 explicitly
  1610. * for better readability.
  1611. */
  1612. memset(&ctxt, 0, sizeof(ctxt));
  1613. init_completion(&comp.comp_pkt.host_event);
  1614. ctxt.pci_pkt.completion_func = hv_pci_compose_compl;
  1615. ctxt.pci_pkt.compl_ctxt = &comp;
  1616. switch (hbus->protocol_version) {
  1617. case PCI_PROTOCOL_VERSION_1_1:
  1618. size = hv_compose_msi_req_v1(&ctxt.int_pkts.v1,
  1619. hpdev->desc.win_slot.slot,
  1620. (u8)vector,
  1621. vector_count);
  1622. break;
  1623. case PCI_PROTOCOL_VERSION_1_2:
  1624. case PCI_PROTOCOL_VERSION_1_3:
  1625. size = hv_compose_msi_req_v2(&ctxt.int_pkts.v2,
  1626. cpu,
  1627. hpdev->desc.win_slot.slot,
  1628. (u8)vector,
  1629. vector_count);
  1630. break;
  1631. case PCI_PROTOCOL_VERSION_1_4:
  1632. size = hv_compose_msi_req_v3(&ctxt.int_pkts.v3,
  1633. cpu,
  1634. hpdev->desc.win_slot.slot,
  1635. vector,
  1636. vector_count);
  1637. break;
  1638. default:
  1639. /* As we only negotiate protocol versions known to this driver,
  1640. * this path should never hit. However, this is it not a hot
  1641. * path so we print a message to aid future updates.
  1642. */
  1643. dev_err(&hbus->hdev->device,
  1644. "Unexpected vPCI protocol, update driver.");
  1645. goto free_int_desc;
  1646. }
  1647. ret = vmbus_sendpacket_getid(hpdev->hbus->hdev->channel, &ctxt.int_pkts,
  1648. size, (unsigned long)&ctxt.pci_pkt,
  1649. &trans_id, VM_PKT_DATA_INBAND,
  1650. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  1651. if (ret) {
  1652. dev_err(&hbus->hdev->device,
  1653. "Sending request for interrupt failed: 0x%x",
  1654. comp.comp_pkt.completion_status);
  1655. goto free_int_desc;
  1656. }
  1657. /*
  1658. * Prevents hv_pci_onchannelcallback() from running concurrently
  1659. * in the tasklet.
  1660. */
  1661. tasklet_disable_in_atomic(&channel->callback_event);
  1662. /*
  1663. * Since this function is called with IRQ locks held, can't
  1664. * do normal wait for completion; instead poll.
  1665. */
  1666. while (!try_wait_for_completion(&comp.comp_pkt.host_event)) {
  1667. unsigned long flags;
  1668. /* 0xFFFF means an invalid PCI VENDOR ID. */
  1669. if (hv_pcifront_get_vendor_id(hpdev) == 0xFFFF) {
  1670. dev_err_once(&hbus->hdev->device,
  1671. "the device has gone\n");
  1672. goto enable_tasklet;
  1673. }
  1674. /*
  1675. * Make sure that the ring buffer data structure doesn't get
  1676. * freed while we dereference the ring buffer pointer. Test
  1677. * for the channel's onchannel_callback being NULL within a
  1678. * sched_lock critical section. See also the inline comments
  1679. * in vmbus_reset_channel_cb().
  1680. */
  1681. spin_lock_irqsave(&channel->sched_lock, flags);
  1682. if (unlikely(channel->onchannel_callback == NULL)) {
  1683. spin_unlock_irqrestore(&channel->sched_lock, flags);
  1684. goto enable_tasklet;
  1685. }
  1686. hv_pci_onchannelcallback(hbus);
  1687. spin_unlock_irqrestore(&channel->sched_lock, flags);
  1688. udelay(100);
  1689. }
  1690. tasklet_enable(&channel->callback_event);
  1691. if (comp.comp_pkt.completion_status < 0) {
  1692. dev_err(&hbus->hdev->device,
  1693. "Request for interrupt failed: 0x%x",
  1694. comp.comp_pkt.completion_status);
  1695. goto free_int_desc;
  1696. }
  1697. /*
  1698. * Record the assignment so that this can be unwound later. Using
  1699. * irq_set_chip_data() here would be appropriate, but the lock it takes
  1700. * is already held.
  1701. */
  1702. *int_desc = comp.int_desc;
  1703. data->chip_data = int_desc;
  1704. /* Pass up the result. */
  1705. msg->address_hi = comp.int_desc.address >> 32;
  1706. msg->address_lo = comp.int_desc.address & 0xffffffff;
  1707. msg->data = comp.int_desc.data;
  1708. put_pcichild(hpdev);
  1709. return;
  1710. enable_tasklet:
  1711. tasklet_enable(&channel->callback_event);
  1712. /*
  1713. * The completion packet on the stack becomes invalid after 'return';
  1714. * remove the ID from the VMbus requestor if the identifier is still
  1715. * mapped to/associated with the packet. (The identifier could have
  1716. * been 're-used', i.e., already removed and (re-)mapped.)
  1717. *
  1718. * Cf. hv_pci_onchannelcallback().
  1719. */
  1720. vmbus_request_addr_match(channel, trans_id, (unsigned long)&ctxt.pci_pkt);
  1721. free_int_desc:
  1722. kfree(int_desc);
  1723. drop_reference:
  1724. put_pcichild(hpdev);
  1725. return_null_message:
  1726. msg->address_hi = 0;
  1727. msg->address_lo = 0;
  1728. msg->data = 0;
  1729. }
  1730. /* HW Interrupt Chip Descriptor */
  1731. static struct irq_chip hv_msi_irq_chip = {
  1732. .name = "Hyper-V PCIe MSI",
  1733. .irq_compose_msi_msg = hv_compose_msi_msg,
  1734. .irq_set_affinity = irq_chip_set_affinity_parent,
  1735. #ifdef CONFIG_X86
  1736. .irq_ack = irq_chip_ack_parent,
  1737. #elif defined(CONFIG_ARM64)
  1738. .irq_eoi = irq_chip_eoi_parent,
  1739. #endif
  1740. .irq_mask = hv_irq_mask,
  1741. .irq_unmask = hv_irq_unmask,
  1742. };
  1743. static struct msi_domain_ops hv_msi_ops = {
  1744. .msi_prepare = hv_msi_prepare,
  1745. .msi_free = hv_msi_free,
  1746. };
  1747. /**
  1748. * hv_pcie_init_irq_domain() - Initialize IRQ domain
  1749. * @hbus: The root PCI bus
  1750. *
  1751. * This function creates an IRQ domain which will be used for
  1752. * interrupts from devices that have been passed through. These
  1753. * devices only support MSI and MSI-X, not line-based interrupts
  1754. * or simulations of line-based interrupts through PCIe's
  1755. * fabric-layer messages. Because interrupts are remapped, we
  1756. * can support multi-message MSI here.
  1757. *
  1758. * Return: '0' on success and error value on failure
  1759. */
  1760. static int hv_pcie_init_irq_domain(struct hv_pcibus_device *hbus)
  1761. {
  1762. hbus->msi_info.chip = &hv_msi_irq_chip;
  1763. hbus->msi_info.ops = &hv_msi_ops;
  1764. hbus->msi_info.flags = (MSI_FLAG_USE_DEF_DOM_OPS |
  1765. MSI_FLAG_USE_DEF_CHIP_OPS | MSI_FLAG_MULTI_PCI_MSI |
  1766. MSI_FLAG_PCI_MSIX);
  1767. hbus->msi_info.handler = FLOW_HANDLER;
  1768. hbus->msi_info.handler_name = FLOW_NAME;
  1769. hbus->msi_info.data = hbus;
  1770. hbus->irq_domain = pci_msi_create_irq_domain(hbus->fwnode,
  1771. &hbus->msi_info,
  1772. hv_pci_get_root_domain());
  1773. if (!hbus->irq_domain) {
  1774. dev_err(&hbus->hdev->device,
  1775. "Failed to build an MSI IRQ domain\n");
  1776. return -ENODEV;
  1777. }
  1778. dev_set_msi_domain(&hbus->bridge->dev, hbus->irq_domain);
  1779. return 0;
  1780. }
  1781. /**
  1782. * get_bar_size() - Get the address space consumed by a BAR
  1783. * @bar_val: Value that a BAR returned after -1 was written
  1784. * to it.
  1785. *
  1786. * This function returns the size of the BAR, rounded up to 1
  1787. * page. It has to be rounded up because the hypervisor's page
  1788. * table entry that maps the BAR into the VM can't specify an
  1789. * offset within a page. The invariant is that the hypervisor
  1790. * must place any BARs of smaller than page length at the
  1791. * beginning of a page.
  1792. *
  1793. * Return: Size in bytes of the consumed MMIO space.
  1794. */
  1795. static u64 get_bar_size(u64 bar_val)
  1796. {
  1797. return round_up((1 + ~(bar_val & PCI_BASE_ADDRESS_MEM_MASK)),
  1798. PAGE_SIZE);
  1799. }
  1800. /**
  1801. * survey_child_resources() - Total all MMIO requirements
  1802. * @hbus: Root PCI bus, as understood by this driver
  1803. */
  1804. static void survey_child_resources(struct hv_pcibus_device *hbus)
  1805. {
  1806. struct hv_pci_dev *hpdev;
  1807. resource_size_t bar_size = 0;
  1808. unsigned long flags;
  1809. struct completion *event;
  1810. u64 bar_val;
  1811. int i;
  1812. /* If nobody is waiting on the answer, don't compute it. */
  1813. event = xchg(&hbus->survey_event, NULL);
  1814. if (!event)
  1815. return;
  1816. /* If the answer has already been computed, go with it. */
  1817. if (hbus->low_mmio_space || hbus->high_mmio_space) {
  1818. complete(event);
  1819. return;
  1820. }
  1821. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1822. /*
  1823. * Due to an interesting quirk of the PCI spec, all memory regions
  1824. * for a child device are a power of 2 in size and aligned in memory,
  1825. * so it's sufficient to just add them up without tracking alignment.
  1826. */
  1827. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  1828. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  1829. if (hpdev->probed_bar[i] & PCI_BASE_ADDRESS_SPACE_IO)
  1830. dev_err(&hbus->hdev->device,
  1831. "There's an I/O BAR in this list!\n");
  1832. if (hpdev->probed_bar[i] != 0) {
  1833. /*
  1834. * A probed BAR has all the upper bits set that
  1835. * can be changed.
  1836. */
  1837. bar_val = hpdev->probed_bar[i];
  1838. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  1839. bar_val |=
  1840. ((u64)hpdev->probed_bar[++i] << 32);
  1841. else
  1842. bar_val |= 0xffffffff00000000ULL;
  1843. bar_size = get_bar_size(bar_val);
  1844. if (bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64)
  1845. hbus->high_mmio_space += bar_size;
  1846. else
  1847. hbus->low_mmio_space += bar_size;
  1848. }
  1849. }
  1850. }
  1851. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1852. complete(event);
  1853. }
  1854. /**
  1855. * prepopulate_bars() - Fill in BARs with defaults
  1856. * @hbus: Root PCI bus, as understood by this driver
  1857. *
  1858. * The core PCI driver code seems much, much happier if the BARs
  1859. * for a device have values upon first scan. So fill them in.
  1860. * The algorithm below works down from large sizes to small,
  1861. * attempting to pack the assignments optimally. The assumption,
  1862. * enforced in other parts of the code, is that the beginning of
  1863. * the memory-mapped I/O space will be aligned on the largest
  1864. * BAR size.
  1865. */
  1866. static void prepopulate_bars(struct hv_pcibus_device *hbus)
  1867. {
  1868. resource_size_t high_size = 0;
  1869. resource_size_t low_size = 0;
  1870. resource_size_t high_base = 0;
  1871. resource_size_t low_base = 0;
  1872. resource_size_t bar_size;
  1873. struct hv_pci_dev *hpdev;
  1874. unsigned long flags;
  1875. u64 bar_val;
  1876. u32 command;
  1877. bool high;
  1878. int i;
  1879. if (hbus->low_mmio_space) {
  1880. low_size = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  1881. low_base = hbus->low_mmio_res->start;
  1882. }
  1883. if (hbus->high_mmio_space) {
  1884. high_size = 1ULL <<
  1885. (63 - __builtin_clzll(hbus->high_mmio_space));
  1886. high_base = hbus->high_mmio_res->start;
  1887. }
  1888. spin_lock_irqsave(&hbus->device_list_lock, flags);
  1889. /*
  1890. * Clear the memory enable bit, in case it's already set. This occurs
  1891. * in the suspend path of hibernation, where the device is suspended,
  1892. * resumed and suspended again: see hibernation_snapshot() and
  1893. * hibernation_platform_enter().
  1894. *
  1895. * If the memory enable bit is already set, Hyper-V silently ignores
  1896. * the below BAR updates, and the related PCI device driver can not
  1897. * work, because reading from the device register(s) always returns
  1898. * 0xFFFFFFFF (PCI_ERROR_RESPONSE).
  1899. */
  1900. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  1901. _hv_pcifront_read_config(hpdev, PCI_COMMAND, 2, &command);
  1902. command &= ~PCI_COMMAND_MEMORY;
  1903. _hv_pcifront_write_config(hpdev, PCI_COMMAND, 2, command);
  1904. }
  1905. /* Pick addresses for the BARs. */
  1906. do {
  1907. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  1908. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  1909. bar_val = hpdev->probed_bar[i];
  1910. if (bar_val == 0)
  1911. continue;
  1912. high = bar_val & PCI_BASE_ADDRESS_MEM_TYPE_64;
  1913. if (high) {
  1914. bar_val |=
  1915. ((u64)hpdev->probed_bar[i + 1]
  1916. << 32);
  1917. } else {
  1918. bar_val |= 0xffffffffULL << 32;
  1919. }
  1920. bar_size = get_bar_size(bar_val);
  1921. if (high) {
  1922. if (high_size != bar_size) {
  1923. i++;
  1924. continue;
  1925. }
  1926. _hv_pcifront_write_config(hpdev,
  1927. PCI_BASE_ADDRESS_0 + (4 * i),
  1928. 4,
  1929. (u32)(high_base & 0xffffff00));
  1930. i++;
  1931. _hv_pcifront_write_config(hpdev,
  1932. PCI_BASE_ADDRESS_0 + (4 * i),
  1933. 4, (u32)(high_base >> 32));
  1934. high_base += bar_size;
  1935. } else {
  1936. if (low_size != bar_size)
  1937. continue;
  1938. _hv_pcifront_write_config(hpdev,
  1939. PCI_BASE_ADDRESS_0 + (4 * i),
  1940. 4,
  1941. (u32)(low_base & 0xffffff00));
  1942. low_base += bar_size;
  1943. }
  1944. }
  1945. if (high_size <= 1 && low_size <= 1) {
  1946. /*
  1947. * No need to set the PCI_COMMAND_MEMORY bit as
  1948. * the core PCI driver doesn't require the bit
  1949. * to be pre-set. Actually here we intentionally
  1950. * keep the bit off so that the PCI BAR probing
  1951. * in the core PCI driver doesn't cause Hyper-V
  1952. * to unnecessarily unmap/map the virtual BARs
  1953. * from/to the physical BARs multiple times.
  1954. * This reduces the VM boot time significantly
  1955. * if the BAR sizes are huge.
  1956. */
  1957. break;
  1958. }
  1959. }
  1960. high_size >>= 1;
  1961. low_size >>= 1;
  1962. } while (high_size || low_size);
  1963. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  1964. }
  1965. /*
  1966. * Assign entries in sysfs pci slot directory.
  1967. *
  1968. * Note that this function does not need to lock the children list
  1969. * because it is called from pci_devices_present_work which
  1970. * is serialized with hv_eject_device_work because they are on the
  1971. * same ordered workqueue. Therefore hbus->children list will not change
  1972. * even when pci_create_slot sleeps.
  1973. */
  1974. static void hv_pci_assign_slots(struct hv_pcibus_device *hbus)
  1975. {
  1976. struct hv_pci_dev *hpdev;
  1977. char name[SLOT_NAME_SIZE];
  1978. int slot_nr;
  1979. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  1980. if (hpdev->pci_slot)
  1981. continue;
  1982. slot_nr = PCI_SLOT(wslot_to_devfn(hpdev->desc.win_slot.slot));
  1983. snprintf(name, SLOT_NAME_SIZE, "%u", hpdev->desc.ser);
  1984. hpdev->pci_slot = pci_create_slot(hbus->bridge->bus, slot_nr,
  1985. name, NULL);
  1986. if (IS_ERR(hpdev->pci_slot)) {
  1987. pr_warn("pci_create slot %s failed\n", name);
  1988. hpdev->pci_slot = NULL;
  1989. }
  1990. }
  1991. }
  1992. /*
  1993. * Remove entries in sysfs pci slot directory.
  1994. */
  1995. static void hv_pci_remove_slots(struct hv_pcibus_device *hbus)
  1996. {
  1997. struct hv_pci_dev *hpdev;
  1998. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  1999. if (!hpdev->pci_slot)
  2000. continue;
  2001. pci_destroy_slot(hpdev->pci_slot);
  2002. hpdev->pci_slot = NULL;
  2003. }
  2004. }
  2005. /*
  2006. * Set NUMA node for the devices on the bus
  2007. */
  2008. static void hv_pci_assign_numa_node(struct hv_pcibus_device *hbus)
  2009. {
  2010. struct pci_dev *dev;
  2011. struct pci_bus *bus = hbus->bridge->bus;
  2012. struct hv_pci_dev *hv_dev;
  2013. list_for_each_entry(dev, &bus->devices, bus_list) {
  2014. hv_dev = get_pcichild_wslot(hbus, devfn_to_wslot(dev->devfn));
  2015. if (!hv_dev)
  2016. continue;
  2017. if (hv_dev->desc.flags & HV_PCI_DEVICE_FLAG_NUMA_AFFINITY &&
  2018. hv_dev->desc.virtual_numa_node < num_possible_nodes())
  2019. /*
  2020. * The kernel may boot with some NUMA nodes offline
  2021. * (e.g. in a KDUMP kernel) or with NUMA disabled via
  2022. * "numa=off". In those cases, adjust the host provided
  2023. * NUMA node to a valid NUMA node used by the kernel.
  2024. */
  2025. set_dev_node(&dev->dev,
  2026. numa_map_to_online_node(
  2027. hv_dev->desc.virtual_numa_node));
  2028. put_pcichild(hv_dev);
  2029. }
  2030. }
  2031. /**
  2032. * create_root_hv_pci_bus() - Expose a new root PCI bus
  2033. * @hbus: Root PCI bus, as understood by this driver
  2034. *
  2035. * Return: 0 on success, -errno on failure
  2036. */
  2037. static int create_root_hv_pci_bus(struct hv_pcibus_device *hbus)
  2038. {
  2039. int error;
  2040. struct pci_host_bridge *bridge = hbus->bridge;
  2041. bridge->dev.parent = &hbus->hdev->device;
  2042. bridge->sysdata = &hbus->sysdata;
  2043. bridge->ops = &hv_pcifront_ops;
  2044. error = pci_scan_root_bus_bridge(bridge);
  2045. if (error)
  2046. return error;
  2047. pci_lock_rescan_remove();
  2048. hv_pci_assign_numa_node(hbus);
  2049. pci_bus_assign_resources(bridge->bus);
  2050. hv_pci_assign_slots(hbus);
  2051. pci_bus_add_devices(bridge->bus);
  2052. pci_unlock_rescan_remove();
  2053. hbus->state = hv_pcibus_installed;
  2054. return 0;
  2055. }
  2056. struct q_res_req_compl {
  2057. struct completion host_event;
  2058. struct hv_pci_dev *hpdev;
  2059. };
  2060. /**
  2061. * q_resource_requirements() - Query Resource Requirements
  2062. * @context: The completion context.
  2063. * @resp: The response that came from the host.
  2064. * @resp_packet_size: The size in bytes of resp.
  2065. *
  2066. * This function is invoked on completion of a Query Resource
  2067. * Requirements packet.
  2068. */
  2069. static void q_resource_requirements(void *context, struct pci_response *resp,
  2070. int resp_packet_size)
  2071. {
  2072. struct q_res_req_compl *completion = context;
  2073. struct pci_q_res_req_response *q_res_req =
  2074. (struct pci_q_res_req_response *)resp;
  2075. s32 status;
  2076. int i;
  2077. status = (resp_packet_size < sizeof(*q_res_req)) ? -1 : resp->status;
  2078. if (status < 0) {
  2079. dev_err(&completion->hpdev->hbus->hdev->device,
  2080. "query resource requirements failed: %x\n",
  2081. status);
  2082. } else {
  2083. for (i = 0; i < PCI_STD_NUM_BARS; i++) {
  2084. completion->hpdev->probed_bar[i] =
  2085. q_res_req->probed_bar[i];
  2086. }
  2087. }
  2088. complete(&completion->host_event);
  2089. }
  2090. /**
  2091. * new_pcichild_device() - Create a new child device
  2092. * @hbus: The internal struct tracking this root PCI bus.
  2093. * @desc: The information supplied so far from the host
  2094. * about the device.
  2095. *
  2096. * This function creates the tracking structure for a new child
  2097. * device and kicks off the process of figuring out what it is.
  2098. *
  2099. * Return: Pointer to the new tracking struct
  2100. */
  2101. static struct hv_pci_dev *new_pcichild_device(struct hv_pcibus_device *hbus,
  2102. struct hv_pcidev_description *desc)
  2103. {
  2104. struct hv_pci_dev *hpdev;
  2105. struct pci_child_message *res_req;
  2106. struct q_res_req_compl comp_pkt;
  2107. struct {
  2108. struct pci_packet init_packet;
  2109. u8 buffer[sizeof(struct pci_child_message)];
  2110. } pkt;
  2111. unsigned long flags;
  2112. int ret;
  2113. hpdev = kzalloc(sizeof(*hpdev), GFP_KERNEL);
  2114. if (!hpdev)
  2115. return NULL;
  2116. hpdev->hbus = hbus;
  2117. memset(&pkt, 0, sizeof(pkt));
  2118. init_completion(&comp_pkt.host_event);
  2119. comp_pkt.hpdev = hpdev;
  2120. pkt.init_packet.compl_ctxt = &comp_pkt;
  2121. pkt.init_packet.completion_func = q_resource_requirements;
  2122. res_req = (struct pci_child_message *)&pkt.init_packet.message;
  2123. res_req->message_type.type = PCI_QUERY_RESOURCE_REQUIREMENTS;
  2124. res_req->wslot.slot = desc->win_slot.slot;
  2125. ret = vmbus_sendpacket(hbus->hdev->channel, res_req,
  2126. sizeof(struct pci_child_message),
  2127. (unsigned long)&pkt.init_packet,
  2128. VM_PKT_DATA_INBAND,
  2129. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2130. if (ret)
  2131. goto error;
  2132. if (wait_for_response(hbus->hdev, &comp_pkt.host_event))
  2133. goto error;
  2134. hpdev->desc = *desc;
  2135. refcount_set(&hpdev->refs, 1);
  2136. get_pcichild(hpdev);
  2137. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2138. list_add_tail(&hpdev->list_entry, &hbus->children);
  2139. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2140. return hpdev;
  2141. error:
  2142. kfree(hpdev);
  2143. return NULL;
  2144. }
  2145. /**
  2146. * get_pcichild_wslot() - Find device from slot
  2147. * @hbus: Root PCI bus, as understood by this driver
  2148. * @wslot: Location on the bus
  2149. *
  2150. * This function looks up a PCI device and returns the internal
  2151. * representation of it. It acquires a reference on it, so that
  2152. * the device won't be deleted while somebody is using it. The
  2153. * caller is responsible for calling put_pcichild() to release
  2154. * this reference.
  2155. *
  2156. * Return: Internal representation of a PCI device
  2157. */
  2158. static struct hv_pci_dev *get_pcichild_wslot(struct hv_pcibus_device *hbus,
  2159. u32 wslot)
  2160. {
  2161. unsigned long flags;
  2162. struct hv_pci_dev *iter, *hpdev = NULL;
  2163. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2164. list_for_each_entry(iter, &hbus->children, list_entry) {
  2165. if (iter->desc.win_slot.slot == wslot) {
  2166. hpdev = iter;
  2167. get_pcichild(hpdev);
  2168. break;
  2169. }
  2170. }
  2171. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2172. return hpdev;
  2173. }
  2174. /**
  2175. * pci_devices_present_work() - Handle new list of child devices
  2176. * @work: Work struct embedded in struct hv_dr_work
  2177. *
  2178. * "Bus Relations" is the Windows term for "children of this
  2179. * bus." The terminology is preserved here for people trying to
  2180. * debug the interaction between Hyper-V and Linux. This
  2181. * function is called when the parent partition reports a list
  2182. * of functions that should be observed under this PCI Express
  2183. * port (bus).
  2184. *
  2185. * This function updates the list, and must tolerate being
  2186. * called multiple times with the same information. The typical
  2187. * number of child devices is one, with very atypical cases
  2188. * involving three or four, so the algorithms used here can be
  2189. * simple and inefficient.
  2190. *
  2191. * It must also treat the omission of a previously observed device as
  2192. * notification that the device no longer exists.
  2193. *
  2194. * Note that this function is serialized with hv_eject_device_work(),
  2195. * because both are pushed to the ordered workqueue hbus->wq.
  2196. */
  2197. static void pci_devices_present_work(struct work_struct *work)
  2198. {
  2199. u32 child_no;
  2200. bool found;
  2201. struct hv_pcidev_description *new_desc;
  2202. struct hv_pci_dev *hpdev;
  2203. struct hv_pcibus_device *hbus;
  2204. struct list_head removed;
  2205. struct hv_dr_work *dr_wrk;
  2206. struct hv_dr_state *dr = NULL;
  2207. unsigned long flags;
  2208. dr_wrk = container_of(work, struct hv_dr_work, wrk);
  2209. hbus = dr_wrk->bus;
  2210. kfree(dr_wrk);
  2211. INIT_LIST_HEAD(&removed);
  2212. /* Pull this off the queue and process it if it was the last one. */
  2213. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2214. while (!list_empty(&hbus->dr_list)) {
  2215. dr = list_first_entry(&hbus->dr_list, struct hv_dr_state,
  2216. list_entry);
  2217. list_del(&dr->list_entry);
  2218. /* Throw this away if the list still has stuff in it. */
  2219. if (!list_empty(&hbus->dr_list)) {
  2220. kfree(dr);
  2221. continue;
  2222. }
  2223. }
  2224. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2225. if (!dr)
  2226. return;
  2227. mutex_lock(&hbus->state_lock);
  2228. /* First, mark all existing children as reported missing. */
  2229. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2230. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2231. hpdev->reported_missing = true;
  2232. }
  2233. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2234. /* Next, add back any reported devices. */
  2235. for (child_no = 0; child_no < dr->device_count; child_no++) {
  2236. found = false;
  2237. new_desc = &dr->func[child_no];
  2238. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2239. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2240. if ((hpdev->desc.win_slot.slot == new_desc->win_slot.slot) &&
  2241. (hpdev->desc.v_id == new_desc->v_id) &&
  2242. (hpdev->desc.d_id == new_desc->d_id) &&
  2243. (hpdev->desc.ser == new_desc->ser)) {
  2244. hpdev->reported_missing = false;
  2245. found = true;
  2246. }
  2247. }
  2248. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2249. if (!found) {
  2250. hpdev = new_pcichild_device(hbus, new_desc);
  2251. if (!hpdev)
  2252. dev_err(&hbus->hdev->device,
  2253. "couldn't record a child device.\n");
  2254. }
  2255. }
  2256. /* Move missing children to a list on the stack. */
  2257. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2258. do {
  2259. found = false;
  2260. list_for_each_entry(hpdev, &hbus->children, list_entry) {
  2261. if (hpdev->reported_missing) {
  2262. found = true;
  2263. put_pcichild(hpdev);
  2264. list_move_tail(&hpdev->list_entry, &removed);
  2265. break;
  2266. }
  2267. }
  2268. } while (found);
  2269. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2270. /* Delete everything that should no longer exist. */
  2271. while (!list_empty(&removed)) {
  2272. hpdev = list_first_entry(&removed, struct hv_pci_dev,
  2273. list_entry);
  2274. list_del(&hpdev->list_entry);
  2275. if (hpdev->pci_slot)
  2276. pci_destroy_slot(hpdev->pci_slot);
  2277. put_pcichild(hpdev);
  2278. }
  2279. switch (hbus->state) {
  2280. case hv_pcibus_installed:
  2281. /*
  2282. * Tell the core to rescan bus
  2283. * because there may have been changes.
  2284. */
  2285. pci_lock_rescan_remove();
  2286. pci_scan_child_bus(hbus->bridge->bus);
  2287. hv_pci_assign_numa_node(hbus);
  2288. hv_pci_assign_slots(hbus);
  2289. pci_unlock_rescan_remove();
  2290. break;
  2291. case hv_pcibus_init:
  2292. case hv_pcibus_probed:
  2293. survey_child_resources(hbus);
  2294. break;
  2295. default:
  2296. break;
  2297. }
  2298. mutex_unlock(&hbus->state_lock);
  2299. kfree(dr);
  2300. }
  2301. /**
  2302. * hv_pci_start_relations_work() - Queue work to start device discovery
  2303. * @hbus: Root PCI bus, as understood by this driver
  2304. * @dr: The list of children returned from host
  2305. *
  2306. * Return: 0 on success, -errno on failure
  2307. */
  2308. static int hv_pci_start_relations_work(struct hv_pcibus_device *hbus,
  2309. struct hv_dr_state *dr)
  2310. {
  2311. struct hv_dr_work *dr_wrk;
  2312. unsigned long flags;
  2313. bool pending_dr;
  2314. if (hbus->state == hv_pcibus_removing) {
  2315. dev_info(&hbus->hdev->device,
  2316. "PCI VMBus BUS_RELATIONS: ignored\n");
  2317. return -ENOENT;
  2318. }
  2319. dr_wrk = kzalloc(sizeof(*dr_wrk), GFP_NOWAIT);
  2320. if (!dr_wrk)
  2321. return -ENOMEM;
  2322. INIT_WORK(&dr_wrk->wrk, pci_devices_present_work);
  2323. dr_wrk->bus = hbus;
  2324. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2325. /*
  2326. * If pending_dr is true, we have already queued a work,
  2327. * which will see the new dr. Otherwise, we need to
  2328. * queue a new work.
  2329. */
  2330. pending_dr = !list_empty(&hbus->dr_list);
  2331. list_add_tail(&dr->list_entry, &hbus->dr_list);
  2332. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2333. if (pending_dr)
  2334. kfree(dr_wrk);
  2335. else
  2336. queue_work(hbus->wq, &dr_wrk->wrk);
  2337. return 0;
  2338. }
  2339. /**
  2340. * hv_pci_devices_present() - Handle list of new children
  2341. * @hbus: Root PCI bus, as understood by this driver
  2342. * @relations: Packet from host listing children
  2343. *
  2344. * Process a new list of devices on the bus. The list of devices is
  2345. * discovered by VSP and sent to us via VSP message PCI_BUS_RELATIONS,
  2346. * whenever a new list of devices for this bus appears.
  2347. */
  2348. static void hv_pci_devices_present(struct hv_pcibus_device *hbus,
  2349. struct pci_bus_relations *relations)
  2350. {
  2351. struct hv_dr_state *dr;
  2352. int i;
  2353. dr = kzalloc(struct_size(dr, func, relations->device_count),
  2354. GFP_NOWAIT);
  2355. if (!dr)
  2356. return;
  2357. dr->device_count = relations->device_count;
  2358. for (i = 0; i < dr->device_count; i++) {
  2359. dr->func[i].v_id = relations->func[i].v_id;
  2360. dr->func[i].d_id = relations->func[i].d_id;
  2361. dr->func[i].rev = relations->func[i].rev;
  2362. dr->func[i].prog_intf = relations->func[i].prog_intf;
  2363. dr->func[i].subclass = relations->func[i].subclass;
  2364. dr->func[i].base_class = relations->func[i].base_class;
  2365. dr->func[i].subsystem_id = relations->func[i].subsystem_id;
  2366. dr->func[i].win_slot = relations->func[i].win_slot;
  2367. dr->func[i].ser = relations->func[i].ser;
  2368. }
  2369. if (hv_pci_start_relations_work(hbus, dr))
  2370. kfree(dr);
  2371. }
  2372. /**
  2373. * hv_pci_devices_present2() - Handle list of new children
  2374. * @hbus: Root PCI bus, as understood by this driver
  2375. * @relations: Packet from host listing children
  2376. *
  2377. * This function is the v2 version of hv_pci_devices_present()
  2378. */
  2379. static void hv_pci_devices_present2(struct hv_pcibus_device *hbus,
  2380. struct pci_bus_relations2 *relations)
  2381. {
  2382. struct hv_dr_state *dr;
  2383. int i;
  2384. dr = kzalloc(struct_size(dr, func, relations->device_count),
  2385. GFP_NOWAIT);
  2386. if (!dr)
  2387. return;
  2388. dr->device_count = relations->device_count;
  2389. for (i = 0; i < dr->device_count; i++) {
  2390. dr->func[i].v_id = relations->func[i].v_id;
  2391. dr->func[i].d_id = relations->func[i].d_id;
  2392. dr->func[i].rev = relations->func[i].rev;
  2393. dr->func[i].prog_intf = relations->func[i].prog_intf;
  2394. dr->func[i].subclass = relations->func[i].subclass;
  2395. dr->func[i].base_class = relations->func[i].base_class;
  2396. dr->func[i].subsystem_id = relations->func[i].subsystem_id;
  2397. dr->func[i].win_slot = relations->func[i].win_slot;
  2398. dr->func[i].ser = relations->func[i].ser;
  2399. dr->func[i].flags = relations->func[i].flags;
  2400. dr->func[i].virtual_numa_node =
  2401. relations->func[i].virtual_numa_node;
  2402. }
  2403. if (hv_pci_start_relations_work(hbus, dr))
  2404. kfree(dr);
  2405. }
  2406. /**
  2407. * hv_eject_device_work() - Asynchronously handles ejection
  2408. * @work: Work struct embedded in internal device struct
  2409. *
  2410. * This function handles ejecting a device. Windows will
  2411. * attempt to gracefully eject a device, waiting 60 seconds to
  2412. * hear back from the guest OS that this completed successfully.
  2413. * If this timer expires, the device will be forcibly removed.
  2414. */
  2415. static void hv_eject_device_work(struct work_struct *work)
  2416. {
  2417. struct pci_eject_response *ejct_pkt;
  2418. struct hv_pcibus_device *hbus;
  2419. struct hv_pci_dev *hpdev;
  2420. struct pci_dev *pdev;
  2421. unsigned long flags;
  2422. int wslot;
  2423. struct {
  2424. struct pci_packet pkt;
  2425. u8 buffer[sizeof(struct pci_eject_response)];
  2426. } ctxt;
  2427. hpdev = container_of(work, struct hv_pci_dev, wrk);
  2428. hbus = hpdev->hbus;
  2429. mutex_lock(&hbus->state_lock);
  2430. /*
  2431. * Ejection can come before or after the PCI bus has been set up, so
  2432. * attempt to find it and tear down the bus state, if it exists. This
  2433. * must be done without constructs like pci_domain_nr(hbus->bridge->bus)
  2434. * because hbus->bridge->bus may not exist yet.
  2435. */
  2436. wslot = wslot_to_devfn(hpdev->desc.win_slot.slot);
  2437. pdev = pci_get_domain_bus_and_slot(hbus->bridge->domain_nr, 0, wslot);
  2438. if (pdev) {
  2439. pci_lock_rescan_remove();
  2440. pci_stop_and_remove_bus_device(pdev);
  2441. pci_dev_put(pdev);
  2442. pci_unlock_rescan_remove();
  2443. }
  2444. spin_lock_irqsave(&hbus->device_list_lock, flags);
  2445. list_del(&hpdev->list_entry);
  2446. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  2447. if (hpdev->pci_slot)
  2448. pci_destroy_slot(hpdev->pci_slot);
  2449. memset(&ctxt, 0, sizeof(ctxt));
  2450. ejct_pkt = (struct pci_eject_response *)&ctxt.pkt.message;
  2451. ejct_pkt->message_type.type = PCI_EJECTION_COMPLETE;
  2452. ejct_pkt->wslot.slot = hpdev->desc.win_slot.slot;
  2453. vmbus_sendpacket(hbus->hdev->channel, ejct_pkt,
  2454. sizeof(*ejct_pkt), 0,
  2455. VM_PKT_DATA_INBAND, 0);
  2456. /* For the get_pcichild() in hv_pci_eject_device() */
  2457. put_pcichild(hpdev);
  2458. /* For the two refs got in new_pcichild_device() */
  2459. put_pcichild(hpdev);
  2460. put_pcichild(hpdev);
  2461. /* hpdev has been freed. Do not use it any more. */
  2462. mutex_unlock(&hbus->state_lock);
  2463. }
  2464. /**
  2465. * hv_pci_eject_device() - Handles device ejection
  2466. * @hpdev: Internal device tracking struct
  2467. *
  2468. * This function is invoked when an ejection packet arrives. It
  2469. * just schedules work so that we don't re-enter the packet
  2470. * delivery code handling the ejection.
  2471. */
  2472. static void hv_pci_eject_device(struct hv_pci_dev *hpdev)
  2473. {
  2474. struct hv_pcibus_device *hbus = hpdev->hbus;
  2475. struct hv_device *hdev = hbus->hdev;
  2476. if (hbus->state == hv_pcibus_removing) {
  2477. dev_info(&hdev->device, "PCI VMBus EJECT: ignored\n");
  2478. return;
  2479. }
  2480. get_pcichild(hpdev);
  2481. INIT_WORK(&hpdev->wrk, hv_eject_device_work);
  2482. queue_work(hbus->wq, &hpdev->wrk);
  2483. }
  2484. /**
  2485. * hv_pci_onchannelcallback() - Handles incoming packets
  2486. * @context: Internal bus tracking struct
  2487. *
  2488. * This function is invoked whenever the host sends a packet to
  2489. * this channel (which is private to this root PCI bus).
  2490. */
  2491. static void hv_pci_onchannelcallback(void *context)
  2492. {
  2493. const int packet_size = 0x100;
  2494. int ret;
  2495. struct hv_pcibus_device *hbus = context;
  2496. struct vmbus_channel *chan = hbus->hdev->channel;
  2497. u32 bytes_recvd;
  2498. u64 req_id, req_addr;
  2499. struct vmpacket_descriptor *desc;
  2500. unsigned char *buffer;
  2501. int bufferlen = packet_size;
  2502. struct pci_packet *comp_packet;
  2503. struct pci_response *response;
  2504. struct pci_incoming_message *new_message;
  2505. struct pci_bus_relations *bus_rel;
  2506. struct pci_bus_relations2 *bus_rel2;
  2507. struct pci_dev_inval_block *inval;
  2508. struct pci_dev_incoming *dev_message;
  2509. struct hv_pci_dev *hpdev;
  2510. unsigned long flags;
  2511. buffer = kmalloc(bufferlen, GFP_ATOMIC);
  2512. if (!buffer)
  2513. return;
  2514. while (1) {
  2515. ret = vmbus_recvpacket_raw(chan, buffer, bufferlen,
  2516. &bytes_recvd, &req_id);
  2517. if (ret == -ENOBUFS) {
  2518. kfree(buffer);
  2519. /* Handle large packet */
  2520. bufferlen = bytes_recvd;
  2521. buffer = kmalloc(bytes_recvd, GFP_ATOMIC);
  2522. if (!buffer)
  2523. return;
  2524. continue;
  2525. }
  2526. /* Zero length indicates there are no more packets. */
  2527. if (ret || !bytes_recvd)
  2528. break;
  2529. /*
  2530. * All incoming packets must be at least as large as a
  2531. * response.
  2532. */
  2533. if (bytes_recvd <= sizeof(struct pci_response))
  2534. continue;
  2535. desc = (struct vmpacket_descriptor *)buffer;
  2536. switch (desc->type) {
  2537. case VM_PKT_COMP:
  2538. lock_requestor(chan, flags);
  2539. req_addr = __vmbus_request_addr_match(chan, req_id,
  2540. VMBUS_RQST_ADDR_ANY);
  2541. if (req_addr == VMBUS_RQST_ERROR) {
  2542. unlock_requestor(chan, flags);
  2543. dev_err(&hbus->hdev->device,
  2544. "Invalid transaction ID %llx\n",
  2545. req_id);
  2546. break;
  2547. }
  2548. comp_packet = (struct pci_packet *)req_addr;
  2549. response = (struct pci_response *)buffer;
  2550. /*
  2551. * Call ->completion_func() within the critical section to make
  2552. * sure that the packet pointer is still valid during the call:
  2553. * here 'valid' means that there's a task still waiting for the
  2554. * completion, and that the packet data is still on the waiting
  2555. * task's stack. Cf. hv_compose_msi_msg().
  2556. */
  2557. comp_packet->completion_func(comp_packet->compl_ctxt,
  2558. response,
  2559. bytes_recvd);
  2560. unlock_requestor(chan, flags);
  2561. break;
  2562. case VM_PKT_DATA_INBAND:
  2563. new_message = (struct pci_incoming_message *)buffer;
  2564. switch (new_message->message_type.type) {
  2565. case PCI_BUS_RELATIONS:
  2566. bus_rel = (struct pci_bus_relations *)buffer;
  2567. if (bytes_recvd < sizeof(*bus_rel) ||
  2568. bytes_recvd <
  2569. struct_size(bus_rel, func,
  2570. bus_rel->device_count)) {
  2571. dev_err(&hbus->hdev->device,
  2572. "bus relations too small\n");
  2573. break;
  2574. }
  2575. hv_pci_devices_present(hbus, bus_rel);
  2576. break;
  2577. case PCI_BUS_RELATIONS2:
  2578. bus_rel2 = (struct pci_bus_relations2 *)buffer;
  2579. if (bytes_recvd < sizeof(*bus_rel2) ||
  2580. bytes_recvd <
  2581. struct_size(bus_rel2, func,
  2582. bus_rel2->device_count)) {
  2583. dev_err(&hbus->hdev->device,
  2584. "bus relations v2 too small\n");
  2585. break;
  2586. }
  2587. hv_pci_devices_present2(hbus, bus_rel2);
  2588. break;
  2589. case PCI_EJECT:
  2590. dev_message = (struct pci_dev_incoming *)buffer;
  2591. if (bytes_recvd < sizeof(*dev_message)) {
  2592. dev_err(&hbus->hdev->device,
  2593. "eject message too small\n");
  2594. break;
  2595. }
  2596. hpdev = get_pcichild_wslot(hbus,
  2597. dev_message->wslot.slot);
  2598. if (hpdev) {
  2599. hv_pci_eject_device(hpdev);
  2600. put_pcichild(hpdev);
  2601. }
  2602. break;
  2603. case PCI_INVALIDATE_BLOCK:
  2604. inval = (struct pci_dev_inval_block *)buffer;
  2605. if (bytes_recvd < sizeof(*inval)) {
  2606. dev_err(&hbus->hdev->device,
  2607. "invalidate message too small\n");
  2608. break;
  2609. }
  2610. hpdev = get_pcichild_wslot(hbus,
  2611. inval->wslot.slot);
  2612. if (hpdev) {
  2613. if (hpdev->block_invalidate) {
  2614. hpdev->block_invalidate(
  2615. hpdev->invalidate_context,
  2616. inval->block_mask);
  2617. }
  2618. put_pcichild(hpdev);
  2619. }
  2620. break;
  2621. default:
  2622. dev_warn(&hbus->hdev->device,
  2623. "Unimplemented protocol message %x\n",
  2624. new_message->message_type.type);
  2625. break;
  2626. }
  2627. break;
  2628. default:
  2629. dev_err(&hbus->hdev->device,
  2630. "unhandled packet type %d, tid %llx len %d\n",
  2631. desc->type, req_id, bytes_recvd);
  2632. break;
  2633. }
  2634. }
  2635. kfree(buffer);
  2636. }
  2637. /**
  2638. * hv_pci_protocol_negotiation() - Set up protocol
  2639. * @hdev: VMBus's tracking struct for this root PCI bus.
  2640. * @version: Array of supported channel protocol versions in
  2641. * the order of probing - highest go first.
  2642. * @num_version: Number of elements in the version array.
  2643. *
  2644. * This driver is intended to support running on Windows 10
  2645. * (server) and later versions. It will not run on earlier
  2646. * versions, as they assume that many of the operations which
  2647. * Linux needs accomplished with a spinlock held were done via
  2648. * asynchronous messaging via VMBus. Windows 10 increases the
  2649. * surface area of PCI emulation so that these actions can take
  2650. * place by suspending a virtual processor for their duration.
  2651. *
  2652. * This function negotiates the channel protocol version,
  2653. * failing if the host doesn't support the necessary protocol
  2654. * level.
  2655. */
  2656. static int hv_pci_protocol_negotiation(struct hv_device *hdev,
  2657. enum pci_protocol_version_t version[],
  2658. int num_version)
  2659. {
  2660. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2661. struct pci_version_request *version_req;
  2662. struct hv_pci_compl comp_pkt;
  2663. struct pci_packet *pkt;
  2664. int ret;
  2665. int i;
  2666. /*
  2667. * Initiate the handshake with the host and negotiate
  2668. * a version that the host can support. We start with the
  2669. * highest version number and go down if the host cannot
  2670. * support it.
  2671. */
  2672. pkt = kzalloc(sizeof(*pkt) + sizeof(*version_req), GFP_KERNEL);
  2673. if (!pkt)
  2674. return -ENOMEM;
  2675. init_completion(&comp_pkt.host_event);
  2676. pkt->completion_func = hv_pci_generic_compl;
  2677. pkt->compl_ctxt = &comp_pkt;
  2678. version_req = (struct pci_version_request *)&pkt->message;
  2679. version_req->message_type.type = PCI_QUERY_PROTOCOL_VERSION;
  2680. for (i = 0; i < num_version; i++) {
  2681. version_req->protocol_version = version[i];
  2682. ret = vmbus_sendpacket(hdev->channel, version_req,
  2683. sizeof(struct pci_version_request),
  2684. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  2685. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2686. if (!ret)
  2687. ret = wait_for_response(hdev, &comp_pkt.host_event);
  2688. if (ret) {
  2689. dev_err(&hdev->device,
  2690. "PCI Pass-through VSP failed to request version: %d",
  2691. ret);
  2692. goto exit;
  2693. }
  2694. if (comp_pkt.completion_status >= 0) {
  2695. hbus->protocol_version = version[i];
  2696. dev_info(&hdev->device,
  2697. "PCI VMBus probing: Using version %#x\n",
  2698. hbus->protocol_version);
  2699. goto exit;
  2700. }
  2701. if (comp_pkt.completion_status != STATUS_REVISION_MISMATCH) {
  2702. dev_err(&hdev->device,
  2703. "PCI Pass-through VSP failed version request: %#x",
  2704. comp_pkt.completion_status);
  2705. ret = -EPROTO;
  2706. goto exit;
  2707. }
  2708. reinit_completion(&comp_pkt.host_event);
  2709. }
  2710. dev_err(&hdev->device,
  2711. "PCI pass-through VSP failed to find supported version");
  2712. ret = -EPROTO;
  2713. exit:
  2714. kfree(pkt);
  2715. return ret;
  2716. }
  2717. /**
  2718. * hv_pci_free_bridge_windows() - Release memory regions for the
  2719. * bus
  2720. * @hbus: Root PCI bus, as understood by this driver
  2721. */
  2722. static void hv_pci_free_bridge_windows(struct hv_pcibus_device *hbus)
  2723. {
  2724. /*
  2725. * Set the resources back to the way they looked when they
  2726. * were allocated by setting IORESOURCE_BUSY again.
  2727. */
  2728. if (hbus->low_mmio_space && hbus->low_mmio_res) {
  2729. hbus->low_mmio_res->flags |= IORESOURCE_BUSY;
  2730. vmbus_free_mmio(hbus->low_mmio_res->start,
  2731. resource_size(hbus->low_mmio_res));
  2732. }
  2733. if (hbus->high_mmio_space && hbus->high_mmio_res) {
  2734. hbus->high_mmio_res->flags |= IORESOURCE_BUSY;
  2735. vmbus_free_mmio(hbus->high_mmio_res->start,
  2736. resource_size(hbus->high_mmio_res));
  2737. }
  2738. }
  2739. /**
  2740. * hv_pci_allocate_bridge_windows() - Allocate memory regions
  2741. * for the bus
  2742. * @hbus: Root PCI bus, as understood by this driver
  2743. *
  2744. * This function calls vmbus_allocate_mmio(), which is itself a
  2745. * bit of a compromise. Ideally, we might change the pnp layer
  2746. * in the kernel such that it comprehends either PCI devices
  2747. * which are "grandchildren of ACPI," with some intermediate bus
  2748. * node (in this case, VMBus) or change it such that it
  2749. * understands VMBus. The pnp layer, however, has been declared
  2750. * deprecated, and not subject to change.
  2751. *
  2752. * The workaround, implemented here, is to ask VMBus to allocate
  2753. * MMIO space for this bus. VMBus itself knows which ranges are
  2754. * appropriate by looking at its own ACPI objects. Then, after
  2755. * these ranges are claimed, they're modified to look like they
  2756. * would have looked if the ACPI and pnp code had allocated
  2757. * bridge windows. These descriptors have to exist in this form
  2758. * in order to satisfy the code which will get invoked when the
  2759. * endpoint PCI function driver calls request_mem_region() or
  2760. * request_mem_region_exclusive().
  2761. *
  2762. * Return: 0 on success, -errno on failure
  2763. */
  2764. static int hv_pci_allocate_bridge_windows(struct hv_pcibus_device *hbus)
  2765. {
  2766. resource_size_t align;
  2767. int ret;
  2768. if (hbus->low_mmio_space) {
  2769. align = 1ULL << (63 - __builtin_clzll(hbus->low_mmio_space));
  2770. ret = vmbus_allocate_mmio(&hbus->low_mmio_res, hbus->hdev, 0,
  2771. (u64)(u32)0xffffffff,
  2772. hbus->low_mmio_space,
  2773. align, false);
  2774. if (ret) {
  2775. dev_err(&hbus->hdev->device,
  2776. "Need %#llx of low MMIO space. Consider reconfiguring the VM.\n",
  2777. hbus->low_mmio_space);
  2778. return ret;
  2779. }
  2780. /* Modify this resource to become a bridge window. */
  2781. hbus->low_mmio_res->flags |= IORESOURCE_WINDOW;
  2782. hbus->low_mmio_res->flags &= ~IORESOURCE_BUSY;
  2783. pci_add_resource(&hbus->bridge->windows, hbus->low_mmio_res);
  2784. }
  2785. if (hbus->high_mmio_space) {
  2786. align = 1ULL << (63 - __builtin_clzll(hbus->high_mmio_space));
  2787. ret = vmbus_allocate_mmio(&hbus->high_mmio_res, hbus->hdev,
  2788. 0x100000000, -1,
  2789. hbus->high_mmio_space, align,
  2790. false);
  2791. if (ret) {
  2792. dev_err(&hbus->hdev->device,
  2793. "Need %#llx of high MMIO space. Consider reconfiguring the VM.\n",
  2794. hbus->high_mmio_space);
  2795. goto release_low_mmio;
  2796. }
  2797. /* Modify this resource to become a bridge window. */
  2798. hbus->high_mmio_res->flags |= IORESOURCE_WINDOW;
  2799. hbus->high_mmio_res->flags &= ~IORESOURCE_BUSY;
  2800. pci_add_resource(&hbus->bridge->windows, hbus->high_mmio_res);
  2801. }
  2802. return 0;
  2803. release_low_mmio:
  2804. if (hbus->low_mmio_res) {
  2805. vmbus_free_mmio(hbus->low_mmio_res->start,
  2806. resource_size(hbus->low_mmio_res));
  2807. }
  2808. return ret;
  2809. }
  2810. /**
  2811. * hv_allocate_config_window() - Find MMIO space for PCI Config
  2812. * @hbus: Root PCI bus, as understood by this driver
  2813. *
  2814. * This function claims memory-mapped I/O space for accessing
  2815. * configuration space for the functions on this bus.
  2816. *
  2817. * Return: 0 on success, -errno on failure
  2818. */
  2819. static int hv_allocate_config_window(struct hv_pcibus_device *hbus)
  2820. {
  2821. int ret;
  2822. /*
  2823. * Set up a region of MMIO space to use for accessing configuration
  2824. * space.
  2825. */
  2826. ret = vmbus_allocate_mmio(&hbus->mem_config, hbus->hdev, 0, -1,
  2827. PCI_CONFIG_MMIO_LENGTH, 0x1000, false);
  2828. if (ret)
  2829. return ret;
  2830. /*
  2831. * vmbus_allocate_mmio() gets used for allocating both device endpoint
  2832. * resource claims (those which cannot be overlapped) and the ranges
  2833. * which are valid for the children of this bus, which are intended
  2834. * to be overlapped by those children. Set the flag on this claim
  2835. * meaning that this region can't be overlapped.
  2836. */
  2837. hbus->mem_config->flags |= IORESOURCE_BUSY;
  2838. return 0;
  2839. }
  2840. static void hv_free_config_window(struct hv_pcibus_device *hbus)
  2841. {
  2842. vmbus_free_mmio(hbus->mem_config->start, PCI_CONFIG_MMIO_LENGTH);
  2843. }
  2844. static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs);
  2845. /**
  2846. * hv_pci_enter_d0() - Bring the "bus" into the D0 power state
  2847. * @hdev: VMBus's tracking struct for this root PCI bus
  2848. *
  2849. * Return: 0 on success, -errno on failure
  2850. */
  2851. static int hv_pci_enter_d0(struct hv_device *hdev)
  2852. {
  2853. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2854. struct pci_bus_d0_entry *d0_entry;
  2855. struct hv_pci_compl comp_pkt;
  2856. struct pci_packet *pkt;
  2857. bool retry = true;
  2858. int ret;
  2859. enter_d0_retry:
  2860. /*
  2861. * Tell the host that the bus is ready to use, and moved into the
  2862. * powered-on state. This includes telling the host which region
  2863. * of memory-mapped I/O space has been chosen for configuration space
  2864. * access.
  2865. */
  2866. pkt = kzalloc(sizeof(*pkt) + sizeof(*d0_entry), GFP_KERNEL);
  2867. if (!pkt)
  2868. return -ENOMEM;
  2869. init_completion(&comp_pkt.host_event);
  2870. pkt->completion_func = hv_pci_generic_compl;
  2871. pkt->compl_ctxt = &comp_pkt;
  2872. d0_entry = (struct pci_bus_d0_entry *)&pkt->message;
  2873. d0_entry->message_type.type = PCI_BUS_D0ENTRY;
  2874. d0_entry->mmio_base = hbus->mem_config->start;
  2875. ret = vmbus_sendpacket(hdev->channel, d0_entry, sizeof(*d0_entry),
  2876. (unsigned long)pkt, VM_PKT_DATA_INBAND,
  2877. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  2878. if (!ret)
  2879. ret = wait_for_response(hdev, &comp_pkt.host_event);
  2880. if (ret)
  2881. goto exit;
  2882. /*
  2883. * In certain case (Kdump) the pci device of interest was
  2884. * not cleanly shut down and resource is still held on host
  2885. * side, the host could return invalid device status.
  2886. * We need to explicitly request host to release the resource
  2887. * and try to enter D0 again.
  2888. */
  2889. if (comp_pkt.completion_status < 0 && retry) {
  2890. retry = false;
  2891. dev_err(&hdev->device, "Retrying D0 Entry\n");
  2892. /*
  2893. * Hv_pci_bus_exit() calls hv_send_resource_released()
  2894. * to free up resources of its child devices.
  2895. * In the kdump kernel we need to set the
  2896. * wslot_res_allocated to 255 so it scans all child
  2897. * devices to release resources allocated in the
  2898. * normal kernel before panic happened.
  2899. */
  2900. hbus->wslot_res_allocated = 255;
  2901. ret = hv_pci_bus_exit(hdev, true);
  2902. if (ret == 0) {
  2903. kfree(pkt);
  2904. goto enter_d0_retry;
  2905. }
  2906. dev_err(&hdev->device,
  2907. "Retrying D0 failed with ret %d\n", ret);
  2908. }
  2909. if (comp_pkt.completion_status < 0) {
  2910. dev_err(&hdev->device,
  2911. "PCI Pass-through VSP failed D0 Entry with status %x\n",
  2912. comp_pkt.completion_status);
  2913. ret = -EPROTO;
  2914. goto exit;
  2915. }
  2916. ret = 0;
  2917. exit:
  2918. kfree(pkt);
  2919. return ret;
  2920. }
  2921. /**
  2922. * hv_pci_query_relations() - Ask host to send list of child
  2923. * devices
  2924. * @hdev: VMBus's tracking struct for this root PCI bus
  2925. *
  2926. * Return: 0 on success, -errno on failure
  2927. */
  2928. static int hv_pci_query_relations(struct hv_device *hdev)
  2929. {
  2930. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2931. struct pci_message message;
  2932. struct completion comp;
  2933. int ret;
  2934. /* Ask the host to send along the list of child devices */
  2935. init_completion(&comp);
  2936. if (cmpxchg(&hbus->survey_event, NULL, &comp))
  2937. return -ENOTEMPTY;
  2938. memset(&message, 0, sizeof(message));
  2939. message.type = PCI_QUERY_BUS_RELATIONS;
  2940. ret = vmbus_sendpacket(hdev->channel, &message, sizeof(message),
  2941. 0, VM_PKT_DATA_INBAND, 0);
  2942. if (!ret)
  2943. ret = wait_for_response(hdev, &comp);
  2944. /*
  2945. * In the case of fast device addition/removal, it's possible that
  2946. * vmbus_sendpacket() or wait_for_response() returns -ENODEV but we
  2947. * already got a PCI_BUS_RELATIONS* message from the host and the
  2948. * channel callback already scheduled a work to hbus->wq, which can be
  2949. * running pci_devices_present_work() -> survey_child_resources() ->
  2950. * complete(&hbus->survey_event), even after hv_pci_query_relations()
  2951. * exits and the stack variable 'comp' is no longer valid; as a result,
  2952. * a hang or a page fault may happen when the complete() calls
  2953. * raw_spin_lock_irqsave(). Flush hbus->wq before we exit from
  2954. * hv_pci_query_relations() to avoid the issues. Note: if 'ret' is
  2955. * -ENODEV, there can't be any more work item scheduled to hbus->wq
  2956. * after the flush_workqueue(): see vmbus_onoffer_rescind() ->
  2957. * vmbus_reset_channel_cb(), vmbus_rescind_cleanup() ->
  2958. * channel->rescind = true.
  2959. */
  2960. flush_workqueue(hbus->wq);
  2961. return ret;
  2962. }
  2963. /**
  2964. * hv_send_resources_allocated() - Report local resource choices
  2965. * @hdev: VMBus's tracking struct for this root PCI bus
  2966. *
  2967. * The host OS is expecting to be sent a request as a message
  2968. * which contains all the resources that the device will use.
  2969. * The response contains those same resources, "translated"
  2970. * which is to say, the values which should be used by the
  2971. * hardware, when it delivers an interrupt. (MMIO resources are
  2972. * used in local terms.) This is nice for Windows, and lines up
  2973. * with the FDO/PDO split, which doesn't exist in Linux. Linux
  2974. * is deeply expecting to scan an emulated PCI configuration
  2975. * space. So this message is sent here only to drive the state
  2976. * machine on the host forward.
  2977. *
  2978. * Return: 0 on success, -errno on failure
  2979. */
  2980. static int hv_send_resources_allocated(struct hv_device *hdev)
  2981. {
  2982. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  2983. struct pci_resources_assigned *res_assigned;
  2984. struct pci_resources_assigned2 *res_assigned2;
  2985. struct hv_pci_compl comp_pkt;
  2986. struct hv_pci_dev *hpdev;
  2987. struct pci_packet *pkt;
  2988. size_t size_res;
  2989. int wslot;
  2990. int ret;
  2991. size_res = (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2)
  2992. ? sizeof(*res_assigned) : sizeof(*res_assigned2);
  2993. pkt = kmalloc(sizeof(*pkt) + size_res, GFP_KERNEL);
  2994. if (!pkt)
  2995. return -ENOMEM;
  2996. ret = 0;
  2997. for (wslot = 0; wslot < 256; wslot++) {
  2998. hpdev = get_pcichild_wslot(hbus, wslot);
  2999. if (!hpdev)
  3000. continue;
  3001. memset(pkt, 0, sizeof(*pkt) + size_res);
  3002. init_completion(&comp_pkt.host_event);
  3003. pkt->completion_func = hv_pci_generic_compl;
  3004. pkt->compl_ctxt = &comp_pkt;
  3005. if (hbus->protocol_version < PCI_PROTOCOL_VERSION_1_2) {
  3006. res_assigned =
  3007. (struct pci_resources_assigned *)&pkt->message;
  3008. res_assigned->message_type.type =
  3009. PCI_RESOURCES_ASSIGNED;
  3010. res_assigned->wslot.slot = hpdev->desc.win_slot.slot;
  3011. } else {
  3012. res_assigned2 =
  3013. (struct pci_resources_assigned2 *)&pkt->message;
  3014. res_assigned2->message_type.type =
  3015. PCI_RESOURCES_ASSIGNED2;
  3016. res_assigned2->wslot.slot = hpdev->desc.win_slot.slot;
  3017. }
  3018. put_pcichild(hpdev);
  3019. ret = vmbus_sendpacket(hdev->channel, &pkt->message,
  3020. size_res, (unsigned long)pkt,
  3021. VM_PKT_DATA_INBAND,
  3022. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  3023. if (!ret)
  3024. ret = wait_for_response(hdev, &comp_pkt.host_event);
  3025. if (ret)
  3026. break;
  3027. if (comp_pkt.completion_status < 0) {
  3028. ret = -EPROTO;
  3029. dev_err(&hdev->device,
  3030. "resource allocated returned 0x%x",
  3031. comp_pkt.completion_status);
  3032. break;
  3033. }
  3034. hbus->wslot_res_allocated = wslot;
  3035. }
  3036. kfree(pkt);
  3037. return ret;
  3038. }
  3039. /**
  3040. * hv_send_resources_released() - Report local resources
  3041. * released
  3042. * @hdev: VMBus's tracking struct for this root PCI bus
  3043. *
  3044. * Return: 0 on success, -errno on failure
  3045. */
  3046. static int hv_send_resources_released(struct hv_device *hdev)
  3047. {
  3048. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3049. struct pci_child_message pkt;
  3050. struct hv_pci_dev *hpdev;
  3051. int wslot;
  3052. int ret;
  3053. for (wslot = hbus->wslot_res_allocated; wslot >= 0; wslot--) {
  3054. hpdev = get_pcichild_wslot(hbus, wslot);
  3055. if (!hpdev)
  3056. continue;
  3057. memset(&pkt, 0, sizeof(pkt));
  3058. pkt.message_type.type = PCI_RESOURCES_RELEASED;
  3059. pkt.wslot.slot = hpdev->desc.win_slot.slot;
  3060. put_pcichild(hpdev);
  3061. ret = vmbus_sendpacket(hdev->channel, &pkt, sizeof(pkt), 0,
  3062. VM_PKT_DATA_INBAND, 0);
  3063. if (ret)
  3064. return ret;
  3065. hbus->wslot_res_allocated = wslot - 1;
  3066. }
  3067. hbus->wslot_res_allocated = -1;
  3068. return 0;
  3069. }
  3070. #define HVPCI_DOM_MAP_SIZE (64 * 1024)
  3071. static DECLARE_BITMAP(hvpci_dom_map, HVPCI_DOM_MAP_SIZE);
  3072. /*
  3073. * PCI domain number 0 is used by emulated devices on Gen1 VMs, so define 0
  3074. * as invalid for passthrough PCI devices of this driver.
  3075. */
  3076. #define HVPCI_DOM_INVALID 0
  3077. /**
  3078. * hv_get_dom_num() - Get a valid PCI domain number
  3079. * Check if the PCI domain number is in use, and return another number if
  3080. * it is in use.
  3081. *
  3082. * @dom: Requested domain number
  3083. *
  3084. * return: domain number on success, HVPCI_DOM_INVALID on failure
  3085. */
  3086. static u16 hv_get_dom_num(u16 dom)
  3087. {
  3088. unsigned int i;
  3089. if (test_and_set_bit(dom, hvpci_dom_map) == 0)
  3090. return dom;
  3091. for_each_clear_bit(i, hvpci_dom_map, HVPCI_DOM_MAP_SIZE) {
  3092. if (test_and_set_bit(i, hvpci_dom_map) == 0)
  3093. return i;
  3094. }
  3095. return HVPCI_DOM_INVALID;
  3096. }
  3097. /**
  3098. * hv_put_dom_num() - Mark the PCI domain number as free
  3099. * @dom: Domain number to be freed
  3100. */
  3101. static void hv_put_dom_num(u16 dom)
  3102. {
  3103. clear_bit(dom, hvpci_dom_map);
  3104. }
  3105. /**
  3106. * hv_pci_probe() - New VMBus channel probe, for a root PCI bus
  3107. * @hdev: VMBus's tracking struct for this root PCI bus
  3108. * @dev_id: Identifies the device itself
  3109. *
  3110. * Return: 0 on success, -errno on failure
  3111. */
  3112. static int hv_pci_probe(struct hv_device *hdev,
  3113. const struct hv_vmbus_device_id *dev_id)
  3114. {
  3115. struct pci_host_bridge *bridge;
  3116. struct hv_pcibus_device *hbus;
  3117. u16 dom_req, dom;
  3118. char *name;
  3119. int ret;
  3120. /*
  3121. * hv_pcibus_device contains the hypercall arguments for retargeting in
  3122. * hv_irq_unmask(). Those must not cross a page boundary.
  3123. */
  3124. BUILD_BUG_ON(sizeof(*hbus) > HV_HYP_PAGE_SIZE);
  3125. bridge = devm_pci_alloc_host_bridge(&hdev->device, 0);
  3126. if (!bridge)
  3127. return -ENOMEM;
  3128. /*
  3129. * With the recent 59bb47985c1d ("mm, sl[aou]b: guarantee natural
  3130. * alignment for kmalloc(power-of-two)"), kzalloc() is able to allocate
  3131. * a 4KB buffer that is guaranteed to be 4KB-aligned. Here the size and
  3132. * alignment of hbus is important because hbus's field
  3133. * retarget_msi_interrupt_params must not cross a 4KB page boundary.
  3134. *
  3135. * Here we prefer kzalloc to get_zeroed_page(), because a buffer
  3136. * allocated by the latter is not tracked and scanned by kmemleak, and
  3137. * hence kmemleak reports the pointer contained in the hbus buffer
  3138. * (i.e. the hpdev struct, which is created in new_pcichild_device() and
  3139. * is tracked by hbus->children) as memory leak (false positive).
  3140. *
  3141. * If the kernel doesn't have 59bb47985c1d, get_zeroed_page() *must* be
  3142. * used to allocate the hbus buffer and we can avoid the kmemleak false
  3143. * positive by using kmemleak_alloc() and kmemleak_free() to ask
  3144. * kmemleak to track and scan the hbus buffer.
  3145. */
  3146. hbus = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
  3147. if (!hbus)
  3148. return -ENOMEM;
  3149. hbus->bridge = bridge;
  3150. mutex_init(&hbus->state_lock);
  3151. hbus->state = hv_pcibus_init;
  3152. hbus->wslot_res_allocated = -1;
  3153. /*
  3154. * The PCI bus "domain" is what is called "segment" in ACPI and other
  3155. * specs. Pull it from the instance ID, to get something usually
  3156. * unique. In rare cases of collision, we will find out another number
  3157. * not in use.
  3158. *
  3159. * Note that, since this code only runs in a Hyper-V VM, Hyper-V
  3160. * together with this guest driver can guarantee that (1) The only
  3161. * domain used by Gen1 VMs for something that looks like a physical
  3162. * PCI bus (which is actually emulated by the hypervisor) is domain 0.
  3163. * (2) There will be no overlap between domains (after fixing possible
  3164. * collisions) in the same VM.
  3165. */
  3166. dom_req = hdev->dev_instance.b[5] << 8 | hdev->dev_instance.b[4];
  3167. dom = hv_get_dom_num(dom_req);
  3168. if (dom == HVPCI_DOM_INVALID) {
  3169. dev_err(&hdev->device,
  3170. "Unable to use dom# 0x%x or other numbers", dom_req);
  3171. ret = -EINVAL;
  3172. goto free_bus;
  3173. }
  3174. if (dom != dom_req)
  3175. dev_info(&hdev->device,
  3176. "PCI dom# 0x%x has collision, using 0x%x",
  3177. dom_req, dom);
  3178. hbus->bridge->domain_nr = dom;
  3179. #ifdef CONFIG_X86
  3180. hbus->sysdata.domain = dom;
  3181. #elif defined(CONFIG_ARM64)
  3182. /*
  3183. * Set the PCI bus parent to be the corresponding VMbus
  3184. * device. Then the VMbus device will be assigned as the
  3185. * ACPI companion in pcibios_root_bridge_prepare() and
  3186. * pci_dma_configure() will propagate device coherence
  3187. * information to devices created on the bus.
  3188. */
  3189. hbus->sysdata.parent = hdev->device.parent;
  3190. #endif
  3191. hbus->hdev = hdev;
  3192. INIT_LIST_HEAD(&hbus->children);
  3193. INIT_LIST_HEAD(&hbus->dr_list);
  3194. spin_lock_init(&hbus->config_lock);
  3195. spin_lock_init(&hbus->device_list_lock);
  3196. spin_lock_init(&hbus->retarget_msi_interrupt_lock);
  3197. hbus->wq = alloc_ordered_workqueue("hv_pci_%x", 0,
  3198. hbus->bridge->domain_nr);
  3199. if (!hbus->wq) {
  3200. ret = -ENOMEM;
  3201. goto free_dom;
  3202. }
  3203. hdev->channel->next_request_id_callback = vmbus_next_request_id;
  3204. hdev->channel->request_addr_callback = vmbus_request_addr;
  3205. hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
  3206. ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
  3207. hv_pci_onchannelcallback, hbus);
  3208. if (ret)
  3209. goto destroy_wq;
  3210. hv_set_drvdata(hdev, hbus);
  3211. ret = hv_pci_protocol_negotiation(hdev, pci_protocol_versions,
  3212. ARRAY_SIZE(pci_protocol_versions));
  3213. if (ret)
  3214. goto close;
  3215. ret = hv_allocate_config_window(hbus);
  3216. if (ret)
  3217. goto close;
  3218. hbus->cfg_addr = ioremap(hbus->mem_config->start,
  3219. PCI_CONFIG_MMIO_LENGTH);
  3220. if (!hbus->cfg_addr) {
  3221. dev_err(&hdev->device,
  3222. "Unable to map a virtual address for config space\n");
  3223. ret = -ENOMEM;
  3224. goto free_config;
  3225. }
  3226. name = kasprintf(GFP_KERNEL, "%pUL", &hdev->dev_instance);
  3227. if (!name) {
  3228. ret = -ENOMEM;
  3229. goto unmap;
  3230. }
  3231. hbus->fwnode = irq_domain_alloc_named_fwnode(name);
  3232. kfree(name);
  3233. if (!hbus->fwnode) {
  3234. ret = -ENOMEM;
  3235. goto unmap;
  3236. }
  3237. ret = hv_pcie_init_irq_domain(hbus);
  3238. if (ret)
  3239. goto free_fwnode;
  3240. ret = hv_pci_query_relations(hdev);
  3241. if (ret)
  3242. goto free_irq_domain;
  3243. mutex_lock(&hbus->state_lock);
  3244. ret = hv_pci_enter_d0(hdev);
  3245. if (ret)
  3246. goto release_state_lock;
  3247. ret = hv_pci_allocate_bridge_windows(hbus);
  3248. if (ret)
  3249. goto exit_d0;
  3250. ret = hv_send_resources_allocated(hdev);
  3251. if (ret)
  3252. goto free_windows;
  3253. prepopulate_bars(hbus);
  3254. hbus->state = hv_pcibus_probed;
  3255. ret = create_root_hv_pci_bus(hbus);
  3256. if (ret)
  3257. goto free_windows;
  3258. mutex_unlock(&hbus->state_lock);
  3259. return 0;
  3260. free_windows:
  3261. hv_pci_free_bridge_windows(hbus);
  3262. exit_d0:
  3263. (void) hv_pci_bus_exit(hdev, true);
  3264. release_state_lock:
  3265. mutex_unlock(&hbus->state_lock);
  3266. free_irq_domain:
  3267. irq_domain_remove(hbus->irq_domain);
  3268. free_fwnode:
  3269. irq_domain_free_fwnode(hbus->fwnode);
  3270. unmap:
  3271. iounmap(hbus->cfg_addr);
  3272. free_config:
  3273. hv_free_config_window(hbus);
  3274. close:
  3275. vmbus_close(hdev->channel);
  3276. destroy_wq:
  3277. destroy_workqueue(hbus->wq);
  3278. free_dom:
  3279. hv_put_dom_num(hbus->bridge->domain_nr);
  3280. free_bus:
  3281. kfree(hbus);
  3282. return ret;
  3283. }
  3284. static int hv_pci_bus_exit(struct hv_device *hdev, bool keep_devs)
  3285. {
  3286. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3287. struct vmbus_channel *chan = hdev->channel;
  3288. struct {
  3289. struct pci_packet teardown_packet;
  3290. u8 buffer[sizeof(struct pci_message)];
  3291. } pkt;
  3292. struct hv_pci_compl comp_pkt;
  3293. struct hv_pci_dev *hpdev, *tmp;
  3294. unsigned long flags;
  3295. u64 trans_id;
  3296. int ret;
  3297. /*
  3298. * After the host sends the RESCIND_CHANNEL message, it doesn't
  3299. * access the per-channel ringbuffer any longer.
  3300. */
  3301. if (chan->rescind)
  3302. return 0;
  3303. if (!keep_devs) {
  3304. struct list_head removed;
  3305. /* Move all present children to the list on stack */
  3306. INIT_LIST_HEAD(&removed);
  3307. spin_lock_irqsave(&hbus->device_list_lock, flags);
  3308. list_for_each_entry_safe(hpdev, tmp, &hbus->children, list_entry)
  3309. list_move_tail(&hpdev->list_entry, &removed);
  3310. spin_unlock_irqrestore(&hbus->device_list_lock, flags);
  3311. /* Remove all children in the list */
  3312. list_for_each_entry_safe(hpdev, tmp, &removed, list_entry) {
  3313. list_del(&hpdev->list_entry);
  3314. if (hpdev->pci_slot)
  3315. pci_destroy_slot(hpdev->pci_slot);
  3316. /* For the two refs got in new_pcichild_device() */
  3317. put_pcichild(hpdev);
  3318. put_pcichild(hpdev);
  3319. }
  3320. }
  3321. ret = hv_send_resources_released(hdev);
  3322. if (ret) {
  3323. dev_err(&hdev->device,
  3324. "Couldn't send resources released packet(s)\n");
  3325. return ret;
  3326. }
  3327. memset(&pkt.teardown_packet, 0, sizeof(pkt.teardown_packet));
  3328. init_completion(&comp_pkt.host_event);
  3329. pkt.teardown_packet.completion_func = hv_pci_generic_compl;
  3330. pkt.teardown_packet.compl_ctxt = &comp_pkt;
  3331. pkt.teardown_packet.message[0].type = PCI_BUS_D0EXIT;
  3332. ret = vmbus_sendpacket_getid(chan, &pkt.teardown_packet.message,
  3333. sizeof(struct pci_message),
  3334. (unsigned long)&pkt.teardown_packet,
  3335. &trans_id, VM_PKT_DATA_INBAND,
  3336. VMBUS_DATA_PACKET_FLAG_COMPLETION_REQUESTED);
  3337. if (ret)
  3338. return ret;
  3339. if (wait_for_completion_timeout(&comp_pkt.host_event, 10 * HZ) == 0) {
  3340. /*
  3341. * The completion packet on the stack becomes invalid after
  3342. * 'return'; remove the ID from the VMbus requestor if the
  3343. * identifier is still mapped to/associated with the packet.
  3344. *
  3345. * Cf. hv_pci_onchannelcallback().
  3346. */
  3347. vmbus_request_addr_match(chan, trans_id,
  3348. (unsigned long)&pkt.teardown_packet);
  3349. return -ETIMEDOUT;
  3350. }
  3351. return 0;
  3352. }
  3353. /**
  3354. * hv_pci_remove() - Remove routine for this VMBus channel
  3355. * @hdev: VMBus's tracking struct for this root PCI bus
  3356. *
  3357. * Return: 0 on success, -errno on failure
  3358. */
  3359. static int hv_pci_remove(struct hv_device *hdev)
  3360. {
  3361. struct hv_pcibus_device *hbus;
  3362. int ret;
  3363. hbus = hv_get_drvdata(hdev);
  3364. if (hbus->state == hv_pcibus_installed) {
  3365. tasklet_disable(&hdev->channel->callback_event);
  3366. hbus->state = hv_pcibus_removing;
  3367. tasklet_enable(&hdev->channel->callback_event);
  3368. destroy_workqueue(hbus->wq);
  3369. hbus->wq = NULL;
  3370. /*
  3371. * At this point, no work is running or can be scheduled
  3372. * on hbus-wq. We can't race with hv_pci_devices_present()
  3373. * or hv_pci_eject_device(), it's safe to proceed.
  3374. */
  3375. /* Remove the bus from PCI's point of view. */
  3376. pci_lock_rescan_remove();
  3377. pci_stop_root_bus(hbus->bridge->bus);
  3378. hv_pci_remove_slots(hbus);
  3379. pci_remove_root_bus(hbus->bridge->bus);
  3380. pci_unlock_rescan_remove();
  3381. }
  3382. ret = hv_pci_bus_exit(hdev, false);
  3383. vmbus_close(hdev->channel);
  3384. iounmap(hbus->cfg_addr);
  3385. hv_free_config_window(hbus);
  3386. hv_pci_free_bridge_windows(hbus);
  3387. irq_domain_remove(hbus->irq_domain);
  3388. irq_domain_free_fwnode(hbus->fwnode);
  3389. hv_put_dom_num(hbus->bridge->domain_nr);
  3390. kfree(hbus);
  3391. return ret;
  3392. }
  3393. static int hv_pci_suspend(struct hv_device *hdev)
  3394. {
  3395. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3396. enum hv_pcibus_state old_state;
  3397. int ret;
  3398. /*
  3399. * hv_pci_suspend() must make sure there are no pending work items
  3400. * before calling vmbus_close(), since it runs in a process context
  3401. * as a callback in dpm_suspend(). When it starts to run, the channel
  3402. * callback hv_pci_onchannelcallback(), which runs in a tasklet
  3403. * context, can be still running concurrently and scheduling new work
  3404. * items onto hbus->wq in hv_pci_devices_present() and
  3405. * hv_pci_eject_device(), and the work item handlers can access the
  3406. * vmbus channel, which can be being closed by hv_pci_suspend(), e.g.
  3407. * the work item handler pci_devices_present_work() ->
  3408. * new_pcichild_device() writes to the vmbus channel.
  3409. *
  3410. * To eliminate the race, hv_pci_suspend() disables the channel
  3411. * callback tasklet, sets hbus->state to hv_pcibus_removing, and
  3412. * re-enables the tasklet. This way, when hv_pci_suspend() proceeds,
  3413. * it knows that no new work item can be scheduled, and then it flushes
  3414. * hbus->wq and safely closes the vmbus channel.
  3415. */
  3416. tasklet_disable(&hdev->channel->callback_event);
  3417. /* Change the hbus state to prevent new work items. */
  3418. old_state = hbus->state;
  3419. if (hbus->state == hv_pcibus_installed)
  3420. hbus->state = hv_pcibus_removing;
  3421. tasklet_enable(&hdev->channel->callback_event);
  3422. if (old_state != hv_pcibus_installed)
  3423. return -EINVAL;
  3424. flush_workqueue(hbus->wq);
  3425. ret = hv_pci_bus_exit(hdev, true);
  3426. if (ret)
  3427. return ret;
  3428. vmbus_close(hdev->channel);
  3429. return 0;
  3430. }
  3431. static int hv_pci_restore_msi_msg(struct pci_dev *pdev, void *arg)
  3432. {
  3433. struct irq_data *irq_data;
  3434. struct msi_desc *entry;
  3435. int ret = 0;
  3436. if (!pdev->msi_enabled && !pdev->msix_enabled)
  3437. return 0;
  3438. msi_lock_descs(&pdev->dev);
  3439. msi_for_each_desc(entry, &pdev->dev, MSI_DESC_ASSOCIATED) {
  3440. irq_data = irq_get_irq_data(entry->irq);
  3441. if (WARN_ON_ONCE(!irq_data)) {
  3442. ret = -EINVAL;
  3443. break;
  3444. }
  3445. hv_compose_msi_msg(irq_data, &entry->msg);
  3446. }
  3447. msi_unlock_descs(&pdev->dev);
  3448. return ret;
  3449. }
  3450. /*
  3451. * Upon resume, pci_restore_msi_state() -> ... -> __pci_write_msi_msg()
  3452. * directly writes the MSI/MSI-X registers via MMIO, but since Hyper-V
  3453. * doesn't trap and emulate the MMIO accesses, here hv_compose_msi_msg()
  3454. * must be used to ask Hyper-V to re-create the IOMMU Interrupt Remapping
  3455. * Table entries.
  3456. */
  3457. static void hv_pci_restore_msi_state(struct hv_pcibus_device *hbus)
  3458. {
  3459. pci_walk_bus(hbus->bridge->bus, hv_pci_restore_msi_msg, NULL);
  3460. }
  3461. static int hv_pci_resume(struct hv_device *hdev)
  3462. {
  3463. struct hv_pcibus_device *hbus = hv_get_drvdata(hdev);
  3464. enum pci_protocol_version_t version[1];
  3465. int ret;
  3466. hbus->state = hv_pcibus_init;
  3467. hdev->channel->next_request_id_callback = vmbus_next_request_id;
  3468. hdev->channel->request_addr_callback = vmbus_request_addr;
  3469. hdev->channel->rqstor_size = HV_PCI_RQSTOR_SIZE;
  3470. ret = vmbus_open(hdev->channel, pci_ring_size, pci_ring_size, NULL, 0,
  3471. hv_pci_onchannelcallback, hbus);
  3472. if (ret)
  3473. return ret;
  3474. /* Only use the version that was in use before hibernation. */
  3475. version[0] = hbus->protocol_version;
  3476. ret = hv_pci_protocol_negotiation(hdev, version, 1);
  3477. if (ret)
  3478. goto out;
  3479. ret = hv_pci_query_relations(hdev);
  3480. if (ret)
  3481. goto out;
  3482. mutex_lock(&hbus->state_lock);
  3483. ret = hv_pci_enter_d0(hdev);
  3484. if (ret)
  3485. goto release_state_lock;
  3486. ret = hv_send_resources_allocated(hdev);
  3487. if (ret)
  3488. goto release_state_lock;
  3489. prepopulate_bars(hbus);
  3490. hv_pci_restore_msi_state(hbus);
  3491. hbus->state = hv_pcibus_installed;
  3492. mutex_unlock(&hbus->state_lock);
  3493. return 0;
  3494. release_state_lock:
  3495. mutex_unlock(&hbus->state_lock);
  3496. out:
  3497. vmbus_close(hdev->channel);
  3498. return ret;
  3499. }
  3500. static const struct hv_vmbus_device_id hv_pci_id_table[] = {
  3501. /* PCI Pass-through Class ID */
  3502. /* 44C4F61D-4444-4400-9D52-802E27EDE19F */
  3503. { HV_PCIE_GUID, },
  3504. { },
  3505. };
  3506. MODULE_DEVICE_TABLE(vmbus, hv_pci_id_table);
  3507. static struct hv_driver hv_pci_drv = {
  3508. .name = "hv_pci",
  3509. .id_table = hv_pci_id_table,
  3510. .probe = hv_pci_probe,
  3511. .remove = hv_pci_remove,
  3512. .suspend = hv_pci_suspend,
  3513. .resume = hv_pci_resume,
  3514. };
  3515. static void __exit exit_hv_pci_drv(void)
  3516. {
  3517. vmbus_driver_unregister(&hv_pci_drv);
  3518. hvpci_block_ops.read_block = NULL;
  3519. hvpci_block_ops.write_block = NULL;
  3520. hvpci_block_ops.reg_blk_invalidate = NULL;
  3521. }
  3522. static int __init init_hv_pci_drv(void)
  3523. {
  3524. int ret;
  3525. if (!hv_is_hyperv_initialized())
  3526. return -ENODEV;
  3527. ret = hv_pci_irqchip_init();
  3528. if (ret)
  3529. return ret;
  3530. /* Set the invalid domain number's bit, so it will not be used */
  3531. set_bit(HVPCI_DOM_INVALID, hvpci_dom_map);
  3532. /* Initialize PCI block r/w interface */
  3533. hvpci_block_ops.read_block = hv_read_config_block;
  3534. hvpci_block_ops.write_block = hv_write_config_block;
  3535. hvpci_block_ops.reg_blk_invalidate = hv_register_block_invalidate;
  3536. return vmbus_driver_register(&hv_pci_drv);
  3537. }
  3538. module_init(init_hv_pci_drv);
  3539. module_exit(exit_hv_pci_drv);
  3540. MODULE_DESCRIPTION("Hyper-V PCI");
  3541. MODULE_LICENSE("GPL v2");