i40e_virtchnl_pf.c 65 KB

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