i40e_virtchnl_pf.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "i40e.h"
  4. /*********************notification routines***********************/
  5. /**
  6. * i40e_vc_vf_broadcast
  7. * @pf: pointer to the PF structure
  8. * @v_opcode: operation code
  9. * @v_retval: return value
  10. * @msg: pointer to the msg buffer
  11. * @msglen: msg length
  12. *
  13. * send a message to all VFs on a given PF
  14. **/
  15. static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
  16. enum virtchnl_ops v_opcode,
  17. i40e_status v_retval, u8 *msg,
  18. u16 msglen)
  19. {
  20. struct i40e_hw *hw = &pf->hw;
  21. struct i40e_vf *vf = pf->vf;
  22. int i;
  23. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  24. int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
  25. /* Not all vfs are enabled so skip the ones that are not */
  26. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
  27. !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  28. continue;
  29. /* Ignore return value on purpose - a given VF may fail, but
  30. * we need to keep going and send to all of them
  31. */
  32. i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  33. msg, msglen, NULL);
  34. }
  35. }
  36. /**
  37. * i40e_vc_notify_vf_link_state
  38. * @vf: pointer to the VF structure
  39. *
  40. * send a link status message to a single VF
  41. **/
  42. static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
  43. {
  44. struct virtchnl_pf_event pfe;
  45. struct i40e_pf *pf = vf->pf;
  46. struct i40e_hw *hw = &pf->hw;
  47. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  48. int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
  49. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  50. pfe.severity = PF_EVENT_SEVERITY_INFO;
  51. if (vf->link_forced) {
  52. pfe.event_data.link_event.link_status = vf->link_up;
  53. pfe.event_data.link_event.link_speed =
  54. (vf->link_up ? VIRTCHNL_LINK_SPEED_40GB : 0);
  55. } else {
  56. pfe.event_data.link_event.link_status =
  57. ls->link_info & I40E_AQ_LINK_UP;
  58. pfe.event_data.link_event.link_speed =
  59. i40e_virtchnl_link_speed(ls->link_speed);
  60. }
  61. i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  62. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  63. }
  64. /**
  65. * i40e_vc_notify_link_state
  66. * @pf: pointer to the PF structure
  67. *
  68. * send a link status message to all VFs on a given PF
  69. **/
  70. void i40e_vc_notify_link_state(struct i40e_pf *pf)
  71. {
  72. int i;
  73. for (i = 0; i < pf->num_alloc_vfs; i++)
  74. i40e_vc_notify_vf_link_state(&pf->vf[i]);
  75. }
  76. /**
  77. * i40e_vc_notify_reset
  78. * @pf: pointer to the PF structure
  79. *
  80. * indicate a pending reset to all VFs on a given PF
  81. **/
  82. void i40e_vc_notify_reset(struct i40e_pf *pf)
  83. {
  84. struct virtchnl_pf_event pfe;
  85. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  86. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  87. i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
  88. (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
  89. }
  90. /**
  91. * i40e_vc_notify_vf_reset
  92. * @vf: pointer to the VF structure
  93. *
  94. * indicate a pending reset to the given VF
  95. **/
  96. void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
  97. {
  98. struct virtchnl_pf_event pfe;
  99. int abs_vf_id;
  100. /* validate the request */
  101. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  102. return;
  103. /* verify if the VF is in either init or active before proceeding */
  104. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
  105. !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  106. return;
  107. abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
  108. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  109. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  110. i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  111. 0, (u8 *)&pfe,
  112. sizeof(struct virtchnl_pf_event), NULL);
  113. }
  114. /***********************misc routines*****************************/
  115. /**
  116. * i40e_vc_disable_vf
  117. * @vf: pointer to the VF info
  118. *
  119. * Disable the VF through a SW reset.
  120. **/
  121. static inline void i40e_vc_disable_vf(struct i40e_vf *vf)
  122. {
  123. struct i40e_pf *pf = vf->pf;
  124. int i;
  125. i40e_vc_notify_vf_reset(vf);
  126. /* We want to ensure that an actual reset occurs initiated after this
  127. * function was called. However, we do not want to wait forever, so
  128. * we'll give a reasonable time and print a message if we failed to
  129. * ensure a reset.
  130. */
  131. for (i = 0; i < 20; i++) {
  132. /* If PF is in VFs releasing state reset VF is impossible,
  133. * so leave it.
  134. */
  135. if (test_bit(__I40E_VFS_RELEASING, pf->state))
  136. return;
  137. if (i40e_reset_vf(vf, false))
  138. return;
  139. usleep_range(10000, 20000);
  140. }
  141. dev_warn(&vf->pf->pdev->dev,
  142. "Failed to initiate reset for VF %d after 200 milliseconds\n",
  143. vf->vf_id);
  144. }
  145. /**
  146. * i40e_vc_isvalid_vsi_id
  147. * @vf: pointer to the VF info
  148. * @vsi_id: VF relative VSI id
  149. *
  150. * check for the valid VSI id
  151. **/
  152. static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
  153. {
  154. struct i40e_pf *pf = vf->pf;
  155. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  156. return (vsi && (vsi->vf_id == vf->vf_id));
  157. }
  158. /**
  159. * i40e_vc_isvalid_queue_id
  160. * @vf: pointer to the VF info
  161. * @vsi_id: vsi id
  162. * @qid: vsi relative queue id
  163. *
  164. * check for the valid queue id
  165. **/
  166. static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
  167. u16 qid)
  168. {
  169. struct i40e_pf *pf = vf->pf;
  170. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  171. return (vsi && (qid < vsi->alloc_queue_pairs));
  172. }
  173. /**
  174. * i40e_vc_isvalid_vector_id
  175. * @vf: pointer to the VF info
  176. * @vector_id: VF relative vector id
  177. *
  178. * check for the valid vector id
  179. **/
  180. static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u32 vector_id)
  181. {
  182. struct i40e_pf *pf = vf->pf;
  183. return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
  184. }
  185. /***********************vf resource mgmt routines*****************/
  186. /**
  187. * i40e_vc_get_pf_queue_id
  188. * @vf: pointer to the VF info
  189. * @vsi_id: id of VSI as provided by the FW
  190. * @vsi_queue_id: vsi relative queue id
  191. *
  192. * return PF relative queue id
  193. **/
  194. static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
  195. u8 vsi_queue_id)
  196. {
  197. struct i40e_pf *pf = vf->pf;
  198. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  199. u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
  200. if (!vsi)
  201. return pf_queue_id;
  202. if (le16_to_cpu(vsi->info.mapping_flags) &
  203. I40E_AQ_VSI_QUE_MAP_NONCONTIG)
  204. pf_queue_id =
  205. le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
  206. else
  207. pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
  208. vsi_queue_id;
  209. return pf_queue_id;
  210. }
  211. /**
  212. * i40e_get_real_pf_qid
  213. * @vf: pointer to the VF info
  214. * @vsi_id: vsi id
  215. * @queue_id: queue number
  216. *
  217. * wrapper function to get pf_queue_id handling ADq code as well
  218. **/
  219. static u16 i40e_get_real_pf_qid(struct i40e_vf *vf, u16 vsi_id, u16 queue_id)
  220. {
  221. int i;
  222. if (vf->adq_enabled) {
  223. /* Although VF considers all the queues(can be 1 to 16) as its
  224. * own but they may actually belong to different VSIs(up to 4).
  225. * We need to find which queues belongs to which VSI.
  226. */
  227. for (i = 0; i < vf->num_tc; i++) {
  228. if (queue_id < vf->ch[i].num_qps) {
  229. vsi_id = vf->ch[i].vsi_id;
  230. break;
  231. }
  232. /* find right queue id which is relative to a
  233. * given VSI.
  234. */
  235. queue_id -= vf->ch[i].num_qps;
  236. }
  237. }
  238. return i40e_vc_get_pf_queue_id(vf, vsi_id, queue_id);
  239. }
  240. /**
  241. * i40e_config_irq_link_list
  242. * @vf: pointer to the VF info
  243. * @vsi_id: id of VSI as given by the FW
  244. * @vecmap: irq map info
  245. *
  246. * configure irq link list from the map
  247. **/
  248. static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
  249. struct virtchnl_vector_map *vecmap)
  250. {
  251. unsigned long linklistmap = 0, tempmap;
  252. struct i40e_pf *pf = vf->pf;
  253. struct i40e_hw *hw = &pf->hw;
  254. u16 vsi_queue_id, pf_queue_id;
  255. enum i40e_queue_type qtype;
  256. u16 next_q, vector_id, size;
  257. u32 reg, reg_idx;
  258. u16 itr_idx = 0;
  259. vector_id = vecmap->vector_id;
  260. /* setup the head */
  261. if (0 == vector_id)
  262. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  263. else
  264. reg_idx = I40E_VPINT_LNKLSTN(
  265. ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
  266. (vector_id - 1));
  267. if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
  268. /* Special case - No queues mapped on this vector */
  269. wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
  270. goto irq_list_done;
  271. }
  272. tempmap = vecmap->rxq_map;
  273. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  274. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  275. vsi_queue_id));
  276. }
  277. tempmap = vecmap->txq_map;
  278. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  279. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  280. vsi_queue_id + 1));
  281. }
  282. size = I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES;
  283. next_q = find_first_bit(&linklistmap, size);
  284. if (unlikely(next_q == size))
  285. goto irq_list_done;
  286. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  287. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  288. pf_queue_id = i40e_get_real_pf_qid(vf, vsi_id, vsi_queue_id);
  289. reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
  290. wr32(hw, reg_idx, reg);
  291. while (next_q < size) {
  292. switch (qtype) {
  293. case I40E_QUEUE_TYPE_RX:
  294. reg_idx = I40E_QINT_RQCTL(pf_queue_id);
  295. itr_idx = vecmap->rxitr_idx;
  296. break;
  297. case I40E_QUEUE_TYPE_TX:
  298. reg_idx = I40E_QINT_TQCTL(pf_queue_id);
  299. itr_idx = vecmap->txitr_idx;
  300. break;
  301. default:
  302. break;
  303. }
  304. next_q = find_next_bit(&linklistmap, size, next_q + 1);
  305. if (next_q < size) {
  306. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  307. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  308. pf_queue_id = i40e_get_real_pf_qid(vf,
  309. vsi_id,
  310. vsi_queue_id);
  311. } else {
  312. pf_queue_id = I40E_QUEUE_END_OF_LIST;
  313. qtype = 0;
  314. }
  315. /* format for the RQCTL & TQCTL regs is same */
  316. reg = (vector_id) |
  317. (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
  318. (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  319. BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
  320. (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
  321. wr32(hw, reg_idx, reg);
  322. }
  323. /* if the vf is running in polling mode and using interrupt zero,
  324. * need to disable auto-mask on enabling zero interrupt for VFs.
  325. */
  326. if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
  327. (vector_id == 0)) {
  328. reg = rd32(hw, I40E_GLINT_CTL);
  329. if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
  330. reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
  331. wr32(hw, I40E_GLINT_CTL, reg);
  332. }
  333. }
  334. irq_list_done:
  335. i40e_flush(hw);
  336. }
  337. /**
  338. * i40e_release_iwarp_qvlist
  339. * @vf: pointer to the VF.
  340. *
  341. **/
  342. static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
  343. {
  344. struct i40e_pf *pf = vf->pf;
  345. struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
  346. u32 msix_vf;
  347. u32 i;
  348. if (!vf->qvlist_info)
  349. return;
  350. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  351. for (i = 0; i < qvlist_info->num_vectors; i++) {
  352. struct virtchnl_iwarp_qv_info *qv_info;
  353. u32 next_q_index, next_q_type;
  354. struct i40e_hw *hw = &pf->hw;
  355. u32 v_idx, reg_idx, reg;
  356. qv_info = &qvlist_info->qv_info[i];
  357. if (!qv_info)
  358. continue;
  359. v_idx = qv_info->v_idx;
  360. if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
  361. /* Figure out the queue after CEQ and make that the
  362. * first queue.
  363. */
  364. reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
  365. reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
  366. next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
  367. >> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
  368. next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
  369. >> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
  370. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  371. reg = (next_q_index &
  372. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  373. (next_q_type <<
  374. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  375. wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
  376. }
  377. }
  378. kfree(vf->qvlist_info);
  379. vf->qvlist_info = NULL;
  380. }
  381. /**
  382. * i40e_config_iwarp_qvlist
  383. * @vf: pointer to the VF info
  384. * @qvlist_info: queue and vector list
  385. *
  386. * Return 0 on success or < 0 on error
  387. **/
  388. static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
  389. struct virtchnl_iwarp_qvlist_info *qvlist_info)
  390. {
  391. struct i40e_pf *pf = vf->pf;
  392. struct i40e_hw *hw = &pf->hw;
  393. struct virtchnl_iwarp_qv_info *qv_info;
  394. u32 v_idx, i, reg_idx, reg;
  395. u32 next_q_idx, next_q_type;
  396. u32 msix_vf, size;
  397. int ret = 0;
  398. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  399. if (qvlist_info->num_vectors > msix_vf) {
  400. dev_warn(&pf->pdev->dev,
  401. "Incorrect number of iwarp vectors %u. Maximum %u allowed.\n",
  402. qvlist_info->num_vectors,
  403. msix_vf);
  404. ret = -EINVAL;
  405. goto err_out;
  406. }
  407. size = sizeof(struct virtchnl_iwarp_qvlist_info) +
  408. (sizeof(struct virtchnl_iwarp_qv_info) *
  409. (qvlist_info->num_vectors - 1));
  410. kfree(vf->qvlist_info);
  411. vf->qvlist_info = kzalloc(size, GFP_KERNEL);
  412. if (!vf->qvlist_info) {
  413. ret = -ENOMEM;
  414. goto err_out;
  415. }
  416. vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
  417. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  418. for (i = 0; i < qvlist_info->num_vectors; i++) {
  419. qv_info = &qvlist_info->qv_info[i];
  420. if (!qv_info)
  421. continue;
  422. v_idx = qv_info->v_idx;
  423. /* Validate vector id belongs to this vf */
  424. if (!i40e_vc_isvalid_vector_id(vf, v_idx)) {
  425. ret = -EINVAL;
  426. goto err_free;
  427. }
  428. vf->qvlist_info->qv_info[i] = *qv_info;
  429. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  430. /* We might be sharing the interrupt, so get the first queue
  431. * index and type, push it down the list by adding the new
  432. * queue on top. Also link it with the new queue in CEQCTL.
  433. */
  434. reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
  435. next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
  436. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
  437. next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
  438. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  439. if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
  440. reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
  441. reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
  442. (v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
  443. (qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
  444. (next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
  445. (next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
  446. wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
  447. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  448. reg = (qv_info->ceq_idx &
  449. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  450. (I40E_QUEUE_TYPE_PE_CEQ <<
  451. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  452. wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
  453. }
  454. if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
  455. reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
  456. (v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
  457. (qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
  458. wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
  459. }
  460. }
  461. return 0;
  462. err_free:
  463. kfree(vf->qvlist_info);
  464. vf->qvlist_info = NULL;
  465. err_out:
  466. return ret;
  467. }
  468. /**
  469. * i40e_config_vsi_tx_queue
  470. * @vf: pointer to the VF info
  471. * @vsi_id: id of VSI as provided by the FW
  472. * @vsi_queue_id: vsi relative queue index
  473. * @info: config. info
  474. *
  475. * configure tx queue
  476. **/
  477. static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
  478. u16 vsi_queue_id,
  479. struct virtchnl_txq_info *info)
  480. {
  481. struct i40e_pf *pf = vf->pf;
  482. struct i40e_hw *hw = &pf->hw;
  483. struct i40e_hmc_obj_txq tx_ctx;
  484. struct i40e_vsi *vsi;
  485. u16 pf_queue_id;
  486. u32 qtx_ctl;
  487. int ret = 0;
  488. if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
  489. ret = -ENOENT;
  490. goto error_context;
  491. }
  492. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  493. vsi = i40e_find_vsi_from_id(pf, vsi_id);
  494. if (!vsi) {
  495. ret = -ENOENT;
  496. goto error_context;
  497. }
  498. /* clear the context structure first */
  499. memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
  500. /* only set the required fields */
  501. tx_ctx.base = info->dma_ring_addr / 128;
  502. tx_ctx.qlen = info->ring_len;
  503. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
  504. tx_ctx.rdylist_act = 0;
  505. tx_ctx.head_wb_ena = info->headwb_enabled;
  506. tx_ctx.head_wb_addr = info->dma_headwb_addr;
  507. /* clear the context in the HMC */
  508. ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
  509. if (ret) {
  510. dev_err(&pf->pdev->dev,
  511. "Failed to clear VF LAN Tx queue context %d, error: %d\n",
  512. pf_queue_id, ret);
  513. ret = -ENOENT;
  514. goto error_context;
  515. }
  516. /* set the context in the HMC */
  517. ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
  518. if (ret) {
  519. dev_err(&pf->pdev->dev,
  520. "Failed to set VF LAN Tx queue context %d error: %d\n",
  521. pf_queue_id, ret);
  522. ret = -ENOENT;
  523. goto error_context;
  524. }
  525. /* associate this queue with the PCI VF function */
  526. qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
  527. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
  528. & I40E_QTX_CTL_PF_INDX_MASK);
  529. qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
  530. << I40E_QTX_CTL_VFVM_INDX_SHIFT)
  531. & I40E_QTX_CTL_VFVM_INDX_MASK);
  532. wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
  533. i40e_flush(hw);
  534. error_context:
  535. return ret;
  536. }
  537. /**
  538. * i40e_config_vsi_rx_queue
  539. * @vf: pointer to the VF info
  540. * @vsi_id: id of VSI as provided by the FW
  541. * @vsi_queue_id: vsi relative queue index
  542. * @info: config. info
  543. *
  544. * configure rx queue
  545. **/
  546. static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
  547. u16 vsi_queue_id,
  548. struct virtchnl_rxq_info *info)
  549. {
  550. struct i40e_pf *pf = vf->pf;
  551. struct i40e_hw *hw = &pf->hw;
  552. struct i40e_hmc_obj_rxq rx_ctx;
  553. u16 pf_queue_id;
  554. int ret = 0;
  555. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  556. /* clear the context structure first */
  557. memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
  558. /* only set the required fields */
  559. rx_ctx.base = info->dma_ring_addr / 128;
  560. rx_ctx.qlen = info->ring_len;
  561. if (info->splithdr_enabled) {
  562. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  563. I40E_RX_SPLIT_IP |
  564. I40E_RX_SPLIT_TCP_UDP |
  565. I40E_RX_SPLIT_SCTP;
  566. /* header length validation */
  567. if (info->hdr_size > ((2 * 1024) - 64)) {
  568. ret = -EINVAL;
  569. goto error_param;
  570. }
  571. rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
  572. /* set split mode 10b */
  573. rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
  574. }
  575. /* databuffer length validation */
  576. if (info->databuffer_size > ((16 * 1024) - 128)) {
  577. ret = -EINVAL;
  578. goto error_param;
  579. }
  580. rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
  581. /* max pkt. length validation */
  582. if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
  583. ret = -EINVAL;
  584. goto error_param;
  585. }
  586. rx_ctx.rxmax = info->max_pkt_size;
  587. /* enable 32bytes desc always */
  588. rx_ctx.dsize = 1;
  589. /* default values */
  590. rx_ctx.lrxqthresh = 1;
  591. rx_ctx.crcstrip = 1;
  592. rx_ctx.prefena = 1;
  593. rx_ctx.l2tsel = 1;
  594. /* clear the context in the HMC */
  595. ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
  596. if (ret) {
  597. dev_err(&pf->pdev->dev,
  598. "Failed to clear VF LAN Rx queue context %d, error: %d\n",
  599. pf_queue_id, ret);
  600. ret = -ENOENT;
  601. goto error_param;
  602. }
  603. /* set the context in the HMC */
  604. ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
  605. if (ret) {
  606. dev_err(&pf->pdev->dev,
  607. "Failed to set VF LAN Rx queue context %d error: %d\n",
  608. pf_queue_id, ret);
  609. ret = -ENOENT;
  610. goto error_param;
  611. }
  612. error_param:
  613. return ret;
  614. }
  615. /**
  616. * i40e_alloc_vsi_res
  617. * @vf: pointer to the VF info
  618. * @idx: VSI index, applies only for ADq mode, zero otherwise
  619. *
  620. * alloc VF vsi context & resources
  621. **/
  622. static int i40e_alloc_vsi_res(struct i40e_vf *vf, u8 idx)
  623. {
  624. struct i40e_mac_filter *f = NULL;
  625. struct i40e_pf *pf = vf->pf;
  626. struct i40e_vsi *vsi;
  627. u64 max_tx_rate = 0;
  628. int ret = 0;
  629. vsi = i40e_vsi_setup(pf, I40E_VSI_SRIOV, pf->vsi[pf->lan_vsi]->seid,
  630. vf->vf_id);
  631. if (!vsi) {
  632. dev_err(&pf->pdev->dev,
  633. "add vsi failed for VF %d, aq_err %d\n",
  634. vf->vf_id, pf->hw.aq.asq_last_status);
  635. ret = -ENOENT;
  636. goto error_alloc_vsi_res;
  637. }
  638. if (!idx) {
  639. u64 hena = i40e_pf_get_default_rss_hena(pf);
  640. u8 broadcast[ETH_ALEN];
  641. vf->lan_vsi_idx = vsi->idx;
  642. vf->lan_vsi_id = vsi->id;
  643. /* If the port VLAN has been configured and then the
  644. * VF driver was removed then the VSI port VLAN
  645. * configuration was destroyed. Check if there is
  646. * a port VLAN and restore the VSI configuration if
  647. * needed.
  648. */
  649. if (vf->port_vlan_id)
  650. i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
  651. spin_lock_bh(&vsi->mac_filter_hash_lock);
  652. if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
  653. f = i40e_add_mac_filter(vsi,
  654. vf->default_lan_addr.addr);
  655. if (!f)
  656. dev_info(&pf->pdev->dev,
  657. "Could not add MAC filter %pM for VF %d\n",
  658. vf->default_lan_addr.addr, vf->vf_id);
  659. }
  660. eth_broadcast_addr(broadcast);
  661. f = i40e_add_mac_filter(vsi, broadcast);
  662. if (!f)
  663. dev_info(&pf->pdev->dev,
  664. "Could not allocate VF broadcast filter\n");
  665. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  666. wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
  667. wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
  668. /* program mac filter only for VF VSI */
  669. ret = i40e_sync_vsi_filters(vsi);
  670. if (ret)
  671. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  672. }
  673. /* storing VSI index and id for ADq and don't apply the mac filter */
  674. if (vf->adq_enabled) {
  675. vf->ch[idx].vsi_idx = vsi->idx;
  676. vf->ch[idx].vsi_id = vsi->id;
  677. }
  678. /* Set VF bandwidth if specified */
  679. if (vf->tx_rate) {
  680. max_tx_rate = vf->tx_rate;
  681. } else if (vf->ch[idx].max_tx_rate) {
  682. max_tx_rate = vf->ch[idx].max_tx_rate;
  683. }
  684. if (max_tx_rate) {
  685. max_tx_rate = div_u64(max_tx_rate, I40E_BW_CREDIT_DIVISOR);
  686. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  687. max_tx_rate, 0, NULL);
  688. if (ret)
  689. dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
  690. vf->vf_id, ret);
  691. }
  692. error_alloc_vsi_res:
  693. return ret;
  694. }
  695. /**
  696. * i40e_map_pf_queues_to_vsi
  697. * @vf: pointer to the VF info
  698. *
  699. * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
  700. * function takes care of first part VSILAN_QTABLE, mapping pf queues to VSI.
  701. **/
  702. static void i40e_map_pf_queues_to_vsi(struct i40e_vf *vf)
  703. {
  704. struct i40e_pf *pf = vf->pf;
  705. struct i40e_hw *hw = &pf->hw;
  706. u32 reg, num_tc = 1; /* VF has at least one traffic class */
  707. u16 vsi_id, qps;
  708. int i, j;
  709. if (vf->adq_enabled)
  710. num_tc = vf->num_tc;
  711. for (i = 0; i < num_tc; i++) {
  712. if (vf->adq_enabled) {
  713. qps = vf->ch[i].num_qps;
  714. vsi_id = vf->ch[i].vsi_id;
  715. } else {
  716. qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  717. vsi_id = vf->lan_vsi_id;
  718. }
  719. for (j = 0; j < 7; j++) {
  720. if (j * 2 >= qps) {
  721. /* end of list */
  722. reg = 0x07FF07FF;
  723. } else {
  724. u16 qid = i40e_vc_get_pf_queue_id(vf,
  725. vsi_id,
  726. j * 2);
  727. reg = qid;
  728. qid = i40e_vc_get_pf_queue_id(vf, vsi_id,
  729. (j * 2) + 1);
  730. reg |= qid << 16;
  731. }
  732. i40e_write_rx_ctl(hw,
  733. I40E_VSILAN_QTABLE(j, vsi_id),
  734. reg);
  735. }
  736. }
  737. }
  738. /**
  739. * i40e_map_pf_to_vf_queues
  740. * @vf: pointer to the VF info
  741. *
  742. * PF maps LQPs to a VF by programming VSILAN_QTABLE & VPLAN_QTABLE. This
  743. * function takes care of the second part VPLAN_QTABLE & completes VF mappings.
  744. **/
  745. static void i40e_map_pf_to_vf_queues(struct i40e_vf *vf)
  746. {
  747. struct i40e_pf *pf = vf->pf;
  748. struct i40e_hw *hw = &pf->hw;
  749. u32 reg, total_qps = 0;
  750. u32 qps, num_tc = 1; /* VF has at least one traffic class */
  751. u16 vsi_id, qid;
  752. int i, j;
  753. if (vf->adq_enabled)
  754. num_tc = vf->num_tc;
  755. for (i = 0; i < num_tc; i++) {
  756. if (vf->adq_enabled) {
  757. qps = vf->ch[i].num_qps;
  758. vsi_id = vf->ch[i].vsi_id;
  759. } else {
  760. qps = pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  761. vsi_id = vf->lan_vsi_id;
  762. }
  763. for (j = 0; j < qps; j++) {
  764. qid = i40e_vc_get_pf_queue_id(vf, vsi_id, j);
  765. reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
  766. wr32(hw, I40E_VPLAN_QTABLE(total_qps, vf->vf_id),
  767. reg);
  768. total_qps++;
  769. }
  770. }
  771. }
  772. /**
  773. * i40e_enable_vf_mappings
  774. * @vf: pointer to the VF info
  775. *
  776. * enable VF mappings
  777. **/
  778. static void i40e_enable_vf_mappings(struct i40e_vf *vf)
  779. {
  780. struct i40e_pf *pf = vf->pf;
  781. struct i40e_hw *hw = &pf->hw;
  782. u32 reg;
  783. /* Tell the hardware we're using noncontiguous mapping. HW requires
  784. * that VF queues be mapped using this method, even when they are
  785. * contiguous in real life
  786. */
  787. i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
  788. I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
  789. /* enable VF vplan_qtable mappings */
  790. reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
  791. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
  792. i40e_map_pf_to_vf_queues(vf);
  793. i40e_map_pf_queues_to_vsi(vf);
  794. i40e_flush(hw);
  795. }
  796. /**
  797. * i40e_disable_vf_mappings
  798. * @vf: pointer to the VF info
  799. *
  800. * disable VF mappings
  801. **/
  802. static void i40e_disable_vf_mappings(struct i40e_vf *vf)
  803. {
  804. struct i40e_pf *pf = vf->pf;
  805. struct i40e_hw *hw = &pf->hw;
  806. int i;
  807. /* disable qp mappings */
  808. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
  809. for (i = 0; i < I40E_MAX_VSI_QP; i++)
  810. wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
  811. I40E_QUEUE_END_OF_LIST);
  812. i40e_flush(hw);
  813. }
  814. /**
  815. * i40e_free_vf_res
  816. * @vf: pointer to the VF info
  817. *
  818. * free VF resources
  819. **/
  820. static void i40e_free_vf_res(struct i40e_vf *vf)
  821. {
  822. struct i40e_pf *pf = vf->pf;
  823. struct i40e_hw *hw = &pf->hw;
  824. u32 reg_idx, reg;
  825. int i, j, msix_vf;
  826. /* Start by disabling VF's configuration API to prevent the OS from
  827. * accessing the VF's VSI after it's freed / invalidated.
  828. */
  829. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  830. /* It's possible the VF had requeuested more queues than the default so
  831. * do the accounting here when we're about to free them.
  832. */
  833. if (vf->num_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF) {
  834. pf->queues_left += vf->num_queue_pairs -
  835. I40E_DEFAULT_QUEUES_PER_VF;
  836. }
  837. /* free vsi & disconnect it from the parent uplink */
  838. if (vf->lan_vsi_idx) {
  839. i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  840. vf->lan_vsi_idx = 0;
  841. vf->lan_vsi_id = 0;
  842. vf->num_mac = 0;
  843. }
  844. /* do the accounting and remove additional ADq VSI's */
  845. if (vf->adq_enabled && vf->ch[0].vsi_idx) {
  846. for (j = 0; j < vf->num_tc; j++) {
  847. /* At this point VSI0 is already released so don't
  848. * release it again and only clear their values in
  849. * structure variables
  850. */
  851. if (j)
  852. i40e_vsi_release(pf->vsi[vf->ch[j].vsi_idx]);
  853. vf->ch[j].vsi_idx = 0;
  854. vf->ch[j].vsi_id = 0;
  855. }
  856. }
  857. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  858. /* disable interrupts so the VF starts in a known state */
  859. for (i = 0; i < msix_vf; i++) {
  860. /* format is same for both registers */
  861. if (0 == i)
  862. reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
  863. else
  864. reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
  865. (vf->vf_id))
  866. + (i - 1));
  867. wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
  868. i40e_flush(hw);
  869. }
  870. /* clear the irq settings */
  871. for (i = 0; i < msix_vf; i++) {
  872. /* format is same for both registers */
  873. if (0 == i)
  874. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  875. else
  876. reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
  877. (vf->vf_id))
  878. + (i - 1));
  879. reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
  880. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
  881. wr32(hw, reg_idx, reg);
  882. i40e_flush(hw);
  883. }
  884. /* reset some of the state variables keeping track of the resources */
  885. vf->num_queue_pairs = 0;
  886. clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  887. clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  888. }
  889. /**
  890. * i40e_alloc_vf_res
  891. * @vf: pointer to the VF info
  892. *
  893. * allocate VF resources
  894. **/
  895. static int i40e_alloc_vf_res(struct i40e_vf *vf)
  896. {
  897. struct i40e_pf *pf = vf->pf;
  898. int total_queue_pairs = 0;
  899. int ret, idx;
  900. if (vf->num_req_queues &&
  901. vf->num_req_queues <= pf->queues_left + I40E_DEFAULT_QUEUES_PER_VF)
  902. pf->num_vf_qps = vf->num_req_queues;
  903. else
  904. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  905. /* allocate hw vsi context & associated resources */
  906. ret = i40e_alloc_vsi_res(vf, 0);
  907. if (ret)
  908. goto error_alloc;
  909. total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  910. /* allocate additional VSIs based on tc information for ADq */
  911. if (vf->adq_enabled) {
  912. if (pf->queues_left >=
  913. (I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF)) {
  914. /* TC 0 always belongs to VF VSI */
  915. for (idx = 1; idx < vf->num_tc; idx++) {
  916. ret = i40e_alloc_vsi_res(vf, idx);
  917. if (ret)
  918. goto error_alloc;
  919. }
  920. /* send correct number of queues */
  921. total_queue_pairs = I40E_MAX_VF_QUEUES;
  922. } else {
  923. dev_info(&pf->pdev->dev, "VF %d: Not enough queues to allocate, disabling ADq\n",
  924. vf->vf_id);
  925. vf->adq_enabled = false;
  926. }
  927. }
  928. /* We account for each VF to get a default number of queue pairs. If
  929. * the VF has now requested more, we need to account for that to make
  930. * certain we never request more queues than we actually have left in
  931. * HW.
  932. */
  933. if (total_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF)
  934. pf->queues_left -=
  935. total_queue_pairs - I40E_DEFAULT_QUEUES_PER_VF;
  936. if (vf->trusted)
  937. set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  938. else
  939. clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  940. /* store the total qps number for the runtime
  941. * VF req validation
  942. */
  943. vf->num_queue_pairs = total_queue_pairs;
  944. /* VF is now completely initialized */
  945. set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  946. error_alloc:
  947. if (ret)
  948. i40e_free_vf_res(vf);
  949. return ret;
  950. }
  951. #define VF_DEVICE_STATUS 0xAA
  952. #define VF_TRANS_PENDING_MASK 0x20
  953. /**
  954. * i40e_quiesce_vf_pci
  955. * @vf: pointer to the VF structure
  956. *
  957. * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
  958. * if the transactions never clear.
  959. **/
  960. static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
  961. {
  962. struct i40e_pf *pf = vf->pf;
  963. struct i40e_hw *hw = &pf->hw;
  964. int vf_abs_id, i;
  965. u32 reg;
  966. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  967. wr32(hw, I40E_PF_PCI_CIAA,
  968. VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
  969. for (i = 0; i < 100; i++) {
  970. reg = rd32(hw, I40E_PF_PCI_CIAD);
  971. if ((reg & VF_TRANS_PENDING_MASK) == 0)
  972. return 0;
  973. udelay(1);
  974. }
  975. return -EIO;
  976. }
  977. /**
  978. * i40e_trigger_vf_reset
  979. * @vf: pointer to the VF structure
  980. * @flr: VFLR was issued or not
  981. *
  982. * Trigger hardware to start a reset for a particular VF. Expects the caller
  983. * to wait the proper amount of time to allow hardware to reset the VF before
  984. * it cleans up and restores VF functionality.
  985. **/
  986. static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
  987. {
  988. struct i40e_pf *pf = vf->pf;
  989. struct i40e_hw *hw = &pf->hw;
  990. u32 reg, reg_idx, bit_idx;
  991. /* warn the VF */
  992. clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  993. /* Disable VF's configuration API during reset. The flag is re-enabled
  994. * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
  995. * It's normally disabled in i40e_free_vf_res(), but it's safer
  996. * to do it earlier to give some time to finish to any VF config
  997. * functions that may still be running at this point.
  998. */
  999. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  1000. /* In the case of a VFLR, the HW has already reset the VF and we
  1001. * just need to clean up, so don't hit the VFRTRIG register.
  1002. */
  1003. if (!flr) {
  1004. /* reset VF using VPGEN_VFRTRIG reg */
  1005. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  1006. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  1007. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  1008. i40e_flush(hw);
  1009. }
  1010. /* clear the VFLR bit in GLGEN_VFLRSTAT */
  1011. reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
  1012. bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
  1013. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1014. i40e_flush(hw);
  1015. if (i40e_quiesce_vf_pci(vf))
  1016. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  1017. vf->vf_id);
  1018. }
  1019. /**
  1020. * i40e_cleanup_reset_vf
  1021. * @vf: pointer to the VF structure
  1022. *
  1023. * Cleanup a VF after the hardware reset is finished. Expects the caller to
  1024. * have verified whether the reset is finished properly, and ensure the
  1025. * minimum amount of wait time has passed.
  1026. **/
  1027. static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
  1028. {
  1029. struct i40e_pf *pf = vf->pf;
  1030. struct i40e_hw *hw = &pf->hw;
  1031. u32 reg;
  1032. /* free VF resources to begin resetting the VSI state */
  1033. i40e_free_vf_res(vf);
  1034. /* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
  1035. * By doing this we allow HW to access VF memory at any point. If we
  1036. * did it any sooner, HW could access memory while it was being freed
  1037. * in i40e_free_vf_res(), causing an IOMMU fault.
  1038. *
  1039. * On the other hand, this needs to be done ASAP, because the VF driver
  1040. * is waiting for this to happen and may report a timeout. It's
  1041. * harmless, but it gets logged into Guest OS kernel log, so best avoid
  1042. * it.
  1043. */
  1044. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  1045. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  1046. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  1047. /* reallocate VF resources to finish resetting the VSI state */
  1048. if (!i40e_alloc_vf_res(vf)) {
  1049. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1050. i40e_enable_vf_mappings(vf);
  1051. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1052. clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  1053. /* Do not notify the client during VF init */
  1054. if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
  1055. &vf->vf_states))
  1056. i40e_notify_client_of_vf_reset(pf, abs_vf_id);
  1057. vf->num_vlan = 0;
  1058. }
  1059. /* Tell the VF driver the reset is done. This needs to be done only
  1060. * after VF has been fully initialized, because the VF driver may
  1061. * request resources immediately after setting this flag.
  1062. */
  1063. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
  1064. }
  1065. /**
  1066. * i40e_reset_vf
  1067. * @vf: pointer to the VF structure
  1068. * @flr: VFLR was issued or not
  1069. *
  1070. * Returns true if the VF is in reset, resets successfully, or resets
  1071. * are disabled and false otherwise.
  1072. **/
  1073. bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
  1074. {
  1075. struct i40e_pf *pf = vf->pf;
  1076. struct i40e_hw *hw = &pf->hw;
  1077. bool rsd = false;
  1078. u32 reg;
  1079. int i;
  1080. if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state))
  1081. return true;
  1082. /* If the VFs have been disabled, this means something else is
  1083. * resetting the VF, so we shouldn't continue.
  1084. */
  1085. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1086. return true;
  1087. i40e_trigger_vf_reset(vf, flr);
  1088. /* poll VPGEN_VFRSTAT reg to make sure
  1089. * that reset is complete
  1090. */
  1091. for (i = 0; i < 10; i++) {
  1092. /* VF reset requires driver to first reset the VF and then
  1093. * poll the status register to make sure that the reset
  1094. * completed successfully. Due to internal HW FIFO flushes,
  1095. * we must wait 10ms before the register will be valid.
  1096. */
  1097. usleep_range(10000, 20000);
  1098. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  1099. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  1100. rsd = true;
  1101. break;
  1102. }
  1103. }
  1104. if (flr)
  1105. usleep_range(10000, 20000);
  1106. if (!rsd)
  1107. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  1108. vf->vf_id);
  1109. usleep_range(10000, 20000);
  1110. /* On initial reset, we don't have any queues to disable */
  1111. if (vf->lan_vsi_idx != 0)
  1112. i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
  1113. i40e_cleanup_reset_vf(vf);
  1114. i40e_flush(hw);
  1115. clear_bit(__I40E_VF_DISABLE, pf->state);
  1116. return true;
  1117. }
  1118. /**
  1119. * i40e_reset_all_vfs
  1120. * @pf: pointer to the PF structure
  1121. * @flr: VFLR was issued or not
  1122. *
  1123. * Reset all allocated VFs in one go. First, tell the hardware to reset each
  1124. * VF, then do all the waiting in one chunk, and finally finish restoring each
  1125. * VF after the wait. This is useful during PF routines which need to reset
  1126. * all VFs, as otherwise it must perform these resets in a serialized fashion.
  1127. *
  1128. * Returns true if any VFs were reset, and false otherwise.
  1129. **/
  1130. bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
  1131. {
  1132. struct i40e_hw *hw = &pf->hw;
  1133. struct i40e_vf *vf;
  1134. int i, v;
  1135. u32 reg;
  1136. /* If we don't have any VFs, then there is nothing to reset */
  1137. if (!pf->num_alloc_vfs)
  1138. return false;
  1139. /* If VFs have been disabled, there is no need to reset */
  1140. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1141. return false;
  1142. /* Begin reset on all VFs at once */
  1143. for (v = 0; v < pf->num_alloc_vfs; v++)
  1144. i40e_trigger_vf_reset(&pf->vf[v], flr);
  1145. /* HW requires some time to make sure it can flush the FIFO for a VF
  1146. * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
  1147. * sequence to make sure that it has completed. We'll keep track of
  1148. * the VFs using a simple iterator that increments once that VF has
  1149. * finished resetting.
  1150. */
  1151. for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
  1152. usleep_range(10000, 20000);
  1153. /* Check each VF in sequence, beginning with the VF to fail
  1154. * the previous check.
  1155. */
  1156. while (v < pf->num_alloc_vfs) {
  1157. vf = &pf->vf[v];
  1158. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  1159. if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
  1160. break;
  1161. /* If the current VF has finished resetting, move on
  1162. * to the next VF in sequence.
  1163. */
  1164. v++;
  1165. }
  1166. }
  1167. if (flr)
  1168. usleep_range(10000, 20000);
  1169. /* Display a warning if at least one VF didn't manage to reset in
  1170. * time, but continue on with the operation.
  1171. */
  1172. if (v < pf->num_alloc_vfs)
  1173. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  1174. pf->vf[v].vf_id);
  1175. usleep_range(10000, 20000);
  1176. /* Begin disabling all the rings associated with VFs, but do not wait
  1177. * between each VF.
  1178. */
  1179. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1180. /* On initial reset, we don't have any queues to disable */
  1181. if (pf->vf[v].lan_vsi_idx == 0)
  1182. continue;
  1183. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1184. }
  1185. /* Now that we've notified HW to disable all of the VF rings, wait
  1186. * until they finish.
  1187. */
  1188. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1189. /* On initial reset, we don't have any queues to disable */
  1190. if (pf->vf[v].lan_vsi_idx == 0)
  1191. continue;
  1192. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1193. }
  1194. /* Hw may need up to 50ms to finish disabling the RX queues. We
  1195. * minimize the wait by delaying only once for all VFs.
  1196. */
  1197. mdelay(50);
  1198. /* Finish the reset on each VF */
  1199. for (v = 0; v < pf->num_alloc_vfs; v++)
  1200. i40e_cleanup_reset_vf(&pf->vf[v]);
  1201. i40e_flush(hw);
  1202. clear_bit(__I40E_VF_DISABLE, pf->state);
  1203. return true;
  1204. }
  1205. /**
  1206. * i40e_free_vfs
  1207. * @pf: pointer to the PF structure
  1208. *
  1209. * free VF resources
  1210. **/
  1211. void i40e_free_vfs(struct i40e_pf *pf)
  1212. {
  1213. struct i40e_hw *hw = &pf->hw;
  1214. u32 reg_idx, bit_idx;
  1215. int i, tmp, vf_id;
  1216. if (!pf->vf)
  1217. return;
  1218. set_bit(__I40E_VFS_RELEASING, pf->state);
  1219. while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1220. usleep_range(1000, 2000);
  1221. i40e_notify_client_of_vf_enable(pf, 0);
  1222. /* Disable IOV before freeing resources. This lets any VF drivers
  1223. * running in the host get themselves cleaned up before we yank
  1224. * the carpet out from underneath their feet.
  1225. */
  1226. if (!pci_vfs_assigned(pf->pdev))
  1227. pci_disable_sriov(pf->pdev);
  1228. else
  1229. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  1230. /* Amortize wait time by stopping all VFs at the same time */
  1231. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1232. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1233. continue;
  1234. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1235. }
  1236. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1237. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1238. continue;
  1239. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1240. }
  1241. /* free up VF resources */
  1242. tmp = pf->num_alloc_vfs;
  1243. pf->num_alloc_vfs = 0;
  1244. for (i = 0; i < tmp; i++) {
  1245. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1246. i40e_free_vf_res(&pf->vf[i]);
  1247. /* disable qp mappings */
  1248. i40e_disable_vf_mappings(&pf->vf[i]);
  1249. }
  1250. kfree(pf->vf);
  1251. pf->vf = NULL;
  1252. /* This check is for when the driver is unloaded while VFs are
  1253. * assigned. Setting the number of VFs to 0 through sysfs is caught
  1254. * before this function ever gets called.
  1255. */
  1256. if (!pci_vfs_assigned(pf->pdev)) {
  1257. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  1258. * work correctly when SR-IOV gets re-enabled.
  1259. */
  1260. for (vf_id = 0; vf_id < tmp; vf_id++) {
  1261. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  1262. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  1263. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1264. }
  1265. }
  1266. clear_bit(__I40E_VF_DISABLE, pf->state);
  1267. clear_bit(__I40E_VFS_RELEASING, pf->state);
  1268. }
  1269. #ifdef CONFIG_PCI_IOV
  1270. /**
  1271. * i40e_alloc_vfs
  1272. * @pf: pointer to the PF structure
  1273. * @num_alloc_vfs: number of VFs to allocate
  1274. *
  1275. * allocate VF resources
  1276. **/
  1277. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  1278. {
  1279. struct i40e_vf *vfs;
  1280. int i, ret = 0;
  1281. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  1282. i40e_irq_dynamic_disable_icr0(pf);
  1283. /* Check to see if we're just allocating resources for extant VFs */
  1284. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  1285. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  1286. if (ret) {
  1287. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1288. pf->num_alloc_vfs = 0;
  1289. goto err_iov;
  1290. }
  1291. }
  1292. /* allocate memory */
  1293. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  1294. if (!vfs) {
  1295. ret = -ENOMEM;
  1296. goto err_alloc;
  1297. }
  1298. pf->vf = vfs;
  1299. /* apply default profile */
  1300. for (i = 0; i < num_alloc_vfs; i++) {
  1301. vfs[i].pf = pf;
  1302. vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
  1303. vfs[i].vf_id = i;
  1304. /* assign default capabilities */
  1305. set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  1306. vfs[i].spoofchk = true;
  1307. set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
  1308. }
  1309. pf->num_alloc_vfs = num_alloc_vfs;
  1310. /* VF resources get allocated during reset */
  1311. i40e_reset_all_vfs(pf, false);
  1312. i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
  1313. err_alloc:
  1314. if (ret)
  1315. i40e_free_vfs(pf);
  1316. err_iov:
  1317. /* Re-enable interrupt 0. */
  1318. i40e_irq_dynamic_enable_icr0(pf);
  1319. return ret;
  1320. }
  1321. #endif
  1322. /**
  1323. * i40e_pci_sriov_enable
  1324. * @pdev: pointer to a pci_dev structure
  1325. * @num_vfs: number of VFs to allocate
  1326. *
  1327. * Enable or change the number of VFs
  1328. **/
  1329. static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
  1330. {
  1331. #ifdef CONFIG_PCI_IOV
  1332. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1333. int pre_existing_vfs = pci_num_vf(pdev);
  1334. int err = 0;
  1335. if (test_bit(__I40E_TESTING, pf->state)) {
  1336. dev_warn(&pdev->dev,
  1337. "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
  1338. err = -EPERM;
  1339. goto err_out;
  1340. }
  1341. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  1342. i40e_free_vfs(pf);
  1343. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  1344. goto out;
  1345. if (num_vfs > pf->num_req_vfs) {
  1346. dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
  1347. num_vfs, pf->num_req_vfs);
  1348. err = -EPERM;
  1349. goto err_out;
  1350. }
  1351. dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
  1352. err = i40e_alloc_vfs(pf, num_vfs);
  1353. if (err) {
  1354. dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
  1355. goto err_out;
  1356. }
  1357. out:
  1358. return num_vfs;
  1359. err_out:
  1360. return err;
  1361. #endif
  1362. return 0;
  1363. }
  1364. /**
  1365. * i40e_pci_sriov_configure
  1366. * @pdev: pointer to a pci_dev structure
  1367. * @num_vfs: number of VFs to allocate
  1368. *
  1369. * Enable or change the number of VFs. Called when the user updates the number
  1370. * of VFs in sysfs.
  1371. **/
  1372. int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  1373. {
  1374. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1375. if (num_vfs) {
  1376. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  1377. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  1378. i40e_do_reset_safe(pf, I40E_PF_RESET_AND_REBUILD_FLAG);
  1379. }
  1380. return i40e_pci_sriov_enable(pdev, num_vfs);
  1381. }
  1382. if (!pci_vfs_assigned(pf->pdev)) {
  1383. i40e_free_vfs(pf);
  1384. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1385. i40e_do_reset_safe(pf, I40E_PF_RESET_AND_REBUILD_FLAG);
  1386. } else {
  1387. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
  1388. return -EINVAL;
  1389. }
  1390. return 0;
  1391. }
  1392. /***********************virtual channel routines******************/
  1393. /**
  1394. * i40e_vc_send_msg_to_vf
  1395. * @vf: pointer to the VF info
  1396. * @v_opcode: virtual channel opcode
  1397. * @v_retval: virtual channel return value
  1398. * @msg: pointer to the msg buffer
  1399. * @msglen: msg length
  1400. *
  1401. * send msg to VF
  1402. **/
  1403. static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
  1404. u32 v_retval, u8 *msg, u16 msglen)
  1405. {
  1406. struct i40e_pf *pf;
  1407. struct i40e_hw *hw;
  1408. int abs_vf_id;
  1409. i40e_status aq_ret;
  1410. /* validate the request */
  1411. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  1412. return -EINVAL;
  1413. pf = vf->pf;
  1414. hw = &pf->hw;
  1415. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1416. /* single place to detect unsuccessful return values */
  1417. if (v_retval) {
  1418. vf->num_invalid_msgs++;
  1419. dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
  1420. vf->vf_id, v_opcode, v_retval);
  1421. if (vf->num_invalid_msgs >
  1422. I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
  1423. dev_err(&pf->pdev->dev,
  1424. "Number of invalid messages exceeded for VF %d\n",
  1425. vf->vf_id);
  1426. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  1427. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  1428. }
  1429. } else {
  1430. vf->num_valid_msgs++;
  1431. /* reset the invalid counter, if a valid message is received. */
  1432. vf->num_invalid_msgs = 0;
  1433. }
  1434. aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  1435. msg, msglen, NULL);
  1436. if (aq_ret) {
  1437. dev_info(&pf->pdev->dev,
  1438. "Unable to send the message to VF %d aq_err %d\n",
  1439. vf->vf_id, pf->hw.aq.asq_last_status);
  1440. return -EIO;
  1441. }
  1442. return 0;
  1443. }
  1444. /**
  1445. * i40e_vc_send_resp_to_vf
  1446. * @vf: pointer to the VF info
  1447. * @opcode: operation code
  1448. * @retval: return value
  1449. *
  1450. * send resp msg to VF
  1451. **/
  1452. static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
  1453. enum virtchnl_ops opcode,
  1454. i40e_status retval)
  1455. {
  1456. return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
  1457. }
  1458. /**
  1459. * i40e_vc_get_version_msg
  1460. * @vf: pointer to the VF info
  1461. * @msg: pointer to the msg buffer
  1462. *
  1463. * called from the VF to request the API version used by the PF
  1464. **/
  1465. static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
  1466. {
  1467. struct virtchnl_version_info info = {
  1468. VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
  1469. };
  1470. vf->vf_ver = *(struct virtchnl_version_info *)msg;
  1471. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1472. if (VF_IS_V10(&vf->vf_ver))
  1473. info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1474. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
  1475. I40E_SUCCESS, (u8 *)&info,
  1476. sizeof(struct virtchnl_version_info));
  1477. }
  1478. /**
  1479. * i40e_del_qch - delete all the additional VSIs created as a part of ADq
  1480. * @vf: pointer to VF structure
  1481. **/
  1482. static void i40e_del_qch(struct i40e_vf *vf)
  1483. {
  1484. struct i40e_pf *pf = vf->pf;
  1485. int i;
  1486. /* first element in the array belongs to primary VF VSI and we shouldn't
  1487. * delete it. We should however delete the rest of the VSIs created
  1488. */
  1489. for (i = 1; i < vf->num_tc; i++) {
  1490. if (vf->ch[i].vsi_idx) {
  1491. i40e_vsi_release(pf->vsi[vf->ch[i].vsi_idx]);
  1492. vf->ch[i].vsi_idx = 0;
  1493. vf->ch[i].vsi_id = 0;
  1494. }
  1495. }
  1496. }
  1497. /**
  1498. * i40e_vc_get_vf_resources_msg
  1499. * @vf: pointer to the VF info
  1500. * @msg: pointer to the msg buffer
  1501. *
  1502. * called from the VF to request its resources
  1503. **/
  1504. static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
  1505. {
  1506. struct virtchnl_vf_resource *vfres = NULL;
  1507. struct i40e_pf *pf = vf->pf;
  1508. i40e_status aq_ret = 0;
  1509. struct i40e_vsi *vsi;
  1510. int num_vsis = 1;
  1511. int len = 0;
  1512. int ret;
  1513. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  1514. aq_ret = I40E_ERR_PARAM;
  1515. goto err;
  1516. }
  1517. len = (sizeof(struct virtchnl_vf_resource) +
  1518. sizeof(struct virtchnl_vsi_resource) * num_vsis);
  1519. vfres = kzalloc(len, GFP_KERNEL);
  1520. if (!vfres) {
  1521. aq_ret = I40E_ERR_NO_MEMORY;
  1522. len = 0;
  1523. goto err;
  1524. }
  1525. if (VF_IS_V11(&vf->vf_ver))
  1526. vf->driver_caps = *(u32 *)msg;
  1527. else
  1528. vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
  1529. VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1530. VIRTCHNL_VF_OFFLOAD_VLAN;
  1531. vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
  1532. vsi = pf->vsi[vf->lan_vsi_idx];
  1533. if (!vsi->info.pvid)
  1534. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
  1535. if (i40e_vf_client_capable(pf, vf->vf_id) &&
  1536. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
  1537. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
  1538. set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1539. } else {
  1540. clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1541. }
  1542. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  1543. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
  1544. } else {
  1545. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  1546. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
  1547. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1548. else
  1549. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1550. }
  1551. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
  1552. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1553. vfres->vf_cap_flags |=
  1554. VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
  1555. }
  1556. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
  1557. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
  1558. if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
  1559. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
  1560. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
  1561. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
  1562. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  1563. dev_err(&pf->pdev->dev,
  1564. "VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
  1565. vf->vf_id);
  1566. aq_ret = I40E_ERR_PARAM;
  1567. goto err;
  1568. }
  1569. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1570. }
  1571. if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
  1572. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  1573. vfres->vf_cap_flags |=
  1574. VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  1575. }
  1576. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
  1577. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
  1578. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)
  1579. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ADQ;
  1580. vfres->num_vsis = num_vsis;
  1581. vfres->num_queue_pairs = vf->num_queue_pairs;
  1582. vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
  1583. vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
  1584. vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
  1585. if (vf->lan_vsi_idx) {
  1586. vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
  1587. vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
  1588. vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
  1589. /* VFs only use TC 0 */
  1590. vfres->vsi_res[0].qset_handle
  1591. = le16_to_cpu(vsi->info.qs_handle[0]);
  1592. ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
  1593. vf->default_lan_addr.addr);
  1594. }
  1595. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1596. err:
  1597. /* send the response back to the VF */
  1598. ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
  1599. aq_ret, (u8 *)vfres, len);
  1600. kfree(vfres);
  1601. return ret;
  1602. }
  1603. /**
  1604. * i40e_vc_reset_vf_msg
  1605. * @vf: pointer to the VF info
  1606. *
  1607. * called from the VF to reset itself,
  1608. * unlike other virtchnl messages, PF driver
  1609. * doesn't send the response back to the VF
  1610. **/
  1611. static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
  1612. {
  1613. if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  1614. i40e_reset_vf(vf, false);
  1615. }
  1616. /**
  1617. * i40e_getnum_vf_vsi_vlan_filters
  1618. * @vsi: pointer to the vsi
  1619. *
  1620. * called to get the number of VLANs offloaded on this VF
  1621. **/
  1622. static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
  1623. {
  1624. struct i40e_mac_filter *f;
  1625. int num_vlans = 0, bkt;
  1626. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1627. if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
  1628. num_vlans++;
  1629. }
  1630. return num_vlans;
  1631. }
  1632. /**
  1633. * i40e_vc_config_promiscuous_mode_msg
  1634. * @vf: pointer to the VF info
  1635. * @msg: pointer to the msg buffer
  1636. * @msglen: msg length
  1637. *
  1638. * called from the VF to configure the promiscuous mode of
  1639. * VF vsis
  1640. **/
  1641. static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
  1642. u8 *msg, u16 msglen)
  1643. {
  1644. struct virtchnl_promisc_info *info =
  1645. (struct virtchnl_promisc_info *)msg;
  1646. struct i40e_pf *pf = vf->pf;
  1647. struct i40e_hw *hw = &pf->hw;
  1648. struct i40e_mac_filter *f;
  1649. i40e_status aq_ret = 0;
  1650. bool allmulti = false;
  1651. struct i40e_vsi *vsi;
  1652. bool alluni = false;
  1653. int aq_err = 0;
  1654. int bkt;
  1655. vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
  1656. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1657. !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
  1658. !vsi) {
  1659. aq_ret = I40E_ERR_PARAM;
  1660. goto error_param;
  1661. }
  1662. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  1663. dev_err(&pf->pdev->dev,
  1664. "Unprivileged VF %d is attempting to configure promiscuous mode\n",
  1665. vf->vf_id);
  1666. /* Lie to the VF on purpose. */
  1667. aq_ret = 0;
  1668. goto error_param;
  1669. }
  1670. /* Multicast promiscuous handling*/
  1671. if (info->flags & FLAG_VF_MULTICAST_PROMISC)
  1672. allmulti = true;
  1673. if (vf->port_vlan_id) {
  1674. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
  1675. allmulti,
  1676. vf->port_vlan_id,
  1677. NULL);
  1678. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1679. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1680. if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
  1681. continue;
  1682. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
  1683. vsi->seid,
  1684. allmulti,
  1685. f->vlan,
  1686. NULL);
  1687. aq_err = pf->hw.aq.asq_last_status;
  1688. if (aq_ret) {
  1689. dev_err(&pf->pdev->dev,
  1690. "Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
  1691. f->vlan,
  1692. i40e_stat_str(&pf->hw, aq_ret),
  1693. i40e_aq_str(&pf->hw, aq_err));
  1694. break;
  1695. }
  1696. }
  1697. } else {
  1698. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
  1699. allmulti, NULL);
  1700. aq_err = pf->hw.aq.asq_last_status;
  1701. if (aq_ret) {
  1702. dev_err(&pf->pdev->dev,
  1703. "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
  1704. vf->vf_id,
  1705. i40e_stat_str(&pf->hw, aq_ret),
  1706. i40e_aq_str(&pf->hw, aq_err));
  1707. goto error_param;
  1708. }
  1709. }
  1710. if (!aq_ret) {
  1711. dev_info(&pf->pdev->dev,
  1712. "VF %d successfully set multicast promiscuous mode\n",
  1713. vf->vf_id);
  1714. if (allmulti)
  1715. set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1716. else
  1717. clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1718. }
  1719. if (info->flags & FLAG_VF_UNICAST_PROMISC)
  1720. alluni = true;
  1721. if (vf->port_vlan_id) {
  1722. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
  1723. alluni,
  1724. vf->port_vlan_id,
  1725. NULL);
  1726. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1727. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1728. if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
  1729. continue;
  1730. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
  1731. vsi->seid,
  1732. alluni,
  1733. f->vlan,
  1734. NULL);
  1735. aq_err = pf->hw.aq.asq_last_status;
  1736. if (aq_ret)
  1737. dev_err(&pf->pdev->dev,
  1738. "Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
  1739. f->vlan,
  1740. i40e_stat_str(&pf->hw, aq_ret),
  1741. i40e_aq_str(&pf->hw, aq_err));
  1742. }
  1743. } else {
  1744. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
  1745. alluni, NULL,
  1746. true);
  1747. aq_err = pf->hw.aq.asq_last_status;
  1748. if (aq_ret) {
  1749. dev_err(&pf->pdev->dev,
  1750. "VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
  1751. vf->vf_id, info->flags,
  1752. i40e_stat_str(&pf->hw, aq_ret),
  1753. i40e_aq_str(&pf->hw, aq_err));
  1754. goto error_param;
  1755. }
  1756. }
  1757. if (!aq_ret) {
  1758. dev_info(&pf->pdev->dev,
  1759. "VF %d successfully set unicast promiscuous mode\n",
  1760. vf->vf_id);
  1761. if (alluni)
  1762. set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1763. else
  1764. clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1765. }
  1766. error_param:
  1767. /* send the response to the VF */
  1768. return i40e_vc_send_resp_to_vf(vf,
  1769. VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  1770. aq_ret);
  1771. }
  1772. /**
  1773. * i40e_vc_config_queues_msg
  1774. * @vf: pointer to the VF info
  1775. * @msg: pointer to the msg buffer
  1776. * @msglen: msg length
  1777. *
  1778. * called from the VF to configure the rx/tx
  1779. * queues
  1780. **/
  1781. static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1782. {
  1783. struct virtchnl_vsi_queue_config_info *qci =
  1784. (struct virtchnl_vsi_queue_config_info *)msg;
  1785. struct virtchnl_queue_pair_info *qpi;
  1786. struct i40e_pf *pf = vf->pf;
  1787. u16 vsi_id, vsi_queue_id = 0;
  1788. i40e_status aq_ret = 0;
  1789. int i, j = 0, idx = 0;
  1790. vsi_id = qci->vsi_id;
  1791. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1792. aq_ret = I40E_ERR_PARAM;
  1793. goto error_param;
  1794. }
  1795. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1796. aq_ret = I40E_ERR_PARAM;
  1797. goto error_param;
  1798. }
  1799. for (i = 0; i < qci->num_queue_pairs; i++) {
  1800. qpi = &qci->qpair[i];
  1801. if (!vf->adq_enabled) {
  1802. vsi_queue_id = qpi->txq.queue_id;
  1803. if (qpi->txq.vsi_id != qci->vsi_id ||
  1804. qpi->rxq.vsi_id != qci->vsi_id ||
  1805. qpi->rxq.queue_id != vsi_queue_id) {
  1806. aq_ret = I40E_ERR_PARAM;
  1807. goto error_param;
  1808. }
  1809. }
  1810. if (!i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
  1811. aq_ret = I40E_ERR_PARAM;
  1812. goto error_param;
  1813. }
  1814. if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
  1815. &qpi->rxq) ||
  1816. i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
  1817. &qpi->txq)) {
  1818. aq_ret = I40E_ERR_PARAM;
  1819. goto error_param;
  1820. }
  1821. /* For ADq there can be up to 4 VSIs with max 4 queues each.
  1822. * VF does not know about these additional VSIs and all
  1823. * it cares is about its own queues. PF configures these queues
  1824. * to its appropriate VSIs based on TC mapping
  1825. **/
  1826. if (vf->adq_enabled) {
  1827. if (j == (vf->ch[idx].num_qps - 1)) {
  1828. idx++;
  1829. j = 0; /* resetting the queue count */
  1830. vsi_queue_id = 0;
  1831. } else {
  1832. j++;
  1833. vsi_queue_id++;
  1834. }
  1835. vsi_id = vf->ch[idx].vsi_id;
  1836. }
  1837. }
  1838. /* set vsi num_queue_pairs in use to num configured by VF */
  1839. if (!vf->adq_enabled) {
  1840. pf->vsi[vf->lan_vsi_idx]->num_queue_pairs =
  1841. qci->num_queue_pairs;
  1842. } else {
  1843. for (i = 0; i < vf->num_tc; i++)
  1844. pf->vsi[vf->ch[i].vsi_idx]->num_queue_pairs =
  1845. vf->ch[i].num_qps;
  1846. }
  1847. error_param:
  1848. /* send the response to the VF */
  1849. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  1850. aq_ret);
  1851. }
  1852. /**
  1853. * i40e_validate_queue_map
  1854. * @vsi_id: vsi id
  1855. * @queuemap: Tx or Rx queue map
  1856. *
  1857. * check if Tx or Rx queue map is valid
  1858. **/
  1859. static int i40e_validate_queue_map(struct i40e_vf *vf, u16 vsi_id,
  1860. unsigned long queuemap)
  1861. {
  1862. u16 vsi_queue_id, queue_id;
  1863. for_each_set_bit(vsi_queue_id, &queuemap, I40E_MAX_VSI_QP) {
  1864. if (vf->adq_enabled) {
  1865. vsi_id = vf->ch[vsi_queue_id / I40E_MAX_VF_VSI].vsi_id;
  1866. queue_id = (vsi_queue_id % I40E_DEFAULT_QUEUES_PER_VF);
  1867. } else {
  1868. queue_id = vsi_queue_id;
  1869. }
  1870. if (!i40e_vc_isvalid_queue_id(vf, vsi_id, queue_id))
  1871. return -EINVAL;
  1872. }
  1873. return 0;
  1874. }
  1875. /**
  1876. * i40e_vc_config_irq_map_msg
  1877. * @vf: pointer to the VF info
  1878. * @msg: pointer to the msg buffer
  1879. * @msglen: msg length
  1880. *
  1881. * called from the VF to configure the irq to
  1882. * queue map
  1883. **/
  1884. static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1885. {
  1886. struct virtchnl_irq_map_info *irqmap_info =
  1887. (struct virtchnl_irq_map_info *)msg;
  1888. struct virtchnl_vector_map *map;
  1889. u16 vsi_id, vector_id;
  1890. i40e_status aq_ret = 0;
  1891. int i;
  1892. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1893. aq_ret = I40E_ERR_PARAM;
  1894. goto error_param;
  1895. }
  1896. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1897. map = &irqmap_info->vecmap[i];
  1898. vector_id = map->vector_id;
  1899. vsi_id = map->vsi_id;
  1900. /* validate msg params */
  1901. if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
  1902. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1903. aq_ret = I40E_ERR_PARAM;
  1904. goto error_param;
  1905. }
  1906. if (i40e_validate_queue_map(vf, vsi_id, map->rxq_map)) {
  1907. aq_ret = I40E_ERR_PARAM;
  1908. goto error_param;
  1909. }
  1910. if (i40e_validate_queue_map(vf, vsi_id, map->txq_map)) {
  1911. aq_ret = I40E_ERR_PARAM;
  1912. goto error_param;
  1913. }
  1914. i40e_config_irq_link_list(vf, vsi_id, map);
  1915. }
  1916. error_param:
  1917. /* send the response to the VF */
  1918. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
  1919. aq_ret);
  1920. }
  1921. /**
  1922. * i40e_ctrl_vf_tx_rings
  1923. * @vsi: the SRIOV VSI being configured
  1924. * @q_map: bit map of the queues to be enabled
  1925. * @enable: start or stop the queue
  1926. **/
  1927. static int i40e_ctrl_vf_tx_rings(struct i40e_vsi *vsi, unsigned long q_map,
  1928. bool enable)
  1929. {
  1930. struct i40e_pf *pf = vsi->back;
  1931. int ret = 0;
  1932. u16 q_id;
  1933. for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
  1934. ret = i40e_control_wait_tx_q(vsi->seid, pf,
  1935. vsi->base_queue + q_id,
  1936. false /*is xdp*/, enable);
  1937. if (ret)
  1938. break;
  1939. }
  1940. return ret;
  1941. }
  1942. /**
  1943. * i40e_ctrl_vf_rx_rings
  1944. * @vsi: the SRIOV VSI being configured
  1945. * @q_map: bit map of the queues to be enabled
  1946. * @enable: start or stop the queue
  1947. **/
  1948. static int i40e_ctrl_vf_rx_rings(struct i40e_vsi *vsi, unsigned long q_map,
  1949. bool enable)
  1950. {
  1951. struct i40e_pf *pf = vsi->back;
  1952. int ret = 0;
  1953. u16 q_id;
  1954. for_each_set_bit(q_id, &q_map, I40E_MAX_VF_QUEUES) {
  1955. ret = i40e_control_wait_rx_q(pf, vsi->base_queue + q_id,
  1956. enable);
  1957. if (ret)
  1958. break;
  1959. }
  1960. return ret;
  1961. }
  1962. /**
  1963. * i40e_vc_enable_queues_msg
  1964. * @vf: pointer to the VF info
  1965. * @msg: pointer to the msg buffer
  1966. * @msglen: msg length
  1967. *
  1968. * called from the VF to enable all or specific queue(s)
  1969. **/
  1970. static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1971. {
  1972. struct virtchnl_queue_select *vqs =
  1973. (struct virtchnl_queue_select *)msg;
  1974. struct i40e_pf *pf = vf->pf;
  1975. u16 vsi_id = vqs->vsi_id;
  1976. i40e_status aq_ret = 0;
  1977. int i;
  1978. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1979. aq_ret = I40E_ERR_PARAM;
  1980. goto error_param;
  1981. }
  1982. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1983. aq_ret = I40E_ERR_PARAM;
  1984. goto error_param;
  1985. }
  1986. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1987. aq_ret = I40E_ERR_PARAM;
  1988. goto error_param;
  1989. }
  1990. /* Use the queue bit map sent by the VF */
  1991. if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
  1992. true)) {
  1993. aq_ret = I40E_ERR_TIMEOUT;
  1994. goto error_param;
  1995. }
  1996. if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
  1997. true)) {
  1998. aq_ret = I40E_ERR_TIMEOUT;
  1999. goto error_param;
  2000. }
  2001. /* need to start the rings for additional ADq VSI's as well */
  2002. if (vf->adq_enabled) {
  2003. /* zero belongs to LAN VSI */
  2004. for (i = 1; i < vf->num_tc; i++) {
  2005. if (i40e_vsi_start_rings(pf->vsi[vf->ch[i].vsi_idx]))
  2006. aq_ret = I40E_ERR_TIMEOUT;
  2007. }
  2008. }
  2009. error_param:
  2010. /* send the response to the VF */
  2011. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
  2012. aq_ret);
  2013. }
  2014. /**
  2015. * i40e_vc_disable_queues_msg
  2016. * @vf: pointer to the VF info
  2017. * @msg: pointer to the msg buffer
  2018. * @msglen: msg length
  2019. *
  2020. * called from the VF to disable all or specific
  2021. * queue(s)
  2022. **/
  2023. static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2024. {
  2025. struct virtchnl_queue_select *vqs =
  2026. (struct virtchnl_queue_select *)msg;
  2027. struct i40e_pf *pf = vf->pf;
  2028. i40e_status aq_ret = 0;
  2029. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2030. aq_ret = I40E_ERR_PARAM;
  2031. goto error_param;
  2032. }
  2033. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  2034. aq_ret = I40E_ERR_PARAM;
  2035. goto error_param;
  2036. }
  2037. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  2038. aq_ret = I40E_ERR_PARAM;
  2039. goto error_param;
  2040. }
  2041. /* Use the queue bit map sent by the VF */
  2042. if (i40e_ctrl_vf_tx_rings(pf->vsi[vf->lan_vsi_idx], vqs->tx_queues,
  2043. false)) {
  2044. aq_ret = I40E_ERR_TIMEOUT;
  2045. goto error_param;
  2046. }
  2047. if (i40e_ctrl_vf_rx_rings(pf->vsi[vf->lan_vsi_idx], vqs->rx_queues,
  2048. false)) {
  2049. aq_ret = I40E_ERR_TIMEOUT;
  2050. goto error_param;
  2051. }
  2052. error_param:
  2053. /* send the response to the VF */
  2054. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
  2055. aq_ret);
  2056. }
  2057. /**
  2058. * i40e_vc_request_queues_msg
  2059. * @vf: pointer to the VF info
  2060. * @msg: pointer to the msg buffer
  2061. * @msglen: msg length
  2062. *
  2063. * VFs get a default number of queues but can use this message to request a
  2064. * different number. If the request is successful, PF will reset the VF and
  2065. * return 0. If unsuccessful, PF will send message informing VF of number of
  2066. * available queues and return result of sending VF a message.
  2067. **/
  2068. static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg, int msglen)
  2069. {
  2070. struct virtchnl_vf_res_request *vfres =
  2071. (struct virtchnl_vf_res_request *)msg;
  2072. int req_pairs = vfres->num_queue_pairs;
  2073. int cur_pairs = vf->num_queue_pairs;
  2074. struct i40e_pf *pf = vf->pf;
  2075. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  2076. return -EINVAL;
  2077. if (req_pairs <= 0) {
  2078. dev_err(&pf->pdev->dev,
  2079. "VF %d tried to request %d queues. Ignoring.\n",
  2080. vf->vf_id, req_pairs);
  2081. } else if (req_pairs > I40E_MAX_VF_QUEUES) {
  2082. dev_err(&pf->pdev->dev,
  2083. "VF %d tried to request more than %d queues.\n",
  2084. vf->vf_id,
  2085. I40E_MAX_VF_QUEUES);
  2086. vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
  2087. } else if (req_pairs - cur_pairs > pf->queues_left) {
  2088. dev_warn(&pf->pdev->dev,
  2089. "VF %d requested %d more queues, but only %d left.\n",
  2090. vf->vf_id,
  2091. req_pairs - cur_pairs,
  2092. pf->queues_left);
  2093. vfres->num_queue_pairs = pf->queues_left + cur_pairs;
  2094. } else {
  2095. /* successful request */
  2096. vf->num_req_queues = req_pairs;
  2097. i40e_vc_notify_vf_reset(vf);
  2098. i40e_reset_vf(vf, false);
  2099. return 0;
  2100. }
  2101. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
  2102. (u8 *)vfres, sizeof(*vfres));
  2103. }
  2104. /**
  2105. * i40e_vc_get_stats_msg
  2106. * @vf: pointer to the VF info
  2107. * @msg: pointer to the msg buffer
  2108. * @msglen: msg length
  2109. *
  2110. * called from the VF to get vsi stats
  2111. **/
  2112. static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2113. {
  2114. struct virtchnl_queue_select *vqs =
  2115. (struct virtchnl_queue_select *)msg;
  2116. struct i40e_pf *pf = vf->pf;
  2117. struct i40e_eth_stats stats;
  2118. i40e_status aq_ret = 0;
  2119. struct i40e_vsi *vsi;
  2120. memset(&stats, 0, sizeof(struct i40e_eth_stats));
  2121. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2122. aq_ret = I40E_ERR_PARAM;
  2123. goto error_param;
  2124. }
  2125. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  2126. aq_ret = I40E_ERR_PARAM;
  2127. goto error_param;
  2128. }
  2129. vsi = pf->vsi[vf->lan_vsi_idx];
  2130. if (!vsi) {
  2131. aq_ret = I40E_ERR_PARAM;
  2132. goto error_param;
  2133. }
  2134. i40e_update_eth_stats(vsi);
  2135. stats = vsi->eth_stats;
  2136. error_param:
  2137. /* send the response back to the VF */
  2138. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
  2139. (u8 *)&stats, sizeof(stats));
  2140. }
  2141. /* If the VF is not trusted restrict the number of MAC/VLAN it can program
  2142. * MAC filters: 16 for multicast, 1 for MAC, 1 for broadcast
  2143. */
  2144. #define I40E_VC_MAX_MAC_ADDR_PER_VF (16 + 1 + 1)
  2145. #define I40E_VC_MAX_VLAN_PER_VF 8
  2146. /**
  2147. * i40e_check_vf_permission
  2148. * @vf: pointer to the VF info
  2149. * @al: MAC address list from virtchnl
  2150. *
  2151. * Check that the given list of MAC addresses is allowed. Will return -EPERM
  2152. * if any address in the list is not valid. Checks the following conditions:
  2153. *
  2154. * 1) broadcast and zero addresses are never valid
  2155. * 2) unicast addresses are not allowed if the VMM has administratively set
  2156. * the VF MAC address, unless the VF is marked as privileged.
  2157. * 3) There is enough space to add all the addresses.
  2158. *
  2159. * Note that to guarantee consistency, it is expected this function be called
  2160. * while holding the mac_filter_hash_lock, as otherwise the current number of
  2161. * addresses might not be accurate.
  2162. **/
  2163. static inline int i40e_check_vf_permission(struct i40e_vf *vf,
  2164. struct virtchnl_ether_addr_list *al)
  2165. {
  2166. struct i40e_pf *pf = vf->pf;
  2167. int i;
  2168. /* If this VF is not privileged, then we can't add more than a limited
  2169. * number of addresses. Check to make sure that the additions do not
  2170. * push us over the limit.
  2171. */
  2172. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
  2173. (vf->num_mac + al->num_elements) > I40E_VC_MAX_MAC_ADDR_PER_VF) {
  2174. dev_err(&pf->pdev->dev,
  2175. "Cannot add more MAC addresses, VF is not trusted, switch the VF to trusted to add more functionality\n");
  2176. return -EPERM;
  2177. }
  2178. for (i = 0; i < al->num_elements; i++) {
  2179. u8 *addr = al->list[i].addr;
  2180. if (is_broadcast_ether_addr(addr) ||
  2181. is_zero_ether_addr(addr)) {
  2182. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
  2183. addr);
  2184. return I40E_ERR_INVALID_MAC_ADDR;
  2185. }
  2186. /* If the host VMM administrator has set the VF MAC address
  2187. * administratively via the ndo_set_vf_mac command then deny
  2188. * permission to the VF to add or delete unicast MAC addresses.
  2189. * Unless the VF is privileged and then it can do whatever.
  2190. * The VF may request to set the MAC address filter already
  2191. * assigned to it so do not return an error in that case.
  2192. */
  2193. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
  2194. !is_multicast_ether_addr(addr) && vf->pf_set_mac &&
  2195. !ether_addr_equal(addr, vf->default_lan_addr.addr)) {
  2196. dev_err(&pf->pdev->dev,
  2197. "VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
  2198. return -EPERM;
  2199. }
  2200. }
  2201. return 0;
  2202. }
  2203. /**
  2204. * i40e_vc_add_mac_addr_msg
  2205. * @vf: pointer to the VF info
  2206. * @msg: pointer to the msg buffer
  2207. * @msglen: msg length
  2208. *
  2209. * add guest mac address filter
  2210. **/
  2211. static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2212. {
  2213. struct virtchnl_ether_addr_list *al =
  2214. (struct virtchnl_ether_addr_list *)msg;
  2215. struct i40e_pf *pf = vf->pf;
  2216. struct i40e_vsi *vsi = NULL;
  2217. u16 vsi_id = al->vsi_id;
  2218. i40e_status ret = 0;
  2219. int i;
  2220. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2221. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2222. ret = I40E_ERR_PARAM;
  2223. goto error_param;
  2224. }
  2225. vsi = pf->vsi[vf->lan_vsi_idx];
  2226. /* Lock once, because all function inside for loop accesses VSI's
  2227. * MAC filter list which needs to be protected using same lock.
  2228. */
  2229. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2230. ret = i40e_check_vf_permission(vf, al);
  2231. if (ret) {
  2232. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2233. goto error_param;
  2234. }
  2235. /* add new addresses to the list */
  2236. for (i = 0; i < al->num_elements; i++) {
  2237. struct i40e_mac_filter *f;
  2238. f = i40e_find_mac(vsi, al->list[i].addr);
  2239. if (!f) {
  2240. f = i40e_add_mac_filter(vsi, al->list[i].addr);
  2241. if (!f) {
  2242. dev_err(&pf->pdev->dev,
  2243. "Unable to add MAC filter %pM for VF %d\n",
  2244. al->list[i].addr, vf->vf_id);
  2245. ret = I40E_ERR_PARAM;
  2246. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2247. goto error_param;
  2248. } else {
  2249. vf->num_mac++;
  2250. }
  2251. }
  2252. }
  2253. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2254. /* program the updated filter list */
  2255. ret = i40e_sync_vsi_filters(vsi);
  2256. if (ret)
  2257. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  2258. vf->vf_id, ret);
  2259. error_param:
  2260. /* send the response to the VF */
  2261. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
  2262. ret);
  2263. }
  2264. /**
  2265. * i40e_vc_del_mac_addr_msg
  2266. * @vf: pointer to the VF info
  2267. * @msg: pointer to the msg buffer
  2268. * @msglen: msg length
  2269. *
  2270. * remove guest mac address filter
  2271. **/
  2272. static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2273. {
  2274. struct virtchnl_ether_addr_list *al =
  2275. (struct virtchnl_ether_addr_list *)msg;
  2276. struct i40e_pf *pf = vf->pf;
  2277. struct i40e_vsi *vsi = NULL;
  2278. u16 vsi_id = al->vsi_id;
  2279. i40e_status ret = 0;
  2280. int i;
  2281. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2282. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2283. ret = I40E_ERR_PARAM;
  2284. goto error_param;
  2285. }
  2286. for (i = 0; i < al->num_elements; i++) {
  2287. if (is_broadcast_ether_addr(al->list[i].addr) ||
  2288. is_zero_ether_addr(al->list[i].addr)) {
  2289. dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
  2290. al->list[i].addr, vf->vf_id);
  2291. ret = I40E_ERR_INVALID_MAC_ADDR;
  2292. goto error_param;
  2293. }
  2294. if (vf->pf_set_mac &&
  2295. ether_addr_equal(al->list[i].addr,
  2296. vf->default_lan_addr.addr)) {
  2297. dev_err(&pf->pdev->dev,
  2298. "MAC addr %pM has been set by PF, cannot delete it for VF %d, reset VF to change MAC addr\n",
  2299. vf->default_lan_addr.addr, vf->vf_id);
  2300. ret = I40E_ERR_PARAM;
  2301. goto error_param;
  2302. }
  2303. }
  2304. vsi = pf->vsi[vf->lan_vsi_idx];
  2305. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2306. /* delete addresses from the list */
  2307. for (i = 0; i < al->num_elements; i++)
  2308. if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
  2309. ret = I40E_ERR_INVALID_MAC_ADDR;
  2310. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2311. goto error_param;
  2312. } else {
  2313. vf->num_mac--;
  2314. }
  2315. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2316. /* program the updated filter list */
  2317. ret = i40e_sync_vsi_filters(vsi);
  2318. if (ret)
  2319. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  2320. vf->vf_id, ret);
  2321. error_param:
  2322. /* send the response to the VF */
  2323. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
  2324. ret);
  2325. }
  2326. /**
  2327. * i40e_vc_add_vlan_msg
  2328. * @vf: pointer to the VF info
  2329. * @msg: pointer to the msg buffer
  2330. * @msglen: msg length
  2331. *
  2332. * program guest vlan id
  2333. **/
  2334. static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2335. {
  2336. struct virtchnl_vlan_filter_list *vfl =
  2337. (struct virtchnl_vlan_filter_list *)msg;
  2338. struct i40e_pf *pf = vf->pf;
  2339. struct i40e_vsi *vsi = NULL;
  2340. u16 vsi_id = vfl->vsi_id;
  2341. i40e_status aq_ret = 0;
  2342. int i;
  2343. if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
  2344. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  2345. dev_err(&pf->pdev->dev,
  2346. "VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
  2347. goto error_param;
  2348. }
  2349. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2350. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2351. aq_ret = I40E_ERR_PARAM;
  2352. goto error_param;
  2353. }
  2354. for (i = 0; i < vfl->num_elements; i++) {
  2355. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2356. aq_ret = I40E_ERR_PARAM;
  2357. dev_err(&pf->pdev->dev,
  2358. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  2359. goto error_param;
  2360. }
  2361. }
  2362. vsi = pf->vsi[vf->lan_vsi_idx];
  2363. if (vsi->info.pvid) {
  2364. aq_ret = I40E_ERR_PARAM;
  2365. goto error_param;
  2366. }
  2367. i40e_vlan_stripping_enable(vsi);
  2368. for (i = 0; i < vfl->num_elements; i++) {
  2369. /* add new VLAN filter */
  2370. int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
  2371. if (!ret)
  2372. vf->num_vlan++;
  2373. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2374. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2375. true,
  2376. vfl->vlan_id[i],
  2377. NULL);
  2378. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2379. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2380. true,
  2381. vfl->vlan_id[i],
  2382. NULL);
  2383. if (ret)
  2384. dev_err(&pf->pdev->dev,
  2385. "Unable to add VLAN filter %d for VF %d, error %d\n",
  2386. vfl->vlan_id[i], vf->vf_id, ret);
  2387. }
  2388. error_param:
  2389. /* send the response to the VF */
  2390. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
  2391. }
  2392. /**
  2393. * i40e_vc_remove_vlan_msg
  2394. * @vf: pointer to the VF info
  2395. * @msg: pointer to the msg buffer
  2396. * @msglen: msg length
  2397. *
  2398. * remove programmed guest vlan id
  2399. **/
  2400. static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2401. {
  2402. struct virtchnl_vlan_filter_list *vfl =
  2403. (struct virtchnl_vlan_filter_list *)msg;
  2404. struct i40e_pf *pf = vf->pf;
  2405. struct i40e_vsi *vsi = NULL;
  2406. u16 vsi_id = vfl->vsi_id;
  2407. i40e_status aq_ret = 0;
  2408. int i;
  2409. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2410. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2411. aq_ret = I40E_ERR_PARAM;
  2412. goto error_param;
  2413. }
  2414. for (i = 0; i < vfl->num_elements; i++) {
  2415. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2416. aq_ret = I40E_ERR_PARAM;
  2417. goto error_param;
  2418. }
  2419. }
  2420. vsi = pf->vsi[vf->lan_vsi_idx];
  2421. if (vsi->info.pvid) {
  2422. aq_ret = I40E_ERR_PARAM;
  2423. goto error_param;
  2424. }
  2425. for (i = 0; i < vfl->num_elements; i++) {
  2426. i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
  2427. vf->num_vlan--;
  2428. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2429. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2430. false,
  2431. vfl->vlan_id[i],
  2432. NULL);
  2433. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2434. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2435. false,
  2436. vfl->vlan_id[i],
  2437. NULL);
  2438. }
  2439. error_param:
  2440. /* send the response to the VF */
  2441. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
  2442. }
  2443. /**
  2444. * i40e_vc_iwarp_msg
  2445. * @vf: pointer to the VF info
  2446. * @msg: pointer to the msg buffer
  2447. * @msglen: msg length
  2448. *
  2449. * called from the VF for the iwarp msgs
  2450. **/
  2451. static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2452. {
  2453. struct i40e_pf *pf = vf->pf;
  2454. int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
  2455. i40e_status aq_ret = 0;
  2456. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2457. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2458. aq_ret = I40E_ERR_PARAM;
  2459. goto error_param;
  2460. }
  2461. i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
  2462. msg, msglen);
  2463. error_param:
  2464. /* send the response to the VF */
  2465. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
  2466. aq_ret);
  2467. }
  2468. /**
  2469. * i40e_vc_iwarp_qvmap_msg
  2470. * @vf: pointer to the VF info
  2471. * @msg: pointer to the msg buffer
  2472. * @msglen: msg length
  2473. * @config: config qvmap or release it
  2474. *
  2475. * called from the VF for the iwarp msgs
  2476. **/
  2477. static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
  2478. bool config)
  2479. {
  2480. struct virtchnl_iwarp_qvlist_info *qvlist_info =
  2481. (struct virtchnl_iwarp_qvlist_info *)msg;
  2482. i40e_status aq_ret = 0;
  2483. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2484. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2485. aq_ret = I40E_ERR_PARAM;
  2486. goto error_param;
  2487. }
  2488. if (config) {
  2489. if (i40e_config_iwarp_qvlist(vf, qvlist_info))
  2490. aq_ret = I40E_ERR_PARAM;
  2491. } else {
  2492. i40e_release_iwarp_qvlist(vf);
  2493. }
  2494. error_param:
  2495. /* send the response to the VF */
  2496. return i40e_vc_send_resp_to_vf(vf,
  2497. config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
  2498. VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
  2499. aq_ret);
  2500. }
  2501. /**
  2502. * i40e_vc_config_rss_key
  2503. * @vf: pointer to the VF info
  2504. * @msg: pointer to the msg buffer
  2505. * @msglen: msg length
  2506. *
  2507. * Configure the VF's RSS key
  2508. **/
  2509. static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2510. {
  2511. struct virtchnl_rss_key *vrk =
  2512. (struct virtchnl_rss_key *)msg;
  2513. struct i40e_pf *pf = vf->pf;
  2514. struct i40e_vsi *vsi = NULL;
  2515. u16 vsi_id = vrk->vsi_id;
  2516. i40e_status aq_ret = 0;
  2517. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2518. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2519. (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
  2520. aq_ret = I40E_ERR_PARAM;
  2521. goto err;
  2522. }
  2523. vsi = pf->vsi[vf->lan_vsi_idx];
  2524. aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
  2525. err:
  2526. /* send the response to the VF */
  2527. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
  2528. aq_ret);
  2529. }
  2530. /**
  2531. * i40e_vc_config_rss_lut
  2532. * @vf: pointer to the VF info
  2533. * @msg: pointer to the msg buffer
  2534. * @msglen: msg length
  2535. *
  2536. * Configure the VF's RSS LUT
  2537. **/
  2538. static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2539. {
  2540. struct virtchnl_rss_lut *vrl =
  2541. (struct virtchnl_rss_lut *)msg;
  2542. struct i40e_pf *pf = vf->pf;
  2543. struct i40e_vsi *vsi = NULL;
  2544. u16 vsi_id = vrl->vsi_id;
  2545. i40e_status aq_ret = 0;
  2546. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2547. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2548. (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
  2549. aq_ret = I40E_ERR_PARAM;
  2550. goto err;
  2551. }
  2552. vsi = pf->vsi[vf->lan_vsi_idx];
  2553. aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
  2554. /* send the response to the VF */
  2555. err:
  2556. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
  2557. aq_ret);
  2558. }
  2559. /**
  2560. * i40e_vc_get_rss_hena
  2561. * @vf: pointer to the VF info
  2562. * @msg: pointer to the msg buffer
  2563. * @msglen: msg length
  2564. *
  2565. * Return the RSS HENA bits allowed by the hardware
  2566. **/
  2567. static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2568. {
  2569. struct virtchnl_rss_hena *vrh = NULL;
  2570. struct i40e_pf *pf = vf->pf;
  2571. i40e_status aq_ret = 0;
  2572. int len = 0;
  2573. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2574. aq_ret = I40E_ERR_PARAM;
  2575. goto err;
  2576. }
  2577. len = sizeof(struct virtchnl_rss_hena);
  2578. vrh = kzalloc(len, GFP_KERNEL);
  2579. if (!vrh) {
  2580. aq_ret = I40E_ERR_NO_MEMORY;
  2581. len = 0;
  2582. goto err;
  2583. }
  2584. vrh->hena = i40e_pf_get_default_rss_hena(pf);
  2585. err:
  2586. /* send the response back to the VF */
  2587. aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
  2588. aq_ret, (u8 *)vrh, len);
  2589. kfree(vrh);
  2590. return aq_ret;
  2591. }
  2592. /**
  2593. * i40e_vc_set_rss_hena
  2594. * @vf: pointer to the VF info
  2595. * @msg: pointer to the msg buffer
  2596. * @msglen: msg length
  2597. *
  2598. * Set the RSS HENA bits for the VF
  2599. **/
  2600. static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2601. {
  2602. struct virtchnl_rss_hena *vrh =
  2603. (struct virtchnl_rss_hena *)msg;
  2604. struct i40e_pf *pf = vf->pf;
  2605. struct i40e_hw *hw = &pf->hw;
  2606. i40e_status aq_ret = 0;
  2607. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2608. aq_ret = I40E_ERR_PARAM;
  2609. goto err;
  2610. }
  2611. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
  2612. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
  2613. (u32)(vrh->hena >> 32));
  2614. /* send the response to the VF */
  2615. err:
  2616. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
  2617. }
  2618. /**
  2619. * i40e_vc_enable_vlan_stripping
  2620. * @vf: pointer to the VF info
  2621. * @msg: pointer to the msg buffer
  2622. * @msglen: msg length
  2623. *
  2624. * Enable vlan header stripping for the VF
  2625. **/
  2626. static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
  2627. u16 msglen)
  2628. {
  2629. struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
  2630. i40e_status aq_ret = 0;
  2631. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2632. aq_ret = I40E_ERR_PARAM;
  2633. goto err;
  2634. }
  2635. i40e_vlan_stripping_enable(vsi);
  2636. /* send the response to the VF */
  2637. err:
  2638. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
  2639. aq_ret);
  2640. }
  2641. /**
  2642. * i40e_vc_disable_vlan_stripping
  2643. * @vf: pointer to the VF info
  2644. * @msg: pointer to the msg buffer
  2645. * @msglen: msg length
  2646. *
  2647. * Disable vlan header stripping for the VF
  2648. **/
  2649. static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
  2650. u16 msglen)
  2651. {
  2652. struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
  2653. i40e_status aq_ret = 0;
  2654. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2655. aq_ret = I40E_ERR_PARAM;
  2656. goto err;
  2657. }
  2658. i40e_vlan_stripping_disable(vsi);
  2659. /* send the response to the VF */
  2660. err:
  2661. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
  2662. aq_ret);
  2663. }
  2664. /**
  2665. * i40e_validate_cloud_filter
  2666. * @mask: mask for TC filter
  2667. * @data: data for TC filter
  2668. *
  2669. * This function validates cloud filter programmed as TC filter for ADq
  2670. **/
  2671. static int i40e_validate_cloud_filter(struct i40e_vf *vf,
  2672. struct virtchnl_filter *tc_filter)
  2673. {
  2674. struct virtchnl_l4_spec mask = tc_filter->mask.tcp_spec;
  2675. struct virtchnl_l4_spec data = tc_filter->data.tcp_spec;
  2676. struct i40e_pf *pf = vf->pf;
  2677. struct i40e_vsi *vsi = NULL;
  2678. struct i40e_mac_filter *f;
  2679. struct hlist_node *h;
  2680. bool found = false;
  2681. int bkt;
  2682. if (!tc_filter->action) {
  2683. dev_info(&pf->pdev->dev,
  2684. "VF %d: Currently ADq doesn't support Drop Action\n",
  2685. vf->vf_id);
  2686. goto err;
  2687. }
  2688. /* action_meta is TC number here to which the filter is applied */
  2689. if (!tc_filter->action_meta ||
  2690. tc_filter->action_meta > I40E_MAX_VF_VSI) {
  2691. dev_info(&pf->pdev->dev, "VF %d: Invalid TC number %u\n",
  2692. vf->vf_id, tc_filter->action_meta);
  2693. goto err;
  2694. }
  2695. /* Check filter if it's programmed for advanced mode or basic mode.
  2696. * There are two ADq modes (for VF only),
  2697. * 1. Basic mode: intended to allow as many filter options as possible
  2698. * to be added to a VF in Non-trusted mode. Main goal is
  2699. * to add filters to its own MAC and VLAN id.
  2700. * 2. Advanced mode: is for allowing filters to be applied other than
  2701. * its own MAC or VLAN. This mode requires the VF to be
  2702. * Trusted.
  2703. */
  2704. if (mask.dst_mac[0] && !mask.dst_ip[0]) {
  2705. vsi = pf->vsi[vf->lan_vsi_idx];
  2706. f = i40e_find_mac(vsi, data.dst_mac);
  2707. if (!f) {
  2708. dev_info(&pf->pdev->dev,
  2709. "Destination MAC %pM doesn't belong to VF %d\n",
  2710. data.dst_mac, vf->vf_id);
  2711. goto err;
  2712. }
  2713. if (mask.vlan_id) {
  2714. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f,
  2715. hlist) {
  2716. if (f->vlan == ntohs(data.vlan_id)) {
  2717. found = true;
  2718. break;
  2719. }
  2720. }
  2721. if (!found) {
  2722. dev_info(&pf->pdev->dev,
  2723. "VF %d doesn't have any VLAN id %u\n",
  2724. vf->vf_id, ntohs(data.vlan_id));
  2725. goto err;
  2726. }
  2727. }
  2728. } else {
  2729. /* Check if VF is trusted */
  2730. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  2731. dev_err(&pf->pdev->dev,
  2732. "VF %d not trusted, make VF trusted to add advanced mode ADq cloud filters\n",
  2733. vf->vf_id);
  2734. return I40E_ERR_CONFIG;
  2735. }
  2736. }
  2737. if (mask.dst_mac[0] & data.dst_mac[0]) {
  2738. if (is_broadcast_ether_addr(data.dst_mac) ||
  2739. is_zero_ether_addr(data.dst_mac)) {
  2740. dev_info(&pf->pdev->dev, "VF %d: Invalid Dest MAC addr %pM\n",
  2741. vf->vf_id, data.dst_mac);
  2742. goto err;
  2743. }
  2744. }
  2745. if (mask.src_mac[0] & data.src_mac[0]) {
  2746. if (is_broadcast_ether_addr(data.src_mac) ||
  2747. is_zero_ether_addr(data.src_mac)) {
  2748. dev_info(&pf->pdev->dev, "VF %d: Invalid Source MAC addr %pM\n",
  2749. vf->vf_id, data.src_mac);
  2750. goto err;
  2751. }
  2752. }
  2753. if (mask.dst_port & data.dst_port) {
  2754. if (!data.dst_port || be16_to_cpu(data.dst_port) > 0xFFFF) {
  2755. dev_info(&pf->pdev->dev, "VF %d: Invalid Dest port\n",
  2756. vf->vf_id);
  2757. goto err;
  2758. }
  2759. }
  2760. if (mask.src_port & data.src_port) {
  2761. if (!data.src_port || be16_to_cpu(data.src_port) > 0xFFFF) {
  2762. dev_info(&pf->pdev->dev, "VF %d: Invalid Source port\n",
  2763. vf->vf_id);
  2764. goto err;
  2765. }
  2766. }
  2767. if (tc_filter->flow_type != VIRTCHNL_TCP_V6_FLOW &&
  2768. tc_filter->flow_type != VIRTCHNL_TCP_V4_FLOW) {
  2769. dev_info(&pf->pdev->dev, "VF %d: Invalid Flow type\n",
  2770. vf->vf_id);
  2771. goto err;
  2772. }
  2773. if (mask.vlan_id & data.vlan_id) {
  2774. if (ntohs(data.vlan_id) > I40E_MAX_VLANID) {
  2775. dev_info(&pf->pdev->dev, "VF %d: invalid VLAN ID\n",
  2776. vf->vf_id);
  2777. goto err;
  2778. }
  2779. }
  2780. return I40E_SUCCESS;
  2781. err:
  2782. return I40E_ERR_CONFIG;
  2783. }
  2784. /**
  2785. * i40e_find_vsi_from_seid - searches for the vsi with the given seid
  2786. * @vf: pointer to the VF info
  2787. * @seid - seid of the vsi it is searching for
  2788. **/
  2789. static struct i40e_vsi *i40e_find_vsi_from_seid(struct i40e_vf *vf, u16 seid)
  2790. {
  2791. struct i40e_pf *pf = vf->pf;
  2792. struct i40e_vsi *vsi = NULL;
  2793. int i;
  2794. for (i = 0; i < vf->num_tc ; i++) {
  2795. vsi = i40e_find_vsi_from_id(pf, vf->ch[i].vsi_id);
  2796. if (vsi && vsi->seid == seid)
  2797. return vsi;
  2798. }
  2799. return NULL;
  2800. }
  2801. /**
  2802. * i40e_del_all_cloud_filters
  2803. * @vf: pointer to the VF info
  2804. *
  2805. * This function deletes all cloud filters
  2806. **/
  2807. static void i40e_del_all_cloud_filters(struct i40e_vf *vf)
  2808. {
  2809. struct i40e_cloud_filter *cfilter = NULL;
  2810. struct i40e_pf *pf = vf->pf;
  2811. struct i40e_vsi *vsi = NULL;
  2812. struct hlist_node *node;
  2813. int ret;
  2814. hlist_for_each_entry_safe(cfilter, node,
  2815. &vf->cloud_filter_list, cloud_node) {
  2816. vsi = i40e_find_vsi_from_seid(vf, cfilter->seid);
  2817. if (!vsi) {
  2818. dev_err(&pf->pdev->dev, "VF %d: no VSI found for matching %u seid, can't delete cloud filter\n",
  2819. vf->vf_id, cfilter->seid);
  2820. continue;
  2821. }
  2822. if (cfilter->dst_port)
  2823. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
  2824. false);
  2825. else
  2826. ret = i40e_add_del_cloud_filter(vsi, cfilter, false);
  2827. if (ret)
  2828. dev_err(&pf->pdev->dev,
  2829. "VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
  2830. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  2831. i40e_aq_str(&pf->hw,
  2832. pf->hw.aq.asq_last_status));
  2833. hlist_del(&cfilter->cloud_node);
  2834. kfree(cfilter);
  2835. vf->num_cloud_filters--;
  2836. }
  2837. }
  2838. /**
  2839. * i40e_vc_del_cloud_filter
  2840. * @vf: pointer to the VF info
  2841. * @msg: pointer to the msg buffer
  2842. *
  2843. * This function deletes a cloud filter programmed as TC filter for ADq
  2844. **/
  2845. static int i40e_vc_del_cloud_filter(struct i40e_vf *vf, u8 *msg)
  2846. {
  2847. struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
  2848. struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
  2849. struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
  2850. struct i40e_cloud_filter cfilter, *cf = NULL;
  2851. struct i40e_pf *pf = vf->pf;
  2852. struct i40e_vsi *vsi = NULL;
  2853. struct hlist_node *node;
  2854. i40e_status aq_ret = 0;
  2855. int i, ret;
  2856. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2857. aq_ret = I40E_ERR_PARAM;
  2858. goto err;
  2859. }
  2860. if (!vf->adq_enabled) {
  2861. dev_info(&pf->pdev->dev,
  2862. "VF %d: ADq not enabled, can't apply cloud filter\n",
  2863. vf->vf_id);
  2864. aq_ret = I40E_ERR_PARAM;
  2865. goto err;
  2866. }
  2867. if (i40e_validate_cloud_filter(vf, vcf)) {
  2868. dev_info(&pf->pdev->dev,
  2869. "VF %d: Invalid input, can't apply cloud filter\n",
  2870. vf->vf_id);
  2871. aq_ret = I40E_ERR_PARAM;
  2872. goto err;
  2873. }
  2874. memset(&cfilter, 0, sizeof(cfilter));
  2875. /* parse destination mac address */
  2876. for (i = 0; i < ETH_ALEN; i++)
  2877. cfilter.dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
  2878. /* parse source mac address */
  2879. for (i = 0; i < ETH_ALEN; i++)
  2880. cfilter.src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
  2881. cfilter.vlan_id = mask.vlan_id & tcf.vlan_id;
  2882. cfilter.dst_port = mask.dst_port & tcf.dst_port;
  2883. cfilter.src_port = mask.src_port & tcf.src_port;
  2884. switch (vcf->flow_type) {
  2885. case VIRTCHNL_TCP_V4_FLOW:
  2886. cfilter.n_proto = ETH_P_IP;
  2887. if (mask.dst_ip[0] & tcf.dst_ip[0])
  2888. memcpy(&cfilter.ip.v4.dst_ip, tcf.dst_ip,
  2889. ARRAY_SIZE(tcf.dst_ip));
  2890. else if (mask.src_ip[0] & tcf.dst_ip[0])
  2891. memcpy(&cfilter.ip.v4.src_ip, tcf.src_ip,
  2892. ARRAY_SIZE(tcf.dst_ip));
  2893. break;
  2894. case VIRTCHNL_TCP_V6_FLOW:
  2895. cfilter.n_proto = ETH_P_IPV6;
  2896. if (mask.dst_ip[3] & tcf.dst_ip[3])
  2897. memcpy(&cfilter.ip.v6.dst_ip6, tcf.dst_ip,
  2898. sizeof(cfilter.ip.v6.dst_ip6));
  2899. if (mask.src_ip[3] & tcf.src_ip[3])
  2900. memcpy(&cfilter.ip.v6.src_ip6, tcf.src_ip,
  2901. sizeof(cfilter.ip.v6.src_ip6));
  2902. break;
  2903. default:
  2904. /* TC filter can be configured based on different combinations
  2905. * and in this case IP is not a part of filter config
  2906. */
  2907. dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
  2908. vf->vf_id);
  2909. }
  2910. /* get the vsi to which the tc belongs to */
  2911. vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
  2912. cfilter.seid = vsi->seid;
  2913. cfilter.flags = vcf->field_flags;
  2914. /* Deleting TC filter */
  2915. if (tcf.dst_port)
  2916. ret = i40e_add_del_cloud_filter_big_buf(vsi, &cfilter, false);
  2917. else
  2918. ret = i40e_add_del_cloud_filter(vsi, &cfilter, false);
  2919. if (ret) {
  2920. dev_err(&pf->pdev->dev,
  2921. "VF %d: Failed to delete cloud filter, err %s aq_err %s\n",
  2922. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  2923. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  2924. goto err;
  2925. }
  2926. hlist_for_each_entry_safe(cf, node,
  2927. &vf->cloud_filter_list, cloud_node) {
  2928. if (cf->seid != cfilter.seid)
  2929. continue;
  2930. if (mask.dst_port)
  2931. if (cfilter.dst_port != cf->dst_port)
  2932. continue;
  2933. if (mask.dst_mac[0])
  2934. if (!ether_addr_equal(cf->src_mac, cfilter.src_mac))
  2935. continue;
  2936. /* for ipv4 data to be valid, only first byte of mask is set */
  2937. if (cfilter.n_proto == ETH_P_IP && mask.dst_ip[0])
  2938. if (memcmp(&cfilter.ip.v4.dst_ip, &cf->ip.v4.dst_ip,
  2939. ARRAY_SIZE(tcf.dst_ip)))
  2940. continue;
  2941. /* for ipv6, mask is set for all sixteen bytes (4 words) */
  2942. if (cfilter.n_proto == ETH_P_IPV6 && mask.dst_ip[3])
  2943. if (memcmp(&cfilter.ip.v6.dst_ip6, &cf->ip.v6.dst_ip6,
  2944. sizeof(cfilter.ip.v6.src_ip6)))
  2945. continue;
  2946. if (mask.vlan_id)
  2947. if (cfilter.vlan_id != cf->vlan_id)
  2948. continue;
  2949. hlist_del(&cf->cloud_node);
  2950. kfree(cf);
  2951. vf->num_cloud_filters--;
  2952. }
  2953. err:
  2954. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_CLOUD_FILTER,
  2955. aq_ret);
  2956. }
  2957. /**
  2958. * i40e_vc_add_cloud_filter
  2959. * @vf: pointer to the VF info
  2960. * @msg: pointer to the msg buffer
  2961. *
  2962. * This function adds a cloud filter programmed as TC filter for ADq
  2963. **/
  2964. static int i40e_vc_add_cloud_filter(struct i40e_vf *vf, u8 *msg)
  2965. {
  2966. struct virtchnl_filter *vcf = (struct virtchnl_filter *)msg;
  2967. struct virtchnl_l4_spec mask = vcf->mask.tcp_spec;
  2968. struct virtchnl_l4_spec tcf = vcf->data.tcp_spec;
  2969. struct i40e_cloud_filter *cfilter = NULL;
  2970. struct i40e_pf *pf = vf->pf;
  2971. struct i40e_vsi *vsi = NULL;
  2972. i40e_status aq_ret = 0;
  2973. int i, ret;
  2974. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2975. aq_ret = I40E_ERR_PARAM;
  2976. goto err_out;
  2977. }
  2978. if (!vf->adq_enabled) {
  2979. dev_info(&pf->pdev->dev,
  2980. "VF %d: ADq is not enabled, can't apply cloud filter\n",
  2981. vf->vf_id);
  2982. aq_ret = I40E_ERR_PARAM;
  2983. goto err_out;
  2984. }
  2985. if (i40e_validate_cloud_filter(vf, vcf)) {
  2986. dev_info(&pf->pdev->dev,
  2987. "VF %d: Invalid input/s, can't apply cloud filter\n",
  2988. vf->vf_id);
  2989. aq_ret = I40E_ERR_PARAM;
  2990. goto err_out;
  2991. }
  2992. cfilter = kzalloc(sizeof(*cfilter), GFP_KERNEL);
  2993. if (!cfilter)
  2994. return -ENOMEM;
  2995. /* parse destination mac address */
  2996. for (i = 0; i < ETH_ALEN; i++)
  2997. cfilter->dst_mac[i] = mask.dst_mac[i] & tcf.dst_mac[i];
  2998. /* parse source mac address */
  2999. for (i = 0; i < ETH_ALEN; i++)
  3000. cfilter->src_mac[i] = mask.src_mac[i] & tcf.src_mac[i];
  3001. cfilter->vlan_id = mask.vlan_id & tcf.vlan_id;
  3002. cfilter->dst_port = mask.dst_port & tcf.dst_port;
  3003. cfilter->src_port = mask.src_port & tcf.src_port;
  3004. switch (vcf->flow_type) {
  3005. case VIRTCHNL_TCP_V4_FLOW:
  3006. cfilter->n_proto = ETH_P_IP;
  3007. if (mask.dst_ip[0] & tcf.dst_ip[0])
  3008. memcpy(&cfilter->ip.v4.dst_ip, tcf.dst_ip,
  3009. ARRAY_SIZE(tcf.dst_ip));
  3010. else if (mask.src_ip[0] & tcf.dst_ip[0])
  3011. memcpy(&cfilter->ip.v4.src_ip, tcf.src_ip,
  3012. ARRAY_SIZE(tcf.dst_ip));
  3013. break;
  3014. case VIRTCHNL_TCP_V6_FLOW:
  3015. cfilter->n_proto = ETH_P_IPV6;
  3016. if (mask.dst_ip[3] & tcf.dst_ip[3])
  3017. memcpy(&cfilter->ip.v6.dst_ip6, tcf.dst_ip,
  3018. sizeof(cfilter->ip.v6.dst_ip6));
  3019. if (mask.src_ip[3] & tcf.src_ip[3])
  3020. memcpy(&cfilter->ip.v6.src_ip6, tcf.src_ip,
  3021. sizeof(cfilter->ip.v6.src_ip6));
  3022. break;
  3023. default:
  3024. /* TC filter can be configured based on different combinations
  3025. * and in this case IP is not a part of filter config
  3026. */
  3027. dev_info(&pf->pdev->dev, "VF %d: Flow type not configured\n",
  3028. vf->vf_id);
  3029. }
  3030. /* get the VSI to which the TC belongs to */
  3031. vsi = pf->vsi[vf->ch[vcf->action_meta].vsi_idx];
  3032. cfilter->seid = vsi->seid;
  3033. cfilter->flags = vcf->field_flags;
  3034. /* Adding cloud filter programmed as TC filter */
  3035. if (tcf.dst_port)
  3036. ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter, true);
  3037. else
  3038. ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
  3039. if (ret) {
  3040. dev_err(&pf->pdev->dev,
  3041. "VF %d: Failed to add cloud filter, err %s aq_err %s\n",
  3042. vf->vf_id, i40e_stat_str(&pf->hw, ret),
  3043. i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
  3044. goto err_free;
  3045. }
  3046. INIT_HLIST_NODE(&cfilter->cloud_node);
  3047. hlist_add_head(&cfilter->cloud_node, &vf->cloud_filter_list);
  3048. /* release the pointer passing it to the collection */
  3049. cfilter = NULL;
  3050. vf->num_cloud_filters++;
  3051. err_free:
  3052. kfree(cfilter);
  3053. err_out:
  3054. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_CLOUD_FILTER,
  3055. aq_ret);
  3056. }
  3057. /**
  3058. * i40e_vc_add_qch_msg: Add queue channel and enable ADq
  3059. * @vf: pointer to the VF info
  3060. * @msg: pointer to the msg buffer
  3061. **/
  3062. static int i40e_vc_add_qch_msg(struct i40e_vf *vf, u8 *msg)
  3063. {
  3064. struct virtchnl_tc_info *tci =
  3065. (struct virtchnl_tc_info *)msg;
  3066. struct i40e_pf *pf = vf->pf;
  3067. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  3068. int i, adq_request_qps = 0, speed = 0;
  3069. i40e_status aq_ret = 0;
  3070. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  3071. aq_ret = I40E_ERR_PARAM;
  3072. goto err;
  3073. }
  3074. /* ADq cannot be applied if spoof check is ON */
  3075. if (vf->spoofchk) {
  3076. dev_err(&pf->pdev->dev,
  3077. "Spoof check is ON, turn it OFF to enable ADq\n");
  3078. aq_ret = I40E_ERR_PARAM;
  3079. goto err;
  3080. }
  3081. if (!(vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ADQ)) {
  3082. dev_err(&pf->pdev->dev,
  3083. "VF %d attempting to enable ADq, but hasn't properly negotiated that capability\n",
  3084. vf->vf_id);
  3085. aq_ret = I40E_ERR_PARAM;
  3086. goto err;
  3087. }
  3088. /* max number of traffic classes for VF currently capped at 4 */
  3089. if (!tci->num_tc || tci->num_tc > I40E_MAX_VF_VSI) {
  3090. dev_err(&pf->pdev->dev,
  3091. "VF %d trying to set %u TCs, valid range 1-4 TCs per VF\n",
  3092. vf->vf_id, tci->num_tc);
  3093. aq_ret = I40E_ERR_PARAM;
  3094. goto err;
  3095. }
  3096. /* validate queues for each TC */
  3097. for (i = 0; i < tci->num_tc; i++)
  3098. if (!tci->list[i].count ||
  3099. tci->list[i].count > I40E_DEFAULT_QUEUES_PER_VF) {
  3100. dev_err(&pf->pdev->dev,
  3101. "VF %d: TC %d trying to set %u queues, valid range 1-4 queues per TC\n",
  3102. vf->vf_id, i, tci->list[i].count);
  3103. aq_ret = I40E_ERR_PARAM;
  3104. goto err;
  3105. }
  3106. /* need Max VF queues but already have default number of queues */
  3107. adq_request_qps = I40E_MAX_VF_QUEUES - I40E_DEFAULT_QUEUES_PER_VF;
  3108. if (pf->queues_left < adq_request_qps) {
  3109. dev_err(&pf->pdev->dev,
  3110. "No queues left to allocate to VF %d\n",
  3111. vf->vf_id);
  3112. aq_ret = I40E_ERR_PARAM;
  3113. goto err;
  3114. } else {
  3115. /* we need to allocate max VF queues to enable ADq so as to
  3116. * make sure ADq enabled VF always gets back queues when it
  3117. * goes through a reset.
  3118. */
  3119. vf->num_queue_pairs = I40E_MAX_VF_QUEUES;
  3120. }
  3121. /* get link speed in MB to validate rate limit */
  3122. switch (ls->link_speed) {
  3123. case VIRTCHNL_LINK_SPEED_100MB:
  3124. speed = SPEED_100;
  3125. break;
  3126. case VIRTCHNL_LINK_SPEED_1GB:
  3127. speed = SPEED_1000;
  3128. break;
  3129. case VIRTCHNL_LINK_SPEED_10GB:
  3130. speed = SPEED_10000;
  3131. break;
  3132. case VIRTCHNL_LINK_SPEED_20GB:
  3133. speed = SPEED_20000;
  3134. break;
  3135. case VIRTCHNL_LINK_SPEED_25GB:
  3136. speed = SPEED_25000;
  3137. break;
  3138. case VIRTCHNL_LINK_SPEED_40GB:
  3139. speed = SPEED_40000;
  3140. break;
  3141. default:
  3142. dev_err(&pf->pdev->dev,
  3143. "Cannot detect link speed\n");
  3144. aq_ret = I40E_ERR_PARAM;
  3145. goto err;
  3146. }
  3147. /* parse data from the queue channel info */
  3148. vf->num_tc = tci->num_tc;
  3149. for (i = 0; i < vf->num_tc; i++) {
  3150. if (tci->list[i].max_tx_rate) {
  3151. if (tci->list[i].max_tx_rate > speed) {
  3152. dev_err(&pf->pdev->dev,
  3153. "Invalid max tx rate %llu specified for VF %d.",
  3154. tci->list[i].max_tx_rate,
  3155. vf->vf_id);
  3156. aq_ret = I40E_ERR_PARAM;
  3157. goto err;
  3158. } else {
  3159. vf->ch[i].max_tx_rate =
  3160. tci->list[i].max_tx_rate;
  3161. }
  3162. }
  3163. vf->ch[i].num_qps = tci->list[i].count;
  3164. }
  3165. /* set this flag only after making sure all inputs are sane */
  3166. vf->adq_enabled = true;
  3167. /* num_req_queues is set when user changes number of queues via ethtool
  3168. * and this causes issue for default VSI(which depends on this variable)
  3169. * when ADq is enabled, hence reset it.
  3170. */
  3171. vf->num_req_queues = 0;
  3172. /* reset the VF in order to allocate resources */
  3173. i40e_vc_notify_vf_reset(vf);
  3174. i40e_reset_vf(vf, false);
  3175. return I40E_SUCCESS;
  3176. /* send the response to the VF */
  3177. err:
  3178. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_CHANNELS,
  3179. aq_ret);
  3180. }
  3181. /**
  3182. * i40e_vc_del_qch_msg
  3183. * @vf: pointer to the VF info
  3184. * @msg: pointer to the msg buffer
  3185. **/
  3186. static int i40e_vc_del_qch_msg(struct i40e_vf *vf, u8 *msg)
  3187. {
  3188. struct i40e_pf *pf = vf->pf;
  3189. i40e_status aq_ret = 0;
  3190. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  3191. aq_ret = I40E_ERR_PARAM;
  3192. goto err;
  3193. }
  3194. if (vf->adq_enabled) {
  3195. i40e_del_all_cloud_filters(vf);
  3196. i40e_del_qch(vf);
  3197. vf->adq_enabled = false;
  3198. vf->num_tc = 0;
  3199. dev_info(&pf->pdev->dev,
  3200. "Deleting Queue Channels and cloud filters for ADq on VF %d\n",
  3201. vf->vf_id);
  3202. } else {
  3203. dev_info(&pf->pdev->dev, "VF %d trying to delete queue channels but ADq isn't enabled\n",
  3204. vf->vf_id);
  3205. aq_ret = I40E_ERR_PARAM;
  3206. }
  3207. /* reset the VF in order to allocate resources */
  3208. i40e_vc_notify_vf_reset(vf);
  3209. i40e_reset_vf(vf, false);
  3210. return I40E_SUCCESS;
  3211. err:
  3212. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_CHANNELS,
  3213. aq_ret);
  3214. }
  3215. /**
  3216. * i40e_vc_process_vf_msg
  3217. * @pf: pointer to the PF structure
  3218. * @vf_id: source VF id
  3219. * @v_opcode: operation code
  3220. * @v_retval: unused return value code
  3221. * @msg: pointer to the msg buffer
  3222. * @msglen: msg length
  3223. *
  3224. * called from the common aeq/arq handler to
  3225. * process request from VF
  3226. **/
  3227. int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
  3228. u32 __always_unused v_retval, u8 *msg, u16 msglen)
  3229. {
  3230. struct i40e_hw *hw = &pf->hw;
  3231. int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
  3232. struct i40e_vf *vf;
  3233. int ret;
  3234. pf->vf_aq_requests++;
  3235. if (local_vf_id >= pf->num_alloc_vfs)
  3236. return -EINVAL;
  3237. vf = &(pf->vf[local_vf_id]);
  3238. /* Check if VF is disabled. */
  3239. if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
  3240. return I40E_ERR_PARAM;
  3241. /* perform basic checks on the msg */
  3242. ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
  3243. /* perform additional checks specific to this driver */
  3244. if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
  3245. struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
  3246. if (vrk->key_len != I40E_HKEY_ARRAY_SIZE)
  3247. ret = -EINVAL;
  3248. } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
  3249. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  3250. if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)
  3251. ret = -EINVAL;
  3252. }
  3253. if (ret) {
  3254. i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
  3255. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
  3256. local_vf_id, v_opcode, msglen);
  3257. switch (ret) {
  3258. case VIRTCHNL_ERR_PARAM:
  3259. return -EPERM;
  3260. default:
  3261. return -EINVAL;
  3262. }
  3263. }
  3264. switch (v_opcode) {
  3265. case VIRTCHNL_OP_VERSION:
  3266. ret = i40e_vc_get_version_msg(vf, msg);
  3267. break;
  3268. case VIRTCHNL_OP_GET_VF_RESOURCES:
  3269. ret = i40e_vc_get_vf_resources_msg(vf, msg);
  3270. i40e_vc_notify_vf_link_state(vf);
  3271. break;
  3272. case VIRTCHNL_OP_RESET_VF:
  3273. i40e_vc_reset_vf_msg(vf);
  3274. ret = 0;
  3275. break;
  3276. case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  3277. ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
  3278. break;
  3279. case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  3280. ret = i40e_vc_config_queues_msg(vf, msg, msglen);
  3281. break;
  3282. case VIRTCHNL_OP_CONFIG_IRQ_MAP:
  3283. ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
  3284. break;
  3285. case VIRTCHNL_OP_ENABLE_QUEUES:
  3286. ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
  3287. i40e_vc_notify_vf_link_state(vf);
  3288. break;
  3289. case VIRTCHNL_OP_DISABLE_QUEUES:
  3290. ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
  3291. break;
  3292. case VIRTCHNL_OP_ADD_ETH_ADDR:
  3293. ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
  3294. break;
  3295. case VIRTCHNL_OP_DEL_ETH_ADDR:
  3296. ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
  3297. break;
  3298. case VIRTCHNL_OP_ADD_VLAN:
  3299. ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
  3300. break;
  3301. case VIRTCHNL_OP_DEL_VLAN:
  3302. ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
  3303. break;
  3304. case VIRTCHNL_OP_GET_STATS:
  3305. ret = i40e_vc_get_stats_msg(vf, msg, msglen);
  3306. break;
  3307. case VIRTCHNL_OP_IWARP:
  3308. ret = i40e_vc_iwarp_msg(vf, msg, msglen);
  3309. break;
  3310. case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
  3311. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
  3312. break;
  3313. case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
  3314. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
  3315. break;
  3316. case VIRTCHNL_OP_CONFIG_RSS_KEY:
  3317. ret = i40e_vc_config_rss_key(vf, msg, msglen);
  3318. break;
  3319. case VIRTCHNL_OP_CONFIG_RSS_LUT:
  3320. ret = i40e_vc_config_rss_lut(vf, msg, msglen);
  3321. break;
  3322. case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
  3323. ret = i40e_vc_get_rss_hena(vf, msg, msglen);
  3324. break;
  3325. case VIRTCHNL_OP_SET_RSS_HENA:
  3326. ret = i40e_vc_set_rss_hena(vf, msg, msglen);
  3327. break;
  3328. case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
  3329. ret = i40e_vc_enable_vlan_stripping(vf, msg, msglen);
  3330. break;
  3331. case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
  3332. ret = i40e_vc_disable_vlan_stripping(vf, msg, msglen);
  3333. break;
  3334. case VIRTCHNL_OP_REQUEST_QUEUES:
  3335. ret = i40e_vc_request_queues_msg(vf, msg, msglen);
  3336. break;
  3337. case VIRTCHNL_OP_ENABLE_CHANNELS:
  3338. ret = i40e_vc_add_qch_msg(vf, msg);
  3339. break;
  3340. case VIRTCHNL_OP_DISABLE_CHANNELS:
  3341. ret = i40e_vc_del_qch_msg(vf, msg);
  3342. break;
  3343. case VIRTCHNL_OP_ADD_CLOUD_FILTER:
  3344. ret = i40e_vc_add_cloud_filter(vf, msg);
  3345. break;
  3346. case VIRTCHNL_OP_DEL_CLOUD_FILTER:
  3347. ret = i40e_vc_del_cloud_filter(vf, msg);
  3348. break;
  3349. case VIRTCHNL_OP_UNKNOWN:
  3350. default:
  3351. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  3352. v_opcode, local_vf_id);
  3353. ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
  3354. I40E_ERR_NOT_IMPLEMENTED);
  3355. break;
  3356. }
  3357. return ret;
  3358. }
  3359. /**
  3360. * i40e_vc_process_vflr_event
  3361. * @pf: pointer to the PF structure
  3362. *
  3363. * called from the vlfr irq handler to
  3364. * free up VF resources and state variables
  3365. **/
  3366. int i40e_vc_process_vflr_event(struct i40e_pf *pf)
  3367. {
  3368. struct i40e_hw *hw = &pf->hw;
  3369. u32 reg, reg_idx, bit_idx;
  3370. struct i40e_vf *vf;
  3371. int vf_id;
  3372. if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  3373. return 0;
  3374. /* Re-enable the VFLR interrupt cause here, before looking for which
  3375. * VF got reset. Otherwise, if another VF gets a reset while the
  3376. * first one is being processed, that interrupt will be lost, and
  3377. * that VF will be stuck in reset forever.
  3378. */
  3379. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  3380. reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
  3381. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  3382. i40e_flush(hw);
  3383. clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  3384. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  3385. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  3386. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  3387. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  3388. vf = &pf->vf[vf_id];
  3389. reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
  3390. if (reg & BIT(bit_idx))
  3391. /* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
  3392. i40e_reset_vf(vf, true);
  3393. }
  3394. return 0;
  3395. }
  3396. /**
  3397. * i40e_ndo_set_vf_mac
  3398. * @netdev: network interface device structure
  3399. * @vf_id: VF identifier
  3400. * @mac: mac address
  3401. *
  3402. * program VF mac address
  3403. **/
  3404. int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  3405. {
  3406. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3407. struct i40e_vsi *vsi = np->vsi;
  3408. struct i40e_pf *pf = vsi->back;
  3409. struct i40e_mac_filter *f;
  3410. struct i40e_vf *vf;
  3411. int ret = 0;
  3412. struct hlist_node *h;
  3413. int bkt;
  3414. u8 i;
  3415. /* validate the request */
  3416. if (vf_id >= pf->num_alloc_vfs) {
  3417. dev_err(&pf->pdev->dev,
  3418. "Invalid VF Identifier %d\n", vf_id);
  3419. ret = -EINVAL;
  3420. goto error_param;
  3421. }
  3422. vf = &(pf->vf[vf_id]);
  3423. vsi = pf->vsi[vf->lan_vsi_idx];
  3424. /* When the VF is resetting wait until it is done.
  3425. * It can take up to 200 milliseconds,
  3426. * but wait for up to 300 milliseconds to be safe.
  3427. */
  3428. for (i = 0; i < 15; i++) {
  3429. if (test_bit(I40E_VF_STATE_INIT, &vf->vf_states))
  3430. break;
  3431. msleep(20);
  3432. }
  3433. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3434. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3435. vf_id);
  3436. ret = -EAGAIN;
  3437. goto error_param;
  3438. }
  3439. if (is_multicast_ether_addr(mac)) {
  3440. dev_err(&pf->pdev->dev,
  3441. "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
  3442. ret = -EINVAL;
  3443. goto error_param;
  3444. }
  3445. /* Lock once because below invoked function add/del_filter requires
  3446. * mac_filter_hash_lock to be held
  3447. */
  3448. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3449. /* delete the temporary mac address */
  3450. if (!is_zero_ether_addr(vf->default_lan_addr.addr))
  3451. i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
  3452. /* Delete all the filters for this VSI - we're going to kill it
  3453. * anyway.
  3454. */
  3455. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  3456. __i40e_del_filter(vsi, f);
  3457. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3458. /* program mac filter */
  3459. if (i40e_sync_vsi_filters(vsi)) {
  3460. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  3461. ret = -EIO;
  3462. goto error_param;
  3463. }
  3464. ether_addr_copy(vf->default_lan_addr.addr, mac);
  3465. if (is_zero_ether_addr(mac)) {
  3466. vf->pf_set_mac = false;
  3467. dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
  3468. } else {
  3469. vf->pf_set_mac = true;
  3470. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
  3471. mac, vf_id);
  3472. }
  3473. /* Force the VF driver stop so it has to reload with new MAC address */
  3474. i40e_vc_disable_vf(vf);
  3475. dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
  3476. error_param:
  3477. return ret;
  3478. }
  3479. /**
  3480. * i40e_vsi_has_vlans - True if VSI has configured VLANs
  3481. * @vsi: pointer to the vsi
  3482. *
  3483. * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
  3484. * we have no configured VLANs. Do not call while holding the
  3485. * mac_filter_hash_lock.
  3486. */
  3487. static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
  3488. {
  3489. bool have_vlans;
  3490. /* If we have a port VLAN, then the VSI cannot have any VLANs
  3491. * configured, as all MAC/VLAN filters will be assigned to the PVID.
  3492. */
  3493. if (vsi->info.pvid)
  3494. return false;
  3495. /* Since we don't have a PVID, we know that if the device is in VLAN
  3496. * mode it must be because of a VLAN filter configured on this VSI.
  3497. */
  3498. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3499. have_vlans = i40e_is_vsi_in_vlan(vsi);
  3500. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3501. return have_vlans;
  3502. }
  3503. /**
  3504. * i40e_ndo_set_vf_port_vlan
  3505. * @netdev: network interface device structure
  3506. * @vf_id: VF identifier
  3507. * @vlan_id: mac address
  3508. * @qos: priority setting
  3509. * @vlan_proto: vlan protocol
  3510. *
  3511. * program VF vlan id and/or qos
  3512. **/
  3513. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
  3514. u16 vlan_id, u8 qos, __be16 vlan_proto)
  3515. {
  3516. u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
  3517. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3518. struct i40e_pf *pf = np->vsi->back;
  3519. struct i40e_vsi *vsi;
  3520. struct i40e_vf *vf;
  3521. int ret = 0;
  3522. /* validate the request */
  3523. if (vf_id >= pf->num_alloc_vfs) {
  3524. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3525. ret = -EINVAL;
  3526. goto error_pvid;
  3527. }
  3528. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  3529. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  3530. ret = -EINVAL;
  3531. goto error_pvid;
  3532. }
  3533. if (vlan_proto != htons(ETH_P_8021Q)) {
  3534. dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
  3535. ret = -EPROTONOSUPPORT;
  3536. goto error_pvid;
  3537. }
  3538. vf = &(pf->vf[vf_id]);
  3539. vsi = pf->vsi[vf->lan_vsi_idx];
  3540. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3541. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3542. vf_id);
  3543. ret = -EAGAIN;
  3544. goto error_pvid;
  3545. }
  3546. if (le16_to_cpu(vsi->info.pvid) == vlanprio)
  3547. /* duplicate request, so just return success */
  3548. goto error_pvid;
  3549. if (i40e_vsi_has_vlans(vsi)) {
  3550. dev_err(&pf->pdev->dev,
  3551. "VF %d has already configured VLAN filters and the administrator is requesting a port VLAN override.\nPlease unload and reload the VF driver for this change to take effect.\n",
  3552. vf_id);
  3553. /* Administrator Error - knock the VF offline until he does
  3554. * the right thing by reconfiguring his network correctly
  3555. * and then reloading the VF driver.
  3556. */
  3557. i40e_vc_disable_vf(vf);
  3558. /* During reset the VF got a new VSI, so refresh the pointer. */
  3559. vsi = pf->vsi[vf->lan_vsi_idx];
  3560. }
  3561. /* Locked once because multiple functions below iterate list */
  3562. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3563. /* Check for condition where there was already a port VLAN ID
  3564. * filter set and now it is being deleted by setting it to zero.
  3565. * Additionally check for the condition where there was a port
  3566. * VLAN but now there is a new and different port VLAN being set.
  3567. * Before deleting all the old VLAN filters we must add new ones
  3568. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  3569. * MAC addresses deleted.
  3570. */
  3571. if ((!(vlan_id || qos) ||
  3572. vlanprio != le16_to_cpu(vsi->info.pvid)) &&
  3573. vsi->info.pvid) {
  3574. ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
  3575. if (ret) {
  3576. dev_info(&vsi->back->pdev->dev,
  3577. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  3578. vsi->back->hw.aq.asq_last_status);
  3579. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3580. goto error_pvid;
  3581. }
  3582. }
  3583. if (vsi->info.pvid) {
  3584. /* remove all filters on the old VLAN */
  3585. i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
  3586. VLAN_VID_MASK));
  3587. }
  3588. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3589. if (vlan_id || qos)
  3590. ret = i40e_vsi_add_pvid(vsi, vlanprio);
  3591. else
  3592. i40e_vsi_remove_pvid(vsi);
  3593. spin_lock_bh(&vsi->mac_filter_hash_lock);
  3594. if (vlan_id) {
  3595. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  3596. vlan_id, qos, vf_id);
  3597. /* add new VLAN filter for each MAC */
  3598. ret = i40e_add_vlan_all_mac(vsi, vlan_id);
  3599. if (ret) {
  3600. dev_info(&vsi->back->pdev->dev,
  3601. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  3602. vsi->back->hw.aq.asq_last_status);
  3603. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3604. goto error_pvid;
  3605. }
  3606. /* remove the previously added non-VLAN MAC filters */
  3607. i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
  3608. }
  3609. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  3610. /* Schedule the worker thread to take care of applying changes */
  3611. i40e_service_event_schedule(vsi->back);
  3612. if (ret) {
  3613. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  3614. goto error_pvid;
  3615. }
  3616. /* The Port VLAN needs to be saved across resets the same as the
  3617. * default LAN MAC address.
  3618. */
  3619. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  3620. ret = 0;
  3621. error_pvid:
  3622. return ret;
  3623. }
  3624. /**
  3625. * i40e_ndo_set_vf_bw
  3626. * @netdev: network interface device structure
  3627. * @vf_id: VF identifier
  3628. * @min_tx_rate: Minimum Tx rate
  3629. * @max_tx_rate: Maximum Tx rate
  3630. *
  3631. * configure VF Tx rate
  3632. **/
  3633. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  3634. int max_tx_rate)
  3635. {
  3636. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3637. struct i40e_pf *pf = np->vsi->back;
  3638. struct i40e_vsi *vsi;
  3639. struct i40e_vf *vf;
  3640. int ret = 0;
  3641. /* validate the request */
  3642. if (vf_id >= pf->num_alloc_vfs) {
  3643. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
  3644. ret = -EINVAL;
  3645. goto error;
  3646. }
  3647. if (min_tx_rate) {
  3648. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
  3649. min_tx_rate, vf_id);
  3650. return -EINVAL;
  3651. }
  3652. vf = &(pf->vf[vf_id]);
  3653. vsi = pf->vsi[vf->lan_vsi_idx];
  3654. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3655. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3656. vf_id);
  3657. ret = -EAGAIN;
  3658. goto error;
  3659. }
  3660. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  3661. if (ret)
  3662. goto error;
  3663. vf->tx_rate = max_tx_rate;
  3664. error:
  3665. return ret;
  3666. }
  3667. /**
  3668. * i40e_ndo_get_vf_config
  3669. * @netdev: network interface device structure
  3670. * @vf_id: VF identifier
  3671. * @ivi: VF configuration structure
  3672. *
  3673. * return VF configuration
  3674. **/
  3675. int i40e_ndo_get_vf_config(struct net_device *netdev,
  3676. int vf_id, struct ifla_vf_info *ivi)
  3677. {
  3678. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3679. struct i40e_vsi *vsi = np->vsi;
  3680. struct i40e_pf *pf = vsi->back;
  3681. struct i40e_vf *vf;
  3682. int ret = 0;
  3683. /* validate the request */
  3684. if (vf_id >= pf->num_alloc_vfs) {
  3685. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3686. ret = -EINVAL;
  3687. goto error_param;
  3688. }
  3689. vf = &(pf->vf[vf_id]);
  3690. /* first vsi is always the LAN vsi */
  3691. vsi = pf->vsi[vf->lan_vsi_idx];
  3692. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3693. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3694. vf_id);
  3695. ret = -EAGAIN;
  3696. goto error_param;
  3697. }
  3698. ivi->vf = vf_id;
  3699. ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
  3700. ivi->max_tx_rate = vf->tx_rate;
  3701. ivi->min_tx_rate = 0;
  3702. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  3703. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  3704. I40E_VLAN_PRIORITY_SHIFT;
  3705. if (vf->link_forced == false)
  3706. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  3707. else if (vf->link_up == true)
  3708. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  3709. else
  3710. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  3711. ivi->spoofchk = vf->spoofchk;
  3712. ivi->trusted = vf->trusted;
  3713. ret = 0;
  3714. error_param:
  3715. return ret;
  3716. }
  3717. /**
  3718. * i40e_ndo_set_vf_link_state
  3719. * @netdev: network interface device structure
  3720. * @vf_id: VF identifier
  3721. * @link: required link state
  3722. *
  3723. * Set the link state of a specified VF, regardless of physical link state
  3724. **/
  3725. int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
  3726. {
  3727. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3728. struct i40e_pf *pf = np->vsi->back;
  3729. struct virtchnl_pf_event pfe;
  3730. struct i40e_hw *hw = &pf->hw;
  3731. struct i40e_vf *vf;
  3732. int abs_vf_id;
  3733. int ret = 0;
  3734. /* validate the request */
  3735. if (vf_id >= pf->num_alloc_vfs) {
  3736. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3737. ret = -EINVAL;
  3738. goto error_out;
  3739. }
  3740. vf = &pf->vf[vf_id];
  3741. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  3742. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  3743. pfe.severity = PF_EVENT_SEVERITY_INFO;
  3744. switch (link) {
  3745. case IFLA_VF_LINK_STATE_AUTO:
  3746. vf->link_forced = false;
  3747. pfe.event_data.link_event.link_status =
  3748. pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
  3749. pfe.event_data.link_event.link_speed =
  3750. (enum virtchnl_link_speed)
  3751. pf->hw.phy.link_info.link_speed;
  3752. break;
  3753. case IFLA_VF_LINK_STATE_ENABLE:
  3754. vf->link_forced = true;
  3755. vf->link_up = true;
  3756. pfe.event_data.link_event.link_status = true;
  3757. pfe.event_data.link_event.link_speed = VIRTCHNL_LINK_SPEED_40GB;
  3758. break;
  3759. case IFLA_VF_LINK_STATE_DISABLE:
  3760. vf->link_forced = true;
  3761. vf->link_up = false;
  3762. pfe.event_data.link_event.link_status = false;
  3763. pfe.event_data.link_event.link_speed = 0;
  3764. break;
  3765. default:
  3766. ret = -EINVAL;
  3767. goto error_out;
  3768. }
  3769. /* Notify the VF of its new link state */
  3770. i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  3771. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  3772. error_out:
  3773. return ret;
  3774. }
  3775. /**
  3776. * i40e_ndo_set_vf_spoofchk
  3777. * @netdev: network interface device structure
  3778. * @vf_id: VF identifier
  3779. * @enable: flag to enable or disable feature
  3780. *
  3781. * Enable or disable VF spoof checking
  3782. **/
  3783. int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
  3784. {
  3785. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3786. struct i40e_vsi *vsi = np->vsi;
  3787. struct i40e_pf *pf = vsi->back;
  3788. struct i40e_vsi_context ctxt;
  3789. struct i40e_hw *hw = &pf->hw;
  3790. struct i40e_vf *vf;
  3791. int ret = 0;
  3792. /* validate the request */
  3793. if (vf_id >= pf->num_alloc_vfs) {
  3794. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3795. ret = -EINVAL;
  3796. goto out;
  3797. }
  3798. vf = &(pf->vf[vf_id]);
  3799. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  3800. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  3801. vf_id);
  3802. ret = -EAGAIN;
  3803. goto out;
  3804. }
  3805. if (enable == vf->spoofchk)
  3806. goto out;
  3807. vf->spoofchk = enable;
  3808. memset(&ctxt, 0, sizeof(ctxt));
  3809. ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
  3810. ctxt.pf_num = pf->hw.pf_id;
  3811. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  3812. if (enable)
  3813. ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  3814. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  3815. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  3816. if (ret) {
  3817. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  3818. ret);
  3819. ret = -EIO;
  3820. }
  3821. out:
  3822. return ret;
  3823. }
  3824. /**
  3825. * i40e_ndo_set_vf_trust
  3826. * @netdev: network interface device structure of the pf
  3827. * @vf_id: VF identifier
  3828. * @setting: trust setting
  3829. *
  3830. * Enable or disable VF trust setting
  3831. **/
  3832. int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
  3833. {
  3834. struct i40e_netdev_priv *np = netdev_priv(netdev);
  3835. struct i40e_pf *pf = np->vsi->back;
  3836. struct i40e_vf *vf;
  3837. int ret = 0;
  3838. /* validate the request */
  3839. if (vf_id >= pf->num_alloc_vfs) {
  3840. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  3841. return -EINVAL;
  3842. }
  3843. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  3844. dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
  3845. return -EINVAL;
  3846. }
  3847. vf = &pf->vf[vf_id];
  3848. if (setting == vf->trusted)
  3849. goto out;
  3850. vf->trusted = setting;
  3851. i40e_vc_disable_vf(vf);
  3852. dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
  3853. vf_id, setting ? "" : "un");
  3854. if (vf->adq_enabled) {
  3855. if (!vf->trusted) {
  3856. dev_info(&pf->pdev->dev,
  3857. "VF %u no longer Trusted, deleting all cloud filters\n",
  3858. vf_id);
  3859. i40e_del_all_cloud_filters(vf);
  3860. }
  3861. }
  3862. out:
  3863. return ret;
  3864. }