ice_virtchnl_pf.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (c) 2018, Intel Corporation. */
  3. #include "ice.h"
  4. #include "ice_lib.h"
  5. /**
  6. * ice_vc_vf_broadcast - Broadcast a message to all VFs on PF
  7. * @pf: pointer to the PF structure
  8. * @v_opcode: operation code
  9. * @v_retval: return value
  10. * @msg: pointer to the msg buffer
  11. * @msglen: msg length
  12. */
  13. static void
  14. ice_vc_vf_broadcast(struct ice_pf *pf, enum virtchnl_ops v_opcode,
  15. enum ice_status v_retval, u8 *msg, u16 msglen)
  16. {
  17. struct ice_hw *hw = &pf->hw;
  18. struct ice_vf *vf = pf->vf;
  19. int i;
  20. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  21. /* Not all vfs are enabled so skip the ones that are not */
  22. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
  23. !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
  24. continue;
  25. /* Ignore return value on purpose - a given VF may fail, but
  26. * we need to keep going and send to all of them
  27. */
  28. ice_aq_send_msg_to_vf(hw, vf->vf_id, v_opcode, v_retval, msg,
  29. msglen, NULL);
  30. }
  31. }
  32. /**
  33. * ice_set_pfe_link - Set the link speed/status of the virtchnl_pf_event
  34. * @vf: pointer to the VF structure
  35. * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
  36. * @ice_link_speed: link speed specified by ICE_AQ_LINK_SPEED_*
  37. * @link_up: whether or not to set the link up/down
  38. */
  39. static void
  40. ice_set_pfe_link(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
  41. int ice_link_speed, bool link_up)
  42. {
  43. if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED) {
  44. pfe->event_data.link_event_adv.link_status = link_up;
  45. /* Speed in Mbps */
  46. pfe->event_data.link_event_adv.link_speed =
  47. ice_conv_link_speed_to_virtchnl(true, ice_link_speed);
  48. } else {
  49. pfe->event_data.link_event.link_status = link_up;
  50. /* Legacy method for virtchnl link speeds */
  51. pfe->event_data.link_event.link_speed =
  52. (enum virtchnl_link_speed)
  53. ice_conv_link_speed_to_virtchnl(false, ice_link_speed);
  54. }
  55. }
  56. /**
  57. * ice_set_pfe_link_forced - Force the virtchnl_pf_event link speed/status
  58. * @vf: pointer to the VF structure
  59. * @pfe: pointer to the virtchnl_pf_event to set link speed/status for
  60. * @link_up: whether or not to set the link up/down
  61. */
  62. static void
  63. ice_set_pfe_link_forced(struct ice_vf *vf, struct virtchnl_pf_event *pfe,
  64. bool link_up)
  65. {
  66. u16 link_speed;
  67. if (link_up)
  68. link_speed = ICE_AQ_LINK_SPEED_40GB;
  69. else
  70. link_speed = ICE_AQ_LINK_SPEED_UNKNOWN;
  71. ice_set_pfe_link(vf, pfe, link_speed, link_up);
  72. }
  73. /**
  74. * ice_vc_notify_vf_link_state - Inform a VF of link status
  75. * @vf: pointer to the VF structure
  76. *
  77. * send a link status message to a single VF
  78. */
  79. static void ice_vc_notify_vf_link_state(struct ice_vf *vf)
  80. {
  81. struct virtchnl_pf_event pfe = { 0 };
  82. struct ice_link_status *ls;
  83. struct ice_pf *pf = vf->pf;
  84. struct ice_hw *hw;
  85. hw = &pf->hw;
  86. ls = &hw->port_info->phy.link_info;
  87. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  88. pfe.severity = PF_EVENT_SEVERITY_INFO;
  89. if (vf->link_forced)
  90. ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
  91. else
  92. ice_set_pfe_link(vf, &pfe, ls->link_speed, ls->link_info &
  93. ICE_AQ_LINK_UP);
  94. ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT, 0, (u8 *)&pfe,
  95. sizeof(pfe), NULL);
  96. }
  97. /**
  98. * ice_get_vf_vector - get VF interrupt vector register offset
  99. * @vf_msix: number of MSIx vector per VF on a PF
  100. * @vf_id: VF identifier
  101. * @i: index of MSIx vector
  102. */
  103. static u32 ice_get_vf_vector(int vf_msix, int vf_id, int i)
  104. {
  105. return ((i == 0) ? VFINT_DYN_CTLN(vf_id) :
  106. VFINT_DYN_CTLN(((vf_msix - 1) * (vf_id)) + (i - 1)));
  107. }
  108. /**
  109. * ice_free_vf_res - Free a VF's resources
  110. * @vf: pointer to the VF info
  111. */
  112. static void ice_free_vf_res(struct ice_vf *vf)
  113. {
  114. struct ice_pf *pf = vf->pf;
  115. int i, pf_vf_msix;
  116. /* First, disable VF's configuration API to prevent OS from
  117. * accessing the VF's VSI after it's freed or invalidated.
  118. */
  119. clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
  120. /* free vsi & disconnect it from the parent uplink */
  121. if (vf->lan_vsi_idx) {
  122. ice_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  123. vf->lan_vsi_idx = 0;
  124. vf->lan_vsi_num = 0;
  125. vf->num_mac = 0;
  126. }
  127. pf_vf_msix = pf->num_vf_msix;
  128. /* Disable interrupts so that VF starts in a known state */
  129. for (i = 0; i < pf_vf_msix; i++) {
  130. u32 reg_idx;
  131. reg_idx = ice_get_vf_vector(pf_vf_msix, vf->vf_id, i);
  132. wr32(&pf->hw, reg_idx, VFINT_DYN_CTLN_CLEARPBA_M);
  133. ice_flush(&pf->hw);
  134. }
  135. /* reset some of the state variables keeping track of the resources */
  136. clear_bit(ICE_VF_STATE_MC_PROMISC, vf->vf_states);
  137. clear_bit(ICE_VF_STATE_UC_PROMISC, vf->vf_states);
  138. }
  139. /***********************enable_vf routines*****************************/
  140. /**
  141. * ice_dis_vf_mappings
  142. * @vf: pointer to the VF structure
  143. */
  144. static void ice_dis_vf_mappings(struct ice_vf *vf)
  145. {
  146. struct ice_pf *pf = vf->pf;
  147. struct ice_vsi *vsi;
  148. int first, last, v;
  149. struct ice_hw *hw;
  150. hw = &pf->hw;
  151. vsi = pf->vsi[vf->lan_vsi_idx];
  152. wr32(hw, VPINT_ALLOC(vf->vf_id), 0);
  153. first = vf->first_vector_idx;
  154. last = first + pf->num_vf_msix - 1;
  155. for (v = first; v <= last; v++) {
  156. u32 reg;
  157. reg = (((1 << GLINT_VECT2FUNC_IS_PF_S) &
  158. GLINT_VECT2FUNC_IS_PF_M) |
  159. ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
  160. GLINT_VECT2FUNC_PF_NUM_M));
  161. wr32(hw, GLINT_VECT2FUNC(v), reg);
  162. }
  163. if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG)
  164. wr32(hw, VPLAN_TX_QBASE(vf->vf_id), 0);
  165. else
  166. dev_err(&pf->pdev->dev,
  167. "Scattered mode for VF Tx queues is not yet implemented\n");
  168. if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG)
  169. wr32(hw, VPLAN_RX_QBASE(vf->vf_id), 0);
  170. else
  171. dev_err(&pf->pdev->dev,
  172. "Scattered mode for VF Rx queues is not yet implemented\n");
  173. }
  174. /**
  175. * ice_free_vfs - Free all VFs
  176. * @pf: pointer to the PF structure
  177. */
  178. void ice_free_vfs(struct ice_pf *pf)
  179. {
  180. struct ice_hw *hw = &pf->hw;
  181. int tmp, i;
  182. if (!pf->vf)
  183. return;
  184. while (test_and_set_bit(__ICE_VF_DIS, pf->state))
  185. usleep_range(1000, 2000);
  186. /* Avoid wait time by stopping all VFs at the same time */
  187. for (i = 0; i < pf->num_alloc_vfs; i++) {
  188. if (!test_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states))
  189. continue;
  190. /* stop rings without wait time */
  191. ice_vsi_stop_tx_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
  192. ICE_NO_RESET, i);
  193. ice_vsi_stop_rx_rings(pf->vsi[pf->vf[i].lan_vsi_idx]);
  194. clear_bit(ICE_VF_STATE_ENA, pf->vf[i].vf_states);
  195. }
  196. /* Disable IOV before freeing resources. This lets any VF drivers
  197. * running in the host get themselves cleaned up before we yank
  198. * the carpet out from underneath their feet.
  199. */
  200. if (!pci_vfs_assigned(pf->pdev))
  201. pci_disable_sriov(pf->pdev);
  202. else
  203. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  204. tmp = pf->num_alloc_vfs;
  205. pf->num_vf_qps = 0;
  206. pf->num_alloc_vfs = 0;
  207. for (i = 0; i < tmp; i++) {
  208. if (test_bit(ICE_VF_STATE_INIT, pf->vf[i].vf_states)) {
  209. /* disable VF qp mappings */
  210. ice_dis_vf_mappings(&pf->vf[i]);
  211. /* Set this state so that assigned VF vectors can be
  212. * reclaimed by PF for reuse in ice_vsi_release(). No
  213. * need to clear this bit since pf->vf array is being
  214. * freed anyways after this for loop
  215. */
  216. set_bit(ICE_VF_STATE_CFG_INTR, pf->vf[i].vf_states);
  217. ice_free_vf_res(&pf->vf[i]);
  218. }
  219. }
  220. devm_kfree(&pf->pdev->dev, pf->vf);
  221. pf->vf = NULL;
  222. /* This check is for when the driver is unloaded while VFs are
  223. * assigned. Setting the number of VFs to 0 through sysfs is caught
  224. * before this function ever gets called.
  225. */
  226. if (!pci_vfs_assigned(pf->pdev)) {
  227. int vf_id;
  228. /* Acknowledge VFLR for all VFs. Without this, VFs will fail to
  229. * work correctly when SR-IOV gets re-enabled.
  230. */
  231. for (vf_id = 0; vf_id < tmp; vf_id++) {
  232. u32 reg_idx, bit_idx;
  233. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  234. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  235. wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  236. }
  237. }
  238. clear_bit(__ICE_VF_DIS, pf->state);
  239. clear_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
  240. }
  241. /**
  242. * ice_trigger_vf_reset - Reset a VF on HW
  243. * @vf: pointer to the VF structure
  244. * @is_vflr: true if VFLR was issued, false if not
  245. *
  246. * Trigger hardware to start a reset for a particular VF. Expects the caller
  247. * to wait the proper amount of time to allow hardware to reset the VF before
  248. * it cleans up and restores VF functionality.
  249. */
  250. static void ice_trigger_vf_reset(struct ice_vf *vf, bool is_vflr)
  251. {
  252. struct ice_pf *pf = vf->pf;
  253. u32 reg, reg_idx, bit_idx;
  254. struct ice_hw *hw;
  255. int vf_abs_id, i;
  256. hw = &pf->hw;
  257. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  258. /* Inform VF that it is no longer active, as a warning */
  259. clear_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  260. /* Disable VF's configuration API during reset. The flag is re-enabled
  261. * in ice_alloc_vf_res(), when it's safe again to access VF's VSI.
  262. * It's normally disabled in ice_free_vf_res(), but it's safer
  263. * to do it earlier to give some time to finish to any VF config
  264. * functions that may still be running at this point.
  265. */
  266. clear_bit(ICE_VF_STATE_INIT, vf->vf_states);
  267. /* In the case of a VFLR, the HW has already reset the VF and we
  268. * just need to clean up, so don't hit the VFRTRIG register.
  269. */
  270. if (!is_vflr) {
  271. /* reset VF using VPGEN_VFRTRIG reg */
  272. reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
  273. reg |= VPGEN_VFRTRIG_VFSWR_M;
  274. wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
  275. }
  276. /* clear the VFLR bit in GLGEN_VFLRSTAT */
  277. reg_idx = (vf_abs_id) / 32;
  278. bit_idx = (vf_abs_id) % 32;
  279. wr32(hw, GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  280. ice_flush(hw);
  281. wr32(hw, PF_PCI_CIAA,
  282. VF_DEVICE_STATUS | (vf_abs_id << PF_PCI_CIAA_VF_NUM_S));
  283. for (i = 0; i < 100; i++) {
  284. reg = rd32(hw, PF_PCI_CIAD);
  285. if ((reg & VF_TRANS_PENDING_M) != 0)
  286. dev_err(&pf->pdev->dev,
  287. "VF %d PCI transactions stuck\n", vf->vf_id);
  288. udelay(1);
  289. }
  290. }
  291. /**
  292. * ice_vsi_set_pvid - Set port VLAN id for the VSI
  293. * @vsi: the VSI being changed
  294. * @vid: the VLAN id to set as a PVID
  295. */
  296. static int ice_vsi_set_pvid(struct ice_vsi *vsi, u16 vid)
  297. {
  298. struct device *dev = &vsi->back->pdev->dev;
  299. struct ice_hw *hw = &vsi->back->hw;
  300. struct ice_vsi_ctx ctxt = { 0 };
  301. enum ice_status status;
  302. ctxt.info.vlan_flags = ICE_AQ_VSI_VLAN_MODE_TAGGED |
  303. ICE_AQ_VSI_PVLAN_INSERT_PVID |
  304. ICE_AQ_VSI_VLAN_EMOD_STR;
  305. ctxt.info.pvid = cpu_to_le16(vid);
  306. ctxt.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_VLAN_VALID);
  307. status = ice_update_vsi(hw, vsi->idx, &ctxt, NULL);
  308. if (status) {
  309. dev_info(dev, "update VSI for VLAN insert failed, err %d aq_err %d\n",
  310. status, hw->adminq.sq_last_status);
  311. return -EIO;
  312. }
  313. vsi->info.pvid = ctxt.info.pvid;
  314. vsi->info.vlan_flags = ctxt.info.vlan_flags;
  315. return 0;
  316. }
  317. /**
  318. * ice_vsi_kill_pvid - Remove port VLAN id from the VSI
  319. * @vsi: the VSI being changed
  320. */
  321. static int ice_vsi_kill_pvid(struct ice_vsi *vsi)
  322. {
  323. struct ice_pf *pf = vsi->back;
  324. if (ice_vsi_manage_vlan_stripping(vsi, false)) {
  325. dev_err(&pf->pdev->dev, "Error removing Port VLAN on VSI %i\n",
  326. vsi->vsi_num);
  327. return -ENODEV;
  328. }
  329. vsi->info.pvid = 0;
  330. return 0;
  331. }
  332. /**
  333. * ice_vf_vsi_setup - Set up a VF VSI
  334. * @pf: board private structure
  335. * @pi: pointer to the port_info instance
  336. * @vf_id: defines VF id to which this VSI connects.
  337. *
  338. * Returns pointer to the successfully allocated VSI struct on success,
  339. * otherwise returns NULL on failure.
  340. */
  341. static struct ice_vsi *
  342. ice_vf_vsi_setup(struct ice_pf *pf, struct ice_port_info *pi, u16 vf_id)
  343. {
  344. return ice_vsi_setup(pf, pi, ICE_VSI_VF, vf_id);
  345. }
  346. /**
  347. * ice_alloc_vsi_res - Setup VF VSI and its resources
  348. * @vf: pointer to the VF structure
  349. *
  350. * Returns 0 on success, negative value on failure
  351. */
  352. static int ice_alloc_vsi_res(struct ice_vf *vf)
  353. {
  354. struct ice_pf *pf = vf->pf;
  355. LIST_HEAD(tmp_add_list);
  356. u8 broadcast[ETH_ALEN];
  357. struct ice_vsi *vsi;
  358. int status = 0;
  359. vsi = ice_vf_vsi_setup(pf, pf->hw.port_info, vf->vf_id);
  360. if (!vsi) {
  361. dev_err(&pf->pdev->dev, "Failed to create VF VSI\n");
  362. return -ENOMEM;
  363. }
  364. vf->lan_vsi_idx = vsi->idx;
  365. vf->lan_vsi_num = vsi->vsi_num;
  366. /* first vector index is the VFs OICR index */
  367. vf->first_vector_idx = vsi->hw_base_vector;
  368. /* Since hw_base_vector holds the vector where data queue interrupts
  369. * starts, increment by 1 since VFs allocated vectors include OICR intr
  370. * as well.
  371. */
  372. vsi->hw_base_vector += 1;
  373. /* Check if port VLAN exist before, and restore it accordingly */
  374. if (vf->port_vlan_id)
  375. ice_vsi_set_pvid(vsi, vf->port_vlan_id);
  376. eth_broadcast_addr(broadcast);
  377. status = ice_add_mac_to_list(vsi, &tmp_add_list, broadcast);
  378. if (status)
  379. goto ice_alloc_vsi_res_exit;
  380. if (is_valid_ether_addr(vf->dflt_lan_addr.addr)) {
  381. status = ice_add_mac_to_list(vsi, &tmp_add_list,
  382. vf->dflt_lan_addr.addr);
  383. if (status)
  384. goto ice_alloc_vsi_res_exit;
  385. }
  386. status = ice_add_mac(&pf->hw, &tmp_add_list);
  387. if (status)
  388. dev_err(&pf->pdev->dev, "could not add mac filters\n");
  389. /* Clear this bit after VF initialization since we shouldn't reclaim
  390. * and reassign interrupts for synchronous or asynchronous VFR events.
  391. * We don't want to reconfigure interrupts since AVF driver doesn't
  392. * expect vector assignment to be changed unless there is a request for
  393. * more vectors.
  394. */
  395. clear_bit(ICE_VF_STATE_CFG_INTR, vf->vf_states);
  396. ice_alloc_vsi_res_exit:
  397. ice_free_fltr_list(&pf->pdev->dev, &tmp_add_list);
  398. return status;
  399. }
  400. /**
  401. * ice_alloc_vf_res - Allocate VF resources
  402. * @vf: pointer to the VF structure
  403. */
  404. static int ice_alloc_vf_res(struct ice_vf *vf)
  405. {
  406. int status;
  407. /* setup VF VSI and necessary resources */
  408. status = ice_alloc_vsi_res(vf);
  409. if (status)
  410. goto ice_alloc_vf_res_exit;
  411. if (vf->trusted)
  412. set_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  413. else
  414. clear_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  415. /* VF is now completely initialized */
  416. set_bit(ICE_VF_STATE_INIT, vf->vf_states);
  417. return status;
  418. ice_alloc_vf_res_exit:
  419. ice_free_vf_res(vf);
  420. return status;
  421. }
  422. /**
  423. * ice_ena_vf_mappings
  424. * @vf: pointer to the VF structure
  425. *
  426. * Enable VF vectors and queues allocation by writing the details into
  427. * respective registers.
  428. */
  429. static void ice_ena_vf_mappings(struct ice_vf *vf)
  430. {
  431. struct ice_pf *pf = vf->pf;
  432. struct ice_vsi *vsi;
  433. int first, last, v;
  434. struct ice_hw *hw;
  435. int abs_vf_id;
  436. u32 reg;
  437. hw = &pf->hw;
  438. vsi = pf->vsi[vf->lan_vsi_idx];
  439. first = vf->first_vector_idx;
  440. last = (first + pf->num_vf_msix) - 1;
  441. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  442. /* VF Vector allocation */
  443. reg = (((first << VPINT_ALLOC_FIRST_S) & VPINT_ALLOC_FIRST_M) |
  444. ((last << VPINT_ALLOC_LAST_S) & VPINT_ALLOC_LAST_M) |
  445. VPINT_ALLOC_VALID_M);
  446. wr32(hw, VPINT_ALLOC(vf->vf_id), reg);
  447. /* map the interrupts to its functions */
  448. for (v = first; v <= last; v++) {
  449. reg = (((abs_vf_id << GLINT_VECT2FUNC_VF_NUM_S) &
  450. GLINT_VECT2FUNC_VF_NUM_M) |
  451. ((hw->pf_id << GLINT_VECT2FUNC_PF_NUM_S) &
  452. GLINT_VECT2FUNC_PF_NUM_M));
  453. wr32(hw, GLINT_VECT2FUNC(v), reg);
  454. }
  455. /* VF Tx queues allocation */
  456. if (vsi->tx_mapping_mode == ICE_VSI_MAP_CONTIG) {
  457. wr32(hw, VPLAN_TXQ_MAPENA(vf->vf_id),
  458. VPLAN_TXQ_MAPENA_TX_ENA_M);
  459. /* set the VF PF Tx queue range
  460. * VFNUMQ value should be set to (number of queues - 1). A value
  461. * of 0 means 1 queue and a value of 255 means 256 queues
  462. */
  463. reg = (((vsi->txq_map[0] << VPLAN_TX_QBASE_VFFIRSTQ_S) &
  464. VPLAN_TX_QBASE_VFFIRSTQ_M) |
  465. (((vsi->alloc_txq - 1) << VPLAN_TX_QBASE_VFNUMQ_S) &
  466. VPLAN_TX_QBASE_VFNUMQ_M));
  467. wr32(hw, VPLAN_TX_QBASE(vf->vf_id), reg);
  468. } else {
  469. dev_err(&pf->pdev->dev,
  470. "Scattered mode for VF Tx queues is not yet implemented\n");
  471. }
  472. /* VF Rx queues allocation */
  473. if (vsi->rx_mapping_mode == ICE_VSI_MAP_CONTIG) {
  474. wr32(hw, VPLAN_RXQ_MAPENA(vf->vf_id),
  475. VPLAN_RXQ_MAPENA_RX_ENA_M);
  476. /* set the VF PF Rx queue range
  477. * VFNUMQ value should be set to (number of queues - 1). A value
  478. * of 0 means 1 queue and a value of 255 means 256 queues
  479. */
  480. reg = (((vsi->rxq_map[0] << VPLAN_RX_QBASE_VFFIRSTQ_S) &
  481. VPLAN_RX_QBASE_VFFIRSTQ_M) |
  482. (((vsi->alloc_txq - 1) << VPLAN_RX_QBASE_VFNUMQ_S) &
  483. VPLAN_RX_QBASE_VFNUMQ_M));
  484. wr32(hw, VPLAN_RX_QBASE(vf->vf_id), reg);
  485. } else {
  486. dev_err(&pf->pdev->dev,
  487. "Scattered mode for VF Rx queues is not yet implemented\n");
  488. }
  489. }
  490. /**
  491. * ice_determine_res
  492. * @pf: pointer to the PF structure
  493. * @avail_res: available resources in the PF structure
  494. * @max_res: maximum resources that can be given per VF
  495. * @min_res: minimum resources that can be given per VF
  496. *
  497. * Returns non-zero value if resources (queues/vectors) are available or
  498. * returns zero if PF cannot accommodate for all num_alloc_vfs.
  499. */
  500. static int
  501. ice_determine_res(struct ice_pf *pf, u16 avail_res, u16 max_res, u16 min_res)
  502. {
  503. bool checked_min_res = false;
  504. int res;
  505. /* start by checking if PF can assign max number of resources for
  506. * all num_alloc_vfs.
  507. * if yes, return number per VF
  508. * If no, divide by 2 and roundup, check again
  509. * repeat the loop till we reach a point where even minimum resources
  510. * are not available, in that case return 0
  511. */
  512. res = max_res;
  513. while ((res >= min_res) && !checked_min_res) {
  514. int num_all_res;
  515. num_all_res = pf->num_alloc_vfs * res;
  516. if (num_all_res <= avail_res)
  517. return res;
  518. if (res == min_res)
  519. checked_min_res = true;
  520. res = DIV_ROUND_UP(res, 2);
  521. }
  522. return 0;
  523. }
  524. /**
  525. * ice_check_avail_res - check if vectors and queues are available
  526. * @pf: pointer to the PF structure
  527. *
  528. * This function is where we calculate actual number of resources for VF VSIs,
  529. * we don't reserve ahead of time during probe. Returns success if vectors and
  530. * queues resources are available, otherwise returns error code
  531. */
  532. static int ice_check_avail_res(struct ice_pf *pf)
  533. {
  534. u16 num_msix, num_txq, num_rxq;
  535. if (!pf->num_alloc_vfs)
  536. return -EINVAL;
  537. /* Grab from HW interrupts common pool
  538. * Note: By the time the user decides it needs more vectors in a VF
  539. * its already too late since one must decide this prior to creating the
  540. * VF interface. So the best we can do is take a guess as to what the
  541. * user might want.
  542. *
  543. * We have two policies for vector allocation:
  544. * 1. if num_alloc_vfs is from 1 to 16, then we consider this as small
  545. * number of NFV VFs used for NFV appliances, since this is a special
  546. * case, we try to assign maximum vectors per VF (65) as much as
  547. * possible, based on determine_resources algorithm.
  548. * 2. if num_alloc_vfs is from 17 to 256, then its large number of
  549. * regular VFs which are not used for any special purpose. Hence try to
  550. * grab default interrupt vectors (5 as supported by AVF driver).
  551. */
  552. if (pf->num_alloc_vfs <= 16) {
  553. num_msix = ice_determine_res(pf, pf->num_avail_hw_msix,
  554. ICE_MAX_INTR_PER_VF,
  555. ICE_MIN_INTR_PER_VF);
  556. } else if (pf->num_alloc_vfs <= ICE_MAX_VF_COUNT) {
  557. num_msix = ice_determine_res(pf, pf->num_avail_hw_msix,
  558. ICE_DFLT_INTR_PER_VF,
  559. ICE_MIN_INTR_PER_VF);
  560. } else {
  561. dev_err(&pf->pdev->dev,
  562. "Number of VFs %d exceeds max VF count %d\n",
  563. pf->num_alloc_vfs, ICE_MAX_VF_COUNT);
  564. return -EIO;
  565. }
  566. if (!num_msix)
  567. return -EIO;
  568. /* Grab from the common pool
  569. * start by requesting Default queues (4 as supported by AVF driver),
  570. * Note that, the main difference between queues and vectors is, latter
  571. * can only be reserved at init time but queues can be requested by VF
  572. * at runtime through Virtchnl, that is the reason we start by reserving
  573. * few queues.
  574. */
  575. num_txq = ice_determine_res(pf, pf->q_left_tx, ICE_DFLT_QS_PER_VF,
  576. ICE_MIN_QS_PER_VF);
  577. num_rxq = ice_determine_res(pf, pf->q_left_rx, ICE_DFLT_QS_PER_VF,
  578. ICE_MIN_QS_PER_VF);
  579. if (!num_txq || !num_rxq)
  580. return -EIO;
  581. /* since AVF driver works with only queue pairs which means, it expects
  582. * to have equal number of Rx and Tx queues, so take the minimum of
  583. * available Tx or Rx queues
  584. */
  585. pf->num_vf_qps = min_t(int, num_txq, num_rxq);
  586. pf->num_vf_msix = num_msix;
  587. return 0;
  588. }
  589. /**
  590. * ice_cleanup_and_realloc_vf - Clean up VF and reallocate resources after reset
  591. * @vf: pointer to the VF structure
  592. *
  593. * Cleanup a VF after the hardware reset is finished. Expects the caller to
  594. * have verified whether the reset is finished properly, and ensure the
  595. * minimum amount of wait time has passed. Reallocate VF resources back to make
  596. * VF state active
  597. */
  598. static void ice_cleanup_and_realloc_vf(struct ice_vf *vf)
  599. {
  600. struct ice_pf *pf = vf->pf;
  601. struct ice_hw *hw;
  602. u32 reg;
  603. hw = &pf->hw;
  604. /* PF software completes the flow by notifying VF that reset flow is
  605. * completed. This is done by enabling hardware by clearing the reset
  606. * bit in the VPGEN_VFRTRIG reg and setting VFR_STATE in the VFGEN_RSTAT
  607. * register to VFR completed (done at the end of this function)
  608. * By doing this we allow HW to access VF memory at any point. If we
  609. * did it any sooner, HW could access memory while it was being freed
  610. * in ice_free_vf_res(), causing an IOMMU fault.
  611. *
  612. * On the other hand, this needs to be done ASAP, because the VF driver
  613. * is waiting for this to happen and may report a timeout. It's
  614. * harmless, but it gets logged into Guest OS kernel log, so best avoid
  615. * it.
  616. */
  617. reg = rd32(hw, VPGEN_VFRTRIG(vf->vf_id));
  618. reg &= ~VPGEN_VFRTRIG_VFSWR_M;
  619. wr32(hw, VPGEN_VFRTRIG(vf->vf_id), reg);
  620. /* reallocate VF resources to finish resetting the VSI state */
  621. if (!ice_alloc_vf_res(vf)) {
  622. ice_ena_vf_mappings(vf);
  623. set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  624. clear_bit(ICE_VF_STATE_DIS, vf->vf_states);
  625. vf->num_vlan = 0;
  626. }
  627. /* Tell the VF driver the reset is done. This needs to be done only
  628. * after VF has been fully initialized, because the VF driver may
  629. * request resources immediately after setting this flag.
  630. */
  631. wr32(hw, VFGEN_RSTAT(vf->vf_id), VIRTCHNL_VFR_VFACTIVE);
  632. }
  633. /**
  634. * ice_reset_all_vfs - reset all allocated VFs in one go
  635. * @pf: pointer to the PF structure
  636. * @is_vflr: true if VFLR was issued, false if not
  637. *
  638. * First, tell the hardware to reset each VF, then do all the waiting in one
  639. * chunk, and finally finish restoring each VF after the wait. This is useful
  640. * during PF routines which need to reset all VFs, as otherwise it must perform
  641. * these resets in a serialized fashion.
  642. *
  643. * Returns true if any VFs were reset, and false otherwise.
  644. */
  645. bool ice_reset_all_vfs(struct ice_pf *pf, bool is_vflr)
  646. {
  647. struct ice_hw *hw = &pf->hw;
  648. int v, i;
  649. /* If we don't have any VFs, then there is nothing to reset */
  650. if (!pf->num_alloc_vfs)
  651. return false;
  652. /* If VFs have been disabled, there is no need to reset */
  653. if (test_and_set_bit(__ICE_VF_DIS, pf->state))
  654. return false;
  655. /* Begin reset on all VFs at once */
  656. for (v = 0; v < pf->num_alloc_vfs; v++)
  657. ice_trigger_vf_reset(&pf->vf[v], is_vflr);
  658. /* Call Disable LAN Tx queue AQ call with VFR bit set and 0
  659. * queues to inform Firmware about VF reset.
  660. */
  661. for (v = 0; v < pf->num_alloc_vfs; v++)
  662. ice_dis_vsi_txq(pf->vsi[0]->port_info, 0, NULL, NULL,
  663. ICE_VF_RESET, v, NULL);
  664. /* HW requires some time to make sure it can flush the FIFO for a VF
  665. * when it resets it. Poll the VPGEN_VFRSTAT register for each VF in
  666. * sequence to make sure that it has completed. We'll keep track of
  667. * the VFs using a simple iterator that increments once that VF has
  668. * finished resetting.
  669. */
  670. for (i = 0, v = 0; i < 10 && v < pf->num_alloc_vfs; i++) {
  671. usleep_range(10000, 20000);
  672. /* Check each VF in sequence */
  673. while (v < pf->num_alloc_vfs) {
  674. struct ice_vf *vf = &pf->vf[v];
  675. u32 reg;
  676. reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
  677. if (!(reg & VPGEN_VFRSTAT_VFRD_M))
  678. break;
  679. /* If the current VF has finished resetting, move on
  680. * to the next VF in sequence.
  681. */
  682. v++;
  683. }
  684. }
  685. /* Display a warning if at least one VF didn't manage to reset in
  686. * time, but continue on with the operation.
  687. */
  688. if (v < pf->num_alloc_vfs)
  689. dev_warn(&pf->pdev->dev, "VF reset check timeout\n");
  690. usleep_range(10000, 20000);
  691. /* free VF resources to begin resetting the VSI state */
  692. for (v = 0; v < pf->num_alloc_vfs; v++)
  693. ice_free_vf_res(&pf->vf[v]);
  694. if (ice_check_avail_res(pf)) {
  695. dev_err(&pf->pdev->dev,
  696. "Cannot allocate VF resources, try with fewer number of VFs\n");
  697. return false;
  698. }
  699. /* Finish the reset on each VF */
  700. for (v = 0; v < pf->num_alloc_vfs; v++)
  701. ice_cleanup_and_realloc_vf(&pf->vf[v]);
  702. ice_flush(hw);
  703. clear_bit(__ICE_VF_DIS, pf->state);
  704. return true;
  705. }
  706. /**
  707. * ice_reset_vf - Reset a particular VF
  708. * @vf: pointer to the VF structure
  709. * @is_vflr: true if VFLR was issued, false if not
  710. *
  711. * Returns true if the VF is reset, false otherwise.
  712. */
  713. static bool ice_reset_vf(struct ice_vf *vf, bool is_vflr)
  714. {
  715. struct ice_pf *pf = vf->pf;
  716. struct ice_hw *hw = &pf->hw;
  717. bool rsd = false;
  718. u32 reg;
  719. int i;
  720. /* If the VFs have been disabled, this means something else is
  721. * resetting the VF, so we shouldn't continue.
  722. */
  723. if (test_and_set_bit(__ICE_VF_DIS, pf->state))
  724. return false;
  725. ice_trigger_vf_reset(vf, is_vflr);
  726. if (test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
  727. ice_vsi_stop_tx_rings(pf->vsi[vf->lan_vsi_idx], ICE_VF_RESET,
  728. vf->vf_id);
  729. ice_vsi_stop_rx_rings(pf->vsi[vf->lan_vsi_idx]);
  730. clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
  731. } else {
  732. /* Call Disable LAN Tx queue AQ call even when queues are not
  733. * enabled. This is needed for successful completiom of VFR
  734. */
  735. ice_dis_vsi_txq(pf->vsi[vf->lan_vsi_idx]->port_info, 0,
  736. NULL, NULL, ICE_VF_RESET, vf->vf_id, NULL);
  737. }
  738. /* poll VPGEN_VFRSTAT reg to make sure
  739. * that reset is complete
  740. */
  741. for (i = 0; i < 10; i++) {
  742. /* VF reset requires driver to first reset the VF and then
  743. * poll the status register to make sure that the reset
  744. * completed successfully.
  745. */
  746. usleep_range(10000, 20000);
  747. reg = rd32(hw, VPGEN_VFRSTAT(vf->vf_id));
  748. if (reg & VPGEN_VFRSTAT_VFRD_M) {
  749. rsd = true;
  750. break;
  751. }
  752. }
  753. /* Display a warning if VF didn't manage to reset in time, but need to
  754. * continue on with the operation.
  755. */
  756. if (!rsd)
  757. dev_warn(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  758. vf->vf_id);
  759. usleep_range(10000, 20000);
  760. /* free VF resources to begin resetting the VSI state */
  761. ice_free_vf_res(vf);
  762. ice_cleanup_and_realloc_vf(vf);
  763. ice_flush(hw);
  764. clear_bit(__ICE_VF_DIS, pf->state);
  765. return true;
  766. }
  767. /**
  768. * ice_vc_notify_reset - Send pending reset message to all VFs
  769. * @pf: pointer to the PF structure
  770. *
  771. * indicate a pending reset to all VFs on a given PF
  772. */
  773. void ice_vc_notify_reset(struct ice_pf *pf)
  774. {
  775. struct virtchnl_pf_event pfe;
  776. if (!pf->num_alloc_vfs)
  777. return;
  778. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  779. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  780. ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, ICE_SUCCESS,
  781. (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
  782. }
  783. /**
  784. * ice_vc_notify_vf_reset - Notify VF of a reset event
  785. * @vf: pointer to the VF structure
  786. */
  787. static void ice_vc_notify_vf_reset(struct ice_vf *vf)
  788. {
  789. struct virtchnl_pf_event pfe;
  790. /* validate the request */
  791. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  792. return;
  793. /* verify if the VF is in either init or active before proceeding */
  794. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
  795. !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
  796. return;
  797. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  798. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  799. ice_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT, 0,
  800. (u8 *)&pfe, sizeof(pfe), NULL);
  801. }
  802. /**
  803. * ice_alloc_vfs - Allocate and set up VFs resources
  804. * @pf: pointer to the PF structure
  805. * @num_alloc_vfs: number of VFs to allocate
  806. */
  807. static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
  808. {
  809. struct ice_hw *hw = &pf->hw;
  810. struct ice_vf *vfs;
  811. int i, ret;
  812. /* Disable global interrupt 0 so we don't try to handle the VFLR. */
  813. wr32(hw, GLINT_DYN_CTL(pf->hw_oicr_idx),
  814. ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
  815. ice_flush(hw);
  816. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  817. if (ret) {
  818. pf->num_alloc_vfs = 0;
  819. goto err_unroll_intr;
  820. }
  821. /* allocate memory */
  822. vfs = devm_kcalloc(&pf->pdev->dev, num_alloc_vfs, sizeof(*vfs),
  823. GFP_KERNEL);
  824. if (!vfs) {
  825. ret = -ENOMEM;
  826. goto err_unroll_sriov;
  827. }
  828. pf->vf = vfs;
  829. /* apply default profile */
  830. for (i = 0; i < num_alloc_vfs; i++) {
  831. vfs[i].pf = pf;
  832. vfs[i].vf_sw_id = pf->first_sw;
  833. vfs[i].vf_id = i;
  834. /* assign default capabilities */
  835. set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  836. vfs[i].spoofchk = true;
  837. /* Set this state so that PF driver does VF vector assignment */
  838. set_bit(ICE_VF_STATE_CFG_INTR, vfs[i].vf_states);
  839. }
  840. pf->num_alloc_vfs = num_alloc_vfs;
  841. /* VF resources get allocated during reset */
  842. if (!ice_reset_all_vfs(pf, false))
  843. goto err_unroll_sriov;
  844. goto err_unroll_intr;
  845. err_unroll_sriov:
  846. pci_disable_sriov(pf->pdev);
  847. err_unroll_intr:
  848. /* rearm interrupts here */
  849. ice_irq_dynamic_ena(hw, NULL, NULL);
  850. return ret;
  851. }
  852. /**
  853. * ice_pf_state_is_nominal - checks the pf for nominal state
  854. * @pf: pointer to pf to check
  855. *
  856. * Check the PF's state for a collection of bits that would indicate
  857. * the PF is in a state that would inhibit normal operation for
  858. * driver functionality.
  859. *
  860. * Returns true if PF is in a nominal state.
  861. * Returns false otherwise
  862. */
  863. static bool ice_pf_state_is_nominal(struct ice_pf *pf)
  864. {
  865. DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
  866. if (!pf)
  867. return false;
  868. bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
  869. if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
  870. return false;
  871. return true;
  872. }
  873. /**
  874. * ice_pci_sriov_ena - Enable or change number of VFs
  875. * @pf: pointer to the PF structure
  876. * @num_vfs: number of VFs to allocate
  877. */
  878. static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
  879. {
  880. int pre_existing_vfs = pci_num_vf(pf->pdev);
  881. struct device *dev = &pf->pdev->dev;
  882. int err;
  883. if (!ice_pf_state_is_nominal(pf)) {
  884. dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
  885. return -EBUSY;
  886. }
  887. if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
  888. dev_err(dev, "This device is not capable of SR-IOV\n");
  889. return -ENODEV;
  890. }
  891. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  892. ice_free_vfs(pf);
  893. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  894. return num_vfs;
  895. if (num_vfs > pf->num_vfs_supported) {
  896. dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
  897. num_vfs, pf->num_vfs_supported);
  898. return -ENOTSUPP;
  899. }
  900. dev_info(dev, "Allocating %d VFs\n", num_vfs);
  901. err = ice_alloc_vfs(pf, num_vfs);
  902. if (err) {
  903. dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
  904. return err;
  905. }
  906. set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
  907. return num_vfs;
  908. }
  909. /**
  910. * ice_sriov_configure - Enable or change number of VFs via sysfs
  911. * @pdev: pointer to a pci_dev structure
  912. * @num_vfs: number of VFs to allocate
  913. *
  914. * This function is called when the user updates the number of VFs in sysfs.
  915. */
  916. int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
  917. {
  918. struct ice_pf *pf = pci_get_drvdata(pdev);
  919. if (num_vfs)
  920. return ice_pci_sriov_ena(pf, num_vfs);
  921. if (!pci_vfs_assigned(pdev)) {
  922. ice_free_vfs(pf);
  923. } else {
  924. dev_err(&pf->pdev->dev,
  925. "can't free VFs because some are assigned to VMs.\n");
  926. return -EBUSY;
  927. }
  928. return 0;
  929. }
  930. /**
  931. * ice_process_vflr_event - Free VF resources via IRQ calls
  932. * @pf: pointer to the PF structure
  933. *
  934. * called from the VLFR IRQ handler to
  935. * free up VF resources and state variables
  936. */
  937. void ice_process_vflr_event(struct ice_pf *pf)
  938. {
  939. struct ice_hw *hw = &pf->hw;
  940. int vf_id;
  941. u32 reg;
  942. if (!test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
  943. !pf->num_alloc_vfs)
  944. return;
  945. /* Re-enable the VFLR interrupt cause here, before looking for which
  946. * VF got reset. Otherwise, if another VF gets a reset while the
  947. * first one is being processed, that interrupt will be lost, and
  948. * that VF will be stuck in reset forever.
  949. */
  950. reg = rd32(hw, PFINT_OICR_ENA);
  951. reg |= PFINT_OICR_VFLR_M;
  952. wr32(hw, PFINT_OICR_ENA, reg);
  953. ice_flush(hw);
  954. clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
  955. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  956. struct ice_vf *vf = &pf->vf[vf_id];
  957. u32 reg_idx, bit_idx;
  958. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  959. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  960. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  961. reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
  962. if (reg & BIT(bit_idx))
  963. /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
  964. ice_reset_vf(vf, true);
  965. }
  966. }
  967. /**
  968. * ice_vc_dis_vf - Disable a given VF via SW reset
  969. * @vf: pointer to the VF info
  970. *
  971. * Disable the VF through a SW reset
  972. */
  973. static void ice_vc_dis_vf(struct ice_vf *vf)
  974. {
  975. ice_vc_notify_vf_reset(vf);
  976. ice_reset_vf(vf, false);
  977. }
  978. /**
  979. * ice_vc_send_msg_to_vf - Send message to VF
  980. * @vf: pointer to the VF info
  981. * @v_opcode: virtual channel opcode
  982. * @v_retval: virtual channel return value
  983. * @msg: pointer to the msg buffer
  984. * @msglen: msg length
  985. *
  986. * send msg to VF
  987. */
  988. static int ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
  989. enum ice_status v_retval, u8 *msg, u16 msglen)
  990. {
  991. enum ice_status aq_ret;
  992. struct ice_pf *pf;
  993. /* validate the request */
  994. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  995. return -EINVAL;
  996. pf = vf->pf;
  997. /* single place to detect unsuccessful return values */
  998. if (v_retval) {
  999. vf->num_inval_msgs++;
  1000. dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
  1001. vf->vf_id, v_opcode, v_retval);
  1002. if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
  1003. dev_err(&pf->pdev->dev,
  1004. "Number of invalid messages exceeded for VF %d\n",
  1005. vf->vf_id);
  1006. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  1007. set_bit(ICE_VF_STATE_DIS, vf->vf_states);
  1008. return -EIO;
  1009. }
  1010. } else {
  1011. vf->num_valid_msgs++;
  1012. /* reset the invalid counter, if a valid message is received. */
  1013. vf->num_inval_msgs = 0;
  1014. }
  1015. aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
  1016. msg, msglen, NULL);
  1017. if (aq_ret) {
  1018. dev_info(&pf->pdev->dev,
  1019. "Unable to send the message to VF %d aq_err %d\n",
  1020. vf->vf_id, pf->hw.mailboxq.sq_last_status);
  1021. return -EIO;
  1022. }
  1023. return 0;
  1024. }
  1025. /**
  1026. * ice_vc_get_ver_msg
  1027. * @vf: pointer to the VF info
  1028. * @msg: pointer to the msg buffer
  1029. *
  1030. * called from the VF to request the API version used by the PF
  1031. */
  1032. static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
  1033. {
  1034. struct virtchnl_version_info info = {
  1035. VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
  1036. };
  1037. vf->vf_ver = *(struct virtchnl_version_info *)msg;
  1038. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1039. if (VF_IS_V10(&vf->vf_ver))
  1040. info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1041. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION, ICE_SUCCESS,
  1042. (u8 *)&info,
  1043. sizeof(struct virtchnl_version_info));
  1044. }
  1045. /**
  1046. * ice_vc_get_vf_res_msg
  1047. * @vf: pointer to the VF info
  1048. * @msg: pointer to the msg buffer
  1049. *
  1050. * called from the VF to request its resources
  1051. */
  1052. static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
  1053. {
  1054. struct virtchnl_vf_resource *vfres = NULL;
  1055. enum ice_status aq_ret = 0;
  1056. struct ice_pf *pf = vf->pf;
  1057. struct ice_vsi *vsi;
  1058. int len = 0;
  1059. int ret;
  1060. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  1061. aq_ret = ICE_ERR_PARAM;
  1062. goto err;
  1063. }
  1064. len = sizeof(struct virtchnl_vf_resource);
  1065. vfres = devm_kzalloc(&pf->pdev->dev, len, GFP_KERNEL);
  1066. if (!vfres) {
  1067. aq_ret = ICE_ERR_NO_MEMORY;
  1068. len = 0;
  1069. goto err;
  1070. }
  1071. if (VF_IS_V11(&vf->vf_ver))
  1072. vf->driver_caps = *(u32 *)msg;
  1073. else
  1074. vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
  1075. VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1076. VIRTCHNL_VF_OFFLOAD_VLAN;
  1077. vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
  1078. vsi = pf->vsi[vf->lan_vsi_idx];
  1079. if (!vsi->info.pvid)
  1080. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
  1081. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  1082. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
  1083. } else {
  1084. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
  1085. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1086. else
  1087. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1088. }
  1089. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1090. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
  1091. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
  1092. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
  1093. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
  1094. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
  1095. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
  1096. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1097. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  1098. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  1099. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
  1100. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
  1101. if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
  1102. vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
  1103. vfres->num_vsis = 1;
  1104. /* Tx and Rx queue are equal for VF */
  1105. vfres->num_queue_pairs = vsi->num_txq;
  1106. vfres->max_vectors = pf->num_vf_msix;
  1107. vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
  1108. vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
  1109. vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
  1110. vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
  1111. vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
  1112. ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
  1113. vf->dflt_lan_addr.addr);
  1114. set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  1115. err:
  1116. /* send the response back to the VF */
  1117. ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, aq_ret,
  1118. (u8 *)vfres, len);
  1119. devm_kfree(&pf->pdev->dev, vfres);
  1120. return ret;
  1121. }
  1122. /**
  1123. * ice_vc_reset_vf_msg
  1124. * @vf: pointer to the VF info
  1125. *
  1126. * called from the VF to reset itself,
  1127. * unlike other virtchnl messages, PF driver
  1128. * doesn't send the response back to the VF
  1129. */
  1130. static void ice_vc_reset_vf_msg(struct ice_vf *vf)
  1131. {
  1132. if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
  1133. ice_reset_vf(vf, false);
  1134. }
  1135. /**
  1136. * ice_find_vsi_from_id
  1137. * @pf: the pf structure to search for the VSI
  1138. * @id: id of the VSI it is searching for
  1139. *
  1140. * searches for the VSI with the given id
  1141. */
  1142. static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
  1143. {
  1144. int i;
  1145. for (i = 0; i < pf->num_alloc_vsi; i++)
  1146. if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
  1147. return pf->vsi[i];
  1148. return NULL;
  1149. }
  1150. /**
  1151. * ice_vc_isvalid_vsi_id
  1152. * @vf: pointer to the VF info
  1153. * @vsi_id: VF relative VSI id
  1154. *
  1155. * check for the valid VSI id
  1156. */
  1157. static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
  1158. {
  1159. struct ice_pf *pf = vf->pf;
  1160. struct ice_vsi *vsi;
  1161. vsi = ice_find_vsi_from_id(pf, vsi_id);
  1162. return (vsi && (vsi->vf_id == vf->vf_id));
  1163. }
  1164. /**
  1165. * ice_vc_isvalid_q_id
  1166. * @vf: pointer to the VF info
  1167. * @vsi_id: VSI id
  1168. * @qid: VSI relative queue id
  1169. *
  1170. * check for the valid queue id
  1171. */
  1172. static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
  1173. {
  1174. struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
  1175. /* allocated Tx and Rx queues should be always equal for VF VSI */
  1176. return (vsi && (qid < vsi->alloc_txq));
  1177. }
  1178. /**
  1179. * ice_vc_config_rss_key
  1180. * @vf: pointer to the VF info
  1181. * @msg: pointer to the msg buffer
  1182. *
  1183. * Configure the VF's RSS key
  1184. */
  1185. static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
  1186. {
  1187. struct virtchnl_rss_key *vrk =
  1188. (struct virtchnl_rss_key *)msg;
  1189. struct ice_vsi *vsi = NULL;
  1190. enum ice_status aq_ret;
  1191. int ret;
  1192. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1193. aq_ret = ICE_ERR_PARAM;
  1194. goto error_param;
  1195. }
  1196. if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
  1197. aq_ret = ICE_ERR_PARAM;
  1198. goto error_param;
  1199. }
  1200. vsi = ice_find_vsi_from_id(vf->pf, vrk->vsi_id);
  1201. if (!vsi) {
  1202. aq_ret = ICE_ERR_PARAM;
  1203. goto error_param;
  1204. }
  1205. if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
  1206. aq_ret = ICE_ERR_PARAM;
  1207. goto error_param;
  1208. }
  1209. if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
  1210. aq_ret = ICE_ERR_PARAM;
  1211. goto error_param;
  1212. }
  1213. ret = ice_set_rss(vsi, vrk->key, NULL, 0);
  1214. aq_ret = ret ? ICE_ERR_PARAM : ICE_SUCCESS;
  1215. error_param:
  1216. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, aq_ret,
  1217. NULL, 0);
  1218. }
  1219. /**
  1220. * ice_vc_config_rss_lut
  1221. * @vf: pointer to the VF info
  1222. * @msg: pointer to the msg buffer
  1223. *
  1224. * Configure the VF's RSS LUT
  1225. */
  1226. static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
  1227. {
  1228. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  1229. struct ice_vsi *vsi = NULL;
  1230. enum ice_status aq_ret;
  1231. int ret;
  1232. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1233. aq_ret = ICE_ERR_PARAM;
  1234. goto error_param;
  1235. }
  1236. if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
  1237. aq_ret = ICE_ERR_PARAM;
  1238. goto error_param;
  1239. }
  1240. vsi = ice_find_vsi_from_id(vf->pf, vrl->vsi_id);
  1241. if (!vsi) {
  1242. aq_ret = ICE_ERR_PARAM;
  1243. goto error_param;
  1244. }
  1245. if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
  1246. aq_ret = ICE_ERR_PARAM;
  1247. goto error_param;
  1248. }
  1249. if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
  1250. aq_ret = ICE_ERR_PARAM;
  1251. goto error_param;
  1252. }
  1253. ret = ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE);
  1254. aq_ret = ret ? ICE_ERR_PARAM : ICE_SUCCESS;
  1255. error_param:
  1256. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, aq_ret,
  1257. NULL, 0);
  1258. }
  1259. /**
  1260. * ice_vc_get_stats_msg
  1261. * @vf: pointer to the VF info
  1262. * @msg: pointer to the msg buffer
  1263. *
  1264. * called from the VF to get VSI stats
  1265. */
  1266. static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
  1267. {
  1268. struct virtchnl_queue_select *vqs =
  1269. (struct virtchnl_queue_select *)msg;
  1270. enum ice_status aq_ret = 0;
  1271. struct ice_eth_stats stats;
  1272. struct ice_vsi *vsi;
  1273. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1274. aq_ret = ICE_ERR_PARAM;
  1275. goto error_param;
  1276. }
  1277. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1278. aq_ret = ICE_ERR_PARAM;
  1279. goto error_param;
  1280. }
  1281. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1282. if (!vsi) {
  1283. aq_ret = ICE_ERR_PARAM;
  1284. goto error_param;
  1285. }
  1286. memset(&stats, 0, sizeof(struct ice_eth_stats));
  1287. ice_update_eth_stats(vsi);
  1288. stats = vsi->eth_stats;
  1289. error_param:
  1290. /* send the response to the VF */
  1291. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
  1292. (u8 *)&stats, sizeof(stats));
  1293. }
  1294. /**
  1295. * ice_vc_ena_qs_msg
  1296. * @vf: pointer to the VF info
  1297. * @msg: pointer to the msg buffer
  1298. *
  1299. * called from the VF to enable all or specific queue(s)
  1300. */
  1301. static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
  1302. {
  1303. struct virtchnl_queue_select *vqs =
  1304. (struct virtchnl_queue_select *)msg;
  1305. enum ice_status aq_ret = 0;
  1306. struct ice_vsi *vsi;
  1307. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1308. aq_ret = ICE_ERR_PARAM;
  1309. goto error_param;
  1310. }
  1311. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1312. aq_ret = ICE_ERR_PARAM;
  1313. goto error_param;
  1314. }
  1315. if (!vqs->rx_queues && !vqs->tx_queues) {
  1316. aq_ret = ICE_ERR_PARAM;
  1317. goto error_param;
  1318. }
  1319. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1320. if (!vsi) {
  1321. aq_ret = ICE_ERR_PARAM;
  1322. goto error_param;
  1323. }
  1324. /* Enable only Rx rings, Tx rings were enabled by the FW when the
  1325. * Tx queue group list was configured and the context bits were
  1326. * programmed using ice_vsi_cfg_txqs
  1327. */
  1328. if (ice_vsi_start_rx_rings(vsi))
  1329. aq_ret = ICE_ERR_PARAM;
  1330. /* Set flag to indicate that queues are enabled */
  1331. if (!aq_ret)
  1332. set_bit(ICE_VF_STATE_ENA, vf->vf_states);
  1333. error_param:
  1334. /* send the response to the VF */
  1335. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, aq_ret,
  1336. NULL, 0);
  1337. }
  1338. /**
  1339. * ice_vc_dis_qs_msg
  1340. * @vf: pointer to the VF info
  1341. * @msg: pointer to the msg buffer
  1342. *
  1343. * called from the VF to disable all or specific
  1344. * queue(s)
  1345. */
  1346. static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
  1347. {
  1348. struct virtchnl_queue_select *vqs =
  1349. (struct virtchnl_queue_select *)msg;
  1350. enum ice_status aq_ret = 0;
  1351. struct ice_vsi *vsi;
  1352. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
  1353. !test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
  1354. aq_ret = ICE_ERR_PARAM;
  1355. goto error_param;
  1356. }
  1357. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1358. aq_ret = ICE_ERR_PARAM;
  1359. goto error_param;
  1360. }
  1361. if (!vqs->rx_queues && !vqs->tx_queues) {
  1362. aq_ret = ICE_ERR_PARAM;
  1363. goto error_param;
  1364. }
  1365. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1366. if (!vsi) {
  1367. aq_ret = ICE_ERR_PARAM;
  1368. goto error_param;
  1369. }
  1370. if (ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, vf->vf_id)) {
  1371. dev_err(&vsi->back->pdev->dev,
  1372. "Failed to stop tx rings on VSI %d\n",
  1373. vsi->vsi_num);
  1374. aq_ret = ICE_ERR_PARAM;
  1375. }
  1376. if (ice_vsi_stop_rx_rings(vsi)) {
  1377. dev_err(&vsi->back->pdev->dev,
  1378. "Failed to stop rx rings on VSI %d\n",
  1379. vsi->vsi_num);
  1380. aq_ret = ICE_ERR_PARAM;
  1381. }
  1382. /* Clear enabled queues flag */
  1383. if (!aq_ret)
  1384. clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
  1385. error_param:
  1386. /* send the response to the VF */
  1387. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, aq_ret,
  1388. NULL, 0);
  1389. }
  1390. /**
  1391. * ice_vc_cfg_irq_map_msg
  1392. * @vf: pointer to the VF info
  1393. * @msg: pointer to the msg buffer
  1394. *
  1395. * called from the VF to configure the IRQ to queue map
  1396. */
  1397. static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
  1398. {
  1399. struct virtchnl_irq_map_info *irqmap_info =
  1400. (struct virtchnl_irq_map_info *)msg;
  1401. u16 vsi_id, vsi_q_id, vector_id;
  1402. struct virtchnl_vector_map *map;
  1403. struct ice_vsi *vsi = NULL;
  1404. struct ice_pf *pf = vf->pf;
  1405. enum ice_status aq_ret = 0;
  1406. unsigned long qmap;
  1407. int i;
  1408. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1409. aq_ret = ICE_ERR_PARAM;
  1410. goto error_param;
  1411. }
  1412. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1413. map = &irqmap_info->vecmap[i];
  1414. vector_id = map->vector_id;
  1415. vsi_id = map->vsi_id;
  1416. /* validate msg params */
  1417. if (!(vector_id < pf->hw.func_caps.common_cap
  1418. .num_msix_vectors) || !ice_vc_isvalid_vsi_id(vf, vsi_id)) {
  1419. aq_ret = ICE_ERR_PARAM;
  1420. goto error_param;
  1421. }
  1422. vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
  1423. if (!vsi) {
  1424. aq_ret = ICE_ERR_PARAM;
  1425. goto error_param;
  1426. }
  1427. /* lookout for the invalid queue index */
  1428. qmap = map->rxq_map;
  1429. for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
  1430. if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
  1431. aq_ret = ICE_ERR_PARAM;
  1432. goto error_param;
  1433. }
  1434. vsi->q_vectors[i]->num_ring_rx++;
  1435. vsi->rx_rings[vsi_q_id]->itr_setting =
  1436. map->rxitr_idx;
  1437. vsi->rx_rings[vsi_q_id]->q_vector = vsi->q_vectors[i];
  1438. }
  1439. qmap = map->txq_map;
  1440. for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
  1441. if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
  1442. aq_ret = ICE_ERR_PARAM;
  1443. goto error_param;
  1444. }
  1445. vsi->q_vectors[i]->num_ring_tx++;
  1446. vsi->tx_rings[vsi_q_id]->itr_setting =
  1447. map->txitr_idx;
  1448. vsi->tx_rings[vsi_q_id]->q_vector = vsi->q_vectors[i];
  1449. }
  1450. }
  1451. if (vsi)
  1452. ice_vsi_cfg_msix(vsi);
  1453. error_param:
  1454. /* send the response to the VF */
  1455. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, aq_ret,
  1456. NULL, 0);
  1457. }
  1458. /**
  1459. * ice_vc_cfg_qs_msg
  1460. * @vf: pointer to the VF info
  1461. * @msg: pointer to the msg buffer
  1462. *
  1463. * called from the VF to configure the Rx/Tx queues
  1464. */
  1465. static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
  1466. {
  1467. struct virtchnl_vsi_queue_config_info *qci =
  1468. (struct virtchnl_vsi_queue_config_info *)msg;
  1469. struct virtchnl_queue_pair_info *qpi;
  1470. enum ice_status aq_ret = 0;
  1471. struct ice_vsi *vsi;
  1472. int i;
  1473. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1474. aq_ret = ICE_ERR_PARAM;
  1475. goto error_param;
  1476. }
  1477. if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
  1478. aq_ret = ICE_ERR_PARAM;
  1479. goto error_param;
  1480. }
  1481. vsi = ice_find_vsi_from_id(vf->pf, qci->vsi_id);
  1482. if (!vsi) {
  1483. aq_ret = ICE_ERR_PARAM;
  1484. goto error_param;
  1485. }
  1486. for (i = 0; i < qci->num_queue_pairs; i++) {
  1487. qpi = &qci->qpair[i];
  1488. if (qpi->txq.vsi_id != qci->vsi_id ||
  1489. qpi->rxq.vsi_id != qci->vsi_id ||
  1490. qpi->rxq.queue_id != qpi->txq.queue_id ||
  1491. !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
  1492. aq_ret = ICE_ERR_PARAM;
  1493. goto error_param;
  1494. }
  1495. /* copy Tx queue info from VF into VSI */
  1496. vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
  1497. vsi->tx_rings[i]->count = qpi->txq.ring_len;
  1498. /* copy Rx queue info from VF into vsi */
  1499. vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
  1500. vsi->rx_rings[i]->count = qpi->rxq.ring_len;
  1501. if (qpi->rxq.databuffer_size > ((16 * 1024) - 128)) {
  1502. aq_ret = ICE_ERR_PARAM;
  1503. goto error_param;
  1504. }
  1505. vsi->rx_buf_len = qpi->rxq.databuffer_size;
  1506. if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
  1507. qpi->rxq.max_pkt_size < 64) {
  1508. aq_ret = ICE_ERR_PARAM;
  1509. goto error_param;
  1510. }
  1511. vsi->max_frame = qpi->rxq.max_pkt_size;
  1512. }
  1513. /* VF can request to configure less than allocated queues
  1514. * or default allocated queues. So update the VSI with new number
  1515. */
  1516. vsi->num_txq = qci->num_queue_pairs;
  1517. vsi->num_rxq = qci->num_queue_pairs;
  1518. if (!ice_vsi_cfg_txqs(vsi) && !ice_vsi_cfg_rxqs(vsi))
  1519. aq_ret = 0;
  1520. else
  1521. aq_ret = ICE_ERR_PARAM;
  1522. error_param:
  1523. /* send the response to the VF */
  1524. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, aq_ret,
  1525. NULL, 0);
  1526. }
  1527. /**
  1528. * ice_is_vf_trusted
  1529. * @vf: pointer to the VF info
  1530. */
  1531. static bool ice_is_vf_trusted(struct ice_vf *vf)
  1532. {
  1533. return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  1534. }
  1535. /**
  1536. * ice_can_vf_change_mac
  1537. * @vf: pointer to the VF info
  1538. *
  1539. * Return true if the VF is allowed to change its MAC filters, false otherwise
  1540. */
  1541. static bool ice_can_vf_change_mac(struct ice_vf *vf)
  1542. {
  1543. /* If the VF MAC address has been set administratively (via the
  1544. * ndo_set_vf_mac command), then deny permission to the VF to
  1545. * add/delete unicast MAC addresses, unless the VF is trusted
  1546. */
  1547. if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
  1548. return false;
  1549. return true;
  1550. }
  1551. /**
  1552. * ice_vc_handle_mac_addr_msg
  1553. * @vf: pointer to the VF info
  1554. * @msg: pointer to the msg buffer
  1555. * @set: true if mac filters are being set, false otherwise
  1556. *
  1557. * add guest mac address filter
  1558. */
  1559. static int
  1560. ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
  1561. {
  1562. struct virtchnl_ether_addr_list *al =
  1563. (struct virtchnl_ether_addr_list *)msg;
  1564. struct ice_pf *pf = vf->pf;
  1565. enum virtchnl_ops vc_op;
  1566. enum ice_status ret;
  1567. LIST_HEAD(mac_list);
  1568. struct ice_vsi *vsi;
  1569. int mac_count = 0;
  1570. int i;
  1571. if (set)
  1572. vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
  1573. else
  1574. vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
  1575. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
  1576. !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
  1577. ret = ICE_ERR_PARAM;
  1578. goto handle_mac_exit;
  1579. }
  1580. if (set && !ice_is_vf_trusted(vf) &&
  1581. (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
  1582. dev_err(&pf->pdev->dev,
  1583. "Can't add more MAC addresses, because VF is not trusted, switch the VF to trusted mode in order to add more functionalities\n");
  1584. ret = ICE_ERR_PARAM;
  1585. goto handle_mac_exit;
  1586. }
  1587. vsi = pf->vsi[vf->lan_vsi_idx];
  1588. for (i = 0; i < al->num_elements; i++) {
  1589. u8 *maddr = al->list[i].addr;
  1590. if (ether_addr_equal(maddr, vf->dflt_lan_addr.addr) ||
  1591. is_broadcast_ether_addr(maddr)) {
  1592. if (set) {
  1593. /* VF is trying to add filters that the PF
  1594. * already added. Just continue.
  1595. */
  1596. dev_info(&pf->pdev->dev,
  1597. "mac %pM already set for VF %d\n",
  1598. maddr, vf->vf_id);
  1599. continue;
  1600. } else {
  1601. /* VF can't remove dflt_lan_addr/bcast mac */
  1602. dev_err(&pf->pdev->dev,
  1603. "can't remove mac %pM for VF %d\n",
  1604. maddr, vf->vf_id);
  1605. ret = ICE_ERR_PARAM;
  1606. goto handle_mac_exit;
  1607. }
  1608. }
  1609. /* check for the invalid cases and bail if necessary */
  1610. if (is_zero_ether_addr(maddr)) {
  1611. dev_err(&pf->pdev->dev,
  1612. "invalid mac %pM provided for VF %d\n",
  1613. maddr, vf->vf_id);
  1614. ret = ICE_ERR_PARAM;
  1615. goto handle_mac_exit;
  1616. }
  1617. if (is_unicast_ether_addr(maddr) &&
  1618. !ice_can_vf_change_mac(vf)) {
  1619. dev_err(&pf->pdev->dev,
  1620. "can't change unicast mac for untrusted VF %d\n",
  1621. vf->vf_id);
  1622. ret = ICE_ERR_PARAM;
  1623. goto handle_mac_exit;
  1624. }
  1625. /* get here if maddr is multicast or if VF can change mac */
  1626. if (ice_add_mac_to_list(vsi, &mac_list, al->list[i].addr)) {
  1627. ret = ICE_ERR_NO_MEMORY;
  1628. goto handle_mac_exit;
  1629. }
  1630. mac_count++;
  1631. }
  1632. /* program the updated filter list */
  1633. if (set)
  1634. ret = ice_add_mac(&pf->hw, &mac_list);
  1635. else
  1636. ret = ice_remove_mac(&pf->hw, &mac_list);
  1637. if (ret) {
  1638. dev_err(&pf->pdev->dev,
  1639. "can't update mac filters for VF %d, error %d\n",
  1640. vf->vf_id, ret);
  1641. } else {
  1642. if (set)
  1643. vf->num_mac += mac_count;
  1644. else
  1645. vf->num_mac -= mac_count;
  1646. }
  1647. handle_mac_exit:
  1648. ice_free_fltr_list(&pf->pdev->dev, &mac_list);
  1649. /* send the response to the VF */
  1650. return ice_vc_send_msg_to_vf(vf, vc_op, ret, NULL, 0);
  1651. }
  1652. /**
  1653. * ice_vc_add_mac_addr_msg
  1654. * @vf: pointer to the VF info
  1655. * @msg: pointer to the msg buffer
  1656. *
  1657. * add guest MAC address filter
  1658. */
  1659. static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
  1660. {
  1661. return ice_vc_handle_mac_addr_msg(vf, msg, true);
  1662. }
  1663. /**
  1664. * ice_vc_del_mac_addr_msg
  1665. * @vf: pointer to the VF info
  1666. * @msg: pointer to the msg buffer
  1667. *
  1668. * remove guest MAC address filter
  1669. */
  1670. static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
  1671. {
  1672. return ice_vc_handle_mac_addr_msg(vf, msg, false);
  1673. }
  1674. /**
  1675. * ice_vc_request_qs_msg
  1676. * @vf: pointer to the VF info
  1677. * @msg: pointer to the msg buffer
  1678. *
  1679. * VFs get a default number of queues but can use this message to request a
  1680. * different number. If the request is successful, PF will reset the VF and
  1681. * return 0. If unsuccessful, PF will send message informing VF of number of
  1682. * available queue pairs via virtchnl message response to VF.
  1683. */
  1684. static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
  1685. {
  1686. struct virtchnl_vf_res_request *vfres =
  1687. (struct virtchnl_vf_res_request *)msg;
  1688. int req_queues = vfres->num_queue_pairs;
  1689. enum ice_status aq_ret = 0;
  1690. struct ice_pf *pf = vf->pf;
  1691. int tx_rx_queue_left;
  1692. int cur_queues;
  1693. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1694. aq_ret = ICE_ERR_PARAM;
  1695. goto error_param;
  1696. }
  1697. cur_queues = pf->num_vf_qps;
  1698. tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
  1699. if (req_queues <= 0) {
  1700. dev_err(&pf->pdev->dev,
  1701. "VF %d tried to request %d queues. Ignoring.\n",
  1702. vf->vf_id, req_queues);
  1703. } else if (req_queues > ICE_MAX_QS_PER_VF) {
  1704. dev_err(&pf->pdev->dev,
  1705. "VF %d tried to request more than %d queues.\n",
  1706. vf->vf_id, ICE_MAX_QS_PER_VF);
  1707. vfres->num_queue_pairs = ICE_MAX_QS_PER_VF;
  1708. } else if (req_queues - cur_queues > tx_rx_queue_left) {
  1709. dev_warn(&pf->pdev->dev,
  1710. "VF %d requested %d more queues, but only %d left.\n",
  1711. vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
  1712. vfres->num_queue_pairs = tx_rx_queue_left + cur_queues;
  1713. } else {
  1714. /* request is successful, then reset VF */
  1715. vf->num_req_qs = req_queues;
  1716. ice_vc_dis_vf(vf);
  1717. dev_info(&pf->pdev->dev,
  1718. "VF %d granted request of %d queues.\n",
  1719. vf->vf_id, req_queues);
  1720. return 0;
  1721. }
  1722. error_param:
  1723. /* send the response to the VF */
  1724. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
  1725. aq_ret, (u8 *)vfres, sizeof(*vfres));
  1726. }
  1727. /**
  1728. * ice_set_vf_port_vlan
  1729. * @netdev: network interface device structure
  1730. * @vf_id: VF identifier
  1731. * @vlan_id: VLAN id being set
  1732. * @qos: priority setting
  1733. * @vlan_proto: VLAN protocol
  1734. *
  1735. * program VF Port VLAN id and/or qos
  1736. */
  1737. int
  1738. ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
  1739. __be16 vlan_proto)
  1740. {
  1741. u16 vlanprio = vlan_id | (qos << ICE_VLAN_PRIORITY_S);
  1742. struct ice_netdev_priv *np = netdev_priv(netdev);
  1743. struct ice_pf *pf = np->vsi->back;
  1744. struct ice_vsi *vsi;
  1745. struct ice_vf *vf;
  1746. int ret = 0;
  1747. /* validate the request */
  1748. if (vf_id >= pf->num_alloc_vfs) {
  1749. dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
  1750. return -EINVAL;
  1751. }
  1752. if (vlan_id > ICE_MAX_VLANID || qos > 7) {
  1753. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  1754. return -EINVAL;
  1755. }
  1756. if (vlan_proto != htons(ETH_P_8021Q)) {
  1757. dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
  1758. return -EPROTONOSUPPORT;
  1759. }
  1760. vf = &pf->vf[vf_id];
  1761. vsi = pf->vsi[vf->lan_vsi_idx];
  1762. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  1763. dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
  1764. return -EBUSY;
  1765. }
  1766. if (le16_to_cpu(vsi->info.pvid) == vlanprio) {
  1767. /* duplicate request, so just return success */
  1768. dev_info(&pf->pdev->dev,
  1769. "Duplicate pvid %d request\n", vlanprio);
  1770. return ret;
  1771. }
  1772. /* If pvid, then remove all filters on the old VLAN */
  1773. if (vsi->info.pvid)
  1774. ice_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
  1775. VLAN_VID_MASK));
  1776. if (vlan_id || qos) {
  1777. ret = ice_vsi_set_pvid(vsi, vlanprio);
  1778. if (ret)
  1779. goto error_set_pvid;
  1780. } else {
  1781. ice_vsi_kill_pvid(vsi);
  1782. }
  1783. if (vlan_id) {
  1784. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  1785. vlan_id, qos, vf_id);
  1786. /* add new VLAN filter for each MAC */
  1787. ret = ice_vsi_add_vlan(vsi, vlan_id);
  1788. if (ret)
  1789. goto error_set_pvid;
  1790. }
  1791. /* The Port VLAN needs to be saved across resets the same as the
  1792. * default LAN MAC address.
  1793. */
  1794. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  1795. error_set_pvid:
  1796. return ret;
  1797. }
  1798. /**
  1799. * ice_vc_process_vlan_msg
  1800. * @vf: pointer to the VF info
  1801. * @msg: pointer to the msg buffer
  1802. * @add_v: Add VLAN if true, otherwise delete VLAN
  1803. *
  1804. * Process virtchnl op to add or remove programmed guest VLAN id
  1805. */
  1806. static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
  1807. {
  1808. struct virtchnl_vlan_filter_list *vfl =
  1809. (struct virtchnl_vlan_filter_list *)msg;
  1810. enum ice_status aq_ret = 0;
  1811. struct ice_pf *pf = vf->pf;
  1812. struct ice_vsi *vsi;
  1813. int i;
  1814. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1815. aq_ret = ICE_ERR_PARAM;
  1816. goto error_param;
  1817. }
  1818. if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
  1819. aq_ret = ICE_ERR_PARAM;
  1820. goto error_param;
  1821. }
  1822. if (add_v && !ice_is_vf_trusted(vf) &&
  1823. vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
  1824. dev_info(&pf->pdev->dev,
  1825. "VF is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n");
  1826. aq_ret = ICE_ERR_PARAM;
  1827. goto error_param;
  1828. }
  1829. for (i = 0; i < vfl->num_elements; i++) {
  1830. if (vfl->vlan_id[i] > ICE_MAX_VLANID) {
  1831. aq_ret = ICE_ERR_PARAM;
  1832. dev_err(&pf->pdev->dev,
  1833. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  1834. goto error_param;
  1835. }
  1836. }
  1837. vsi = ice_find_vsi_from_id(vf->pf, vfl->vsi_id);
  1838. if (!vsi) {
  1839. aq_ret = ICE_ERR_PARAM;
  1840. goto error_param;
  1841. }
  1842. if (vsi->info.pvid) {
  1843. aq_ret = ICE_ERR_PARAM;
  1844. goto error_param;
  1845. }
  1846. if (ice_vsi_manage_vlan_stripping(vsi, add_v)) {
  1847. dev_err(&pf->pdev->dev,
  1848. "%sable VLAN stripping failed for VSI %i\n",
  1849. add_v ? "en" : "dis", vsi->vsi_num);
  1850. aq_ret = ICE_ERR_PARAM;
  1851. goto error_param;
  1852. }
  1853. if (add_v) {
  1854. for (i = 0; i < vfl->num_elements; i++) {
  1855. u16 vid = vfl->vlan_id[i];
  1856. if (!ice_vsi_add_vlan(vsi, vid)) {
  1857. vf->num_vlan++;
  1858. set_bit(vid, vsi->active_vlans);
  1859. /* Enable VLAN pruning when VLAN 0 is added */
  1860. if (unlikely(!vid))
  1861. if (ice_cfg_vlan_pruning(vsi, true))
  1862. aq_ret = ICE_ERR_PARAM;
  1863. } else {
  1864. aq_ret = ICE_ERR_PARAM;
  1865. }
  1866. }
  1867. } else {
  1868. for (i = 0; i < vfl->num_elements; i++) {
  1869. u16 vid = vfl->vlan_id[i];
  1870. /* Make sure ice_vsi_kill_vlan is successful before
  1871. * updating VLAN information
  1872. */
  1873. if (!ice_vsi_kill_vlan(vsi, vid)) {
  1874. vf->num_vlan--;
  1875. clear_bit(vid, vsi->active_vlans);
  1876. /* Disable VLAN pruning when removing VLAN 0 */
  1877. if (unlikely(!vid))
  1878. ice_cfg_vlan_pruning(vsi, false);
  1879. }
  1880. }
  1881. }
  1882. error_param:
  1883. /* send the response to the VF */
  1884. if (add_v)
  1885. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret,
  1886. NULL, 0);
  1887. else
  1888. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret,
  1889. NULL, 0);
  1890. }
  1891. /**
  1892. * ice_vc_add_vlan_msg
  1893. * @vf: pointer to the VF info
  1894. * @msg: pointer to the msg buffer
  1895. *
  1896. * Add and program guest VLAN id
  1897. */
  1898. static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
  1899. {
  1900. return ice_vc_process_vlan_msg(vf, msg, true);
  1901. }
  1902. /**
  1903. * ice_vc_remove_vlan_msg
  1904. * @vf: pointer to the VF info
  1905. * @msg: pointer to the msg buffer
  1906. *
  1907. * remove programmed guest VLAN id
  1908. */
  1909. static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
  1910. {
  1911. return ice_vc_process_vlan_msg(vf, msg, false);
  1912. }
  1913. /**
  1914. * ice_vc_ena_vlan_stripping
  1915. * @vf: pointer to the VF info
  1916. *
  1917. * Enable VLAN header stripping for a given VF
  1918. */
  1919. static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
  1920. {
  1921. enum ice_status aq_ret = 0;
  1922. struct ice_pf *pf = vf->pf;
  1923. struct ice_vsi *vsi;
  1924. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1925. aq_ret = ICE_ERR_PARAM;
  1926. goto error_param;
  1927. }
  1928. vsi = pf->vsi[vf->lan_vsi_idx];
  1929. if (ice_vsi_manage_vlan_stripping(vsi, true))
  1930. aq_ret = ICE_ERR_AQ_ERROR;
  1931. error_param:
  1932. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
  1933. aq_ret, NULL, 0);
  1934. }
  1935. /**
  1936. * ice_vc_dis_vlan_stripping
  1937. * @vf: pointer to the VF info
  1938. *
  1939. * Disable VLAN header stripping for a given VF
  1940. */
  1941. static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
  1942. {
  1943. enum ice_status aq_ret = 0;
  1944. struct ice_pf *pf = vf->pf;
  1945. struct ice_vsi *vsi;
  1946. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1947. aq_ret = ICE_ERR_PARAM;
  1948. goto error_param;
  1949. }
  1950. vsi = pf->vsi[vf->lan_vsi_idx];
  1951. if (ice_vsi_manage_vlan_stripping(vsi, false))
  1952. aq_ret = ICE_ERR_AQ_ERROR;
  1953. error_param:
  1954. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
  1955. aq_ret, NULL, 0);
  1956. }
  1957. /**
  1958. * ice_vc_process_vf_msg - Process request from VF
  1959. * @pf: pointer to the PF structure
  1960. * @event: pointer to the AQ event
  1961. *
  1962. * called from the common asq/arq handler to
  1963. * process request from VF
  1964. */
  1965. void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
  1966. {
  1967. u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
  1968. s16 vf_id = le16_to_cpu(event->desc.retval);
  1969. u16 msglen = event->msg_len;
  1970. u8 *msg = event->msg_buf;
  1971. struct ice_vf *vf = NULL;
  1972. int err = 0;
  1973. if (vf_id >= pf->num_alloc_vfs) {
  1974. err = -EINVAL;
  1975. goto error_handler;
  1976. }
  1977. vf = &pf->vf[vf_id];
  1978. /* Check if VF is disabled. */
  1979. if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
  1980. err = -EPERM;
  1981. goto error_handler;
  1982. }
  1983. /* Perform basic checks on the msg */
  1984. err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
  1985. if (err) {
  1986. if (err == VIRTCHNL_ERR_PARAM)
  1987. err = -EPERM;
  1988. else
  1989. err = -EINVAL;
  1990. goto error_handler;
  1991. }
  1992. /* Perform additional checks specific to RSS and Virtchnl */
  1993. if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
  1994. struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
  1995. if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE)
  1996. err = -EINVAL;
  1997. } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
  1998. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  1999. if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE)
  2000. err = -EINVAL;
  2001. }
  2002. error_handler:
  2003. if (err) {
  2004. ice_vc_send_msg_to_vf(vf, v_opcode, ICE_ERR_PARAM, NULL, 0);
  2005. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
  2006. vf_id, v_opcode, msglen, err);
  2007. return;
  2008. }
  2009. switch (v_opcode) {
  2010. case VIRTCHNL_OP_VERSION:
  2011. err = ice_vc_get_ver_msg(vf, msg);
  2012. break;
  2013. case VIRTCHNL_OP_GET_VF_RESOURCES:
  2014. err = ice_vc_get_vf_res_msg(vf, msg);
  2015. break;
  2016. case VIRTCHNL_OP_RESET_VF:
  2017. ice_vc_reset_vf_msg(vf);
  2018. break;
  2019. case VIRTCHNL_OP_ADD_ETH_ADDR:
  2020. err = ice_vc_add_mac_addr_msg(vf, msg);
  2021. break;
  2022. case VIRTCHNL_OP_DEL_ETH_ADDR:
  2023. err = ice_vc_del_mac_addr_msg(vf, msg);
  2024. break;
  2025. case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  2026. err = ice_vc_cfg_qs_msg(vf, msg);
  2027. break;
  2028. case VIRTCHNL_OP_ENABLE_QUEUES:
  2029. err = ice_vc_ena_qs_msg(vf, msg);
  2030. ice_vc_notify_vf_link_state(vf);
  2031. break;
  2032. case VIRTCHNL_OP_DISABLE_QUEUES:
  2033. err = ice_vc_dis_qs_msg(vf, msg);
  2034. break;
  2035. case VIRTCHNL_OP_REQUEST_QUEUES:
  2036. err = ice_vc_request_qs_msg(vf, msg);
  2037. break;
  2038. case VIRTCHNL_OP_CONFIG_IRQ_MAP:
  2039. err = ice_vc_cfg_irq_map_msg(vf, msg);
  2040. break;
  2041. case VIRTCHNL_OP_CONFIG_RSS_KEY:
  2042. err = ice_vc_config_rss_key(vf, msg);
  2043. break;
  2044. case VIRTCHNL_OP_CONFIG_RSS_LUT:
  2045. err = ice_vc_config_rss_lut(vf, msg);
  2046. break;
  2047. case VIRTCHNL_OP_GET_STATS:
  2048. err = ice_vc_get_stats_msg(vf, msg);
  2049. break;
  2050. case VIRTCHNL_OP_ADD_VLAN:
  2051. err = ice_vc_add_vlan_msg(vf, msg);
  2052. break;
  2053. case VIRTCHNL_OP_DEL_VLAN:
  2054. err = ice_vc_remove_vlan_msg(vf, msg);
  2055. break;
  2056. case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
  2057. err = ice_vc_ena_vlan_stripping(vf);
  2058. break;
  2059. case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
  2060. err = ice_vc_dis_vlan_stripping(vf);
  2061. break;
  2062. case VIRTCHNL_OP_UNKNOWN:
  2063. default:
  2064. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  2065. v_opcode, vf_id);
  2066. err = ice_vc_send_msg_to_vf(vf, v_opcode, ICE_ERR_NOT_IMPL,
  2067. NULL, 0);
  2068. break;
  2069. }
  2070. if (err) {
  2071. /* Helper function cares less about error return values here
  2072. * as it is busy with pending work.
  2073. */
  2074. dev_info(&pf->pdev->dev,
  2075. "PF failed to honor VF %d, opcode %d\n, error %d\n",
  2076. vf_id, v_opcode, err);
  2077. }
  2078. }
  2079. /**
  2080. * ice_get_vf_cfg
  2081. * @netdev: network interface device structure
  2082. * @vf_id: VF identifier
  2083. * @ivi: VF configuration structure
  2084. *
  2085. * return VF configuration
  2086. */
  2087. int ice_get_vf_cfg(struct net_device *netdev, int vf_id,
  2088. struct ifla_vf_info *ivi)
  2089. {
  2090. struct ice_netdev_priv *np = netdev_priv(netdev);
  2091. struct ice_vsi *vsi = np->vsi;
  2092. struct ice_pf *pf = vsi->back;
  2093. struct ice_vf *vf;
  2094. /* validate the request */
  2095. if (vf_id >= pf->num_alloc_vfs) {
  2096. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2097. return -EINVAL;
  2098. }
  2099. vf = &pf->vf[vf_id];
  2100. vsi = pf->vsi[vf->lan_vsi_idx];
  2101. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2102. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2103. return -EBUSY;
  2104. }
  2105. ivi->vf = vf_id;
  2106. ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
  2107. /* VF configuration for VLAN and applicable QoS */
  2108. ivi->vlan = le16_to_cpu(vsi->info.pvid) & ICE_VLAN_M;
  2109. ivi->qos = (le16_to_cpu(vsi->info.pvid) & ICE_PRIORITY_M) >>
  2110. ICE_VLAN_PRIORITY_S;
  2111. ivi->trusted = vf->trusted;
  2112. ivi->spoofchk = vf->spoofchk;
  2113. if (!vf->link_forced)
  2114. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2115. else if (vf->link_up)
  2116. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2117. else
  2118. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2119. ivi->max_tx_rate = vf->tx_rate;
  2120. ivi->min_tx_rate = 0;
  2121. return 0;
  2122. }
  2123. /**
  2124. * ice_set_vf_spoofchk
  2125. * @netdev: network interface device structure
  2126. * @vf_id: VF identifier
  2127. * @ena: flag to enable or disable feature
  2128. *
  2129. * Enable or disable VF spoof checking
  2130. */
  2131. int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
  2132. {
  2133. struct ice_netdev_priv *np = netdev_priv(netdev);
  2134. struct ice_vsi_ctx ctx = { 0 };
  2135. struct ice_vsi *vsi = np->vsi;
  2136. struct ice_pf *pf = vsi->back;
  2137. struct ice_vf *vf;
  2138. int status;
  2139. /* validate the request */
  2140. if (vf_id >= pf->num_alloc_vfs) {
  2141. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2142. return -EINVAL;
  2143. }
  2144. vf = &pf->vf[vf_id];
  2145. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2146. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2147. return -EBUSY;
  2148. }
  2149. if (ena == vf->spoofchk) {
  2150. dev_dbg(&pf->pdev->dev, "VF spoofchk already %s\n",
  2151. ena ? "ON" : "OFF");
  2152. return 0;
  2153. }
  2154. ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
  2155. if (ena) {
  2156. ctx.info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
  2157. ctx.info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_PRUNE_EN_M;
  2158. }
  2159. status = ice_update_vsi(&pf->hw, vsi->idx, &ctx, NULL);
  2160. if (status) {
  2161. dev_dbg(&pf->pdev->dev,
  2162. "Error %d, failed to update VSI* parameters\n", status);
  2163. return -EIO;
  2164. }
  2165. vf->spoofchk = ena;
  2166. vsi->info.sec_flags = ctx.info.sec_flags;
  2167. vsi->info.sw_flags2 = ctx.info.sw_flags2;
  2168. return status;
  2169. }
  2170. /**
  2171. * ice_set_vf_mac
  2172. * @netdev: network interface device structure
  2173. * @vf_id: VF identifier
  2174. * @mac: mac address
  2175. *
  2176. * program VF mac address
  2177. */
  2178. int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  2179. {
  2180. struct ice_netdev_priv *np = netdev_priv(netdev);
  2181. struct ice_vsi *vsi = np->vsi;
  2182. struct ice_pf *pf = vsi->back;
  2183. struct ice_vf *vf;
  2184. int ret = 0;
  2185. /* validate the request */
  2186. if (vf_id >= pf->num_alloc_vfs) {
  2187. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2188. return -EINVAL;
  2189. }
  2190. vf = &pf->vf[vf_id];
  2191. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2192. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2193. return -EBUSY;
  2194. }
  2195. if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
  2196. netdev_err(netdev, "%pM not a valid unicast address\n", mac);
  2197. return -EINVAL;
  2198. }
  2199. /* copy mac into dflt_lan_addr and trigger a VF reset. The reset
  2200. * flow will use the updated dflt_lan_addr and add a MAC filter
  2201. * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
  2202. * set the MAC address for this VF.
  2203. */
  2204. ether_addr_copy(vf->dflt_lan_addr.addr, mac);
  2205. vf->pf_set_mac = true;
  2206. netdev_info(netdev,
  2207. "mac on VF %d set to %pM\n. VF driver will be reinitialized\n",
  2208. vf_id, mac);
  2209. ice_vc_dis_vf(vf);
  2210. return ret;
  2211. }
  2212. /**
  2213. * ice_set_vf_trust
  2214. * @netdev: network interface device structure
  2215. * @vf_id: VF identifier
  2216. * @trusted: Boolean value to enable/disable trusted VF
  2217. *
  2218. * Enable or disable a given VF as trusted
  2219. */
  2220. int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
  2221. {
  2222. struct ice_netdev_priv *np = netdev_priv(netdev);
  2223. struct ice_vsi *vsi = np->vsi;
  2224. struct ice_pf *pf = vsi->back;
  2225. struct ice_vf *vf;
  2226. /* validate the request */
  2227. if (vf_id >= pf->num_alloc_vfs) {
  2228. dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
  2229. return -EINVAL;
  2230. }
  2231. vf = &pf->vf[vf_id];
  2232. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2233. dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
  2234. return -EBUSY;
  2235. }
  2236. /* Check if already trusted */
  2237. if (trusted == vf->trusted)
  2238. return 0;
  2239. vf->trusted = trusted;
  2240. ice_vc_dis_vf(vf);
  2241. dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
  2242. vf_id, trusted ? "" : "un");
  2243. return 0;
  2244. }
  2245. /**
  2246. * ice_set_vf_link_state
  2247. * @netdev: network interface device structure
  2248. * @vf_id: VF identifier
  2249. * @link_state: required link state
  2250. *
  2251. * Set VF's link state, irrespective of physical link state status
  2252. */
  2253. int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
  2254. {
  2255. struct ice_netdev_priv *np = netdev_priv(netdev);
  2256. struct ice_pf *pf = np->vsi->back;
  2257. struct virtchnl_pf_event pfe = { 0 };
  2258. struct ice_link_status *ls;
  2259. struct ice_vf *vf;
  2260. struct ice_hw *hw;
  2261. if (vf_id >= pf->num_alloc_vfs) {
  2262. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2263. return -EINVAL;
  2264. }
  2265. vf = &pf->vf[vf_id];
  2266. hw = &pf->hw;
  2267. ls = &pf->hw.port_info->phy.link_info;
  2268. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2269. dev_err(&pf->pdev->dev, "vf %d in reset. Try again.\n", vf_id);
  2270. return -EBUSY;
  2271. }
  2272. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  2273. pfe.severity = PF_EVENT_SEVERITY_INFO;
  2274. switch (link_state) {
  2275. case IFLA_VF_LINK_STATE_AUTO:
  2276. vf->link_forced = false;
  2277. vf->link_up = ls->link_info & ICE_AQ_LINK_UP;
  2278. break;
  2279. case IFLA_VF_LINK_STATE_ENABLE:
  2280. vf->link_forced = true;
  2281. vf->link_up = true;
  2282. break;
  2283. case IFLA_VF_LINK_STATE_DISABLE:
  2284. vf->link_forced = true;
  2285. vf->link_up = false;
  2286. break;
  2287. default:
  2288. return -EINVAL;
  2289. }
  2290. if (vf->link_forced)
  2291. ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
  2292. else
  2293. ice_set_pfe_link(vf, &pfe, ls->link_speed, vf->link_up);
  2294. /* Notify the VF of its new link state */
  2295. ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT, 0, (u8 *)&pfe,
  2296. sizeof(pfe), NULL);
  2297. return 0;
  2298. }