i40e_virtchnl_pf.c 67 KB

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