vfio_ap_ops.c 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Adjunct processor matrix VFIO device driver callbacks.
  4. *
  5. * Copyright IBM Corp. 2018
  6. *
  7. * Author(s): Tony Krowiak <akrowiak@linux.ibm.com>
  8. * Halil Pasic <pasic@linux.ibm.com>
  9. * Pierre Morel <pmorel@linux.ibm.com>
  10. */
  11. #include <linux/string.h>
  12. #include <linux/vfio.h>
  13. #include <linux/device.h>
  14. #include <linux/list.h>
  15. #include <linux/ctype.h>
  16. #include <linux/bitops.h>
  17. #include <linux/kvm_host.h>
  18. #include <linux/module.h>
  19. #include <linux/uuid.h>
  20. #include <asm/kvm.h>
  21. #include <asm/zcrypt.h>
  22. #include "vfio_ap_private.h"
  23. #include "vfio_ap_debug.h"
  24. #define VFIO_AP_MDEV_TYPE_HWVIRT "passthrough"
  25. #define VFIO_AP_MDEV_NAME_HWVIRT "VFIO AP Passthrough Device"
  26. #define AP_QUEUE_ASSIGNED "assigned"
  27. #define AP_QUEUE_UNASSIGNED "unassigned"
  28. #define AP_QUEUE_IN_USE "in use"
  29. #define AP_RESET_INTERVAL 20 /* Reset sleep interval (20ms) */
  30. static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev);
  31. static int vfio_ap_mdev_reset_qlist(struct list_head *qlist);
  32. static struct vfio_ap_queue *vfio_ap_find_queue(int apqn);
  33. static const struct vfio_device_ops vfio_ap_matrix_dev_ops;
  34. static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q);
  35. /**
  36. * get_update_locks_for_kvm: Acquire the locks required to dynamically update a
  37. * KVM guest's APCB in the proper order.
  38. *
  39. * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
  40. *
  41. * The proper locking order is:
  42. * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
  43. * guest's APCB.
  44. * 2. kvm->lock: required to update a guest's APCB
  45. * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
  46. *
  47. * Note: If @kvm is NULL, the KVM lock will not be taken.
  48. */
  49. static inline void get_update_locks_for_kvm(struct kvm *kvm)
  50. {
  51. mutex_lock(&matrix_dev->guests_lock);
  52. if (kvm)
  53. mutex_lock(&kvm->lock);
  54. mutex_lock(&matrix_dev->mdevs_lock);
  55. }
  56. /**
  57. * release_update_locks_for_kvm: Release the locks used to dynamically update a
  58. * KVM guest's APCB in the proper order.
  59. *
  60. * @kvm: a pointer to a struct kvm object containing the KVM guest's APCB.
  61. *
  62. * The proper unlocking order is:
  63. * 1. matrix_dev->mdevs_lock
  64. * 2. kvm->lock
  65. * 3. matrix_dev->guests_lock
  66. *
  67. * Note: If @kvm is NULL, the KVM lock will not be released.
  68. */
  69. static inline void release_update_locks_for_kvm(struct kvm *kvm)
  70. {
  71. mutex_unlock(&matrix_dev->mdevs_lock);
  72. if (kvm)
  73. mutex_unlock(&kvm->lock);
  74. mutex_unlock(&matrix_dev->guests_lock);
  75. }
  76. /**
  77. * get_update_locks_for_mdev: Acquire the locks required to dynamically update a
  78. * KVM guest's APCB in the proper order.
  79. *
  80. * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
  81. * configuration data to use to update a KVM guest's APCB.
  82. *
  83. * The proper locking order is:
  84. * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
  85. * guest's APCB.
  86. * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
  87. * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
  88. *
  89. * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
  90. * lock will not be taken.
  91. */
  92. static inline void get_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
  93. {
  94. mutex_lock(&matrix_dev->guests_lock);
  95. if (matrix_mdev && matrix_mdev->kvm)
  96. mutex_lock(&matrix_mdev->kvm->lock);
  97. mutex_lock(&matrix_dev->mdevs_lock);
  98. }
  99. /**
  100. * release_update_locks_for_mdev: Release the locks used to dynamically update a
  101. * KVM guest's APCB in the proper order.
  102. *
  103. * @matrix_mdev: a pointer to a struct ap_matrix_mdev object containing the AP
  104. * configuration data to use to update a KVM guest's APCB.
  105. *
  106. * The proper unlocking order is:
  107. * 1. matrix_dev->mdevs_lock
  108. * 2. matrix_mdev->kvm->lock
  109. * 3. matrix_dev->guests_lock
  110. *
  111. * Note: If @matrix_mdev is NULL or is not attached to a KVM guest, the KVM
  112. * lock will not be released.
  113. */
  114. static inline void release_update_locks_for_mdev(struct ap_matrix_mdev *matrix_mdev)
  115. {
  116. mutex_unlock(&matrix_dev->mdevs_lock);
  117. if (matrix_mdev && matrix_mdev->kvm)
  118. mutex_unlock(&matrix_mdev->kvm->lock);
  119. mutex_unlock(&matrix_dev->guests_lock);
  120. }
  121. /**
  122. * get_update_locks_by_apqn: Find the mdev to which an APQN is assigned and
  123. * acquire the locks required to update the APCB of
  124. * the KVM guest to which the mdev is attached.
  125. *
  126. * @apqn: the APQN of a queue device.
  127. *
  128. * The proper locking order is:
  129. * 1. matrix_dev->guests_lock: required to use the KVM pointer to update a KVM
  130. * guest's APCB.
  131. * 2. matrix_mdev->kvm->lock: required to update a guest's APCB
  132. * 3. matrix_dev->mdevs_lock: required to access data stored in a matrix_mdev
  133. *
  134. * Note: If @apqn is not assigned to a matrix_mdev, the matrix_mdev->kvm->lock
  135. * will not be taken.
  136. *
  137. * Return: the ap_matrix_mdev object to which @apqn is assigned or NULL if @apqn
  138. * is not assigned to an ap_matrix_mdev.
  139. */
  140. static struct ap_matrix_mdev *get_update_locks_by_apqn(int apqn)
  141. {
  142. struct ap_matrix_mdev *matrix_mdev;
  143. mutex_lock(&matrix_dev->guests_lock);
  144. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  145. if (test_bit_inv(AP_QID_CARD(apqn), matrix_mdev->matrix.apm) &&
  146. test_bit_inv(AP_QID_QUEUE(apqn), matrix_mdev->matrix.aqm)) {
  147. if (matrix_mdev->kvm)
  148. mutex_lock(&matrix_mdev->kvm->lock);
  149. mutex_lock(&matrix_dev->mdevs_lock);
  150. return matrix_mdev;
  151. }
  152. }
  153. mutex_lock(&matrix_dev->mdevs_lock);
  154. return NULL;
  155. }
  156. /**
  157. * get_update_locks_for_queue: get the locks required to update the APCB of the
  158. * KVM guest to which the matrix mdev linked to a
  159. * vfio_ap_queue object is attached.
  160. *
  161. * @q: a pointer to a vfio_ap_queue object.
  162. *
  163. * The proper locking order is:
  164. * 1. q->matrix_dev->guests_lock: required to use the KVM pointer to update a
  165. * KVM guest's APCB.
  166. * 2. q->matrix_mdev->kvm->lock: required to update a guest's APCB
  167. * 3. matrix_dev->mdevs_lock: required to access data stored in matrix_mdev
  168. *
  169. * Note: if @queue is not linked to an ap_matrix_mdev object, the KVM lock
  170. * will not be taken.
  171. */
  172. static inline void get_update_locks_for_queue(struct vfio_ap_queue *q)
  173. {
  174. mutex_lock(&matrix_dev->guests_lock);
  175. if (q->matrix_mdev && q->matrix_mdev->kvm)
  176. mutex_lock(&q->matrix_mdev->kvm->lock);
  177. mutex_lock(&matrix_dev->mdevs_lock);
  178. }
  179. /**
  180. * vfio_ap_mdev_get_queue - retrieve a queue with a specific APQN from a
  181. * hash table of queues assigned to a matrix mdev
  182. * @matrix_mdev: the matrix mdev
  183. * @apqn: The APQN of a queue device
  184. *
  185. * Return: the pointer to the vfio_ap_queue struct representing the queue or
  186. * NULL if the queue is not assigned to @matrix_mdev
  187. */
  188. static struct vfio_ap_queue *vfio_ap_mdev_get_queue(
  189. struct ap_matrix_mdev *matrix_mdev,
  190. int apqn)
  191. {
  192. struct vfio_ap_queue *q;
  193. hash_for_each_possible(matrix_mdev->qtable.queues, q, mdev_qnode,
  194. apqn) {
  195. if (q && q->apqn == apqn)
  196. return q;
  197. }
  198. return NULL;
  199. }
  200. /**
  201. * vfio_ap_wait_for_irqclear - clears the IR bit or gives up after 5 tries
  202. * @apqn: The AP Queue number
  203. *
  204. * Checks the IRQ bit for the status of this APQN using ap_tapq.
  205. * Returns if the ap_tapq function succeeded and the bit is clear.
  206. * Returns if ap_tapq function failed with invalid, deconfigured or
  207. * checkstopped AP.
  208. * Otherwise retries up to 5 times after waiting 20ms.
  209. */
  210. static void vfio_ap_wait_for_irqclear(int apqn)
  211. {
  212. struct ap_queue_status status;
  213. int retry = 5;
  214. do {
  215. status = ap_tapq(apqn, NULL);
  216. switch (status.response_code) {
  217. case AP_RESPONSE_NORMAL:
  218. case AP_RESPONSE_RESET_IN_PROGRESS:
  219. if (!status.irq_enabled)
  220. return;
  221. fallthrough;
  222. case AP_RESPONSE_BUSY:
  223. msleep(20);
  224. break;
  225. case AP_RESPONSE_Q_NOT_AVAIL:
  226. case AP_RESPONSE_DECONFIGURED:
  227. case AP_RESPONSE_CHECKSTOPPED:
  228. default:
  229. WARN_ONCE(1, "%s: tapq rc %02x: %04x\n", __func__,
  230. status.response_code, apqn);
  231. return;
  232. }
  233. } while (--retry);
  234. WARN_ONCE(1, "%s: tapq rc %02x: %04x could not clear IR bit\n",
  235. __func__, status.response_code, apqn);
  236. }
  237. /**
  238. * vfio_ap_free_aqic_resources - free vfio_ap_queue resources
  239. * @q: The vfio_ap_queue
  240. *
  241. * Unregisters the ISC in the GIB when the saved ISC not invalid.
  242. * Unpins the guest's page holding the NIB when it exists.
  243. * Resets the saved_iova and saved_isc to invalid values.
  244. */
  245. static void vfio_ap_free_aqic_resources(struct vfio_ap_queue *q)
  246. {
  247. if (!q)
  248. return;
  249. if (q->saved_isc != VFIO_AP_ISC_INVALID &&
  250. !WARN_ON(!(q->matrix_mdev && q->matrix_mdev->kvm))) {
  251. kvm_s390_gisc_unregister(q->matrix_mdev->kvm, q->saved_isc);
  252. q->saved_isc = VFIO_AP_ISC_INVALID;
  253. }
  254. if (q->saved_iova && !WARN_ON(!q->matrix_mdev)) {
  255. vfio_unpin_pages(&q->matrix_mdev->vdev, q->saved_iova, 1);
  256. q->saved_iova = 0;
  257. }
  258. }
  259. /**
  260. * vfio_ap_irq_disable - disables and clears an ap_queue interrupt
  261. * @q: The vfio_ap_queue
  262. *
  263. * Uses ap_aqic to disable the interruption and in case of success, reset
  264. * in progress or IRQ disable command already proceeded: calls
  265. * vfio_ap_wait_for_irqclear() to check for the IRQ bit to be clear
  266. * and calls vfio_ap_free_aqic_resources() to free the resources associated
  267. * with the AP interrupt handling.
  268. *
  269. * In the case the AP is busy, or a reset is in progress,
  270. * retries after 20ms, up to 5 times.
  271. *
  272. * Returns if ap_aqic function failed with invalid, deconfigured or
  273. * checkstopped AP.
  274. *
  275. * Return: &struct ap_queue_status
  276. */
  277. static struct ap_queue_status vfio_ap_irq_disable(struct vfio_ap_queue *q)
  278. {
  279. union ap_qirq_ctrl aqic_gisa = { .value = 0 };
  280. struct ap_queue_status status;
  281. int retries = 5;
  282. do {
  283. status = ap_aqic(q->apqn, aqic_gisa, 0);
  284. switch (status.response_code) {
  285. case AP_RESPONSE_OTHERWISE_CHANGED:
  286. case AP_RESPONSE_NORMAL:
  287. vfio_ap_wait_for_irqclear(q->apqn);
  288. goto end_free;
  289. case AP_RESPONSE_RESET_IN_PROGRESS:
  290. case AP_RESPONSE_BUSY:
  291. msleep(20);
  292. break;
  293. case AP_RESPONSE_Q_NOT_AVAIL:
  294. case AP_RESPONSE_DECONFIGURED:
  295. case AP_RESPONSE_CHECKSTOPPED:
  296. case AP_RESPONSE_INVALID_ADDRESS:
  297. default:
  298. /* All cases in default means AP not operational */
  299. WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
  300. status.response_code);
  301. goto end_free;
  302. }
  303. } while (retries--);
  304. WARN_ONCE(1, "%s: ap_aqic status %d\n", __func__,
  305. status.response_code);
  306. end_free:
  307. vfio_ap_free_aqic_resources(q);
  308. return status;
  309. }
  310. /**
  311. * vfio_ap_validate_nib - validate a notification indicator byte (nib) address.
  312. *
  313. * @vcpu: the object representing the vcpu executing the PQAP(AQIC) instruction.
  314. * @nib: the location for storing the nib address.
  315. *
  316. * When the PQAP(AQIC) instruction is executed, general register 2 contains the
  317. * address of the notification indicator byte (nib) used for IRQ notification.
  318. * This function parses and validates the nib from gr2.
  319. *
  320. * Return: returns zero if the nib address is a valid; otherwise, returns
  321. * -EINVAL.
  322. */
  323. static int vfio_ap_validate_nib(struct kvm_vcpu *vcpu, dma_addr_t *nib)
  324. {
  325. *nib = vcpu->run->s.regs.gprs[2];
  326. if (!*nib)
  327. return -EINVAL;
  328. if (!kvm_s390_is_gpa_in_memslot(vcpu->kvm, *nib))
  329. return -EINVAL;
  330. return 0;
  331. }
  332. /**
  333. * ensure_nib_shared() - Ensure the address of the NIB is secure and shared
  334. * @addr: the physical (absolute) address of the NIB
  335. *
  336. * This function checks whether the NIB page, which has been pinned with
  337. * vfio_pin_pages(), is a shared page belonging to a secure guest.
  338. *
  339. * It will call uv_pin_shared() on it; if the page was already pinned shared
  340. * (i.e. if the NIB belongs to a secure guest and is shared), then 0
  341. * (success) is returned. If the NIB was not shared, vfio_pin_pages() had
  342. * exported it and now it does not belong to the secure guest anymore. In
  343. * that case, an error is returned.
  344. *
  345. * Context: the NIB (at physical address @addr) has to be pinned with
  346. * vfio_pin_pages() before calling this function.
  347. *
  348. * Return: 0 in case of success, otherwise an error < 0.
  349. */
  350. static int ensure_nib_shared(unsigned long addr)
  351. {
  352. /*
  353. * The nib has to be located in shared storage since guest and
  354. * host access it. vfio_pin_pages() will do a pin shared and
  355. * if that fails (possibly because it's not a shared page) it
  356. * calls export. We try to do a second pin shared here so that
  357. * the UV gives us an error code if we try to pin a non-shared
  358. * page.
  359. *
  360. * If the page is already pinned shared the UV will return a success.
  361. */
  362. return uv_pin_shared(addr);
  363. }
  364. /**
  365. * vfio_ap_irq_enable - Enable Interruption for a APQN
  366. *
  367. * @q: the vfio_ap_queue holding AQIC parameters
  368. * @isc: the guest ISC to register with the GIB interface
  369. * @vcpu: the vcpu object containing the registers specifying the parameters
  370. * passed to the PQAP(AQIC) instruction.
  371. *
  372. * Pin the NIB saved in *q
  373. * Register the guest ISC to GIB interface and retrieve the
  374. * host ISC to issue the host side PQAP/AQIC
  375. *
  376. * status.response_code may be set to AP_RESPONSE_INVALID_ADDRESS in case the
  377. * vfio_pin_pages or kvm_s390_gisc_register failed.
  378. *
  379. * Otherwise return the ap_queue_status returned by the ap_aqic(),
  380. * all retry handling will be done by the guest.
  381. *
  382. * Return: &struct ap_queue_status
  383. */
  384. static struct ap_queue_status vfio_ap_irq_enable(struct vfio_ap_queue *q,
  385. int isc,
  386. struct kvm_vcpu *vcpu)
  387. {
  388. union ap_qirq_ctrl aqic_gisa = { .value = 0 };
  389. struct ap_queue_status status = {};
  390. struct kvm_s390_gisa *gisa;
  391. struct page *h_page;
  392. int nisc;
  393. struct kvm *kvm;
  394. phys_addr_t h_nib;
  395. dma_addr_t nib;
  396. int ret;
  397. /* Verify that the notification indicator byte address is valid */
  398. if (vfio_ap_validate_nib(vcpu, &nib)) {
  399. VFIO_AP_DBF_WARN("%s: invalid NIB address: nib=%pad, apqn=%#04x\n",
  400. __func__, &nib, q->apqn);
  401. status.response_code = AP_RESPONSE_INVALID_ADDRESS;
  402. return status;
  403. }
  404. /* The pin will probably be successful even if the NIB was not shared */
  405. ret = vfio_pin_pages(&q->matrix_mdev->vdev, nib, 1,
  406. IOMMU_READ | IOMMU_WRITE, &h_page);
  407. switch (ret) {
  408. case 1:
  409. break;
  410. default:
  411. VFIO_AP_DBF_WARN("%s: vfio_pin_pages failed: rc=%d,"
  412. "nib=%pad, apqn=%#04x\n",
  413. __func__, ret, &nib, q->apqn);
  414. status.response_code = AP_RESPONSE_INVALID_ADDRESS;
  415. return status;
  416. }
  417. kvm = q->matrix_mdev->kvm;
  418. gisa = kvm->arch.gisa_int.origin;
  419. h_nib = page_to_phys(h_page) | (nib & ~PAGE_MASK);
  420. aqic_gisa.gisc = isc;
  421. /* NIB in non-shared storage is a rc 6 for PV guests */
  422. if (kvm_s390_pv_cpu_is_protected(vcpu) &&
  423. ensure_nib_shared(h_nib & PAGE_MASK)) {
  424. vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
  425. status.response_code = AP_RESPONSE_INVALID_ADDRESS;
  426. return status;
  427. }
  428. nisc = kvm_s390_gisc_register(kvm, isc);
  429. if (nisc < 0) {
  430. VFIO_AP_DBF_WARN("%s: gisc registration failed: nisc=%d, isc=%d, apqn=%#04x\n",
  431. __func__, nisc, isc, q->apqn);
  432. vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
  433. status.response_code = AP_RESPONSE_INVALID_ADDRESS;
  434. return status;
  435. }
  436. aqic_gisa.isc = nisc;
  437. aqic_gisa.ir = 1;
  438. aqic_gisa.gisa = virt_to_phys(gisa) >> 4;
  439. status = ap_aqic(q->apqn, aqic_gisa, h_nib);
  440. switch (status.response_code) {
  441. case AP_RESPONSE_NORMAL:
  442. /* See if we did clear older IRQ configuration */
  443. vfio_ap_free_aqic_resources(q);
  444. q->saved_iova = nib;
  445. q->saved_isc = isc;
  446. break;
  447. case AP_RESPONSE_OTHERWISE_CHANGED:
  448. /* We could not modify IRQ settings: clear new configuration */
  449. ret = kvm_s390_gisc_unregister(kvm, isc);
  450. if (ret)
  451. VFIO_AP_DBF_WARN("%s: kvm_s390_gisc_unregister: rc=%d isc=%d, apqn=%#04x\n",
  452. __func__, ret, isc, q->apqn);
  453. vfio_unpin_pages(&q->matrix_mdev->vdev, nib, 1);
  454. break;
  455. default:
  456. pr_warn("%s: apqn %04x: response: %02x\n", __func__, q->apqn,
  457. status.response_code);
  458. vfio_ap_irq_disable(q);
  459. break;
  460. }
  461. if (status.response_code != AP_RESPONSE_NORMAL) {
  462. VFIO_AP_DBF_WARN("%s: PQAP(AQIC) failed with status=%#02x: "
  463. "zone=%#x, ir=%#x, gisc=%#x, f=%#x,"
  464. "gisa=%#x, isc=%#x, apqn=%#04x\n",
  465. __func__, status.response_code,
  466. aqic_gisa.zone, aqic_gisa.ir, aqic_gisa.gisc,
  467. aqic_gisa.gf, aqic_gisa.gisa, aqic_gisa.isc,
  468. q->apqn);
  469. }
  470. return status;
  471. }
  472. /**
  473. * vfio_ap_le_guid_to_be_uuid - convert a little endian guid array into an array
  474. * of big endian elements that can be passed by
  475. * value to an s390dbf sprintf event function to
  476. * format a UUID string.
  477. *
  478. * @guid: the object containing the little endian guid
  479. * @uuid: a six-element array of long values that can be passed by value as
  480. * arguments for a formatting string specifying a UUID.
  481. *
  482. * The S390 Debug Feature (s390dbf) allows the use of "%s" in the sprintf
  483. * event functions if the memory for the passed string is available as long as
  484. * the debug feature exists. Since a mediated device can be removed at any
  485. * time, it's name can not be used because %s passes the reference to the string
  486. * in memory and the reference will go stale once the device is removed .
  487. *
  488. * The s390dbf string formatting function allows a maximum of 9 arguments for a
  489. * message to be displayed in the 'sprintf' view. In order to use the bytes
  490. * comprising the mediated device's UUID to display the mediated device name,
  491. * they will have to be converted into an array whose elements can be passed by
  492. * value to sprintf. For example:
  493. *
  494. * guid array: { 83, 78, 17, 62, bb, f1, f0, 47, 91, 4d, 32, a2, 2e, 3a, 88, 04 }
  495. * mdev name: 62177883-f1bb-47f0-914d-32a22e3a8804
  496. * array returned: { 62177883, f1bb, 47f0, 914d, 32a2, 2e3a8804 }
  497. * formatting string: "%08lx-%04lx-%04lx-%04lx-%02lx%04lx"
  498. */
  499. static void vfio_ap_le_guid_to_be_uuid(guid_t *guid, unsigned long *uuid)
  500. {
  501. /*
  502. * The input guid is ordered in little endian, so it needs to be
  503. * reordered for displaying a UUID as a string. This specifies the
  504. * guid indices in proper order.
  505. */
  506. uuid[0] = le32_to_cpup((__le32 *)guid);
  507. uuid[1] = le16_to_cpup((__le16 *)&guid->b[4]);
  508. uuid[2] = le16_to_cpup((__le16 *)&guid->b[6]);
  509. uuid[3] = *((__u16 *)&guid->b[8]);
  510. uuid[4] = *((__u16 *)&guid->b[10]);
  511. uuid[5] = *((__u32 *)&guid->b[12]);
  512. }
  513. /**
  514. * handle_pqap - PQAP instruction callback
  515. *
  516. * @vcpu: The vcpu on which we received the PQAP instruction
  517. *
  518. * Get the general register contents to initialize internal variables.
  519. * REG[0]: APQN
  520. * REG[1]: IR and ISC
  521. * REG[2]: NIB
  522. *
  523. * Response.status may be set to following Response Code:
  524. * - AP_RESPONSE_Q_NOT_AVAIL: if the queue is not available
  525. * - AP_RESPONSE_DECONFIGURED: if the queue is not configured
  526. * - AP_RESPONSE_NORMAL (0) : in case of success
  527. * Check vfio_ap_setirq() and vfio_ap_clrirq() for other possible RC.
  528. * We take the matrix_dev lock to ensure serialization on queues and
  529. * mediated device access.
  530. *
  531. * Return: 0 if we could handle the request inside KVM.
  532. * Otherwise, returns -EOPNOTSUPP to let QEMU handle the fault.
  533. */
  534. static int handle_pqap(struct kvm_vcpu *vcpu)
  535. {
  536. uint64_t status;
  537. uint16_t apqn;
  538. unsigned long uuid[6];
  539. struct vfio_ap_queue *q;
  540. struct ap_queue_status qstatus = {
  541. .response_code = AP_RESPONSE_Q_NOT_AVAIL, };
  542. struct ap_matrix_mdev *matrix_mdev;
  543. apqn = vcpu->run->s.regs.gprs[0] & 0xffff;
  544. /* If we do not use the AIV facility just go to userland */
  545. if (!(vcpu->arch.sie_block->eca & ECA_AIV)) {
  546. VFIO_AP_DBF_WARN("%s: AIV facility not installed: apqn=0x%04x, eca=0x%04x\n",
  547. __func__, apqn, vcpu->arch.sie_block->eca);
  548. return -EOPNOTSUPP;
  549. }
  550. mutex_lock(&matrix_dev->mdevs_lock);
  551. if (!vcpu->kvm->arch.crypto.pqap_hook) {
  552. VFIO_AP_DBF_WARN("%s: PQAP(AQIC) hook not registered with the vfio_ap driver: apqn=0x%04x\n",
  553. __func__, apqn);
  554. goto out_unlock;
  555. }
  556. matrix_mdev = container_of(vcpu->kvm->arch.crypto.pqap_hook,
  557. struct ap_matrix_mdev, pqap_hook);
  558. /* If the there is no guest using the mdev, there is nothing to do */
  559. if (!matrix_mdev->kvm) {
  560. vfio_ap_le_guid_to_be_uuid(&matrix_mdev->mdev->uuid, uuid);
  561. VFIO_AP_DBF_WARN("%s: mdev %08lx-%04lx-%04lx-%04lx-%04lx%08lx not in use: apqn=0x%04x\n",
  562. __func__, uuid[0], uuid[1], uuid[2],
  563. uuid[3], uuid[4], uuid[5], apqn);
  564. goto out_unlock;
  565. }
  566. q = vfio_ap_mdev_get_queue(matrix_mdev, apqn);
  567. if (!q) {
  568. VFIO_AP_DBF_WARN("%s: Queue %02x.%04x not bound to the vfio_ap driver\n",
  569. __func__, AP_QID_CARD(apqn),
  570. AP_QID_QUEUE(apqn));
  571. goto out_unlock;
  572. }
  573. status = vcpu->run->s.regs.gprs[1];
  574. /* If IR bit(16) is set we enable the interrupt */
  575. if ((status >> (63 - 16)) & 0x01)
  576. qstatus = vfio_ap_irq_enable(q, status & 0x07, vcpu);
  577. else
  578. qstatus = vfio_ap_irq_disable(q);
  579. out_unlock:
  580. memcpy(&vcpu->run->s.regs.gprs[1], &qstatus, sizeof(qstatus));
  581. vcpu->run->s.regs.gprs[1] >>= 32;
  582. mutex_unlock(&matrix_dev->mdevs_lock);
  583. return 0;
  584. }
  585. static void vfio_ap_matrix_init(struct ap_config_info *info,
  586. struct ap_matrix *matrix)
  587. {
  588. matrix->apm_max = info->apxa ? info->na : 63;
  589. matrix->aqm_max = info->apxa ? info->nd : 15;
  590. matrix->adm_max = info->apxa ? info->nd : 15;
  591. }
  592. static void signal_guest_ap_cfg_changed(struct ap_matrix_mdev *matrix_mdev)
  593. {
  594. if (matrix_mdev->cfg_chg_trigger)
  595. eventfd_signal(matrix_mdev->cfg_chg_trigger);
  596. }
  597. static void vfio_ap_mdev_update_guest_apcb(struct ap_matrix_mdev *matrix_mdev)
  598. {
  599. if (matrix_mdev->kvm) {
  600. kvm_arch_crypto_set_masks(matrix_mdev->kvm,
  601. matrix_mdev->shadow_apcb.apm,
  602. matrix_mdev->shadow_apcb.aqm,
  603. matrix_mdev->shadow_apcb.adm);
  604. signal_guest_ap_cfg_changed(matrix_mdev);
  605. }
  606. }
  607. static bool vfio_ap_mdev_filter_cdoms(struct ap_matrix_mdev *matrix_mdev)
  608. {
  609. DECLARE_BITMAP(prev_shadow_adm, AP_DOMAINS);
  610. bitmap_copy(prev_shadow_adm, matrix_mdev->shadow_apcb.adm, AP_DOMAINS);
  611. bitmap_and(matrix_mdev->shadow_apcb.adm, matrix_mdev->matrix.adm,
  612. (unsigned long *)matrix_dev->info.adm, AP_DOMAINS);
  613. return !bitmap_equal(prev_shadow_adm, matrix_mdev->shadow_apcb.adm,
  614. AP_DOMAINS);
  615. }
  616. static bool _queue_passable(struct vfio_ap_queue *q)
  617. {
  618. if (!q)
  619. return false;
  620. switch (q->reset_status.response_code) {
  621. case AP_RESPONSE_NORMAL:
  622. case AP_RESPONSE_DECONFIGURED:
  623. case AP_RESPONSE_CHECKSTOPPED:
  624. return true;
  625. default:
  626. return false;
  627. }
  628. }
  629. /*
  630. * vfio_ap_mdev_filter_matrix - filter the APQNs assigned to the matrix mdev
  631. * to ensure no queue devices are passed through to
  632. * the guest that are not bound to the vfio_ap
  633. * device driver.
  634. *
  635. * @matrix_mdev: the matrix mdev whose matrix is to be filtered.
  636. * @apm_filtered: a 256-bit bitmap for storing the APIDs filtered from the
  637. * guest's AP configuration that are still in the host's AP
  638. * configuration.
  639. *
  640. * Note: If an APQN referencing a queue device that is not bound to the vfio_ap
  641. * driver, its APID will be filtered from the guest's APCB. The matrix
  642. * structure precludes filtering an individual APQN, so its APID will be
  643. * filtered. Consequently, all queues associated with the adapter that
  644. * are in the host's AP configuration must be reset. If queues are
  645. * subsequently made available again to the guest, they should re-appear
  646. * in a reset state
  647. *
  648. * Return: a boolean value indicating whether the KVM guest's APCB was changed
  649. * by the filtering or not.
  650. */
  651. static bool vfio_ap_mdev_filter_matrix(struct ap_matrix_mdev *matrix_mdev,
  652. unsigned long *apm_filtered)
  653. {
  654. unsigned long apid, apqi, apqn;
  655. DECLARE_BITMAP(prev_shadow_apm, AP_DEVICES);
  656. DECLARE_BITMAP(prev_shadow_aqm, AP_DOMAINS);
  657. bitmap_copy(prev_shadow_apm, matrix_mdev->shadow_apcb.apm, AP_DEVICES);
  658. bitmap_copy(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS);
  659. vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
  660. bitmap_clear(apm_filtered, 0, AP_DEVICES);
  661. /*
  662. * Copy the adapters, domains and control domains to the shadow_apcb
  663. * from the matrix mdev, but only those that are assigned to the host's
  664. * AP configuration.
  665. */
  666. bitmap_and(matrix_mdev->shadow_apcb.apm, matrix_mdev->matrix.apm,
  667. (unsigned long *)matrix_dev->info.apm, AP_DEVICES);
  668. bitmap_and(matrix_mdev->shadow_apcb.aqm, matrix_mdev->matrix.aqm,
  669. (unsigned long *)matrix_dev->info.aqm, AP_DOMAINS);
  670. for_each_set_bit_inv(apid, matrix_mdev->shadow_apcb.apm, AP_DEVICES) {
  671. for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm,
  672. AP_DOMAINS) {
  673. /*
  674. * If the APQN is not bound to the vfio_ap device
  675. * driver, then we can't assign it to the guest's
  676. * AP configuration. The AP architecture won't
  677. * allow filtering of a single APQN, so let's filter
  678. * the APID since an adapter represents a physical
  679. * hardware device.
  680. */
  681. apqn = AP_MKQID(apid, apqi);
  682. if (!_queue_passable(vfio_ap_mdev_get_queue(matrix_mdev, apqn))) {
  683. clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
  684. /*
  685. * If the adapter was previously plugged into
  686. * the guest, let's let the caller know that
  687. * the APID was filtered.
  688. */
  689. if (test_bit_inv(apid, prev_shadow_apm))
  690. set_bit_inv(apid, apm_filtered);
  691. break;
  692. }
  693. }
  694. }
  695. return !bitmap_equal(prev_shadow_apm, matrix_mdev->shadow_apcb.apm,
  696. AP_DEVICES) ||
  697. !bitmap_equal(prev_shadow_aqm, matrix_mdev->shadow_apcb.aqm,
  698. AP_DOMAINS);
  699. }
  700. static int vfio_ap_mdev_init_dev(struct vfio_device *vdev)
  701. {
  702. struct ap_matrix_mdev *matrix_mdev =
  703. container_of(vdev, struct ap_matrix_mdev, vdev);
  704. matrix_mdev->mdev = to_mdev_device(vdev->dev);
  705. vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->matrix);
  706. matrix_mdev->pqap_hook = handle_pqap;
  707. vfio_ap_matrix_init(&matrix_dev->info, &matrix_mdev->shadow_apcb);
  708. hash_init(matrix_mdev->qtable.queues);
  709. return 0;
  710. }
  711. static int vfio_ap_mdev_probe(struct mdev_device *mdev)
  712. {
  713. struct ap_matrix_mdev *matrix_mdev;
  714. int ret;
  715. matrix_mdev = vfio_alloc_device(ap_matrix_mdev, vdev, &mdev->dev,
  716. &vfio_ap_matrix_dev_ops);
  717. if (IS_ERR(matrix_mdev))
  718. return PTR_ERR(matrix_mdev);
  719. ret = vfio_register_emulated_iommu_dev(&matrix_mdev->vdev);
  720. if (ret)
  721. goto err_put_vdev;
  722. matrix_mdev->req_trigger = NULL;
  723. matrix_mdev->cfg_chg_trigger = NULL;
  724. dev_set_drvdata(&mdev->dev, matrix_mdev);
  725. mutex_lock(&matrix_dev->mdevs_lock);
  726. list_add(&matrix_mdev->node, &matrix_dev->mdev_list);
  727. mutex_unlock(&matrix_dev->mdevs_lock);
  728. return 0;
  729. err_put_vdev:
  730. vfio_put_device(&matrix_mdev->vdev);
  731. return ret;
  732. }
  733. static void vfio_ap_mdev_link_queue(struct ap_matrix_mdev *matrix_mdev,
  734. struct vfio_ap_queue *q)
  735. {
  736. if (!q || vfio_ap_mdev_get_queue(matrix_mdev, q->apqn))
  737. return;
  738. q->matrix_mdev = matrix_mdev;
  739. hash_add(matrix_mdev->qtable.queues, &q->mdev_qnode, q->apqn);
  740. }
  741. static void vfio_ap_mdev_link_apqn(struct ap_matrix_mdev *matrix_mdev, int apqn)
  742. {
  743. struct vfio_ap_queue *q;
  744. q = vfio_ap_find_queue(apqn);
  745. vfio_ap_mdev_link_queue(matrix_mdev, q);
  746. }
  747. static void vfio_ap_unlink_queue_fr_mdev(struct vfio_ap_queue *q)
  748. {
  749. hash_del(&q->mdev_qnode);
  750. }
  751. static void vfio_ap_unlink_mdev_fr_queue(struct vfio_ap_queue *q)
  752. {
  753. q->matrix_mdev = NULL;
  754. }
  755. static void vfio_ap_mdev_unlink_fr_queues(struct ap_matrix_mdev *matrix_mdev)
  756. {
  757. struct vfio_ap_queue *q;
  758. unsigned long apid, apqi;
  759. for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
  760. for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm,
  761. AP_DOMAINS) {
  762. q = vfio_ap_mdev_get_queue(matrix_mdev,
  763. AP_MKQID(apid, apqi));
  764. if (q)
  765. q->matrix_mdev = NULL;
  766. }
  767. }
  768. }
  769. static void vfio_ap_mdev_remove(struct mdev_device *mdev)
  770. {
  771. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(&mdev->dev);
  772. vfio_unregister_group_dev(&matrix_mdev->vdev);
  773. mutex_lock(&matrix_dev->guests_lock);
  774. mutex_lock(&matrix_dev->mdevs_lock);
  775. vfio_ap_mdev_reset_queues(matrix_mdev);
  776. vfio_ap_mdev_unlink_fr_queues(matrix_mdev);
  777. list_del(&matrix_mdev->node);
  778. mutex_unlock(&matrix_dev->mdevs_lock);
  779. mutex_unlock(&matrix_dev->guests_lock);
  780. vfio_put_device(&matrix_mdev->vdev);
  781. }
  782. #define MDEV_SHARING_ERR "Userspace may not assign queue %02lx.%04lx to mdev: already assigned to %s"
  783. #define MDEV_IN_USE_ERR "Can not reserve queue %02lx.%04lx for host driver: in use by mdev"
  784. static void vfio_ap_mdev_log_sharing_err(struct ap_matrix_mdev *assignee,
  785. struct ap_matrix_mdev *assigned_to,
  786. unsigned long *apm, unsigned long *aqm)
  787. {
  788. unsigned long apid, apqi;
  789. for_each_set_bit_inv(apid, apm, AP_DEVICES) {
  790. for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
  791. dev_warn(mdev_dev(assignee->mdev), MDEV_SHARING_ERR,
  792. apid, apqi, dev_name(mdev_dev(assigned_to->mdev)));
  793. }
  794. }
  795. }
  796. static void vfio_ap_mdev_log_in_use_err(struct ap_matrix_mdev *assignee,
  797. unsigned long *apm, unsigned long *aqm)
  798. {
  799. unsigned long apid, apqi;
  800. for_each_set_bit_inv(apid, apm, AP_DEVICES) {
  801. for_each_set_bit_inv(apqi, aqm, AP_DOMAINS)
  802. dev_warn(mdev_dev(assignee->mdev), MDEV_IN_USE_ERR, apid, apqi);
  803. }
  804. }
  805. /**
  806. * vfio_ap_mdev_verify_no_sharing - verify APQNs are not shared by matrix mdevs
  807. *
  808. * @assignee: the matrix mdev to which @mdev_apm and @mdev_aqm are being
  809. * assigned; or, NULL if this function was called by the AP bus
  810. * driver in_use callback to verify none of the APQNs being reserved
  811. * for the host device driver are in use by a vfio_ap mediated device
  812. * @mdev_apm: mask indicating the APIDs of the APQNs to be verified
  813. * @mdev_aqm: mask indicating the APQIs of the APQNs to be verified
  814. *
  815. * Verifies that each APQN derived from the Cartesian product of APIDs
  816. * represented by the bits set in @mdev_apm and the APQIs of the bits set in
  817. * @mdev_aqm is not assigned to a mediated device other than the mdev to which
  818. * the APQN is being assigned (@assignee). AP queue sharing is not allowed.
  819. *
  820. * Return: 0 if the APQNs are not shared; otherwise return -EADDRINUSE.
  821. */
  822. static int vfio_ap_mdev_verify_no_sharing(struct ap_matrix_mdev *assignee,
  823. unsigned long *mdev_apm,
  824. unsigned long *mdev_aqm)
  825. {
  826. struct ap_matrix_mdev *assigned_to;
  827. DECLARE_BITMAP(apm, AP_DEVICES);
  828. DECLARE_BITMAP(aqm, AP_DOMAINS);
  829. list_for_each_entry(assigned_to, &matrix_dev->mdev_list, node) {
  830. /*
  831. * If the mdev to which the mdev_apm and mdev_aqm is being
  832. * assigned is the same as the mdev being verified
  833. */
  834. if (assignee == assigned_to)
  835. continue;
  836. memset(apm, 0, sizeof(apm));
  837. memset(aqm, 0, sizeof(aqm));
  838. /*
  839. * We work on full longs, as we can only exclude the leftover
  840. * bits in non-inverse order. The leftover is all zeros.
  841. */
  842. if (!bitmap_and(apm, mdev_apm, assigned_to->matrix.apm, AP_DEVICES))
  843. continue;
  844. if (!bitmap_and(aqm, mdev_aqm, assigned_to->matrix.aqm, AP_DOMAINS))
  845. continue;
  846. if (assignee)
  847. vfio_ap_mdev_log_sharing_err(assignee, assigned_to, apm, aqm);
  848. else
  849. vfio_ap_mdev_log_in_use_err(assigned_to, apm, aqm);
  850. return -EADDRINUSE;
  851. }
  852. return 0;
  853. }
  854. /**
  855. * vfio_ap_mdev_validate_masks - verify that the APQNs assigned to the mdev are
  856. * not reserved for the default zcrypt driver and
  857. * are not assigned to another mdev.
  858. *
  859. * @matrix_mdev: the mdev to which the APQNs being validated are assigned.
  860. *
  861. * Return: One of the following values:
  862. * o the error returned from the ap_apqn_in_matrix_owned_by_def_drv() function,
  863. * most likely -EBUSY indicating the ap_attr_mutex lock is already held.
  864. * o EADDRNOTAVAIL if an APQN assigned to @matrix_mdev is reserved for the
  865. * zcrypt default driver.
  866. * o EADDRINUSE if an APQN assigned to @matrix_mdev is assigned to another mdev
  867. * o A zero indicating validation succeeded.
  868. */
  869. static int vfio_ap_mdev_validate_masks(struct ap_matrix_mdev *matrix_mdev)
  870. {
  871. if (ap_apqn_in_matrix_owned_by_def_drv(matrix_mdev->matrix.apm,
  872. matrix_mdev->matrix.aqm))
  873. return -EADDRNOTAVAIL;
  874. return vfio_ap_mdev_verify_no_sharing(matrix_mdev,
  875. matrix_mdev->matrix.apm,
  876. matrix_mdev->matrix.aqm);
  877. }
  878. static void vfio_ap_mdev_link_adapter(struct ap_matrix_mdev *matrix_mdev,
  879. unsigned long apid)
  880. {
  881. unsigned long apqi;
  882. for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS)
  883. vfio_ap_mdev_link_apqn(matrix_mdev,
  884. AP_MKQID(apid, apqi));
  885. }
  886. static void collect_queues_to_reset(struct ap_matrix_mdev *matrix_mdev,
  887. unsigned long apid,
  888. struct list_head *qlist)
  889. {
  890. struct vfio_ap_queue *q;
  891. unsigned long apqi;
  892. for_each_set_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm, AP_DOMAINS) {
  893. q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
  894. if (q)
  895. list_add_tail(&q->reset_qnode, qlist);
  896. }
  897. }
  898. static void reset_queues_for_apid(struct ap_matrix_mdev *matrix_mdev,
  899. unsigned long apid)
  900. {
  901. struct list_head qlist;
  902. INIT_LIST_HEAD(&qlist);
  903. collect_queues_to_reset(matrix_mdev, apid, &qlist);
  904. vfio_ap_mdev_reset_qlist(&qlist);
  905. }
  906. static int reset_queues_for_apids(struct ap_matrix_mdev *matrix_mdev,
  907. unsigned long *apm_reset)
  908. {
  909. struct list_head qlist;
  910. unsigned long apid;
  911. if (bitmap_empty(apm_reset, AP_DEVICES))
  912. return 0;
  913. INIT_LIST_HEAD(&qlist);
  914. for_each_set_bit_inv(apid, apm_reset, AP_DEVICES)
  915. collect_queues_to_reset(matrix_mdev, apid, &qlist);
  916. return vfio_ap_mdev_reset_qlist(&qlist);
  917. }
  918. /**
  919. * assign_adapter_store - parses the APID from @buf and sets the
  920. * corresponding bit in the mediated matrix device's APM
  921. *
  922. * @dev: the matrix device
  923. * @attr: the mediated matrix device's assign_adapter attribute
  924. * @buf: a buffer containing the AP adapter number (APID) to
  925. * be assigned
  926. * @count: the number of bytes in @buf
  927. *
  928. * Return: the number of bytes processed if the APID is valid; otherwise,
  929. * returns one of the following errors:
  930. *
  931. * 1. -EINVAL
  932. * The APID is not a valid number
  933. *
  934. * 2. -ENODEV
  935. * The APID exceeds the maximum value configured for the system
  936. *
  937. * 3. -EADDRNOTAVAIL
  938. * An APQN derived from the cross product of the APID being assigned
  939. * and the APQIs previously assigned is not bound to the vfio_ap device
  940. * driver; or, if no APQIs have yet been assigned, the APID is not
  941. * contained in an APQN bound to the vfio_ap device driver.
  942. *
  943. * 4. -EADDRINUSE
  944. * An APQN derived from the cross product of the APID being assigned
  945. * and the APQIs previously assigned is being used by another mediated
  946. * matrix device
  947. *
  948. * 5. -EAGAIN
  949. * A lock required to validate the mdev's AP configuration could not
  950. * be obtained.
  951. */
  952. static ssize_t assign_adapter_store(struct device *dev,
  953. struct device_attribute *attr,
  954. const char *buf, size_t count)
  955. {
  956. int ret;
  957. unsigned long apid;
  958. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  959. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  960. mutex_lock(&ap_attr_mutex);
  961. get_update_locks_for_mdev(matrix_mdev);
  962. ret = kstrtoul(buf, 0, &apid);
  963. if (ret)
  964. goto done;
  965. if (apid > matrix_mdev->matrix.apm_max) {
  966. ret = -ENODEV;
  967. goto done;
  968. }
  969. if (test_bit_inv(apid, matrix_mdev->matrix.apm)) {
  970. ret = count;
  971. goto done;
  972. }
  973. set_bit_inv(apid, matrix_mdev->matrix.apm);
  974. ret = vfio_ap_mdev_validate_masks(matrix_mdev);
  975. if (ret) {
  976. clear_bit_inv(apid, matrix_mdev->matrix.apm);
  977. goto done;
  978. }
  979. vfio_ap_mdev_link_adapter(matrix_mdev, apid);
  980. if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
  981. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  982. reset_queues_for_apids(matrix_mdev, apm_filtered);
  983. }
  984. ret = count;
  985. done:
  986. release_update_locks_for_mdev(matrix_mdev);
  987. mutex_unlock(&ap_attr_mutex);
  988. return ret;
  989. }
  990. static DEVICE_ATTR_WO(assign_adapter);
  991. static struct vfio_ap_queue
  992. *vfio_ap_unlink_apqn_fr_mdev(struct ap_matrix_mdev *matrix_mdev,
  993. unsigned long apid, unsigned long apqi)
  994. {
  995. struct vfio_ap_queue *q = NULL;
  996. q = vfio_ap_mdev_get_queue(matrix_mdev, AP_MKQID(apid, apqi));
  997. /* If the queue is assigned to the matrix mdev, unlink it. */
  998. if (q)
  999. vfio_ap_unlink_queue_fr_mdev(q);
  1000. return q;
  1001. }
  1002. /**
  1003. * vfio_ap_mdev_unlink_adapter - unlink all queues associated with unassigned
  1004. * adapter from the matrix mdev to which the
  1005. * adapter was assigned.
  1006. * @matrix_mdev: the matrix mediated device to which the adapter was assigned.
  1007. * @apid: the APID of the unassigned adapter.
  1008. * @qlist: list for storing queues associated with unassigned adapter that
  1009. * need to be reset.
  1010. */
  1011. static void vfio_ap_mdev_unlink_adapter(struct ap_matrix_mdev *matrix_mdev,
  1012. unsigned long apid,
  1013. struct list_head *qlist)
  1014. {
  1015. unsigned long apqi;
  1016. struct vfio_ap_queue *q;
  1017. for_each_set_bit_inv(apqi, matrix_mdev->matrix.aqm, AP_DOMAINS) {
  1018. q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
  1019. if (q && qlist) {
  1020. if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
  1021. test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
  1022. list_add_tail(&q->reset_qnode, qlist);
  1023. }
  1024. }
  1025. }
  1026. static void vfio_ap_mdev_hot_unplug_adapters(struct ap_matrix_mdev *matrix_mdev,
  1027. unsigned long *apids)
  1028. {
  1029. struct vfio_ap_queue *q, *tmpq;
  1030. struct list_head qlist;
  1031. unsigned long apid;
  1032. bool apcb_update = false;
  1033. INIT_LIST_HEAD(&qlist);
  1034. for_each_set_bit_inv(apid, apids, AP_DEVICES) {
  1035. vfio_ap_mdev_unlink_adapter(matrix_mdev, apid, &qlist);
  1036. if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm)) {
  1037. clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
  1038. apcb_update = true;
  1039. }
  1040. }
  1041. /* Only update apcb if needed to avoid impacting guest */
  1042. if (apcb_update)
  1043. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1044. vfio_ap_mdev_reset_qlist(&qlist);
  1045. list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
  1046. vfio_ap_unlink_mdev_fr_queue(q);
  1047. list_del(&q->reset_qnode);
  1048. }
  1049. }
  1050. static void vfio_ap_mdev_hot_unplug_adapter(struct ap_matrix_mdev *matrix_mdev,
  1051. unsigned long apid)
  1052. {
  1053. DECLARE_BITMAP(apids, AP_DEVICES);
  1054. bitmap_zero(apids, AP_DEVICES);
  1055. set_bit_inv(apid, apids);
  1056. vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, apids);
  1057. }
  1058. /**
  1059. * unassign_adapter_store - parses the APID from @buf and clears the
  1060. * corresponding bit in the mediated matrix device's APM
  1061. *
  1062. * @dev: the matrix device
  1063. * @attr: the mediated matrix device's unassign_adapter attribute
  1064. * @buf: a buffer containing the adapter number (APID) to be unassigned
  1065. * @count: the number of bytes in @buf
  1066. *
  1067. * Return: the number of bytes processed if the APID is valid; otherwise,
  1068. * returns one of the following errors:
  1069. * -EINVAL if the APID is not a number
  1070. * -ENODEV if the APID it exceeds the maximum value configured for the
  1071. * system
  1072. */
  1073. static ssize_t unassign_adapter_store(struct device *dev,
  1074. struct device_attribute *attr,
  1075. const char *buf, size_t count)
  1076. {
  1077. int ret;
  1078. unsigned long apid;
  1079. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1080. get_update_locks_for_mdev(matrix_mdev);
  1081. ret = kstrtoul(buf, 0, &apid);
  1082. if (ret)
  1083. goto done;
  1084. if (apid > matrix_mdev->matrix.apm_max) {
  1085. ret = -ENODEV;
  1086. goto done;
  1087. }
  1088. if (!test_bit_inv(apid, matrix_mdev->matrix.apm)) {
  1089. ret = count;
  1090. goto done;
  1091. }
  1092. clear_bit_inv((unsigned long)apid, matrix_mdev->matrix.apm);
  1093. vfio_ap_mdev_hot_unplug_adapter(matrix_mdev, apid);
  1094. ret = count;
  1095. done:
  1096. release_update_locks_for_mdev(matrix_mdev);
  1097. return ret;
  1098. }
  1099. static DEVICE_ATTR_WO(unassign_adapter);
  1100. static void vfio_ap_mdev_link_domain(struct ap_matrix_mdev *matrix_mdev,
  1101. unsigned long apqi)
  1102. {
  1103. unsigned long apid;
  1104. for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES)
  1105. vfio_ap_mdev_link_apqn(matrix_mdev,
  1106. AP_MKQID(apid, apqi));
  1107. }
  1108. /**
  1109. * assign_domain_store - parses the APQI from @buf and sets the
  1110. * corresponding bit in the mediated matrix device's AQM
  1111. *
  1112. * @dev: the matrix device
  1113. * @attr: the mediated matrix device's assign_domain attribute
  1114. * @buf: a buffer containing the AP queue index (APQI) of the domain to
  1115. * be assigned
  1116. * @count: the number of bytes in @buf
  1117. *
  1118. * Return: the number of bytes processed if the APQI is valid; otherwise returns
  1119. * one of the following errors:
  1120. *
  1121. * 1. -EINVAL
  1122. * The APQI is not a valid number
  1123. *
  1124. * 2. -ENODEV
  1125. * The APQI exceeds the maximum value configured for the system
  1126. *
  1127. * 3. -EADDRNOTAVAIL
  1128. * An APQN derived from the cross product of the APQI being assigned
  1129. * and the APIDs previously assigned is not bound to the vfio_ap device
  1130. * driver; or, if no APIDs have yet been assigned, the APQI is not
  1131. * contained in an APQN bound to the vfio_ap device driver.
  1132. *
  1133. * 4. -EADDRINUSE
  1134. * An APQN derived from the cross product of the APQI being assigned
  1135. * and the APIDs previously assigned is being used by another mediated
  1136. * matrix device
  1137. *
  1138. * 5. -EAGAIN
  1139. * The lock required to validate the mdev's AP configuration could not
  1140. * be obtained.
  1141. */
  1142. static ssize_t assign_domain_store(struct device *dev,
  1143. struct device_attribute *attr,
  1144. const char *buf, size_t count)
  1145. {
  1146. int ret;
  1147. unsigned long apqi;
  1148. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  1149. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1150. mutex_lock(&ap_attr_mutex);
  1151. get_update_locks_for_mdev(matrix_mdev);
  1152. ret = kstrtoul(buf, 0, &apqi);
  1153. if (ret)
  1154. goto done;
  1155. if (apqi > matrix_mdev->matrix.aqm_max) {
  1156. ret = -ENODEV;
  1157. goto done;
  1158. }
  1159. if (test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
  1160. ret = count;
  1161. goto done;
  1162. }
  1163. set_bit_inv(apqi, matrix_mdev->matrix.aqm);
  1164. ret = vfio_ap_mdev_validate_masks(matrix_mdev);
  1165. if (ret) {
  1166. clear_bit_inv(apqi, matrix_mdev->matrix.aqm);
  1167. goto done;
  1168. }
  1169. vfio_ap_mdev_link_domain(matrix_mdev, apqi);
  1170. if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
  1171. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1172. reset_queues_for_apids(matrix_mdev, apm_filtered);
  1173. }
  1174. ret = count;
  1175. done:
  1176. release_update_locks_for_mdev(matrix_mdev);
  1177. mutex_unlock(&ap_attr_mutex);
  1178. return ret;
  1179. }
  1180. static DEVICE_ATTR_WO(assign_domain);
  1181. static void vfio_ap_mdev_unlink_domain(struct ap_matrix_mdev *matrix_mdev,
  1182. unsigned long apqi,
  1183. struct list_head *qlist)
  1184. {
  1185. unsigned long apid;
  1186. struct vfio_ap_queue *q;
  1187. for_each_set_bit_inv(apid, matrix_mdev->matrix.apm, AP_DEVICES) {
  1188. q = vfio_ap_unlink_apqn_fr_mdev(matrix_mdev, apid, apqi);
  1189. if (q && qlist) {
  1190. if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
  1191. test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
  1192. list_add_tail(&q->reset_qnode, qlist);
  1193. }
  1194. }
  1195. }
  1196. static void vfio_ap_mdev_hot_unplug_domains(struct ap_matrix_mdev *matrix_mdev,
  1197. unsigned long *apqis)
  1198. {
  1199. struct vfio_ap_queue *q, *tmpq;
  1200. struct list_head qlist;
  1201. unsigned long apqi;
  1202. bool apcb_update = false;
  1203. INIT_LIST_HEAD(&qlist);
  1204. for_each_set_bit_inv(apqi, apqis, AP_DOMAINS) {
  1205. vfio_ap_mdev_unlink_domain(matrix_mdev, apqi, &qlist);
  1206. if (test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
  1207. clear_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm);
  1208. apcb_update = true;
  1209. }
  1210. }
  1211. /* Only update apcb if needed to avoid impacting guest */
  1212. if (apcb_update)
  1213. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1214. vfio_ap_mdev_reset_qlist(&qlist);
  1215. list_for_each_entry_safe(q, tmpq, &qlist, reset_qnode) {
  1216. vfio_ap_unlink_mdev_fr_queue(q);
  1217. list_del(&q->reset_qnode);
  1218. }
  1219. }
  1220. static void vfio_ap_mdev_hot_unplug_domain(struct ap_matrix_mdev *matrix_mdev,
  1221. unsigned long apqi)
  1222. {
  1223. DECLARE_BITMAP(apqis, AP_DOMAINS);
  1224. bitmap_zero(apqis, AP_DEVICES);
  1225. set_bit_inv(apqi, apqis);
  1226. vfio_ap_mdev_hot_unplug_domains(matrix_mdev, apqis);
  1227. }
  1228. /**
  1229. * unassign_domain_store - parses the APQI from @buf and clears the
  1230. * corresponding bit in the mediated matrix device's AQM
  1231. *
  1232. * @dev: the matrix device
  1233. * @attr: the mediated matrix device's unassign_domain attribute
  1234. * @buf: a buffer containing the AP queue index (APQI) of the domain to
  1235. * be unassigned
  1236. * @count: the number of bytes in @buf
  1237. *
  1238. * Return: the number of bytes processed if the APQI is valid; otherwise,
  1239. * returns one of the following errors:
  1240. * -EINVAL if the APQI is not a number
  1241. * -ENODEV if the APQI exceeds the maximum value configured for the system
  1242. */
  1243. static ssize_t unassign_domain_store(struct device *dev,
  1244. struct device_attribute *attr,
  1245. const char *buf, size_t count)
  1246. {
  1247. int ret;
  1248. unsigned long apqi;
  1249. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1250. get_update_locks_for_mdev(matrix_mdev);
  1251. ret = kstrtoul(buf, 0, &apqi);
  1252. if (ret)
  1253. goto done;
  1254. if (apqi > matrix_mdev->matrix.aqm_max) {
  1255. ret = -ENODEV;
  1256. goto done;
  1257. }
  1258. if (!test_bit_inv(apqi, matrix_mdev->matrix.aqm)) {
  1259. ret = count;
  1260. goto done;
  1261. }
  1262. clear_bit_inv((unsigned long)apqi, matrix_mdev->matrix.aqm);
  1263. vfio_ap_mdev_hot_unplug_domain(matrix_mdev, apqi);
  1264. ret = count;
  1265. done:
  1266. release_update_locks_for_mdev(matrix_mdev);
  1267. return ret;
  1268. }
  1269. static DEVICE_ATTR_WO(unassign_domain);
  1270. /**
  1271. * assign_control_domain_store - parses the domain ID from @buf and sets
  1272. * the corresponding bit in the mediated matrix device's ADM
  1273. *
  1274. * @dev: the matrix device
  1275. * @attr: the mediated matrix device's assign_control_domain attribute
  1276. * @buf: a buffer containing the domain ID to be assigned
  1277. * @count: the number of bytes in @buf
  1278. *
  1279. * Return: the number of bytes processed if the domain ID is valid; otherwise,
  1280. * returns one of the following errors:
  1281. * -EINVAL if the ID is not a number
  1282. * -ENODEV if the ID exceeds the maximum value configured for the system
  1283. */
  1284. static ssize_t assign_control_domain_store(struct device *dev,
  1285. struct device_attribute *attr,
  1286. const char *buf, size_t count)
  1287. {
  1288. int ret;
  1289. unsigned long id;
  1290. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1291. get_update_locks_for_mdev(matrix_mdev);
  1292. ret = kstrtoul(buf, 0, &id);
  1293. if (ret)
  1294. goto done;
  1295. if (id > matrix_mdev->matrix.adm_max) {
  1296. ret = -ENODEV;
  1297. goto done;
  1298. }
  1299. if (test_bit_inv(id, matrix_mdev->matrix.adm)) {
  1300. ret = count;
  1301. goto done;
  1302. }
  1303. /* Set the bit in the ADM (bitmask) corresponding to the AP control
  1304. * domain number (id). The bits in the mask, from most significant to
  1305. * least significant, correspond to IDs 0 up to the one less than the
  1306. * number of control domains that can be assigned.
  1307. */
  1308. set_bit_inv(id, matrix_mdev->matrix.adm);
  1309. if (vfio_ap_mdev_filter_cdoms(matrix_mdev))
  1310. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1311. ret = count;
  1312. done:
  1313. release_update_locks_for_mdev(matrix_mdev);
  1314. return ret;
  1315. }
  1316. static DEVICE_ATTR_WO(assign_control_domain);
  1317. /**
  1318. * unassign_control_domain_store - parses the domain ID from @buf and
  1319. * clears the corresponding bit in the mediated matrix device's ADM
  1320. *
  1321. * @dev: the matrix device
  1322. * @attr: the mediated matrix device's unassign_control_domain attribute
  1323. * @buf: a buffer containing the domain ID to be unassigned
  1324. * @count: the number of bytes in @buf
  1325. *
  1326. * Return: the number of bytes processed if the domain ID is valid; otherwise,
  1327. * returns one of the following errors:
  1328. * -EINVAL if the ID is not a number
  1329. * -ENODEV if the ID exceeds the maximum value configured for the system
  1330. */
  1331. static ssize_t unassign_control_domain_store(struct device *dev,
  1332. struct device_attribute *attr,
  1333. const char *buf, size_t count)
  1334. {
  1335. int ret;
  1336. unsigned long domid;
  1337. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1338. get_update_locks_for_mdev(matrix_mdev);
  1339. ret = kstrtoul(buf, 0, &domid);
  1340. if (ret)
  1341. goto done;
  1342. if (domid > matrix_mdev->matrix.adm_max) {
  1343. ret = -ENODEV;
  1344. goto done;
  1345. }
  1346. if (!test_bit_inv(domid, matrix_mdev->matrix.adm)) {
  1347. ret = count;
  1348. goto done;
  1349. }
  1350. clear_bit_inv(domid, matrix_mdev->matrix.adm);
  1351. if (test_bit_inv(domid, matrix_mdev->shadow_apcb.adm)) {
  1352. clear_bit_inv(domid, matrix_mdev->shadow_apcb.adm);
  1353. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1354. }
  1355. ret = count;
  1356. done:
  1357. release_update_locks_for_mdev(matrix_mdev);
  1358. return ret;
  1359. }
  1360. static DEVICE_ATTR_WO(unassign_control_domain);
  1361. static ssize_t control_domains_show(struct device *dev,
  1362. struct device_attribute *dev_attr,
  1363. char *buf)
  1364. {
  1365. unsigned long id;
  1366. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1367. unsigned long max_domid = matrix_mdev->matrix.adm_max;
  1368. int nchars = 0;
  1369. mutex_lock(&matrix_dev->mdevs_lock);
  1370. for_each_set_bit_inv(id, matrix_mdev->matrix.adm, max_domid + 1)
  1371. nchars += sysfs_emit_at(buf, nchars, "%04lx\n", id);
  1372. mutex_unlock(&matrix_dev->mdevs_lock);
  1373. return nchars;
  1374. }
  1375. static DEVICE_ATTR_RO(control_domains);
  1376. static ssize_t vfio_ap_mdev_matrix_show(struct ap_matrix *matrix, char *buf)
  1377. {
  1378. unsigned long apid;
  1379. unsigned long apqi;
  1380. unsigned long apid1;
  1381. unsigned long apqi1;
  1382. unsigned long napm_bits = matrix->apm_max + 1;
  1383. unsigned long naqm_bits = matrix->aqm_max + 1;
  1384. int nchars = 0;
  1385. apid1 = find_first_bit_inv(matrix->apm, napm_bits);
  1386. apqi1 = find_first_bit_inv(matrix->aqm, naqm_bits);
  1387. if ((apid1 < napm_bits) && (apqi1 < naqm_bits)) {
  1388. for_each_set_bit_inv(apid, matrix->apm, napm_bits) {
  1389. for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits)
  1390. nchars += sysfs_emit_at(buf, nchars, "%02lx.%04lx\n", apid, apqi);
  1391. }
  1392. } else if (apid1 < napm_bits) {
  1393. for_each_set_bit_inv(apid, matrix->apm, napm_bits)
  1394. nchars += sysfs_emit_at(buf, nchars, "%02lx.\n", apid);
  1395. } else if (apqi1 < naqm_bits) {
  1396. for_each_set_bit_inv(apqi, matrix->aqm, naqm_bits)
  1397. nchars += sysfs_emit_at(buf, nchars, ".%04lx\n", apqi);
  1398. }
  1399. return nchars;
  1400. }
  1401. static ssize_t matrix_show(struct device *dev, struct device_attribute *attr,
  1402. char *buf)
  1403. {
  1404. ssize_t nchars;
  1405. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1406. mutex_lock(&matrix_dev->mdevs_lock);
  1407. nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->matrix, buf);
  1408. mutex_unlock(&matrix_dev->mdevs_lock);
  1409. return nchars;
  1410. }
  1411. static DEVICE_ATTR_RO(matrix);
  1412. static ssize_t guest_matrix_show(struct device *dev,
  1413. struct device_attribute *attr, char *buf)
  1414. {
  1415. ssize_t nchars;
  1416. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1417. mutex_lock(&matrix_dev->mdevs_lock);
  1418. nchars = vfio_ap_mdev_matrix_show(&matrix_mdev->shadow_apcb, buf);
  1419. mutex_unlock(&matrix_dev->mdevs_lock);
  1420. return nchars;
  1421. }
  1422. static DEVICE_ATTR_RO(guest_matrix);
  1423. static ssize_t write_ap_bitmap(unsigned long *bitmap, char *buf, int offset, char sep)
  1424. {
  1425. return sysfs_emit_at(buf, offset, "0x%016lx%016lx%016lx%016lx%c",
  1426. bitmap[0], bitmap[1], bitmap[2], bitmap[3], sep);
  1427. }
  1428. static ssize_t ap_config_show(struct device *dev, struct device_attribute *attr,
  1429. char *buf)
  1430. {
  1431. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1432. int idx = 0;
  1433. idx += write_ap_bitmap(matrix_mdev->matrix.apm, buf, idx, ',');
  1434. idx += write_ap_bitmap(matrix_mdev->matrix.aqm, buf, idx, ',');
  1435. idx += write_ap_bitmap(matrix_mdev->matrix.adm, buf, idx, '\n');
  1436. return idx;
  1437. }
  1438. /* Number of characters needed for a complete hex mask representing the bits in .. */
  1439. #define AP_DEVICES_STRLEN (AP_DEVICES / 4 + 3)
  1440. #define AP_DOMAINS_STRLEN (AP_DOMAINS / 4 + 3)
  1441. #define AP_CONFIG_STRLEN (AP_DEVICES_STRLEN + 2 * AP_DOMAINS_STRLEN)
  1442. static int parse_bitmap(char **strbufptr, unsigned long *bitmap, int nbits)
  1443. {
  1444. char *curmask;
  1445. curmask = strsep(strbufptr, ",\n");
  1446. if (!curmask)
  1447. return -EINVAL;
  1448. bitmap_clear(bitmap, 0, nbits);
  1449. return ap_hex2bitmap(curmask, bitmap, nbits);
  1450. }
  1451. static int ap_matrix_overflow_check(struct ap_matrix_mdev *matrix_mdev)
  1452. {
  1453. unsigned long bit;
  1454. for_each_set_bit_inv(bit, matrix_mdev->matrix.apm, AP_DEVICES) {
  1455. if (bit > matrix_mdev->matrix.apm_max)
  1456. return -ENODEV;
  1457. }
  1458. for_each_set_bit_inv(bit, matrix_mdev->matrix.aqm, AP_DOMAINS) {
  1459. if (bit > matrix_mdev->matrix.aqm_max)
  1460. return -ENODEV;
  1461. }
  1462. for_each_set_bit_inv(bit, matrix_mdev->matrix.adm, AP_DOMAINS) {
  1463. if (bit > matrix_mdev->matrix.adm_max)
  1464. return -ENODEV;
  1465. }
  1466. return 0;
  1467. }
  1468. static void ap_matrix_copy(struct ap_matrix *dst, struct ap_matrix *src)
  1469. {
  1470. /* This check works around false positive gcc -Wstringop-overread */
  1471. if (!src)
  1472. return;
  1473. bitmap_copy(dst->apm, src->apm, AP_DEVICES);
  1474. bitmap_copy(dst->aqm, src->aqm, AP_DOMAINS);
  1475. bitmap_copy(dst->adm, src->adm, AP_DOMAINS);
  1476. }
  1477. static ssize_t ap_config_store(struct device *dev, struct device_attribute *attr,
  1478. const char *buf, size_t count)
  1479. {
  1480. struct ap_matrix_mdev *matrix_mdev = dev_get_drvdata(dev);
  1481. struct ap_matrix m_new, m_old, m_added, m_removed;
  1482. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  1483. unsigned long newbit;
  1484. char *newbuf, *rest;
  1485. int rc = count;
  1486. bool do_update;
  1487. newbuf = kstrndup(buf, AP_CONFIG_STRLEN, GFP_KERNEL);
  1488. if (!newbuf)
  1489. return -ENOMEM;
  1490. rest = newbuf;
  1491. mutex_lock(&ap_attr_mutex);
  1492. get_update_locks_for_mdev(matrix_mdev);
  1493. /* Save old state */
  1494. ap_matrix_copy(&m_old, &matrix_mdev->matrix);
  1495. if (parse_bitmap(&rest, m_new.apm, AP_DEVICES) ||
  1496. parse_bitmap(&rest, m_new.aqm, AP_DOMAINS) ||
  1497. parse_bitmap(&rest, m_new.adm, AP_DOMAINS)) {
  1498. rc = -EINVAL;
  1499. goto out;
  1500. }
  1501. bitmap_andnot(m_removed.apm, m_old.apm, m_new.apm, AP_DEVICES);
  1502. bitmap_andnot(m_removed.aqm, m_old.aqm, m_new.aqm, AP_DOMAINS);
  1503. bitmap_andnot(m_added.apm, m_new.apm, m_old.apm, AP_DEVICES);
  1504. bitmap_andnot(m_added.aqm, m_new.aqm, m_old.aqm, AP_DOMAINS);
  1505. /* Need new bitmaps in matrix_mdev for validation */
  1506. ap_matrix_copy(&matrix_mdev->matrix, &m_new);
  1507. /* Ensure new state is valid, else undo new state */
  1508. rc = vfio_ap_mdev_validate_masks(matrix_mdev);
  1509. if (rc) {
  1510. ap_matrix_copy(&matrix_mdev->matrix, &m_old);
  1511. goto out;
  1512. }
  1513. rc = ap_matrix_overflow_check(matrix_mdev);
  1514. if (rc) {
  1515. ap_matrix_copy(&matrix_mdev->matrix, &m_old);
  1516. goto out;
  1517. }
  1518. rc = count;
  1519. /* Need old bitmaps in matrix_mdev for unplug/unlink */
  1520. ap_matrix_copy(&matrix_mdev->matrix, &m_old);
  1521. /* Unlink removed adapters/domains */
  1522. vfio_ap_mdev_hot_unplug_adapters(matrix_mdev, m_removed.apm);
  1523. vfio_ap_mdev_hot_unplug_domains(matrix_mdev, m_removed.aqm);
  1524. /* Need new bitmaps in matrix_mdev for linking new adapters/domains */
  1525. ap_matrix_copy(&matrix_mdev->matrix, &m_new);
  1526. /* Link newly added adapters */
  1527. for_each_set_bit_inv(newbit, m_added.apm, AP_DEVICES)
  1528. vfio_ap_mdev_link_adapter(matrix_mdev, newbit);
  1529. for_each_set_bit_inv(newbit, m_added.aqm, AP_DOMAINS)
  1530. vfio_ap_mdev_link_domain(matrix_mdev, newbit);
  1531. /* filter resources not bound to vfio-ap */
  1532. do_update = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
  1533. do_update |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
  1534. /* Apply changes to shadow apbc if things changed */
  1535. if (do_update) {
  1536. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1537. reset_queues_for_apids(matrix_mdev, apm_filtered);
  1538. }
  1539. out:
  1540. release_update_locks_for_mdev(matrix_mdev);
  1541. mutex_unlock(&ap_attr_mutex);
  1542. kfree(newbuf);
  1543. return rc;
  1544. }
  1545. static DEVICE_ATTR_RW(ap_config);
  1546. static struct attribute *vfio_ap_mdev_attrs[] = {
  1547. &dev_attr_assign_adapter.attr,
  1548. &dev_attr_unassign_adapter.attr,
  1549. &dev_attr_assign_domain.attr,
  1550. &dev_attr_unassign_domain.attr,
  1551. &dev_attr_assign_control_domain.attr,
  1552. &dev_attr_unassign_control_domain.attr,
  1553. &dev_attr_ap_config.attr,
  1554. &dev_attr_control_domains.attr,
  1555. &dev_attr_matrix.attr,
  1556. &dev_attr_guest_matrix.attr,
  1557. NULL,
  1558. };
  1559. static struct attribute_group vfio_ap_mdev_attr_group = {
  1560. .attrs = vfio_ap_mdev_attrs
  1561. };
  1562. static const struct attribute_group *vfio_ap_mdev_attr_groups[] = {
  1563. &vfio_ap_mdev_attr_group,
  1564. NULL
  1565. };
  1566. /**
  1567. * vfio_ap_mdev_set_kvm - sets all data for @matrix_mdev that are needed
  1568. * to manage AP resources for the guest whose state is represented by @kvm
  1569. *
  1570. * @matrix_mdev: a mediated matrix device
  1571. * @kvm: reference to KVM instance
  1572. *
  1573. * Return: 0 if no other mediated matrix device has a reference to @kvm;
  1574. * otherwise, returns an -EPERM.
  1575. */
  1576. static int vfio_ap_mdev_set_kvm(struct ap_matrix_mdev *matrix_mdev,
  1577. struct kvm *kvm)
  1578. {
  1579. struct ap_matrix_mdev *m;
  1580. if (kvm->arch.crypto.crycbd) {
  1581. down_write(&kvm->arch.crypto.pqap_hook_rwsem);
  1582. kvm->arch.crypto.pqap_hook = &matrix_mdev->pqap_hook;
  1583. up_write(&kvm->arch.crypto.pqap_hook_rwsem);
  1584. get_update_locks_for_kvm(kvm);
  1585. list_for_each_entry(m, &matrix_dev->mdev_list, node) {
  1586. if (m != matrix_mdev && m->kvm == kvm) {
  1587. release_update_locks_for_kvm(kvm);
  1588. return -EPERM;
  1589. }
  1590. }
  1591. kvm_get_kvm(kvm);
  1592. matrix_mdev->kvm = kvm;
  1593. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  1594. release_update_locks_for_kvm(kvm);
  1595. }
  1596. return 0;
  1597. }
  1598. static void unmap_iova(struct ap_matrix_mdev *matrix_mdev, u64 iova, u64 length)
  1599. {
  1600. struct ap_queue_table *qtable = &matrix_mdev->qtable;
  1601. struct vfio_ap_queue *q;
  1602. int loop_cursor;
  1603. hash_for_each(qtable->queues, loop_cursor, q, mdev_qnode) {
  1604. if (q->saved_iova >= iova && q->saved_iova < iova + length)
  1605. vfio_ap_irq_disable(q);
  1606. }
  1607. }
  1608. static void vfio_ap_mdev_dma_unmap(struct vfio_device *vdev, u64 iova,
  1609. u64 length)
  1610. {
  1611. struct ap_matrix_mdev *matrix_mdev =
  1612. container_of(vdev, struct ap_matrix_mdev, vdev);
  1613. mutex_lock(&matrix_dev->mdevs_lock);
  1614. unmap_iova(matrix_mdev, iova, length);
  1615. mutex_unlock(&matrix_dev->mdevs_lock);
  1616. }
  1617. /**
  1618. * vfio_ap_mdev_unset_kvm - performs clean-up of resources no longer needed
  1619. * by @matrix_mdev.
  1620. *
  1621. * @matrix_mdev: a matrix mediated device
  1622. */
  1623. static void vfio_ap_mdev_unset_kvm(struct ap_matrix_mdev *matrix_mdev)
  1624. {
  1625. struct kvm *kvm = matrix_mdev->kvm;
  1626. if (kvm && kvm->arch.crypto.crycbd) {
  1627. down_write(&kvm->arch.crypto.pqap_hook_rwsem);
  1628. kvm->arch.crypto.pqap_hook = NULL;
  1629. up_write(&kvm->arch.crypto.pqap_hook_rwsem);
  1630. get_update_locks_for_kvm(kvm);
  1631. kvm_arch_crypto_clear_masks(kvm);
  1632. vfio_ap_mdev_reset_queues(matrix_mdev);
  1633. kvm_put_kvm(kvm);
  1634. matrix_mdev->kvm = NULL;
  1635. release_update_locks_for_kvm(kvm);
  1636. }
  1637. }
  1638. static struct vfio_ap_queue *vfio_ap_find_queue(int apqn)
  1639. {
  1640. struct ap_queue *queue;
  1641. struct vfio_ap_queue *q = NULL;
  1642. queue = ap_get_qdev(apqn);
  1643. if (!queue)
  1644. return NULL;
  1645. if (queue->ap_dev.device.driver == &matrix_dev->vfio_ap_drv->driver)
  1646. q = dev_get_drvdata(&queue->ap_dev.device);
  1647. put_device(&queue->ap_dev.device);
  1648. return q;
  1649. }
  1650. static int apq_status_check(int apqn, struct ap_queue_status *status)
  1651. {
  1652. switch (status->response_code) {
  1653. case AP_RESPONSE_NORMAL:
  1654. case AP_RESPONSE_DECONFIGURED:
  1655. case AP_RESPONSE_CHECKSTOPPED:
  1656. return 0;
  1657. case AP_RESPONSE_RESET_IN_PROGRESS:
  1658. case AP_RESPONSE_BUSY:
  1659. return -EBUSY;
  1660. case AP_RESPONSE_ASSOC_SECRET_NOT_UNIQUE:
  1661. case AP_RESPONSE_ASSOC_FAILED:
  1662. /*
  1663. * These asynchronous response codes indicate a PQAP(AAPQ)
  1664. * instruction to associate a secret with the guest failed. All
  1665. * subsequent AP instructions will end with the asynchronous
  1666. * response code until the AP queue is reset; so, let's return
  1667. * a value indicating a reset needs to be performed again.
  1668. */
  1669. return -EAGAIN;
  1670. default:
  1671. WARN(true,
  1672. "failed to verify reset of queue %02x.%04x: TAPQ rc=%u\n",
  1673. AP_QID_CARD(apqn), AP_QID_QUEUE(apqn),
  1674. status->response_code);
  1675. return -EIO;
  1676. }
  1677. }
  1678. #define WAIT_MSG "Waited %dms for reset of queue %02x.%04x (%u, %u, %u)"
  1679. static void apq_reset_check(struct work_struct *reset_work)
  1680. {
  1681. int ret = -EBUSY, elapsed = 0;
  1682. struct ap_queue_status status;
  1683. struct vfio_ap_queue *q;
  1684. q = container_of(reset_work, struct vfio_ap_queue, reset_work);
  1685. memcpy(&status, &q->reset_status, sizeof(status));
  1686. while (true) {
  1687. msleep(AP_RESET_INTERVAL);
  1688. elapsed += AP_RESET_INTERVAL;
  1689. status = ap_tapq(q->apqn, NULL);
  1690. ret = apq_status_check(q->apqn, &status);
  1691. if (ret == -EIO)
  1692. return;
  1693. if (ret == -EBUSY) {
  1694. pr_notice_ratelimited(WAIT_MSG, elapsed,
  1695. AP_QID_CARD(q->apqn),
  1696. AP_QID_QUEUE(q->apqn),
  1697. status.response_code,
  1698. status.queue_empty,
  1699. status.irq_enabled);
  1700. } else {
  1701. if (q->reset_status.response_code == AP_RESPONSE_RESET_IN_PROGRESS ||
  1702. q->reset_status.response_code == AP_RESPONSE_BUSY ||
  1703. q->reset_status.response_code == AP_RESPONSE_STATE_CHANGE_IN_PROGRESS ||
  1704. ret == -EAGAIN) {
  1705. status = ap_zapq(q->apqn, 0);
  1706. memcpy(&q->reset_status, &status, sizeof(status));
  1707. continue;
  1708. }
  1709. if (q->saved_isc != VFIO_AP_ISC_INVALID)
  1710. vfio_ap_free_aqic_resources(q);
  1711. break;
  1712. }
  1713. }
  1714. }
  1715. static void vfio_ap_mdev_reset_queue(struct vfio_ap_queue *q)
  1716. {
  1717. struct ap_queue_status status;
  1718. if (!q)
  1719. return;
  1720. status = ap_zapq(q->apqn, 0);
  1721. memcpy(&q->reset_status, &status, sizeof(status));
  1722. switch (status.response_code) {
  1723. case AP_RESPONSE_NORMAL:
  1724. case AP_RESPONSE_RESET_IN_PROGRESS:
  1725. case AP_RESPONSE_BUSY:
  1726. case AP_RESPONSE_STATE_CHANGE_IN_PROGRESS:
  1727. /*
  1728. * Let's verify whether the ZAPQ completed successfully on a work queue.
  1729. */
  1730. queue_work(system_long_wq, &q->reset_work);
  1731. break;
  1732. case AP_RESPONSE_DECONFIGURED:
  1733. case AP_RESPONSE_CHECKSTOPPED:
  1734. vfio_ap_free_aqic_resources(q);
  1735. break;
  1736. default:
  1737. WARN(true,
  1738. "PQAP/ZAPQ for %02x.%04x failed with invalid rc=%u\n",
  1739. AP_QID_CARD(q->apqn), AP_QID_QUEUE(q->apqn),
  1740. status.response_code);
  1741. }
  1742. }
  1743. static int vfio_ap_mdev_reset_queues(struct ap_matrix_mdev *matrix_mdev)
  1744. {
  1745. int ret = 0, loop_cursor;
  1746. struct vfio_ap_queue *q;
  1747. hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode)
  1748. vfio_ap_mdev_reset_queue(q);
  1749. hash_for_each(matrix_mdev->qtable.queues, loop_cursor, q, mdev_qnode) {
  1750. flush_work(&q->reset_work);
  1751. if (q->reset_status.response_code)
  1752. ret = -EIO;
  1753. }
  1754. return ret;
  1755. }
  1756. static int vfio_ap_mdev_reset_qlist(struct list_head *qlist)
  1757. {
  1758. int ret = 0;
  1759. struct vfio_ap_queue *q;
  1760. list_for_each_entry(q, qlist, reset_qnode)
  1761. vfio_ap_mdev_reset_queue(q);
  1762. list_for_each_entry(q, qlist, reset_qnode) {
  1763. flush_work(&q->reset_work);
  1764. if (q->reset_status.response_code)
  1765. ret = -EIO;
  1766. }
  1767. return ret;
  1768. }
  1769. static int vfio_ap_mdev_open_device(struct vfio_device *vdev)
  1770. {
  1771. struct ap_matrix_mdev *matrix_mdev =
  1772. container_of(vdev, struct ap_matrix_mdev, vdev);
  1773. if (!vdev->kvm)
  1774. return -EINVAL;
  1775. return vfio_ap_mdev_set_kvm(matrix_mdev, vdev->kvm);
  1776. }
  1777. static void vfio_ap_mdev_close_device(struct vfio_device *vdev)
  1778. {
  1779. struct ap_matrix_mdev *matrix_mdev =
  1780. container_of(vdev, struct ap_matrix_mdev, vdev);
  1781. vfio_ap_mdev_unset_kvm(matrix_mdev);
  1782. }
  1783. static void vfio_ap_mdev_request(struct vfio_device *vdev, unsigned int count)
  1784. {
  1785. struct device *dev = vdev->dev;
  1786. struct ap_matrix_mdev *matrix_mdev;
  1787. matrix_mdev = container_of(vdev, struct ap_matrix_mdev, vdev);
  1788. get_update_locks_for_mdev(matrix_mdev);
  1789. if (matrix_mdev->kvm) {
  1790. kvm_arch_crypto_clear_masks(matrix_mdev->kvm);
  1791. signal_guest_ap_cfg_changed(matrix_mdev);
  1792. }
  1793. if (matrix_mdev->req_trigger) {
  1794. if (!(count % 10))
  1795. dev_notice_ratelimited(dev,
  1796. "Relaying device request to user (#%u)\n",
  1797. count);
  1798. eventfd_signal(matrix_mdev->req_trigger);
  1799. } else if (count == 0) {
  1800. dev_notice(dev,
  1801. "No device request registered, blocked until released by user\n");
  1802. }
  1803. release_update_locks_for_mdev(matrix_mdev);
  1804. }
  1805. static int vfio_ap_mdev_get_device_info(unsigned long arg)
  1806. {
  1807. unsigned long minsz;
  1808. struct vfio_device_info info;
  1809. minsz = offsetofend(struct vfio_device_info, num_irqs);
  1810. if (copy_from_user(&info, (void __user *)arg, minsz))
  1811. return -EFAULT;
  1812. if (info.argsz < minsz)
  1813. return -EINVAL;
  1814. info.flags = VFIO_DEVICE_FLAGS_AP | VFIO_DEVICE_FLAGS_RESET;
  1815. info.num_regions = 0;
  1816. info.num_irqs = VFIO_AP_NUM_IRQS;
  1817. return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
  1818. }
  1819. static ssize_t vfio_ap_get_irq_info(unsigned long arg)
  1820. {
  1821. unsigned long minsz;
  1822. struct vfio_irq_info info;
  1823. minsz = offsetofend(struct vfio_irq_info, count);
  1824. if (copy_from_user(&info, (void __user *)arg, minsz))
  1825. return -EFAULT;
  1826. if (info.argsz < minsz || info.index >= VFIO_AP_NUM_IRQS)
  1827. return -EINVAL;
  1828. switch (info.index) {
  1829. case VFIO_AP_REQ_IRQ_INDEX:
  1830. info.count = 1;
  1831. info.flags = VFIO_IRQ_INFO_EVENTFD;
  1832. break;
  1833. case VFIO_AP_CFG_CHG_IRQ_INDEX:
  1834. info.count = 1;
  1835. info.flags = VFIO_IRQ_INFO_EVENTFD;
  1836. break;
  1837. default:
  1838. return -EINVAL;
  1839. }
  1840. return copy_to_user((void __user *)arg, &info, minsz) ? -EFAULT : 0;
  1841. }
  1842. static int vfio_ap_irq_set_init(struct vfio_irq_set *irq_set, unsigned long arg)
  1843. {
  1844. int ret;
  1845. size_t data_size;
  1846. unsigned long minsz;
  1847. minsz = offsetofend(struct vfio_irq_set, count);
  1848. if (copy_from_user(irq_set, (void __user *)arg, minsz))
  1849. return -EFAULT;
  1850. ret = vfio_set_irqs_validate_and_prepare(irq_set, 1, VFIO_AP_NUM_IRQS,
  1851. &data_size);
  1852. if (ret)
  1853. return ret;
  1854. if (!(irq_set->flags & VFIO_IRQ_SET_ACTION_TRIGGER))
  1855. return -EINVAL;
  1856. return 0;
  1857. }
  1858. static int vfio_ap_set_request_irq(struct ap_matrix_mdev *matrix_mdev,
  1859. unsigned long arg)
  1860. {
  1861. s32 fd;
  1862. void __user *data;
  1863. unsigned long minsz;
  1864. struct eventfd_ctx *req_trigger;
  1865. minsz = offsetofend(struct vfio_irq_set, count);
  1866. data = (void __user *)(arg + minsz);
  1867. if (get_user(fd, (s32 __user *)data))
  1868. return -EFAULT;
  1869. if (fd == -1) {
  1870. if (matrix_mdev->req_trigger)
  1871. eventfd_ctx_put(matrix_mdev->req_trigger);
  1872. matrix_mdev->req_trigger = NULL;
  1873. } else if (fd >= 0) {
  1874. req_trigger = eventfd_ctx_fdget(fd);
  1875. if (IS_ERR(req_trigger))
  1876. return PTR_ERR(req_trigger);
  1877. if (matrix_mdev->req_trigger)
  1878. eventfd_ctx_put(matrix_mdev->req_trigger);
  1879. matrix_mdev->req_trigger = req_trigger;
  1880. } else {
  1881. return -EINVAL;
  1882. }
  1883. return 0;
  1884. }
  1885. static int vfio_ap_set_cfg_change_irq(struct ap_matrix_mdev *matrix_mdev, unsigned long arg)
  1886. {
  1887. s32 fd;
  1888. void __user *data;
  1889. unsigned long minsz;
  1890. struct eventfd_ctx *cfg_chg_trigger;
  1891. minsz = offsetofend(struct vfio_irq_set, count);
  1892. data = (void __user *)(arg + minsz);
  1893. if (get_user(fd, (s32 __user *)data))
  1894. return -EFAULT;
  1895. if (fd == -1) {
  1896. if (matrix_mdev->cfg_chg_trigger)
  1897. eventfd_ctx_put(matrix_mdev->cfg_chg_trigger);
  1898. matrix_mdev->cfg_chg_trigger = NULL;
  1899. } else if (fd >= 0) {
  1900. cfg_chg_trigger = eventfd_ctx_fdget(fd);
  1901. if (IS_ERR(cfg_chg_trigger))
  1902. return PTR_ERR(cfg_chg_trigger);
  1903. if (matrix_mdev->cfg_chg_trigger)
  1904. eventfd_ctx_put(matrix_mdev->cfg_chg_trigger);
  1905. matrix_mdev->cfg_chg_trigger = cfg_chg_trigger;
  1906. } else {
  1907. return -EINVAL;
  1908. }
  1909. return 0;
  1910. }
  1911. static int vfio_ap_set_irqs(struct ap_matrix_mdev *matrix_mdev,
  1912. unsigned long arg)
  1913. {
  1914. int ret;
  1915. struct vfio_irq_set irq_set;
  1916. ret = vfio_ap_irq_set_init(&irq_set, arg);
  1917. if (ret)
  1918. return ret;
  1919. switch (irq_set.flags & VFIO_IRQ_SET_DATA_TYPE_MASK) {
  1920. case VFIO_IRQ_SET_DATA_EVENTFD:
  1921. switch (irq_set.index) {
  1922. case VFIO_AP_REQ_IRQ_INDEX:
  1923. return vfio_ap_set_request_irq(matrix_mdev, arg);
  1924. case VFIO_AP_CFG_CHG_IRQ_INDEX:
  1925. return vfio_ap_set_cfg_change_irq(matrix_mdev, arg);
  1926. default:
  1927. return -EINVAL;
  1928. }
  1929. default:
  1930. return -EINVAL;
  1931. }
  1932. }
  1933. static ssize_t vfio_ap_mdev_ioctl(struct vfio_device *vdev,
  1934. unsigned int cmd, unsigned long arg)
  1935. {
  1936. struct ap_matrix_mdev *matrix_mdev =
  1937. container_of(vdev, struct ap_matrix_mdev, vdev);
  1938. int ret;
  1939. mutex_lock(&matrix_dev->mdevs_lock);
  1940. switch (cmd) {
  1941. case VFIO_DEVICE_GET_INFO:
  1942. ret = vfio_ap_mdev_get_device_info(arg);
  1943. break;
  1944. case VFIO_DEVICE_RESET:
  1945. ret = vfio_ap_mdev_reset_queues(matrix_mdev);
  1946. break;
  1947. case VFIO_DEVICE_GET_IRQ_INFO:
  1948. ret = vfio_ap_get_irq_info(arg);
  1949. break;
  1950. case VFIO_DEVICE_SET_IRQS:
  1951. ret = vfio_ap_set_irqs(matrix_mdev, arg);
  1952. break;
  1953. default:
  1954. ret = -EOPNOTSUPP;
  1955. break;
  1956. }
  1957. mutex_unlock(&matrix_dev->mdevs_lock);
  1958. return ret;
  1959. }
  1960. static struct ap_matrix_mdev *vfio_ap_mdev_for_queue(struct vfio_ap_queue *q)
  1961. {
  1962. struct ap_matrix_mdev *matrix_mdev;
  1963. unsigned long apid = AP_QID_CARD(q->apqn);
  1964. unsigned long apqi = AP_QID_QUEUE(q->apqn);
  1965. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  1966. if (test_bit_inv(apid, matrix_mdev->matrix.apm) &&
  1967. test_bit_inv(apqi, matrix_mdev->matrix.aqm))
  1968. return matrix_mdev;
  1969. }
  1970. return NULL;
  1971. }
  1972. static ssize_t status_show(struct device *dev,
  1973. struct device_attribute *attr,
  1974. char *buf)
  1975. {
  1976. ssize_t nchars = 0;
  1977. struct vfio_ap_queue *q;
  1978. unsigned long apid, apqi;
  1979. struct ap_matrix_mdev *matrix_mdev;
  1980. struct ap_device *apdev = to_ap_dev(dev);
  1981. mutex_lock(&matrix_dev->mdevs_lock);
  1982. q = dev_get_drvdata(&apdev->device);
  1983. matrix_mdev = vfio_ap_mdev_for_queue(q);
  1984. /* If the queue is assigned to the matrix mediated device, then
  1985. * determine whether it is passed through to a guest; otherwise,
  1986. * indicate that it is unassigned.
  1987. */
  1988. if (matrix_mdev) {
  1989. apid = AP_QID_CARD(q->apqn);
  1990. apqi = AP_QID_QUEUE(q->apqn);
  1991. /*
  1992. * If the queue is passed through to the guest, then indicate
  1993. * that it is in use; otherwise, indicate that it is
  1994. * merely assigned to a matrix mediated device.
  1995. */
  1996. if (matrix_mdev->kvm &&
  1997. test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
  1998. test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm))
  1999. nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_IN_USE);
  2000. else
  2001. nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_ASSIGNED);
  2002. } else {
  2003. nchars = sysfs_emit(buf, "%s\n", AP_QUEUE_UNASSIGNED);
  2004. }
  2005. mutex_unlock(&matrix_dev->mdevs_lock);
  2006. return nchars;
  2007. }
  2008. static DEVICE_ATTR_RO(status);
  2009. static struct attribute *vfio_queue_attrs[] = {
  2010. &dev_attr_status.attr,
  2011. NULL,
  2012. };
  2013. static const struct attribute_group vfio_queue_attr_group = {
  2014. .attrs = vfio_queue_attrs,
  2015. };
  2016. static const struct vfio_device_ops vfio_ap_matrix_dev_ops = {
  2017. .init = vfio_ap_mdev_init_dev,
  2018. .open_device = vfio_ap_mdev_open_device,
  2019. .close_device = vfio_ap_mdev_close_device,
  2020. .ioctl = vfio_ap_mdev_ioctl,
  2021. .dma_unmap = vfio_ap_mdev_dma_unmap,
  2022. .bind_iommufd = vfio_iommufd_emulated_bind,
  2023. .unbind_iommufd = vfio_iommufd_emulated_unbind,
  2024. .attach_ioas = vfio_iommufd_emulated_attach_ioas,
  2025. .detach_ioas = vfio_iommufd_emulated_detach_ioas,
  2026. .request = vfio_ap_mdev_request
  2027. };
  2028. static struct mdev_driver vfio_ap_matrix_driver = {
  2029. .device_api = VFIO_DEVICE_API_AP_STRING,
  2030. .max_instances = MAX_ZDEV_ENTRIES_EXT,
  2031. .driver = {
  2032. .name = "vfio_ap_mdev",
  2033. .owner = THIS_MODULE,
  2034. .mod_name = KBUILD_MODNAME,
  2035. .dev_groups = vfio_ap_mdev_attr_groups,
  2036. },
  2037. .probe = vfio_ap_mdev_probe,
  2038. .remove = vfio_ap_mdev_remove,
  2039. };
  2040. int vfio_ap_mdev_register(void)
  2041. {
  2042. int ret;
  2043. ret = mdev_register_driver(&vfio_ap_matrix_driver);
  2044. if (ret)
  2045. return ret;
  2046. matrix_dev->mdev_type.sysfs_name = VFIO_AP_MDEV_TYPE_HWVIRT;
  2047. matrix_dev->mdev_type.pretty_name = VFIO_AP_MDEV_NAME_HWVIRT;
  2048. matrix_dev->mdev_types = &matrix_dev->mdev_type;
  2049. ret = mdev_register_parent(&matrix_dev->parent, &matrix_dev->device,
  2050. &vfio_ap_matrix_driver,
  2051. &matrix_dev->mdev_types, 1);
  2052. if (ret)
  2053. goto err_driver;
  2054. return 0;
  2055. err_driver:
  2056. mdev_unregister_driver(&vfio_ap_matrix_driver);
  2057. return ret;
  2058. }
  2059. void vfio_ap_mdev_unregister(void)
  2060. {
  2061. mdev_unregister_parent(&matrix_dev->parent);
  2062. mdev_unregister_driver(&vfio_ap_matrix_driver);
  2063. }
  2064. int vfio_ap_mdev_probe_queue(struct ap_device *apdev)
  2065. {
  2066. int ret;
  2067. struct vfio_ap_queue *q;
  2068. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  2069. struct ap_matrix_mdev *matrix_mdev;
  2070. ret = sysfs_create_group(&apdev->device.kobj, &vfio_queue_attr_group);
  2071. if (ret)
  2072. return ret;
  2073. q = kzalloc_obj(*q);
  2074. if (!q) {
  2075. ret = -ENOMEM;
  2076. goto err_remove_group;
  2077. }
  2078. q->apqn = to_ap_queue(&apdev->device)->qid;
  2079. q->saved_isc = VFIO_AP_ISC_INVALID;
  2080. memset(&q->reset_status, 0, sizeof(q->reset_status));
  2081. INIT_WORK(&q->reset_work, apq_reset_check);
  2082. matrix_mdev = get_update_locks_by_apqn(q->apqn);
  2083. if (matrix_mdev) {
  2084. vfio_ap_mdev_link_queue(matrix_mdev, q);
  2085. /*
  2086. * If we're in the process of handling the adding of adapters or
  2087. * domains to the host's AP configuration, then let the
  2088. * vfio_ap device driver's on_scan_complete callback filter the
  2089. * matrix and update the guest's AP configuration after all of
  2090. * the new queue devices are probed.
  2091. */
  2092. if (!bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) ||
  2093. !bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS))
  2094. goto done;
  2095. if (vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered)) {
  2096. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  2097. reset_queues_for_apids(matrix_mdev, apm_filtered);
  2098. }
  2099. }
  2100. done:
  2101. dev_set_drvdata(&apdev->device, q);
  2102. release_update_locks_for_mdev(matrix_mdev);
  2103. return ret;
  2104. err_remove_group:
  2105. sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
  2106. return ret;
  2107. }
  2108. void vfio_ap_mdev_remove_queue(struct ap_device *apdev)
  2109. {
  2110. unsigned long apid, apqi;
  2111. struct vfio_ap_queue *q;
  2112. struct ap_matrix_mdev *matrix_mdev;
  2113. sysfs_remove_group(&apdev->device.kobj, &vfio_queue_attr_group);
  2114. q = dev_get_drvdata(&apdev->device);
  2115. get_update_locks_for_queue(q);
  2116. matrix_mdev = q->matrix_mdev;
  2117. apid = AP_QID_CARD(q->apqn);
  2118. apqi = AP_QID_QUEUE(q->apqn);
  2119. if (matrix_mdev) {
  2120. /* If the queue is assigned to the guest's AP configuration */
  2121. if (test_bit_inv(apid, matrix_mdev->shadow_apcb.apm) &&
  2122. test_bit_inv(apqi, matrix_mdev->shadow_apcb.aqm)) {
  2123. /*
  2124. * Since the queues are defined via a matrix of adapters
  2125. * and domains, it is not possible to hot unplug a
  2126. * single queue; so, let's unplug the adapter.
  2127. */
  2128. clear_bit_inv(apid, matrix_mdev->shadow_apcb.apm);
  2129. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  2130. reset_queues_for_apid(matrix_mdev, apid);
  2131. goto done;
  2132. }
  2133. }
  2134. /*
  2135. * If the queue is not in the host's AP configuration, then resetting
  2136. * it will fail with response code 01, (APQN not valid); so, let's make
  2137. * sure it is in the host's config.
  2138. */
  2139. if (test_bit_inv(apid, (unsigned long *)matrix_dev->info.apm) &&
  2140. test_bit_inv(apqi, (unsigned long *)matrix_dev->info.aqm)) {
  2141. vfio_ap_mdev_reset_queue(q);
  2142. flush_work(&q->reset_work);
  2143. }
  2144. done:
  2145. if (matrix_mdev)
  2146. vfio_ap_unlink_queue_fr_mdev(q);
  2147. dev_set_drvdata(&apdev->device, NULL);
  2148. kfree(q);
  2149. release_update_locks_for_mdev(matrix_mdev);
  2150. }
  2151. /**
  2152. * vfio_ap_mdev_resource_in_use: check whether any of a set of APQNs is
  2153. * assigned to a mediated device under the control
  2154. * of the vfio_ap device driver.
  2155. *
  2156. * @apm: a bitmap specifying a set of APIDs comprising the APQNs to check.
  2157. * @aqm: a bitmap specifying a set of APQIs comprising the APQNs to check.
  2158. *
  2159. * Return:
  2160. * * -EADDRINUSE if one or more of the APQNs specified via @apm/@aqm are
  2161. * assigned to a mediated device under the control of the vfio_ap
  2162. * device driver.
  2163. * * Otherwise, return 0.
  2164. */
  2165. int vfio_ap_mdev_resource_in_use(unsigned long *apm, unsigned long *aqm)
  2166. {
  2167. int ret;
  2168. mutex_lock(&matrix_dev->guests_lock);
  2169. mutex_lock(&matrix_dev->mdevs_lock);
  2170. ret = vfio_ap_mdev_verify_no_sharing(NULL, apm, aqm);
  2171. mutex_unlock(&matrix_dev->mdevs_lock);
  2172. mutex_unlock(&matrix_dev->guests_lock);
  2173. return ret;
  2174. }
  2175. /**
  2176. * vfio_ap_mdev_hot_unplug_cfg - hot unplug the adapters, domains and control
  2177. * domains that have been removed from the host's
  2178. * AP configuration from a guest.
  2179. *
  2180. * @matrix_mdev: an ap_matrix_mdev object attached to a KVM guest.
  2181. * @aprem: the adapters that have been removed from the host's AP configuration
  2182. * @aqrem: the domains that have been removed from the host's AP configuration
  2183. * @cdrem: the control domains that have been removed from the host's AP
  2184. * configuration.
  2185. */
  2186. static void vfio_ap_mdev_hot_unplug_cfg(struct ap_matrix_mdev *matrix_mdev,
  2187. unsigned long *aprem,
  2188. unsigned long *aqrem,
  2189. unsigned long *cdrem)
  2190. {
  2191. int do_hotplug = 0;
  2192. if (!bitmap_empty(aprem, AP_DEVICES)) {
  2193. do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.apm,
  2194. matrix_mdev->shadow_apcb.apm,
  2195. aprem, AP_DEVICES);
  2196. }
  2197. if (!bitmap_empty(aqrem, AP_DOMAINS)) {
  2198. do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.aqm,
  2199. matrix_mdev->shadow_apcb.aqm,
  2200. aqrem, AP_DEVICES);
  2201. }
  2202. if (!bitmap_empty(cdrem, AP_DOMAINS))
  2203. do_hotplug |= bitmap_andnot(matrix_mdev->shadow_apcb.adm,
  2204. matrix_mdev->shadow_apcb.adm,
  2205. cdrem, AP_DOMAINS);
  2206. if (do_hotplug)
  2207. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  2208. }
  2209. /**
  2210. * vfio_ap_mdev_cfg_remove - determines which guests are using the adapters,
  2211. * domains and control domains that have been removed
  2212. * from the host AP configuration and unplugs them
  2213. * from those guests.
  2214. *
  2215. * @ap_remove: bitmap specifying which adapters have been removed from the host
  2216. * config.
  2217. * @aq_remove: bitmap specifying which domains have been removed from the host
  2218. * config.
  2219. * @cd_remove: bitmap specifying which control domains have been removed from
  2220. * the host config.
  2221. */
  2222. static void vfio_ap_mdev_cfg_remove(unsigned long *ap_remove,
  2223. unsigned long *aq_remove,
  2224. unsigned long *cd_remove)
  2225. {
  2226. struct ap_matrix_mdev *matrix_mdev;
  2227. DECLARE_BITMAP(aprem, AP_DEVICES);
  2228. DECLARE_BITMAP(aqrem, AP_DOMAINS);
  2229. DECLARE_BITMAP(cdrem, AP_DOMAINS);
  2230. int do_remove = 0;
  2231. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  2232. mutex_lock(&matrix_mdev->kvm->lock);
  2233. mutex_lock(&matrix_dev->mdevs_lock);
  2234. do_remove |= bitmap_and(aprem, ap_remove,
  2235. matrix_mdev->matrix.apm,
  2236. AP_DEVICES);
  2237. do_remove |= bitmap_and(aqrem, aq_remove,
  2238. matrix_mdev->matrix.aqm,
  2239. AP_DOMAINS);
  2240. do_remove |= bitmap_andnot(cdrem, cd_remove,
  2241. matrix_mdev->matrix.adm,
  2242. AP_DOMAINS);
  2243. if (do_remove)
  2244. vfio_ap_mdev_hot_unplug_cfg(matrix_mdev, aprem, aqrem,
  2245. cdrem);
  2246. mutex_unlock(&matrix_dev->mdevs_lock);
  2247. mutex_unlock(&matrix_mdev->kvm->lock);
  2248. }
  2249. }
  2250. /**
  2251. * vfio_ap_mdev_on_cfg_remove - responds to the removal of adapters, domains and
  2252. * control domains from the host AP configuration
  2253. * by unplugging them from the guests that are
  2254. * using them.
  2255. * @cur_config_info: the current host AP configuration information
  2256. * @prev_config_info: the previous host AP configuration information
  2257. */
  2258. static void vfio_ap_mdev_on_cfg_remove(struct ap_config_info *cur_config_info,
  2259. struct ap_config_info *prev_config_info)
  2260. {
  2261. int do_remove;
  2262. DECLARE_BITMAP(aprem, AP_DEVICES);
  2263. DECLARE_BITMAP(aqrem, AP_DOMAINS);
  2264. DECLARE_BITMAP(cdrem, AP_DOMAINS);
  2265. do_remove = bitmap_andnot(aprem,
  2266. (unsigned long *)prev_config_info->apm,
  2267. (unsigned long *)cur_config_info->apm,
  2268. AP_DEVICES);
  2269. do_remove |= bitmap_andnot(aqrem,
  2270. (unsigned long *)prev_config_info->aqm,
  2271. (unsigned long *)cur_config_info->aqm,
  2272. AP_DEVICES);
  2273. do_remove |= bitmap_andnot(cdrem,
  2274. (unsigned long *)prev_config_info->adm,
  2275. (unsigned long *)cur_config_info->adm,
  2276. AP_DEVICES);
  2277. if (do_remove)
  2278. vfio_ap_mdev_cfg_remove(aprem, aqrem, cdrem);
  2279. }
  2280. /**
  2281. * vfio_ap_filter_apid_by_qtype: filter APIDs from an AP mask for adapters that
  2282. * are older than AP type 10 (CEX4).
  2283. * @apm: a bitmap of the APIDs to examine
  2284. * @aqm: a bitmap of the APQIs of the queues to query for the AP type.
  2285. */
  2286. static void vfio_ap_filter_apid_by_qtype(unsigned long *apm, unsigned long *aqm)
  2287. {
  2288. bool apid_cleared;
  2289. struct ap_queue_status status;
  2290. unsigned long apid, apqi;
  2291. struct ap_tapq_hwinfo info;
  2292. for_each_set_bit_inv(apid, apm, AP_DEVICES) {
  2293. apid_cleared = false;
  2294. for_each_set_bit_inv(apqi, aqm, AP_DOMAINS) {
  2295. status = ap_test_queue(AP_MKQID(apid, apqi), 1, &info);
  2296. switch (status.response_code) {
  2297. /*
  2298. * According to the architecture in each case
  2299. * below, the queue's info should be filled.
  2300. */
  2301. case AP_RESPONSE_NORMAL:
  2302. case AP_RESPONSE_RESET_IN_PROGRESS:
  2303. case AP_RESPONSE_DECONFIGURED:
  2304. case AP_RESPONSE_CHECKSTOPPED:
  2305. case AP_RESPONSE_BUSY:
  2306. /*
  2307. * The vfio_ap device driver only
  2308. * supports CEX4 and newer adapters, so
  2309. * remove the APID if the adapter is
  2310. * older than a CEX4.
  2311. */
  2312. if (info.at < AP_DEVICE_TYPE_CEX4) {
  2313. clear_bit_inv(apid, apm);
  2314. apid_cleared = true;
  2315. }
  2316. break;
  2317. default:
  2318. /*
  2319. * If we don't know the adapter type,
  2320. * clear its APID since it can't be
  2321. * determined whether the vfio_ap
  2322. * device driver supports it.
  2323. */
  2324. clear_bit_inv(apid, apm);
  2325. apid_cleared = true;
  2326. break;
  2327. }
  2328. /*
  2329. * If we've already cleared the APID from the apm, there
  2330. * is no need to continue examining the remainin AP
  2331. * queues to determine the type of the adapter.
  2332. */
  2333. if (apid_cleared)
  2334. continue;
  2335. }
  2336. }
  2337. }
  2338. /**
  2339. * vfio_ap_mdev_cfg_add - store bitmaps specifying the adapters, domains and
  2340. * control domains that have been added to the host's
  2341. * AP configuration for each matrix mdev to which they
  2342. * are assigned.
  2343. *
  2344. * @apm_add: a bitmap specifying the adapters that have been added to the AP
  2345. * configuration.
  2346. * @aqm_add: a bitmap specifying the domains that have been added to the AP
  2347. * configuration.
  2348. * @adm_add: a bitmap specifying the control domains that have been added to the
  2349. * AP configuration.
  2350. */
  2351. static void vfio_ap_mdev_cfg_add(unsigned long *apm_add, unsigned long *aqm_add,
  2352. unsigned long *adm_add)
  2353. {
  2354. struct ap_matrix_mdev *matrix_mdev;
  2355. if (list_empty(&matrix_dev->mdev_list))
  2356. return;
  2357. vfio_ap_filter_apid_by_qtype(apm_add, aqm_add);
  2358. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  2359. bitmap_and(matrix_mdev->apm_add,
  2360. matrix_mdev->matrix.apm, apm_add, AP_DEVICES);
  2361. bitmap_and(matrix_mdev->aqm_add,
  2362. matrix_mdev->matrix.aqm, aqm_add, AP_DOMAINS);
  2363. bitmap_and(matrix_mdev->adm_add,
  2364. matrix_mdev->matrix.adm, adm_add, AP_DEVICES);
  2365. }
  2366. }
  2367. /**
  2368. * vfio_ap_mdev_on_cfg_add - responds to the addition of adapters, domains and
  2369. * control domains to the host AP configuration
  2370. * by updating the bitmaps that specify what adapters,
  2371. * domains and control domains have been added so they
  2372. * can be hot plugged into the guest when the AP bus
  2373. * scan completes (see vfio_ap_on_scan_complete
  2374. * function).
  2375. * @cur_config_info: the current AP configuration information
  2376. * @prev_config_info: the previous AP configuration information
  2377. */
  2378. static void vfio_ap_mdev_on_cfg_add(struct ap_config_info *cur_config_info,
  2379. struct ap_config_info *prev_config_info)
  2380. {
  2381. bool do_add;
  2382. DECLARE_BITMAP(apm_add, AP_DEVICES);
  2383. DECLARE_BITMAP(aqm_add, AP_DOMAINS);
  2384. DECLARE_BITMAP(adm_add, AP_DOMAINS);
  2385. do_add = bitmap_andnot(apm_add,
  2386. (unsigned long *)cur_config_info->apm,
  2387. (unsigned long *)prev_config_info->apm,
  2388. AP_DEVICES);
  2389. do_add |= bitmap_andnot(aqm_add,
  2390. (unsigned long *)cur_config_info->aqm,
  2391. (unsigned long *)prev_config_info->aqm,
  2392. AP_DOMAINS);
  2393. do_add |= bitmap_andnot(adm_add,
  2394. (unsigned long *)cur_config_info->adm,
  2395. (unsigned long *)prev_config_info->adm,
  2396. AP_DOMAINS);
  2397. if (do_add)
  2398. vfio_ap_mdev_cfg_add(apm_add, aqm_add, adm_add);
  2399. }
  2400. /**
  2401. * vfio_ap_on_cfg_changed - handles notification of changes to the host AP
  2402. * configuration.
  2403. *
  2404. * @cur_cfg_info: the current host AP configuration
  2405. * @prev_cfg_info: the previous host AP configuration
  2406. */
  2407. void vfio_ap_on_cfg_changed(struct ap_config_info *cur_cfg_info,
  2408. struct ap_config_info *prev_cfg_info)
  2409. {
  2410. if (!cur_cfg_info || !prev_cfg_info)
  2411. return;
  2412. mutex_lock(&matrix_dev->guests_lock);
  2413. vfio_ap_mdev_on_cfg_remove(cur_cfg_info, prev_cfg_info);
  2414. vfio_ap_mdev_on_cfg_add(cur_cfg_info, prev_cfg_info);
  2415. memcpy(&matrix_dev->info, cur_cfg_info, sizeof(*cur_cfg_info));
  2416. mutex_unlock(&matrix_dev->guests_lock);
  2417. }
  2418. static void vfio_ap_mdev_hot_plug_cfg(struct ap_matrix_mdev *matrix_mdev)
  2419. {
  2420. DECLARE_BITMAP(apm_filtered, AP_DEVICES);
  2421. bool filter_domains, filter_adapters, filter_cdoms, do_hotplug = false;
  2422. mutex_lock(&matrix_mdev->kvm->lock);
  2423. mutex_lock(&matrix_dev->mdevs_lock);
  2424. filter_adapters = bitmap_intersects(matrix_mdev->matrix.apm,
  2425. matrix_mdev->apm_add, AP_DEVICES);
  2426. filter_domains = bitmap_intersects(matrix_mdev->matrix.aqm,
  2427. matrix_mdev->aqm_add, AP_DOMAINS);
  2428. filter_cdoms = bitmap_intersects(matrix_mdev->matrix.adm,
  2429. matrix_mdev->adm_add, AP_DOMAINS);
  2430. if (filter_adapters || filter_domains)
  2431. do_hotplug = vfio_ap_mdev_filter_matrix(matrix_mdev, apm_filtered);
  2432. if (filter_cdoms)
  2433. do_hotplug |= vfio_ap_mdev_filter_cdoms(matrix_mdev);
  2434. if (do_hotplug)
  2435. vfio_ap_mdev_update_guest_apcb(matrix_mdev);
  2436. reset_queues_for_apids(matrix_mdev, apm_filtered);
  2437. mutex_unlock(&matrix_dev->mdevs_lock);
  2438. mutex_unlock(&matrix_mdev->kvm->lock);
  2439. }
  2440. void vfio_ap_on_scan_complete(struct ap_config_info *new_config_info,
  2441. struct ap_config_info *old_config_info)
  2442. {
  2443. struct ap_matrix_mdev *matrix_mdev;
  2444. mutex_lock(&matrix_dev->guests_lock);
  2445. list_for_each_entry(matrix_mdev, &matrix_dev->mdev_list, node) {
  2446. if (bitmap_empty(matrix_mdev->apm_add, AP_DEVICES) &&
  2447. bitmap_empty(matrix_mdev->aqm_add, AP_DOMAINS) &&
  2448. bitmap_empty(matrix_mdev->adm_add, AP_DOMAINS))
  2449. continue;
  2450. vfio_ap_mdev_hot_plug_cfg(matrix_mdev);
  2451. bitmap_clear(matrix_mdev->apm_add, 0, AP_DEVICES);
  2452. bitmap_clear(matrix_mdev->aqm_add, 0, AP_DOMAINS);
  2453. bitmap_clear(matrix_mdev->adm_add, 0, AP_DOMAINS);
  2454. }
  2455. mutex_unlock(&matrix_dev->guests_lock);
  2456. }