i40e_virtchnl_pf.c 63 KB

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