i40e_virtchnl_pf.c 90 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include "i40e.h"
  27. /*********************notification routines***********************/
  28. /**
  29. * i40e_vc_vf_broadcast
  30. * @pf: pointer to the PF structure
  31. * @opcode: operation code
  32. * @retval: return value
  33. * @msg: pointer to the msg buffer
  34. * @msglen: msg length
  35. *
  36. * send a message to all VFs on a given PF
  37. **/
  38. static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
  39. enum virtchnl_ops v_opcode,
  40. i40e_status v_retval, u8 *msg,
  41. u16 msglen)
  42. {
  43. struct i40e_hw *hw = &pf->hw;
  44. struct i40e_vf *vf = pf->vf;
  45. int i;
  46. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  47. int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
  48. /* Not all vfs are enabled so skip the ones that are not */
  49. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
  50. !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  51. continue;
  52. /* Ignore return value on purpose - a given VF may fail, but
  53. * we need to keep going and send to all of them
  54. */
  55. i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  56. msg, msglen, NULL);
  57. }
  58. }
  59. /**
  60. * i40e_vc_notify_vf_link_state
  61. * @vf: pointer to the VF structure
  62. *
  63. * send a link status message to a single VF
  64. **/
  65. static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
  66. {
  67. struct virtchnl_pf_event pfe;
  68. struct i40e_pf *pf = vf->pf;
  69. struct i40e_hw *hw = &pf->hw;
  70. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  71. int abs_vf_id = vf->vf_id + (int)hw->func_caps.vf_base_id;
  72. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  73. pfe.severity = PF_EVENT_SEVERITY_INFO;
  74. if (vf->link_forced) {
  75. pfe.event_data.link_event.link_status = vf->link_up;
  76. pfe.event_data.link_event.link_speed =
  77. (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
  78. } else {
  79. pfe.event_data.link_event.link_status =
  80. ls->link_info & I40E_AQ_LINK_UP;
  81. pfe.event_data.link_event.link_speed =
  82. (enum virtchnl_link_speed)ls->link_speed;
  83. }
  84. i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  85. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  86. }
  87. /**
  88. * i40e_vc_notify_link_state
  89. * @pf: pointer to the PF structure
  90. *
  91. * send a link status message to all VFs on a given PF
  92. **/
  93. void i40e_vc_notify_link_state(struct i40e_pf *pf)
  94. {
  95. int i;
  96. for (i = 0; i < pf->num_alloc_vfs; i++)
  97. i40e_vc_notify_vf_link_state(&pf->vf[i]);
  98. }
  99. /**
  100. * i40e_vc_notify_reset
  101. * @pf: pointer to the PF structure
  102. *
  103. * indicate a pending reset to all VFs on a given PF
  104. **/
  105. void i40e_vc_notify_reset(struct i40e_pf *pf)
  106. {
  107. struct virtchnl_pf_event pfe;
  108. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  109. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  110. i40e_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, 0,
  111. (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
  112. }
  113. /**
  114. * i40e_vc_notify_vf_reset
  115. * @vf: pointer to the VF structure
  116. *
  117. * indicate a pending reset to the given VF
  118. **/
  119. void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
  120. {
  121. struct virtchnl_pf_event pfe;
  122. int abs_vf_id;
  123. /* validate the request */
  124. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  125. return;
  126. /* verify if the VF is in either init or active before proceeding */
  127. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states) &&
  128. !test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  129. return;
  130. abs_vf_id = vf->vf_id + (int)vf->pf->hw.func_caps.vf_base_id;
  131. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  132. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  133. i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  134. 0, (u8 *)&pfe,
  135. sizeof(struct virtchnl_pf_event), NULL);
  136. }
  137. /***********************misc routines*****************************/
  138. /**
  139. * i40e_vc_disable_vf
  140. * @vf: pointer to the VF info
  141. *
  142. * Disable the VF through a SW reset.
  143. **/
  144. static inline void i40e_vc_disable_vf(struct i40e_vf *vf)
  145. {
  146. int i;
  147. i40e_vc_notify_vf_reset(vf);
  148. /* We want to ensure that an actual reset occurs initiated after this
  149. * function was called. However, we do not want to wait forever, so
  150. * we'll give a reasonable time and print a message if we failed to
  151. * ensure a reset.
  152. */
  153. for (i = 0; i < 20; i++) {
  154. if (i40e_reset_vf(vf, false))
  155. return;
  156. usleep_range(10000, 20000);
  157. }
  158. dev_warn(&vf->pf->pdev->dev,
  159. "Failed to initiate reset for VF %d after 200 milliseconds\n",
  160. vf->vf_id);
  161. }
  162. /**
  163. * i40e_vc_isvalid_vsi_id
  164. * @vf: pointer to the VF info
  165. * @vsi_id: VF relative VSI id
  166. *
  167. * check for the valid VSI id
  168. **/
  169. static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
  170. {
  171. struct i40e_pf *pf = vf->pf;
  172. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  173. return (vsi && (vsi->vf_id == vf->vf_id));
  174. }
  175. /**
  176. * i40e_vc_isvalid_queue_id
  177. * @vf: pointer to the VF info
  178. * @vsi_id: vsi id
  179. * @qid: vsi relative queue id
  180. *
  181. * check for the valid queue id
  182. **/
  183. static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
  184. u8 qid)
  185. {
  186. struct i40e_pf *pf = vf->pf;
  187. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  188. return (vsi && (qid < vsi->alloc_queue_pairs));
  189. }
  190. /**
  191. * i40e_vc_isvalid_vector_id
  192. * @vf: pointer to the VF info
  193. * @vector_id: VF relative vector id
  194. *
  195. * check for the valid vector id
  196. **/
  197. static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
  198. {
  199. struct i40e_pf *pf = vf->pf;
  200. return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
  201. }
  202. /***********************vf resource mgmt routines*****************/
  203. /**
  204. * i40e_vc_get_pf_queue_id
  205. * @vf: pointer to the VF info
  206. * @vsi_id: id of VSI as provided by the FW
  207. * @vsi_queue_id: vsi relative queue id
  208. *
  209. * return PF relative queue id
  210. **/
  211. static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
  212. u8 vsi_queue_id)
  213. {
  214. struct i40e_pf *pf = vf->pf;
  215. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  216. u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
  217. if (!vsi)
  218. return pf_queue_id;
  219. if (le16_to_cpu(vsi->info.mapping_flags) &
  220. I40E_AQ_VSI_QUE_MAP_NONCONTIG)
  221. pf_queue_id =
  222. le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
  223. else
  224. pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
  225. vsi_queue_id;
  226. return pf_queue_id;
  227. }
  228. /**
  229. * i40e_config_irq_link_list
  230. * @vf: pointer to the VF info
  231. * @vsi_id: id of VSI as given by the FW
  232. * @vecmap: irq map info
  233. *
  234. * configure irq link list from the map
  235. **/
  236. static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
  237. struct virtchnl_vector_map *vecmap)
  238. {
  239. unsigned long linklistmap = 0, tempmap;
  240. struct i40e_pf *pf = vf->pf;
  241. struct i40e_hw *hw = &pf->hw;
  242. u16 vsi_queue_id, pf_queue_id;
  243. enum i40e_queue_type qtype;
  244. u16 next_q, vector_id, size;
  245. u32 reg, reg_idx;
  246. u16 itr_idx = 0;
  247. vector_id = vecmap->vector_id;
  248. /* setup the head */
  249. if (0 == vector_id)
  250. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  251. else
  252. reg_idx = I40E_VPINT_LNKLSTN(
  253. ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
  254. (vector_id - 1));
  255. if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
  256. /* Special case - No queues mapped on this vector */
  257. wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
  258. goto irq_list_done;
  259. }
  260. tempmap = vecmap->rxq_map;
  261. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  262. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  263. vsi_queue_id));
  264. }
  265. tempmap = vecmap->txq_map;
  266. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  267. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  268. vsi_queue_id + 1));
  269. }
  270. size = I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES;
  271. next_q = find_first_bit(&linklistmap, size);
  272. if (unlikely(next_q == size))
  273. goto irq_list_done;
  274. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  275. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  276. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  277. reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
  278. wr32(hw, reg_idx, reg);
  279. while (next_q < size) {
  280. switch (qtype) {
  281. case I40E_QUEUE_TYPE_RX:
  282. reg_idx = I40E_QINT_RQCTL(pf_queue_id);
  283. itr_idx = vecmap->rxitr_idx;
  284. break;
  285. case I40E_QUEUE_TYPE_TX:
  286. reg_idx = I40E_QINT_TQCTL(pf_queue_id);
  287. itr_idx = vecmap->txitr_idx;
  288. break;
  289. default:
  290. break;
  291. }
  292. next_q = find_next_bit(&linklistmap, size, next_q + 1);
  293. if (next_q < size) {
  294. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  295. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  296. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
  297. vsi_queue_id);
  298. } else {
  299. pf_queue_id = I40E_QUEUE_END_OF_LIST;
  300. qtype = 0;
  301. }
  302. /* format for the RQCTL & TQCTL regs is same */
  303. reg = (vector_id) |
  304. (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
  305. (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  306. BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
  307. (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
  308. wr32(hw, reg_idx, reg);
  309. }
  310. /* if the vf is running in polling mode and using interrupt zero,
  311. * need to disable auto-mask on enabling zero interrupt for VFs.
  312. */
  313. if ((vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
  314. (vector_id == 0)) {
  315. reg = rd32(hw, I40E_GLINT_CTL);
  316. if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
  317. reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
  318. wr32(hw, I40E_GLINT_CTL, reg);
  319. }
  320. }
  321. irq_list_done:
  322. i40e_flush(hw);
  323. }
  324. /**
  325. * i40e_release_iwarp_qvlist
  326. * @vf: pointer to the VF.
  327. *
  328. **/
  329. static void i40e_release_iwarp_qvlist(struct i40e_vf *vf)
  330. {
  331. struct i40e_pf *pf = vf->pf;
  332. struct virtchnl_iwarp_qvlist_info *qvlist_info = vf->qvlist_info;
  333. u32 msix_vf;
  334. u32 i;
  335. if (!vf->qvlist_info)
  336. return;
  337. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  338. for (i = 0; i < qvlist_info->num_vectors; i++) {
  339. struct virtchnl_iwarp_qv_info *qv_info;
  340. u32 next_q_index, next_q_type;
  341. struct i40e_hw *hw = &pf->hw;
  342. u32 v_idx, reg_idx, reg;
  343. qv_info = &qvlist_info->qv_info[i];
  344. if (!qv_info)
  345. continue;
  346. v_idx = qv_info->v_idx;
  347. if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
  348. /* Figure out the queue after CEQ and make that the
  349. * first queue.
  350. */
  351. reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
  352. reg = rd32(hw, I40E_VPINT_CEQCTL(reg_idx));
  353. next_q_index = (reg & I40E_VPINT_CEQCTL_NEXTQ_INDX_MASK)
  354. >> I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT;
  355. next_q_type = (reg & I40E_VPINT_CEQCTL_NEXTQ_TYPE_MASK)
  356. >> I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT;
  357. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  358. reg = (next_q_index &
  359. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  360. (next_q_type <<
  361. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  362. wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
  363. }
  364. }
  365. kfree(vf->qvlist_info);
  366. vf->qvlist_info = NULL;
  367. }
  368. /**
  369. * i40e_config_iwarp_qvlist
  370. * @vf: pointer to the VF info
  371. * @qvlist_info: queue and vector list
  372. *
  373. * Return 0 on success or < 0 on error
  374. **/
  375. static int i40e_config_iwarp_qvlist(struct i40e_vf *vf,
  376. struct virtchnl_iwarp_qvlist_info *qvlist_info)
  377. {
  378. struct i40e_pf *pf = vf->pf;
  379. struct i40e_hw *hw = &pf->hw;
  380. struct virtchnl_iwarp_qv_info *qv_info;
  381. u32 v_idx, i, reg_idx, reg;
  382. u32 next_q_idx, next_q_type;
  383. u32 msix_vf, size;
  384. size = sizeof(struct virtchnl_iwarp_qvlist_info) +
  385. (sizeof(struct virtchnl_iwarp_qv_info) *
  386. (qvlist_info->num_vectors - 1));
  387. vf->qvlist_info = kzalloc(size, GFP_KERNEL);
  388. if (!vf->qvlist_info)
  389. return -ENOMEM;
  390. vf->qvlist_info->num_vectors = qvlist_info->num_vectors;
  391. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  392. for (i = 0; i < qvlist_info->num_vectors; i++) {
  393. qv_info = &qvlist_info->qv_info[i];
  394. if (!qv_info)
  395. continue;
  396. v_idx = qv_info->v_idx;
  397. /* Validate vector id belongs to this vf */
  398. if (!i40e_vc_isvalid_vector_id(vf, v_idx))
  399. goto err;
  400. vf->qvlist_info->qv_info[i] = *qv_info;
  401. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  402. /* We might be sharing the interrupt, so get the first queue
  403. * index and type, push it down the list by adding the new
  404. * queue on top. Also link it with the new queue in CEQCTL.
  405. */
  406. reg = rd32(hw, I40E_VPINT_LNKLSTN(reg_idx));
  407. next_q_idx = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) >>
  408. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_SHIFT);
  409. next_q_type = ((reg & I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK) >>
  410. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  411. if (qv_info->ceq_idx != I40E_QUEUE_INVALID_IDX) {
  412. reg_idx = (msix_vf - 1) * vf->vf_id + qv_info->ceq_idx;
  413. reg = (I40E_VPINT_CEQCTL_CAUSE_ENA_MASK |
  414. (v_idx << I40E_VPINT_CEQCTL_MSIX_INDX_SHIFT) |
  415. (qv_info->itr_idx << I40E_VPINT_CEQCTL_ITR_INDX_SHIFT) |
  416. (next_q_type << I40E_VPINT_CEQCTL_NEXTQ_TYPE_SHIFT) |
  417. (next_q_idx << I40E_VPINT_CEQCTL_NEXTQ_INDX_SHIFT));
  418. wr32(hw, I40E_VPINT_CEQCTL(reg_idx), reg);
  419. reg_idx = ((msix_vf - 1) * vf->vf_id) + (v_idx - 1);
  420. reg = (qv_info->ceq_idx &
  421. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK) |
  422. (I40E_QUEUE_TYPE_PE_CEQ <<
  423. I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT);
  424. wr32(hw, I40E_VPINT_LNKLSTN(reg_idx), reg);
  425. }
  426. if (qv_info->aeq_idx != I40E_QUEUE_INVALID_IDX) {
  427. reg = (I40E_VPINT_AEQCTL_CAUSE_ENA_MASK |
  428. (v_idx << I40E_VPINT_AEQCTL_MSIX_INDX_SHIFT) |
  429. (qv_info->itr_idx << I40E_VPINT_AEQCTL_ITR_INDX_SHIFT));
  430. wr32(hw, I40E_VPINT_AEQCTL(vf->vf_id), reg);
  431. }
  432. }
  433. return 0;
  434. err:
  435. kfree(vf->qvlist_info);
  436. vf->qvlist_info = NULL;
  437. return -EINVAL;
  438. }
  439. /**
  440. * i40e_config_vsi_tx_queue
  441. * @vf: pointer to the VF info
  442. * @vsi_id: id of VSI as provided by the FW
  443. * @vsi_queue_id: vsi relative queue index
  444. * @info: config. info
  445. *
  446. * configure tx queue
  447. **/
  448. static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
  449. u16 vsi_queue_id,
  450. struct virtchnl_txq_info *info)
  451. {
  452. struct i40e_pf *pf = vf->pf;
  453. struct i40e_hw *hw = &pf->hw;
  454. struct i40e_hmc_obj_txq tx_ctx;
  455. struct i40e_vsi *vsi;
  456. u16 pf_queue_id;
  457. u32 qtx_ctl;
  458. int ret = 0;
  459. if (!i40e_vc_isvalid_vsi_id(vf, info->vsi_id)) {
  460. ret = -ENOENT;
  461. goto error_context;
  462. }
  463. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  464. vsi = i40e_find_vsi_from_id(pf, vsi_id);
  465. if (!vsi) {
  466. ret = -ENOENT;
  467. goto error_context;
  468. }
  469. /* clear the context structure first */
  470. memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
  471. /* only set the required fields */
  472. tx_ctx.base = info->dma_ring_addr / 128;
  473. tx_ctx.qlen = info->ring_len;
  474. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
  475. tx_ctx.rdylist_act = 0;
  476. tx_ctx.head_wb_ena = info->headwb_enabled;
  477. tx_ctx.head_wb_addr = info->dma_headwb_addr;
  478. /* clear the context in the HMC */
  479. ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
  480. if (ret) {
  481. dev_err(&pf->pdev->dev,
  482. "Failed to clear VF LAN Tx queue context %d, error: %d\n",
  483. pf_queue_id, ret);
  484. ret = -ENOENT;
  485. goto error_context;
  486. }
  487. /* set the context in the HMC */
  488. ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
  489. if (ret) {
  490. dev_err(&pf->pdev->dev,
  491. "Failed to set VF LAN Tx queue context %d error: %d\n",
  492. pf_queue_id, ret);
  493. ret = -ENOENT;
  494. goto error_context;
  495. }
  496. /* associate this queue with the PCI VF function */
  497. qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
  498. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
  499. & I40E_QTX_CTL_PF_INDX_MASK);
  500. qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
  501. << I40E_QTX_CTL_VFVM_INDX_SHIFT)
  502. & I40E_QTX_CTL_VFVM_INDX_MASK);
  503. wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
  504. i40e_flush(hw);
  505. error_context:
  506. return ret;
  507. }
  508. /**
  509. * i40e_config_vsi_rx_queue
  510. * @vf: pointer to the VF info
  511. * @vsi_id: id of VSI as provided by the FW
  512. * @vsi_queue_id: vsi relative queue index
  513. * @info: config. info
  514. *
  515. * configure rx queue
  516. **/
  517. static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
  518. u16 vsi_queue_id,
  519. struct virtchnl_rxq_info *info)
  520. {
  521. struct i40e_pf *pf = vf->pf;
  522. struct i40e_hw *hw = &pf->hw;
  523. struct i40e_hmc_obj_rxq rx_ctx;
  524. u16 pf_queue_id;
  525. int ret = 0;
  526. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  527. /* clear the context structure first */
  528. memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
  529. /* only set the required fields */
  530. rx_ctx.base = info->dma_ring_addr / 128;
  531. rx_ctx.qlen = info->ring_len;
  532. if (info->splithdr_enabled) {
  533. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  534. I40E_RX_SPLIT_IP |
  535. I40E_RX_SPLIT_TCP_UDP |
  536. I40E_RX_SPLIT_SCTP;
  537. /* header length validation */
  538. if (info->hdr_size > ((2 * 1024) - 64)) {
  539. ret = -EINVAL;
  540. goto error_param;
  541. }
  542. rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
  543. /* set split mode 10b */
  544. rx_ctx.dtype = I40E_RX_DTYPE_HEADER_SPLIT;
  545. }
  546. /* databuffer length validation */
  547. if (info->databuffer_size > ((16 * 1024) - 128)) {
  548. ret = -EINVAL;
  549. goto error_param;
  550. }
  551. rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
  552. /* max pkt. length validation */
  553. if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
  554. ret = -EINVAL;
  555. goto error_param;
  556. }
  557. rx_ctx.rxmax = info->max_pkt_size;
  558. /* enable 32bytes desc always */
  559. rx_ctx.dsize = 1;
  560. /* default values */
  561. rx_ctx.lrxqthresh = 1;
  562. rx_ctx.crcstrip = 1;
  563. rx_ctx.prefena = 1;
  564. rx_ctx.l2tsel = 1;
  565. /* clear the context in the HMC */
  566. ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
  567. if (ret) {
  568. dev_err(&pf->pdev->dev,
  569. "Failed to clear VF LAN Rx queue context %d, error: %d\n",
  570. pf_queue_id, ret);
  571. ret = -ENOENT;
  572. goto error_param;
  573. }
  574. /* set the context in the HMC */
  575. ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
  576. if (ret) {
  577. dev_err(&pf->pdev->dev,
  578. "Failed to set VF LAN Rx queue context %d error: %d\n",
  579. pf_queue_id, ret);
  580. ret = -ENOENT;
  581. goto error_param;
  582. }
  583. error_param:
  584. return ret;
  585. }
  586. /**
  587. * i40e_alloc_vsi_res
  588. * @vf: pointer to the VF info
  589. * @type: type of VSI to allocate
  590. *
  591. * alloc VF vsi context & resources
  592. **/
  593. static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
  594. {
  595. struct i40e_mac_filter *f = NULL;
  596. struct i40e_pf *pf = vf->pf;
  597. struct i40e_vsi *vsi;
  598. int ret = 0;
  599. vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
  600. if (!vsi) {
  601. dev_err(&pf->pdev->dev,
  602. "add vsi failed for VF %d, aq_err %d\n",
  603. vf->vf_id, pf->hw.aq.asq_last_status);
  604. ret = -ENOENT;
  605. goto error_alloc_vsi_res;
  606. }
  607. if (type == I40E_VSI_SRIOV) {
  608. u64 hena = i40e_pf_get_default_rss_hena(pf);
  609. u8 broadcast[ETH_ALEN];
  610. vf->lan_vsi_idx = vsi->idx;
  611. vf->lan_vsi_id = vsi->id;
  612. /* If the port VLAN has been configured and then the
  613. * VF driver was removed then the VSI port VLAN
  614. * configuration was destroyed. Check if there is
  615. * a port VLAN and restore the VSI configuration if
  616. * needed.
  617. */
  618. if (vf->port_vlan_id)
  619. i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
  620. spin_lock_bh(&vsi->mac_filter_hash_lock);
  621. if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
  622. f = i40e_add_mac_filter(vsi,
  623. vf->default_lan_addr.addr);
  624. if (!f)
  625. dev_info(&pf->pdev->dev,
  626. "Could not add MAC filter %pM for VF %d\n",
  627. vf->default_lan_addr.addr, vf->vf_id);
  628. }
  629. eth_broadcast_addr(broadcast);
  630. f = i40e_add_mac_filter(vsi, broadcast);
  631. if (!f)
  632. dev_info(&pf->pdev->dev,
  633. "Could not allocate VF broadcast filter\n");
  634. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  635. wr32(&pf->hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)hena);
  636. wr32(&pf->hw, I40E_VFQF_HENA1(1, vf->vf_id), (u32)(hena >> 32));
  637. }
  638. /* program mac filter */
  639. ret = i40e_sync_vsi_filters(vsi);
  640. if (ret)
  641. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  642. /* Set VF bandwidth if specified */
  643. if (vf->tx_rate) {
  644. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  645. vf->tx_rate / 50, 0, NULL);
  646. if (ret)
  647. dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
  648. vf->vf_id, ret);
  649. }
  650. error_alloc_vsi_res:
  651. return ret;
  652. }
  653. /**
  654. * i40e_enable_vf_mappings
  655. * @vf: pointer to the VF info
  656. *
  657. * enable VF mappings
  658. **/
  659. static void i40e_enable_vf_mappings(struct i40e_vf *vf)
  660. {
  661. struct i40e_pf *pf = vf->pf;
  662. struct i40e_hw *hw = &pf->hw;
  663. u32 reg, total_queue_pairs = 0;
  664. int j;
  665. /* Tell the hardware we're using noncontiguous mapping. HW requires
  666. * that VF queues be mapped using this method, even when they are
  667. * contiguous in real life
  668. */
  669. i40e_write_rx_ctl(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
  670. I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
  671. /* enable VF vplan_qtable mappings */
  672. reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
  673. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
  674. /* map PF queues to VF queues */
  675. for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
  676. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
  677. reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
  678. wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
  679. total_queue_pairs++;
  680. }
  681. /* map PF queues to VSI */
  682. for (j = 0; j < 7; j++) {
  683. if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
  684. reg = 0x07FF07FF; /* unused */
  685. } else {
  686. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
  687. j * 2);
  688. reg = qid;
  689. qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
  690. (j * 2) + 1);
  691. reg |= qid << 16;
  692. }
  693. i40e_write_rx_ctl(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id),
  694. reg);
  695. }
  696. i40e_flush(hw);
  697. }
  698. /**
  699. * i40e_disable_vf_mappings
  700. * @vf: pointer to the VF info
  701. *
  702. * disable VF mappings
  703. **/
  704. static void i40e_disable_vf_mappings(struct i40e_vf *vf)
  705. {
  706. struct i40e_pf *pf = vf->pf;
  707. struct i40e_hw *hw = &pf->hw;
  708. int i;
  709. /* disable qp mappings */
  710. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
  711. for (i = 0; i < I40E_MAX_VSI_QP; i++)
  712. wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
  713. I40E_QUEUE_END_OF_LIST);
  714. i40e_flush(hw);
  715. }
  716. /**
  717. * i40e_free_vf_res
  718. * @vf: pointer to the VF info
  719. *
  720. * free VF resources
  721. **/
  722. static void i40e_free_vf_res(struct i40e_vf *vf)
  723. {
  724. struct i40e_pf *pf = vf->pf;
  725. struct i40e_hw *hw = &pf->hw;
  726. u32 reg_idx, reg;
  727. int i, msix_vf;
  728. /* Start by disabling VF's configuration API to prevent the OS from
  729. * accessing the VF's VSI after it's freed / invalidated.
  730. */
  731. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  732. /* It's possible the VF had requeuested more queues than the default so
  733. * do the accounting here when we're about to free them.
  734. */
  735. if (vf->num_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF) {
  736. pf->queues_left += vf->num_queue_pairs -
  737. I40E_DEFAULT_QUEUES_PER_VF;
  738. }
  739. /* free vsi & disconnect it from the parent uplink */
  740. if (vf->lan_vsi_idx) {
  741. i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  742. vf->lan_vsi_idx = 0;
  743. vf->lan_vsi_id = 0;
  744. vf->num_mac = 0;
  745. }
  746. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  747. /* disable interrupts so the VF starts in a known state */
  748. for (i = 0; i < msix_vf; i++) {
  749. /* format is same for both registers */
  750. if (0 == i)
  751. reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
  752. else
  753. reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
  754. (vf->vf_id))
  755. + (i - 1));
  756. wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
  757. i40e_flush(hw);
  758. }
  759. /* clear the irq settings */
  760. for (i = 0; i < msix_vf; i++) {
  761. /* format is same for both registers */
  762. if (0 == i)
  763. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  764. else
  765. reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
  766. (vf->vf_id))
  767. + (i - 1));
  768. reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
  769. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
  770. wr32(hw, reg_idx, reg);
  771. i40e_flush(hw);
  772. }
  773. /* reset some of the state variables keeping track of the resources */
  774. vf->num_queue_pairs = 0;
  775. clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  776. clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  777. }
  778. /**
  779. * i40e_alloc_vf_res
  780. * @vf: pointer to the VF info
  781. *
  782. * allocate VF resources
  783. **/
  784. static int i40e_alloc_vf_res(struct i40e_vf *vf)
  785. {
  786. struct i40e_pf *pf = vf->pf;
  787. int total_queue_pairs = 0;
  788. int ret;
  789. if (vf->num_req_queues &&
  790. vf->num_req_queues <= pf->queues_left + I40E_DEFAULT_QUEUES_PER_VF)
  791. pf->num_vf_qps = vf->num_req_queues;
  792. else
  793. pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
  794. /* allocate hw vsi context & associated resources */
  795. ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
  796. if (ret)
  797. goto error_alloc;
  798. total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  799. /* We account for each VF to get a default number of queue pairs. If
  800. * the VF has now requested more, we need to account for that to make
  801. * certain we never request more queues than we actually have left in
  802. * HW.
  803. */
  804. if (total_queue_pairs > I40E_DEFAULT_QUEUES_PER_VF)
  805. pf->queues_left -=
  806. total_queue_pairs - I40E_DEFAULT_QUEUES_PER_VF;
  807. if (vf->trusted)
  808. set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  809. else
  810. clear_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  811. /* store the total qps number for the runtime
  812. * VF req validation
  813. */
  814. vf->num_queue_pairs = total_queue_pairs;
  815. /* VF is now completely initialized */
  816. set_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  817. error_alloc:
  818. if (ret)
  819. i40e_free_vf_res(vf);
  820. return ret;
  821. }
  822. #define VF_DEVICE_STATUS 0xAA
  823. #define VF_TRANS_PENDING_MASK 0x20
  824. /**
  825. * i40e_quiesce_vf_pci
  826. * @vf: pointer to the VF structure
  827. *
  828. * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
  829. * if the transactions never clear.
  830. **/
  831. static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
  832. {
  833. struct i40e_pf *pf = vf->pf;
  834. struct i40e_hw *hw = &pf->hw;
  835. int vf_abs_id, i;
  836. u32 reg;
  837. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  838. wr32(hw, I40E_PF_PCI_CIAA,
  839. VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
  840. for (i = 0; i < 100; i++) {
  841. reg = rd32(hw, I40E_PF_PCI_CIAD);
  842. if ((reg & VF_TRANS_PENDING_MASK) == 0)
  843. return 0;
  844. udelay(1);
  845. }
  846. return -EIO;
  847. }
  848. /**
  849. * i40e_trigger_vf_reset
  850. * @vf: pointer to the VF structure
  851. * @flr: VFLR was issued or not
  852. *
  853. * Trigger hardware to start a reset for a particular VF. Expects the caller
  854. * to wait the proper amount of time to allow hardware to reset the VF before
  855. * it cleans up and restores VF functionality.
  856. **/
  857. static void i40e_trigger_vf_reset(struct i40e_vf *vf, bool flr)
  858. {
  859. struct i40e_pf *pf = vf->pf;
  860. struct i40e_hw *hw = &pf->hw;
  861. u32 reg, reg_idx, bit_idx;
  862. /* warn the VF */
  863. clear_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  864. /* Disable VF's configuration API during reset. The flag is re-enabled
  865. * in i40e_alloc_vf_res(), when it's safe again to access VF's VSI.
  866. * It's normally disabled in i40e_free_vf_res(), but it's safer
  867. * to do it earlier to give some time to finish to any VF config
  868. * functions that may still be running at this point.
  869. */
  870. clear_bit(I40E_VF_STATE_INIT, &vf->vf_states);
  871. /* In the case of a VFLR, the HW has already reset the VF and we
  872. * just need to clean up, so don't hit the VFRTRIG register.
  873. */
  874. if (!flr) {
  875. /* reset VF using VPGEN_VFRTRIG reg */
  876. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  877. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  878. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  879. i40e_flush(hw);
  880. }
  881. /* clear the VFLR bit in GLGEN_VFLRSTAT */
  882. reg_idx = (hw->func_caps.vf_base_id + vf->vf_id) / 32;
  883. bit_idx = (hw->func_caps.vf_base_id + vf->vf_id) % 32;
  884. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  885. i40e_flush(hw);
  886. if (i40e_quiesce_vf_pci(vf))
  887. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  888. vf->vf_id);
  889. }
  890. /**
  891. * i40e_cleanup_reset_vf
  892. * @vf: pointer to the VF structure
  893. *
  894. * Cleanup a VF after the hardware reset is finished. Expects the caller to
  895. * have verified whether the reset is finished properly, and ensure the
  896. * minimum amount of wait time has passed.
  897. **/
  898. static void i40e_cleanup_reset_vf(struct i40e_vf *vf)
  899. {
  900. struct i40e_pf *pf = vf->pf;
  901. struct i40e_hw *hw = &pf->hw;
  902. u32 reg;
  903. /* free VF resources to begin resetting the VSI state */
  904. i40e_free_vf_res(vf);
  905. /* Enable hardware by clearing the reset bit in the VPGEN_VFRTRIG reg.
  906. * By doing this we allow HW to access VF memory at any point. If we
  907. * did it any sooner, HW could access memory while it was being freed
  908. * in i40e_free_vf_res(), causing an IOMMU fault.
  909. *
  910. * On the other hand, this needs to be done ASAP, because the VF driver
  911. * is waiting for this to happen and may report a timeout. It's
  912. * harmless, but it gets logged into Guest OS kernel log, so best avoid
  913. * it.
  914. */
  915. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  916. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  917. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  918. /* reallocate VF resources to finish resetting the VSI state */
  919. if (!i40e_alloc_vf_res(vf)) {
  920. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  921. i40e_enable_vf_mappings(vf);
  922. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  923. clear_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  924. /* Do not notify the client during VF init */
  925. if (!test_and_clear_bit(I40E_VF_STATE_PRE_ENABLE,
  926. &vf->vf_states))
  927. i40e_notify_client_of_vf_reset(pf, abs_vf_id);
  928. vf->num_vlan = 0;
  929. }
  930. /* Tell the VF driver the reset is done. This needs to be done only
  931. * after VF has been fully initialized, because the VF driver may
  932. * request resources immediately after setting this flag.
  933. */
  934. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
  935. }
  936. /**
  937. * i40e_reset_vf
  938. * @vf: pointer to the VF structure
  939. * @flr: VFLR was issued or not
  940. *
  941. * Returns true if the VF is reset, false otherwise.
  942. **/
  943. bool i40e_reset_vf(struct i40e_vf *vf, bool flr)
  944. {
  945. struct i40e_pf *pf = vf->pf;
  946. struct i40e_hw *hw = &pf->hw;
  947. bool rsd = false;
  948. u32 reg;
  949. int i;
  950. /* If the VFs have been disabled, this means something else is
  951. * resetting the VF, so we shouldn't continue.
  952. */
  953. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  954. return false;
  955. i40e_trigger_vf_reset(vf, flr);
  956. /* poll VPGEN_VFRSTAT reg to make sure
  957. * that reset is complete
  958. */
  959. for (i = 0; i < 10; i++) {
  960. /* VF reset requires driver to first reset the VF and then
  961. * poll the status register to make sure that the reset
  962. * completed successfully. Due to internal HW FIFO flushes,
  963. * we must wait 10ms before the register will be valid.
  964. */
  965. usleep_range(10000, 20000);
  966. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  967. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  968. rsd = true;
  969. break;
  970. }
  971. }
  972. if (flr)
  973. usleep_range(10000, 20000);
  974. if (!rsd)
  975. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  976. vf->vf_id);
  977. usleep_range(10000, 20000);
  978. /* On initial reset, we don't have any queues to disable */
  979. if (vf->lan_vsi_idx != 0)
  980. i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
  981. i40e_cleanup_reset_vf(vf);
  982. i40e_flush(hw);
  983. clear_bit(__I40E_VF_DISABLE, pf->state);
  984. return true;
  985. }
  986. /**
  987. * i40e_reset_all_vfs
  988. * @pf: pointer to the PF structure
  989. * @flr: VFLR was issued or not
  990. *
  991. * Reset all allocated VFs in one go. First, tell the hardware to reset each
  992. * VF, then do all the waiting in one chunk, and finally finish restoring each
  993. * VF after the wait. This is useful during PF routines which need to reset
  994. * all VFs, as otherwise it must perform these resets in a serialized fashion.
  995. *
  996. * Returns true if any VFs were reset, and false otherwise.
  997. **/
  998. bool i40e_reset_all_vfs(struct i40e_pf *pf, bool flr)
  999. {
  1000. struct i40e_hw *hw = &pf->hw;
  1001. struct i40e_vf *vf;
  1002. int i, v;
  1003. u32 reg;
  1004. /* If we don't have any VFs, then there is nothing to reset */
  1005. if (!pf->num_alloc_vfs)
  1006. return false;
  1007. /* If VFs have been disabled, there is no need to reset */
  1008. if (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1009. return false;
  1010. /* Begin reset on all VFs at once */
  1011. for (v = 0; v < pf->num_alloc_vfs; v++)
  1012. i40e_trigger_vf_reset(&pf->vf[v], flr);
  1013. /* HW requires some time to make sure it can flush the FIFO for a VF
  1014. * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
  1015. * sequence to make sure that it has completed. We'll keep track of
  1016. * the VFs using a simple iterator that increments once that VF has
  1017. * finished resetting.
  1018. */
  1019. for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
  1020. usleep_range(10000, 20000);
  1021. /* Check each VF in sequence, beginning with the VF to fail
  1022. * the previous check.
  1023. */
  1024. while (v < pf->num_alloc_vfs) {
  1025. vf = &pf->vf[v];
  1026. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  1027. if (!(reg & I40E_VPGEN_VFRSTAT_VFRD_MASK))
  1028. break;
  1029. /* If the current VF has finished resetting, move on
  1030. * to the next VF in sequence.
  1031. */
  1032. v++;
  1033. }
  1034. }
  1035. if (flr)
  1036. usleep_range(10000, 20000);
  1037. /* Display a warning if at least one VF didn't manage to reset in
  1038. * time, but continue on with the operation.
  1039. */
  1040. if (v < pf->num_alloc_vfs)
  1041. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  1042. pf->vf[v].vf_id);
  1043. usleep_range(10000, 20000);
  1044. /* Begin disabling all the rings associated with VFs, but do not wait
  1045. * between each VF.
  1046. */
  1047. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1048. /* On initial reset, we don't have any queues to disable */
  1049. if (pf->vf[v].lan_vsi_idx == 0)
  1050. continue;
  1051. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1052. }
  1053. /* Now that we've notified HW to disable all of the VF rings, wait
  1054. * until they finish.
  1055. */
  1056. for (v = 0; v < pf->num_alloc_vfs; v++) {
  1057. /* On initial reset, we don't have any queues to disable */
  1058. if (pf->vf[v].lan_vsi_idx == 0)
  1059. continue;
  1060. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[v].lan_vsi_idx]);
  1061. }
  1062. /* Hw may need up to 50ms to finish disabling the RX queues. We
  1063. * minimize the wait by delaying only once for all VFs.
  1064. */
  1065. mdelay(50);
  1066. /* Finish the reset on each VF */
  1067. for (v = 0; v < pf->num_alloc_vfs; v++)
  1068. i40e_cleanup_reset_vf(&pf->vf[v]);
  1069. i40e_flush(hw);
  1070. clear_bit(__I40E_VF_DISABLE, pf->state);
  1071. return true;
  1072. }
  1073. /**
  1074. * i40e_free_vfs
  1075. * @pf: pointer to the PF structure
  1076. *
  1077. * free VF resources
  1078. **/
  1079. void i40e_free_vfs(struct i40e_pf *pf)
  1080. {
  1081. struct i40e_hw *hw = &pf->hw;
  1082. u32 reg_idx, bit_idx;
  1083. int i, tmp, vf_id;
  1084. if (!pf->vf)
  1085. return;
  1086. while (test_and_set_bit(__I40E_VF_DISABLE, pf->state))
  1087. usleep_range(1000, 2000);
  1088. i40e_notify_client_of_vf_enable(pf, 0);
  1089. /* Amortize wait time by stopping all VFs at the same time */
  1090. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1091. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1092. continue;
  1093. i40e_vsi_stop_rings_no_wait(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1094. }
  1095. for (i = 0; i < pf->num_alloc_vfs; i++) {
  1096. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1097. continue;
  1098. i40e_vsi_wait_queues_disabled(pf->vsi[pf->vf[i].lan_vsi_idx]);
  1099. }
  1100. /* Disable IOV before freeing resources. This lets any VF drivers
  1101. * running in the host get themselves cleaned up before we yank
  1102. * the carpet out from underneath their feet.
  1103. */
  1104. if (!pci_vfs_assigned(pf->pdev))
  1105. pci_disable_sriov(pf->pdev);
  1106. else
  1107. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  1108. /* free up VF resources */
  1109. tmp = pf->num_alloc_vfs;
  1110. pf->num_alloc_vfs = 0;
  1111. for (i = 0; i < tmp; i++) {
  1112. if (test_bit(I40E_VF_STATE_INIT, &pf->vf[i].vf_states))
  1113. i40e_free_vf_res(&pf->vf[i]);
  1114. /* disable qp mappings */
  1115. i40e_disable_vf_mappings(&pf->vf[i]);
  1116. }
  1117. kfree(pf->vf);
  1118. pf->vf = NULL;
  1119. /* This check is for when the driver is unloaded while VFs are
  1120. * assigned. Setting the number of VFs to 0 through sysfs is caught
  1121. * before this function ever gets called.
  1122. */
  1123. if (!pci_vfs_assigned(pf->pdev)) {
  1124. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  1125. * work correctly when SR-IOV gets re-enabled.
  1126. */
  1127. for (vf_id = 0; vf_id < tmp; vf_id++) {
  1128. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  1129. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  1130. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1131. }
  1132. }
  1133. clear_bit(__I40E_VF_DISABLE, pf->state);
  1134. }
  1135. #ifdef CONFIG_PCI_IOV
  1136. /**
  1137. * i40e_alloc_vfs
  1138. * @pf: pointer to the PF structure
  1139. * @num_alloc_vfs: number of VFs to allocate
  1140. *
  1141. * allocate VF resources
  1142. **/
  1143. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  1144. {
  1145. struct i40e_vf *vfs;
  1146. int i, ret = 0;
  1147. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  1148. i40e_irq_dynamic_disable_icr0(pf);
  1149. /* Check to see if we're just allocating resources for extant VFs */
  1150. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  1151. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  1152. if (ret) {
  1153. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1154. pf->num_alloc_vfs = 0;
  1155. goto err_iov;
  1156. }
  1157. }
  1158. /* allocate memory */
  1159. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  1160. if (!vfs) {
  1161. ret = -ENOMEM;
  1162. goto err_alloc;
  1163. }
  1164. pf->vf = vfs;
  1165. /* apply default profile */
  1166. for (i = 0; i < num_alloc_vfs; i++) {
  1167. vfs[i].pf = pf;
  1168. vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
  1169. vfs[i].vf_id = i;
  1170. /* assign default capabilities */
  1171. set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  1172. vfs[i].spoofchk = true;
  1173. set_bit(I40E_VF_STATE_PRE_ENABLE, &vfs[i].vf_states);
  1174. }
  1175. pf->num_alloc_vfs = num_alloc_vfs;
  1176. /* VF resources get allocated during reset */
  1177. i40e_reset_all_vfs(pf, false);
  1178. i40e_notify_client_of_vf_enable(pf, num_alloc_vfs);
  1179. err_alloc:
  1180. if (ret)
  1181. i40e_free_vfs(pf);
  1182. err_iov:
  1183. /* Re-enable interrupt 0. */
  1184. i40e_irq_dynamic_enable_icr0(pf);
  1185. return ret;
  1186. }
  1187. #endif
  1188. /**
  1189. * i40e_pci_sriov_enable
  1190. * @pdev: pointer to a pci_dev structure
  1191. * @num_vfs: number of VFs to allocate
  1192. *
  1193. * Enable or change the number of VFs
  1194. **/
  1195. static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
  1196. {
  1197. #ifdef CONFIG_PCI_IOV
  1198. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1199. int pre_existing_vfs = pci_num_vf(pdev);
  1200. int err = 0;
  1201. if (test_bit(__I40E_TESTING, pf->state)) {
  1202. dev_warn(&pdev->dev,
  1203. "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
  1204. err = -EPERM;
  1205. goto err_out;
  1206. }
  1207. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  1208. i40e_free_vfs(pf);
  1209. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  1210. goto out;
  1211. if (num_vfs > pf->num_req_vfs) {
  1212. dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
  1213. num_vfs, pf->num_req_vfs);
  1214. err = -EPERM;
  1215. goto err_out;
  1216. }
  1217. dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
  1218. err = i40e_alloc_vfs(pf, num_vfs);
  1219. if (err) {
  1220. dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
  1221. goto err_out;
  1222. }
  1223. out:
  1224. return num_vfs;
  1225. err_out:
  1226. return err;
  1227. #endif
  1228. return 0;
  1229. }
  1230. /**
  1231. * i40e_pci_sriov_configure
  1232. * @pdev: pointer to a pci_dev structure
  1233. * @num_vfs: number of VFs to allocate
  1234. *
  1235. * Enable or change the number of VFs. Called when the user updates the number
  1236. * of VFs in sysfs.
  1237. **/
  1238. int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  1239. {
  1240. struct i40e_pf *pf = pci_get_drvdata(pdev);
  1241. if (num_vfs) {
  1242. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  1243. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  1244. i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
  1245. }
  1246. return i40e_pci_sriov_enable(pdev, num_vfs);
  1247. }
  1248. if (!pci_vfs_assigned(pf->pdev)) {
  1249. i40e_free_vfs(pf);
  1250. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  1251. i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
  1252. } else {
  1253. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
  1254. return -EINVAL;
  1255. }
  1256. return 0;
  1257. }
  1258. /***********************virtual channel routines******************/
  1259. /**
  1260. * i40e_vc_send_msg_to_vf
  1261. * @vf: pointer to the VF info
  1262. * @v_opcode: virtual channel opcode
  1263. * @v_retval: virtual channel return value
  1264. * @msg: pointer to the msg buffer
  1265. * @msglen: msg length
  1266. *
  1267. * send msg to VF
  1268. **/
  1269. static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
  1270. u32 v_retval, u8 *msg, u16 msglen)
  1271. {
  1272. struct i40e_pf *pf;
  1273. struct i40e_hw *hw;
  1274. int abs_vf_id;
  1275. i40e_status aq_ret;
  1276. /* validate the request */
  1277. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  1278. return -EINVAL;
  1279. pf = vf->pf;
  1280. hw = &pf->hw;
  1281. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  1282. /* single place to detect unsuccessful return values */
  1283. if (v_retval) {
  1284. vf->num_invalid_msgs++;
  1285. dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
  1286. vf->vf_id, v_opcode, v_retval);
  1287. if (vf->num_invalid_msgs >
  1288. I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
  1289. dev_err(&pf->pdev->dev,
  1290. "Number of invalid messages exceeded for VF %d\n",
  1291. vf->vf_id);
  1292. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  1293. set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
  1294. }
  1295. } else {
  1296. vf->num_valid_msgs++;
  1297. /* reset the invalid counter, if a valid message is received. */
  1298. vf->num_invalid_msgs = 0;
  1299. }
  1300. aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  1301. msg, msglen, NULL);
  1302. if (aq_ret) {
  1303. dev_info(&pf->pdev->dev,
  1304. "Unable to send the message to VF %d aq_err %d\n",
  1305. vf->vf_id, pf->hw.aq.asq_last_status);
  1306. return -EIO;
  1307. }
  1308. return 0;
  1309. }
  1310. /**
  1311. * i40e_vc_send_resp_to_vf
  1312. * @vf: pointer to the VF info
  1313. * @opcode: operation code
  1314. * @retval: return value
  1315. *
  1316. * send resp msg to VF
  1317. **/
  1318. static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
  1319. enum virtchnl_ops opcode,
  1320. i40e_status retval)
  1321. {
  1322. return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
  1323. }
  1324. /**
  1325. * i40e_vc_get_version_msg
  1326. * @vf: pointer to the VF info
  1327. *
  1328. * called from the VF to request the API version used by the PF
  1329. **/
  1330. static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
  1331. {
  1332. struct virtchnl_version_info info = {
  1333. VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
  1334. };
  1335. vf->vf_ver = *(struct virtchnl_version_info *)msg;
  1336. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1337. if (VF_IS_V10(&vf->vf_ver))
  1338. info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1339. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION,
  1340. I40E_SUCCESS, (u8 *)&info,
  1341. sizeof(struct virtchnl_version_info));
  1342. }
  1343. /**
  1344. * i40e_vc_get_vf_resources_msg
  1345. * @vf: pointer to the VF info
  1346. * @msg: pointer to the msg buffer
  1347. * @msglen: msg length
  1348. *
  1349. * called from the VF to request its resources
  1350. **/
  1351. static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
  1352. {
  1353. struct virtchnl_vf_resource *vfres = NULL;
  1354. struct i40e_pf *pf = vf->pf;
  1355. i40e_status aq_ret = 0;
  1356. struct i40e_vsi *vsi;
  1357. int num_vsis = 1;
  1358. int len = 0;
  1359. int ret;
  1360. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  1361. aq_ret = I40E_ERR_PARAM;
  1362. goto err;
  1363. }
  1364. len = (sizeof(struct virtchnl_vf_resource) +
  1365. sizeof(struct virtchnl_vsi_resource) * num_vsis);
  1366. vfres = kzalloc(len, GFP_KERNEL);
  1367. if (!vfres) {
  1368. aq_ret = I40E_ERR_NO_MEMORY;
  1369. len = 0;
  1370. goto err;
  1371. }
  1372. if (VF_IS_V11(&vf->vf_ver))
  1373. vf->driver_caps = *(u32 *)msg;
  1374. else
  1375. vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
  1376. VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1377. VIRTCHNL_VF_OFFLOAD_VLAN;
  1378. vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
  1379. vsi = pf->vsi[vf->lan_vsi_idx];
  1380. if (!vsi->info.pvid)
  1381. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
  1382. if (i40e_vf_client_capable(pf, vf->vf_id) &&
  1383. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_IWARP)) {
  1384. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_IWARP;
  1385. set_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1386. } else {
  1387. clear_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states);
  1388. }
  1389. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  1390. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
  1391. } else {
  1392. if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
  1393. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ))
  1394. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1395. else
  1396. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1397. }
  1398. if (pf->hw_features & I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE) {
  1399. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1400. vfres->vf_cap_flags |=
  1401. VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
  1402. }
  1403. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
  1404. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
  1405. if ((pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE) &&
  1406. (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
  1407. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
  1408. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING) {
  1409. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  1410. dev_err(&pf->pdev->dev,
  1411. "VF %d requested polling mode: this feature is supported only when the device is running in single function per port (SFP) mode\n",
  1412. vf->vf_id);
  1413. aq_ret = I40E_ERR_PARAM;
  1414. goto err;
  1415. }
  1416. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1417. }
  1418. if (pf->hw_features & I40E_HW_WB_ON_ITR_CAPABLE) {
  1419. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  1420. vfres->vf_cap_flags |=
  1421. VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  1422. }
  1423. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
  1424. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
  1425. vfres->num_vsis = num_vsis;
  1426. vfres->num_queue_pairs = vf->num_queue_pairs;
  1427. vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
  1428. vfres->rss_key_size = I40E_HKEY_ARRAY_SIZE;
  1429. vfres->rss_lut_size = I40E_VF_HLUT_ARRAY_SIZE;
  1430. if (vf->lan_vsi_idx) {
  1431. vfres->vsi_res[0].vsi_id = vf->lan_vsi_id;
  1432. vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
  1433. vfres->vsi_res[0].num_queue_pairs = vsi->alloc_queue_pairs;
  1434. /* VFs only use TC 0 */
  1435. vfres->vsi_res[0].qset_handle
  1436. = le16_to_cpu(vsi->info.qs_handle[0]);
  1437. ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
  1438. vf->default_lan_addr.addr);
  1439. }
  1440. set_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states);
  1441. err:
  1442. /* send the response back to the VF */
  1443. ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES,
  1444. aq_ret, (u8 *)vfres, len);
  1445. kfree(vfres);
  1446. return ret;
  1447. }
  1448. /**
  1449. * i40e_vc_reset_vf_msg
  1450. * @vf: pointer to the VF info
  1451. * @msg: pointer to the msg buffer
  1452. * @msglen: msg length
  1453. *
  1454. * called from the VF to reset itself,
  1455. * unlike other virtchnl messages, PF driver
  1456. * doesn't send the response back to the VF
  1457. **/
  1458. static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
  1459. {
  1460. if (test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  1461. i40e_reset_vf(vf, false);
  1462. }
  1463. /**
  1464. * i40e_getnum_vf_vsi_vlan_filters
  1465. * @vsi: pointer to the vsi
  1466. *
  1467. * called to get the number of VLANs offloaded on this VF
  1468. **/
  1469. static inline int i40e_getnum_vf_vsi_vlan_filters(struct i40e_vsi *vsi)
  1470. {
  1471. struct i40e_mac_filter *f;
  1472. int num_vlans = 0, bkt;
  1473. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1474. if (f->vlan >= 0 && f->vlan <= I40E_MAX_VLANID)
  1475. num_vlans++;
  1476. }
  1477. return num_vlans;
  1478. }
  1479. /**
  1480. * i40e_vc_config_promiscuous_mode_msg
  1481. * @vf: pointer to the VF info
  1482. * @msg: pointer to the msg buffer
  1483. * @msglen: msg length
  1484. *
  1485. * called from the VF to configure the promiscuous mode of
  1486. * VF vsis
  1487. **/
  1488. static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
  1489. u8 *msg, u16 msglen)
  1490. {
  1491. struct virtchnl_promisc_info *info =
  1492. (struct virtchnl_promisc_info *)msg;
  1493. struct i40e_pf *pf = vf->pf;
  1494. struct i40e_hw *hw = &pf->hw;
  1495. struct i40e_mac_filter *f;
  1496. i40e_status aq_ret = 0;
  1497. bool allmulti = false;
  1498. struct i40e_vsi *vsi;
  1499. bool alluni = false;
  1500. int aq_err = 0;
  1501. int bkt;
  1502. vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
  1503. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1504. !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
  1505. !vsi) {
  1506. aq_ret = I40E_ERR_PARAM;
  1507. goto error_param;
  1508. }
  1509. if (!test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  1510. dev_err(&pf->pdev->dev,
  1511. "Unprivileged VF %d is attempting to configure promiscuous mode\n",
  1512. vf->vf_id);
  1513. /* Lie to the VF on purpose. */
  1514. aq_ret = 0;
  1515. goto error_param;
  1516. }
  1517. /* Multicast promiscuous handling*/
  1518. if (info->flags & FLAG_VF_MULTICAST_PROMISC)
  1519. allmulti = true;
  1520. if (vf->port_vlan_id) {
  1521. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw, vsi->seid,
  1522. allmulti,
  1523. vf->port_vlan_id,
  1524. NULL);
  1525. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1526. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1527. if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
  1528. continue;
  1529. aq_ret = i40e_aq_set_vsi_mc_promisc_on_vlan(hw,
  1530. vsi->seid,
  1531. allmulti,
  1532. f->vlan,
  1533. NULL);
  1534. aq_err = pf->hw.aq.asq_last_status;
  1535. if (aq_ret) {
  1536. dev_err(&pf->pdev->dev,
  1537. "Could not add VLAN %d to multicast promiscuous domain err %s aq_err %s\n",
  1538. f->vlan,
  1539. i40e_stat_str(&pf->hw, aq_ret),
  1540. i40e_aq_str(&pf->hw, aq_err));
  1541. break;
  1542. }
  1543. }
  1544. } else {
  1545. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
  1546. allmulti, NULL);
  1547. aq_err = pf->hw.aq.asq_last_status;
  1548. if (aq_ret) {
  1549. dev_err(&pf->pdev->dev,
  1550. "VF %d failed to set multicast promiscuous mode err %s aq_err %s\n",
  1551. vf->vf_id,
  1552. i40e_stat_str(&pf->hw, aq_ret),
  1553. i40e_aq_str(&pf->hw, aq_err));
  1554. goto error_param;
  1555. }
  1556. }
  1557. if (!aq_ret) {
  1558. dev_info(&pf->pdev->dev,
  1559. "VF %d successfully set multicast promiscuous mode\n",
  1560. vf->vf_id);
  1561. if (allmulti)
  1562. set_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1563. else
  1564. clear_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states);
  1565. }
  1566. if (info->flags & FLAG_VF_UNICAST_PROMISC)
  1567. alluni = true;
  1568. if (vf->port_vlan_id) {
  1569. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw, vsi->seid,
  1570. alluni,
  1571. vf->port_vlan_id,
  1572. NULL);
  1573. } else if (i40e_getnum_vf_vsi_vlan_filters(vsi)) {
  1574. hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
  1575. if (f->vlan < 0 || f->vlan > I40E_MAX_VLANID)
  1576. continue;
  1577. aq_ret = i40e_aq_set_vsi_uc_promisc_on_vlan(hw,
  1578. vsi->seid,
  1579. alluni,
  1580. f->vlan,
  1581. NULL);
  1582. aq_err = pf->hw.aq.asq_last_status;
  1583. if (aq_ret)
  1584. dev_err(&pf->pdev->dev,
  1585. "Could not add VLAN %d to Unicast promiscuous domain err %s aq_err %s\n",
  1586. f->vlan,
  1587. i40e_stat_str(&pf->hw, aq_ret),
  1588. i40e_aq_str(&pf->hw, aq_err));
  1589. }
  1590. } else {
  1591. aq_ret = i40e_aq_set_vsi_unicast_promiscuous(hw, vsi->seid,
  1592. alluni, NULL,
  1593. true);
  1594. aq_err = pf->hw.aq.asq_last_status;
  1595. if (aq_ret) {
  1596. dev_err(&pf->pdev->dev,
  1597. "VF %d failed to set unicast promiscuous mode %8.8x err %s aq_err %s\n",
  1598. vf->vf_id, info->flags,
  1599. i40e_stat_str(&pf->hw, aq_ret),
  1600. i40e_aq_str(&pf->hw, aq_err));
  1601. goto error_param;
  1602. }
  1603. }
  1604. if (!aq_ret) {
  1605. dev_info(&pf->pdev->dev,
  1606. "VF %d successfully set unicast promiscuous mode\n",
  1607. vf->vf_id);
  1608. if (alluni)
  1609. set_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1610. else
  1611. clear_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states);
  1612. }
  1613. error_param:
  1614. /* send the response to the VF */
  1615. return i40e_vc_send_resp_to_vf(vf,
  1616. VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  1617. aq_ret);
  1618. }
  1619. /**
  1620. * i40e_vc_config_queues_msg
  1621. * @vf: pointer to the VF info
  1622. * @msg: pointer to the msg buffer
  1623. * @msglen: msg length
  1624. *
  1625. * called from the VF to configure the rx/tx
  1626. * queues
  1627. **/
  1628. static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1629. {
  1630. struct virtchnl_vsi_queue_config_info *qci =
  1631. (struct virtchnl_vsi_queue_config_info *)msg;
  1632. struct virtchnl_queue_pair_info *qpi;
  1633. struct i40e_pf *pf = vf->pf;
  1634. u16 vsi_id, vsi_queue_id;
  1635. i40e_status aq_ret = 0;
  1636. int i;
  1637. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1638. aq_ret = I40E_ERR_PARAM;
  1639. goto error_param;
  1640. }
  1641. vsi_id = qci->vsi_id;
  1642. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1643. aq_ret = I40E_ERR_PARAM;
  1644. goto error_param;
  1645. }
  1646. for (i = 0; i < qci->num_queue_pairs; i++) {
  1647. qpi = &qci->qpair[i];
  1648. vsi_queue_id = qpi->txq.queue_id;
  1649. if ((qpi->txq.vsi_id != vsi_id) ||
  1650. (qpi->rxq.vsi_id != vsi_id) ||
  1651. (qpi->rxq.queue_id != vsi_queue_id) ||
  1652. !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
  1653. aq_ret = I40E_ERR_PARAM;
  1654. goto error_param;
  1655. }
  1656. if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
  1657. &qpi->rxq) ||
  1658. i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
  1659. &qpi->txq)) {
  1660. aq_ret = I40E_ERR_PARAM;
  1661. goto error_param;
  1662. }
  1663. }
  1664. /* set vsi num_queue_pairs in use to num configured by VF */
  1665. pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
  1666. error_param:
  1667. /* send the response to the VF */
  1668. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  1669. aq_ret);
  1670. }
  1671. /**
  1672. * i40e_vc_config_irq_map_msg
  1673. * @vf: pointer to the VF info
  1674. * @msg: pointer to the msg buffer
  1675. * @msglen: msg length
  1676. *
  1677. * called from the VF to configure the irq to
  1678. * queue map
  1679. **/
  1680. static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1681. {
  1682. struct virtchnl_irq_map_info *irqmap_info =
  1683. (struct virtchnl_irq_map_info *)msg;
  1684. struct virtchnl_vector_map *map;
  1685. u16 vsi_id, vsi_queue_id, vector_id;
  1686. i40e_status aq_ret = 0;
  1687. unsigned long tempmap;
  1688. int i;
  1689. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1690. aq_ret = I40E_ERR_PARAM;
  1691. goto error_param;
  1692. }
  1693. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1694. map = &irqmap_info->vecmap[i];
  1695. vector_id = map->vector_id;
  1696. vsi_id = map->vsi_id;
  1697. /* validate msg params */
  1698. if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
  1699. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1700. aq_ret = I40E_ERR_PARAM;
  1701. goto error_param;
  1702. }
  1703. /* lookout for the invalid queue index */
  1704. tempmap = map->rxq_map;
  1705. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1706. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1707. vsi_queue_id)) {
  1708. aq_ret = I40E_ERR_PARAM;
  1709. goto error_param;
  1710. }
  1711. }
  1712. tempmap = map->txq_map;
  1713. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1714. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1715. vsi_queue_id)) {
  1716. aq_ret = I40E_ERR_PARAM;
  1717. goto error_param;
  1718. }
  1719. }
  1720. i40e_config_irq_link_list(vf, vsi_id, map);
  1721. }
  1722. error_param:
  1723. /* send the response to the VF */
  1724. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP,
  1725. aq_ret);
  1726. }
  1727. /**
  1728. * i40e_vc_enable_queues_msg
  1729. * @vf: pointer to the VF info
  1730. * @msg: pointer to the msg buffer
  1731. * @msglen: msg length
  1732. *
  1733. * called from the VF to enable all or specific queue(s)
  1734. **/
  1735. static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1736. {
  1737. struct virtchnl_queue_select *vqs =
  1738. (struct virtchnl_queue_select *)msg;
  1739. struct i40e_pf *pf = vf->pf;
  1740. u16 vsi_id = vqs->vsi_id;
  1741. i40e_status aq_ret = 0;
  1742. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1743. aq_ret = I40E_ERR_PARAM;
  1744. goto error_param;
  1745. }
  1746. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1747. aq_ret = I40E_ERR_PARAM;
  1748. goto error_param;
  1749. }
  1750. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1751. aq_ret = I40E_ERR_PARAM;
  1752. goto error_param;
  1753. }
  1754. if (i40e_vsi_start_rings(pf->vsi[vf->lan_vsi_idx]))
  1755. aq_ret = I40E_ERR_TIMEOUT;
  1756. error_param:
  1757. /* send the response to the VF */
  1758. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES,
  1759. aq_ret);
  1760. }
  1761. /**
  1762. * i40e_vc_disable_queues_msg
  1763. * @vf: pointer to the VF info
  1764. * @msg: pointer to the msg buffer
  1765. * @msglen: msg length
  1766. *
  1767. * called from the VF to disable all or specific
  1768. * queue(s)
  1769. **/
  1770. static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1771. {
  1772. struct virtchnl_queue_select *vqs =
  1773. (struct virtchnl_queue_select *)msg;
  1774. struct i40e_pf *pf = vf->pf;
  1775. i40e_status aq_ret = 0;
  1776. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1777. aq_ret = I40E_ERR_PARAM;
  1778. goto error_param;
  1779. }
  1780. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1781. aq_ret = I40E_ERR_PARAM;
  1782. goto error_param;
  1783. }
  1784. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1785. aq_ret = I40E_ERR_PARAM;
  1786. goto error_param;
  1787. }
  1788. i40e_vsi_stop_rings(pf->vsi[vf->lan_vsi_idx]);
  1789. error_param:
  1790. /* send the response to the VF */
  1791. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES,
  1792. aq_ret);
  1793. }
  1794. /**
  1795. * i40e_vc_request_queues_msg
  1796. * @vf: pointer to the VF info
  1797. * @msg: pointer to the msg buffer
  1798. * @msglen: msg length
  1799. *
  1800. * VFs get a default number of queues but can use this message to request a
  1801. * different number. If the request is successful, PF will reset the VF and
  1802. * return 0. If unsuccessful, PF will send message informing VF of number of
  1803. * available queues and return result of sending VF a message.
  1804. **/
  1805. static int i40e_vc_request_queues_msg(struct i40e_vf *vf, u8 *msg, int msglen)
  1806. {
  1807. struct virtchnl_vf_res_request *vfres =
  1808. (struct virtchnl_vf_res_request *)msg;
  1809. int req_pairs = vfres->num_queue_pairs;
  1810. int cur_pairs = vf->num_queue_pairs;
  1811. struct i40e_pf *pf = vf->pf;
  1812. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states))
  1813. return -EINVAL;
  1814. if (req_pairs <= 0) {
  1815. dev_err(&pf->pdev->dev,
  1816. "VF %d tried to request %d queues. Ignoring.\n",
  1817. vf->vf_id, req_pairs);
  1818. } else if (req_pairs > I40E_MAX_VF_QUEUES) {
  1819. dev_err(&pf->pdev->dev,
  1820. "VF %d tried to request more than %d queues.\n",
  1821. vf->vf_id,
  1822. I40E_MAX_VF_QUEUES);
  1823. vfres->num_queue_pairs = I40E_MAX_VF_QUEUES;
  1824. } else if (req_pairs - cur_pairs > pf->queues_left) {
  1825. dev_warn(&pf->pdev->dev,
  1826. "VF %d requested %d more queues, but only %d left.\n",
  1827. vf->vf_id,
  1828. req_pairs - cur_pairs,
  1829. pf->queues_left);
  1830. vfres->num_queue_pairs = pf->queues_left + cur_pairs;
  1831. } else {
  1832. /* successful request */
  1833. vf->num_req_queues = req_pairs;
  1834. i40e_vc_notify_vf_reset(vf);
  1835. i40e_reset_vf(vf, false);
  1836. return 0;
  1837. }
  1838. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES, 0,
  1839. (u8 *)vfres, sizeof(vfres));
  1840. }
  1841. /**
  1842. * i40e_vc_get_stats_msg
  1843. * @vf: pointer to the VF info
  1844. * @msg: pointer to the msg buffer
  1845. * @msglen: msg length
  1846. *
  1847. * called from the VF to get vsi stats
  1848. **/
  1849. static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1850. {
  1851. struct virtchnl_queue_select *vqs =
  1852. (struct virtchnl_queue_select *)msg;
  1853. struct i40e_pf *pf = vf->pf;
  1854. struct i40e_eth_stats stats;
  1855. i40e_status aq_ret = 0;
  1856. struct i40e_vsi *vsi;
  1857. memset(&stats, 0, sizeof(struct i40e_eth_stats));
  1858. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  1859. aq_ret = I40E_ERR_PARAM;
  1860. goto error_param;
  1861. }
  1862. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1863. aq_ret = I40E_ERR_PARAM;
  1864. goto error_param;
  1865. }
  1866. vsi = pf->vsi[vf->lan_vsi_idx];
  1867. if (!vsi) {
  1868. aq_ret = I40E_ERR_PARAM;
  1869. goto error_param;
  1870. }
  1871. i40e_update_eth_stats(vsi);
  1872. stats = vsi->eth_stats;
  1873. error_param:
  1874. /* send the response back to the VF */
  1875. return i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
  1876. (u8 *)&stats, sizeof(stats));
  1877. }
  1878. /* If the VF is not trusted restrict the number of MAC/VLAN it can program */
  1879. #define I40E_VC_MAX_MAC_ADDR_PER_VF 12
  1880. #define I40E_VC_MAX_VLAN_PER_VF 8
  1881. /**
  1882. * i40e_check_vf_permission
  1883. * @vf: pointer to the VF info
  1884. * @macaddr: pointer to the MAC Address being checked
  1885. *
  1886. * Check if the VF has permission to add or delete unicast MAC address
  1887. * filters and return error code -EPERM if not. Then check if the
  1888. * address filter requested is broadcast or zero and if so return
  1889. * an invalid MAC address error code.
  1890. **/
  1891. static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
  1892. {
  1893. struct i40e_pf *pf = vf->pf;
  1894. int ret = 0;
  1895. if (is_broadcast_ether_addr(macaddr) ||
  1896. is_zero_ether_addr(macaddr)) {
  1897. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
  1898. ret = I40E_ERR_INVALID_MAC_ADDR;
  1899. } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
  1900. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) &&
  1901. !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
  1902. /* If the host VMM administrator has set the VF MAC address
  1903. * administratively via the ndo_set_vf_mac command then deny
  1904. * permission to the VF to add or delete unicast MAC addresses.
  1905. * Unless the VF is privileged and then it can do whatever.
  1906. * The VF may request to set the MAC address filter already
  1907. * assigned to it so do not return an error in that case.
  1908. */
  1909. dev_err(&pf->pdev->dev,
  1910. "VF attempting to override administratively set MAC address, reload the VF driver to resume normal operation\n");
  1911. ret = -EPERM;
  1912. } else if ((vf->num_mac >= I40E_VC_MAX_MAC_ADDR_PER_VF) &&
  1913. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  1914. dev_err(&pf->pdev->dev,
  1915. "VF is not trusted, switch the VF to trusted to add more functionality\n");
  1916. ret = -EPERM;
  1917. }
  1918. return ret;
  1919. }
  1920. /**
  1921. * i40e_vc_add_mac_addr_msg
  1922. * @vf: pointer to the VF info
  1923. * @msg: pointer to the msg buffer
  1924. * @msglen: msg length
  1925. *
  1926. * add guest mac address filter
  1927. **/
  1928. static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1929. {
  1930. struct virtchnl_ether_addr_list *al =
  1931. (struct virtchnl_ether_addr_list *)msg;
  1932. struct i40e_pf *pf = vf->pf;
  1933. struct i40e_vsi *vsi = NULL;
  1934. u16 vsi_id = al->vsi_id;
  1935. i40e_status ret = 0;
  1936. int i;
  1937. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1938. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1939. ret = I40E_ERR_PARAM;
  1940. goto error_param;
  1941. }
  1942. for (i = 0; i < al->num_elements; i++) {
  1943. ret = i40e_check_vf_permission(vf, al->list[i].addr);
  1944. if (ret)
  1945. goto error_param;
  1946. }
  1947. vsi = pf->vsi[vf->lan_vsi_idx];
  1948. /* Lock once, because all function inside for loop accesses VSI's
  1949. * MAC filter list which needs to be protected using same lock.
  1950. */
  1951. spin_lock_bh(&vsi->mac_filter_hash_lock);
  1952. /* add new addresses to the list */
  1953. for (i = 0; i < al->num_elements; i++) {
  1954. struct i40e_mac_filter *f;
  1955. f = i40e_find_mac(vsi, al->list[i].addr);
  1956. if (!f)
  1957. f = i40e_add_mac_filter(vsi, al->list[i].addr);
  1958. if (!f) {
  1959. dev_err(&pf->pdev->dev,
  1960. "Unable to add MAC filter %pM for VF %d\n",
  1961. al->list[i].addr, vf->vf_id);
  1962. ret = I40E_ERR_PARAM;
  1963. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1964. goto error_param;
  1965. } else {
  1966. vf->num_mac++;
  1967. }
  1968. }
  1969. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  1970. /* program the updated filter list */
  1971. ret = i40e_sync_vsi_filters(vsi);
  1972. if (ret)
  1973. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  1974. vf->vf_id, ret);
  1975. error_param:
  1976. /* send the response to the VF */
  1977. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_ETH_ADDR,
  1978. ret);
  1979. }
  1980. /**
  1981. * i40e_vc_del_mac_addr_msg
  1982. * @vf: pointer to the VF info
  1983. * @msg: pointer to the msg buffer
  1984. * @msglen: msg length
  1985. *
  1986. * remove guest mac address filter
  1987. **/
  1988. static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1989. {
  1990. struct virtchnl_ether_addr_list *al =
  1991. (struct virtchnl_ether_addr_list *)msg;
  1992. struct i40e_pf *pf = vf->pf;
  1993. struct i40e_vsi *vsi = NULL;
  1994. u16 vsi_id = al->vsi_id;
  1995. i40e_status ret = 0;
  1996. int i;
  1997. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  1998. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1999. ret = I40E_ERR_PARAM;
  2000. goto error_param;
  2001. }
  2002. for (i = 0; i < al->num_elements; i++) {
  2003. if (is_broadcast_ether_addr(al->list[i].addr) ||
  2004. is_zero_ether_addr(al->list[i].addr)) {
  2005. dev_err(&pf->pdev->dev, "Invalid MAC addr %pM for VF %d\n",
  2006. al->list[i].addr, vf->vf_id);
  2007. ret = I40E_ERR_INVALID_MAC_ADDR;
  2008. goto error_param;
  2009. }
  2010. }
  2011. vsi = pf->vsi[vf->lan_vsi_idx];
  2012. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2013. /* delete addresses from the list */
  2014. for (i = 0; i < al->num_elements; i++)
  2015. if (i40e_del_mac_filter(vsi, al->list[i].addr)) {
  2016. ret = I40E_ERR_INVALID_MAC_ADDR;
  2017. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2018. goto error_param;
  2019. } else {
  2020. vf->num_mac--;
  2021. }
  2022. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2023. /* program the updated filter list */
  2024. ret = i40e_sync_vsi_filters(vsi);
  2025. if (ret)
  2026. dev_err(&pf->pdev->dev, "Unable to program VF %d MAC filters, error %d\n",
  2027. vf->vf_id, ret);
  2028. error_param:
  2029. /* send the response to the VF */
  2030. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_ETH_ADDR,
  2031. ret);
  2032. }
  2033. /**
  2034. * i40e_vc_add_vlan_msg
  2035. * @vf: pointer to the VF info
  2036. * @msg: pointer to the msg buffer
  2037. * @msglen: msg length
  2038. *
  2039. * program guest vlan id
  2040. **/
  2041. static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2042. {
  2043. struct virtchnl_vlan_filter_list *vfl =
  2044. (struct virtchnl_vlan_filter_list *)msg;
  2045. struct i40e_pf *pf = vf->pf;
  2046. struct i40e_vsi *vsi = NULL;
  2047. u16 vsi_id = vfl->vsi_id;
  2048. i40e_status aq_ret = 0;
  2049. int i;
  2050. if ((vf->num_vlan >= I40E_VC_MAX_VLAN_PER_VF) &&
  2051. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps)) {
  2052. dev_err(&pf->pdev->dev,
  2053. "VF is not trusted, switch the VF to trusted to add more VLAN addresses\n");
  2054. goto error_param;
  2055. }
  2056. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2057. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2058. aq_ret = I40E_ERR_PARAM;
  2059. goto error_param;
  2060. }
  2061. for (i = 0; i < vfl->num_elements; i++) {
  2062. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2063. aq_ret = I40E_ERR_PARAM;
  2064. dev_err(&pf->pdev->dev,
  2065. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  2066. goto error_param;
  2067. }
  2068. }
  2069. vsi = pf->vsi[vf->lan_vsi_idx];
  2070. if (vsi->info.pvid) {
  2071. aq_ret = I40E_ERR_PARAM;
  2072. goto error_param;
  2073. }
  2074. i40e_vlan_stripping_enable(vsi);
  2075. for (i = 0; i < vfl->num_elements; i++) {
  2076. /* add new VLAN filter */
  2077. int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
  2078. if (!ret)
  2079. vf->num_vlan++;
  2080. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2081. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2082. true,
  2083. vfl->vlan_id[i],
  2084. NULL);
  2085. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2086. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2087. true,
  2088. vfl->vlan_id[i],
  2089. NULL);
  2090. if (ret)
  2091. dev_err(&pf->pdev->dev,
  2092. "Unable to add VLAN filter %d for VF %d, error %d\n",
  2093. vfl->vlan_id[i], vf->vf_id, ret);
  2094. }
  2095. error_param:
  2096. /* send the response to the VF */
  2097. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret);
  2098. }
  2099. /**
  2100. * i40e_vc_remove_vlan_msg
  2101. * @vf: pointer to the VF info
  2102. * @msg: pointer to the msg buffer
  2103. * @msglen: msg length
  2104. *
  2105. * remove programmed guest vlan id
  2106. **/
  2107. static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2108. {
  2109. struct virtchnl_vlan_filter_list *vfl =
  2110. (struct virtchnl_vlan_filter_list *)msg;
  2111. struct i40e_pf *pf = vf->pf;
  2112. struct i40e_vsi *vsi = NULL;
  2113. u16 vsi_id = vfl->vsi_id;
  2114. i40e_status aq_ret = 0;
  2115. int i;
  2116. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2117. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  2118. aq_ret = I40E_ERR_PARAM;
  2119. goto error_param;
  2120. }
  2121. for (i = 0; i < vfl->num_elements; i++) {
  2122. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  2123. aq_ret = I40E_ERR_PARAM;
  2124. goto error_param;
  2125. }
  2126. }
  2127. vsi = pf->vsi[vf->lan_vsi_idx];
  2128. if (vsi->info.pvid) {
  2129. aq_ret = I40E_ERR_PARAM;
  2130. goto error_param;
  2131. }
  2132. for (i = 0; i < vfl->num_elements; i++) {
  2133. i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
  2134. vf->num_vlan--;
  2135. if (test_bit(I40E_VF_STATE_UC_PROMISC, &vf->vf_states))
  2136. i40e_aq_set_vsi_uc_promisc_on_vlan(&pf->hw, vsi->seid,
  2137. false,
  2138. vfl->vlan_id[i],
  2139. NULL);
  2140. if (test_bit(I40E_VF_STATE_MC_PROMISC, &vf->vf_states))
  2141. i40e_aq_set_vsi_mc_promisc_on_vlan(&pf->hw, vsi->seid,
  2142. false,
  2143. vfl->vlan_id[i],
  2144. NULL);
  2145. }
  2146. error_param:
  2147. /* send the response to the VF */
  2148. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret);
  2149. }
  2150. /**
  2151. * i40e_vc_iwarp_msg
  2152. * @vf: pointer to the VF info
  2153. * @msg: pointer to the msg buffer
  2154. * @msglen: msg length
  2155. *
  2156. * called from the VF for the iwarp msgs
  2157. **/
  2158. static int i40e_vc_iwarp_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2159. {
  2160. struct i40e_pf *pf = vf->pf;
  2161. int abs_vf_id = vf->vf_id + pf->hw.func_caps.vf_base_id;
  2162. i40e_status aq_ret = 0;
  2163. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2164. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2165. aq_ret = I40E_ERR_PARAM;
  2166. goto error_param;
  2167. }
  2168. i40e_notify_client_of_vf_msg(pf->vsi[pf->lan_vsi], abs_vf_id,
  2169. msg, msglen);
  2170. error_param:
  2171. /* send the response to the VF */
  2172. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_IWARP,
  2173. aq_ret);
  2174. }
  2175. /**
  2176. * i40e_vc_iwarp_qvmap_msg
  2177. * @vf: pointer to the VF info
  2178. * @msg: pointer to the msg buffer
  2179. * @msglen: msg length
  2180. * @config: config qvmap or release it
  2181. *
  2182. * called from the VF for the iwarp msgs
  2183. **/
  2184. static int i40e_vc_iwarp_qvmap_msg(struct i40e_vf *vf, u8 *msg, u16 msglen,
  2185. bool config)
  2186. {
  2187. struct virtchnl_iwarp_qvlist_info *qvlist_info =
  2188. (struct virtchnl_iwarp_qvlist_info *)msg;
  2189. i40e_status aq_ret = 0;
  2190. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2191. !test_bit(I40E_VF_STATE_IWARPENA, &vf->vf_states)) {
  2192. aq_ret = I40E_ERR_PARAM;
  2193. goto error_param;
  2194. }
  2195. if (config) {
  2196. if (i40e_config_iwarp_qvlist(vf, qvlist_info))
  2197. aq_ret = I40E_ERR_PARAM;
  2198. } else {
  2199. i40e_release_iwarp_qvlist(vf);
  2200. }
  2201. error_param:
  2202. /* send the response to the VF */
  2203. return i40e_vc_send_resp_to_vf(vf,
  2204. config ? VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP :
  2205. VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP,
  2206. aq_ret);
  2207. }
  2208. /**
  2209. * i40e_vc_config_rss_key
  2210. * @vf: pointer to the VF info
  2211. * @msg: pointer to the msg buffer
  2212. * @msglen: msg length
  2213. *
  2214. * Configure the VF's RSS key
  2215. **/
  2216. static int i40e_vc_config_rss_key(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2217. {
  2218. struct virtchnl_rss_key *vrk =
  2219. (struct virtchnl_rss_key *)msg;
  2220. struct i40e_pf *pf = vf->pf;
  2221. struct i40e_vsi *vsi = NULL;
  2222. u16 vsi_id = vrk->vsi_id;
  2223. i40e_status aq_ret = 0;
  2224. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2225. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2226. (vrk->key_len != I40E_HKEY_ARRAY_SIZE)) {
  2227. aq_ret = I40E_ERR_PARAM;
  2228. goto err;
  2229. }
  2230. vsi = pf->vsi[vf->lan_vsi_idx];
  2231. aq_ret = i40e_config_rss(vsi, vrk->key, NULL, 0);
  2232. err:
  2233. /* send the response to the VF */
  2234. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY,
  2235. aq_ret);
  2236. }
  2237. /**
  2238. * i40e_vc_config_rss_lut
  2239. * @vf: pointer to the VF info
  2240. * @msg: pointer to the msg buffer
  2241. * @msglen: msg length
  2242. *
  2243. * Configure the VF's RSS LUT
  2244. **/
  2245. static int i40e_vc_config_rss_lut(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2246. {
  2247. struct virtchnl_rss_lut *vrl =
  2248. (struct virtchnl_rss_lut *)msg;
  2249. struct i40e_pf *pf = vf->pf;
  2250. struct i40e_vsi *vsi = NULL;
  2251. u16 vsi_id = vrl->vsi_id;
  2252. i40e_status aq_ret = 0;
  2253. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states) ||
  2254. !i40e_vc_isvalid_vsi_id(vf, vsi_id) ||
  2255. (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)) {
  2256. aq_ret = I40E_ERR_PARAM;
  2257. goto err;
  2258. }
  2259. vsi = pf->vsi[vf->lan_vsi_idx];
  2260. aq_ret = i40e_config_rss(vsi, NULL, vrl->lut, I40E_VF_HLUT_ARRAY_SIZE);
  2261. /* send the response to the VF */
  2262. err:
  2263. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT,
  2264. aq_ret);
  2265. }
  2266. /**
  2267. * i40e_vc_get_rss_hena
  2268. * @vf: pointer to the VF info
  2269. * @msg: pointer to the msg buffer
  2270. * @msglen: msg length
  2271. *
  2272. * Return the RSS HENA bits allowed by the hardware
  2273. **/
  2274. static int i40e_vc_get_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2275. {
  2276. struct virtchnl_rss_hena *vrh = NULL;
  2277. struct i40e_pf *pf = vf->pf;
  2278. i40e_status aq_ret = 0;
  2279. int len = 0;
  2280. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2281. aq_ret = I40E_ERR_PARAM;
  2282. goto err;
  2283. }
  2284. len = sizeof(struct virtchnl_rss_hena);
  2285. vrh = kzalloc(len, GFP_KERNEL);
  2286. if (!vrh) {
  2287. aq_ret = I40E_ERR_NO_MEMORY;
  2288. len = 0;
  2289. goto err;
  2290. }
  2291. vrh->hena = i40e_pf_get_default_rss_hena(pf);
  2292. err:
  2293. /* send the response back to the VF */
  2294. aq_ret = i40e_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_RSS_HENA_CAPS,
  2295. aq_ret, (u8 *)vrh, len);
  2296. kfree(vrh);
  2297. return aq_ret;
  2298. }
  2299. /**
  2300. * i40e_vc_set_rss_hena
  2301. * @vf: pointer to the VF info
  2302. * @msg: pointer to the msg buffer
  2303. * @msglen: msg length
  2304. *
  2305. * Set the RSS HENA bits for the VF
  2306. **/
  2307. static int i40e_vc_set_rss_hena(struct i40e_vf *vf, u8 *msg, u16 msglen)
  2308. {
  2309. struct virtchnl_rss_hena *vrh =
  2310. (struct virtchnl_rss_hena *)msg;
  2311. struct i40e_pf *pf = vf->pf;
  2312. struct i40e_hw *hw = &pf->hw;
  2313. i40e_status aq_ret = 0;
  2314. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2315. aq_ret = I40E_ERR_PARAM;
  2316. goto err;
  2317. }
  2318. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(0, vf->vf_id), (u32)vrh->hena);
  2319. i40e_write_rx_ctl(hw, I40E_VFQF_HENA1(1, vf->vf_id),
  2320. (u32)(vrh->hena >> 32));
  2321. /* send the response to the VF */
  2322. err:
  2323. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_SET_RSS_HENA, aq_ret);
  2324. }
  2325. /**
  2326. * i40e_vc_enable_vlan_stripping
  2327. * @vf: pointer to the VF info
  2328. * @msg: pointer to the msg buffer
  2329. * @msglen: msg length
  2330. *
  2331. * Enable vlan header stripping for the VF
  2332. **/
  2333. static int i40e_vc_enable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
  2334. u16 msglen)
  2335. {
  2336. struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
  2337. i40e_status aq_ret = 0;
  2338. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2339. aq_ret = I40E_ERR_PARAM;
  2340. goto err;
  2341. }
  2342. i40e_vlan_stripping_enable(vsi);
  2343. /* send the response to the VF */
  2344. err:
  2345. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
  2346. aq_ret);
  2347. }
  2348. /**
  2349. * i40e_vc_disable_vlan_stripping
  2350. * @vf: pointer to the VF info
  2351. * @msg: pointer to the msg buffer
  2352. * @msglen: msg length
  2353. *
  2354. * Disable vlan header stripping for the VF
  2355. **/
  2356. static int i40e_vc_disable_vlan_stripping(struct i40e_vf *vf, u8 *msg,
  2357. u16 msglen)
  2358. {
  2359. struct i40e_vsi *vsi = vf->pf->vsi[vf->lan_vsi_idx];
  2360. i40e_status aq_ret = 0;
  2361. if (!test_bit(I40E_VF_STATE_ACTIVE, &vf->vf_states)) {
  2362. aq_ret = I40E_ERR_PARAM;
  2363. goto err;
  2364. }
  2365. i40e_vlan_stripping_disable(vsi);
  2366. /* send the response to the VF */
  2367. err:
  2368. return i40e_vc_send_resp_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
  2369. aq_ret);
  2370. }
  2371. /**
  2372. * i40e_vc_process_vf_msg
  2373. * @pf: pointer to the PF structure
  2374. * @vf_id: source VF id
  2375. * @msg: pointer to the msg buffer
  2376. * @msglen: msg length
  2377. * @msghndl: msg handle
  2378. *
  2379. * called from the common aeq/arq handler to
  2380. * process request from VF
  2381. **/
  2382. int i40e_vc_process_vf_msg(struct i40e_pf *pf, s16 vf_id, u32 v_opcode,
  2383. u32 v_retval, u8 *msg, u16 msglen)
  2384. {
  2385. struct i40e_hw *hw = &pf->hw;
  2386. int local_vf_id = vf_id - (s16)hw->func_caps.vf_base_id;
  2387. struct i40e_vf *vf;
  2388. int ret;
  2389. pf->vf_aq_requests++;
  2390. if (local_vf_id >= pf->num_alloc_vfs)
  2391. return -EINVAL;
  2392. vf = &(pf->vf[local_vf_id]);
  2393. /* Check if VF is disabled. */
  2394. if (test_bit(I40E_VF_STATE_DISABLED, &vf->vf_states))
  2395. return I40E_ERR_PARAM;
  2396. /* perform basic checks on the msg */
  2397. ret = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
  2398. /* perform additional checks specific to this driver */
  2399. if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
  2400. struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
  2401. if (vrk->key_len != I40E_HKEY_ARRAY_SIZE)
  2402. ret = -EINVAL;
  2403. } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
  2404. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  2405. if (vrl->lut_entries != I40E_VF_HLUT_ARRAY_SIZE)
  2406. ret = -EINVAL;
  2407. }
  2408. if (ret) {
  2409. i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
  2410. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
  2411. local_vf_id, v_opcode, msglen);
  2412. switch (ret) {
  2413. case VIRTCHNL_ERR_PARAM:
  2414. return -EPERM;
  2415. default:
  2416. return -EINVAL;
  2417. }
  2418. }
  2419. switch (v_opcode) {
  2420. case VIRTCHNL_OP_VERSION:
  2421. ret = i40e_vc_get_version_msg(vf, msg);
  2422. break;
  2423. case VIRTCHNL_OP_GET_VF_RESOURCES:
  2424. ret = i40e_vc_get_vf_resources_msg(vf, msg);
  2425. break;
  2426. case VIRTCHNL_OP_RESET_VF:
  2427. i40e_vc_reset_vf_msg(vf);
  2428. ret = 0;
  2429. break;
  2430. case VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  2431. ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
  2432. break;
  2433. case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  2434. ret = i40e_vc_config_queues_msg(vf, msg, msglen);
  2435. break;
  2436. case VIRTCHNL_OP_CONFIG_IRQ_MAP:
  2437. ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
  2438. break;
  2439. case VIRTCHNL_OP_ENABLE_QUEUES:
  2440. ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
  2441. i40e_vc_notify_vf_link_state(vf);
  2442. break;
  2443. case VIRTCHNL_OP_DISABLE_QUEUES:
  2444. ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
  2445. break;
  2446. case VIRTCHNL_OP_ADD_ETH_ADDR:
  2447. ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
  2448. break;
  2449. case VIRTCHNL_OP_DEL_ETH_ADDR:
  2450. ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
  2451. break;
  2452. case VIRTCHNL_OP_ADD_VLAN:
  2453. ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
  2454. break;
  2455. case VIRTCHNL_OP_DEL_VLAN:
  2456. ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
  2457. break;
  2458. case VIRTCHNL_OP_GET_STATS:
  2459. ret = i40e_vc_get_stats_msg(vf, msg, msglen);
  2460. break;
  2461. case VIRTCHNL_OP_IWARP:
  2462. ret = i40e_vc_iwarp_msg(vf, msg, msglen);
  2463. break;
  2464. case VIRTCHNL_OP_CONFIG_IWARP_IRQ_MAP:
  2465. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, true);
  2466. break;
  2467. case VIRTCHNL_OP_RELEASE_IWARP_IRQ_MAP:
  2468. ret = i40e_vc_iwarp_qvmap_msg(vf, msg, msglen, false);
  2469. break;
  2470. case VIRTCHNL_OP_CONFIG_RSS_KEY:
  2471. ret = i40e_vc_config_rss_key(vf, msg, msglen);
  2472. break;
  2473. case VIRTCHNL_OP_CONFIG_RSS_LUT:
  2474. ret = i40e_vc_config_rss_lut(vf, msg, msglen);
  2475. break;
  2476. case VIRTCHNL_OP_GET_RSS_HENA_CAPS:
  2477. ret = i40e_vc_get_rss_hena(vf, msg, msglen);
  2478. break;
  2479. case VIRTCHNL_OP_SET_RSS_HENA:
  2480. ret = i40e_vc_set_rss_hena(vf, msg, msglen);
  2481. break;
  2482. case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
  2483. ret = i40e_vc_enable_vlan_stripping(vf, msg, msglen);
  2484. break;
  2485. case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
  2486. ret = i40e_vc_disable_vlan_stripping(vf, msg, msglen);
  2487. break;
  2488. case VIRTCHNL_OP_REQUEST_QUEUES:
  2489. ret = i40e_vc_request_queues_msg(vf, msg, msglen);
  2490. break;
  2491. case VIRTCHNL_OP_UNKNOWN:
  2492. default:
  2493. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  2494. v_opcode, local_vf_id);
  2495. ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
  2496. I40E_ERR_NOT_IMPLEMENTED);
  2497. break;
  2498. }
  2499. return ret;
  2500. }
  2501. /**
  2502. * i40e_vc_process_vflr_event
  2503. * @pf: pointer to the PF structure
  2504. *
  2505. * called from the vlfr irq handler to
  2506. * free up VF resources and state variables
  2507. **/
  2508. int i40e_vc_process_vflr_event(struct i40e_pf *pf)
  2509. {
  2510. struct i40e_hw *hw = &pf->hw;
  2511. u32 reg, reg_idx, bit_idx;
  2512. struct i40e_vf *vf;
  2513. int vf_id;
  2514. if (!test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
  2515. return 0;
  2516. /* Re-enable the VFLR interrupt cause here, before looking for which
  2517. * VF got reset. Otherwise, if another VF gets a reset while the
  2518. * first one is being processed, that interrupt will be lost, and
  2519. * that VF will be stuck in reset forever.
  2520. */
  2521. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  2522. reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
  2523. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  2524. i40e_flush(hw);
  2525. clear_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
  2526. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  2527. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  2528. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  2529. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  2530. vf = &pf->vf[vf_id];
  2531. reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
  2532. if (reg & BIT(bit_idx))
  2533. /* i40e_reset_vf will clear the bit in GLGEN_VFLRSTAT */
  2534. i40e_reset_vf(vf, true);
  2535. }
  2536. return 0;
  2537. }
  2538. /**
  2539. * i40e_ndo_set_vf_mac
  2540. * @netdev: network interface device structure
  2541. * @vf_id: VF identifier
  2542. * @mac: mac address
  2543. *
  2544. * program VF mac address
  2545. **/
  2546. int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  2547. {
  2548. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2549. struct i40e_vsi *vsi = np->vsi;
  2550. struct i40e_pf *pf = vsi->back;
  2551. struct i40e_mac_filter *f;
  2552. struct i40e_vf *vf;
  2553. int ret = 0;
  2554. struct hlist_node *h;
  2555. int bkt;
  2556. /* validate the request */
  2557. if (vf_id >= pf->num_alloc_vfs) {
  2558. dev_err(&pf->pdev->dev,
  2559. "Invalid VF Identifier %d\n", vf_id);
  2560. ret = -EINVAL;
  2561. goto error_param;
  2562. }
  2563. vf = &(pf->vf[vf_id]);
  2564. vsi = pf->vsi[vf->lan_vsi_idx];
  2565. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2566. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2567. vf_id);
  2568. ret = -EAGAIN;
  2569. goto error_param;
  2570. }
  2571. if (is_multicast_ether_addr(mac)) {
  2572. dev_err(&pf->pdev->dev,
  2573. "Invalid Ethernet address %pM for VF %d\n", mac, vf_id);
  2574. ret = -EINVAL;
  2575. goto error_param;
  2576. }
  2577. /* Lock once because below invoked function add/del_filter requires
  2578. * mac_filter_hash_lock to be held
  2579. */
  2580. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2581. /* delete the temporary mac address */
  2582. if (!is_zero_ether_addr(vf->default_lan_addr.addr))
  2583. i40e_del_mac_filter(vsi, vf->default_lan_addr.addr);
  2584. /* Delete all the filters for this VSI - we're going to kill it
  2585. * anyway.
  2586. */
  2587. hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
  2588. __i40e_del_filter(vsi, f);
  2589. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2590. /* program mac filter */
  2591. if (i40e_sync_vsi_filters(vsi)) {
  2592. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  2593. ret = -EIO;
  2594. goto error_param;
  2595. }
  2596. ether_addr_copy(vf->default_lan_addr.addr, mac);
  2597. if (is_zero_ether_addr(mac)) {
  2598. vf->pf_set_mac = false;
  2599. dev_info(&pf->pdev->dev, "Removing MAC on VF %d\n", vf_id);
  2600. } else {
  2601. vf->pf_set_mac = true;
  2602. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n",
  2603. mac, vf_id);
  2604. }
  2605. /* Force the VF driver stop so it has to reload with new MAC address */
  2606. i40e_vc_disable_vf(vf);
  2607. dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
  2608. error_param:
  2609. return ret;
  2610. }
  2611. /**
  2612. * i40e_vsi_has_vlans - True if VSI has configured VLANs
  2613. * @vsi: pointer to the vsi
  2614. *
  2615. * Check if a VSI has configured any VLANs. False if we have a port VLAN or if
  2616. * we have no configured VLANs. Do not call while holding the
  2617. * mac_filter_hash_lock.
  2618. */
  2619. static bool i40e_vsi_has_vlans(struct i40e_vsi *vsi)
  2620. {
  2621. bool have_vlans;
  2622. /* If we have a port VLAN, then the VSI cannot have any VLANs
  2623. * configured, as all MAC/VLAN filters will be assigned to the PVID.
  2624. */
  2625. if (vsi->info.pvid)
  2626. return false;
  2627. /* Since we don't have a PVID, we know that if the device is in VLAN
  2628. * mode it must be because of a VLAN filter configured on this VSI.
  2629. */
  2630. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2631. have_vlans = i40e_is_vsi_in_vlan(vsi);
  2632. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2633. return have_vlans;
  2634. }
  2635. /**
  2636. * i40e_ndo_set_vf_port_vlan
  2637. * @netdev: network interface device structure
  2638. * @vf_id: VF identifier
  2639. * @vlan_id: mac address
  2640. * @qos: priority setting
  2641. * @vlan_proto: vlan protocol
  2642. *
  2643. * program VF vlan id and/or qos
  2644. **/
  2645. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev, int vf_id,
  2646. u16 vlan_id, u8 qos, __be16 vlan_proto)
  2647. {
  2648. u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
  2649. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2650. struct i40e_pf *pf = np->vsi->back;
  2651. struct i40e_vsi *vsi;
  2652. struct i40e_vf *vf;
  2653. int ret = 0;
  2654. /* validate the request */
  2655. if (vf_id >= pf->num_alloc_vfs) {
  2656. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2657. ret = -EINVAL;
  2658. goto error_pvid;
  2659. }
  2660. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  2661. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  2662. ret = -EINVAL;
  2663. goto error_pvid;
  2664. }
  2665. if (vlan_proto != htons(ETH_P_8021Q)) {
  2666. dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
  2667. ret = -EPROTONOSUPPORT;
  2668. goto error_pvid;
  2669. }
  2670. vf = &(pf->vf[vf_id]);
  2671. vsi = pf->vsi[vf->lan_vsi_idx];
  2672. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2673. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2674. vf_id);
  2675. ret = -EAGAIN;
  2676. goto error_pvid;
  2677. }
  2678. if (le16_to_cpu(vsi->info.pvid) == vlanprio)
  2679. /* duplicate request, so just return success */
  2680. goto error_pvid;
  2681. if (i40e_vsi_has_vlans(vsi)) {
  2682. dev_err(&pf->pdev->dev,
  2683. "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",
  2684. vf_id);
  2685. /* Administrator Error - knock the VF offline until he does
  2686. * the right thing by reconfiguring his network correctly
  2687. * and then reloading the VF driver.
  2688. */
  2689. i40e_vc_disable_vf(vf);
  2690. /* During reset the VF got a new VSI, so refresh the pointer. */
  2691. vsi = pf->vsi[vf->lan_vsi_idx];
  2692. }
  2693. /* Locked once because multiple functions below iterate list */
  2694. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2695. /* Check for condition where there was already a port VLAN ID
  2696. * filter set and now it is being deleted by setting it to zero.
  2697. * Additionally check for the condition where there was a port
  2698. * VLAN but now there is a new and different port VLAN being set.
  2699. * Before deleting all the old VLAN filters we must add new ones
  2700. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  2701. * MAC addresses deleted.
  2702. */
  2703. if ((!(vlan_id || qos) ||
  2704. vlanprio != le16_to_cpu(vsi->info.pvid)) &&
  2705. vsi->info.pvid) {
  2706. ret = i40e_add_vlan_all_mac(vsi, I40E_VLAN_ANY);
  2707. if (ret) {
  2708. dev_info(&vsi->back->pdev->dev,
  2709. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  2710. vsi->back->hw.aq.asq_last_status);
  2711. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2712. goto error_pvid;
  2713. }
  2714. }
  2715. if (vsi->info.pvid) {
  2716. /* remove all filters on the old VLAN */
  2717. i40e_rm_vlan_all_mac(vsi, (le16_to_cpu(vsi->info.pvid) &
  2718. VLAN_VID_MASK));
  2719. }
  2720. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2721. if (vlan_id || qos)
  2722. ret = i40e_vsi_add_pvid(vsi, vlanprio);
  2723. else
  2724. i40e_vsi_remove_pvid(vsi);
  2725. spin_lock_bh(&vsi->mac_filter_hash_lock);
  2726. if (vlan_id) {
  2727. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  2728. vlan_id, qos, vf_id);
  2729. /* add new VLAN filter for each MAC */
  2730. ret = i40e_add_vlan_all_mac(vsi, vlan_id);
  2731. if (ret) {
  2732. dev_info(&vsi->back->pdev->dev,
  2733. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  2734. vsi->back->hw.aq.asq_last_status);
  2735. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2736. goto error_pvid;
  2737. }
  2738. /* remove the previously added non-VLAN MAC filters */
  2739. i40e_rm_vlan_all_mac(vsi, I40E_VLAN_ANY);
  2740. }
  2741. spin_unlock_bh(&vsi->mac_filter_hash_lock);
  2742. /* Schedule the worker thread to take care of applying changes */
  2743. i40e_service_event_schedule(vsi->back);
  2744. if (ret) {
  2745. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  2746. goto error_pvid;
  2747. }
  2748. /* The Port VLAN needs to be saved across resets the same as the
  2749. * default LAN MAC address.
  2750. */
  2751. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  2752. ret = 0;
  2753. error_pvid:
  2754. return ret;
  2755. }
  2756. /**
  2757. * i40e_ndo_set_vf_bw
  2758. * @netdev: network interface device structure
  2759. * @vf_id: VF identifier
  2760. * @tx_rate: Tx rate
  2761. *
  2762. * configure VF Tx rate
  2763. **/
  2764. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  2765. int max_tx_rate)
  2766. {
  2767. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2768. struct i40e_pf *pf = np->vsi->back;
  2769. struct i40e_vsi *vsi;
  2770. struct i40e_vf *vf;
  2771. int ret = 0;
  2772. /* validate the request */
  2773. if (vf_id >= pf->num_alloc_vfs) {
  2774. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
  2775. ret = -EINVAL;
  2776. goto error;
  2777. }
  2778. if (min_tx_rate) {
  2779. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
  2780. min_tx_rate, vf_id);
  2781. return -EINVAL;
  2782. }
  2783. vf = &(pf->vf[vf_id]);
  2784. vsi = pf->vsi[vf->lan_vsi_idx];
  2785. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2786. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2787. vf_id);
  2788. ret = -EAGAIN;
  2789. goto error;
  2790. }
  2791. ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
  2792. if (ret)
  2793. goto error;
  2794. vf->tx_rate = max_tx_rate;
  2795. error:
  2796. return ret;
  2797. }
  2798. /**
  2799. * i40e_ndo_get_vf_config
  2800. * @netdev: network interface device structure
  2801. * @vf_id: VF identifier
  2802. * @ivi: VF configuration structure
  2803. *
  2804. * return VF configuration
  2805. **/
  2806. int i40e_ndo_get_vf_config(struct net_device *netdev,
  2807. int vf_id, struct ifla_vf_info *ivi)
  2808. {
  2809. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2810. struct i40e_vsi *vsi = np->vsi;
  2811. struct i40e_pf *pf = vsi->back;
  2812. struct i40e_vf *vf;
  2813. int ret = 0;
  2814. /* validate the request */
  2815. if (vf_id >= pf->num_alloc_vfs) {
  2816. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2817. ret = -EINVAL;
  2818. goto error_param;
  2819. }
  2820. vf = &(pf->vf[vf_id]);
  2821. /* first vsi is always the LAN vsi */
  2822. vsi = pf->vsi[vf->lan_vsi_idx];
  2823. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2824. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2825. vf_id);
  2826. ret = -EAGAIN;
  2827. goto error_param;
  2828. }
  2829. ivi->vf = vf_id;
  2830. ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
  2831. ivi->max_tx_rate = vf->tx_rate;
  2832. ivi->min_tx_rate = 0;
  2833. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  2834. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  2835. I40E_VLAN_PRIORITY_SHIFT;
  2836. if (vf->link_forced == false)
  2837. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2838. else if (vf->link_up == true)
  2839. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2840. else
  2841. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2842. ivi->spoofchk = vf->spoofchk;
  2843. ivi->trusted = vf->trusted;
  2844. ret = 0;
  2845. error_param:
  2846. return ret;
  2847. }
  2848. /**
  2849. * i40e_ndo_set_vf_link_state
  2850. * @netdev: network interface device structure
  2851. * @vf_id: VF identifier
  2852. * @link: required link state
  2853. *
  2854. * Set the link state of a specified VF, regardless of physical link state
  2855. **/
  2856. int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
  2857. {
  2858. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2859. struct i40e_pf *pf = np->vsi->back;
  2860. struct virtchnl_pf_event pfe;
  2861. struct i40e_hw *hw = &pf->hw;
  2862. struct i40e_vf *vf;
  2863. int abs_vf_id;
  2864. int ret = 0;
  2865. /* validate the request */
  2866. if (vf_id >= pf->num_alloc_vfs) {
  2867. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2868. ret = -EINVAL;
  2869. goto error_out;
  2870. }
  2871. vf = &pf->vf[vf_id];
  2872. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  2873. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  2874. pfe.severity = PF_EVENT_SEVERITY_INFO;
  2875. switch (link) {
  2876. case IFLA_VF_LINK_STATE_AUTO:
  2877. vf->link_forced = false;
  2878. pfe.event_data.link_event.link_status =
  2879. pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
  2880. pfe.event_data.link_event.link_speed =
  2881. (enum virtchnl_link_speed)
  2882. pf->hw.phy.link_info.link_speed;
  2883. break;
  2884. case IFLA_VF_LINK_STATE_ENABLE:
  2885. vf->link_forced = true;
  2886. vf->link_up = true;
  2887. pfe.event_data.link_event.link_status = true;
  2888. pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
  2889. break;
  2890. case IFLA_VF_LINK_STATE_DISABLE:
  2891. vf->link_forced = true;
  2892. vf->link_up = false;
  2893. pfe.event_data.link_event.link_status = false;
  2894. pfe.event_data.link_event.link_speed = 0;
  2895. break;
  2896. default:
  2897. ret = -EINVAL;
  2898. goto error_out;
  2899. }
  2900. /* Notify the VF of its new link state */
  2901. i40e_aq_send_msg_to_vf(hw, abs_vf_id, VIRTCHNL_OP_EVENT,
  2902. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  2903. error_out:
  2904. return ret;
  2905. }
  2906. /**
  2907. * i40e_ndo_set_vf_spoofchk
  2908. * @netdev: network interface device structure
  2909. * @vf_id: VF identifier
  2910. * @enable: flag to enable or disable feature
  2911. *
  2912. * Enable or disable VF spoof checking
  2913. **/
  2914. int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
  2915. {
  2916. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2917. struct i40e_vsi *vsi = np->vsi;
  2918. struct i40e_pf *pf = vsi->back;
  2919. struct i40e_vsi_context ctxt;
  2920. struct i40e_hw *hw = &pf->hw;
  2921. struct i40e_vf *vf;
  2922. int ret = 0;
  2923. /* validate the request */
  2924. if (vf_id >= pf->num_alloc_vfs) {
  2925. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2926. ret = -EINVAL;
  2927. goto out;
  2928. }
  2929. vf = &(pf->vf[vf_id]);
  2930. if (!test_bit(I40E_VF_STATE_INIT, &vf->vf_states)) {
  2931. dev_err(&pf->pdev->dev, "VF %d still in reset. Try again.\n",
  2932. vf_id);
  2933. ret = -EAGAIN;
  2934. goto out;
  2935. }
  2936. if (enable == vf->spoofchk)
  2937. goto out;
  2938. vf->spoofchk = enable;
  2939. memset(&ctxt, 0, sizeof(ctxt));
  2940. ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
  2941. ctxt.pf_num = pf->hw.pf_id;
  2942. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  2943. if (enable)
  2944. ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  2945. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  2946. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  2947. if (ret) {
  2948. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  2949. ret);
  2950. ret = -EIO;
  2951. }
  2952. out:
  2953. return ret;
  2954. }
  2955. /**
  2956. * i40e_ndo_set_vf_trust
  2957. * @netdev: network interface device structure of the pf
  2958. * @vf_id: VF identifier
  2959. * @setting: trust setting
  2960. *
  2961. * Enable or disable VF trust setting
  2962. **/
  2963. int i40e_ndo_set_vf_trust(struct net_device *netdev, int vf_id, bool setting)
  2964. {
  2965. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2966. struct i40e_pf *pf = np->vsi->back;
  2967. struct i40e_vf *vf;
  2968. int ret = 0;
  2969. /* validate the request */
  2970. if (vf_id >= pf->num_alloc_vfs) {
  2971. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2972. return -EINVAL;
  2973. }
  2974. if (pf->flags & I40E_FLAG_MFP_ENABLED) {
  2975. dev_err(&pf->pdev->dev, "Trusted VF not supported in MFP mode.\n");
  2976. return -EINVAL;
  2977. }
  2978. vf = &pf->vf[vf_id];
  2979. if (setting == vf->trusted)
  2980. goto out;
  2981. vf->trusted = setting;
  2982. i40e_vc_disable_vf(vf);
  2983. dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
  2984. vf_id, setting ? "" : "un");
  2985. out:
  2986. return ret;
  2987. }