i40e_virtchnl_pf.c 114 KB

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