i40e_virtchnl_pf.c 62 KB

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