ice_virtchnl_pf.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664
  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_link_state - Inform all VFs on a PF of link status
  769. * @pf: pointer to the PF structure
  770. */
  771. void ice_vc_notify_link_state(struct ice_pf *pf)
  772. {
  773. int i;
  774. for (i = 0; i < pf->num_alloc_vfs; i++)
  775. ice_vc_notify_vf_link_state(&pf->vf[i]);
  776. }
  777. /**
  778. * ice_vc_notify_reset - Send pending reset message to all VFs
  779. * @pf: pointer to the PF structure
  780. *
  781. * indicate a pending reset to all VFs on a given PF
  782. */
  783. void ice_vc_notify_reset(struct ice_pf *pf)
  784. {
  785. struct virtchnl_pf_event pfe;
  786. if (!pf->num_alloc_vfs)
  787. return;
  788. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  789. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  790. ice_vc_vf_broadcast(pf, VIRTCHNL_OP_EVENT, ICE_SUCCESS,
  791. (u8 *)&pfe, sizeof(struct virtchnl_pf_event));
  792. }
  793. /**
  794. * ice_vc_notify_vf_reset - Notify VF of a reset event
  795. * @vf: pointer to the VF structure
  796. */
  797. static void ice_vc_notify_vf_reset(struct ice_vf *vf)
  798. {
  799. struct virtchnl_pf_event pfe;
  800. /* validate the request */
  801. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  802. return;
  803. /* verify if the VF is in either init or active before proceeding */
  804. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states) &&
  805. !test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
  806. return;
  807. pfe.event = VIRTCHNL_EVENT_RESET_IMPENDING;
  808. pfe.severity = PF_EVENT_SEVERITY_CERTAIN_DOOM;
  809. ice_aq_send_msg_to_vf(&vf->pf->hw, vf->vf_id, VIRTCHNL_OP_EVENT, 0,
  810. (u8 *)&pfe, sizeof(pfe), NULL);
  811. }
  812. /**
  813. * ice_alloc_vfs - Allocate and set up VFs resources
  814. * @pf: pointer to the PF structure
  815. * @num_alloc_vfs: number of VFs to allocate
  816. */
  817. static int ice_alloc_vfs(struct ice_pf *pf, u16 num_alloc_vfs)
  818. {
  819. struct ice_hw *hw = &pf->hw;
  820. struct ice_vf *vfs;
  821. int i, ret;
  822. /* Disable global interrupt 0 so we don't try to handle the VFLR. */
  823. wr32(hw, GLINT_DYN_CTL(pf->hw_oicr_idx),
  824. ICE_ITR_NONE << GLINT_DYN_CTL_ITR_INDX_S);
  825. ice_flush(hw);
  826. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  827. if (ret) {
  828. pf->num_alloc_vfs = 0;
  829. goto err_unroll_intr;
  830. }
  831. /* allocate memory */
  832. vfs = devm_kcalloc(&pf->pdev->dev, num_alloc_vfs, sizeof(*vfs),
  833. GFP_KERNEL);
  834. if (!vfs) {
  835. ret = -ENOMEM;
  836. goto err_unroll_sriov;
  837. }
  838. pf->vf = vfs;
  839. /* apply default profile */
  840. for (i = 0; i < num_alloc_vfs; i++) {
  841. vfs[i].pf = pf;
  842. vfs[i].vf_sw_id = pf->first_sw;
  843. vfs[i].vf_id = i;
  844. /* assign default capabilities */
  845. set_bit(ICE_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  846. vfs[i].spoofchk = true;
  847. /* Set this state so that PF driver does VF vector assignment */
  848. set_bit(ICE_VF_STATE_CFG_INTR, vfs[i].vf_states);
  849. }
  850. pf->num_alloc_vfs = num_alloc_vfs;
  851. /* VF resources get allocated during reset */
  852. if (!ice_reset_all_vfs(pf, false))
  853. goto err_unroll_sriov;
  854. goto err_unroll_intr;
  855. err_unroll_sriov:
  856. pci_disable_sriov(pf->pdev);
  857. err_unroll_intr:
  858. /* rearm interrupts here */
  859. ice_irq_dynamic_ena(hw, NULL, NULL);
  860. return ret;
  861. }
  862. /**
  863. * ice_pf_state_is_nominal - checks the pf for nominal state
  864. * @pf: pointer to pf to check
  865. *
  866. * Check the PF's state for a collection of bits that would indicate
  867. * the PF is in a state that would inhibit normal operation for
  868. * driver functionality.
  869. *
  870. * Returns true if PF is in a nominal state.
  871. * Returns false otherwise
  872. */
  873. static bool ice_pf_state_is_nominal(struct ice_pf *pf)
  874. {
  875. DECLARE_BITMAP(check_bits, __ICE_STATE_NBITS) = { 0 };
  876. if (!pf)
  877. return false;
  878. bitmap_set(check_bits, 0, __ICE_STATE_NOMINAL_CHECK_BITS);
  879. if (bitmap_intersects(pf->state, check_bits, __ICE_STATE_NBITS))
  880. return false;
  881. return true;
  882. }
  883. /**
  884. * ice_pci_sriov_ena - Enable or change number of VFs
  885. * @pf: pointer to the PF structure
  886. * @num_vfs: number of VFs to allocate
  887. */
  888. static int ice_pci_sriov_ena(struct ice_pf *pf, int num_vfs)
  889. {
  890. int pre_existing_vfs = pci_num_vf(pf->pdev);
  891. struct device *dev = &pf->pdev->dev;
  892. int err;
  893. if (!ice_pf_state_is_nominal(pf)) {
  894. dev_err(dev, "Cannot enable SR-IOV, device not ready\n");
  895. return -EBUSY;
  896. }
  897. if (!test_bit(ICE_FLAG_SRIOV_CAPABLE, pf->flags)) {
  898. dev_err(dev, "This device is not capable of SR-IOV\n");
  899. return -ENODEV;
  900. }
  901. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  902. ice_free_vfs(pf);
  903. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  904. return num_vfs;
  905. if (num_vfs > pf->num_vfs_supported) {
  906. dev_err(dev, "Can't enable %d VFs, max VFs supported is %d\n",
  907. num_vfs, pf->num_vfs_supported);
  908. return -ENOTSUPP;
  909. }
  910. dev_info(dev, "Allocating %d VFs\n", num_vfs);
  911. err = ice_alloc_vfs(pf, num_vfs);
  912. if (err) {
  913. dev_err(dev, "Failed to enable SR-IOV: %d\n", err);
  914. return err;
  915. }
  916. set_bit(ICE_FLAG_SRIOV_ENA, pf->flags);
  917. return num_vfs;
  918. }
  919. /**
  920. * ice_sriov_configure - Enable or change number of VFs via sysfs
  921. * @pdev: pointer to a pci_dev structure
  922. * @num_vfs: number of VFs to allocate
  923. *
  924. * This function is called when the user updates the number of VFs in sysfs.
  925. */
  926. int ice_sriov_configure(struct pci_dev *pdev, int num_vfs)
  927. {
  928. struct ice_pf *pf = pci_get_drvdata(pdev);
  929. if (num_vfs)
  930. return ice_pci_sriov_ena(pf, num_vfs);
  931. if (!pci_vfs_assigned(pdev)) {
  932. ice_free_vfs(pf);
  933. } else {
  934. dev_err(&pf->pdev->dev,
  935. "can't free VFs because some are assigned to VMs.\n");
  936. return -EBUSY;
  937. }
  938. return 0;
  939. }
  940. /**
  941. * ice_process_vflr_event - Free VF resources via IRQ calls
  942. * @pf: pointer to the PF structure
  943. *
  944. * called from the VLFR IRQ handler to
  945. * free up VF resources and state variables
  946. */
  947. void ice_process_vflr_event(struct ice_pf *pf)
  948. {
  949. struct ice_hw *hw = &pf->hw;
  950. int vf_id;
  951. u32 reg;
  952. if (!test_bit(__ICE_VFLR_EVENT_PENDING, pf->state) ||
  953. !pf->num_alloc_vfs)
  954. return;
  955. /* Re-enable the VFLR interrupt cause here, before looking for which
  956. * VF got reset. Otherwise, if another VF gets a reset while the
  957. * first one is being processed, that interrupt will be lost, and
  958. * that VF will be stuck in reset forever.
  959. */
  960. reg = rd32(hw, PFINT_OICR_ENA);
  961. reg |= PFINT_OICR_VFLR_M;
  962. wr32(hw, PFINT_OICR_ENA, reg);
  963. ice_flush(hw);
  964. clear_bit(__ICE_VFLR_EVENT_PENDING, pf->state);
  965. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  966. struct ice_vf *vf = &pf->vf[vf_id];
  967. u32 reg_idx, bit_idx;
  968. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  969. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  970. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  971. reg = rd32(hw, GLGEN_VFLRSTAT(reg_idx));
  972. if (reg & BIT(bit_idx))
  973. /* GLGEN_VFLRSTAT bit will be cleared in ice_reset_vf */
  974. ice_reset_vf(vf, true);
  975. }
  976. }
  977. /**
  978. * ice_vc_dis_vf - Disable a given VF via SW reset
  979. * @vf: pointer to the VF info
  980. *
  981. * Disable the VF through a SW reset
  982. */
  983. static void ice_vc_dis_vf(struct ice_vf *vf)
  984. {
  985. ice_vc_notify_vf_reset(vf);
  986. ice_reset_vf(vf, false);
  987. }
  988. /**
  989. * ice_vc_send_msg_to_vf - Send message to VF
  990. * @vf: pointer to the VF info
  991. * @v_opcode: virtual channel opcode
  992. * @v_retval: virtual channel return value
  993. * @msg: pointer to the msg buffer
  994. * @msglen: msg length
  995. *
  996. * send msg to VF
  997. */
  998. static int ice_vc_send_msg_to_vf(struct ice_vf *vf, u32 v_opcode,
  999. enum ice_status v_retval, u8 *msg, u16 msglen)
  1000. {
  1001. enum ice_status aq_ret;
  1002. struct ice_pf *pf;
  1003. /* validate the request */
  1004. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  1005. return -EINVAL;
  1006. pf = vf->pf;
  1007. /* single place to detect unsuccessful return values */
  1008. if (v_retval) {
  1009. vf->num_inval_msgs++;
  1010. dev_info(&pf->pdev->dev, "VF %d failed opcode %d, retval: %d\n",
  1011. vf->vf_id, v_opcode, v_retval);
  1012. if (vf->num_inval_msgs > ICE_DFLT_NUM_INVAL_MSGS_ALLOWED) {
  1013. dev_err(&pf->pdev->dev,
  1014. "Number of invalid messages exceeded for VF %d\n",
  1015. vf->vf_id);
  1016. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  1017. set_bit(ICE_VF_STATE_DIS, vf->vf_states);
  1018. return -EIO;
  1019. }
  1020. } else {
  1021. vf->num_valid_msgs++;
  1022. /* reset the invalid counter, if a valid message is received. */
  1023. vf->num_inval_msgs = 0;
  1024. }
  1025. aq_ret = ice_aq_send_msg_to_vf(&pf->hw, vf->vf_id, v_opcode, v_retval,
  1026. msg, msglen, NULL);
  1027. if (aq_ret) {
  1028. dev_info(&pf->pdev->dev,
  1029. "Unable to send the message to VF %d aq_err %d\n",
  1030. vf->vf_id, pf->hw.mailboxq.sq_last_status);
  1031. return -EIO;
  1032. }
  1033. return 0;
  1034. }
  1035. /**
  1036. * ice_vc_get_ver_msg
  1037. * @vf: pointer to the VF info
  1038. * @msg: pointer to the msg buffer
  1039. *
  1040. * called from the VF to request the API version used by the PF
  1041. */
  1042. static int ice_vc_get_ver_msg(struct ice_vf *vf, u8 *msg)
  1043. {
  1044. struct virtchnl_version_info info = {
  1045. VIRTCHNL_VERSION_MAJOR, VIRTCHNL_VERSION_MINOR
  1046. };
  1047. vf->vf_ver = *(struct virtchnl_version_info *)msg;
  1048. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1049. if (VF_IS_V10(&vf->vf_ver))
  1050. info.minor = VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1051. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_VERSION, ICE_SUCCESS,
  1052. (u8 *)&info,
  1053. sizeof(struct virtchnl_version_info));
  1054. }
  1055. /**
  1056. * ice_vc_get_vf_res_msg
  1057. * @vf: pointer to the VF info
  1058. * @msg: pointer to the msg buffer
  1059. *
  1060. * called from the VF to request its resources
  1061. */
  1062. static int ice_vc_get_vf_res_msg(struct ice_vf *vf, u8 *msg)
  1063. {
  1064. struct virtchnl_vf_resource *vfres = NULL;
  1065. enum ice_status aq_ret = 0;
  1066. struct ice_pf *pf = vf->pf;
  1067. struct ice_vsi *vsi;
  1068. int len = 0;
  1069. int ret;
  1070. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  1071. aq_ret = ICE_ERR_PARAM;
  1072. goto err;
  1073. }
  1074. len = sizeof(struct virtchnl_vf_resource);
  1075. vfres = devm_kzalloc(&pf->pdev->dev, len, GFP_KERNEL);
  1076. if (!vfres) {
  1077. aq_ret = ICE_ERR_NO_MEMORY;
  1078. len = 0;
  1079. goto err;
  1080. }
  1081. if (VF_IS_V11(&vf->vf_ver))
  1082. vf->driver_caps = *(u32 *)msg;
  1083. else
  1084. vf->driver_caps = VIRTCHNL_VF_OFFLOAD_L2 |
  1085. VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1086. VIRTCHNL_VF_OFFLOAD_VLAN;
  1087. vfres->vf_cap_flags = VIRTCHNL_VF_OFFLOAD_L2;
  1088. vsi = pf->vsi[vf->lan_vsi_idx];
  1089. if (!vsi->info.pvid)
  1090. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_VLAN;
  1091. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  1092. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PF;
  1093. } else {
  1094. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_AQ)
  1095. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1096. else
  1097. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1098. }
  1099. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1100. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2;
  1101. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP)
  1102. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP;
  1103. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM)
  1104. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM;
  1105. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_RX_POLLING)
  1106. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1107. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  1108. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_WB_ON_ITR;
  1109. if (vf->driver_caps & VIRTCHNL_VF_OFFLOAD_REQ_QUEUES)
  1110. vfres->vf_cap_flags |= VIRTCHNL_VF_OFFLOAD_REQ_QUEUES;
  1111. if (vf->driver_caps & VIRTCHNL_VF_CAP_ADV_LINK_SPEED)
  1112. vfres->vf_cap_flags |= VIRTCHNL_VF_CAP_ADV_LINK_SPEED;
  1113. vfres->num_vsis = 1;
  1114. /* Tx and Rx queue are equal for VF */
  1115. vfres->num_queue_pairs = vsi->num_txq;
  1116. vfres->max_vectors = pf->num_vf_msix;
  1117. vfres->rss_key_size = ICE_VSIQF_HKEY_ARRAY_SIZE;
  1118. vfres->rss_lut_size = ICE_VSIQF_HLUT_ARRAY_SIZE;
  1119. vfres->vsi_res[0].vsi_id = vf->lan_vsi_num;
  1120. vfres->vsi_res[0].vsi_type = VIRTCHNL_VSI_SRIOV;
  1121. vfres->vsi_res[0].num_queue_pairs = vsi->num_txq;
  1122. ether_addr_copy(vfres->vsi_res[0].default_mac_addr,
  1123. vf->dflt_lan_addr.addr);
  1124. set_bit(ICE_VF_STATE_ACTIVE, vf->vf_states);
  1125. err:
  1126. /* send the response back to the VF */
  1127. ret = ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_VF_RESOURCES, aq_ret,
  1128. (u8 *)vfres, len);
  1129. devm_kfree(&pf->pdev->dev, vfres);
  1130. return ret;
  1131. }
  1132. /**
  1133. * ice_vc_reset_vf_msg
  1134. * @vf: pointer to the VF info
  1135. *
  1136. * called from the VF to reset itself,
  1137. * unlike other virtchnl messages, PF driver
  1138. * doesn't send the response back to the VF
  1139. */
  1140. static void ice_vc_reset_vf_msg(struct ice_vf *vf)
  1141. {
  1142. if (test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states))
  1143. ice_reset_vf(vf, false);
  1144. }
  1145. /**
  1146. * ice_find_vsi_from_id
  1147. * @pf: the pf structure to search for the VSI
  1148. * @id: id of the VSI it is searching for
  1149. *
  1150. * searches for the VSI with the given id
  1151. */
  1152. static struct ice_vsi *ice_find_vsi_from_id(struct ice_pf *pf, u16 id)
  1153. {
  1154. int i;
  1155. for (i = 0; i < pf->num_alloc_vsi; i++)
  1156. if (pf->vsi[i] && pf->vsi[i]->vsi_num == id)
  1157. return pf->vsi[i];
  1158. return NULL;
  1159. }
  1160. /**
  1161. * ice_vc_isvalid_vsi_id
  1162. * @vf: pointer to the VF info
  1163. * @vsi_id: VF relative VSI id
  1164. *
  1165. * check for the valid VSI id
  1166. */
  1167. static bool ice_vc_isvalid_vsi_id(struct ice_vf *vf, u16 vsi_id)
  1168. {
  1169. struct ice_pf *pf = vf->pf;
  1170. struct ice_vsi *vsi;
  1171. vsi = ice_find_vsi_from_id(pf, vsi_id);
  1172. return (vsi && (vsi->vf_id == vf->vf_id));
  1173. }
  1174. /**
  1175. * ice_vc_isvalid_q_id
  1176. * @vf: pointer to the VF info
  1177. * @vsi_id: VSI id
  1178. * @qid: VSI relative queue id
  1179. *
  1180. * check for the valid queue id
  1181. */
  1182. static bool ice_vc_isvalid_q_id(struct ice_vf *vf, u16 vsi_id, u8 qid)
  1183. {
  1184. struct ice_vsi *vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
  1185. /* allocated Tx and Rx queues should be always equal for VF VSI */
  1186. return (vsi && (qid < vsi->alloc_txq));
  1187. }
  1188. /**
  1189. * ice_vc_config_rss_key
  1190. * @vf: pointer to the VF info
  1191. * @msg: pointer to the msg buffer
  1192. *
  1193. * Configure the VF's RSS key
  1194. */
  1195. static int ice_vc_config_rss_key(struct ice_vf *vf, u8 *msg)
  1196. {
  1197. struct virtchnl_rss_key *vrk =
  1198. (struct virtchnl_rss_key *)msg;
  1199. struct ice_vsi *vsi = NULL;
  1200. enum ice_status aq_ret;
  1201. int ret;
  1202. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1203. aq_ret = ICE_ERR_PARAM;
  1204. goto error_param;
  1205. }
  1206. if (!ice_vc_isvalid_vsi_id(vf, vrk->vsi_id)) {
  1207. aq_ret = ICE_ERR_PARAM;
  1208. goto error_param;
  1209. }
  1210. vsi = ice_find_vsi_from_id(vf->pf, vrk->vsi_id);
  1211. if (!vsi) {
  1212. aq_ret = ICE_ERR_PARAM;
  1213. goto error_param;
  1214. }
  1215. if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE) {
  1216. aq_ret = ICE_ERR_PARAM;
  1217. goto error_param;
  1218. }
  1219. if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
  1220. aq_ret = ICE_ERR_PARAM;
  1221. goto error_param;
  1222. }
  1223. ret = ice_set_rss(vsi, vrk->key, NULL, 0);
  1224. aq_ret = ret ? ICE_ERR_PARAM : ICE_SUCCESS;
  1225. error_param:
  1226. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_KEY, aq_ret,
  1227. NULL, 0);
  1228. }
  1229. /**
  1230. * ice_vc_config_rss_lut
  1231. * @vf: pointer to the VF info
  1232. * @msg: pointer to the msg buffer
  1233. *
  1234. * Configure the VF's RSS LUT
  1235. */
  1236. static int ice_vc_config_rss_lut(struct ice_vf *vf, u8 *msg)
  1237. {
  1238. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  1239. struct ice_vsi *vsi = NULL;
  1240. enum ice_status aq_ret;
  1241. int ret;
  1242. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1243. aq_ret = ICE_ERR_PARAM;
  1244. goto error_param;
  1245. }
  1246. if (!ice_vc_isvalid_vsi_id(vf, vrl->vsi_id)) {
  1247. aq_ret = ICE_ERR_PARAM;
  1248. goto error_param;
  1249. }
  1250. vsi = ice_find_vsi_from_id(vf->pf, vrl->vsi_id);
  1251. if (!vsi) {
  1252. aq_ret = ICE_ERR_PARAM;
  1253. goto error_param;
  1254. }
  1255. if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE) {
  1256. aq_ret = ICE_ERR_PARAM;
  1257. goto error_param;
  1258. }
  1259. if (!test_bit(ICE_FLAG_RSS_ENA, vf->pf->flags)) {
  1260. aq_ret = ICE_ERR_PARAM;
  1261. goto error_param;
  1262. }
  1263. ret = ice_set_rss(vsi, NULL, vrl->lut, ICE_VSIQF_HLUT_ARRAY_SIZE);
  1264. aq_ret = ret ? ICE_ERR_PARAM : ICE_SUCCESS;
  1265. error_param:
  1266. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_RSS_LUT, aq_ret,
  1267. NULL, 0);
  1268. }
  1269. /**
  1270. * ice_vc_get_stats_msg
  1271. * @vf: pointer to the VF info
  1272. * @msg: pointer to the msg buffer
  1273. *
  1274. * called from the VF to get VSI stats
  1275. */
  1276. static int ice_vc_get_stats_msg(struct ice_vf *vf, u8 *msg)
  1277. {
  1278. struct virtchnl_queue_select *vqs =
  1279. (struct virtchnl_queue_select *)msg;
  1280. enum ice_status aq_ret = 0;
  1281. struct ice_eth_stats stats;
  1282. struct ice_vsi *vsi;
  1283. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1284. aq_ret = ICE_ERR_PARAM;
  1285. goto error_param;
  1286. }
  1287. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1288. aq_ret = ICE_ERR_PARAM;
  1289. goto error_param;
  1290. }
  1291. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1292. if (!vsi) {
  1293. aq_ret = ICE_ERR_PARAM;
  1294. goto error_param;
  1295. }
  1296. memset(&stats, 0, sizeof(struct ice_eth_stats));
  1297. ice_update_eth_stats(vsi);
  1298. stats = vsi->eth_stats;
  1299. error_param:
  1300. /* send the response to the VF */
  1301. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_GET_STATS, aq_ret,
  1302. (u8 *)&stats, sizeof(stats));
  1303. }
  1304. /**
  1305. * ice_vc_ena_qs_msg
  1306. * @vf: pointer to the VF info
  1307. * @msg: pointer to the msg buffer
  1308. *
  1309. * called from the VF to enable all or specific queue(s)
  1310. */
  1311. static int ice_vc_ena_qs_msg(struct ice_vf *vf, u8 *msg)
  1312. {
  1313. struct virtchnl_queue_select *vqs =
  1314. (struct virtchnl_queue_select *)msg;
  1315. enum ice_status aq_ret = 0;
  1316. struct ice_vsi *vsi;
  1317. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1318. aq_ret = ICE_ERR_PARAM;
  1319. goto error_param;
  1320. }
  1321. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1322. aq_ret = ICE_ERR_PARAM;
  1323. goto error_param;
  1324. }
  1325. if (!vqs->rx_queues && !vqs->tx_queues) {
  1326. aq_ret = ICE_ERR_PARAM;
  1327. goto error_param;
  1328. }
  1329. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1330. if (!vsi) {
  1331. aq_ret = ICE_ERR_PARAM;
  1332. goto error_param;
  1333. }
  1334. /* Enable only Rx rings, Tx rings were enabled by the FW when the
  1335. * Tx queue group list was configured and the context bits were
  1336. * programmed using ice_vsi_cfg_txqs
  1337. */
  1338. if (ice_vsi_start_rx_rings(vsi))
  1339. aq_ret = ICE_ERR_PARAM;
  1340. /* Set flag to indicate that queues are enabled */
  1341. if (!aq_ret)
  1342. set_bit(ICE_VF_STATE_ENA, vf->vf_states);
  1343. error_param:
  1344. /* send the response to the VF */
  1345. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_QUEUES, aq_ret,
  1346. NULL, 0);
  1347. }
  1348. /**
  1349. * ice_vc_dis_qs_msg
  1350. * @vf: pointer to the VF info
  1351. * @msg: pointer to the msg buffer
  1352. *
  1353. * called from the VF to disable all or specific
  1354. * queue(s)
  1355. */
  1356. static int ice_vc_dis_qs_msg(struct ice_vf *vf, u8 *msg)
  1357. {
  1358. struct virtchnl_queue_select *vqs =
  1359. (struct virtchnl_queue_select *)msg;
  1360. enum ice_status aq_ret = 0;
  1361. struct ice_vsi *vsi;
  1362. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) &&
  1363. !test_bit(ICE_VF_STATE_ENA, vf->vf_states)) {
  1364. aq_ret = ICE_ERR_PARAM;
  1365. goto error_param;
  1366. }
  1367. if (!ice_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1368. aq_ret = ICE_ERR_PARAM;
  1369. goto error_param;
  1370. }
  1371. if (!vqs->rx_queues && !vqs->tx_queues) {
  1372. aq_ret = ICE_ERR_PARAM;
  1373. goto error_param;
  1374. }
  1375. vsi = ice_find_vsi_from_id(vf->pf, vqs->vsi_id);
  1376. if (!vsi) {
  1377. aq_ret = ICE_ERR_PARAM;
  1378. goto error_param;
  1379. }
  1380. if (ice_vsi_stop_tx_rings(vsi, ICE_NO_RESET, vf->vf_id)) {
  1381. dev_err(&vsi->back->pdev->dev,
  1382. "Failed to stop tx rings on VSI %d\n",
  1383. vsi->vsi_num);
  1384. aq_ret = ICE_ERR_PARAM;
  1385. }
  1386. if (ice_vsi_stop_rx_rings(vsi)) {
  1387. dev_err(&vsi->back->pdev->dev,
  1388. "Failed to stop rx rings on VSI %d\n",
  1389. vsi->vsi_num);
  1390. aq_ret = ICE_ERR_PARAM;
  1391. }
  1392. /* Clear enabled queues flag */
  1393. if (!aq_ret)
  1394. clear_bit(ICE_VF_STATE_ENA, vf->vf_states);
  1395. error_param:
  1396. /* send the response to the VF */
  1397. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_QUEUES, aq_ret,
  1398. NULL, 0);
  1399. }
  1400. /**
  1401. * ice_vc_cfg_irq_map_msg
  1402. * @vf: pointer to the VF info
  1403. * @msg: pointer to the msg buffer
  1404. *
  1405. * called from the VF to configure the IRQ to queue map
  1406. */
  1407. static int ice_vc_cfg_irq_map_msg(struct ice_vf *vf, u8 *msg)
  1408. {
  1409. struct virtchnl_irq_map_info *irqmap_info =
  1410. (struct virtchnl_irq_map_info *)msg;
  1411. u16 vsi_id, vsi_q_id, vector_id;
  1412. struct virtchnl_vector_map *map;
  1413. struct ice_vsi *vsi = NULL;
  1414. struct ice_pf *pf = vf->pf;
  1415. enum ice_status aq_ret = 0;
  1416. unsigned long qmap;
  1417. int i;
  1418. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1419. aq_ret = ICE_ERR_PARAM;
  1420. goto error_param;
  1421. }
  1422. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1423. map = &irqmap_info->vecmap[i];
  1424. vector_id = map->vector_id;
  1425. vsi_id = map->vsi_id;
  1426. /* validate msg params */
  1427. if (!(vector_id < pf->hw.func_caps.common_cap
  1428. .num_msix_vectors) || !ice_vc_isvalid_vsi_id(vf, vsi_id)) {
  1429. aq_ret = ICE_ERR_PARAM;
  1430. goto error_param;
  1431. }
  1432. vsi = ice_find_vsi_from_id(vf->pf, vsi_id);
  1433. if (!vsi) {
  1434. aq_ret = ICE_ERR_PARAM;
  1435. goto error_param;
  1436. }
  1437. /* lookout for the invalid queue index */
  1438. qmap = map->rxq_map;
  1439. for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
  1440. if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
  1441. aq_ret = ICE_ERR_PARAM;
  1442. goto error_param;
  1443. }
  1444. vsi->q_vectors[i]->num_ring_rx++;
  1445. vsi->rx_rings[vsi_q_id]->itr_setting =
  1446. map->rxitr_idx;
  1447. vsi->rx_rings[vsi_q_id]->q_vector = vsi->q_vectors[i];
  1448. }
  1449. qmap = map->txq_map;
  1450. for_each_set_bit(vsi_q_id, &qmap, ICE_MAX_BASE_QS_PER_VF) {
  1451. if (!ice_vc_isvalid_q_id(vf, vsi_id, vsi_q_id)) {
  1452. aq_ret = ICE_ERR_PARAM;
  1453. goto error_param;
  1454. }
  1455. vsi->q_vectors[i]->num_ring_tx++;
  1456. vsi->tx_rings[vsi_q_id]->itr_setting =
  1457. map->txitr_idx;
  1458. vsi->tx_rings[vsi_q_id]->q_vector = vsi->q_vectors[i];
  1459. }
  1460. }
  1461. if (vsi)
  1462. ice_vsi_cfg_msix(vsi);
  1463. error_param:
  1464. /* send the response to the VF */
  1465. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_IRQ_MAP, aq_ret,
  1466. NULL, 0);
  1467. }
  1468. /**
  1469. * ice_vc_cfg_qs_msg
  1470. * @vf: pointer to the VF info
  1471. * @msg: pointer to the msg buffer
  1472. *
  1473. * called from the VF to configure the Rx/Tx queues
  1474. */
  1475. static int ice_vc_cfg_qs_msg(struct ice_vf *vf, u8 *msg)
  1476. {
  1477. struct virtchnl_vsi_queue_config_info *qci =
  1478. (struct virtchnl_vsi_queue_config_info *)msg;
  1479. struct virtchnl_queue_pair_info *qpi;
  1480. enum ice_status aq_ret = 0;
  1481. struct ice_vsi *vsi;
  1482. int i;
  1483. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1484. aq_ret = ICE_ERR_PARAM;
  1485. goto error_param;
  1486. }
  1487. if (!ice_vc_isvalid_vsi_id(vf, qci->vsi_id)) {
  1488. aq_ret = ICE_ERR_PARAM;
  1489. goto error_param;
  1490. }
  1491. vsi = ice_find_vsi_from_id(vf->pf, qci->vsi_id);
  1492. if (!vsi) {
  1493. aq_ret = ICE_ERR_PARAM;
  1494. goto error_param;
  1495. }
  1496. for (i = 0; i < qci->num_queue_pairs; i++) {
  1497. qpi = &qci->qpair[i];
  1498. if (qpi->txq.vsi_id != qci->vsi_id ||
  1499. qpi->rxq.vsi_id != qci->vsi_id ||
  1500. qpi->rxq.queue_id != qpi->txq.queue_id ||
  1501. !ice_vc_isvalid_q_id(vf, qci->vsi_id, qpi->txq.queue_id)) {
  1502. aq_ret = ICE_ERR_PARAM;
  1503. goto error_param;
  1504. }
  1505. /* copy Tx queue info from VF into VSI */
  1506. vsi->tx_rings[i]->dma = qpi->txq.dma_ring_addr;
  1507. vsi->tx_rings[i]->count = qpi->txq.ring_len;
  1508. /* copy Rx queue info from VF into vsi */
  1509. vsi->rx_rings[i]->dma = qpi->rxq.dma_ring_addr;
  1510. vsi->rx_rings[i]->count = qpi->rxq.ring_len;
  1511. if (qpi->rxq.databuffer_size > ((16 * 1024) - 128)) {
  1512. aq_ret = ICE_ERR_PARAM;
  1513. goto error_param;
  1514. }
  1515. vsi->rx_buf_len = qpi->rxq.databuffer_size;
  1516. if (qpi->rxq.max_pkt_size >= (16 * 1024) ||
  1517. qpi->rxq.max_pkt_size < 64) {
  1518. aq_ret = ICE_ERR_PARAM;
  1519. goto error_param;
  1520. }
  1521. vsi->max_frame = qpi->rxq.max_pkt_size;
  1522. }
  1523. /* VF can request to configure less than allocated queues
  1524. * or default allocated queues. So update the VSI with new number
  1525. */
  1526. vsi->num_txq = qci->num_queue_pairs;
  1527. vsi->num_rxq = qci->num_queue_pairs;
  1528. if (!ice_vsi_cfg_txqs(vsi) && !ice_vsi_cfg_rxqs(vsi))
  1529. aq_ret = 0;
  1530. else
  1531. aq_ret = ICE_ERR_PARAM;
  1532. error_param:
  1533. /* send the response to the VF */
  1534. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_CONFIG_VSI_QUEUES, aq_ret,
  1535. NULL, 0);
  1536. }
  1537. /**
  1538. * ice_is_vf_trusted
  1539. * @vf: pointer to the VF info
  1540. */
  1541. static bool ice_is_vf_trusted(struct ice_vf *vf)
  1542. {
  1543. return test_bit(ICE_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  1544. }
  1545. /**
  1546. * ice_can_vf_change_mac
  1547. * @vf: pointer to the VF info
  1548. *
  1549. * Return true if the VF is allowed to change its MAC filters, false otherwise
  1550. */
  1551. static bool ice_can_vf_change_mac(struct ice_vf *vf)
  1552. {
  1553. /* If the VF MAC address has been set administratively (via the
  1554. * ndo_set_vf_mac command), then deny permission to the VF to
  1555. * add/delete unicast MAC addresses, unless the VF is trusted
  1556. */
  1557. if (vf->pf_set_mac && !ice_is_vf_trusted(vf))
  1558. return false;
  1559. return true;
  1560. }
  1561. /**
  1562. * ice_vc_handle_mac_addr_msg
  1563. * @vf: pointer to the VF info
  1564. * @msg: pointer to the msg buffer
  1565. * @set: true if mac filters are being set, false otherwise
  1566. *
  1567. * add guest mac address filter
  1568. */
  1569. static int
  1570. ice_vc_handle_mac_addr_msg(struct ice_vf *vf, u8 *msg, bool set)
  1571. {
  1572. struct virtchnl_ether_addr_list *al =
  1573. (struct virtchnl_ether_addr_list *)msg;
  1574. struct ice_pf *pf = vf->pf;
  1575. enum virtchnl_ops vc_op;
  1576. enum ice_status ret;
  1577. LIST_HEAD(mac_list);
  1578. struct ice_vsi *vsi;
  1579. int mac_count = 0;
  1580. int i;
  1581. if (set)
  1582. vc_op = VIRTCHNL_OP_ADD_ETH_ADDR;
  1583. else
  1584. vc_op = VIRTCHNL_OP_DEL_ETH_ADDR;
  1585. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states) ||
  1586. !ice_vc_isvalid_vsi_id(vf, al->vsi_id)) {
  1587. ret = ICE_ERR_PARAM;
  1588. goto handle_mac_exit;
  1589. }
  1590. if (set && !ice_is_vf_trusted(vf) &&
  1591. (vf->num_mac + al->num_elements) > ICE_MAX_MACADDR_PER_VF) {
  1592. dev_err(&pf->pdev->dev,
  1593. "Can't add more MAC addresses, because VF is not trusted, switch the VF to trusted mode in order to add more functionalities\n");
  1594. ret = ICE_ERR_PARAM;
  1595. goto handle_mac_exit;
  1596. }
  1597. vsi = pf->vsi[vf->lan_vsi_idx];
  1598. for (i = 0; i < al->num_elements; i++) {
  1599. u8 *maddr = al->list[i].addr;
  1600. if (ether_addr_equal(maddr, vf->dflt_lan_addr.addr) ||
  1601. is_broadcast_ether_addr(maddr)) {
  1602. if (set) {
  1603. /* VF is trying to add filters that the PF
  1604. * already added. Just continue.
  1605. */
  1606. dev_info(&pf->pdev->dev,
  1607. "mac %pM already set for VF %d\n",
  1608. maddr, vf->vf_id);
  1609. continue;
  1610. } else {
  1611. /* VF can't remove dflt_lan_addr/bcast mac */
  1612. dev_err(&pf->pdev->dev,
  1613. "can't remove mac %pM for VF %d\n",
  1614. maddr, vf->vf_id);
  1615. ret = ICE_ERR_PARAM;
  1616. goto handle_mac_exit;
  1617. }
  1618. }
  1619. /* check for the invalid cases and bail if necessary */
  1620. if (is_zero_ether_addr(maddr)) {
  1621. dev_err(&pf->pdev->dev,
  1622. "invalid mac %pM provided for VF %d\n",
  1623. maddr, vf->vf_id);
  1624. ret = ICE_ERR_PARAM;
  1625. goto handle_mac_exit;
  1626. }
  1627. if (is_unicast_ether_addr(maddr) &&
  1628. !ice_can_vf_change_mac(vf)) {
  1629. dev_err(&pf->pdev->dev,
  1630. "can't change unicast mac for untrusted VF %d\n",
  1631. vf->vf_id);
  1632. ret = ICE_ERR_PARAM;
  1633. goto handle_mac_exit;
  1634. }
  1635. /* get here if maddr is multicast or if VF can change mac */
  1636. if (ice_add_mac_to_list(vsi, &mac_list, al->list[i].addr)) {
  1637. ret = ICE_ERR_NO_MEMORY;
  1638. goto handle_mac_exit;
  1639. }
  1640. mac_count++;
  1641. }
  1642. /* program the updated filter list */
  1643. if (set)
  1644. ret = ice_add_mac(&pf->hw, &mac_list);
  1645. else
  1646. ret = ice_remove_mac(&pf->hw, &mac_list);
  1647. if (ret) {
  1648. dev_err(&pf->pdev->dev,
  1649. "can't update mac filters for VF %d, error %d\n",
  1650. vf->vf_id, ret);
  1651. } else {
  1652. if (set)
  1653. vf->num_mac += mac_count;
  1654. else
  1655. vf->num_mac -= mac_count;
  1656. }
  1657. handle_mac_exit:
  1658. ice_free_fltr_list(&pf->pdev->dev, &mac_list);
  1659. /* send the response to the VF */
  1660. return ice_vc_send_msg_to_vf(vf, vc_op, ret, NULL, 0);
  1661. }
  1662. /**
  1663. * ice_vc_add_mac_addr_msg
  1664. * @vf: pointer to the VF info
  1665. * @msg: pointer to the msg buffer
  1666. *
  1667. * add guest MAC address filter
  1668. */
  1669. static int ice_vc_add_mac_addr_msg(struct ice_vf *vf, u8 *msg)
  1670. {
  1671. return ice_vc_handle_mac_addr_msg(vf, msg, true);
  1672. }
  1673. /**
  1674. * ice_vc_del_mac_addr_msg
  1675. * @vf: pointer to the VF info
  1676. * @msg: pointer to the msg buffer
  1677. *
  1678. * remove guest MAC address filter
  1679. */
  1680. static int ice_vc_del_mac_addr_msg(struct ice_vf *vf, u8 *msg)
  1681. {
  1682. return ice_vc_handle_mac_addr_msg(vf, msg, false);
  1683. }
  1684. /**
  1685. * ice_vc_request_qs_msg
  1686. * @vf: pointer to the VF info
  1687. * @msg: pointer to the msg buffer
  1688. *
  1689. * VFs get a default number of queues but can use this message to request a
  1690. * different number. If the request is successful, PF will reset the VF and
  1691. * return 0. If unsuccessful, PF will send message informing VF of number of
  1692. * available queue pairs via virtchnl message response to VF.
  1693. */
  1694. static int ice_vc_request_qs_msg(struct ice_vf *vf, u8 *msg)
  1695. {
  1696. struct virtchnl_vf_res_request *vfres =
  1697. (struct virtchnl_vf_res_request *)msg;
  1698. int req_queues = vfres->num_queue_pairs;
  1699. enum ice_status aq_ret = 0;
  1700. struct ice_pf *pf = vf->pf;
  1701. int tx_rx_queue_left;
  1702. int cur_queues;
  1703. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1704. aq_ret = ICE_ERR_PARAM;
  1705. goto error_param;
  1706. }
  1707. cur_queues = pf->num_vf_qps;
  1708. tx_rx_queue_left = min_t(int, pf->q_left_tx, pf->q_left_rx);
  1709. if (req_queues <= 0) {
  1710. dev_err(&pf->pdev->dev,
  1711. "VF %d tried to request %d queues. Ignoring.\n",
  1712. vf->vf_id, req_queues);
  1713. } else if (req_queues > ICE_MAX_QS_PER_VF) {
  1714. dev_err(&pf->pdev->dev,
  1715. "VF %d tried to request more than %d queues.\n",
  1716. vf->vf_id, ICE_MAX_QS_PER_VF);
  1717. vfres->num_queue_pairs = ICE_MAX_QS_PER_VF;
  1718. } else if (req_queues - cur_queues > tx_rx_queue_left) {
  1719. dev_warn(&pf->pdev->dev,
  1720. "VF %d requested %d more queues, but only %d left.\n",
  1721. vf->vf_id, req_queues - cur_queues, tx_rx_queue_left);
  1722. vfres->num_queue_pairs = tx_rx_queue_left + cur_queues;
  1723. } else {
  1724. /* request is successful, then reset VF */
  1725. vf->num_req_qs = req_queues;
  1726. ice_vc_dis_vf(vf);
  1727. dev_info(&pf->pdev->dev,
  1728. "VF %d granted request of %d queues.\n",
  1729. vf->vf_id, req_queues);
  1730. return 0;
  1731. }
  1732. error_param:
  1733. /* send the response to the VF */
  1734. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_REQUEST_QUEUES,
  1735. aq_ret, (u8 *)vfres, sizeof(*vfres));
  1736. }
  1737. /**
  1738. * ice_set_vf_port_vlan
  1739. * @netdev: network interface device structure
  1740. * @vf_id: VF identifier
  1741. * @vlan_id: VLAN id being set
  1742. * @qos: priority setting
  1743. * @vlan_proto: VLAN protocol
  1744. *
  1745. * program VF Port VLAN id and/or qos
  1746. */
  1747. int
  1748. ice_set_vf_port_vlan(struct net_device *netdev, int vf_id, u16 vlan_id, u8 qos,
  1749. __be16 vlan_proto)
  1750. {
  1751. u16 vlanprio = vlan_id | (qos << ICE_VLAN_PRIORITY_S);
  1752. struct ice_netdev_priv *np = netdev_priv(netdev);
  1753. struct ice_pf *pf = np->vsi->back;
  1754. struct ice_vsi *vsi;
  1755. struct ice_vf *vf;
  1756. int ret = 0;
  1757. /* validate the request */
  1758. if (vf_id >= pf->num_alloc_vfs) {
  1759. dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
  1760. return -EINVAL;
  1761. }
  1762. if (vlan_id > ICE_MAX_VLANID || qos > 7) {
  1763. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  1764. return -EINVAL;
  1765. }
  1766. if (vlan_proto != htons(ETH_P_8021Q)) {
  1767. dev_err(&pf->pdev->dev, "VF VLAN protocol is not supported\n");
  1768. return -EPROTONOSUPPORT;
  1769. }
  1770. vf = &pf->vf[vf_id];
  1771. vsi = pf->vsi[vf->lan_vsi_idx];
  1772. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  1773. dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
  1774. return -EBUSY;
  1775. }
  1776. if (le16_to_cpu(vsi->info.pvid) == vlanprio) {
  1777. /* duplicate request, so just return success */
  1778. dev_info(&pf->pdev->dev,
  1779. "Duplicate pvid %d request\n", vlanprio);
  1780. return ret;
  1781. }
  1782. /* If pvid, then remove all filters on the old VLAN */
  1783. if (vsi->info.pvid)
  1784. ice_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
  1785. VLAN_VID_MASK));
  1786. if (vlan_id || qos) {
  1787. ret = ice_vsi_set_pvid(vsi, vlanprio);
  1788. if (ret)
  1789. goto error_set_pvid;
  1790. } else {
  1791. ice_vsi_kill_pvid(vsi);
  1792. }
  1793. if (vlan_id) {
  1794. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  1795. vlan_id, qos, vf_id);
  1796. /* add new VLAN filter for each MAC */
  1797. ret = ice_vsi_add_vlan(vsi, vlan_id);
  1798. if (ret)
  1799. goto error_set_pvid;
  1800. }
  1801. /* The Port VLAN needs to be saved across resets the same as the
  1802. * default LAN MAC address.
  1803. */
  1804. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  1805. error_set_pvid:
  1806. return ret;
  1807. }
  1808. /**
  1809. * ice_vc_process_vlan_msg
  1810. * @vf: pointer to the VF info
  1811. * @msg: pointer to the msg buffer
  1812. * @add_v: Add VLAN if true, otherwise delete VLAN
  1813. *
  1814. * Process virtchnl op to add or remove programmed guest VLAN id
  1815. */
  1816. static int ice_vc_process_vlan_msg(struct ice_vf *vf, u8 *msg, bool add_v)
  1817. {
  1818. struct virtchnl_vlan_filter_list *vfl =
  1819. (struct virtchnl_vlan_filter_list *)msg;
  1820. enum ice_status aq_ret = 0;
  1821. struct ice_pf *pf = vf->pf;
  1822. struct ice_vsi *vsi;
  1823. int i;
  1824. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1825. aq_ret = ICE_ERR_PARAM;
  1826. goto error_param;
  1827. }
  1828. if (!ice_vc_isvalid_vsi_id(vf, vfl->vsi_id)) {
  1829. aq_ret = ICE_ERR_PARAM;
  1830. goto error_param;
  1831. }
  1832. if (add_v && !ice_is_vf_trusted(vf) &&
  1833. vf->num_vlan >= ICE_MAX_VLAN_PER_VF) {
  1834. dev_info(&pf->pdev->dev,
  1835. "VF is not trusted, switch the VF to trusted mode, in order to add more VLAN addresses\n");
  1836. aq_ret = ICE_ERR_PARAM;
  1837. goto error_param;
  1838. }
  1839. for (i = 0; i < vfl->num_elements; i++) {
  1840. if (vfl->vlan_id[i] > ICE_MAX_VLANID) {
  1841. aq_ret = ICE_ERR_PARAM;
  1842. dev_err(&pf->pdev->dev,
  1843. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  1844. goto error_param;
  1845. }
  1846. }
  1847. vsi = ice_find_vsi_from_id(vf->pf, vfl->vsi_id);
  1848. if (!vsi) {
  1849. aq_ret = ICE_ERR_PARAM;
  1850. goto error_param;
  1851. }
  1852. if (vsi->info.pvid) {
  1853. aq_ret = ICE_ERR_PARAM;
  1854. goto error_param;
  1855. }
  1856. if (ice_vsi_manage_vlan_stripping(vsi, add_v)) {
  1857. dev_err(&pf->pdev->dev,
  1858. "%sable VLAN stripping failed for VSI %i\n",
  1859. add_v ? "en" : "dis", vsi->vsi_num);
  1860. aq_ret = ICE_ERR_PARAM;
  1861. goto error_param;
  1862. }
  1863. if (add_v) {
  1864. for (i = 0; i < vfl->num_elements; i++) {
  1865. u16 vid = vfl->vlan_id[i];
  1866. if (!ice_vsi_add_vlan(vsi, vid)) {
  1867. vf->num_vlan++;
  1868. set_bit(vid, vsi->active_vlans);
  1869. /* Enable VLAN pruning when VLAN 0 is added */
  1870. if (unlikely(!vid))
  1871. if (ice_cfg_vlan_pruning(vsi, true))
  1872. aq_ret = ICE_ERR_PARAM;
  1873. } else {
  1874. aq_ret = ICE_ERR_PARAM;
  1875. }
  1876. }
  1877. } else {
  1878. for (i = 0; i < vfl->num_elements; i++) {
  1879. u16 vid = vfl->vlan_id[i];
  1880. /* Make sure ice_vsi_kill_vlan is successful before
  1881. * updating VLAN information
  1882. */
  1883. if (!ice_vsi_kill_vlan(vsi, vid)) {
  1884. vf->num_vlan--;
  1885. clear_bit(vid, vsi->active_vlans);
  1886. /* Disable VLAN pruning when removing VLAN 0 */
  1887. if (unlikely(!vid))
  1888. ice_cfg_vlan_pruning(vsi, false);
  1889. }
  1890. }
  1891. }
  1892. error_param:
  1893. /* send the response to the VF */
  1894. if (add_v)
  1895. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ADD_VLAN, aq_ret,
  1896. NULL, 0);
  1897. else
  1898. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DEL_VLAN, aq_ret,
  1899. NULL, 0);
  1900. }
  1901. /**
  1902. * ice_vc_add_vlan_msg
  1903. * @vf: pointer to the VF info
  1904. * @msg: pointer to the msg buffer
  1905. *
  1906. * Add and program guest VLAN id
  1907. */
  1908. static int ice_vc_add_vlan_msg(struct ice_vf *vf, u8 *msg)
  1909. {
  1910. return ice_vc_process_vlan_msg(vf, msg, true);
  1911. }
  1912. /**
  1913. * ice_vc_remove_vlan_msg
  1914. * @vf: pointer to the VF info
  1915. * @msg: pointer to the msg buffer
  1916. *
  1917. * remove programmed guest VLAN id
  1918. */
  1919. static int ice_vc_remove_vlan_msg(struct ice_vf *vf, u8 *msg)
  1920. {
  1921. return ice_vc_process_vlan_msg(vf, msg, false);
  1922. }
  1923. /**
  1924. * ice_vc_ena_vlan_stripping
  1925. * @vf: pointer to the VF info
  1926. *
  1927. * Enable VLAN header stripping for a given VF
  1928. */
  1929. static int ice_vc_ena_vlan_stripping(struct ice_vf *vf)
  1930. {
  1931. enum ice_status aq_ret = 0;
  1932. struct ice_pf *pf = vf->pf;
  1933. struct ice_vsi *vsi;
  1934. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1935. aq_ret = ICE_ERR_PARAM;
  1936. goto error_param;
  1937. }
  1938. vsi = pf->vsi[vf->lan_vsi_idx];
  1939. if (ice_vsi_manage_vlan_stripping(vsi, true))
  1940. aq_ret = ICE_ERR_AQ_ERROR;
  1941. error_param:
  1942. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_ENABLE_VLAN_STRIPPING,
  1943. aq_ret, NULL, 0);
  1944. }
  1945. /**
  1946. * ice_vc_dis_vlan_stripping
  1947. * @vf: pointer to the VF info
  1948. *
  1949. * Disable VLAN header stripping for a given VF
  1950. */
  1951. static int ice_vc_dis_vlan_stripping(struct ice_vf *vf)
  1952. {
  1953. enum ice_status aq_ret = 0;
  1954. struct ice_pf *pf = vf->pf;
  1955. struct ice_vsi *vsi;
  1956. if (!test_bit(ICE_VF_STATE_ACTIVE, vf->vf_states)) {
  1957. aq_ret = ICE_ERR_PARAM;
  1958. goto error_param;
  1959. }
  1960. vsi = pf->vsi[vf->lan_vsi_idx];
  1961. if (ice_vsi_manage_vlan_stripping(vsi, false))
  1962. aq_ret = ICE_ERR_AQ_ERROR;
  1963. error_param:
  1964. return ice_vc_send_msg_to_vf(vf, VIRTCHNL_OP_DISABLE_VLAN_STRIPPING,
  1965. aq_ret, NULL, 0);
  1966. }
  1967. /**
  1968. * ice_vc_process_vf_msg - Process request from VF
  1969. * @pf: pointer to the PF structure
  1970. * @event: pointer to the AQ event
  1971. *
  1972. * called from the common asq/arq handler to
  1973. * process request from VF
  1974. */
  1975. void ice_vc_process_vf_msg(struct ice_pf *pf, struct ice_rq_event_info *event)
  1976. {
  1977. u32 v_opcode = le32_to_cpu(event->desc.cookie_high);
  1978. s16 vf_id = le16_to_cpu(event->desc.retval);
  1979. u16 msglen = event->msg_len;
  1980. u8 *msg = event->msg_buf;
  1981. struct ice_vf *vf = NULL;
  1982. int err = 0;
  1983. if (vf_id >= pf->num_alloc_vfs) {
  1984. err = -EINVAL;
  1985. goto error_handler;
  1986. }
  1987. vf = &pf->vf[vf_id];
  1988. /* Check if VF is disabled. */
  1989. if (test_bit(ICE_VF_STATE_DIS, vf->vf_states)) {
  1990. err = -EPERM;
  1991. goto error_handler;
  1992. }
  1993. /* Perform basic checks on the msg */
  1994. err = virtchnl_vc_validate_vf_msg(&vf->vf_ver, v_opcode, msg, msglen);
  1995. if (err) {
  1996. if (err == VIRTCHNL_ERR_PARAM)
  1997. err = -EPERM;
  1998. else
  1999. err = -EINVAL;
  2000. goto error_handler;
  2001. }
  2002. /* Perform additional checks specific to RSS and Virtchnl */
  2003. if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_KEY) {
  2004. struct virtchnl_rss_key *vrk = (struct virtchnl_rss_key *)msg;
  2005. if (vrk->key_len != ICE_VSIQF_HKEY_ARRAY_SIZE)
  2006. err = -EINVAL;
  2007. } else if (v_opcode == VIRTCHNL_OP_CONFIG_RSS_LUT) {
  2008. struct virtchnl_rss_lut *vrl = (struct virtchnl_rss_lut *)msg;
  2009. if (vrl->lut_entries != ICE_VSIQF_HLUT_ARRAY_SIZE)
  2010. err = -EINVAL;
  2011. }
  2012. error_handler:
  2013. if (err) {
  2014. ice_vc_send_msg_to_vf(vf, v_opcode, ICE_ERR_PARAM, NULL, 0);
  2015. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d, error %d\n",
  2016. vf_id, v_opcode, msglen, err);
  2017. return;
  2018. }
  2019. switch (v_opcode) {
  2020. case VIRTCHNL_OP_VERSION:
  2021. err = ice_vc_get_ver_msg(vf, msg);
  2022. break;
  2023. case VIRTCHNL_OP_GET_VF_RESOURCES:
  2024. err = ice_vc_get_vf_res_msg(vf, msg);
  2025. break;
  2026. case VIRTCHNL_OP_RESET_VF:
  2027. ice_vc_reset_vf_msg(vf);
  2028. break;
  2029. case VIRTCHNL_OP_ADD_ETH_ADDR:
  2030. err = ice_vc_add_mac_addr_msg(vf, msg);
  2031. break;
  2032. case VIRTCHNL_OP_DEL_ETH_ADDR:
  2033. err = ice_vc_del_mac_addr_msg(vf, msg);
  2034. break;
  2035. case VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  2036. err = ice_vc_cfg_qs_msg(vf, msg);
  2037. break;
  2038. case VIRTCHNL_OP_ENABLE_QUEUES:
  2039. err = ice_vc_ena_qs_msg(vf, msg);
  2040. ice_vc_notify_vf_link_state(vf);
  2041. break;
  2042. case VIRTCHNL_OP_DISABLE_QUEUES:
  2043. err = ice_vc_dis_qs_msg(vf, msg);
  2044. break;
  2045. case VIRTCHNL_OP_REQUEST_QUEUES:
  2046. err = ice_vc_request_qs_msg(vf, msg);
  2047. break;
  2048. case VIRTCHNL_OP_CONFIG_IRQ_MAP:
  2049. err = ice_vc_cfg_irq_map_msg(vf, msg);
  2050. break;
  2051. case VIRTCHNL_OP_CONFIG_RSS_KEY:
  2052. err = ice_vc_config_rss_key(vf, msg);
  2053. break;
  2054. case VIRTCHNL_OP_CONFIG_RSS_LUT:
  2055. err = ice_vc_config_rss_lut(vf, msg);
  2056. break;
  2057. case VIRTCHNL_OP_GET_STATS:
  2058. err = ice_vc_get_stats_msg(vf, msg);
  2059. break;
  2060. case VIRTCHNL_OP_ADD_VLAN:
  2061. err = ice_vc_add_vlan_msg(vf, msg);
  2062. break;
  2063. case VIRTCHNL_OP_DEL_VLAN:
  2064. err = ice_vc_remove_vlan_msg(vf, msg);
  2065. break;
  2066. case VIRTCHNL_OP_ENABLE_VLAN_STRIPPING:
  2067. err = ice_vc_ena_vlan_stripping(vf);
  2068. break;
  2069. case VIRTCHNL_OP_DISABLE_VLAN_STRIPPING:
  2070. err = ice_vc_dis_vlan_stripping(vf);
  2071. break;
  2072. case VIRTCHNL_OP_UNKNOWN:
  2073. default:
  2074. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  2075. v_opcode, vf_id);
  2076. err = ice_vc_send_msg_to_vf(vf, v_opcode, ICE_ERR_NOT_IMPL,
  2077. NULL, 0);
  2078. break;
  2079. }
  2080. if (err) {
  2081. /* Helper function cares less about error return values here
  2082. * as it is busy with pending work.
  2083. */
  2084. dev_info(&pf->pdev->dev,
  2085. "PF failed to honor VF %d, opcode %d\n, error %d\n",
  2086. vf_id, v_opcode, err);
  2087. }
  2088. }
  2089. /**
  2090. * ice_get_vf_cfg
  2091. * @netdev: network interface device structure
  2092. * @vf_id: VF identifier
  2093. * @ivi: VF configuration structure
  2094. *
  2095. * return VF configuration
  2096. */
  2097. int ice_get_vf_cfg(struct net_device *netdev, int vf_id,
  2098. struct ifla_vf_info *ivi)
  2099. {
  2100. struct ice_netdev_priv *np = netdev_priv(netdev);
  2101. struct ice_vsi *vsi = np->vsi;
  2102. struct ice_pf *pf = vsi->back;
  2103. struct ice_vf *vf;
  2104. /* validate the request */
  2105. if (vf_id >= pf->num_alloc_vfs) {
  2106. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2107. return -EINVAL;
  2108. }
  2109. vf = &pf->vf[vf_id];
  2110. vsi = pf->vsi[vf->lan_vsi_idx];
  2111. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2112. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2113. return -EBUSY;
  2114. }
  2115. ivi->vf = vf_id;
  2116. ether_addr_copy(ivi->mac, vf->dflt_lan_addr.addr);
  2117. /* VF configuration for VLAN and applicable QoS */
  2118. ivi->vlan = le16_to_cpu(vsi->info.pvid) & ICE_VLAN_M;
  2119. ivi->qos = (le16_to_cpu(vsi->info.pvid) & ICE_PRIORITY_M) >>
  2120. ICE_VLAN_PRIORITY_S;
  2121. ivi->trusted = vf->trusted;
  2122. ivi->spoofchk = vf->spoofchk;
  2123. if (!vf->link_forced)
  2124. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2125. else if (vf->link_up)
  2126. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2127. else
  2128. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2129. ivi->max_tx_rate = vf->tx_rate;
  2130. ivi->min_tx_rate = 0;
  2131. return 0;
  2132. }
  2133. /**
  2134. * ice_set_vf_spoofchk
  2135. * @netdev: network interface device structure
  2136. * @vf_id: VF identifier
  2137. * @ena: flag to enable or disable feature
  2138. *
  2139. * Enable or disable VF spoof checking
  2140. */
  2141. int ice_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool ena)
  2142. {
  2143. struct ice_netdev_priv *np = netdev_priv(netdev);
  2144. struct ice_vsi_ctx ctx = { 0 };
  2145. struct ice_vsi *vsi = np->vsi;
  2146. struct ice_pf *pf = vsi->back;
  2147. struct ice_vf *vf;
  2148. int status;
  2149. /* validate the request */
  2150. if (vf_id >= pf->num_alloc_vfs) {
  2151. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2152. return -EINVAL;
  2153. }
  2154. vf = &pf->vf[vf_id];
  2155. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2156. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2157. return -EBUSY;
  2158. }
  2159. if (ena == vf->spoofchk) {
  2160. dev_dbg(&pf->pdev->dev, "VF spoofchk already %s\n",
  2161. ena ? "ON" : "OFF");
  2162. return 0;
  2163. }
  2164. ctx.info.valid_sections = cpu_to_le16(ICE_AQ_VSI_PROP_SECURITY_VALID);
  2165. if (ena) {
  2166. ctx.info.sec_flags |= ICE_AQ_VSI_SEC_FLAG_ENA_MAC_ANTI_SPOOF;
  2167. ctx.info.sw_flags2 |= ICE_AQ_VSI_SW_FLAG_RX_PRUNE_EN_M;
  2168. }
  2169. status = ice_update_vsi(&pf->hw, vsi->idx, &ctx, NULL);
  2170. if (status) {
  2171. dev_dbg(&pf->pdev->dev,
  2172. "Error %d, failed to update VSI* parameters\n", status);
  2173. return -EIO;
  2174. }
  2175. vf->spoofchk = ena;
  2176. vsi->info.sec_flags = ctx.info.sec_flags;
  2177. vsi->info.sw_flags2 = ctx.info.sw_flags2;
  2178. return status;
  2179. }
  2180. /**
  2181. * ice_set_vf_mac
  2182. * @netdev: network interface device structure
  2183. * @vf_id: VF identifier
  2184. * @mac: mac address
  2185. *
  2186. * program VF mac address
  2187. */
  2188. int ice_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  2189. {
  2190. struct ice_netdev_priv *np = netdev_priv(netdev);
  2191. struct ice_vsi *vsi = np->vsi;
  2192. struct ice_pf *pf = vsi->back;
  2193. struct ice_vf *vf;
  2194. int ret = 0;
  2195. /* validate the request */
  2196. if (vf_id >= pf->num_alloc_vfs) {
  2197. netdev_err(netdev, "invalid VF id: %d\n", vf_id);
  2198. return -EINVAL;
  2199. }
  2200. vf = &pf->vf[vf_id];
  2201. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2202. netdev_err(netdev, "VF %d in reset. Try again.\n", vf_id);
  2203. return -EBUSY;
  2204. }
  2205. if (is_zero_ether_addr(mac) || is_multicast_ether_addr(mac)) {
  2206. netdev_err(netdev, "%pM not a valid unicast address\n", mac);
  2207. return -EINVAL;
  2208. }
  2209. /* copy mac into dflt_lan_addr and trigger a VF reset. The reset
  2210. * flow will use the updated dflt_lan_addr and add a MAC filter
  2211. * using ice_add_mac. Also set pf_set_mac to indicate that the PF has
  2212. * set the MAC address for this VF.
  2213. */
  2214. ether_addr_copy(vf->dflt_lan_addr.addr, mac);
  2215. vf->pf_set_mac = true;
  2216. netdev_info(netdev,
  2217. "mac on VF %d set to %pM\n. VF driver will be reinitialized\n",
  2218. vf_id, mac);
  2219. ice_vc_dis_vf(vf);
  2220. return ret;
  2221. }
  2222. /**
  2223. * ice_set_vf_trust
  2224. * @netdev: network interface device structure
  2225. * @vf_id: VF identifier
  2226. * @trusted: Boolean value to enable/disable trusted VF
  2227. *
  2228. * Enable or disable a given VF as trusted
  2229. */
  2230. int ice_set_vf_trust(struct net_device *netdev, int vf_id, bool trusted)
  2231. {
  2232. struct ice_netdev_priv *np = netdev_priv(netdev);
  2233. struct ice_vsi *vsi = np->vsi;
  2234. struct ice_pf *pf = vsi->back;
  2235. struct ice_vf *vf;
  2236. /* validate the request */
  2237. if (vf_id >= pf->num_alloc_vfs) {
  2238. dev_err(&pf->pdev->dev, "invalid VF id: %d\n", vf_id);
  2239. return -EINVAL;
  2240. }
  2241. vf = &pf->vf[vf_id];
  2242. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2243. dev_err(&pf->pdev->dev, "VF %d in reset. Try again.\n", vf_id);
  2244. return -EBUSY;
  2245. }
  2246. /* Check if already trusted */
  2247. if (trusted == vf->trusted)
  2248. return 0;
  2249. vf->trusted = trusted;
  2250. ice_vc_dis_vf(vf);
  2251. dev_info(&pf->pdev->dev, "VF %u is now %strusted\n",
  2252. vf_id, trusted ? "" : "un");
  2253. return 0;
  2254. }
  2255. /**
  2256. * ice_set_vf_link_state
  2257. * @netdev: network interface device structure
  2258. * @vf_id: VF identifier
  2259. * @link_state: required link state
  2260. *
  2261. * Set VF's link state, irrespective of physical link state status
  2262. */
  2263. int ice_set_vf_link_state(struct net_device *netdev, int vf_id, int link_state)
  2264. {
  2265. struct ice_netdev_priv *np = netdev_priv(netdev);
  2266. struct ice_pf *pf = np->vsi->back;
  2267. struct virtchnl_pf_event pfe = { 0 };
  2268. struct ice_link_status *ls;
  2269. struct ice_vf *vf;
  2270. struct ice_hw *hw;
  2271. if (vf_id >= pf->num_alloc_vfs) {
  2272. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2273. return -EINVAL;
  2274. }
  2275. vf = &pf->vf[vf_id];
  2276. hw = &pf->hw;
  2277. ls = &pf->hw.port_info->phy.link_info;
  2278. if (!test_bit(ICE_VF_STATE_INIT, vf->vf_states)) {
  2279. dev_err(&pf->pdev->dev, "vf %d in reset. Try again.\n", vf_id);
  2280. return -EBUSY;
  2281. }
  2282. pfe.event = VIRTCHNL_EVENT_LINK_CHANGE;
  2283. pfe.severity = PF_EVENT_SEVERITY_INFO;
  2284. switch (link_state) {
  2285. case IFLA_VF_LINK_STATE_AUTO:
  2286. vf->link_forced = false;
  2287. vf->link_up = ls->link_info & ICE_AQ_LINK_UP;
  2288. break;
  2289. case IFLA_VF_LINK_STATE_ENABLE:
  2290. vf->link_forced = true;
  2291. vf->link_up = true;
  2292. break;
  2293. case IFLA_VF_LINK_STATE_DISABLE:
  2294. vf->link_forced = true;
  2295. vf->link_up = false;
  2296. break;
  2297. default:
  2298. return -EINVAL;
  2299. }
  2300. if (vf->link_forced)
  2301. ice_set_pfe_link_forced(vf, &pfe, vf->link_up);
  2302. else
  2303. ice_set_pfe_link(vf, &pfe, ls->link_speed, vf->link_up);
  2304. /* Notify the VF of its new link state */
  2305. ice_aq_send_msg_to_vf(hw, vf->vf_id, VIRTCHNL_OP_EVENT, 0, (u8 *)&pfe,
  2306. sizeof(pfe), NULL);
  2307. return 0;
  2308. }