i40e_virtchnl_pf.c 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Driver
  4. * Copyright(c) 2013 - 2015 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include "i40e.h"
  27. /*********************notification routines***********************/
  28. /**
  29. * i40e_vc_vf_broadcast
  30. * @pf: pointer to the PF structure
  31. * @opcode: operation code
  32. * @retval: return value
  33. * @msg: pointer to the msg buffer
  34. * @msglen: msg length
  35. *
  36. * send a message to all VFs on a given PF
  37. **/
  38. static void i40e_vc_vf_broadcast(struct i40e_pf *pf,
  39. enum i40e_virtchnl_ops v_opcode,
  40. i40e_status v_retval, u8 *msg,
  41. u16 msglen)
  42. {
  43. struct i40e_hw *hw = &pf->hw;
  44. struct i40e_vf *vf = pf->vf;
  45. int i;
  46. for (i = 0; i < pf->num_alloc_vfs; i++, vf++) {
  47. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  48. /* Not all vfs are enabled so skip the ones that are not */
  49. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
  50. !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
  51. continue;
  52. /* Ignore return value on purpose - a given VF may fail, but
  53. * we need to keep going and send to all of them
  54. */
  55. i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  56. msg, msglen, NULL);
  57. }
  58. }
  59. /**
  60. * i40e_vc_notify_link_state
  61. * @vf: pointer to the VF structure
  62. *
  63. * send a link status message to a single VF
  64. **/
  65. static void i40e_vc_notify_vf_link_state(struct i40e_vf *vf)
  66. {
  67. struct i40e_virtchnl_pf_event pfe;
  68. struct i40e_pf *pf = vf->pf;
  69. struct i40e_hw *hw = &pf->hw;
  70. struct i40e_link_status *ls = &pf->hw.phy.link_info;
  71. int abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  72. pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
  73. pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
  74. if (vf->link_forced) {
  75. pfe.event_data.link_event.link_status = vf->link_up;
  76. pfe.event_data.link_event.link_speed =
  77. (vf->link_up ? I40E_LINK_SPEED_40GB : 0);
  78. } else {
  79. pfe.event_data.link_event.link_status =
  80. ls->link_info & I40E_AQ_LINK_UP;
  81. pfe.event_data.link_event.link_speed = ls->link_speed;
  82. }
  83. i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  84. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  85. }
  86. /**
  87. * i40e_vc_notify_link_state
  88. * @pf: pointer to the PF structure
  89. *
  90. * send a link status message to all VFs on a given PF
  91. **/
  92. void i40e_vc_notify_link_state(struct i40e_pf *pf)
  93. {
  94. int i;
  95. for (i = 0; i < pf->num_alloc_vfs; i++)
  96. i40e_vc_notify_vf_link_state(&pf->vf[i]);
  97. }
  98. /**
  99. * i40e_vc_notify_reset
  100. * @pf: pointer to the PF structure
  101. *
  102. * indicate a pending reset to all VFs on a given PF
  103. **/
  104. void i40e_vc_notify_reset(struct i40e_pf *pf)
  105. {
  106. struct i40e_virtchnl_pf_event pfe;
  107. pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
  108. pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
  109. i40e_vc_vf_broadcast(pf, I40E_VIRTCHNL_OP_EVENT, 0,
  110. (u8 *)&pfe, sizeof(struct i40e_virtchnl_pf_event));
  111. }
  112. /**
  113. * i40e_vc_notify_vf_reset
  114. * @vf: pointer to the VF structure
  115. *
  116. * indicate a pending reset to the given VF
  117. **/
  118. void i40e_vc_notify_vf_reset(struct i40e_vf *vf)
  119. {
  120. struct i40e_virtchnl_pf_event pfe;
  121. int abs_vf_id;
  122. /* validate the request */
  123. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  124. return;
  125. /* verify if the VF is in either init or active before proceeding */
  126. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states) &&
  127. !test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
  128. return;
  129. abs_vf_id = vf->vf_id + vf->pf->hw.func_caps.vf_base_id;
  130. pfe.event = I40E_VIRTCHNL_EVENT_RESET_IMPENDING;
  131. pfe.severity = I40E_PF_EVENT_SEVERITY_CERTAIN_DOOM;
  132. i40e_aq_send_msg_to_vf(&vf->pf->hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  133. 0, (u8 *)&pfe,
  134. sizeof(struct i40e_virtchnl_pf_event), NULL);
  135. }
  136. /***********************misc routines*****************************/
  137. /**
  138. * i40e_vc_disable_vf
  139. * @pf: pointer to the PF info
  140. * @vf: pointer to the VF info
  141. *
  142. * Disable the VF through a SW reset
  143. **/
  144. static inline void i40e_vc_disable_vf(struct i40e_pf *pf, struct i40e_vf *vf)
  145. {
  146. i40e_vc_notify_vf_reset(vf);
  147. i40e_reset_vf(vf, false);
  148. }
  149. /**
  150. * i40e_vc_isvalid_vsi_id
  151. * @vf: pointer to the VF info
  152. * @vsi_id: VF relative VSI id
  153. *
  154. * check for the valid VSI id
  155. **/
  156. static inline bool i40e_vc_isvalid_vsi_id(struct i40e_vf *vf, u16 vsi_id)
  157. {
  158. struct i40e_pf *pf = vf->pf;
  159. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  160. return (vsi && (vsi->vf_id == vf->vf_id));
  161. }
  162. /**
  163. * i40e_vc_isvalid_queue_id
  164. * @vf: pointer to the VF info
  165. * @vsi_id: vsi id
  166. * @qid: vsi relative queue id
  167. *
  168. * check for the valid queue id
  169. **/
  170. static inline bool i40e_vc_isvalid_queue_id(struct i40e_vf *vf, u16 vsi_id,
  171. u8 qid)
  172. {
  173. struct i40e_pf *pf = vf->pf;
  174. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  175. return (vsi && (qid < vsi->alloc_queue_pairs));
  176. }
  177. /**
  178. * i40e_vc_isvalid_vector_id
  179. * @vf: pointer to the VF info
  180. * @vector_id: VF relative vector id
  181. *
  182. * check for the valid vector id
  183. **/
  184. static inline bool i40e_vc_isvalid_vector_id(struct i40e_vf *vf, u8 vector_id)
  185. {
  186. struct i40e_pf *pf = vf->pf;
  187. return vector_id < pf->hw.func_caps.num_msix_vectors_vf;
  188. }
  189. /***********************vf resource mgmt routines*****************/
  190. /**
  191. * i40e_vc_get_pf_queue_id
  192. * @vf: pointer to the VF info
  193. * @vsi_id: id of VSI as provided by the FW
  194. * @vsi_queue_id: vsi relative queue id
  195. *
  196. * return PF relative queue id
  197. **/
  198. static u16 i40e_vc_get_pf_queue_id(struct i40e_vf *vf, u16 vsi_id,
  199. u8 vsi_queue_id)
  200. {
  201. struct i40e_pf *pf = vf->pf;
  202. struct i40e_vsi *vsi = i40e_find_vsi_from_id(pf, vsi_id);
  203. u16 pf_queue_id = I40E_QUEUE_END_OF_LIST;
  204. if (!vsi)
  205. return pf_queue_id;
  206. if (le16_to_cpu(vsi->info.mapping_flags) &
  207. I40E_AQ_VSI_QUE_MAP_NONCONTIG)
  208. pf_queue_id =
  209. le16_to_cpu(vsi->info.queue_mapping[vsi_queue_id]);
  210. else
  211. pf_queue_id = le16_to_cpu(vsi->info.queue_mapping[0]) +
  212. vsi_queue_id;
  213. return pf_queue_id;
  214. }
  215. /**
  216. * i40e_config_irq_link_list
  217. * @vf: pointer to the VF info
  218. * @vsi_id: id of VSI as given by the FW
  219. * @vecmap: irq map info
  220. *
  221. * configure irq link list from the map
  222. **/
  223. static void i40e_config_irq_link_list(struct i40e_vf *vf, u16 vsi_id,
  224. struct i40e_virtchnl_vector_map *vecmap)
  225. {
  226. unsigned long linklistmap = 0, tempmap;
  227. struct i40e_pf *pf = vf->pf;
  228. struct i40e_hw *hw = &pf->hw;
  229. u16 vsi_queue_id, pf_queue_id;
  230. enum i40e_queue_type qtype;
  231. u16 next_q, vector_id;
  232. u32 reg, reg_idx;
  233. u16 itr_idx = 0;
  234. vector_id = vecmap->vector_id;
  235. /* setup the head */
  236. if (0 == vector_id)
  237. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  238. else
  239. reg_idx = I40E_VPINT_LNKLSTN(
  240. ((pf->hw.func_caps.num_msix_vectors_vf - 1) * vf->vf_id) +
  241. (vector_id - 1));
  242. if (vecmap->rxq_map == 0 && vecmap->txq_map == 0) {
  243. /* Special case - No queues mapped on this vector */
  244. wr32(hw, reg_idx, I40E_VPINT_LNKLST0_FIRSTQ_INDX_MASK);
  245. goto irq_list_done;
  246. }
  247. tempmap = vecmap->rxq_map;
  248. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  249. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  250. vsi_queue_id));
  251. }
  252. tempmap = vecmap->txq_map;
  253. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  254. linklistmap |= (BIT(I40E_VIRTCHNL_SUPPORTED_QTYPES *
  255. vsi_queue_id + 1));
  256. }
  257. next_q = find_first_bit(&linklistmap,
  258. (I40E_MAX_VSI_QP *
  259. I40E_VIRTCHNL_SUPPORTED_QTYPES));
  260. vsi_queue_id = next_q/I40E_VIRTCHNL_SUPPORTED_QTYPES;
  261. qtype = next_q%I40E_VIRTCHNL_SUPPORTED_QTYPES;
  262. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  263. reg = ((qtype << I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_SHIFT) | pf_queue_id);
  264. wr32(hw, reg_idx, reg);
  265. while (next_q < (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
  266. switch (qtype) {
  267. case I40E_QUEUE_TYPE_RX:
  268. reg_idx = I40E_QINT_RQCTL(pf_queue_id);
  269. itr_idx = vecmap->rxitr_idx;
  270. break;
  271. case I40E_QUEUE_TYPE_TX:
  272. reg_idx = I40E_QINT_TQCTL(pf_queue_id);
  273. itr_idx = vecmap->txitr_idx;
  274. break;
  275. default:
  276. break;
  277. }
  278. next_q = find_next_bit(&linklistmap,
  279. (I40E_MAX_VSI_QP *
  280. I40E_VIRTCHNL_SUPPORTED_QTYPES),
  281. next_q + 1);
  282. if (next_q <
  283. (I40E_MAX_VSI_QP * I40E_VIRTCHNL_SUPPORTED_QTYPES)) {
  284. vsi_queue_id = next_q / I40E_VIRTCHNL_SUPPORTED_QTYPES;
  285. qtype = next_q % I40E_VIRTCHNL_SUPPORTED_QTYPES;
  286. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id,
  287. vsi_queue_id);
  288. } else {
  289. pf_queue_id = I40E_QUEUE_END_OF_LIST;
  290. qtype = 0;
  291. }
  292. /* format for the RQCTL & TQCTL regs is same */
  293. reg = (vector_id) |
  294. (qtype << I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT) |
  295. (pf_queue_id << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
  296. BIT(I40E_QINT_RQCTL_CAUSE_ENA_SHIFT) |
  297. (itr_idx << I40E_QINT_RQCTL_ITR_INDX_SHIFT);
  298. wr32(hw, reg_idx, reg);
  299. }
  300. /* if the vf is running in polling mode and using interrupt zero,
  301. * need to disable auto-mask on enabling zero interrupt for VFs.
  302. */
  303. if ((vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING) &&
  304. (vector_id == 0)) {
  305. reg = rd32(hw, I40E_GLINT_CTL);
  306. if (!(reg & I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK)) {
  307. reg |= I40E_GLINT_CTL_DIS_AUTOMASK_VF0_MASK;
  308. wr32(hw, I40E_GLINT_CTL, reg);
  309. }
  310. }
  311. irq_list_done:
  312. i40e_flush(hw);
  313. }
  314. /**
  315. * i40e_config_vsi_tx_queue
  316. * @vf: pointer to the VF info
  317. * @vsi_id: id of VSI as provided by the FW
  318. * @vsi_queue_id: vsi relative queue index
  319. * @info: config. info
  320. *
  321. * configure tx queue
  322. **/
  323. static int i40e_config_vsi_tx_queue(struct i40e_vf *vf, u16 vsi_id,
  324. u16 vsi_queue_id,
  325. struct i40e_virtchnl_txq_info *info)
  326. {
  327. struct i40e_pf *pf = vf->pf;
  328. struct i40e_hw *hw = &pf->hw;
  329. struct i40e_hmc_obj_txq tx_ctx;
  330. struct i40e_vsi *vsi;
  331. u16 pf_queue_id;
  332. u32 qtx_ctl;
  333. int ret = 0;
  334. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  335. vsi = i40e_find_vsi_from_id(pf, vsi_id);
  336. /* clear the context structure first */
  337. memset(&tx_ctx, 0, sizeof(struct i40e_hmc_obj_txq));
  338. /* only set the required fields */
  339. tx_ctx.base = info->dma_ring_addr / 128;
  340. tx_ctx.qlen = info->ring_len;
  341. tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[0]);
  342. tx_ctx.rdylist_act = 0;
  343. tx_ctx.head_wb_ena = info->headwb_enabled;
  344. tx_ctx.head_wb_addr = info->dma_headwb_addr;
  345. /* clear the context in the HMC */
  346. ret = i40e_clear_lan_tx_queue_context(hw, pf_queue_id);
  347. if (ret) {
  348. dev_err(&pf->pdev->dev,
  349. "Failed to clear VF LAN Tx queue context %d, error: %d\n",
  350. pf_queue_id, ret);
  351. ret = -ENOENT;
  352. goto error_context;
  353. }
  354. /* set the context in the HMC */
  355. ret = i40e_set_lan_tx_queue_context(hw, pf_queue_id, &tx_ctx);
  356. if (ret) {
  357. dev_err(&pf->pdev->dev,
  358. "Failed to set VF LAN Tx queue context %d error: %d\n",
  359. pf_queue_id, ret);
  360. ret = -ENOENT;
  361. goto error_context;
  362. }
  363. /* associate this queue with the PCI VF function */
  364. qtx_ctl = I40E_QTX_CTL_VF_QUEUE;
  365. qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT)
  366. & I40E_QTX_CTL_PF_INDX_MASK);
  367. qtx_ctl |= (((vf->vf_id + hw->func_caps.vf_base_id)
  368. << I40E_QTX_CTL_VFVM_INDX_SHIFT)
  369. & I40E_QTX_CTL_VFVM_INDX_MASK);
  370. wr32(hw, I40E_QTX_CTL(pf_queue_id), qtx_ctl);
  371. i40e_flush(hw);
  372. error_context:
  373. return ret;
  374. }
  375. /**
  376. * i40e_config_vsi_rx_queue
  377. * @vf: pointer to the VF info
  378. * @vsi_id: id of VSI as provided by the FW
  379. * @vsi_queue_id: vsi relative queue index
  380. * @info: config. info
  381. *
  382. * configure rx queue
  383. **/
  384. static int i40e_config_vsi_rx_queue(struct i40e_vf *vf, u16 vsi_id,
  385. u16 vsi_queue_id,
  386. struct i40e_virtchnl_rxq_info *info)
  387. {
  388. struct i40e_pf *pf = vf->pf;
  389. struct i40e_hw *hw = &pf->hw;
  390. struct i40e_hmc_obj_rxq rx_ctx;
  391. u16 pf_queue_id;
  392. int ret = 0;
  393. pf_queue_id = i40e_vc_get_pf_queue_id(vf, vsi_id, vsi_queue_id);
  394. /* clear the context structure first */
  395. memset(&rx_ctx, 0, sizeof(struct i40e_hmc_obj_rxq));
  396. /* only set the required fields */
  397. rx_ctx.base = info->dma_ring_addr / 128;
  398. rx_ctx.qlen = info->ring_len;
  399. if (info->splithdr_enabled) {
  400. rx_ctx.hsplit_0 = I40E_RX_SPLIT_L2 |
  401. I40E_RX_SPLIT_IP |
  402. I40E_RX_SPLIT_TCP_UDP |
  403. I40E_RX_SPLIT_SCTP;
  404. /* header length validation */
  405. if (info->hdr_size > ((2 * 1024) - 64)) {
  406. ret = -EINVAL;
  407. goto error_param;
  408. }
  409. rx_ctx.hbuff = info->hdr_size >> I40E_RXQ_CTX_HBUFF_SHIFT;
  410. /* set splitalways mode 10b */
  411. rx_ctx.dtype = 0x2;
  412. }
  413. /* databuffer length validation */
  414. if (info->databuffer_size > ((16 * 1024) - 128)) {
  415. ret = -EINVAL;
  416. goto error_param;
  417. }
  418. rx_ctx.dbuff = info->databuffer_size >> I40E_RXQ_CTX_DBUFF_SHIFT;
  419. /* max pkt. length validation */
  420. if (info->max_pkt_size >= (16 * 1024) || info->max_pkt_size < 64) {
  421. ret = -EINVAL;
  422. goto error_param;
  423. }
  424. rx_ctx.rxmax = info->max_pkt_size;
  425. /* enable 32bytes desc always */
  426. rx_ctx.dsize = 1;
  427. /* default values */
  428. rx_ctx.lrxqthresh = 2;
  429. rx_ctx.crcstrip = 1;
  430. rx_ctx.prefena = 1;
  431. rx_ctx.l2tsel = 1;
  432. /* clear the context in the HMC */
  433. ret = i40e_clear_lan_rx_queue_context(hw, pf_queue_id);
  434. if (ret) {
  435. dev_err(&pf->pdev->dev,
  436. "Failed to clear VF LAN Rx queue context %d, error: %d\n",
  437. pf_queue_id, ret);
  438. ret = -ENOENT;
  439. goto error_param;
  440. }
  441. /* set the context in the HMC */
  442. ret = i40e_set_lan_rx_queue_context(hw, pf_queue_id, &rx_ctx);
  443. if (ret) {
  444. dev_err(&pf->pdev->dev,
  445. "Failed to set VF LAN Rx queue context %d error: %d\n",
  446. pf_queue_id, ret);
  447. ret = -ENOENT;
  448. goto error_param;
  449. }
  450. error_param:
  451. return ret;
  452. }
  453. /**
  454. * i40e_alloc_vsi_res
  455. * @vf: pointer to the VF info
  456. * @type: type of VSI to allocate
  457. *
  458. * alloc VF vsi context & resources
  459. **/
  460. static int i40e_alloc_vsi_res(struct i40e_vf *vf, enum i40e_vsi_type type)
  461. {
  462. struct i40e_mac_filter *f = NULL;
  463. struct i40e_pf *pf = vf->pf;
  464. struct i40e_vsi *vsi;
  465. int ret = 0;
  466. vsi = i40e_vsi_setup(pf, type, pf->vsi[pf->lan_vsi]->seid, vf->vf_id);
  467. if (!vsi) {
  468. dev_err(&pf->pdev->dev,
  469. "add vsi failed for VF %d, aq_err %d\n",
  470. vf->vf_id, pf->hw.aq.asq_last_status);
  471. ret = -ENOENT;
  472. goto error_alloc_vsi_res;
  473. }
  474. if (type == I40E_VSI_SRIOV) {
  475. u8 brdcast[ETH_ALEN] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  476. vf->lan_vsi_idx = vsi->idx;
  477. vf->lan_vsi_id = vsi->id;
  478. /* If the port VLAN has been configured and then the
  479. * VF driver was removed then the VSI port VLAN
  480. * configuration was destroyed. Check if there is
  481. * a port VLAN and restore the VSI configuration if
  482. * needed.
  483. */
  484. if (vf->port_vlan_id)
  485. i40e_vsi_add_pvid(vsi, vf->port_vlan_id);
  486. spin_lock_bh(&vsi->mac_filter_list_lock);
  487. if (is_valid_ether_addr(vf->default_lan_addr.addr)) {
  488. f = i40e_add_filter(vsi, vf->default_lan_addr.addr,
  489. vf->port_vlan_id ? vf->port_vlan_id : -1,
  490. true, false);
  491. if (!f)
  492. dev_info(&pf->pdev->dev,
  493. "Could not add MAC filter %pM for VF %d\n",
  494. vf->default_lan_addr.addr, vf->vf_id);
  495. }
  496. f = i40e_add_filter(vsi, brdcast,
  497. vf->port_vlan_id ? vf->port_vlan_id : -1,
  498. true, false);
  499. if (!f)
  500. dev_info(&pf->pdev->dev,
  501. "Could not allocate VF broadcast filter\n");
  502. spin_unlock_bh(&vsi->mac_filter_list_lock);
  503. }
  504. /* program mac filter */
  505. ret = i40e_sync_vsi_filters(vsi, false);
  506. if (ret)
  507. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  508. /* Set VF bandwidth if specified */
  509. if (vf->tx_rate) {
  510. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  511. vf->tx_rate / 50, 0, NULL);
  512. if (ret)
  513. dev_err(&pf->pdev->dev, "Unable to set tx rate, VF %d, error code %d.\n",
  514. vf->vf_id, ret);
  515. }
  516. error_alloc_vsi_res:
  517. return ret;
  518. }
  519. /**
  520. * i40e_enable_vf_mappings
  521. * @vf: pointer to the VF info
  522. *
  523. * enable VF mappings
  524. **/
  525. static void i40e_enable_vf_mappings(struct i40e_vf *vf)
  526. {
  527. struct i40e_pf *pf = vf->pf;
  528. struct i40e_hw *hw = &pf->hw;
  529. u32 reg, total_queue_pairs = 0;
  530. int j;
  531. /* Tell the hardware we're using noncontiguous mapping. HW requires
  532. * that VF queues be mapped using this method, even when they are
  533. * contiguous in real life
  534. */
  535. wr32(hw, I40E_VSILAN_QBASE(vf->lan_vsi_id),
  536. I40E_VSILAN_QBASE_VSIQTABLE_ENA_MASK);
  537. /* enable VF vplan_qtable mappings */
  538. reg = I40E_VPLAN_MAPENA_TXRX_ENA_MASK;
  539. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), reg);
  540. /* map PF queues to VF queues */
  541. for (j = 0; j < pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs; j++) {
  542. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id, j);
  543. reg = (qid & I40E_VPLAN_QTABLE_QINDEX_MASK);
  544. wr32(hw, I40E_VPLAN_QTABLE(total_queue_pairs, vf->vf_id), reg);
  545. total_queue_pairs++;
  546. }
  547. /* map PF queues to VSI */
  548. for (j = 0; j < 7; j++) {
  549. if (j * 2 >= pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs) {
  550. reg = 0x07FF07FF; /* unused */
  551. } else {
  552. u16 qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
  553. j * 2);
  554. reg = qid;
  555. qid = i40e_vc_get_pf_queue_id(vf, vf->lan_vsi_id,
  556. (j * 2) + 1);
  557. reg |= qid << 16;
  558. }
  559. wr32(hw, I40E_VSILAN_QTABLE(j, vf->lan_vsi_id), reg);
  560. }
  561. i40e_flush(hw);
  562. }
  563. /**
  564. * i40e_disable_vf_mappings
  565. * @vf: pointer to the VF info
  566. *
  567. * disable VF mappings
  568. **/
  569. static void i40e_disable_vf_mappings(struct i40e_vf *vf)
  570. {
  571. struct i40e_pf *pf = vf->pf;
  572. struct i40e_hw *hw = &pf->hw;
  573. int i;
  574. /* disable qp mappings */
  575. wr32(hw, I40E_VPLAN_MAPENA(vf->vf_id), 0);
  576. for (i = 0; i < I40E_MAX_VSI_QP; i++)
  577. wr32(hw, I40E_VPLAN_QTABLE(i, vf->vf_id),
  578. I40E_QUEUE_END_OF_LIST);
  579. i40e_flush(hw);
  580. }
  581. /**
  582. * i40e_free_vf_res
  583. * @vf: pointer to the VF info
  584. *
  585. * free VF resources
  586. **/
  587. static void i40e_free_vf_res(struct i40e_vf *vf)
  588. {
  589. struct i40e_pf *pf = vf->pf;
  590. struct i40e_hw *hw = &pf->hw;
  591. u32 reg_idx, reg;
  592. int i, msix_vf;
  593. /* free vsi & disconnect it from the parent uplink */
  594. if (vf->lan_vsi_idx) {
  595. i40e_vsi_release(pf->vsi[vf->lan_vsi_idx]);
  596. vf->lan_vsi_idx = 0;
  597. vf->lan_vsi_id = 0;
  598. }
  599. msix_vf = pf->hw.func_caps.num_msix_vectors_vf;
  600. /* disable interrupts so the VF starts in a known state */
  601. for (i = 0; i < msix_vf; i++) {
  602. /* format is same for both registers */
  603. if (0 == i)
  604. reg_idx = I40E_VFINT_DYN_CTL0(vf->vf_id);
  605. else
  606. reg_idx = I40E_VFINT_DYN_CTLN(((msix_vf - 1) *
  607. (vf->vf_id))
  608. + (i - 1));
  609. wr32(hw, reg_idx, I40E_VFINT_DYN_CTLN_CLEARPBA_MASK);
  610. i40e_flush(hw);
  611. }
  612. /* clear the irq settings */
  613. for (i = 0; i < msix_vf; i++) {
  614. /* format is same for both registers */
  615. if (0 == i)
  616. reg_idx = I40E_VPINT_LNKLST0(vf->vf_id);
  617. else
  618. reg_idx = I40E_VPINT_LNKLSTN(((msix_vf - 1) *
  619. (vf->vf_id))
  620. + (i - 1));
  621. reg = (I40E_VPINT_LNKLSTN_FIRSTQ_TYPE_MASK |
  622. I40E_VPINT_LNKLSTN_FIRSTQ_INDX_MASK);
  623. wr32(hw, reg_idx, reg);
  624. i40e_flush(hw);
  625. }
  626. /* reset some of the state varibles keeping
  627. * track of the resources
  628. */
  629. vf->num_queue_pairs = 0;
  630. vf->vf_states = 0;
  631. clear_bit(I40E_VF_STAT_INIT, &vf->vf_states);
  632. }
  633. /**
  634. * i40e_alloc_vf_res
  635. * @vf: pointer to the VF info
  636. *
  637. * allocate VF resources
  638. **/
  639. static int i40e_alloc_vf_res(struct i40e_vf *vf)
  640. {
  641. struct i40e_pf *pf = vf->pf;
  642. int total_queue_pairs = 0;
  643. int ret;
  644. /* allocate hw vsi context & associated resources */
  645. ret = i40e_alloc_vsi_res(vf, I40E_VSI_SRIOV);
  646. if (ret)
  647. goto error_alloc;
  648. total_queue_pairs += pf->vsi[vf->lan_vsi_idx]->alloc_queue_pairs;
  649. set_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps);
  650. /* store the total qps number for the runtime
  651. * VF req validation
  652. */
  653. vf->num_queue_pairs = total_queue_pairs;
  654. /* VF is now completely initialized */
  655. set_bit(I40E_VF_STAT_INIT, &vf->vf_states);
  656. error_alloc:
  657. if (ret)
  658. i40e_free_vf_res(vf);
  659. return ret;
  660. }
  661. #define VF_DEVICE_STATUS 0xAA
  662. #define VF_TRANS_PENDING_MASK 0x20
  663. /**
  664. * i40e_quiesce_vf_pci
  665. * @vf: pointer to the VF structure
  666. *
  667. * Wait for VF PCI transactions to be cleared after reset. Returns -EIO
  668. * if the transactions never clear.
  669. **/
  670. static int i40e_quiesce_vf_pci(struct i40e_vf *vf)
  671. {
  672. struct i40e_pf *pf = vf->pf;
  673. struct i40e_hw *hw = &pf->hw;
  674. int vf_abs_id, i;
  675. u32 reg;
  676. vf_abs_id = vf->vf_id + hw->func_caps.vf_base_id;
  677. wr32(hw, I40E_PF_PCI_CIAA,
  678. VF_DEVICE_STATUS | (vf_abs_id << I40E_PF_PCI_CIAA_VF_NUM_SHIFT));
  679. for (i = 0; i < 100; i++) {
  680. reg = rd32(hw, I40E_PF_PCI_CIAD);
  681. if ((reg & VF_TRANS_PENDING_MASK) == 0)
  682. return 0;
  683. udelay(1);
  684. }
  685. return -EIO;
  686. }
  687. /**
  688. * i40e_reset_vf
  689. * @vf: pointer to the VF structure
  690. * @flr: VFLR was issued or not
  691. *
  692. * reset the VF
  693. **/
  694. void i40e_reset_vf(struct i40e_vf *vf, bool flr)
  695. {
  696. struct i40e_pf *pf = vf->pf;
  697. struct i40e_hw *hw = &pf->hw;
  698. bool rsd = false;
  699. int i;
  700. u32 reg;
  701. if (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
  702. return;
  703. /* warn the VF */
  704. clear_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  705. /* In the case of a VFLR, the HW has already reset the VF and we
  706. * just need to clean up, so don't hit the VFRTRIG register.
  707. */
  708. if (!flr) {
  709. /* reset VF using VPGEN_VFRTRIG reg */
  710. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  711. reg |= I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  712. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  713. i40e_flush(hw);
  714. }
  715. if (i40e_quiesce_vf_pci(vf))
  716. dev_err(&pf->pdev->dev, "VF %d PCI transactions stuck\n",
  717. vf->vf_id);
  718. /* poll VPGEN_VFRSTAT reg to make sure
  719. * that reset is complete
  720. */
  721. for (i = 0; i < 10; i++) {
  722. /* VF reset requires driver to first reset the VF and then
  723. * poll the status register to make sure that the reset
  724. * completed successfully. Due to internal HW FIFO flushes,
  725. * we must wait 10ms before the register will be valid.
  726. */
  727. usleep_range(10000, 20000);
  728. reg = rd32(hw, I40E_VPGEN_VFRSTAT(vf->vf_id));
  729. if (reg & I40E_VPGEN_VFRSTAT_VFRD_MASK) {
  730. rsd = true;
  731. break;
  732. }
  733. }
  734. if (flr)
  735. usleep_range(10000, 20000);
  736. if (!rsd)
  737. dev_err(&pf->pdev->dev, "VF reset check timeout on VF %d\n",
  738. vf->vf_id);
  739. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_COMPLETED);
  740. /* clear the reset bit in the VPGEN_VFRTRIG reg */
  741. reg = rd32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id));
  742. reg &= ~I40E_VPGEN_VFRTRIG_VFSWR_MASK;
  743. wr32(hw, I40E_VPGEN_VFRTRIG(vf->vf_id), reg);
  744. /* On initial reset, we won't have any queues */
  745. if (vf->lan_vsi_idx == 0)
  746. goto complete_reset;
  747. i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false);
  748. complete_reset:
  749. /* reallocate VF resources to reset the VSI state */
  750. i40e_free_vf_res(vf);
  751. if (!i40e_alloc_vf_res(vf)) {
  752. i40e_enable_vf_mappings(vf);
  753. set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  754. clear_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  755. }
  756. /* tell the VF the reset is done */
  757. wr32(hw, I40E_VFGEN_RSTAT1(vf->vf_id), I40E_VFR_VFACTIVE);
  758. i40e_flush(hw);
  759. clear_bit(__I40E_VF_DISABLE, &pf->state);
  760. }
  761. /**
  762. * i40e_free_vfs
  763. * @pf: pointer to the PF structure
  764. *
  765. * free VF resources
  766. **/
  767. void i40e_free_vfs(struct i40e_pf *pf)
  768. {
  769. struct i40e_hw *hw = &pf->hw;
  770. u32 reg_idx, bit_idx;
  771. int i, tmp, vf_id;
  772. if (!pf->vf)
  773. return;
  774. while (test_and_set_bit(__I40E_VF_DISABLE, &pf->state))
  775. usleep_range(1000, 2000);
  776. for (i = 0; i < pf->num_alloc_vfs; i++)
  777. if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
  778. i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
  779. false);
  780. for (i = 0; i < pf->num_alloc_vfs; i++)
  781. if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
  782. i40e_vsi_control_rings(pf->vsi[pf->vf[i].lan_vsi_idx],
  783. false);
  784. /* Disable IOV before freeing resources. This lets any VF drivers
  785. * running in the host get themselves cleaned up before we yank
  786. * the carpet out from underneath their feet.
  787. */
  788. if (!pci_vfs_assigned(pf->pdev))
  789. pci_disable_sriov(pf->pdev);
  790. else
  791. dev_warn(&pf->pdev->dev, "VFs are assigned - not disabling SR-IOV\n");
  792. msleep(20); /* let any messages in transit get finished up */
  793. /* free up VF resources */
  794. tmp = pf->num_alloc_vfs;
  795. pf->num_alloc_vfs = 0;
  796. for (i = 0; i < tmp; i++) {
  797. if (test_bit(I40E_VF_STAT_INIT, &pf->vf[i].vf_states))
  798. i40e_free_vf_res(&pf->vf[i]);
  799. /* disable qp mappings */
  800. i40e_disable_vf_mappings(&pf->vf[i]);
  801. }
  802. kfree(pf->vf);
  803. pf->vf = NULL;
  804. /* This check is for when the driver is unloaded while VFs are
  805. * assigned. Setting the number of VFs to 0 through sysfs is caught
  806. * before this function ever gets called.
  807. */
  808. if (!pci_vfs_assigned(pf->pdev)) {
  809. /* Acknowledge VFLR for all VFS. Without this, VFs will fail to
  810. * work correctly when SR-IOV gets re-enabled.
  811. */
  812. for (vf_id = 0; vf_id < tmp; vf_id++) {
  813. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  814. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  815. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  816. }
  817. }
  818. clear_bit(__I40E_VF_DISABLE, &pf->state);
  819. }
  820. #ifdef CONFIG_PCI_IOV
  821. /**
  822. * i40e_alloc_vfs
  823. * @pf: pointer to the PF structure
  824. * @num_alloc_vfs: number of VFs to allocate
  825. *
  826. * allocate VF resources
  827. **/
  828. int i40e_alloc_vfs(struct i40e_pf *pf, u16 num_alloc_vfs)
  829. {
  830. struct i40e_vf *vfs;
  831. int i, ret = 0;
  832. /* Disable interrupt 0 so we don't try to handle the VFLR. */
  833. i40e_irq_dynamic_disable_icr0(pf);
  834. /* Check to see if we're just allocating resources for extant VFs */
  835. if (pci_num_vf(pf->pdev) != num_alloc_vfs) {
  836. ret = pci_enable_sriov(pf->pdev, num_alloc_vfs);
  837. if (ret) {
  838. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  839. pf->num_alloc_vfs = 0;
  840. goto err_iov;
  841. }
  842. }
  843. /* allocate memory */
  844. vfs = kcalloc(num_alloc_vfs, sizeof(struct i40e_vf), GFP_KERNEL);
  845. if (!vfs) {
  846. ret = -ENOMEM;
  847. goto err_alloc;
  848. }
  849. pf->vf = vfs;
  850. /* apply default profile */
  851. for (i = 0; i < num_alloc_vfs; i++) {
  852. vfs[i].pf = pf;
  853. vfs[i].parent_type = I40E_SWITCH_ELEMENT_TYPE_VEB;
  854. vfs[i].vf_id = i;
  855. /* assign default capabilities */
  856. set_bit(I40E_VIRTCHNL_VF_CAP_L2, &vfs[i].vf_caps);
  857. vfs[i].spoofchk = true;
  858. /* VF resources get allocated during reset */
  859. i40e_reset_vf(&vfs[i], false);
  860. }
  861. pf->num_alloc_vfs = num_alloc_vfs;
  862. err_alloc:
  863. if (ret)
  864. i40e_free_vfs(pf);
  865. err_iov:
  866. /* Re-enable interrupt 0. */
  867. i40e_irq_dynamic_enable_icr0(pf);
  868. return ret;
  869. }
  870. #endif
  871. /**
  872. * i40e_pci_sriov_enable
  873. * @pdev: pointer to a pci_dev structure
  874. * @num_vfs: number of VFs to allocate
  875. *
  876. * Enable or change the number of VFs
  877. **/
  878. static int i40e_pci_sriov_enable(struct pci_dev *pdev, int num_vfs)
  879. {
  880. #ifdef CONFIG_PCI_IOV
  881. struct i40e_pf *pf = pci_get_drvdata(pdev);
  882. int pre_existing_vfs = pci_num_vf(pdev);
  883. int err = 0;
  884. if (test_bit(__I40E_TESTING, &pf->state)) {
  885. dev_warn(&pdev->dev,
  886. "Cannot enable SR-IOV virtual functions while the device is undergoing diagnostic testing\n");
  887. err = -EPERM;
  888. goto err_out;
  889. }
  890. if (pre_existing_vfs && pre_existing_vfs != num_vfs)
  891. i40e_free_vfs(pf);
  892. else if (pre_existing_vfs && pre_existing_vfs == num_vfs)
  893. goto out;
  894. if (num_vfs > pf->num_req_vfs) {
  895. dev_warn(&pdev->dev, "Unable to enable %d VFs. Limited to %d VFs due to device resource constraints.\n",
  896. num_vfs, pf->num_req_vfs);
  897. err = -EPERM;
  898. goto err_out;
  899. }
  900. dev_info(&pdev->dev, "Allocating %d VFs.\n", num_vfs);
  901. err = i40e_alloc_vfs(pf, num_vfs);
  902. if (err) {
  903. dev_warn(&pdev->dev, "Failed to enable SR-IOV: %d\n", err);
  904. goto err_out;
  905. }
  906. out:
  907. return num_vfs;
  908. err_out:
  909. return err;
  910. #endif
  911. return 0;
  912. }
  913. /**
  914. * i40e_pci_sriov_configure
  915. * @pdev: pointer to a pci_dev structure
  916. * @num_vfs: number of VFs to allocate
  917. *
  918. * Enable or change the number of VFs. Called when the user updates the number
  919. * of VFs in sysfs.
  920. **/
  921. int i40e_pci_sriov_configure(struct pci_dev *pdev, int num_vfs)
  922. {
  923. struct i40e_pf *pf = pci_get_drvdata(pdev);
  924. if (num_vfs) {
  925. if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
  926. pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
  927. i40e_do_reset_safe(pf,
  928. BIT_ULL(__I40E_PF_RESET_REQUESTED));
  929. }
  930. return i40e_pci_sriov_enable(pdev, num_vfs);
  931. }
  932. if (!pci_vfs_assigned(pf->pdev)) {
  933. i40e_free_vfs(pf);
  934. pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
  935. i40e_do_reset_safe(pf, BIT_ULL(__I40E_PF_RESET_REQUESTED));
  936. } else {
  937. dev_warn(&pdev->dev, "Unable to free VFs because some are assigned to VMs.\n");
  938. return -EINVAL;
  939. }
  940. return 0;
  941. }
  942. /***********************virtual channel routines******************/
  943. /**
  944. * i40e_vc_send_msg_to_vf
  945. * @vf: pointer to the VF info
  946. * @v_opcode: virtual channel opcode
  947. * @v_retval: virtual channel return value
  948. * @msg: pointer to the msg buffer
  949. * @msglen: msg length
  950. *
  951. * send msg to VF
  952. **/
  953. static int i40e_vc_send_msg_to_vf(struct i40e_vf *vf, u32 v_opcode,
  954. u32 v_retval, u8 *msg, u16 msglen)
  955. {
  956. struct i40e_pf *pf;
  957. struct i40e_hw *hw;
  958. int abs_vf_id;
  959. i40e_status aq_ret;
  960. /* validate the request */
  961. if (!vf || vf->vf_id >= vf->pf->num_alloc_vfs)
  962. return -EINVAL;
  963. pf = vf->pf;
  964. hw = &pf->hw;
  965. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  966. /* single place to detect unsuccessful return values */
  967. if (v_retval) {
  968. vf->num_invalid_msgs++;
  969. dev_err(&pf->pdev->dev, "Failed opcode %d Error: %d\n",
  970. v_opcode, v_retval);
  971. if (vf->num_invalid_msgs >
  972. I40E_DEFAULT_NUM_INVALID_MSGS_ALLOWED) {
  973. dev_err(&pf->pdev->dev,
  974. "Number of invalid messages exceeded for VF %d\n",
  975. vf->vf_id);
  976. dev_err(&pf->pdev->dev, "Use PF Control I/F to enable the VF\n");
  977. set_bit(I40E_VF_STAT_DISABLED, &vf->vf_states);
  978. }
  979. } else {
  980. vf->num_valid_msgs++;
  981. /* reset the invalid counter, if a valid message is received. */
  982. vf->num_invalid_msgs = 0;
  983. }
  984. aq_ret = i40e_aq_send_msg_to_vf(hw, abs_vf_id, v_opcode, v_retval,
  985. msg, msglen, NULL);
  986. if (aq_ret) {
  987. dev_err(&pf->pdev->dev,
  988. "Unable to send the message to VF %d aq_err %d\n",
  989. vf->vf_id, pf->hw.aq.asq_last_status);
  990. return -EIO;
  991. }
  992. return 0;
  993. }
  994. /**
  995. * i40e_vc_send_resp_to_vf
  996. * @vf: pointer to the VF info
  997. * @opcode: operation code
  998. * @retval: return value
  999. *
  1000. * send resp msg to VF
  1001. **/
  1002. static int i40e_vc_send_resp_to_vf(struct i40e_vf *vf,
  1003. enum i40e_virtchnl_ops opcode,
  1004. i40e_status retval)
  1005. {
  1006. return i40e_vc_send_msg_to_vf(vf, opcode, retval, NULL, 0);
  1007. }
  1008. /**
  1009. * i40e_vc_get_version_msg
  1010. * @vf: pointer to the VF info
  1011. *
  1012. * called from the VF to request the API version used by the PF
  1013. **/
  1014. static int i40e_vc_get_version_msg(struct i40e_vf *vf, u8 *msg)
  1015. {
  1016. struct i40e_virtchnl_version_info info = {
  1017. I40E_VIRTCHNL_VERSION_MAJOR, I40E_VIRTCHNL_VERSION_MINOR
  1018. };
  1019. vf->vf_ver = *(struct i40e_virtchnl_version_info *)msg;
  1020. /* VFs running the 1.0 API expect to get 1.0 back or they will cry. */
  1021. if (VF_IS_V10(vf))
  1022. info.minor = I40E_VIRTCHNL_VERSION_MINOR_NO_VF_CAPS;
  1023. return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_VERSION,
  1024. I40E_SUCCESS, (u8 *)&info,
  1025. sizeof(struct
  1026. i40e_virtchnl_version_info));
  1027. }
  1028. /**
  1029. * i40e_vc_get_vf_resources_msg
  1030. * @vf: pointer to the VF info
  1031. * @msg: pointer to the msg buffer
  1032. * @msglen: msg length
  1033. *
  1034. * called from the VF to request its resources
  1035. **/
  1036. static int i40e_vc_get_vf_resources_msg(struct i40e_vf *vf, u8 *msg)
  1037. {
  1038. struct i40e_virtchnl_vf_resource *vfres = NULL;
  1039. struct i40e_pf *pf = vf->pf;
  1040. i40e_status aq_ret = 0;
  1041. struct i40e_vsi *vsi;
  1042. int i = 0, len = 0;
  1043. int num_vsis = 1;
  1044. int ret;
  1045. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1046. aq_ret = I40E_ERR_PARAM;
  1047. goto err;
  1048. }
  1049. len = (sizeof(struct i40e_virtchnl_vf_resource) +
  1050. sizeof(struct i40e_virtchnl_vsi_resource) * num_vsis);
  1051. vfres = kzalloc(len, GFP_KERNEL);
  1052. if (!vfres) {
  1053. aq_ret = I40E_ERR_NO_MEMORY;
  1054. len = 0;
  1055. goto err;
  1056. }
  1057. if (VF_IS_V11(vf))
  1058. vf->driver_caps = *(u32 *)msg;
  1059. else
  1060. vf->driver_caps = I40E_VIRTCHNL_VF_OFFLOAD_L2 |
  1061. I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG |
  1062. I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
  1063. vfres->vf_offload_flags = I40E_VIRTCHNL_VF_OFFLOAD_L2;
  1064. vsi = pf->vsi[vf->lan_vsi_idx];
  1065. if (!vsi->info.pvid)
  1066. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_VLAN;
  1067. if (pf->flags & I40E_FLAG_RSS_AQ_CAPABLE) {
  1068. if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ)
  1069. vfres->vf_offload_flags |=
  1070. I40E_VIRTCHNL_VF_OFFLOAD_RSS_AQ;
  1071. } else {
  1072. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RSS_REG;
  1073. }
  1074. if (vf->driver_caps & I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING)
  1075. vfres->vf_offload_flags |= I40E_VIRTCHNL_VF_OFFLOAD_RX_POLLING;
  1076. vfres->num_vsis = num_vsis;
  1077. vfres->num_queue_pairs = vf->num_queue_pairs;
  1078. vfres->max_vectors = pf->hw.func_caps.num_msix_vectors_vf;
  1079. if (vf->lan_vsi_idx) {
  1080. vfres->vsi_res[i].vsi_id = vf->lan_vsi_id;
  1081. vfres->vsi_res[i].vsi_type = I40E_VSI_SRIOV;
  1082. vfres->vsi_res[i].num_queue_pairs = vsi->alloc_queue_pairs;
  1083. /* VFs only use TC 0 */
  1084. vfres->vsi_res[i].qset_handle
  1085. = le16_to_cpu(vsi->info.qs_handle[0]);
  1086. ether_addr_copy(vfres->vsi_res[i].default_mac_addr,
  1087. vf->default_lan_addr.addr);
  1088. i++;
  1089. }
  1090. set_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states);
  1091. err:
  1092. /* send the response back to the VF */
  1093. ret = i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_VF_RESOURCES,
  1094. aq_ret, (u8 *)vfres, len);
  1095. kfree(vfres);
  1096. return ret;
  1097. }
  1098. /**
  1099. * i40e_vc_reset_vf_msg
  1100. * @vf: pointer to the VF info
  1101. * @msg: pointer to the msg buffer
  1102. * @msglen: msg length
  1103. *
  1104. * called from the VF to reset itself,
  1105. * unlike other virtchnl messages, PF driver
  1106. * doesn't send the response back to the VF
  1107. **/
  1108. static void i40e_vc_reset_vf_msg(struct i40e_vf *vf)
  1109. {
  1110. if (test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states))
  1111. i40e_reset_vf(vf, false);
  1112. }
  1113. /**
  1114. * i40e_vc_config_promiscuous_mode_msg
  1115. * @vf: pointer to the VF info
  1116. * @msg: pointer to the msg buffer
  1117. * @msglen: msg length
  1118. *
  1119. * called from the VF to configure the promiscuous mode of
  1120. * VF vsis
  1121. **/
  1122. static int i40e_vc_config_promiscuous_mode_msg(struct i40e_vf *vf,
  1123. u8 *msg, u16 msglen)
  1124. {
  1125. struct i40e_virtchnl_promisc_info *info =
  1126. (struct i40e_virtchnl_promisc_info *)msg;
  1127. struct i40e_pf *pf = vf->pf;
  1128. struct i40e_hw *hw = &pf->hw;
  1129. struct i40e_vsi *vsi;
  1130. bool allmulti = false;
  1131. i40e_status aq_ret;
  1132. vsi = i40e_find_vsi_from_id(pf, info->vsi_id);
  1133. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1134. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1135. !i40e_vc_isvalid_vsi_id(vf, info->vsi_id) ||
  1136. (vsi->type != I40E_VSI_FCOE)) {
  1137. aq_ret = I40E_ERR_PARAM;
  1138. goto error_param;
  1139. }
  1140. if (info->flags & I40E_FLAG_VF_MULTICAST_PROMISC)
  1141. allmulti = true;
  1142. aq_ret = i40e_aq_set_vsi_multicast_promiscuous(hw, vsi->seid,
  1143. allmulti, NULL);
  1144. error_param:
  1145. /* send the response to the VF */
  1146. return i40e_vc_send_resp_to_vf(vf,
  1147. I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE,
  1148. aq_ret);
  1149. }
  1150. /**
  1151. * i40e_vc_config_queues_msg
  1152. * @vf: pointer to the VF info
  1153. * @msg: pointer to the msg buffer
  1154. * @msglen: msg length
  1155. *
  1156. * called from the VF to configure the rx/tx
  1157. * queues
  1158. **/
  1159. static int i40e_vc_config_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1160. {
  1161. struct i40e_virtchnl_vsi_queue_config_info *qci =
  1162. (struct i40e_virtchnl_vsi_queue_config_info *)msg;
  1163. struct i40e_virtchnl_queue_pair_info *qpi;
  1164. struct i40e_pf *pf = vf->pf;
  1165. u16 vsi_id, vsi_queue_id;
  1166. i40e_status aq_ret = 0;
  1167. int i;
  1168. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1169. aq_ret = I40E_ERR_PARAM;
  1170. goto error_param;
  1171. }
  1172. vsi_id = qci->vsi_id;
  1173. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1174. aq_ret = I40E_ERR_PARAM;
  1175. goto error_param;
  1176. }
  1177. for (i = 0; i < qci->num_queue_pairs; i++) {
  1178. qpi = &qci->qpair[i];
  1179. vsi_queue_id = qpi->txq.queue_id;
  1180. if ((qpi->txq.vsi_id != vsi_id) ||
  1181. (qpi->rxq.vsi_id != vsi_id) ||
  1182. (qpi->rxq.queue_id != vsi_queue_id) ||
  1183. !i40e_vc_isvalid_queue_id(vf, vsi_id, vsi_queue_id)) {
  1184. aq_ret = I40E_ERR_PARAM;
  1185. goto error_param;
  1186. }
  1187. if (i40e_config_vsi_rx_queue(vf, vsi_id, vsi_queue_id,
  1188. &qpi->rxq) ||
  1189. i40e_config_vsi_tx_queue(vf, vsi_id, vsi_queue_id,
  1190. &qpi->txq)) {
  1191. aq_ret = I40E_ERR_PARAM;
  1192. goto error_param;
  1193. }
  1194. }
  1195. /* set vsi num_queue_pairs in use to num configured by VF */
  1196. pf->vsi[vf->lan_vsi_idx]->num_queue_pairs = qci->num_queue_pairs;
  1197. error_param:
  1198. /* send the response to the VF */
  1199. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES,
  1200. aq_ret);
  1201. }
  1202. /**
  1203. * i40e_vc_config_irq_map_msg
  1204. * @vf: pointer to the VF info
  1205. * @msg: pointer to the msg buffer
  1206. * @msglen: msg length
  1207. *
  1208. * called from the VF to configure the irq to
  1209. * queue map
  1210. **/
  1211. static int i40e_vc_config_irq_map_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1212. {
  1213. struct i40e_virtchnl_irq_map_info *irqmap_info =
  1214. (struct i40e_virtchnl_irq_map_info *)msg;
  1215. struct i40e_virtchnl_vector_map *map;
  1216. u16 vsi_id, vsi_queue_id, vector_id;
  1217. i40e_status aq_ret = 0;
  1218. unsigned long tempmap;
  1219. int i;
  1220. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1221. aq_ret = I40E_ERR_PARAM;
  1222. goto error_param;
  1223. }
  1224. for (i = 0; i < irqmap_info->num_vectors; i++) {
  1225. map = &irqmap_info->vecmap[i];
  1226. vector_id = map->vector_id;
  1227. vsi_id = map->vsi_id;
  1228. /* validate msg params */
  1229. if (!i40e_vc_isvalid_vector_id(vf, vector_id) ||
  1230. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1231. aq_ret = I40E_ERR_PARAM;
  1232. goto error_param;
  1233. }
  1234. /* lookout for the invalid queue index */
  1235. tempmap = map->rxq_map;
  1236. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1237. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1238. vsi_queue_id)) {
  1239. aq_ret = I40E_ERR_PARAM;
  1240. goto error_param;
  1241. }
  1242. }
  1243. tempmap = map->txq_map;
  1244. for_each_set_bit(vsi_queue_id, &tempmap, I40E_MAX_VSI_QP) {
  1245. if (!i40e_vc_isvalid_queue_id(vf, vsi_id,
  1246. vsi_queue_id)) {
  1247. aq_ret = I40E_ERR_PARAM;
  1248. goto error_param;
  1249. }
  1250. }
  1251. i40e_config_irq_link_list(vf, vsi_id, map);
  1252. }
  1253. error_param:
  1254. /* send the response to the VF */
  1255. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP,
  1256. aq_ret);
  1257. }
  1258. /**
  1259. * i40e_vc_enable_queues_msg
  1260. * @vf: pointer to the VF info
  1261. * @msg: pointer to the msg buffer
  1262. * @msglen: msg length
  1263. *
  1264. * called from the VF to enable all or specific queue(s)
  1265. **/
  1266. static int i40e_vc_enable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1267. {
  1268. struct i40e_virtchnl_queue_select *vqs =
  1269. (struct i40e_virtchnl_queue_select *)msg;
  1270. struct i40e_pf *pf = vf->pf;
  1271. u16 vsi_id = vqs->vsi_id;
  1272. i40e_status aq_ret = 0;
  1273. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1274. aq_ret = I40E_ERR_PARAM;
  1275. goto error_param;
  1276. }
  1277. if (!i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1278. aq_ret = I40E_ERR_PARAM;
  1279. goto error_param;
  1280. }
  1281. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1282. aq_ret = I40E_ERR_PARAM;
  1283. goto error_param;
  1284. }
  1285. if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], true))
  1286. aq_ret = I40E_ERR_TIMEOUT;
  1287. error_param:
  1288. /* send the response to the VF */
  1289. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ENABLE_QUEUES,
  1290. aq_ret);
  1291. }
  1292. /**
  1293. * i40e_vc_disable_queues_msg
  1294. * @vf: pointer to the VF info
  1295. * @msg: pointer to the msg buffer
  1296. * @msglen: msg length
  1297. *
  1298. * called from the VF to disable all or specific
  1299. * queue(s)
  1300. **/
  1301. static int i40e_vc_disable_queues_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1302. {
  1303. struct i40e_virtchnl_queue_select *vqs =
  1304. (struct i40e_virtchnl_queue_select *)msg;
  1305. struct i40e_pf *pf = vf->pf;
  1306. i40e_status aq_ret = 0;
  1307. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1308. aq_ret = I40E_ERR_PARAM;
  1309. goto error_param;
  1310. }
  1311. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1312. aq_ret = I40E_ERR_PARAM;
  1313. goto error_param;
  1314. }
  1315. if ((0 == vqs->rx_queues) && (0 == vqs->tx_queues)) {
  1316. aq_ret = I40E_ERR_PARAM;
  1317. goto error_param;
  1318. }
  1319. if (i40e_vsi_control_rings(pf->vsi[vf->lan_vsi_idx], false))
  1320. aq_ret = I40E_ERR_TIMEOUT;
  1321. error_param:
  1322. /* send the response to the VF */
  1323. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DISABLE_QUEUES,
  1324. aq_ret);
  1325. }
  1326. /**
  1327. * i40e_vc_get_stats_msg
  1328. * @vf: pointer to the VF info
  1329. * @msg: pointer to the msg buffer
  1330. * @msglen: msg length
  1331. *
  1332. * called from the VF to get vsi stats
  1333. **/
  1334. static int i40e_vc_get_stats_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1335. {
  1336. struct i40e_virtchnl_queue_select *vqs =
  1337. (struct i40e_virtchnl_queue_select *)msg;
  1338. struct i40e_pf *pf = vf->pf;
  1339. struct i40e_eth_stats stats;
  1340. i40e_status aq_ret = 0;
  1341. struct i40e_vsi *vsi;
  1342. memset(&stats, 0, sizeof(struct i40e_eth_stats));
  1343. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states)) {
  1344. aq_ret = I40E_ERR_PARAM;
  1345. goto error_param;
  1346. }
  1347. if (!i40e_vc_isvalid_vsi_id(vf, vqs->vsi_id)) {
  1348. aq_ret = I40E_ERR_PARAM;
  1349. goto error_param;
  1350. }
  1351. vsi = pf->vsi[vf->lan_vsi_idx];
  1352. if (!vsi) {
  1353. aq_ret = I40E_ERR_PARAM;
  1354. goto error_param;
  1355. }
  1356. i40e_update_eth_stats(vsi);
  1357. stats = vsi->eth_stats;
  1358. error_param:
  1359. /* send the response back to the VF */
  1360. return i40e_vc_send_msg_to_vf(vf, I40E_VIRTCHNL_OP_GET_STATS, aq_ret,
  1361. (u8 *)&stats, sizeof(stats));
  1362. }
  1363. /**
  1364. * i40e_check_vf_permission
  1365. * @vf: pointer to the VF info
  1366. * @macaddr: pointer to the MAC Address being checked
  1367. *
  1368. * Check if the VF has permission to add or delete unicast MAC address
  1369. * filters and return error code -EPERM if not. Then check if the
  1370. * address filter requested is broadcast or zero and if so return
  1371. * an invalid MAC address error code.
  1372. **/
  1373. static inline int i40e_check_vf_permission(struct i40e_vf *vf, u8 *macaddr)
  1374. {
  1375. struct i40e_pf *pf = vf->pf;
  1376. int ret = 0;
  1377. if (is_broadcast_ether_addr(macaddr) ||
  1378. is_zero_ether_addr(macaddr)) {
  1379. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n", macaddr);
  1380. ret = I40E_ERR_INVALID_MAC_ADDR;
  1381. } else if (vf->pf_set_mac && !is_multicast_ether_addr(macaddr) &&
  1382. !ether_addr_equal(macaddr, vf->default_lan_addr.addr)) {
  1383. /* If the host VMM administrator has set the VF MAC address
  1384. * administratively via the ndo_set_vf_mac command then deny
  1385. * permission to the VF to add or delete unicast MAC addresses.
  1386. * The VF may request to set the MAC address filter already
  1387. * assigned to it so do not return an error in that case.
  1388. */
  1389. dev_err(&pf->pdev->dev,
  1390. "VF attempting to override administratively set MAC address\nPlease reload the VF driver to resume normal operation\n");
  1391. ret = -EPERM;
  1392. }
  1393. return ret;
  1394. }
  1395. /**
  1396. * i40e_vc_add_mac_addr_msg
  1397. * @vf: pointer to the VF info
  1398. * @msg: pointer to the msg buffer
  1399. * @msglen: msg length
  1400. *
  1401. * add guest mac address filter
  1402. **/
  1403. static int i40e_vc_add_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1404. {
  1405. struct i40e_virtchnl_ether_addr_list *al =
  1406. (struct i40e_virtchnl_ether_addr_list *)msg;
  1407. struct i40e_pf *pf = vf->pf;
  1408. struct i40e_vsi *vsi = NULL;
  1409. u16 vsi_id = al->vsi_id;
  1410. i40e_status ret = 0;
  1411. int i;
  1412. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1413. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1414. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1415. ret = I40E_ERR_PARAM;
  1416. goto error_param;
  1417. }
  1418. for (i = 0; i < al->num_elements; i++) {
  1419. ret = i40e_check_vf_permission(vf, al->list[i].addr);
  1420. if (ret)
  1421. goto error_param;
  1422. }
  1423. vsi = pf->vsi[vf->lan_vsi_idx];
  1424. /* Lock once, because all function inside for loop accesses VSI's
  1425. * MAC filter list which needs to be protected using same lock.
  1426. */
  1427. spin_lock_bh(&vsi->mac_filter_list_lock);
  1428. /* add new addresses to the list */
  1429. for (i = 0; i < al->num_elements; i++) {
  1430. struct i40e_mac_filter *f;
  1431. f = i40e_find_mac(vsi, al->list[i].addr, true, false);
  1432. if (!f) {
  1433. if (i40e_is_vsi_in_vlan(vsi))
  1434. f = i40e_put_mac_in_vlan(vsi, al->list[i].addr,
  1435. true, false);
  1436. else
  1437. f = i40e_add_filter(vsi, al->list[i].addr, -1,
  1438. true, false);
  1439. }
  1440. if (!f) {
  1441. dev_err(&pf->pdev->dev,
  1442. "Unable to add VF MAC filter\n");
  1443. ret = I40E_ERR_PARAM;
  1444. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1445. goto error_param;
  1446. }
  1447. }
  1448. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1449. /* program the updated filter list */
  1450. if (i40e_sync_vsi_filters(vsi, false))
  1451. dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
  1452. error_param:
  1453. /* send the response to the VF */
  1454. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS,
  1455. ret);
  1456. }
  1457. /**
  1458. * i40e_vc_del_mac_addr_msg
  1459. * @vf: pointer to the VF info
  1460. * @msg: pointer to the msg buffer
  1461. * @msglen: msg length
  1462. *
  1463. * remove guest mac address filter
  1464. **/
  1465. static int i40e_vc_del_mac_addr_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1466. {
  1467. struct i40e_virtchnl_ether_addr_list *al =
  1468. (struct i40e_virtchnl_ether_addr_list *)msg;
  1469. struct i40e_pf *pf = vf->pf;
  1470. struct i40e_vsi *vsi = NULL;
  1471. u16 vsi_id = al->vsi_id;
  1472. i40e_status ret = 0;
  1473. int i;
  1474. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1475. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1476. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1477. ret = I40E_ERR_PARAM;
  1478. goto error_param;
  1479. }
  1480. for (i = 0; i < al->num_elements; i++) {
  1481. if (is_broadcast_ether_addr(al->list[i].addr) ||
  1482. is_zero_ether_addr(al->list[i].addr)) {
  1483. dev_err(&pf->pdev->dev, "invalid VF MAC addr %pM\n",
  1484. al->list[i].addr);
  1485. ret = I40E_ERR_INVALID_MAC_ADDR;
  1486. goto error_param;
  1487. }
  1488. }
  1489. vsi = pf->vsi[vf->lan_vsi_idx];
  1490. spin_lock_bh(&vsi->mac_filter_list_lock);
  1491. /* delete addresses from the list */
  1492. for (i = 0; i < al->num_elements; i++)
  1493. if (i40e_del_mac_all_vlan(vsi, al->list[i].addr, true, false)) {
  1494. ret = I40E_ERR_INVALID_MAC_ADDR;
  1495. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1496. goto error_param;
  1497. }
  1498. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1499. /* program the updated filter list */
  1500. if (i40e_sync_vsi_filters(vsi, false))
  1501. dev_err(&pf->pdev->dev, "Unable to program VF MAC filters\n");
  1502. error_param:
  1503. /* send the response to the VF */
  1504. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS,
  1505. ret);
  1506. }
  1507. /**
  1508. * i40e_vc_add_vlan_msg
  1509. * @vf: pointer to the VF info
  1510. * @msg: pointer to the msg buffer
  1511. * @msglen: msg length
  1512. *
  1513. * program guest vlan id
  1514. **/
  1515. static int i40e_vc_add_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1516. {
  1517. struct i40e_virtchnl_vlan_filter_list *vfl =
  1518. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1519. struct i40e_pf *pf = vf->pf;
  1520. struct i40e_vsi *vsi = NULL;
  1521. u16 vsi_id = vfl->vsi_id;
  1522. i40e_status aq_ret = 0;
  1523. int i;
  1524. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1525. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1526. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1527. aq_ret = I40E_ERR_PARAM;
  1528. goto error_param;
  1529. }
  1530. for (i = 0; i < vfl->num_elements; i++) {
  1531. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  1532. aq_ret = I40E_ERR_PARAM;
  1533. dev_err(&pf->pdev->dev,
  1534. "invalid VF VLAN id %d\n", vfl->vlan_id[i]);
  1535. goto error_param;
  1536. }
  1537. }
  1538. vsi = pf->vsi[vf->lan_vsi_idx];
  1539. if (vsi->info.pvid) {
  1540. aq_ret = I40E_ERR_PARAM;
  1541. goto error_param;
  1542. }
  1543. i40e_vlan_stripping_enable(vsi);
  1544. for (i = 0; i < vfl->num_elements; i++) {
  1545. /* add new VLAN filter */
  1546. int ret = i40e_vsi_add_vlan(vsi, vfl->vlan_id[i]);
  1547. if (ret)
  1548. dev_err(&pf->pdev->dev,
  1549. "Unable to add VF vlan filter %d, error %d\n",
  1550. vfl->vlan_id[i], ret);
  1551. }
  1552. error_param:
  1553. /* send the response to the VF */
  1554. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_ADD_VLAN, aq_ret);
  1555. }
  1556. /**
  1557. * i40e_vc_remove_vlan_msg
  1558. * @vf: pointer to the VF info
  1559. * @msg: pointer to the msg buffer
  1560. * @msglen: msg length
  1561. *
  1562. * remove programmed guest vlan id
  1563. **/
  1564. static int i40e_vc_remove_vlan_msg(struct i40e_vf *vf, u8 *msg, u16 msglen)
  1565. {
  1566. struct i40e_virtchnl_vlan_filter_list *vfl =
  1567. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1568. struct i40e_pf *pf = vf->pf;
  1569. struct i40e_vsi *vsi = NULL;
  1570. u16 vsi_id = vfl->vsi_id;
  1571. i40e_status aq_ret = 0;
  1572. int i;
  1573. if (!test_bit(I40E_VF_STAT_ACTIVE, &vf->vf_states) ||
  1574. !test_bit(I40E_VIRTCHNL_VF_CAP_PRIVILEGE, &vf->vf_caps) ||
  1575. !i40e_vc_isvalid_vsi_id(vf, vsi_id)) {
  1576. aq_ret = I40E_ERR_PARAM;
  1577. goto error_param;
  1578. }
  1579. for (i = 0; i < vfl->num_elements; i++) {
  1580. if (vfl->vlan_id[i] > I40E_MAX_VLANID) {
  1581. aq_ret = I40E_ERR_PARAM;
  1582. goto error_param;
  1583. }
  1584. }
  1585. vsi = pf->vsi[vf->lan_vsi_idx];
  1586. if (vsi->info.pvid) {
  1587. aq_ret = I40E_ERR_PARAM;
  1588. goto error_param;
  1589. }
  1590. for (i = 0; i < vfl->num_elements; i++) {
  1591. int ret = i40e_vsi_kill_vlan(vsi, vfl->vlan_id[i]);
  1592. if (ret)
  1593. dev_err(&pf->pdev->dev,
  1594. "Unable to delete VF vlan filter %d, error %d\n",
  1595. vfl->vlan_id[i], ret);
  1596. }
  1597. error_param:
  1598. /* send the response to the VF */
  1599. return i40e_vc_send_resp_to_vf(vf, I40E_VIRTCHNL_OP_DEL_VLAN, aq_ret);
  1600. }
  1601. /**
  1602. * i40e_vc_validate_vf_msg
  1603. * @vf: pointer to the VF info
  1604. * @msg: pointer to the msg buffer
  1605. * @msglen: msg length
  1606. * @msghndl: msg handle
  1607. *
  1608. * validate msg
  1609. **/
  1610. static int i40e_vc_validate_vf_msg(struct i40e_vf *vf, u32 v_opcode,
  1611. u32 v_retval, u8 *msg, u16 msglen)
  1612. {
  1613. bool err_msg_format = false;
  1614. int valid_len;
  1615. /* Check if VF is disabled. */
  1616. if (test_bit(I40E_VF_STAT_DISABLED, &vf->vf_states))
  1617. return I40E_ERR_PARAM;
  1618. /* Validate message length. */
  1619. switch (v_opcode) {
  1620. case I40E_VIRTCHNL_OP_VERSION:
  1621. valid_len = sizeof(struct i40e_virtchnl_version_info);
  1622. break;
  1623. case I40E_VIRTCHNL_OP_RESET_VF:
  1624. valid_len = 0;
  1625. break;
  1626. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
  1627. if (VF_IS_V11(vf))
  1628. valid_len = sizeof(u32);
  1629. else
  1630. valid_len = 0;
  1631. break;
  1632. case I40E_VIRTCHNL_OP_CONFIG_TX_QUEUE:
  1633. valid_len = sizeof(struct i40e_virtchnl_txq_info);
  1634. break;
  1635. case I40E_VIRTCHNL_OP_CONFIG_RX_QUEUE:
  1636. valid_len = sizeof(struct i40e_virtchnl_rxq_info);
  1637. break;
  1638. case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  1639. valid_len = sizeof(struct i40e_virtchnl_vsi_queue_config_info);
  1640. if (msglen >= valid_len) {
  1641. struct i40e_virtchnl_vsi_queue_config_info *vqc =
  1642. (struct i40e_virtchnl_vsi_queue_config_info *)msg;
  1643. valid_len += (vqc->num_queue_pairs *
  1644. sizeof(struct
  1645. i40e_virtchnl_queue_pair_info));
  1646. if (vqc->num_queue_pairs == 0)
  1647. err_msg_format = true;
  1648. }
  1649. break;
  1650. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  1651. valid_len = sizeof(struct i40e_virtchnl_irq_map_info);
  1652. if (msglen >= valid_len) {
  1653. struct i40e_virtchnl_irq_map_info *vimi =
  1654. (struct i40e_virtchnl_irq_map_info *)msg;
  1655. valid_len += (vimi->num_vectors *
  1656. sizeof(struct i40e_virtchnl_vector_map));
  1657. if (vimi->num_vectors == 0)
  1658. err_msg_format = true;
  1659. }
  1660. break;
  1661. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  1662. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  1663. valid_len = sizeof(struct i40e_virtchnl_queue_select);
  1664. break;
  1665. case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
  1666. case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
  1667. valid_len = sizeof(struct i40e_virtchnl_ether_addr_list);
  1668. if (msglen >= valid_len) {
  1669. struct i40e_virtchnl_ether_addr_list *veal =
  1670. (struct i40e_virtchnl_ether_addr_list *)msg;
  1671. valid_len += veal->num_elements *
  1672. sizeof(struct i40e_virtchnl_ether_addr);
  1673. if (veal->num_elements == 0)
  1674. err_msg_format = true;
  1675. }
  1676. break;
  1677. case I40E_VIRTCHNL_OP_ADD_VLAN:
  1678. case I40E_VIRTCHNL_OP_DEL_VLAN:
  1679. valid_len = sizeof(struct i40e_virtchnl_vlan_filter_list);
  1680. if (msglen >= valid_len) {
  1681. struct i40e_virtchnl_vlan_filter_list *vfl =
  1682. (struct i40e_virtchnl_vlan_filter_list *)msg;
  1683. valid_len += vfl->num_elements * sizeof(u16);
  1684. if (vfl->num_elements == 0)
  1685. err_msg_format = true;
  1686. }
  1687. break;
  1688. case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  1689. valid_len = sizeof(struct i40e_virtchnl_promisc_info);
  1690. break;
  1691. case I40E_VIRTCHNL_OP_GET_STATS:
  1692. valid_len = sizeof(struct i40e_virtchnl_queue_select);
  1693. break;
  1694. /* These are always errors coming from the VF. */
  1695. case I40E_VIRTCHNL_OP_EVENT:
  1696. case I40E_VIRTCHNL_OP_UNKNOWN:
  1697. default:
  1698. return -EPERM;
  1699. }
  1700. /* few more checks */
  1701. if ((valid_len != msglen) || (err_msg_format)) {
  1702. i40e_vc_send_resp_to_vf(vf, v_opcode, I40E_ERR_PARAM);
  1703. return -EINVAL;
  1704. } else {
  1705. return 0;
  1706. }
  1707. }
  1708. /**
  1709. * i40e_vc_process_vf_msg
  1710. * @pf: pointer to the PF structure
  1711. * @vf_id: source VF id
  1712. * @msg: pointer to the msg buffer
  1713. * @msglen: msg length
  1714. * @msghndl: msg handle
  1715. *
  1716. * called from the common aeq/arq handler to
  1717. * process request from VF
  1718. **/
  1719. int i40e_vc_process_vf_msg(struct i40e_pf *pf, u16 vf_id, u32 v_opcode,
  1720. u32 v_retval, u8 *msg, u16 msglen)
  1721. {
  1722. struct i40e_hw *hw = &pf->hw;
  1723. unsigned int local_vf_id = vf_id - hw->func_caps.vf_base_id;
  1724. struct i40e_vf *vf;
  1725. int ret;
  1726. pf->vf_aq_requests++;
  1727. if (local_vf_id >= pf->num_alloc_vfs)
  1728. return -EINVAL;
  1729. vf = &(pf->vf[local_vf_id]);
  1730. /* perform basic checks on the msg */
  1731. ret = i40e_vc_validate_vf_msg(vf, v_opcode, v_retval, msg, msglen);
  1732. if (ret) {
  1733. dev_err(&pf->pdev->dev, "Invalid message from VF %d, opcode %d, len %d\n",
  1734. local_vf_id, v_opcode, msglen);
  1735. return ret;
  1736. }
  1737. switch (v_opcode) {
  1738. case I40E_VIRTCHNL_OP_VERSION:
  1739. ret = i40e_vc_get_version_msg(vf, msg);
  1740. break;
  1741. case I40E_VIRTCHNL_OP_GET_VF_RESOURCES:
  1742. ret = i40e_vc_get_vf_resources_msg(vf, msg);
  1743. break;
  1744. case I40E_VIRTCHNL_OP_RESET_VF:
  1745. i40e_vc_reset_vf_msg(vf);
  1746. ret = 0;
  1747. break;
  1748. case I40E_VIRTCHNL_OP_CONFIG_PROMISCUOUS_MODE:
  1749. ret = i40e_vc_config_promiscuous_mode_msg(vf, msg, msglen);
  1750. break;
  1751. case I40E_VIRTCHNL_OP_CONFIG_VSI_QUEUES:
  1752. ret = i40e_vc_config_queues_msg(vf, msg, msglen);
  1753. break;
  1754. case I40E_VIRTCHNL_OP_CONFIG_IRQ_MAP:
  1755. ret = i40e_vc_config_irq_map_msg(vf, msg, msglen);
  1756. break;
  1757. case I40E_VIRTCHNL_OP_ENABLE_QUEUES:
  1758. ret = i40e_vc_enable_queues_msg(vf, msg, msglen);
  1759. i40e_vc_notify_vf_link_state(vf);
  1760. break;
  1761. case I40E_VIRTCHNL_OP_DISABLE_QUEUES:
  1762. ret = i40e_vc_disable_queues_msg(vf, msg, msglen);
  1763. break;
  1764. case I40E_VIRTCHNL_OP_ADD_ETHER_ADDRESS:
  1765. ret = i40e_vc_add_mac_addr_msg(vf, msg, msglen);
  1766. break;
  1767. case I40E_VIRTCHNL_OP_DEL_ETHER_ADDRESS:
  1768. ret = i40e_vc_del_mac_addr_msg(vf, msg, msglen);
  1769. break;
  1770. case I40E_VIRTCHNL_OP_ADD_VLAN:
  1771. ret = i40e_vc_add_vlan_msg(vf, msg, msglen);
  1772. break;
  1773. case I40E_VIRTCHNL_OP_DEL_VLAN:
  1774. ret = i40e_vc_remove_vlan_msg(vf, msg, msglen);
  1775. break;
  1776. case I40E_VIRTCHNL_OP_GET_STATS:
  1777. ret = i40e_vc_get_stats_msg(vf, msg, msglen);
  1778. break;
  1779. case I40E_VIRTCHNL_OP_UNKNOWN:
  1780. default:
  1781. dev_err(&pf->pdev->dev, "Unsupported opcode %d from VF %d\n",
  1782. v_opcode, local_vf_id);
  1783. ret = i40e_vc_send_resp_to_vf(vf, v_opcode,
  1784. I40E_ERR_NOT_IMPLEMENTED);
  1785. break;
  1786. }
  1787. return ret;
  1788. }
  1789. /**
  1790. * i40e_vc_process_vflr_event
  1791. * @pf: pointer to the PF structure
  1792. *
  1793. * called from the vlfr irq handler to
  1794. * free up VF resources and state variables
  1795. **/
  1796. int i40e_vc_process_vflr_event(struct i40e_pf *pf)
  1797. {
  1798. u32 reg, reg_idx, bit_idx, vf_id;
  1799. struct i40e_hw *hw = &pf->hw;
  1800. struct i40e_vf *vf;
  1801. if (!test_bit(__I40E_VFLR_EVENT_PENDING, &pf->state))
  1802. return 0;
  1803. /* re-enable vflr interrupt cause */
  1804. reg = rd32(hw, I40E_PFINT_ICR0_ENA);
  1805. reg |= I40E_PFINT_ICR0_ENA_VFLR_MASK;
  1806. wr32(hw, I40E_PFINT_ICR0_ENA, reg);
  1807. i40e_flush(hw);
  1808. clear_bit(__I40E_VFLR_EVENT_PENDING, &pf->state);
  1809. for (vf_id = 0; vf_id < pf->num_alloc_vfs; vf_id++) {
  1810. reg_idx = (hw->func_caps.vf_base_id + vf_id) / 32;
  1811. bit_idx = (hw->func_caps.vf_base_id + vf_id) % 32;
  1812. /* read GLGEN_VFLRSTAT register to find out the flr VFs */
  1813. vf = &pf->vf[vf_id];
  1814. reg = rd32(hw, I40E_GLGEN_VFLRSTAT(reg_idx));
  1815. if (reg & BIT(bit_idx)) {
  1816. /* clear the bit in GLGEN_VFLRSTAT */
  1817. wr32(hw, I40E_GLGEN_VFLRSTAT(reg_idx), BIT(bit_idx));
  1818. if (!test_bit(__I40E_DOWN, &pf->state))
  1819. i40e_reset_vf(vf, true);
  1820. }
  1821. }
  1822. return 0;
  1823. }
  1824. /**
  1825. * i40e_ndo_set_vf_mac
  1826. * @netdev: network interface device structure
  1827. * @vf_id: VF identifier
  1828. * @mac: mac address
  1829. *
  1830. * program VF mac address
  1831. **/
  1832. int i40e_ndo_set_vf_mac(struct net_device *netdev, int vf_id, u8 *mac)
  1833. {
  1834. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1835. struct i40e_vsi *vsi = np->vsi;
  1836. struct i40e_pf *pf = vsi->back;
  1837. struct i40e_mac_filter *f;
  1838. struct i40e_vf *vf;
  1839. int ret = 0;
  1840. /* validate the request */
  1841. if (vf_id >= pf->num_alloc_vfs) {
  1842. dev_err(&pf->pdev->dev,
  1843. "Invalid VF Identifier %d\n", vf_id);
  1844. ret = -EINVAL;
  1845. goto error_param;
  1846. }
  1847. vf = &(pf->vf[vf_id]);
  1848. vsi = pf->vsi[vf->lan_vsi_idx];
  1849. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1850. dev_err(&pf->pdev->dev,
  1851. "Uninitialized VF %d\n", vf_id);
  1852. ret = -EINVAL;
  1853. goto error_param;
  1854. }
  1855. if (!is_valid_ether_addr(mac)) {
  1856. dev_err(&pf->pdev->dev,
  1857. "Invalid VF ethernet address\n");
  1858. ret = -EINVAL;
  1859. goto error_param;
  1860. }
  1861. /* Lock once because below invoked function add/del_filter requires
  1862. * mac_filter_list_lock to be held
  1863. */
  1864. spin_lock_bh(&vsi->mac_filter_list_lock);
  1865. /* delete the temporary mac address */
  1866. i40e_del_filter(vsi, vf->default_lan_addr.addr,
  1867. vf->port_vlan_id ? vf->port_vlan_id : -1,
  1868. true, false);
  1869. /* Delete all the filters for this VSI - we're going to kill it
  1870. * anyway.
  1871. */
  1872. list_for_each_entry(f, &vsi->mac_filter_list, list)
  1873. i40e_del_filter(vsi, f->macaddr, f->vlan, true, false);
  1874. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1875. dev_info(&pf->pdev->dev, "Setting MAC %pM on VF %d\n", mac, vf_id);
  1876. /* program mac filter */
  1877. if (i40e_sync_vsi_filters(vsi, false)) {
  1878. dev_err(&pf->pdev->dev, "Unable to program ucast filters\n");
  1879. ret = -EIO;
  1880. goto error_param;
  1881. }
  1882. ether_addr_copy(vf->default_lan_addr.addr, mac);
  1883. vf->pf_set_mac = true;
  1884. /* Force the VF driver stop so it has to reload with new MAC address */
  1885. i40e_vc_disable_vf(pf, vf);
  1886. dev_info(&pf->pdev->dev, "Reload the VF driver to make this change effective.\n");
  1887. error_param:
  1888. return ret;
  1889. }
  1890. /**
  1891. * i40e_ndo_set_vf_port_vlan
  1892. * @netdev: network interface device structure
  1893. * @vf_id: VF identifier
  1894. * @vlan_id: mac address
  1895. * @qos: priority setting
  1896. *
  1897. * program VF vlan id and/or qos
  1898. **/
  1899. int i40e_ndo_set_vf_port_vlan(struct net_device *netdev,
  1900. int vf_id, u16 vlan_id, u8 qos)
  1901. {
  1902. u16 vlanprio = vlan_id | (qos << I40E_VLAN_PRIORITY_SHIFT);
  1903. struct i40e_netdev_priv *np = netdev_priv(netdev);
  1904. struct i40e_pf *pf = np->vsi->back;
  1905. bool is_vsi_in_vlan = false;
  1906. struct i40e_vsi *vsi;
  1907. struct i40e_vf *vf;
  1908. int ret = 0;
  1909. /* validate the request */
  1910. if (vf_id >= pf->num_alloc_vfs) {
  1911. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  1912. ret = -EINVAL;
  1913. goto error_pvid;
  1914. }
  1915. if ((vlan_id > I40E_MAX_VLANID) || (qos > 7)) {
  1916. dev_err(&pf->pdev->dev, "Invalid VF Parameters\n");
  1917. ret = -EINVAL;
  1918. goto error_pvid;
  1919. }
  1920. vf = &(pf->vf[vf_id]);
  1921. vsi = pf->vsi[vf->lan_vsi_idx];
  1922. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  1923. dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
  1924. ret = -EINVAL;
  1925. goto error_pvid;
  1926. }
  1927. if (le16_to_cpu(vsi->info.pvid) == vlanprio)
  1928. /* duplicate request, so just return success */
  1929. goto error_pvid;
  1930. spin_lock_bh(&vsi->mac_filter_list_lock);
  1931. is_vsi_in_vlan = i40e_is_vsi_in_vlan(vsi);
  1932. spin_unlock_bh(&vsi->mac_filter_list_lock);
  1933. if (le16_to_cpu(vsi->info.pvid) == 0 && is_vsi_in_vlan) {
  1934. dev_err(&pf->pdev->dev,
  1935. "VF %d has already configured VLAN filters and the administrator is requesting a port VLAN override.\nPlease unload and reload the VF driver for this change to take effect.\n",
  1936. vf_id);
  1937. /* Administrator Error - knock the VF offline until he does
  1938. * the right thing by reconfiguring his network correctly
  1939. * and then reloading the VF driver.
  1940. */
  1941. i40e_vc_disable_vf(pf, vf);
  1942. }
  1943. /* Check for condition where there was already a port VLAN ID
  1944. * filter set and now it is being deleted by setting it to zero.
  1945. * Additionally check for the condition where there was a port
  1946. * VLAN but now there is a new and different port VLAN being set.
  1947. * Before deleting all the old VLAN filters we must add new ones
  1948. * with -1 (I40E_VLAN_ANY) or otherwise we're left with all our
  1949. * MAC addresses deleted.
  1950. */
  1951. if ((!(vlan_id || qos) ||
  1952. vlanprio != le16_to_cpu(vsi->info.pvid)) &&
  1953. vsi->info.pvid)
  1954. ret = i40e_vsi_add_vlan(vsi, I40E_VLAN_ANY);
  1955. if (vsi->info.pvid) {
  1956. /* kill old VLAN */
  1957. ret = i40e_vsi_kill_vlan(vsi, (le16_to_cpu(vsi->info.pvid) &
  1958. VLAN_VID_MASK));
  1959. if (ret) {
  1960. dev_info(&vsi->back->pdev->dev,
  1961. "remove VLAN failed, ret=%d, aq_err=%d\n",
  1962. ret, pf->hw.aq.asq_last_status);
  1963. }
  1964. }
  1965. if (vlan_id || qos)
  1966. ret = i40e_vsi_add_pvid(vsi, vlanprio);
  1967. else
  1968. i40e_vsi_remove_pvid(vsi);
  1969. if (vlan_id) {
  1970. dev_info(&pf->pdev->dev, "Setting VLAN %d, QOS 0x%x on VF %d\n",
  1971. vlan_id, qos, vf_id);
  1972. /* add new VLAN filter */
  1973. ret = i40e_vsi_add_vlan(vsi, vlan_id);
  1974. if (ret) {
  1975. dev_info(&vsi->back->pdev->dev,
  1976. "add VF VLAN failed, ret=%d aq_err=%d\n", ret,
  1977. vsi->back->hw.aq.asq_last_status);
  1978. goto error_pvid;
  1979. }
  1980. /* Kill non-vlan MAC filters - ignore error return since
  1981. * there might not be any non-vlan MAC filters.
  1982. */
  1983. i40e_vsi_kill_vlan(vsi, I40E_VLAN_ANY);
  1984. }
  1985. if (ret) {
  1986. dev_err(&pf->pdev->dev, "Unable to update VF vsi context\n");
  1987. goto error_pvid;
  1988. }
  1989. /* The Port VLAN needs to be saved across resets the same as the
  1990. * default LAN MAC address.
  1991. */
  1992. vf->port_vlan_id = le16_to_cpu(vsi->info.pvid);
  1993. ret = 0;
  1994. error_pvid:
  1995. return ret;
  1996. }
  1997. #define I40E_BW_CREDIT_DIVISOR 50 /* 50Mbps per BW credit */
  1998. #define I40E_MAX_BW_INACTIVE_ACCUM 4 /* device can accumulate 4 credits max */
  1999. /**
  2000. * i40e_ndo_set_vf_bw
  2001. * @netdev: network interface device structure
  2002. * @vf_id: VF identifier
  2003. * @tx_rate: Tx rate
  2004. *
  2005. * configure VF Tx rate
  2006. **/
  2007. int i40e_ndo_set_vf_bw(struct net_device *netdev, int vf_id, int min_tx_rate,
  2008. int max_tx_rate)
  2009. {
  2010. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2011. struct i40e_pf *pf = np->vsi->back;
  2012. struct i40e_vsi *vsi;
  2013. struct i40e_vf *vf;
  2014. int speed = 0;
  2015. int ret = 0;
  2016. /* validate the request */
  2017. if (vf_id >= pf->num_alloc_vfs) {
  2018. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d.\n", vf_id);
  2019. ret = -EINVAL;
  2020. goto error;
  2021. }
  2022. if (min_tx_rate) {
  2023. dev_err(&pf->pdev->dev, "Invalid min tx rate (%d) (greater than 0) specified for VF %d.\n",
  2024. min_tx_rate, vf_id);
  2025. return -EINVAL;
  2026. }
  2027. vf = &(pf->vf[vf_id]);
  2028. vsi = pf->vsi[vf->lan_vsi_idx];
  2029. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  2030. dev_err(&pf->pdev->dev, "Uninitialized VF %d.\n", vf_id);
  2031. ret = -EINVAL;
  2032. goto error;
  2033. }
  2034. switch (pf->hw.phy.link_info.link_speed) {
  2035. case I40E_LINK_SPEED_40GB:
  2036. speed = 40000;
  2037. break;
  2038. case I40E_LINK_SPEED_10GB:
  2039. speed = 10000;
  2040. break;
  2041. case I40E_LINK_SPEED_1GB:
  2042. speed = 1000;
  2043. break;
  2044. default:
  2045. break;
  2046. }
  2047. if (max_tx_rate > speed) {
  2048. dev_err(&pf->pdev->dev, "Invalid max tx rate %d specified for VF %d.",
  2049. max_tx_rate, vf->vf_id);
  2050. ret = -EINVAL;
  2051. goto error;
  2052. }
  2053. if ((max_tx_rate < 50) && (max_tx_rate > 0)) {
  2054. dev_warn(&pf->pdev->dev, "Setting max Tx rate to minimum usable value of 50Mbps.\n");
  2055. max_tx_rate = 50;
  2056. }
  2057. /* Tx rate credits are in values of 50Mbps, 0 is disabled*/
  2058. ret = i40e_aq_config_vsi_bw_limit(&pf->hw, vsi->seid,
  2059. max_tx_rate / I40E_BW_CREDIT_DIVISOR,
  2060. I40E_MAX_BW_INACTIVE_ACCUM, NULL);
  2061. if (ret) {
  2062. dev_err(&pf->pdev->dev, "Unable to set max tx rate, error code %d.\n",
  2063. ret);
  2064. ret = -EIO;
  2065. goto error;
  2066. }
  2067. vf->tx_rate = max_tx_rate;
  2068. error:
  2069. return ret;
  2070. }
  2071. /**
  2072. * i40e_ndo_get_vf_config
  2073. * @netdev: network interface device structure
  2074. * @vf_id: VF identifier
  2075. * @ivi: VF configuration structure
  2076. *
  2077. * return VF configuration
  2078. **/
  2079. int i40e_ndo_get_vf_config(struct net_device *netdev,
  2080. int vf_id, struct ifla_vf_info *ivi)
  2081. {
  2082. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2083. struct i40e_vsi *vsi = np->vsi;
  2084. struct i40e_pf *pf = vsi->back;
  2085. struct i40e_vf *vf;
  2086. int ret = 0;
  2087. /* validate the request */
  2088. if (vf_id >= pf->num_alloc_vfs) {
  2089. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2090. ret = -EINVAL;
  2091. goto error_param;
  2092. }
  2093. vf = &(pf->vf[vf_id]);
  2094. /* first vsi is always the LAN vsi */
  2095. vsi = pf->vsi[vf->lan_vsi_idx];
  2096. if (!test_bit(I40E_VF_STAT_INIT, &vf->vf_states)) {
  2097. dev_err(&pf->pdev->dev, "Uninitialized VF %d\n", vf_id);
  2098. ret = -EINVAL;
  2099. goto error_param;
  2100. }
  2101. ivi->vf = vf_id;
  2102. ether_addr_copy(ivi->mac, vf->default_lan_addr.addr);
  2103. ivi->max_tx_rate = vf->tx_rate;
  2104. ivi->min_tx_rate = 0;
  2105. ivi->vlan = le16_to_cpu(vsi->info.pvid) & I40E_VLAN_MASK;
  2106. ivi->qos = (le16_to_cpu(vsi->info.pvid) & I40E_PRIORITY_MASK) >>
  2107. I40E_VLAN_PRIORITY_SHIFT;
  2108. if (vf->link_forced == false)
  2109. ivi->linkstate = IFLA_VF_LINK_STATE_AUTO;
  2110. else if (vf->link_up == true)
  2111. ivi->linkstate = IFLA_VF_LINK_STATE_ENABLE;
  2112. else
  2113. ivi->linkstate = IFLA_VF_LINK_STATE_DISABLE;
  2114. ivi->spoofchk = vf->spoofchk;
  2115. ret = 0;
  2116. error_param:
  2117. return ret;
  2118. }
  2119. /**
  2120. * i40e_ndo_set_vf_link_state
  2121. * @netdev: network interface device structure
  2122. * @vf_id: VF identifier
  2123. * @link: required link state
  2124. *
  2125. * Set the link state of a specified VF, regardless of physical link state
  2126. **/
  2127. int i40e_ndo_set_vf_link_state(struct net_device *netdev, int vf_id, int link)
  2128. {
  2129. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2130. struct i40e_pf *pf = np->vsi->back;
  2131. struct i40e_virtchnl_pf_event pfe;
  2132. struct i40e_hw *hw = &pf->hw;
  2133. struct i40e_vf *vf;
  2134. int abs_vf_id;
  2135. int ret = 0;
  2136. /* validate the request */
  2137. if (vf_id >= pf->num_alloc_vfs) {
  2138. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2139. ret = -EINVAL;
  2140. goto error_out;
  2141. }
  2142. vf = &pf->vf[vf_id];
  2143. abs_vf_id = vf->vf_id + hw->func_caps.vf_base_id;
  2144. pfe.event = I40E_VIRTCHNL_EVENT_LINK_CHANGE;
  2145. pfe.severity = I40E_PF_EVENT_SEVERITY_INFO;
  2146. switch (link) {
  2147. case IFLA_VF_LINK_STATE_AUTO:
  2148. vf->link_forced = false;
  2149. pfe.event_data.link_event.link_status =
  2150. pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP;
  2151. pfe.event_data.link_event.link_speed =
  2152. pf->hw.phy.link_info.link_speed;
  2153. break;
  2154. case IFLA_VF_LINK_STATE_ENABLE:
  2155. vf->link_forced = true;
  2156. vf->link_up = true;
  2157. pfe.event_data.link_event.link_status = true;
  2158. pfe.event_data.link_event.link_speed = I40E_LINK_SPEED_40GB;
  2159. break;
  2160. case IFLA_VF_LINK_STATE_DISABLE:
  2161. vf->link_forced = true;
  2162. vf->link_up = false;
  2163. pfe.event_data.link_event.link_status = false;
  2164. pfe.event_data.link_event.link_speed = 0;
  2165. break;
  2166. default:
  2167. ret = -EINVAL;
  2168. goto error_out;
  2169. }
  2170. /* Notify the VF of its new link state */
  2171. i40e_aq_send_msg_to_vf(hw, abs_vf_id, I40E_VIRTCHNL_OP_EVENT,
  2172. 0, (u8 *)&pfe, sizeof(pfe), NULL);
  2173. error_out:
  2174. return ret;
  2175. }
  2176. /**
  2177. * i40e_ndo_set_vf_spoofchk
  2178. * @netdev: network interface device structure
  2179. * @vf_id: VF identifier
  2180. * @enable: flag to enable or disable feature
  2181. *
  2182. * Enable or disable VF spoof checking
  2183. **/
  2184. int i40e_ndo_set_vf_spoofchk(struct net_device *netdev, int vf_id, bool enable)
  2185. {
  2186. struct i40e_netdev_priv *np = netdev_priv(netdev);
  2187. struct i40e_vsi *vsi = np->vsi;
  2188. struct i40e_pf *pf = vsi->back;
  2189. struct i40e_vsi_context ctxt;
  2190. struct i40e_hw *hw = &pf->hw;
  2191. struct i40e_vf *vf;
  2192. int ret = 0;
  2193. /* validate the request */
  2194. if (vf_id >= pf->num_alloc_vfs) {
  2195. dev_err(&pf->pdev->dev, "Invalid VF Identifier %d\n", vf_id);
  2196. ret = -EINVAL;
  2197. goto out;
  2198. }
  2199. vf = &(pf->vf[vf_id]);
  2200. if (enable == vf->spoofchk)
  2201. goto out;
  2202. vf->spoofchk = enable;
  2203. memset(&ctxt, 0, sizeof(ctxt));
  2204. ctxt.seid = pf->vsi[vf->lan_vsi_idx]->seid;
  2205. ctxt.pf_num = pf->hw.pf_id;
  2206. ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
  2207. if (enable)
  2208. ctxt.info.sec_flags |= (I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
  2209. I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
  2210. ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
  2211. if (ret) {
  2212. dev_err(&pf->pdev->dev, "Error %d updating VSI parameters\n",
  2213. ret);
  2214. ret = -EIO;
  2215. }
  2216. out:
  2217. return ret;
  2218. }