i40e_virtchnl_pf.c 74 KB

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