i40e_virtchnl_pf.c 64 KB

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