i40e_virtchnl_pf.c 63 KB

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