pci-hyperv.c 74 KB

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