i40e_virtchnl_pf.c 62 KB

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