i40evf_main.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 2013 - 2018 Intel Corporation. */
  3. #include "i40evf.h"
  4. #include "i40e_prototype.h"
  5. #include "i40evf_client.h"
  6. /* All i40evf tracepoints are defined by the include below, which must
  7. * be included exactly once across the whole kernel with
  8. * CREATE_TRACE_POINTS defined
  9. */
  10. #define CREATE_TRACE_POINTS
  11. #include "i40e_trace.h"
  12. static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
  13. static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
  14. static int i40evf_close(struct net_device *netdev);
  15. char i40evf_driver_name[] = "i40evf";
  16. static const char i40evf_driver_string[] =
  17. "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
  18. #define DRV_KERN "-k"
  19. #define DRV_VERSION_MAJOR 3
  20. #define DRV_VERSION_MINOR 2
  21. #define DRV_VERSION_BUILD 2
  22. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  23. __stringify(DRV_VERSION_MINOR) "." \
  24. __stringify(DRV_VERSION_BUILD) \
  25. DRV_KERN
  26. const char i40evf_driver_version[] = DRV_VERSION;
  27. static const char i40evf_copyright[] =
  28. "Copyright (c) 2013 - 2015 Intel Corporation.";
  29. /* i40evf_pci_tbl - PCI Device ID Table
  30. *
  31. * Wildcard entries (PCI_ANY_ID) should come last
  32. * Last entry must be all 0s
  33. *
  34. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  35. * Class, Class Mask, private data (not used) }
  36. */
  37. static const struct pci_device_id i40evf_pci_tbl[] = {
  38. {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
  39. {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
  40. {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
  41. {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
  42. /* required last entry */
  43. {0, }
  44. };
  45. MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
  46. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  47. MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
  48. MODULE_LICENSE("GPL");
  49. MODULE_VERSION(DRV_VERSION);
  50. static struct workqueue_struct *i40evf_wq;
  51. /**
  52. * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
  53. * @hw: pointer to the HW structure
  54. * @mem: ptr to mem struct to fill out
  55. * @size: size of memory requested
  56. * @alignment: what to align the allocation to
  57. **/
  58. i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
  59. struct i40e_dma_mem *mem,
  60. u64 size, u32 alignment)
  61. {
  62. struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  63. if (!mem)
  64. return I40E_ERR_PARAM;
  65. mem->size = ALIGN(size, alignment);
  66. mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
  67. (dma_addr_t *)&mem->pa, GFP_KERNEL);
  68. if (mem->va)
  69. return 0;
  70. else
  71. return I40E_ERR_NO_MEMORY;
  72. }
  73. /**
  74. * i40evf_free_dma_mem_d - OS specific memory free for shared code
  75. * @hw: pointer to the HW structure
  76. * @mem: ptr to mem struct to free
  77. **/
  78. i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  79. {
  80. struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  81. if (!mem || !mem->va)
  82. return I40E_ERR_PARAM;
  83. dma_free_coherent(&adapter->pdev->dev, mem->size,
  84. mem->va, (dma_addr_t)mem->pa);
  85. return 0;
  86. }
  87. /**
  88. * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
  89. * @hw: pointer to the HW structure
  90. * @mem: ptr to mem struct to fill out
  91. * @size: size of memory requested
  92. **/
  93. i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
  94. struct i40e_virt_mem *mem, u32 size)
  95. {
  96. if (!mem)
  97. return I40E_ERR_PARAM;
  98. mem->size = size;
  99. mem->va = kzalloc(size, GFP_KERNEL);
  100. if (mem->va)
  101. return 0;
  102. else
  103. return I40E_ERR_NO_MEMORY;
  104. }
  105. /**
  106. * i40evf_free_virt_mem_d - OS specific memory free for shared code
  107. * @hw: pointer to the HW structure
  108. * @mem: ptr to mem struct to free
  109. **/
  110. i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
  111. struct i40e_virt_mem *mem)
  112. {
  113. if (!mem)
  114. return I40E_ERR_PARAM;
  115. /* it's ok to kfree a NULL pointer */
  116. kfree(mem->va);
  117. return 0;
  118. }
  119. /**
  120. * i40evf_debug_d - OS dependent version of debug printing
  121. * @hw: pointer to the HW structure
  122. * @mask: debug level mask
  123. * @fmt_str: printf-type format description
  124. **/
  125. void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
  126. {
  127. char buf[512];
  128. va_list argptr;
  129. if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
  130. return;
  131. va_start(argptr, fmt_str);
  132. vsnprintf(buf, sizeof(buf), fmt_str, argptr);
  133. va_end(argptr);
  134. /* the debug string is already formatted with a newline */
  135. pr_info("%s", buf);
  136. }
  137. /**
  138. * i40evf_schedule_reset - Set the flags and schedule a reset event
  139. * @adapter: board private structure
  140. **/
  141. void i40evf_schedule_reset(struct i40evf_adapter *adapter)
  142. {
  143. if (!(adapter->flags &
  144. (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
  145. adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
  146. schedule_work(&adapter->reset_task);
  147. }
  148. }
  149. /**
  150. * i40evf_tx_timeout - Respond to a Tx Hang
  151. * @netdev: network interface device structure
  152. **/
  153. static void i40evf_tx_timeout(struct net_device *netdev)
  154. {
  155. struct i40evf_adapter *adapter = netdev_priv(netdev);
  156. adapter->tx_timeout_count++;
  157. i40evf_schedule_reset(adapter);
  158. }
  159. /**
  160. * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
  161. * @adapter: board private structure
  162. **/
  163. static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
  164. {
  165. struct i40e_hw *hw = &adapter->hw;
  166. if (!adapter->msix_entries)
  167. return;
  168. wr32(hw, I40E_VFINT_DYN_CTL01, 0);
  169. /* read flush */
  170. rd32(hw, I40E_VFGEN_RSTAT);
  171. synchronize_irq(adapter->msix_entries[0].vector);
  172. }
  173. /**
  174. * i40evf_misc_irq_enable - Enable default interrupt generation settings
  175. * @adapter: board private structure
  176. **/
  177. static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
  178. {
  179. struct i40e_hw *hw = &adapter->hw;
  180. wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
  181. I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
  182. wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
  183. /* read flush */
  184. rd32(hw, I40E_VFGEN_RSTAT);
  185. }
  186. /**
  187. * i40evf_irq_disable - Mask off interrupt generation on the NIC
  188. * @adapter: board private structure
  189. **/
  190. static void i40evf_irq_disable(struct i40evf_adapter *adapter)
  191. {
  192. int i;
  193. struct i40e_hw *hw = &adapter->hw;
  194. if (!adapter->msix_entries)
  195. return;
  196. for (i = 1; i < adapter->num_msix_vectors; i++) {
  197. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
  198. synchronize_irq(adapter->msix_entries[i].vector);
  199. }
  200. /* read flush */
  201. rd32(hw, I40E_VFGEN_RSTAT);
  202. }
  203. /**
  204. * i40evf_irq_enable_queues - Enable interrupt for specified queues
  205. * @adapter: board private structure
  206. * @mask: bitmap of queues to enable
  207. **/
  208. void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
  209. {
  210. struct i40e_hw *hw = &adapter->hw;
  211. int i;
  212. for (i = 1; i < adapter->num_msix_vectors; i++) {
  213. if (mask & BIT(i - 1)) {
  214. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
  215. I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  216. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK);
  217. }
  218. }
  219. }
  220. /**
  221. * i40evf_irq_enable - Enable default interrupt generation settings
  222. * @adapter: board private structure
  223. * @flush: boolean value whether to run rd32()
  224. **/
  225. void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
  226. {
  227. struct i40e_hw *hw = &adapter->hw;
  228. i40evf_misc_irq_enable(adapter);
  229. i40evf_irq_enable_queues(adapter, ~0);
  230. if (flush)
  231. rd32(hw, I40E_VFGEN_RSTAT);
  232. }
  233. /**
  234. * i40evf_msix_aq - Interrupt handler for vector 0
  235. * @irq: interrupt number
  236. * @data: pointer to netdev
  237. **/
  238. static irqreturn_t i40evf_msix_aq(int irq, void *data)
  239. {
  240. struct net_device *netdev = data;
  241. struct i40evf_adapter *adapter = netdev_priv(netdev);
  242. struct i40e_hw *hw = &adapter->hw;
  243. /* handle non-queue interrupts, these reads clear the registers */
  244. rd32(hw, I40E_VFINT_ICR01);
  245. rd32(hw, I40E_VFINT_ICR0_ENA1);
  246. /* schedule work on the private workqueue */
  247. schedule_work(&adapter->adminq_task);
  248. return IRQ_HANDLED;
  249. }
  250. /**
  251. * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
  252. * @irq: interrupt number
  253. * @data: pointer to a q_vector
  254. **/
  255. static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
  256. {
  257. struct i40e_q_vector *q_vector = data;
  258. if (!q_vector->tx.ring && !q_vector->rx.ring)
  259. return IRQ_HANDLED;
  260. napi_schedule_irqoff(&q_vector->napi);
  261. return IRQ_HANDLED;
  262. }
  263. /**
  264. * i40evf_map_vector_to_rxq - associate irqs with rx queues
  265. * @adapter: board private structure
  266. * @v_idx: interrupt number
  267. * @r_idx: queue number
  268. **/
  269. static void
  270. i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
  271. {
  272. struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
  273. struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
  274. struct i40e_hw *hw = &adapter->hw;
  275. rx_ring->q_vector = q_vector;
  276. rx_ring->next = q_vector->rx.ring;
  277. rx_ring->vsi = &adapter->vsi;
  278. q_vector->rx.ring = rx_ring;
  279. q_vector->rx.count++;
  280. q_vector->rx.next_update = jiffies + 1;
  281. q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
  282. q_vector->ring_mask |= BIT(r_idx);
  283. wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, q_vector->reg_idx),
  284. q_vector->rx.current_itr);
  285. q_vector->rx.current_itr = q_vector->rx.target_itr;
  286. }
  287. /**
  288. * i40evf_map_vector_to_txq - associate irqs with tx queues
  289. * @adapter: board private structure
  290. * @v_idx: interrupt number
  291. * @t_idx: queue number
  292. **/
  293. static void
  294. i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
  295. {
  296. struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
  297. struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
  298. struct i40e_hw *hw = &adapter->hw;
  299. tx_ring->q_vector = q_vector;
  300. tx_ring->next = q_vector->tx.ring;
  301. tx_ring->vsi = &adapter->vsi;
  302. q_vector->tx.ring = tx_ring;
  303. q_vector->tx.count++;
  304. q_vector->tx.next_update = jiffies + 1;
  305. q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
  306. q_vector->num_ringpairs++;
  307. wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, q_vector->reg_idx),
  308. q_vector->tx.target_itr);
  309. q_vector->tx.current_itr = q_vector->tx.target_itr;
  310. }
  311. /**
  312. * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
  313. * @adapter: board private structure to initialize
  314. *
  315. * This function maps descriptor rings to the queue-specific vectors
  316. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  317. * one vector per ring/queue, but on a constrained vector budget, we
  318. * group the rings as "efficiently" as possible. You would add new
  319. * mapping configurations in here.
  320. **/
  321. static void i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
  322. {
  323. int rings_remaining = adapter->num_active_queues;
  324. int ridx = 0, vidx = 0;
  325. int q_vectors;
  326. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  327. for (; ridx < rings_remaining; ridx++) {
  328. i40evf_map_vector_to_rxq(adapter, vidx, ridx);
  329. i40evf_map_vector_to_txq(adapter, vidx, ridx);
  330. /* In the case where we have more queues than vectors, continue
  331. * round-robin on vectors until all queues are mapped.
  332. */
  333. if (++vidx >= q_vectors)
  334. vidx = 0;
  335. }
  336. adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
  337. }
  338. #ifdef CONFIG_NET_POLL_CONTROLLER
  339. /**
  340. * i40evf_netpoll - A Polling 'interrupt' handler
  341. * @netdev: network interface device structure
  342. *
  343. * This is used by netconsole to send skbs without having to re-enable
  344. * interrupts. It's not called while the normal interrupt routine is executing.
  345. **/
  346. static void i40evf_netpoll(struct net_device *netdev)
  347. {
  348. struct i40evf_adapter *adapter = netdev_priv(netdev);
  349. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  350. int i;
  351. /* if interface is down do nothing */
  352. if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
  353. return;
  354. for (i = 0; i < q_vectors; i++)
  355. i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
  356. }
  357. #endif
  358. /**
  359. * i40evf_irq_affinity_notify - Callback for affinity changes
  360. * @notify: context as to what irq was changed
  361. * @mask: the new affinity mask
  362. *
  363. * This is a callback function used by the irq_set_affinity_notifier function
  364. * so that we may register to receive changes to the irq affinity masks.
  365. **/
  366. static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
  367. const cpumask_t *mask)
  368. {
  369. struct i40e_q_vector *q_vector =
  370. container_of(notify, struct i40e_q_vector, affinity_notify);
  371. cpumask_copy(&q_vector->affinity_mask, mask);
  372. }
  373. /**
  374. * i40evf_irq_affinity_release - Callback for affinity notifier release
  375. * @ref: internal core kernel usage
  376. *
  377. * This is a callback function used by the irq_set_affinity_notifier function
  378. * to inform the current notification subscriber that they will no longer
  379. * receive notifications.
  380. **/
  381. static void i40evf_irq_affinity_release(struct kref *ref) {}
  382. /**
  383. * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
  384. * @adapter: board private structure
  385. * @basename: device basename
  386. *
  387. * Allocates MSI-X vectors for tx and rx handling, and requests
  388. * interrupts from the kernel.
  389. **/
  390. static int
  391. i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
  392. {
  393. unsigned int vector, q_vectors;
  394. unsigned int rx_int_idx = 0, tx_int_idx = 0;
  395. int irq_num, err;
  396. int cpu;
  397. i40evf_irq_disable(adapter);
  398. /* Decrement for Other and TCP Timer vectors */
  399. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  400. for (vector = 0; vector < q_vectors; vector++) {
  401. struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
  402. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  403. if (q_vector->tx.ring && q_vector->rx.ring) {
  404. snprintf(q_vector->name, sizeof(q_vector->name),
  405. "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
  406. tx_int_idx++;
  407. } else if (q_vector->rx.ring) {
  408. snprintf(q_vector->name, sizeof(q_vector->name),
  409. "i40evf-%s-rx-%d", basename, rx_int_idx++);
  410. } else if (q_vector->tx.ring) {
  411. snprintf(q_vector->name, sizeof(q_vector->name),
  412. "i40evf-%s-tx-%d", basename, tx_int_idx++);
  413. } else {
  414. /* skip this unused q_vector */
  415. continue;
  416. }
  417. err = request_irq(irq_num,
  418. i40evf_msix_clean_rings,
  419. 0,
  420. q_vector->name,
  421. q_vector);
  422. if (err) {
  423. dev_info(&adapter->pdev->dev,
  424. "Request_irq failed, error: %d\n", err);
  425. goto free_queue_irqs;
  426. }
  427. /* register for affinity change notifications */
  428. q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
  429. q_vector->affinity_notify.release =
  430. i40evf_irq_affinity_release;
  431. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  432. /* Spread the IRQ affinity hints across online CPUs. Note that
  433. * get_cpu_mask returns a mask with a permanent lifetime so
  434. * it's safe to use as a hint for irq_set_affinity_hint.
  435. */
  436. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  437. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  438. }
  439. return 0;
  440. free_queue_irqs:
  441. while (vector) {
  442. vector--;
  443. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  444. irq_set_affinity_notifier(irq_num, NULL);
  445. irq_set_affinity_hint(irq_num, NULL);
  446. free_irq(irq_num, &adapter->q_vectors[vector]);
  447. }
  448. return err;
  449. }
  450. /**
  451. * i40evf_request_misc_irq - Initialize MSI-X interrupts
  452. * @adapter: board private structure
  453. *
  454. * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
  455. * vector is only for the admin queue, and stays active even when the netdev
  456. * is closed.
  457. **/
  458. static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
  459. {
  460. struct net_device *netdev = adapter->netdev;
  461. int err;
  462. snprintf(adapter->misc_vector_name,
  463. sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
  464. dev_name(&adapter->pdev->dev));
  465. err = request_irq(adapter->msix_entries[0].vector,
  466. &i40evf_msix_aq, 0,
  467. adapter->misc_vector_name, netdev);
  468. if (err) {
  469. dev_err(&adapter->pdev->dev,
  470. "request_irq for %s failed: %d\n",
  471. adapter->misc_vector_name, err);
  472. free_irq(adapter->msix_entries[0].vector, netdev);
  473. }
  474. return err;
  475. }
  476. /**
  477. * i40evf_free_traffic_irqs - Free MSI-X interrupts
  478. * @adapter: board private structure
  479. *
  480. * Frees all MSI-X vectors other than 0.
  481. **/
  482. static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
  483. {
  484. int vector, irq_num, q_vectors;
  485. if (!adapter->msix_entries)
  486. return;
  487. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  488. for (vector = 0; vector < q_vectors; vector++) {
  489. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  490. irq_set_affinity_notifier(irq_num, NULL);
  491. irq_set_affinity_hint(irq_num, NULL);
  492. free_irq(irq_num, &adapter->q_vectors[vector]);
  493. }
  494. }
  495. /**
  496. * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
  497. * @adapter: board private structure
  498. *
  499. * Frees MSI-X vector 0.
  500. **/
  501. static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
  502. {
  503. struct net_device *netdev = adapter->netdev;
  504. if (!adapter->msix_entries)
  505. return;
  506. free_irq(adapter->msix_entries[0].vector, netdev);
  507. }
  508. /**
  509. * i40evf_configure_tx - Configure Transmit Unit after Reset
  510. * @adapter: board private structure
  511. *
  512. * Configure the Tx unit of the MAC after a reset.
  513. **/
  514. static void i40evf_configure_tx(struct i40evf_adapter *adapter)
  515. {
  516. struct i40e_hw *hw = &adapter->hw;
  517. int i;
  518. for (i = 0; i < adapter->num_active_queues; i++)
  519. adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
  520. }
  521. /**
  522. * i40evf_configure_rx - Configure Receive Unit after Reset
  523. * @adapter: board private structure
  524. *
  525. * Configure the Rx unit of the MAC after a reset.
  526. **/
  527. static void i40evf_configure_rx(struct i40evf_adapter *adapter)
  528. {
  529. unsigned int rx_buf_len = I40E_RXBUFFER_2048;
  530. struct i40e_hw *hw = &adapter->hw;
  531. int i;
  532. /* Legacy Rx will always default to a 2048 buffer size. */
  533. #if (PAGE_SIZE < 8192)
  534. if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
  535. struct net_device *netdev = adapter->netdev;
  536. /* For jumbo frames on systems with 4K pages we have to use
  537. * an order 1 page, so we might as well increase the size
  538. * of our Rx buffer to make better use of the available space
  539. */
  540. rx_buf_len = I40E_RXBUFFER_3072;
  541. /* We use a 1536 buffer size for configurations with
  542. * standard Ethernet mtu. On x86 this gives us enough room
  543. * for shared info and 192 bytes of padding.
  544. */
  545. if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  546. (netdev->mtu <= ETH_DATA_LEN))
  547. rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  548. }
  549. #endif
  550. for (i = 0; i < adapter->num_active_queues; i++) {
  551. adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
  552. adapter->rx_rings[i].rx_buf_len = rx_buf_len;
  553. if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
  554. clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
  555. else
  556. set_ring_build_skb_enabled(&adapter->rx_rings[i]);
  557. }
  558. }
  559. /**
  560. * i40evf_find_vlan - Search filter list for specific vlan filter
  561. * @adapter: board private structure
  562. * @vlan: vlan tag
  563. *
  564. * Returns ptr to the filter object or NULL. Must be called while holding the
  565. * mac_vlan_list_lock.
  566. **/
  567. static struct
  568. i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
  569. {
  570. struct i40evf_vlan_filter *f;
  571. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  572. if (vlan == f->vlan)
  573. return f;
  574. }
  575. return NULL;
  576. }
  577. /**
  578. * i40evf_add_vlan - Add a vlan filter to the list
  579. * @adapter: board private structure
  580. * @vlan: VLAN tag
  581. *
  582. * Returns ptr to the filter object or NULL when no memory available.
  583. **/
  584. static struct
  585. i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
  586. {
  587. struct i40evf_vlan_filter *f = NULL;
  588. spin_lock_bh(&adapter->mac_vlan_list_lock);
  589. f = i40evf_find_vlan(adapter, vlan);
  590. if (!f) {
  591. f = kzalloc(sizeof(*f), GFP_KERNEL);
  592. if (!f)
  593. goto clearout;
  594. f->vlan = vlan;
  595. INIT_LIST_HEAD(&f->list);
  596. list_add(&f->list, &adapter->vlan_filter_list);
  597. f->add = true;
  598. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  599. }
  600. clearout:
  601. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  602. return f;
  603. }
  604. /**
  605. * i40evf_del_vlan - Remove a vlan filter from the list
  606. * @adapter: board private structure
  607. * @vlan: VLAN tag
  608. **/
  609. static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
  610. {
  611. struct i40evf_vlan_filter *f;
  612. spin_lock_bh(&adapter->mac_vlan_list_lock);
  613. f = i40evf_find_vlan(adapter, vlan);
  614. if (f) {
  615. f->remove = true;
  616. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  617. }
  618. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  619. }
  620. /**
  621. * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
  622. * @netdev: network device struct
  623. * @proto: unused protocol data
  624. * @vid: VLAN tag
  625. **/
  626. static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
  627. __always_unused __be16 proto, u16 vid)
  628. {
  629. struct i40evf_adapter *adapter = netdev_priv(netdev);
  630. if (!VLAN_ALLOWED(adapter))
  631. return -EIO;
  632. if (i40evf_add_vlan(adapter, vid) == NULL)
  633. return -ENOMEM;
  634. return 0;
  635. }
  636. /**
  637. * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
  638. * @netdev: network device struct
  639. * @proto: unused protocol data
  640. * @vid: VLAN tag
  641. **/
  642. static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
  643. __always_unused __be16 proto, u16 vid)
  644. {
  645. struct i40evf_adapter *adapter = netdev_priv(netdev);
  646. if (VLAN_ALLOWED(adapter)) {
  647. i40evf_del_vlan(adapter, vid);
  648. return 0;
  649. }
  650. return -EIO;
  651. }
  652. /**
  653. * i40evf_find_filter - Search filter list for specific mac filter
  654. * @adapter: board private structure
  655. * @macaddr: the MAC address
  656. *
  657. * Returns ptr to the filter object or NULL. Must be called while holding the
  658. * mac_vlan_list_lock.
  659. **/
  660. static struct
  661. i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
  662. const u8 *macaddr)
  663. {
  664. struct i40evf_mac_filter *f;
  665. if (!macaddr)
  666. return NULL;
  667. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  668. if (ether_addr_equal(macaddr, f->macaddr))
  669. return f;
  670. }
  671. return NULL;
  672. }
  673. /**
  674. * i40e_add_filter - Add a mac filter to the filter list
  675. * @adapter: board private structure
  676. * @macaddr: the MAC address
  677. *
  678. * Returns ptr to the filter object or NULL when no memory available.
  679. **/
  680. static struct
  681. i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
  682. const u8 *macaddr)
  683. {
  684. struct i40evf_mac_filter *f;
  685. if (!macaddr)
  686. return NULL;
  687. f = i40evf_find_filter(adapter, macaddr);
  688. if (!f) {
  689. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  690. if (!f)
  691. return f;
  692. ether_addr_copy(f->macaddr, macaddr);
  693. list_add_tail(&f->list, &adapter->mac_filter_list);
  694. f->add = true;
  695. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  696. } else {
  697. f->remove = false;
  698. }
  699. return f;
  700. }
  701. /**
  702. * i40evf_set_mac - NDO callback to set port mac address
  703. * @netdev: network interface device structure
  704. * @p: pointer to an address structure
  705. *
  706. * Returns 0 on success, negative on failure
  707. **/
  708. static int i40evf_set_mac(struct net_device *netdev, void *p)
  709. {
  710. struct i40evf_adapter *adapter = netdev_priv(netdev);
  711. struct i40e_hw *hw = &adapter->hw;
  712. struct i40evf_mac_filter *f;
  713. struct sockaddr *addr = p;
  714. if (!is_valid_ether_addr(addr->sa_data))
  715. return -EADDRNOTAVAIL;
  716. if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
  717. return 0;
  718. if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
  719. return -EPERM;
  720. spin_lock_bh(&adapter->mac_vlan_list_lock);
  721. f = i40evf_find_filter(adapter, hw->mac.addr);
  722. if (f) {
  723. f->remove = true;
  724. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  725. }
  726. f = i40evf_add_filter(adapter, addr->sa_data);
  727. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  728. if (f) {
  729. ether_addr_copy(hw->mac.addr, addr->sa_data);
  730. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  731. }
  732. return (f == NULL) ? -ENOMEM : 0;
  733. }
  734. /**
  735. * i40evf_addr_sync - Callback for dev_(mc|uc)_sync to add address
  736. * @netdev: the netdevice
  737. * @addr: address to add
  738. *
  739. * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
  740. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  741. */
  742. static int i40evf_addr_sync(struct net_device *netdev, const u8 *addr)
  743. {
  744. struct i40evf_adapter *adapter = netdev_priv(netdev);
  745. if (i40evf_add_filter(adapter, addr))
  746. return 0;
  747. else
  748. return -ENOMEM;
  749. }
  750. /**
  751. * i40evf_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
  752. * @netdev: the netdevice
  753. * @addr: address to add
  754. *
  755. * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
  756. * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
  757. */
  758. static int i40evf_addr_unsync(struct net_device *netdev, const u8 *addr)
  759. {
  760. struct i40evf_adapter *adapter = netdev_priv(netdev);
  761. struct i40evf_mac_filter *f;
  762. /* Under some circumstances, we might receive a request to delete
  763. * our own device address from our uc list. Because we store the
  764. * device address in the VSI's MAC/VLAN filter list, we need to ignore
  765. * such requests and not delete our device address from this list.
  766. */
  767. if (ether_addr_equal(addr, netdev->dev_addr))
  768. return 0;
  769. f = i40evf_find_filter(adapter, addr);
  770. if (f) {
  771. f->remove = true;
  772. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  773. }
  774. return 0;
  775. }
  776. /**
  777. * i40evf_set_rx_mode - NDO callback to set the netdev filters
  778. * @netdev: network interface device structure
  779. **/
  780. static void i40evf_set_rx_mode(struct net_device *netdev)
  781. {
  782. struct i40evf_adapter *adapter = netdev_priv(netdev);
  783. spin_lock_bh(&adapter->mac_vlan_list_lock);
  784. __dev_uc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
  785. __dev_mc_sync(netdev, i40evf_addr_sync, i40evf_addr_unsync);
  786. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  787. if (netdev->flags & IFF_PROMISC &&
  788. !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
  789. adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
  790. else if (!(netdev->flags & IFF_PROMISC) &&
  791. adapter->flags & I40EVF_FLAG_PROMISC_ON)
  792. adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
  793. if (netdev->flags & IFF_ALLMULTI &&
  794. !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
  795. adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
  796. else if (!(netdev->flags & IFF_ALLMULTI) &&
  797. adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
  798. adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
  799. }
  800. /**
  801. * i40evf_napi_enable_all - enable NAPI on all queue vectors
  802. * @adapter: board private structure
  803. **/
  804. static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
  805. {
  806. int q_idx;
  807. struct i40e_q_vector *q_vector;
  808. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  809. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  810. struct napi_struct *napi;
  811. q_vector = &adapter->q_vectors[q_idx];
  812. napi = &q_vector->napi;
  813. napi_enable(napi);
  814. }
  815. }
  816. /**
  817. * i40evf_napi_disable_all - disable NAPI on all queue vectors
  818. * @adapter: board private structure
  819. **/
  820. static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
  821. {
  822. int q_idx;
  823. struct i40e_q_vector *q_vector;
  824. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  825. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  826. q_vector = &adapter->q_vectors[q_idx];
  827. napi_disable(&q_vector->napi);
  828. }
  829. }
  830. /**
  831. * i40evf_configure - set up transmit and receive data structures
  832. * @adapter: board private structure
  833. **/
  834. static void i40evf_configure(struct i40evf_adapter *adapter)
  835. {
  836. struct net_device *netdev = adapter->netdev;
  837. int i;
  838. i40evf_set_rx_mode(netdev);
  839. i40evf_configure_tx(adapter);
  840. i40evf_configure_rx(adapter);
  841. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
  842. for (i = 0; i < adapter->num_active_queues; i++) {
  843. struct i40e_ring *ring = &adapter->rx_rings[i];
  844. i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  845. }
  846. }
  847. /**
  848. * i40evf_up_complete - Finish the last steps of bringing up a connection
  849. * @adapter: board private structure
  850. *
  851. * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
  852. **/
  853. static void i40evf_up_complete(struct i40evf_adapter *adapter)
  854. {
  855. adapter->state = __I40EVF_RUNNING;
  856. clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  857. i40evf_napi_enable_all(adapter);
  858. adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
  859. if (CLIENT_ENABLED(adapter))
  860. adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
  861. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  862. }
  863. /**
  864. * i40e_down - Shutdown the connection processing
  865. * @adapter: board private structure
  866. *
  867. * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
  868. **/
  869. void i40evf_down(struct i40evf_adapter *adapter)
  870. {
  871. struct net_device *netdev = adapter->netdev;
  872. struct i40evf_vlan_filter *vlf;
  873. struct i40evf_mac_filter *f;
  874. struct i40evf_cloud_filter *cf;
  875. if (adapter->state <= __I40EVF_DOWN_PENDING)
  876. return;
  877. netif_carrier_off(netdev);
  878. netif_tx_disable(netdev);
  879. adapter->link_up = false;
  880. i40evf_napi_disable_all(adapter);
  881. i40evf_irq_disable(adapter);
  882. spin_lock_bh(&adapter->mac_vlan_list_lock);
  883. /* clear the sync flag on all filters */
  884. __dev_uc_unsync(adapter->netdev, NULL);
  885. __dev_mc_unsync(adapter->netdev, NULL);
  886. /* remove all MAC filters */
  887. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  888. f->remove = true;
  889. }
  890. /* remove all VLAN filters */
  891. list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
  892. vlf->remove = true;
  893. }
  894. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  895. /* remove all cloud filters */
  896. spin_lock_bh(&adapter->cloud_filter_list_lock);
  897. list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
  898. cf->del = true;
  899. }
  900. spin_unlock_bh(&adapter->cloud_filter_list_lock);
  901. if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
  902. adapter->state != __I40EVF_RESETTING) {
  903. /* cancel any current operation */
  904. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  905. /* Schedule operations to close down the HW. Don't wait
  906. * here for this to complete. The watchdog is still running
  907. * and it will take care of this.
  908. */
  909. adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  910. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  911. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_CLOUD_FILTER;
  912. adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
  913. }
  914. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  915. }
  916. /**
  917. * i40evf_acquire_msix_vectors - Setup the MSIX capability
  918. * @adapter: board private structure
  919. * @vectors: number of vectors to request
  920. *
  921. * Work with the OS to set up the MSIX vectors needed.
  922. *
  923. * Returns 0 on success, negative on failure
  924. **/
  925. static int
  926. i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
  927. {
  928. int err, vector_threshold;
  929. /* We'll want at least 3 (vector_threshold):
  930. * 0) Other (Admin Queue and link, mostly)
  931. * 1) TxQ[0] Cleanup
  932. * 2) RxQ[0] Cleanup
  933. */
  934. vector_threshold = MIN_MSIX_COUNT;
  935. /* The more we get, the more we will assign to Tx/Rx Cleanup
  936. * for the separate queues...where Rx Cleanup >= Tx Cleanup.
  937. * Right now, we simply care about how many we'll get; we'll
  938. * set them up later while requesting irq's.
  939. */
  940. err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
  941. vector_threshold, vectors);
  942. if (err < 0) {
  943. dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
  944. kfree(adapter->msix_entries);
  945. adapter->msix_entries = NULL;
  946. return err;
  947. }
  948. /* Adjust for only the vectors we'll use, which is minimum
  949. * of max_msix_q_vectors + NONQ_VECS, or the number of
  950. * vectors we were allocated.
  951. */
  952. adapter->num_msix_vectors = err;
  953. return 0;
  954. }
  955. /**
  956. * i40evf_free_queues - Free memory for all rings
  957. * @adapter: board private structure to initialize
  958. *
  959. * Free all of the memory associated with queue pairs.
  960. **/
  961. static void i40evf_free_queues(struct i40evf_adapter *adapter)
  962. {
  963. if (!adapter->vsi_res)
  964. return;
  965. adapter->num_active_queues = 0;
  966. kfree(adapter->tx_rings);
  967. adapter->tx_rings = NULL;
  968. kfree(adapter->rx_rings);
  969. adapter->rx_rings = NULL;
  970. }
  971. /**
  972. * i40evf_alloc_queues - Allocate memory for all rings
  973. * @adapter: board private structure to initialize
  974. *
  975. * We allocate one ring per queue at run-time since we don't know the
  976. * number of queues at compile-time. The polling_netdev array is
  977. * intended for Multiqueue, but should work fine with a single queue.
  978. **/
  979. static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
  980. {
  981. int i, num_active_queues;
  982. /* If we're in reset reallocating queues we don't actually know yet for
  983. * certain the PF gave us the number of queues we asked for but we'll
  984. * assume it did. Once basic reset is finished we'll confirm once we
  985. * start negotiating config with PF.
  986. */
  987. if (adapter->num_req_queues)
  988. num_active_queues = adapter->num_req_queues;
  989. else if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
  990. adapter->num_tc)
  991. num_active_queues = adapter->ch_config.total_qps;
  992. else
  993. num_active_queues = min_t(int,
  994. adapter->vsi_res->num_queue_pairs,
  995. (int)(num_online_cpus()));
  996. adapter->tx_rings = kcalloc(num_active_queues,
  997. sizeof(struct i40e_ring), GFP_KERNEL);
  998. if (!adapter->tx_rings)
  999. goto err_out;
  1000. adapter->rx_rings = kcalloc(num_active_queues,
  1001. sizeof(struct i40e_ring), GFP_KERNEL);
  1002. if (!adapter->rx_rings)
  1003. goto err_out;
  1004. for (i = 0; i < num_active_queues; i++) {
  1005. struct i40e_ring *tx_ring;
  1006. struct i40e_ring *rx_ring;
  1007. tx_ring = &adapter->tx_rings[i];
  1008. tx_ring->queue_index = i;
  1009. tx_ring->netdev = adapter->netdev;
  1010. tx_ring->dev = &adapter->pdev->dev;
  1011. tx_ring->count = adapter->tx_desc_count;
  1012. tx_ring->itr_setting = I40E_ITR_TX_DEF;
  1013. if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
  1014. tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
  1015. rx_ring = &adapter->rx_rings[i];
  1016. rx_ring->queue_index = i;
  1017. rx_ring->netdev = adapter->netdev;
  1018. rx_ring->dev = &adapter->pdev->dev;
  1019. rx_ring->count = adapter->rx_desc_count;
  1020. rx_ring->itr_setting = I40E_ITR_RX_DEF;
  1021. }
  1022. adapter->num_active_queues = num_active_queues;
  1023. return 0;
  1024. err_out:
  1025. i40evf_free_queues(adapter);
  1026. return -ENOMEM;
  1027. }
  1028. /**
  1029. * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
  1030. * @adapter: board private structure to initialize
  1031. *
  1032. * Attempt to configure the interrupts using the best available
  1033. * capabilities of the hardware and the kernel.
  1034. **/
  1035. static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
  1036. {
  1037. int vector, v_budget;
  1038. int pairs = 0;
  1039. int err = 0;
  1040. if (!adapter->vsi_res) {
  1041. err = -EIO;
  1042. goto out;
  1043. }
  1044. pairs = adapter->num_active_queues;
  1045. /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
  1046. * us much good if we have more vectors than CPUs. However, we already
  1047. * limit the total number of queues by the number of CPUs so we do not
  1048. * need any further limiting here.
  1049. */
  1050. v_budget = min_t(int, pairs + NONQ_VECS,
  1051. (int)adapter->vf_res->max_vectors);
  1052. adapter->msix_entries = kcalloc(v_budget,
  1053. sizeof(struct msix_entry), GFP_KERNEL);
  1054. if (!adapter->msix_entries) {
  1055. err = -ENOMEM;
  1056. goto out;
  1057. }
  1058. for (vector = 0; vector < v_budget; vector++)
  1059. adapter->msix_entries[vector].entry = vector;
  1060. err = i40evf_acquire_msix_vectors(adapter, v_budget);
  1061. out:
  1062. netif_set_real_num_rx_queues(adapter->netdev, pairs);
  1063. netif_set_real_num_tx_queues(adapter->netdev, pairs);
  1064. return err;
  1065. }
  1066. /**
  1067. * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
  1068. * @adapter: board private structure
  1069. *
  1070. * Return 0 on success, negative on failure
  1071. **/
  1072. static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
  1073. {
  1074. struct i40e_aqc_get_set_rss_key_data *rss_key =
  1075. (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
  1076. struct i40e_hw *hw = &adapter->hw;
  1077. int ret = 0;
  1078. if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
  1079. /* bail because we already have a command pending */
  1080. dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
  1081. adapter->current_op);
  1082. return -EBUSY;
  1083. }
  1084. ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
  1085. if (ret) {
  1086. dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
  1087. i40evf_stat_str(hw, ret),
  1088. i40evf_aq_str(hw, hw->aq.asq_last_status));
  1089. return ret;
  1090. }
  1091. ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
  1092. adapter->rss_lut, adapter->rss_lut_size);
  1093. if (ret) {
  1094. dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
  1095. i40evf_stat_str(hw, ret),
  1096. i40evf_aq_str(hw, hw->aq.asq_last_status));
  1097. }
  1098. return ret;
  1099. }
  1100. /**
  1101. * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
  1102. * @adapter: board private structure
  1103. *
  1104. * Returns 0 on success, negative on failure
  1105. **/
  1106. static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
  1107. {
  1108. struct i40e_hw *hw = &adapter->hw;
  1109. u32 *dw;
  1110. u16 i;
  1111. dw = (u32 *)adapter->rss_key;
  1112. for (i = 0; i <= adapter->rss_key_size / 4; i++)
  1113. wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
  1114. dw = (u32 *)adapter->rss_lut;
  1115. for (i = 0; i <= adapter->rss_lut_size / 4; i++)
  1116. wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
  1117. i40e_flush(hw);
  1118. return 0;
  1119. }
  1120. /**
  1121. * i40evf_config_rss - Configure RSS keys and lut
  1122. * @adapter: board private structure
  1123. *
  1124. * Returns 0 on success, negative on failure
  1125. **/
  1126. int i40evf_config_rss(struct i40evf_adapter *adapter)
  1127. {
  1128. if (RSS_PF(adapter)) {
  1129. adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
  1130. I40EVF_FLAG_AQ_SET_RSS_KEY;
  1131. return 0;
  1132. } else if (RSS_AQ(adapter)) {
  1133. return i40evf_config_rss_aq(adapter);
  1134. } else {
  1135. return i40evf_config_rss_reg(adapter);
  1136. }
  1137. }
  1138. /**
  1139. * i40evf_fill_rss_lut - Fill the lut with default values
  1140. * @adapter: board private structure
  1141. **/
  1142. static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
  1143. {
  1144. u16 i;
  1145. for (i = 0; i < adapter->rss_lut_size; i++)
  1146. adapter->rss_lut[i] = i % adapter->num_active_queues;
  1147. }
  1148. /**
  1149. * i40evf_init_rss - Prepare for RSS
  1150. * @adapter: board private structure
  1151. *
  1152. * Return 0 on success, negative on failure
  1153. **/
  1154. static int i40evf_init_rss(struct i40evf_adapter *adapter)
  1155. {
  1156. struct i40e_hw *hw = &adapter->hw;
  1157. int ret;
  1158. if (!RSS_PF(adapter)) {
  1159. /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
  1160. if (adapter->vf_res->vf_cap_flags &
  1161. VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1162. adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
  1163. else
  1164. adapter->hena = I40E_DEFAULT_RSS_HENA;
  1165. wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
  1166. wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
  1167. }
  1168. i40evf_fill_rss_lut(adapter);
  1169. netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
  1170. ret = i40evf_config_rss(adapter);
  1171. return ret;
  1172. }
  1173. /**
  1174. * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
  1175. * @adapter: board private structure to initialize
  1176. *
  1177. * We allocate one q_vector per queue interrupt. If allocation fails we
  1178. * return -ENOMEM.
  1179. **/
  1180. static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
  1181. {
  1182. int q_idx = 0, num_q_vectors;
  1183. struct i40e_q_vector *q_vector;
  1184. num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  1185. adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
  1186. GFP_KERNEL);
  1187. if (!adapter->q_vectors)
  1188. return -ENOMEM;
  1189. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1190. q_vector = &adapter->q_vectors[q_idx];
  1191. q_vector->adapter = adapter;
  1192. q_vector->vsi = &adapter->vsi;
  1193. q_vector->v_idx = q_idx;
  1194. q_vector->reg_idx = q_idx;
  1195. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  1196. netif_napi_add(adapter->netdev, &q_vector->napi,
  1197. i40evf_napi_poll, NAPI_POLL_WEIGHT);
  1198. }
  1199. return 0;
  1200. }
  1201. /**
  1202. * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
  1203. * @adapter: board private structure to initialize
  1204. *
  1205. * This function frees the memory allocated to the q_vectors. In addition if
  1206. * NAPI is enabled it will delete any references to the NAPI struct prior
  1207. * to freeing the q_vector.
  1208. **/
  1209. static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
  1210. {
  1211. int q_idx, num_q_vectors;
  1212. int napi_vectors;
  1213. if (!adapter->q_vectors)
  1214. return;
  1215. num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  1216. napi_vectors = adapter->num_active_queues;
  1217. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1218. struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
  1219. if (q_idx < napi_vectors)
  1220. netif_napi_del(&q_vector->napi);
  1221. }
  1222. kfree(adapter->q_vectors);
  1223. adapter->q_vectors = NULL;
  1224. }
  1225. /**
  1226. * i40evf_reset_interrupt_capability - Reset MSIX setup
  1227. * @adapter: board private structure
  1228. *
  1229. **/
  1230. void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
  1231. {
  1232. if (!adapter->msix_entries)
  1233. return;
  1234. pci_disable_msix(adapter->pdev);
  1235. kfree(adapter->msix_entries);
  1236. adapter->msix_entries = NULL;
  1237. }
  1238. /**
  1239. * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
  1240. * @adapter: board private structure to initialize
  1241. *
  1242. **/
  1243. int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
  1244. {
  1245. int err;
  1246. err = i40evf_alloc_queues(adapter);
  1247. if (err) {
  1248. dev_err(&adapter->pdev->dev,
  1249. "Unable to allocate memory for queues\n");
  1250. goto err_alloc_queues;
  1251. }
  1252. rtnl_lock();
  1253. err = i40evf_set_interrupt_capability(adapter);
  1254. rtnl_unlock();
  1255. if (err) {
  1256. dev_err(&adapter->pdev->dev,
  1257. "Unable to setup interrupt capabilities\n");
  1258. goto err_set_interrupt;
  1259. }
  1260. err = i40evf_alloc_q_vectors(adapter);
  1261. if (err) {
  1262. dev_err(&adapter->pdev->dev,
  1263. "Unable to allocate memory for queue vectors\n");
  1264. goto err_alloc_q_vectors;
  1265. }
  1266. /* If we've made it so far while ADq flag being ON, then we haven't
  1267. * bailed out anywhere in middle. And ADq isn't just enabled but actual
  1268. * resources have been allocated in the reset path.
  1269. * Now we can truly claim that ADq is enabled.
  1270. */
  1271. if ((adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
  1272. adapter->num_tc)
  1273. dev_info(&adapter->pdev->dev, "ADq Enabled, %u TCs created",
  1274. adapter->num_tc);
  1275. dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
  1276. (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
  1277. adapter->num_active_queues);
  1278. return 0;
  1279. err_alloc_q_vectors:
  1280. i40evf_reset_interrupt_capability(adapter);
  1281. err_set_interrupt:
  1282. i40evf_free_queues(adapter);
  1283. err_alloc_queues:
  1284. return err;
  1285. }
  1286. /**
  1287. * i40evf_free_rss - Free memory used by RSS structs
  1288. * @adapter: board private structure
  1289. **/
  1290. static void i40evf_free_rss(struct i40evf_adapter *adapter)
  1291. {
  1292. kfree(adapter->rss_key);
  1293. adapter->rss_key = NULL;
  1294. kfree(adapter->rss_lut);
  1295. adapter->rss_lut = NULL;
  1296. }
  1297. /**
  1298. * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
  1299. * @adapter: board private structure
  1300. *
  1301. * Returns 0 on success, negative on failure
  1302. **/
  1303. static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
  1304. {
  1305. struct net_device *netdev = adapter->netdev;
  1306. int err;
  1307. if (netif_running(netdev))
  1308. i40evf_free_traffic_irqs(adapter);
  1309. i40evf_free_misc_irq(adapter);
  1310. i40evf_reset_interrupt_capability(adapter);
  1311. i40evf_free_q_vectors(adapter);
  1312. i40evf_free_queues(adapter);
  1313. err = i40evf_init_interrupt_scheme(adapter);
  1314. if (err)
  1315. goto err;
  1316. netif_tx_stop_all_queues(netdev);
  1317. err = i40evf_request_misc_irq(adapter);
  1318. if (err)
  1319. goto err;
  1320. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  1321. i40evf_map_rings_to_vectors(adapter);
  1322. if (RSS_AQ(adapter))
  1323. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
  1324. else
  1325. err = i40evf_init_rss(adapter);
  1326. err:
  1327. return err;
  1328. }
  1329. /**
  1330. * i40evf_watchdog_timer - Periodic call-back timer
  1331. * @data: pointer to adapter disguised as unsigned long
  1332. **/
  1333. static void i40evf_watchdog_timer(struct timer_list *t)
  1334. {
  1335. struct i40evf_adapter *adapter = from_timer(adapter, t,
  1336. watchdog_timer);
  1337. schedule_work(&adapter->watchdog_task);
  1338. /* timer will be rescheduled in watchdog task */
  1339. }
  1340. /**
  1341. * i40evf_watchdog_task - Periodic call-back task
  1342. * @work: pointer to work_struct
  1343. **/
  1344. static void i40evf_watchdog_task(struct work_struct *work)
  1345. {
  1346. struct i40evf_adapter *adapter = container_of(work,
  1347. struct i40evf_adapter,
  1348. watchdog_task);
  1349. struct i40e_hw *hw = &adapter->hw;
  1350. u32 reg_val;
  1351. if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
  1352. goto restart_watchdog;
  1353. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
  1354. reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
  1355. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1356. if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
  1357. (reg_val == VIRTCHNL_VFR_COMPLETED)) {
  1358. /* A chance for redemption! */
  1359. dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
  1360. adapter->state = __I40EVF_STARTUP;
  1361. adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
  1362. schedule_delayed_work(&adapter->init_task, 10);
  1363. clear_bit(__I40EVF_IN_CRITICAL_TASK,
  1364. &adapter->crit_section);
  1365. /* Don't reschedule the watchdog, since we've restarted
  1366. * the init task. When init_task contacts the PF and
  1367. * gets everything set up again, it'll restart the
  1368. * watchdog for us. Down, boy. Sit. Stay. Woof.
  1369. */
  1370. return;
  1371. }
  1372. adapter->aq_required = 0;
  1373. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1374. goto watchdog_done;
  1375. }
  1376. if ((adapter->state < __I40EVF_DOWN) ||
  1377. (adapter->flags & I40EVF_FLAG_RESET_PENDING))
  1378. goto watchdog_done;
  1379. /* check for reset */
  1380. reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
  1381. if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
  1382. adapter->state = __I40EVF_RESETTING;
  1383. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  1384. dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
  1385. schedule_work(&adapter->reset_task);
  1386. adapter->aq_required = 0;
  1387. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1388. goto watchdog_done;
  1389. }
  1390. /* Process admin queue tasks. After init, everything gets done
  1391. * here so we don't race on the admin queue.
  1392. */
  1393. if (adapter->current_op) {
  1394. if (!i40evf_asq_done(hw)) {
  1395. dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
  1396. i40evf_send_api_ver(adapter);
  1397. }
  1398. goto watchdog_done;
  1399. }
  1400. if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
  1401. i40evf_send_vf_config_msg(adapter);
  1402. goto watchdog_done;
  1403. }
  1404. if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
  1405. i40evf_disable_queues(adapter);
  1406. goto watchdog_done;
  1407. }
  1408. if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
  1409. i40evf_map_queues(adapter);
  1410. goto watchdog_done;
  1411. }
  1412. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
  1413. i40evf_add_ether_addrs(adapter);
  1414. goto watchdog_done;
  1415. }
  1416. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
  1417. i40evf_add_vlans(adapter);
  1418. goto watchdog_done;
  1419. }
  1420. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
  1421. i40evf_del_ether_addrs(adapter);
  1422. goto watchdog_done;
  1423. }
  1424. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
  1425. i40evf_del_vlans(adapter);
  1426. goto watchdog_done;
  1427. }
  1428. if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
  1429. i40evf_enable_vlan_stripping(adapter);
  1430. goto watchdog_done;
  1431. }
  1432. if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
  1433. i40evf_disable_vlan_stripping(adapter);
  1434. goto watchdog_done;
  1435. }
  1436. if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
  1437. i40evf_configure_queues(adapter);
  1438. goto watchdog_done;
  1439. }
  1440. if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
  1441. i40evf_enable_queues(adapter);
  1442. goto watchdog_done;
  1443. }
  1444. if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
  1445. /* This message goes straight to the firmware, not the
  1446. * PF, so we don't have to set current_op as we will
  1447. * not get a response through the ARQ.
  1448. */
  1449. i40evf_init_rss(adapter);
  1450. adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
  1451. goto watchdog_done;
  1452. }
  1453. if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
  1454. i40evf_get_hena(adapter);
  1455. goto watchdog_done;
  1456. }
  1457. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
  1458. i40evf_set_hena(adapter);
  1459. goto watchdog_done;
  1460. }
  1461. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
  1462. i40evf_set_rss_key(adapter);
  1463. goto watchdog_done;
  1464. }
  1465. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
  1466. i40evf_set_rss_lut(adapter);
  1467. goto watchdog_done;
  1468. }
  1469. if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
  1470. i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
  1471. FLAG_VF_MULTICAST_PROMISC);
  1472. goto watchdog_done;
  1473. }
  1474. if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
  1475. i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
  1476. goto watchdog_done;
  1477. }
  1478. if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
  1479. (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
  1480. i40evf_set_promiscuous(adapter, 0);
  1481. goto watchdog_done;
  1482. }
  1483. if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_CHANNELS) {
  1484. i40evf_enable_channels(adapter);
  1485. goto watchdog_done;
  1486. }
  1487. if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_CHANNELS) {
  1488. i40evf_disable_channels(adapter);
  1489. goto watchdog_done;
  1490. }
  1491. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_CLOUD_FILTER) {
  1492. i40evf_add_cloud_filter(adapter);
  1493. goto watchdog_done;
  1494. }
  1495. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_CLOUD_FILTER) {
  1496. i40evf_del_cloud_filter(adapter);
  1497. goto watchdog_done;
  1498. }
  1499. schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
  1500. if (adapter->state == __I40EVF_RUNNING)
  1501. i40evf_request_stats(adapter);
  1502. watchdog_done:
  1503. if (adapter->state == __I40EVF_RUNNING)
  1504. i40evf_detect_recover_hung(&adapter->vsi);
  1505. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1506. restart_watchdog:
  1507. if (adapter->state == __I40EVF_REMOVE)
  1508. return;
  1509. if (adapter->aq_required)
  1510. mod_timer(&adapter->watchdog_timer,
  1511. jiffies + msecs_to_jiffies(20));
  1512. else
  1513. mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
  1514. schedule_work(&adapter->adminq_task);
  1515. }
  1516. static void i40evf_disable_vf(struct i40evf_adapter *adapter)
  1517. {
  1518. struct i40evf_mac_filter *f, *ftmp;
  1519. struct i40evf_vlan_filter *fv, *fvtmp;
  1520. struct i40evf_cloud_filter *cf, *cftmp;
  1521. adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
  1522. /* We don't use netif_running() because it may be true prior to
  1523. * ndo_open() returning, so we can't assume it means all our open
  1524. * tasks have finished, since we're not holding the rtnl_lock here.
  1525. */
  1526. if (adapter->state == __I40EVF_RUNNING) {
  1527. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  1528. netif_carrier_off(adapter->netdev);
  1529. netif_tx_disable(adapter->netdev);
  1530. adapter->link_up = false;
  1531. i40evf_napi_disable_all(adapter);
  1532. i40evf_irq_disable(adapter);
  1533. i40evf_free_traffic_irqs(adapter);
  1534. i40evf_free_all_tx_resources(adapter);
  1535. i40evf_free_all_rx_resources(adapter);
  1536. }
  1537. spin_lock_bh(&adapter->mac_vlan_list_lock);
  1538. /* Delete all of the filters */
  1539. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  1540. list_del(&f->list);
  1541. kfree(f);
  1542. }
  1543. list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
  1544. list_del(&fv->list);
  1545. kfree(fv);
  1546. }
  1547. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  1548. spin_lock_bh(&adapter->cloud_filter_list_lock);
  1549. list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
  1550. list_del(&cf->list);
  1551. kfree(cf);
  1552. adapter->num_cloud_filters--;
  1553. }
  1554. spin_unlock_bh(&adapter->cloud_filter_list_lock);
  1555. i40evf_free_misc_irq(adapter);
  1556. i40evf_reset_interrupt_capability(adapter);
  1557. i40evf_free_queues(adapter);
  1558. i40evf_free_q_vectors(adapter);
  1559. kfree(adapter->vf_res);
  1560. i40evf_shutdown_adminq(&adapter->hw);
  1561. adapter->netdev->flags &= ~IFF_UP;
  1562. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1563. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1564. adapter->state = __I40EVF_DOWN;
  1565. wake_up(&adapter->down_waitqueue);
  1566. dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
  1567. }
  1568. #define I40EVF_RESET_WAIT_MS 10
  1569. #define I40EVF_RESET_WAIT_COUNT 500
  1570. /**
  1571. * i40evf_reset_task - Call-back task to handle hardware reset
  1572. * @work: pointer to work_struct
  1573. *
  1574. * During reset we need to shut down and reinitialize the admin queue
  1575. * before we can use it to communicate with the PF again. We also clear
  1576. * and reinit the rings because that context is lost as well.
  1577. **/
  1578. static void i40evf_reset_task(struct work_struct *work)
  1579. {
  1580. struct i40evf_adapter *adapter = container_of(work,
  1581. struct i40evf_adapter,
  1582. reset_task);
  1583. struct virtchnl_vf_resource *vfres = adapter->vf_res;
  1584. struct net_device *netdev = adapter->netdev;
  1585. struct i40e_hw *hw = &adapter->hw;
  1586. struct i40evf_vlan_filter *vlf;
  1587. struct i40evf_cloud_filter *cf;
  1588. struct i40evf_mac_filter *f;
  1589. u32 reg_val;
  1590. int i = 0, err;
  1591. bool running;
  1592. /* When device is being removed it doesn't make sense to run the reset
  1593. * task, just return in such a case.
  1594. */
  1595. if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
  1596. return;
  1597. while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
  1598. &adapter->crit_section))
  1599. usleep_range(500, 1000);
  1600. if (CLIENT_ENABLED(adapter)) {
  1601. adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
  1602. I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
  1603. I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
  1604. I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
  1605. cancel_delayed_work_sync(&adapter->client_task);
  1606. i40evf_notify_client_close(&adapter->vsi, true);
  1607. }
  1608. i40evf_misc_irq_disable(adapter);
  1609. if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
  1610. adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
  1611. /* Restart the AQ here. If we have been reset but didn't
  1612. * detect it, or if the PF had to reinit, our AQ will be hosed.
  1613. */
  1614. i40evf_shutdown_adminq(hw);
  1615. i40evf_init_adminq(hw);
  1616. i40evf_request_reset(adapter);
  1617. }
  1618. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  1619. /* poll until we see the reset actually happen */
  1620. for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
  1621. reg_val = rd32(hw, I40E_VF_ARQLEN1) &
  1622. I40E_VF_ARQLEN1_ARQENABLE_MASK;
  1623. if (!reg_val)
  1624. break;
  1625. usleep_range(5000, 10000);
  1626. }
  1627. if (i == I40EVF_RESET_WAIT_COUNT) {
  1628. dev_info(&adapter->pdev->dev, "Never saw reset\n");
  1629. goto continue_reset; /* act like the reset happened */
  1630. }
  1631. /* wait until the reset is complete and the PF is responding to us */
  1632. for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
  1633. /* sleep first to make sure a minimum wait time is met */
  1634. msleep(I40EVF_RESET_WAIT_MS);
  1635. reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
  1636. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1637. if (reg_val == VIRTCHNL_VFR_VFACTIVE)
  1638. break;
  1639. }
  1640. pci_set_master(adapter->pdev);
  1641. if (i == I40EVF_RESET_WAIT_COUNT) {
  1642. dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
  1643. reg_val);
  1644. i40evf_disable_vf(adapter);
  1645. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1646. return; /* Do not attempt to reinit. It's dead, Jim. */
  1647. }
  1648. continue_reset:
  1649. /* We don't use netif_running() because it may be true prior to
  1650. * ndo_open() returning, so we can't assume it means all our open
  1651. * tasks have finished, since we're not holding the rtnl_lock here.
  1652. */
  1653. running = ((adapter->state == __I40EVF_RUNNING) ||
  1654. (adapter->state == __I40EVF_RESETTING));
  1655. if (running) {
  1656. netif_carrier_off(netdev);
  1657. netif_tx_stop_all_queues(netdev);
  1658. adapter->link_up = false;
  1659. i40evf_napi_disable_all(adapter);
  1660. }
  1661. i40evf_irq_disable(adapter);
  1662. adapter->state = __I40EVF_RESETTING;
  1663. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1664. /* free the Tx/Rx rings and descriptors, might be better to just
  1665. * re-use them sometime in the future
  1666. */
  1667. i40evf_free_all_rx_resources(adapter);
  1668. i40evf_free_all_tx_resources(adapter);
  1669. adapter->flags |= I40EVF_FLAG_QUEUES_DISABLED;
  1670. /* kill and reinit the admin queue */
  1671. i40evf_shutdown_adminq(hw);
  1672. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1673. err = i40evf_init_adminq(hw);
  1674. if (err)
  1675. dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
  1676. err);
  1677. adapter->aq_required = 0;
  1678. if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
  1679. err = i40evf_reinit_interrupt_scheme(adapter);
  1680. if (err)
  1681. goto reset_err;
  1682. }
  1683. adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
  1684. adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
  1685. spin_lock_bh(&adapter->mac_vlan_list_lock);
  1686. /* re-add all MAC filters */
  1687. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  1688. f->add = true;
  1689. }
  1690. /* re-add all VLAN filters */
  1691. list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
  1692. vlf->add = true;
  1693. }
  1694. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  1695. /* check if TCs are running and re-add all cloud filters */
  1696. spin_lock_bh(&adapter->cloud_filter_list_lock);
  1697. if ((vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ) &&
  1698. adapter->num_tc) {
  1699. list_for_each_entry(cf, &adapter->cloud_filter_list, list) {
  1700. cf->add = true;
  1701. }
  1702. }
  1703. spin_unlock_bh(&adapter->cloud_filter_list_lock);
  1704. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  1705. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  1706. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_CLOUD_FILTER;
  1707. i40evf_misc_irq_enable(adapter);
  1708. mod_timer(&adapter->watchdog_timer, jiffies + 2);
  1709. /* We were running when the reset started, so we need to restore some
  1710. * state here.
  1711. */
  1712. if (running) {
  1713. /* allocate transmit descriptors */
  1714. err = i40evf_setup_all_tx_resources(adapter);
  1715. if (err)
  1716. goto reset_err;
  1717. /* allocate receive descriptors */
  1718. err = i40evf_setup_all_rx_resources(adapter);
  1719. if (err)
  1720. goto reset_err;
  1721. if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
  1722. err = i40evf_request_traffic_irqs(adapter,
  1723. netdev->name);
  1724. if (err)
  1725. goto reset_err;
  1726. adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
  1727. }
  1728. i40evf_configure(adapter);
  1729. i40evf_up_complete(adapter);
  1730. i40evf_irq_enable(adapter, true);
  1731. } else {
  1732. adapter->state = __I40EVF_DOWN;
  1733. wake_up(&adapter->down_waitqueue);
  1734. }
  1735. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1736. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1737. return;
  1738. reset_err:
  1739. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1740. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1741. dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
  1742. i40evf_close(netdev);
  1743. }
  1744. /**
  1745. * i40evf_adminq_task - worker thread to clean the admin queue
  1746. * @work: pointer to work_struct containing our data
  1747. **/
  1748. static void i40evf_adminq_task(struct work_struct *work)
  1749. {
  1750. struct i40evf_adapter *adapter =
  1751. container_of(work, struct i40evf_adapter, adminq_task);
  1752. struct i40e_hw *hw = &adapter->hw;
  1753. struct i40e_arq_event_info event;
  1754. enum virtchnl_ops v_op;
  1755. i40e_status ret, v_ret;
  1756. u32 val, oldval;
  1757. u16 pending;
  1758. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
  1759. goto out;
  1760. event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
  1761. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  1762. if (!event.msg_buf)
  1763. goto out;
  1764. do {
  1765. ret = i40evf_clean_arq_element(hw, &event, &pending);
  1766. v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
  1767. v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
  1768. if (ret || !v_op)
  1769. break; /* No event to process or error cleaning ARQ */
  1770. i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
  1771. event.msg_len);
  1772. if (pending != 0)
  1773. memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
  1774. } while (pending);
  1775. if ((adapter->flags &
  1776. (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
  1777. adapter->state == __I40EVF_RESETTING)
  1778. goto freedom;
  1779. /* check for error indications */
  1780. val = rd32(hw, hw->aq.arq.len);
  1781. if (val == 0xdeadbeef) /* indicates device in reset */
  1782. goto freedom;
  1783. oldval = val;
  1784. if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
  1785. dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
  1786. val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
  1787. }
  1788. if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
  1789. dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
  1790. val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
  1791. }
  1792. if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
  1793. dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
  1794. val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
  1795. }
  1796. if (oldval != val)
  1797. wr32(hw, hw->aq.arq.len, val);
  1798. val = rd32(hw, hw->aq.asq.len);
  1799. oldval = val;
  1800. if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
  1801. dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
  1802. val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
  1803. }
  1804. if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
  1805. dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
  1806. val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
  1807. }
  1808. if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
  1809. dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
  1810. val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
  1811. }
  1812. if (oldval != val)
  1813. wr32(hw, hw->aq.asq.len, val);
  1814. freedom:
  1815. kfree(event.msg_buf);
  1816. out:
  1817. /* re-enable Admin queue interrupt cause */
  1818. i40evf_misc_irq_enable(adapter);
  1819. }
  1820. /**
  1821. * i40evf_client_task - worker thread to perform client work
  1822. * @work: pointer to work_struct containing our data
  1823. *
  1824. * This task handles client interactions. Because client calls can be
  1825. * reentrant, we can't handle them in the watchdog.
  1826. **/
  1827. static void i40evf_client_task(struct work_struct *work)
  1828. {
  1829. struct i40evf_adapter *adapter =
  1830. container_of(work, struct i40evf_adapter, client_task.work);
  1831. /* If we can't get the client bit, just give up. We'll be rescheduled
  1832. * later.
  1833. */
  1834. if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
  1835. return;
  1836. if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
  1837. i40evf_client_subtask(adapter);
  1838. adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
  1839. goto out;
  1840. }
  1841. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
  1842. i40evf_notify_client_l2_params(&adapter->vsi);
  1843. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
  1844. goto out;
  1845. }
  1846. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
  1847. i40evf_notify_client_close(&adapter->vsi, false);
  1848. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
  1849. goto out;
  1850. }
  1851. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
  1852. i40evf_notify_client_open(&adapter->vsi);
  1853. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
  1854. }
  1855. out:
  1856. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1857. }
  1858. /**
  1859. * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
  1860. * @adapter: board private structure
  1861. *
  1862. * Free all transmit software resources
  1863. **/
  1864. void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
  1865. {
  1866. int i;
  1867. if (!adapter->tx_rings)
  1868. return;
  1869. for (i = 0; i < adapter->num_active_queues; i++)
  1870. if (adapter->tx_rings[i].desc)
  1871. i40evf_free_tx_resources(&adapter->tx_rings[i]);
  1872. }
  1873. /**
  1874. * i40evf_setup_all_tx_resources - allocate all queues Tx resources
  1875. * @adapter: board private structure
  1876. *
  1877. * If this function returns with an error, then it's possible one or
  1878. * more of the rings is populated (while the rest are not). It is the
  1879. * callers duty to clean those orphaned rings.
  1880. *
  1881. * Return 0 on success, negative on failure
  1882. **/
  1883. static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
  1884. {
  1885. int i, err = 0;
  1886. for (i = 0; i < adapter->num_active_queues; i++) {
  1887. adapter->tx_rings[i].count = adapter->tx_desc_count;
  1888. err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
  1889. if (!err)
  1890. continue;
  1891. dev_err(&adapter->pdev->dev,
  1892. "Allocation for Tx Queue %u failed\n", i);
  1893. break;
  1894. }
  1895. return err;
  1896. }
  1897. /**
  1898. * i40evf_setup_all_rx_resources - allocate all queues Rx resources
  1899. * @adapter: board private structure
  1900. *
  1901. * If this function returns with an error, then it's possible one or
  1902. * more of the rings is populated (while the rest are not). It is the
  1903. * callers duty to clean those orphaned rings.
  1904. *
  1905. * Return 0 on success, negative on failure
  1906. **/
  1907. static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
  1908. {
  1909. int i, err = 0;
  1910. for (i = 0; i < adapter->num_active_queues; i++) {
  1911. adapter->rx_rings[i].count = adapter->rx_desc_count;
  1912. err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
  1913. if (!err)
  1914. continue;
  1915. dev_err(&adapter->pdev->dev,
  1916. "Allocation for Rx Queue %u failed\n", i);
  1917. break;
  1918. }
  1919. return err;
  1920. }
  1921. /**
  1922. * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
  1923. * @adapter: board private structure
  1924. *
  1925. * Free all receive software resources
  1926. **/
  1927. void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
  1928. {
  1929. int i;
  1930. if (!adapter->rx_rings)
  1931. return;
  1932. for (i = 0; i < adapter->num_active_queues; i++)
  1933. if (adapter->rx_rings[i].desc)
  1934. i40evf_free_rx_resources(&adapter->rx_rings[i]);
  1935. }
  1936. /**
  1937. * i40evf_validate_tx_bandwidth - validate the max Tx bandwidth
  1938. * @adapter: board private structure
  1939. * @max_tx_rate: max Tx bw for a tc
  1940. **/
  1941. static int i40evf_validate_tx_bandwidth(struct i40evf_adapter *adapter,
  1942. u64 max_tx_rate)
  1943. {
  1944. int speed = 0, ret = 0;
  1945. switch (adapter->link_speed) {
  1946. case I40E_LINK_SPEED_40GB:
  1947. speed = 40000;
  1948. break;
  1949. case I40E_LINK_SPEED_25GB:
  1950. speed = 25000;
  1951. break;
  1952. case I40E_LINK_SPEED_20GB:
  1953. speed = 20000;
  1954. break;
  1955. case I40E_LINK_SPEED_10GB:
  1956. speed = 10000;
  1957. break;
  1958. case I40E_LINK_SPEED_1GB:
  1959. speed = 1000;
  1960. break;
  1961. case I40E_LINK_SPEED_100MB:
  1962. speed = 100;
  1963. break;
  1964. default:
  1965. break;
  1966. }
  1967. if (max_tx_rate > speed) {
  1968. dev_err(&adapter->pdev->dev,
  1969. "Invalid tx rate specified\n");
  1970. ret = -EINVAL;
  1971. }
  1972. return ret;
  1973. }
  1974. /**
  1975. * i40evf_validate_channel_config - validate queue mapping info
  1976. * @adapter: board private structure
  1977. * @mqprio_qopt: queue parameters
  1978. *
  1979. * This function validates if the config provided by the user to
  1980. * configure queue channels is valid or not. Returns 0 on a valid
  1981. * config.
  1982. **/
  1983. static int i40evf_validate_ch_config(struct i40evf_adapter *adapter,
  1984. struct tc_mqprio_qopt_offload *mqprio_qopt)
  1985. {
  1986. u64 total_max_rate = 0;
  1987. int i, num_qps = 0;
  1988. u64 tx_rate = 0;
  1989. int ret = 0;
  1990. if (mqprio_qopt->qopt.num_tc > I40EVF_MAX_TRAFFIC_CLASS ||
  1991. mqprio_qopt->qopt.num_tc < 1)
  1992. return -EINVAL;
  1993. for (i = 0; i <= mqprio_qopt->qopt.num_tc - 1; i++) {
  1994. if (!mqprio_qopt->qopt.count[i] ||
  1995. mqprio_qopt->qopt.offset[i] != num_qps)
  1996. return -EINVAL;
  1997. if (mqprio_qopt->min_rate[i]) {
  1998. dev_err(&adapter->pdev->dev,
  1999. "Invalid min tx rate (greater than 0) specified\n");
  2000. return -EINVAL;
  2001. }
  2002. /*convert to Mbps */
  2003. tx_rate = div_u64(mqprio_qopt->max_rate[i],
  2004. I40EVF_MBPS_DIVISOR);
  2005. total_max_rate += tx_rate;
  2006. num_qps += mqprio_qopt->qopt.count[i];
  2007. }
  2008. if (num_qps > I40EVF_MAX_REQ_QUEUES)
  2009. return -EINVAL;
  2010. ret = i40evf_validate_tx_bandwidth(adapter, total_max_rate);
  2011. return ret;
  2012. }
  2013. /**
  2014. * i40evf_del_all_cloud_filters - delete all cloud filters
  2015. * on the traffic classes
  2016. **/
  2017. static void i40evf_del_all_cloud_filters(struct i40evf_adapter *adapter)
  2018. {
  2019. struct i40evf_cloud_filter *cf, *cftmp;
  2020. spin_lock_bh(&adapter->cloud_filter_list_lock);
  2021. list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list,
  2022. list) {
  2023. list_del(&cf->list);
  2024. kfree(cf);
  2025. adapter->num_cloud_filters--;
  2026. }
  2027. spin_unlock_bh(&adapter->cloud_filter_list_lock);
  2028. }
  2029. /**
  2030. * __i40evf_setup_tc - configure multiple traffic classes
  2031. * @netdev: network interface device structure
  2032. * @type_date: tc offload data
  2033. *
  2034. * This function processes the config information provided by the
  2035. * user to configure traffic classes/queue channels and packages the
  2036. * information to request the PF to setup traffic classes.
  2037. *
  2038. * Returns 0 on success.
  2039. **/
  2040. static int __i40evf_setup_tc(struct net_device *netdev, void *type_data)
  2041. {
  2042. struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
  2043. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2044. struct virtchnl_vf_resource *vfres = adapter->vf_res;
  2045. u8 num_tc = 0, total_qps = 0;
  2046. int ret = 0, netdev_tc = 0;
  2047. u64 max_tx_rate;
  2048. u16 mode;
  2049. int i;
  2050. num_tc = mqprio_qopt->qopt.num_tc;
  2051. mode = mqprio_qopt->mode;
  2052. /* delete queue_channel */
  2053. if (!mqprio_qopt->qopt.hw) {
  2054. if (adapter->ch_config.state == __I40EVF_TC_RUNNING) {
  2055. /* reset the tc configuration */
  2056. netdev_reset_tc(netdev);
  2057. adapter->num_tc = 0;
  2058. netif_tx_stop_all_queues(netdev);
  2059. netif_tx_disable(netdev);
  2060. i40evf_del_all_cloud_filters(adapter);
  2061. adapter->aq_required = I40EVF_FLAG_AQ_DISABLE_CHANNELS;
  2062. goto exit;
  2063. } else {
  2064. return -EINVAL;
  2065. }
  2066. }
  2067. /* add queue channel */
  2068. if (mode == TC_MQPRIO_MODE_CHANNEL) {
  2069. if (!(vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)) {
  2070. dev_err(&adapter->pdev->dev, "ADq not supported\n");
  2071. return -EOPNOTSUPP;
  2072. }
  2073. if (adapter->ch_config.state != __I40EVF_TC_INVALID) {
  2074. dev_err(&adapter->pdev->dev, "TC configuration already exists\n");
  2075. return -EINVAL;
  2076. }
  2077. ret = i40evf_validate_ch_config(adapter, mqprio_qopt);
  2078. if (ret)
  2079. return ret;
  2080. /* Return if same TC config is requested */
  2081. if (adapter->num_tc == num_tc)
  2082. return 0;
  2083. adapter->num_tc = num_tc;
  2084. for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
  2085. if (i < num_tc) {
  2086. adapter->ch_config.ch_info[i].count =
  2087. mqprio_qopt->qopt.count[i];
  2088. adapter->ch_config.ch_info[i].offset =
  2089. mqprio_qopt->qopt.offset[i];
  2090. total_qps += mqprio_qopt->qopt.count[i];
  2091. max_tx_rate = mqprio_qopt->max_rate[i];
  2092. /* convert to Mbps */
  2093. max_tx_rate = div_u64(max_tx_rate,
  2094. I40EVF_MBPS_DIVISOR);
  2095. adapter->ch_config.ch_info[i].max_tx_rate =
  2096. max_tx_rate;
  2097. } else {
  2098. adapter->ch_config.ch_info[i].count = 1;
  2099. adapter->ch_config.ch_info[i].offset = 0;
  2100. }
  2101. }
  2102. adapter->ch_config.total_qps = total_qps;
  2103. netif_tx_stop_all_queues(netdev);
  2104. netif_tx_disable(netdev);
  2105. adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_CHANNELS;
  2106. netdev_reset_tc(netdev);
  2107. /* Report the tc mapping up the stack */
  2108. netdev_set_num_tc(adapter->netdev, num_tc);
  2109. for (i = 0; i < I40EVF_MAX_TRAFFIC_CLASS; i++) {
  2110. u16 qcount = mqprio_qopt->qopt.count[i];
  2111. u16 qoffset = mqprio_qopt->qopt.offset[i];
  2112. if (i < num_tc)
  2113. netdev_set_tc_queue(netdev, netdev_tc++, qcount,
  2114. qoffset);
  2115. }
  2116. }
  2117. exit:
  2118. return ret;
  2119. }
  2120. /**
  2121. * i40evf_parse_cls_flower - Parse tc flower filters provided by kernel
  2122. * @adapter: board private structure
  2123. * @cls_flower: pointer to struct tc_cls_flower_offload
  2124. * @filter: pointer to cloud filter structure
  2125. */
  2126. static int i40evf_parse_cls_flower(struct i40evf_adapter *adapter,
  2127. struct tc_cls_flower_offload *f,
  2128. struct i40evf_cloud_filter *filter)
  2129. {
  2130. u16 n_proto_mask = 0;
  2131. u16 n_proto_key = 0;
  2132. u8 field_flags = 0;
  2133. u16 addr_type = 0;
  2134. u16 n_proto = 0;
  2135. int i = 0;
  2136. struct virtchnl_filter *vf = &filter->f;
  2137. if (f->dissector->used_keys &
  2138. ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
  2139. BIT(FLOW_DISSECTOR_KEY_BASIC) |
  2140. BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
  2141. BIT(FLOW_DISSECTOR_KEY_VLAN) |
  2142. BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
  2143. BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
  2144. BIT(FLOW_DISSECTOR_KEY_PORTS) |
  2145. BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
  2146. dev_err(&adapter->pdev->dev, "Unsupported key used: 0x%x\n",
  2147. f->dissector->used_keys);
  2148. return -EOPNOTSUPP;
  2149. }
  2150. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
  2151. struct flow_dissector_key_keyid *mask =
  2152. skb_flow_dissector_target(f->dissector,
  2153. FLOW_DISSECTOR_KEY_ENC_KEYID,
  2154. f->mask);
  2155. if (mask->keyid != 0)
  2156. field_flags |= I40EVF_CLOUD_FIELD_TEN_ID;
  2157. }
  2158. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
  2159. struct flow_dissector_key_basic *key =
  2160. skb_flow_dissector_target(f->dissector,
  2161. FLOW_DISSECTOR_KEY_BASIC,
  2162. f->key);
  2163. struct flow_dissector_key_basic *mask =
  2164. skb_flow_dissector_target(f->dissector,
  2165. FLOW_DISSECTOR_KEY_BASIC,
  2166. f->mask);
  2167. n_proto_key = ntohs(key->n_proto);
  2168. n_proto_mask = ntohs(mask->n_proto);
  2169. if (n_proto_key == ETH_P_ALL) {
  2170. n_proto_key = 0;
  2171. n_proto_mask = 0;
  2172. }
  2173. n_proto = n_proto_key & n_proto_mask;
  2174. if (n_proto != ETH_P_IP && n_proto != ETH_P_IPV6)
  2175. return -EINVAL;
  2176. if (n_proto == ETH_P_IPV6) {
  2177. /* specify flow type as TCP IPv6 */
  2178. vf->flow_type = VIRTCHNL_TCP_V6_FLOW;
  2179. }
  2180. if (key->ip_proto != IPPROTO_TCP) {
  2181. dev_info(&adapter->pdev->dev, "Only TCP transport is supported\n");
  2182. return -EINVAL;
  2183. }
  2184. }
  2185. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
  2186. struct flow_dissector_key_eth_addrs *key =
  2187. skb_flow_dissector_target(f->dissector,
  2188. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  2189. f->key);
  2190. struct flow_dissector_key_eth_addrs *mask =
  2191. skb_flow_dissector_target(f->dissector,
  2192. FLOW_DISSECTOR_KEY_ETH_ADDRS,
  2193. f->mask);
  2194. /* use is_broadcast and is_zero to check for all 0xf or 0 */
  2195. if (!is_zero_ether_addr(mask->dst)) {
  2196. if (is_broadcast_ether_addr(mask->dst)) {
  2197. field_flags |= I40EVF_CLOUD_FIELD_OMAC;
  2198. } else {
  2199. dev_err(&adapter->pdev->dev, "Bad ether dest mask %pM\n",
  2200. mask->dst);
  2201. return I40E_ERR_CONFIG;
  2202. }
  2203. }
  2204. if (!is_zero_ether_addr(mask->src)) {
  2205. if (is_broadcast_ether_addr(mask->src)) {
  2206. field_flags |= I40EVF_CLOUD_FIELD_IMAC;
  2207. } else {
  2208. dev_err(&adapter->pdev->dev, "Bad ether src mask %pM\n",
  2209. mask->src);
  2210. return I40E_ERR_CONFIG;
  2211. }
  2212. }
  2213. if (!is_zero_ether_addr(key->dst))
  2214. if (is_valid_ether_addr(key->dst) ||
  2215. is_multicast_ether_addr(key->dst)) {
  2216. /* set the mask if a valid dst_mac address */
  2217. for (i = 0; i < ETH_ALEN; i++)
  2218. vf->mask.tcp_spec.dst_mac[i] |= 0xff;
  2219. ether_addr_copy(vf->data.tcp_spec.dst_mac,
  2220. key->dst);
  2221. }
  2222. if (!is_zero_ether_addr(key->src))
  2223. if (is_valid_ether_addr(key->src) ||
  2224. is_multicast_ether_addr(key->src)) {
  2225. /* set the mask if a valid dst_mac address */
  2226. for (i = 0; i < ETH_ALEN; i++)
  2227. vf->mask.tcp_spec.src_mac[i] |= 0xff;
  2228. ether_addr_copy(vf->data.tcp_spec.src_mac,
  2229. key->src);
  2230. }
  2231. }
  2232. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_VLAN)) {
  2233. struct flow_dissector_key_vlan *key =
  2234. skb_flow_dissector_target(f->dissector,
  2235. FLOW_DISSECTOR_KEY_VLAN,
  2236. f->key);
  2237. struct flow_dissector_key_vlan *mask =
  2238. skb_flow_dissector_target(f->dissector,
  2239. FLOW_DISSECTOR_KEY_VLAN,
  2240. f->mask);
  2241. if (mask->vlan_id) {
  2242. if (mask->vlan_id == VLAN_VID_MASK) {
  2243. field_flags |= I40EVF_CLOUD_FIELD_IVLAN;
  2244. } else {
  2245. dev_err(&adapter->pdev->dev, "Bad vlan mask %u\n",
  2246. mask->vlan_id);
  2247. return I40E_ERR_CONFIG;
  2248. }
  2249. }
  2250. vf->mask.tcp_spec.vlan_id |= cpu_to_be16(0xffff);
  2251. vf->data.tcp_spec.vlan_id = cpu_to_be16(key->vlan_id);
  2252. }
  2253. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
  2254. struct flow_dissector_key_control *key =
  2255. skb_flow_dissector_target(f->dissector,
  2256. FLOW_DISSECTOR_KEY_CONTROL,
  2257. f->key);
  2258. addr_type = key->addr_type;
  2259. }
  2260. if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
  2261. struct flow_dissector_key_ipv4_addrs *key =
  2262. skb_flow_dissector_target(f->dissector,
  2263. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  2264. f->key);
  2265. struct flow_dissector_key_ipv4_addrs *mask =
  2266. skb_flow_dissector_target(f->dissector,
  2267. FLOW_DISSECTOR_KEY_IPV4_ADDRS,
  2268. f->mask);
  2269. if (mask->dst) {
  2270. if (mask->dst == cpu_to_be32(0xffffffff)) {
  2271. field_flags |= I40EVF_CLOUD_FIELD_IIP;
  2272. } else {
  2273. dev_err(&adapter->pdev->dev, "Bad ip dst mask 0x%08x\n",
  2274. be32_to_cpu(mask->dst));
  2275. return I40E_ERR_CONFIG;
  2276. }
  2277. }
  2278. if (mask->src) {
  2279. if (mask->src == cpu_to_be32(0xffffffff)) {
  2280. field_flags |= I40EVF_CLOUD_FIELD_IIP;
  2281. } else {
  2282. dev_err(&adapter->pdev->dev, "Bad ip src mask 0x%08x\n",
  2283. be32_to_cpu(mask->dst));
  2284. return I40E_ERR_CONFIG;
  2285. }
  2286. }
  2287. if (field_flags & I40EVF_CLOUD_FIELD_TEN_ID) {
  2288. dev_info(&adapter->pdev->dev, "Tenant id not allowed for ip filter\n");
  2289. return I40E_ERR_CONFIG;
  2290. }
  2291. if (key->dst) {
  2292. vf->mask.tcp_spec.dst_ip[0] |= cpu_to_be32(0xffffffff);
  2293. vf->data.tcp_spec.dst_ip[0] = key->dst;
  2294. }
  2295. if (key->src) {
  2296. vf->mask.tcp_spec.src_ip[0] |= cpu_to_be32(0xffffffff);
  2297. vf->data.tcp_spec.src_ip[0] = key->src;
  2298. }
  2299. }
  2300. if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
  2301. struct flow_dissector_key_ipv6_addrs *key =
  2302. skb_flow_dissector_target(f->dissector,
  2303. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  2304. f->key);
  2305. struct flow_dissector_key_ipv6_addrs *mask =
  2306. skb_flow_dissector_target(f->dissector,
  2307. FLOW_DISSECTOR_KEY_IPV6_ADDRS,
  2308. f->mask);
  2309. /* validate mask, make sure it is not IPV6_ADDR_ANY */
  2310. if (ipv6_addr_any(&mask->dst)) {
  2311. dev_err(&adapter->pdev->dev, "Bad ipv6 dst mask 0x%02x\n",
  2312. IPV6_ADDR_ANY);
  2313. return I40E_ERR_CONFIG;
  2314. }
  2315. /* src and dest IPv6 address should not be LOOPBACK
  2316. * (0:0:0:0:0:0:0:1) which can be represented as ::1
  2317. */
  2318. if (ipv6_addr_loopback(&key->dst) ||
  2319. ipv6_addr_loopback(&key->src)) {
  2320. dev_err(&adapter->pdev->dev,
  2321. "ipv6 addr should not be loopback\n");
  2322. return I40E_ERR_CONFIG;
  2323. }
  2324. if (!ipv6_addr_any(&mask->dst) || !ipv6_addr_any(&mask->src))
  2325. field_flags |= I40EVF_CLOUD_FIELD_IIP;
  2326. for (i = 0; i < 4; i++)
  2327. vf->mask.tcp_spec.dst_ip[i] |= cpu_to_be32(0xffffffff);
  2328. memcpy(&vf->data.tcp_spec.dst_ip, &key->dst.s6_addr32,
  2329. sizeof(vf->data.tcp_spec.dst_ip));
  2330. for (i = 0; i < 4; i++)
  2331. vf->mask.tcp_spec.src_ip[i] |= cpu_to_be32(0xffffffff);
  2332. memcpy(&vf->data.tcp_spec.src_ip, &key->src.s6_addr32,
  2333. sizeof(vf->data.tcp_spec.src_ip));
  2334. }
  2335. if (dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_PORTS)) {
  2336. struct flow_dissector_key_ports *key =
  2337. skb_flow_dissector_target(f->dissector,
  2338. FLOW_DISSECTOR_KEY_PORTS,
  2339. f->key);
  2340. struct flow_dissector_key_ports *mask =
  2341. skb_flow_dissector_target(f->dissector,
  2342. FLOW_DISSECTOR_KEY_PORTS,
  2343. f->mask);
  2344. if (mask->src) {
  2345. if (mask->src == cpu_to_be16(0xffff)) {
  2346. field_flags |= I40EVF_CLOUD_FIELD_IIP;
  2347. } else {
  2348. dev_err(&adapter->pdev->dev, "Bad src port mask %u\n",
  2349. be16_to_cpu(mask->src));
  2350. return I40E_ERR_CONFIG;
  2351. }
  2352. }
  2353. if (mask->dst) {
  2354. if (mask->dst == cpu_to_be16(0xffff)) {
  2355. field_flags |= I40EVF_CLOUD_FIELD_IIP;
  2356. } else {
  2357. dev_err(&adapter->pdev->dev, "Bad dst port mask %u\n",
  2358. be16_to_cpu(mask->dst));
  2359. return I40E_ERR_CONFIG;
  2360. }
  2361. }
  2362. if (key->dst) {
  2363. vf->mask.tcp_spec.dst_port |= cpu_to_be16(0xffff);
  2364. vf->data.tcp_spec.dst_port = key->dst;
  2365. }
  2366. if (key->src) {
  2367. vf->mask.tcp_spec.src_port |= cpu_to_be16(0xffff);
  2368. vf->data.tcp_spec.src_port = key->src;
  2369. }
  2370. }
  2371. vf->field_flags = field_flags;
  2372. return 0;
  2373. }
  2374. /**
  2375. * i40evf_handle_tclass - Forward to a traffic class on the device
  2376. * @adapter: board private structure
  2377. * @tc: traffic class index on the device
  2378. * @filter: pointer to cloud filter structure
  2379. */
  2380. static int i40evf_handle_tclass(struct i40evf_adapter *adapter, u32 tc,
  2381. struct i40evf_cloud_filter *filter)
  2382. {
  2383. if (tc == 0)
  2384. return 0;
  2385. if (tc < adapter->num_tc) {
  2386. if (!filter->f.data.tcp_spec.dst_port) {
  2387. dev_err(&adapter->pdev->dev,
  2388. "Specify destination port to redirect to traffic class other than TC0\n");
  2389. return -EINVAL;
  2390. }
  2391. }
  2392. /* redirect to a traffic class on the same device */
  2393. filter->f.action = VIRTCHNL_ACTION_TC_REDIRECT;
  2394. filter->f.action_meta = tc;
  2395. return 0;
  2396. }
  2397. /**
  2398. * i40evf_configure_clsflower - Add tc flower filters
  2399. * @adapter: board private structure
  2400. * @cls_flower: Pointer to struct tc_cls_flower_offload
  2401. */
  2402. static int i40evf_configure_clsflower(struct i40evf_adapter *adapter,
  2403. struct tc_cls_flower_offload *cls_flower)
  2404. {
  2405. int tc = tc_classid_to_hwtc(adapter->netdev, cls_flower->classid);
  2406. struct i40evf_cloud_filter *filter = NULL;
  2407. int err = -EINVAL, count = 50;
  2408. if (tc < 0) {
  2409. dev_err(&adapter->pdev->dev, "Invalid traffic class\n");
  2410. return -EINVAL;
  2411. }
  2412. filter = kzalloc(sizeof(*filter), GFP_KERNEL);
  2413. if (!filter)
  2414. return -ENOMEM;
  2415. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  2416. &adapter->crit_section)) {
  2417. if (--count == 0)
  2418. goto err;
  2419. udelay(1);
  2420. }
  2421. filter->cookie = cls_flower->cookie;
  2422. /* set the mask to all zeroes to begin with */
  2423. memset(&filter->f.mask.tcp_spec, 0, sizeof(struct virtchnl_l4_spec));
  2424. /* start out with flow type and eth type IPv4 to begin with */
  2425. filter->f.flow_type = VIRTCHNL_TCP_V4_FLOW;
  2426. err = i40evf_parse_cls_flower(adapter, cls_flower, filter);
  2427. if (err < 0)
  2428. goto err;
  2429. err = i40evf_handle_tclass(adapter, tc, filter);
  2430. if (err < 0)
  2431. goto err;
  2432. /* add filter to the list */
  2433. spin_lock_bh(&adapter->cloud_filter_list_lock);
  2434. list_add_tail(&filter->list, &adapter->cloud_filter_list);
  2435. adapter->num_cloud_filters++;
  2436. filter->add = true;
  2437. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_CLOUD_FILTER;
  2438. spin_unlock_bh(&adapter->cloud_filter_list_lock);
  2439. err:
  2440. if (err)
  2441. kfree(filter);
  2442. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  2443. return err;
  2444. }
  2445. /* i40evf_find_cf - Find the cloud filter in the list
  2446. * @adapter: Board private structure
  2447. * @cookie: filter specific cookie
  2448. *
  2449. * Returns ptr to the filter object or NULL. Must be called while holding the
  2450. * cloud_filter_list_lock.
  2451. */
  2452. static struct i40evf_cloud_filter *i40evf_find_cf(struct i40evf_adapter *adapter,
  2453. unsigned long *cookie)
  2454. {
  2455. struct i40evf_cloud_filter *filter = NULL;
  2456. if (!cookie)
  2457. return NULL;
  2458. list_for_each_entry(filter, &adapter->cloud_filter_list, list) {
  2459. if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
  2460. return filter;
  2461. }
  2462. return NULL;
  2463. }
  2464. /**
  2465. * i40evf_delete_clsflower - Remove tc flower filters
  2466. * @adapter: board private structure
  2467. * @cls_flower: Pointer to struct tc_cls_flower_offload
  2468. */
  2469. static int i40evf_delete_clsflower(struct i40evf_adapter *adapter,
  2470. struct tc_cls_flower_offload *cls_flower)
  2471. {
  2472. struct i40evf_cloud_filter *filter = NULL;
  2473. int err = 0;
  2474. spin_lock_bh(&adapter->cloud_filter_list_lock);
  2475. filter = i40evf_find_cf(adapter, &cls_flower->cookie);
  2476. if (filter) {
  2477. filter->del = true;
  2478. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_CLOUD_FILTER;
  2479. } else {
  2480. err = -EINVAL;
  2481. }
  2482. spin_unlock_bh(&adapter->cloud_filter_list_lock);
  2483. return err;
  2484. }
  2485. /**
  2486. * i40evf_setup_tc_cls_flower - flower classifier offloads
  2487. * @netdev: net device to configure
  2488. * @type_data: offload data
  2489. */
  2490. static int i40evf_setup_tc_cls_flower(struct i40evf_adapter *adapter,
  2491. struct tc_cls_flower_offload *cls_flower)
  2492. {
  2493. if (cls_flower->common.chain_index)
  2494. return -EOPNOTSUPP;
  2495. switch (cls_flower->command) {
  2496. case TC_CLSFLOWER_REPLACE:
  2497. return i40evf_configure_clsflower(adapter, cls_flower);
  2498. case TC_CLSFLOWER_DESTROY:
  2499. return i40evf_delete_clsflower(adapter, cls_flower);
  2500. case TC_CLSFLOWER_STATS:
  2501. return -EOPNOTSUPP;
  2502. default:
  2503. return -EOPNOTSUPP;
  2504. }
  2505. }
  2506. /**
  2507. * i40evf_setup_tc_block_cb - block callback for tc
  2508. * @type: type of offload
  2509. * @type_data: offload data
  2510. * @cb_priv:
  2511. *
  2512. * This function is the block callback for traffic classes
  2513. **/
  2514. static int i40evf_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
  2515. void *cb_priv)
  2516. {
  2517. switch (type) {
  2518. case TC_SETUP_CLSFLOWER:
  2519. return i40evf_setup_tc_cls_flower(cb_priv, type_data);
  2520. default:
  2521. return -EOPNOTSUPP;
  2522. }
  2523. }
  2524. /**
  2525. * i40evf_setup_tc_block - register callbacks for tc
  2526. * @netdev: network interface device structure
  2527. * @f: tc offload data
  2528. *
  2529. * This function registers block callbacks for tc
  2530. * offloads
  2531. **/
  2532. static int i40evf_setup_tc_block(struct net_device *dev,
  2533. struct tc_block_offload *f)
  2534. {
  2535. struct i40evf_adapter *adapter = netdev_priv(dev);
  2536. if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
  2537. return -EOPNOTSUPP;
  2538. switch (f->command) {
  2539. case TC_BLOCK_BIND:
  2540. return tcf_block_cb_register(f->block, i40evf_setup_tc_block_cb,
  2541. adapter, adapter, f->extack);
  2542. case TC_BLOCK_UNBIND:
  2543. tcf_block_cb_unregister(f->block, i40evf_setup_tc_block_cb,
  2544. adapter);
  2545. return 0;
  2546. default:
  2547. return -EOPNOTSUPP;
  2548. }
  2549. }
  2550. /**
  2551. * i40evf_setup_tc - configure multiple traffic classes
  2552. * @netdev: network interface device structure
  2553. * @type: type of offload
  2554. * @type_date: tc offload data
  2555. *
  2556. * This function is the callback to ndo_setup_tc in the
  2557. * netdev_ops.
  2558. *
  2559. * Returns 0 on success
  2560. **/
  2561. static int i40evf_setup_tc(struct net_device *netdev, enum tc_setup_type type,
  2562. void *type_data)
  2563. {
  2564. switch (type) {
  2565. case TC_SETUP_QDISC_MQPRIO:
  2566. return __i40evf_setup_tc(netdev, type_data);
  2567. case TC_SETUP_BLOCK:
  2568. return i40evf_setup_tc_block(netdev, type_data);
  2569. default:
  2570. return -EOPNOTSUPP;
  2571. }
  2572. }
  2573. /**
  2574. * i40evf_open - Called when a network interface is made active
  2575. * @netdev: network interface device structure
  2576. *
  2577. * Returns 0 on success, negative value on failure
  2578. *
  2579. * The open entry point is called when a network interface is made
  2580. * active by the system (IFF_UP). At this point all resources needed
  2581. * for transmit and receive operations are allocated, the interrupt
  2582. * handler is registered with the OS, the watchdog timer is started,
  2583. * and the stack is notified that the interface is ready.
  2584. **/
  2585. static int i40evf_open(struct net_device *netdev)
  2586. {
  2587. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2588. int err;
  2589. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
  2590. dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
  2591. return -EIO;
  2592. }
  2593. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  2594. &adapter->crit_section))
  2595. usleep_range(500, 1000);
  2596. if (adapter->state != __I40EVF_DOWN) {
  2597. err = -EBUSY;
  2598. goto err_unlock;
  2599. }
  2600. /* allocate transmit descriptors */
  2601. err = i40evf_setup_all_tx_resources(adapter);
  2602. if (err)
  2603. goto err_setup_tx;
  2604. /* allocate receive descriptors */
  2605. err = i40evf_setup_all_rx_resources(adapter);
  2606. if (err)
  2607. goto err_setup_rx;
  2608. /* clear any pending interrupts, may auto mask */
  2609. err = i40evf_request_traffic_irqs(adapter, netdev->name);
  2610. if (err)
  2611. goto err_req_irq;
  2612. spin_lock_bh(&adapter->mac_vlan_list_lock);
  2613. i40evf_add_filter(adapter, adapter->hw.mac.addr);
  2614. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  2615. i40evf_configure(adapter);
  2616. i40evf_up_complete(adapter);
  2617. i40evf_irq_enable(adapter, true);
  2618. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  2619. return 0;
  2620. err_req_irq:
  2621. i40evf_down(adapter);
  2622. i40evf_free_traffic_irqs(adapter);
  2623. err_setup_rx:
  2624. i40evf_free_all_rx_resources(adapter);
  2625. err_setup_tx:
  2626. i40evf_free_all_tx_resources(adapter);
  2627. err_unlock:
  2628. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  2629. return err;
  2630. }
  2631. /**
  2632. * i40evf_close - Disables a network interface
  2633. * @netdev: network interface device structure
  2634. *
  2635. * Returns 0, this is not allowed to fail
  2636. *
  2637. * The close entry point is called when an interface is de-activated
  2638. * by the OS. The hardware is still under the drivers control, but
  2639. * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
  2640. * are freed, along with all transmit and receive resources.
  2641. **/
  2642. static int i40evf_close(struct net_device *netdev)
  2643. {
  2644. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2645. int status;
  2646. if (adapter->state <= __I40EVF_DOWN_PENDING)
  2647. return 0;
  2648. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  2649. &adapter->crit_section))
  2650. usleep_range(500, 1000);
  2651. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  2652. if (CLIENT_ENABLED(adapter))
  2653. adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
  2654. i40evf_down(adapter);
  2655. adapter->state = __I40EVF_DOWN_PENDING;
  2656. i40evf_free_traffic_irqs(adapter);
  2657. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  2658. /* We explicitly don't free resources here because the hardware is
  2659. * still active and can DMA into memory. Resources are cleared in
  2660. * i40evf_virtchnl_completion() after we get confirmation from the PF
  2661. * driver that the rings have been stopped.
  2662. *
  2663. * Also, we wait for state to transition to __I40EVF_DOWN before
  2664. * returning. State change occurs in i40evf_virtchnl_completion() after
  2665. * VF resources are released (which occurs after PF driver processes and
  2666. * responds to admin queue commands).
  2667. */
  2668. status = wait_event_timeout(adapter->down_waitqueue,
  2669. adapter->state == __I40EVF_DOWN,
  2670. msecs_to_jiffies(200));
  2671. if (!status)
  2672. netdev_warn(netdev, "Device resources not yet released\n");
  2673. return 0;
  2674. }
  2675. /**
  2676. * i40evf_change_mtu - Change the Maximum Transfer Unit
  2677. * @netdev: network interface device structure
  2678. * @new_mtu: new value for maximum frame size
  2679. *
  2680. * Returns 0 on success, negative on failure
  2681. **/
  2682. static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
  2683. {
  2684. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2685. netdev->mtu = new_mtu;
  2686. if (CLIENT_ENABLED(adapter)) {
  2687. i40evf_notify_client_l2_params(&adapter->vsi);
  2688. adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
  2689. }
  2690. adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
  2691. schedule_work(&adapter->reset_task);
  2692. return 0;
  2693. }
  2694. /**
  2695. * i40e_set_features - set the netdev feature flags
  2696. * @netdev: ptr to the netdev being adjusted
  2697. * @features: the feature set that the stack is suggesting
  2698. * Note: expects to be called while under rtnl_lock()
  2699. **/
  2700. static int i40evf_set_features(struct net_device *netdev,
  2701. netdev_features_t features)
  2702. {
  2703. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2704. /* Don't allow changing VLAN_RX flag when VLAN is set for VF
  2705. * and return an error in this case
  2706. */
  2707. if (VLAN_ALLOWED(adapter)) {
  2708. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2709. adapter->aq_required |=
  2710. I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
  2711. else
  2712. adapter->aq_required |=
  2713. I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
  2714. } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
  2715. return -EINVAL;
  2716. }
  2717. return 0;
  2718. }
  2719. /**
  2720. * i40evf_features_check - Validate encapsulated packet conforms to limits
  2721. * @skb: skb buff
  2722. * @dev: This physical port's netdev
  2723. * @features: Offload features that the stack believes apply
  2724. **/
  2725. static netdev_features_t i40evf_features_check(struct sk_buff *skb,
  2726. struct net_device *dev,
  2727. netdev_features_t features)
  2728. {
  2729. size_t len;
  2730. /* No point in doing any of this if neither checksum nor GSO are
  2731. * being requested for this frame. We can rule out both by just
  2732. * checking for CHECKSUM_PARTIAL
  2733. */
  2734. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2735. return features;
  2736. /* We cannot support GSO if the MSS is going to be less than
  2737. * 64 bytes. If it is then we need to drop support for GSO.
  2738. */
  2739. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  2740. features &= ~NETIF_F_GSO_MASK;
  2741. /* MACLEN can support at most 63 words */
  2742. len = skb_network_header(skb) - skb->data;
  2743. if (len & ~(63 * 2))
  2744. goto out_err;
  2745. /* IPLEN and EIPLEN can support at most 127 dwords */
  2746. len = skb_transport_header(skb) - skb_network_header(skb);
  2747. if (len & ~(127 * 4))
  2748. goto out_err;
  2749. if (skb->encapsulation) {
  2750. /* L4TUNLEN can support 127 words */
  2751. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  2752. if (len & ~(127 * 2))
  2753. goto out_err;
  2754. /* IPLEN can support at most 127 dwords */
  2755. len = skb_inner_transport_header(skb) -
  2756. skb_inner_network_header(skb);
  2757. if (len & ~(127 * 4))
  2758. goto out_err;
  2759. }
  2760. /* No need to validate L4LEN as TCP is the only protocol with a
  2761. * a flexible value and we support all possible values supported
  2762. * by TCP, which is at most 15 dwords
  2763. */
  2764. return features;
  2765. out_err:
  2766. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2767. }
  2768. /**
  2769. * i40evf_fix_features - fix up the netdev feature bits
  2770. * @netdev: our net device
  2771. * @features: desired feature bits
  2772. *
  2773. * Returns fixed-up features bits
  2774. **/
  2775. static netdev_features_t i40evf_fix_features(struct net_device *netdev,
  2776. netdev_features_t features)
  2777. {
  2778. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2779. if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
  2780. features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
  2781. NETIF_F_HW_VLAN_CTAG_RX |
  2782. NETIF_F_HW_VLAN_CTAG_FILTER);
  2783. return features;
  2784. }
  2785. static const struct net_device_ops i40evf_netdev_ops = {
  2786. .ndo_open = i40evf_open,
  2787. .ndo_stop = i40evf_close,
  2788. .ndo_start_xmit = i40evf_xmit_frame,
  2789. .ndo_set_rx_mode = i40evf_set_rx_mode,
  2790. .ndo_validate_addr = eth_validate_addr,
  2791. .ndo_set_mac_address = i40evf_set_mac,
  2792. .ndo_change_mtu = i40evf_change_mtu,
  2793. .ndo_tx_timeout = i40evf_tx_timeout,
  2794. .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
  2795. .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
  2796. .ndo_features_check = i40evf_features_check,
  2797. .ndo_fix_features = i40evf_fix_features,
  2798. .ndo_set_features = i40evf_set_features,
  2799. #ifdef CONFIG_NET_POLL_CONTROLLER
  2800. .ndo_poll_controller = i40evf_netpoll,
  2801. #endif
  2802. .ndo_setup_tc = i40evf_setup_tc,
  2803. };
  2804. /**
  2805. * i40evf_check_reset_complete - check that VF reset is complete
  2806. * @hw: pointer to hw struct
  2807. *
  2808. * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
  2809. **/
  2810. static int i40evf_check_reset_complete(struct i40e_hw *hw)
  2811. {
  2812. u32 rstat;
  2813. int i;
  2814. for (i = 0; i < 100; i++) {
  2815. rstat = rd32(hw, I40E_VFGEN_RSTAT) &
  2816. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  2817. if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
  2818. (rstat == VIRTCHNL_VFR_COMPLETED))
  2819. return 0;
  2820. usleep_range(10, 20);
  2821. }
  2822. return -EBUSY;
  2823. }
  2824. /**
  2825. * i40evf_process_config - Process the config information we got from the PF
  2826. * @adapter: board private structure
  2827. *
  2828. * Verify that we have a valid config struct, and set up our netdev features
  2829. * and our VSI struct.
  2830. **/
  2831. int i40evf_process_config(struct i40evf_adapter *adapter)
  2832. {
  2833. struct virtchnl_vf_resource *vfres = adapter->vf_res;
  2834. int i, num_req_queues = adapter->num_req_queues;
  2835. struct net_device *netdev = adapter->netdev;
  2836. struct i40e_vsi *vsi = &adapter->vsi;
  2837. netdev_features_t hw_enc_features;
  2838. netdev_features_t hw_features;
  2839. /* got VF config message back from PF, now we can parse it */
  2840. for (i = 0; i < vfres->num_vsis; i++) {
  2841. if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
  2842. adapter->vsi_res = &vfres->vsi_res[i];
  2843. }
  2844. if (!adapter->vsi_res) {
  2845. dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
  2846. return -ENODEV;
  2847. }
  2848. if (num_req_queues &&
  2849. num_req_queues != adapter->vsi_res->num_queue_pairs) {
  2850. /* Problem. The PF gave us fewer queues than what we had
  2851. * negotiated in our request. Need a reset to see if we can't
  2852. * get back to a working state.
  2853. */
  2854. dev_err(&adapter->pdev->dev,
  2855. "Requested %d queues, but PF only gave us %d.\n",
  2856. num_req_queues,
  2857. adapter->vsi_res->num_queue_pairs);
  2858. adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
  2859. adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
  2860. i40evf_schedule_reset(adapter);
  2861. return -ENODEV;
  2862. }
  2863. adapter->num_req_queues = 0;
  2864. hw_enc_features = NETIF_F_SG |
  2865. NETIF_F_IP_CSUM |
  2866. NETIF_F_IPV6_CSUM |
  2867. NETIF_F_HIGHDMA |
  2868. NETIF_F_SOFT_FEATURES |
  2869. NETIF_F_TSO |
  2870. NETIF_F_TSO_ECN |
  2871. NETIF_F_TSO6 |
  2872. NETIF_F_SCTP_CRC |
  2873. NETIF_F_RXHASH |
  2874. NETIF_F_RXCSUM |
  2875. 0;
  2876. /* advertise to stack only if offloads for encapsulated packets is
  2877. * supported
  2878. */
  2879. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
  2880. hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
  2881. NETIF_F_GSO_GRE |
  2882. NETIF_F_GSO_GRE_CSUM |
  2883. NETIF_F_GSO_IPXIP4 |
  2884. NETIF_F_GSO_IPXIP6 |
  2885. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  2886. NETIF_F_GSO_PARTIAL |
  2887. 0;
  2888. if (!(vfres->vf_cap_flags &
  2889. VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
  2890. netdev->gso_partial_features |=
  2891. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  2892. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  2893. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  2894. netdev->hw_enc_features |= hw_enc_features;
  2895. }
  2896. /* record features VLANs can make use of */
  2897. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  2898. /* Write features and hw_features separately to avoid polluting
  2899. * with, or dropping, features that are set when we registered.
  2900. */
  2901. hw_features = hw_enc_features;
  2902. /* Enable VLAN features if supported */
  2903. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
  2904. hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
  2905. NETIF_F_HW_VLAN_CTAG_RX);
  2906. /* Enable cloud filter if ADQ is supported */
  2907. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ADQ)
  2908. hw_features |= NETIF_F_HW_TC;
  2909. netdev->hw_features |= hw_features;
  2910. netdev->features |= hw_features;
  2911. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
  2912. netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  2913. netdev->priv_flags |= IFF_UNICAST_FLT;
  2914. /* Do not turn on offloads when they are requested to be turned off.
  2915. * TSO needs minimum 576 bytes to work correctly.
  2916. */
  2917. if (netdev->wanted_features) {
  2918. if (!(netdev->wanted_features & NETIF_F_TSO) ||
  2919. netdev->mtu < 576)
  2920. netdev->features &= ~NETIF_F_TSO;
  2921. if (!(netdev->wanted_features & NETIF_F_TSO6) ||
  2922. netdev->mtu < 576)
  2923. netdev->features &= ~NETIF_F_TSO6;
  2924. if (!(netdev->wanted_features & NETIF_F_TSO_ECN))
  2925. netdev->features &= ~NETIF_F_TSO_ECN;
  2926. if (!(netdev->wanted_features & NETIF_F_GRO))
  2927. netdev->features &= ~NETIF_F_GRO;
  2928. if (!(netdev->wanted_features & NETIF_F_GSO))
  2929. netdev->features &= ~NETIF_F_GSO;
  2930. }
  2931. adapter->vsi.id = adapter->vsi_res->vsi_id;
  2932. adapter->vsi.back = adapter;
  2933. adapter->vsi.base_vector = 1;
  2934. adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
  2935. vsi->netdev = adapter->netdev;
  2936. vsi->qs_handle = adapter->vsi_res->qset_handle;
  2937. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  2938. adapter->rss_key_size = vfres->rss_key_size;
  2939. adapter->rss_lut_size = vfres->rss_lut_size;
  2940. } else {
  2941. adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
  2942. adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
  2943. }
  2944. return 0;
  2945. }
  2946. /**
  2947. * i40evf_init_task - worker thread to perform delayed initialization
  2948. * @work: pointer to work_struct containing our data
  2949. *
  2950. * This task completes the work that was begun in probe. Due to the nature
  2951. * of VF-PF communications, we may need to wait tens of milliseconds to get
  2952. * responses back from the PF. Rather than busy-wait in probe and bog down the
  2953. * whole system, we'll do it in a task so we can sleep.
  2954. * This task only runs during driver init. Once we've established
  2955. * communications with the PF driver and set up our netdev, the watchdog
  2956. * takes over.
  2957. **/
  2958. static void i40evf_init_task(struct work_struct *work)
  2959. {
  2960. struct i40evf_adapter *adapter = container_of(work,
  2961. struct i40evf_adapter,
  2962. init_task.work);
  2963. struct net_device *netdev = adapter->netdev;
  2964. struct i40e_hw *hw = &adapter->hw;
  2965. struct pci_dev *pdev = adapter->pdev;
  2966. int err, bufsz;
  2967. switch (adapter->state) {
  2968. case __I40EVF_STARTUP:
  2969. /* driver loaded, probe complete */
  2970. adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
  2971. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  2972. err = i40e_set_mac_type(hw);
  2973. if (err) {
  2974. dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
  2975. err);
  2976. goto err;
  2977. }
  2978. err = i40evf_check_reset_complete(hw);
  2979. if (err) {
  2980. dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
  2981. err);
  2982. goto err;
  2983. }
  2984. hw->aq.num_arq_entries = I40EVF_AQ_LEN;
  2985. hw->aq.num_asq_entries = I40EVF_AQ_LEN;
  2986. hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
  2987. hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
  2988. err = i40evf_init_adminq(hw);
  2989. if (err) {
  2990. dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
  2991. err);
  2992. goto err;
  2993. }
  2994. err = i40evf_send_api_ver(adapter);
  2995. if (err) {
  2996. dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
  2997. i40evf_shutdown_adminq(hw);
  2998. goto err;
  2999. }
  3000. adapter->state = __I40EVF_INIT_VERSION_CHECK;
  3001. goto restart;
  3002. case __I40EVF_INIT_VERSION_CHECK:
  3003. if (!i40evf_asq_done(hw)) {
  3004. dev_err(&pdev->dev, "Admin queue command never completed\n");
  3005. i40evf_shutdown_adminq(hw);
  3006. adapter->state = __I40EVF_STARTUP;
  3007. goto err;
  3008. }
  3009. /* aq msg sent, awaiting reply */
  3010. err = i40evf_verify_api_ver(adapter);
  3011. if (err) {
  3012. if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  3013. err = i40evf_send_api_ver(adapter);
  3014. else
  3015. dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
  3016. adapter->pf_version.major,
  3017. adapter->pf_version.minor,
  3018. VIRTCHNL_VERSION_MAJOR,
  3019. VIRTCHNL_VERSION_MINOR);
  3020. goto err;
  3021. }
  3022. err = i40evf_send_vf_config_msg(adapter);
  3023. if (err) {
  3024. dev_err(&pdev->dev, "Unable to send config request (%d)\n",
  3025. err);
  3026. goto err;
  3027. }
  3028. adapter->state = __I40EVF_INIT_GET_RESOURCES;
  3029. goto restart;
  3030. case __I40EVF_INIT_GET_RESOURCES:
  3031. /* aq msg sent, awaiting reply */
  3032. if (!adapter->vf_res) {
  3033. bufsz = sizeof(struct virtchnl_vf_resource) +
  3034. (I40E_MAX_VF_VSI *
  3035. sizeof(struct virtchnl_vsi_resource));
  3036. adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
  3037. if (!adapter->vf_res)
  3038. goto err;
  3039. }
  3040. err = i40evf_get_vf_config(adapter);
  3041. if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
  3042. err = i40evf_send_vf_config_msg(adapter);
  3043. goto err;
  3044. } else if (err == I40E_ERR_PARAM) {
  3045. /* We only get ERR_PARAM if the device is in a very bad
  3046. * state or if we've been disabled for previous bad
  3047. * behavior. Either way, we're done now.
  3048. */
  3049. i40evf_shutdown_adminq(hw);
  3050. dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
  3051. return;
  3052. }
  3053. if (err) {
  3054. dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
  3055. err);
  3056. goto err_alloc;
  3057. }
  3058. adapter->state = __I40EVF_INIT_SW;
  3059. break;
  3060. default:
  3061. goto err_alloc;
  3062. }
  3063. if (i40evf_process_config(adapter))
  3064. goto err_alloc;
  3065. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  3066. adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
  3067. netdev->netdev_ops = &i40evf_netdev_ops;
  3068. i40evf_set_ethtool_ops(netdev);
  3069. netdev->watchdog_timeo = 5 * HZ;
  3070. /* MTU range: 68 - 9710 */
  3071. netdev->min_mtu = ETH_MIN_MTU;
  3072. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  3073. if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
  3074. dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
  3075. adapter->hw.mac.addr);
  3076. eth_hw_addr_random(netdev);
  3077. ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
  3078. } else {
  3079. adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
  3080. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  3081. ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
  3082. }
  3083. timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
  3084. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  3085. adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
  3086. adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
  3087. err = i40evf_init_interrupt_scheme(adapter);
  3088. if (err)
  3089. goto err_sw_init;
  3090. i40evf_map_rings_to_vectors(adapter);
  3091. if (adapter->vf_res->vf_cap_flags &
  3092. VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  3093. adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
  3094. err = i40evf_request_misc_irq(adapter);
  3095. if (err)
  3096. goto err_sw_init;
  3097. netif_carrier_off(netdev);
  3098. adapter->link_up = false;
  3099. if (!adapter->netdev_registered) {
  3100. err = register_netdev(netdev);
  3101. if (err)
  3102. goto err_register;
  3103. }
  3104. adapter->netdev_registered = true;
  3105. netif_tx_stop_all_queues(netdev);
  3106. if (CLIENT_ALLOWED(adapter)) {
  3107. err = i40evf_lan_add_device(adapter);
  3108. if (err)
  3109. dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
  3110. err);
  3111. }
  3112. dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
  3113. if (netdev->features & NETIF_F_GRO)
  3114. dev_info(&pdev->dev, "GRO is enabled\n");
  3115. adapter->state = __I40EVF_DOWN;
  3116. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  3117. i40evf_misc_irq_enable(adapter);
  3118. wake_up(&adapter->down_waitqueue);
  3119. adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
  3120. adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
  3121. if (!adapter->rss_key || !adapter->rss_lut)
  3122. goto err_mem;
  3123. if (RSS_AQ(adapter)) {
  3124. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
  3125. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  3126. } else {
  3127. i40evf_init_rss(adapter);
  3128. }
  3129. return;
  3130. restart:
  3131. schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
  3132. return;
  3133. err_mem:
  3134. i40evf_free_rss(adapter);
  3135. err_register:
  3136. i40evf_free_misc_irq(adapter);
  3137. err_sw_init:
  3138. i40evf_reset_interrupt_capability(adapter);
  3139. err_alloc:
  3140. kfree(adapter->vf_res);
  3141. adapter->vf_res = NULL;
  3142. err:
  3143. /* Things went into the weeds, so try again later */
  3144. if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
  3145. dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
  3146. adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
  3147. i40evf_shutdown_adminq(hw);
  3148. adapter->state = __I40EVF_STARTUP;
  3149. schedule_delayed_work(&adapter->init_task, HZ * 5);
  3150. return;
  3151. }
  3152. schedule_delayed_work(&adapter->init_task, HZ);
  3153. }
  3154. /**
  3155. * i40evf_shutdown - Shutdown the device in preparation for a reboot
  3156. * @pdev: pci device structure
  3157. **/
  3158. static void i40evf_shutdown(struct pci_dev *pdev)
  3159. {
  3160. struct net_device *netdev = pci_get_drvdata(pdev);
  3161. struct i40evf_adapter *adapter = netdev_priv(netdev);
  3162. netif_device_detach(netdev);
  3163. if (netif_running(netdev))
  3164. i40evf_close(netdev);
  3165. /* Prevent the watchdog from running. */
  3166. adapter->state = __I40EVF_REMOVE;
  3167. adapter->aq_required = 0;
  3168. #ifdef CONFIG_PM
  3169. pci_save_state(pdev);
  3170. #endif
  3171. pci_disable_device(pdev);
  3172. }
  3173. /**
  3174. * i40evf_probe - Device Initialization Routine
  3175. * @pdev: PCI device information struct
  3176. * @ent: entry in i40evf_pci_tbl
  3177. *
  3178. * Returns 0 on success, negative on failure
  3179. *
  3180. * i40evf_probe initializes an adapter identified by a pci_dev structure.
  3181. * The OS initialization, configuring of the adapter private structure,
  3182. * and a hardware reset occur.
  3183. **/
  3184. static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  3185. {
  3186. struct net_device *netdev;
  3187. struct i40evf_adapter *adapter = NULL;
  3188. struct i40e_hw *hw = NULL;
  3189. int err;
  3190. err = pci_enable_device(pdev);
  3191. if (err)
  3192. return err;
  3193. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  3194. if (err) {
  3195. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  3196. if (err) {
  3197. dev_err(&pdev->dev,
  3198. "DMA configuration failed: 0x%x\n", err);
  3199. goto err_dma;
  3200. }
  3201. }
  3202. err = pci_request_regions(pdev, i40evf_driver_name);
  3203. if (err) {
  3204. dev_err(&pdev->dev,
  3205. "pci_request_regions failed 0x%x\n", err);
  3206. goto err_pci_reg;
  3207. }
  3208. pci_enable_pcie_error_reporting(pdev);
  3209. pci_set_master(pdev);
  3210. netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter),
  3211. I40EVF_MAX_REQ_QUEUES);
  3212. if (!netdev) {
  3213. err = -ENOMEM;
  3214. goto err_alloc_etherdev;
  3215. }
  3216. SET_NETDEV_DEV(netdev, &pdev->dev);
  3217. pci_set_drvdata(pdev, netdev);
  3218. adapter = netdev_priv(netdev);
  3219. adapter->netdev = netdev;
  3220. adapter->pdev = pdev;
  3221. hw = &adapter->hw;
  3222. hw->back = adapter;
  3223. adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
  3224. adapter->state = __I40EVF_STARTUP;
  3225. /* Call save state here because it relies on the adapter struct. */
  3226. pci_save_state(pdev);
  3227. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  3228. pci_resource_len(pdev, 0));
  3229. if (!hw->hw_addr) {
  3230. err = -EIO;
  3231. goto err_ioremap;
  3232. }
  3233. hw->vendor_id = pdev->vendor;
  3234. hw->device_id = pdev->device;
  3235. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  3236. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  3237. hw->subsystem_device_id = pdev->subsystem_device;
  3238. hw->bus.device = PCI_SLOT(pdev->devfn);
  3239. hw->bus.func = PCI_FUNC(pdev->devfn);
  3240. hw->bus.bus_id = pdev->bus->number;
  3241. /* set up the locks for the AQ, do this only once in probe
  3242. * and destroy them only once in remove
  3243. */
  3244. mutex_init(&hw->aq.asq_mutex);
  3245. mutex_init(&hw->aq.arq_mutex);
  3246. spin_lock_init(&adapter->mac_vlan_list_lock);
  3247. spin_lock_init(&adapter->cloud_filter_list_lock);
  3248. INIT_LIST_HEAD(&adapter->mac_filter_list);
  3249. INIT_LIST_HEAD(&adapter->vlan_filter_list);
  3250. INIT_LIST_HEAD(&adapter->cloud_filter_list);
  3251. INIT_WORK(&adapter->reset_task, i40evf_reset_task);
  3252. INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
  3253. INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
  3254. INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
  3255. INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
  3256. schedule_delayed_work(&adapter->init_task,
  3257. msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
  3258. /* Setup the wait queue for indicating transition to down status */
  3259. init_waitqueue_head(&adapter->down_waitqueue);
  3260. return 0;
  3261. err_ioremap:
  3262. free_netdev(netdev);
  3263. err_alloc_etherdev:
  3264. pci_release_regions(pdev);
  3265. err_pci_reg:
  3266. err_dma:
  3267. pci_disable_device(pdev);
  3268. return err;
  3269. }
  3270. #ifdef CONFIG_PM
  3271. /**
  3272. * i40evf_suspend - Power management suspend routine
  3273. * @pdev: PCI device information struct
  3274. * @state: unused
  3275. *
  3276. * Called when the system (VM) is entering sleep/suspend.
  3277. **/
  3278. static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
  3279. {
  3280. struct net_device *netdev = pci_get_drvdata(pdev);
  3281. struct i40evf_adapter *adapter = netdev_priv(netdev);
  3282. int retval = 0;
  3283. netif_device_detach(netdev);
  3284. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  3285. &adapter->crit_section))
  3286. usleep_range(500, 1000);
  3287. if (netif_running(netdev)) {
  3288. rtnl_lock();
  3289. i40evf_down(adapter);
  3290. rtnl_unlock();
  3291. }
  3292. i40evf_free_misc_irq(adapter);
  3293. i40evf_reset_interrupt_capability(adapter);
  3294. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  3295. retval = pci_save_state(pdev);
  3296. if (retval)
  3297. return retval;
  3298. pci_disable_device(pdev);
  3299. return 0;
  3300. }
  3301. /**
  3302. * i40evf_resume - Power management resume routine
  3303. * @pdev: PCI device information struct
  3304. *
  3305. * Called when the system (VM) is resumed from sleep/suspend.
  3306. **/
  3307. static int i40evf_resume(struct pci_dev *pdev)
  3308. {
  3309. struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
  3310. struct net_device *netdev = adapter->netdev;
  3311. u32 err;
  3312. pci_set_power_state(pdev, PCI_D0);
  3313. pci_restore_state(pdev);
  3314. /* pci_restore_state clears dev->state_saved so call
  3315. * pci_save_state to restore it.
  3316. */
  3317. pci_save_state(pdev);
  3318. err = pci_enable_device_mem(pdev);
  3319. if (err) {
  3320. dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
  3321. return err;
  3322. }
  3323. pci_set_master(pdev);
  3324. rtnl_lock();
  3325. err = i40evf_set_interrupt_capability(adapter);
  3326. if (err) {
  3327. rtnl_unlock();
  3328. dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
  3329. return err;
  3330. }
  3331. err = i40evf_request_misc_irq(adapter);
  3332. rtnl_unlock();
  3333. if (err) {
  3334. dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
  3335. return err;
  3336. }
  3337. schedule_work(&adapter->reset_task);
  3338. netif_device_attach(netdev);
  3339. return err;
  3340. }
  3341. #endif /* CONFIG_PM */
  3342. /**
  3343. * i40evf_remove - Device Removal Routine
  3344. * @pdev: PCI device information struct
  3345. *
  3346. * i40evf_remove is called by the PCI subsystem to alert the driver
  3347. * that it should release a PCI device. The could be caused by a
  3348. * Hot-Plug event, or because the driver is going to be removed from
  3349. * memory.
  3350. **/
  3351. static void i40evf_remove(struct pci_dev *pdev)
  3352. {
  3353. struct net_device *netdev = pci_get_drvdata(pdev);
  3354. struct i40evf_adapter *adapter = netdev_priv(netdev);
  3355. struct i40evf_vlan_filter *vlf, *vlftmp;
  3356. struct i40evf_mac_filter *f, *ftmp;
  3357. struct i40evf_cloud_filter *cf, *cftmp;
  3358. struct i40e_hw *hw = &adapter->hw;
  3359. int err;
  3360. /* Indicate we are in remove and not to run reset_task */
  3361. set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
  3362. cancel_delayed_work_sync(&adapter->init_task);
  3363. cancel_work_sync(&adapter->reset_task);
  3364. cancel_delayed_work_sync(&adapter->client_task);
  3365. if (adapter->netdev_registered) {
  3366. unregister_netdev(netdev);
  3367. adapter->netdev_registered = false;
  3368. }
  3369. if (CLIENT_ALLOWED(adapter)) {
  3370. err = i40evf_lan_del_device(adapter);
  3371. if (err)
  3372. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  3373. err);
  3374. }
  3375. /* Shut down all the garbage mashers on the detention level */
  3376. adapter->state = __I40EVF_REMOVE;
  3377. adapter->aq_required = 0;
  3378. adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
  3379. i40evf_request_reset(adapter);
  3380. msleep(50);
  3381. /* If the FW isn't responding, kick it once, but only once. */
  3382. if (!i40evf_asq_done(hw)) {
  3383. i40evf_request_reset(adapter);
  3384. msleep(50);
  3385. }
  3386. i40evf_free_all_tx_resources(adapter);
  3387. i40evf_free_all_rx_resources(adapter);
  3388. i40evf_misc_irq_disable(adapter);
  3389. i40evf_free_misc_irq(adapter);
  3390. i40evf_reset_interrupt_capability(adapter);
  3391. i40evf_free_q_vectors(adapter);
  3392. if (adapter->watchdog_timer.function)
  3393. del_timer_sync(&adapter->watchdog_timer);
  3394. i40evf_free_rss(adapter);
  3395. if (hw->aq.asq.count)
  3396. i40evf_shutdown_adminq(hw);
  3397. /* destroy the locks only once, here */
  3398. mutex_destroy(&hw->aq.arq_mutex);
  3399. mutex_destroy(&hw->aq.asq_mutex);
  3400. iounmap(hw->hw_addr);
  3401. pci_release_regions(pdev);
  3402. i40evf_free_all_tx_resources(adapter);
  3403. i40evf_free_all_rx_resources(adapter);
  3404. i40evf_free_queues(adapter);
  3405. kfree(adapter->vf_res);
  3406. spin_lock_bh(&adapter->mac_vlan_list_lock);
  3407. /* If we got removed before an up/down sequence, we've got a filter
  3408. * hanging out there that we need to get rid of.
  3409. */
  3410. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  3411. list_del(&f->list);
  3412. kfree(f);
  3413. }
  3414. list_for_each_entry_safe(vlf, vlftmp, &adapter->vlan_filter_list,
  3415. list) {
  3416. list_del(&vlf->list);
  3417. kfree(vlf);
  3418. }
  3419. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  3420. spin_lock_bh(&adapter->cloud_filter_list_lock);
  3421. list_for_each_entry_safe(cf, cftmp, &adapter->cloud_filter_list, list) {
  3422. list_del(&cf->list);
  3423. kfree(cf);
  3424. }
  3425. spin_unlock_bh(&adapter->cloud_filter_list_lock);
  3426. free_netdev(netdev);
  3427. pci_disable_pcie_error_reporting(pdev);
  3428. pci_disable_device(pdev);
  3429. }
  3430. static struct pci_driver i40evf_driver = {
  3431. .name = i40evf_driver_name,
  3432. .id_table = i40evf_pci_tbl,
  3433. .probe = i40evf_probe,
  3434. .remove = i40evf_remove,
  3435. #ifdef CONFIG_PM
  3436. .suspend = i40evf_suspend,
  3437. .resume = i40evf_resume,
  3438. #endif
  3439. .shutdown = i40evf_shutdown,
  3440. };
  3441. /**
  3442. * i40e_init_module - Driver Registration Routine
  3443. *
  3444. * i40e_init_module is the first routine called when the driver is
  3445. * loaded. All it does is register with the PCI subsystem.
  3446. **/
  3447. static int __init i40evf_init_module(void)
  3448. {
  3449. int ret;
  3450. pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
  3451. i40evf_driver_version);
  3452. pr_info("%s\n", i40evf_copyright);
  3453. i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  3454. i40evf_driver_name);
  3455. if (!i40evf_wq) {
  3456. pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
  3457. return -ENOMEM;
  3458. }
  3459. ret = pci_register_driver(&i40evf_driver);
  3460. return ret;
  3461. }
  3462. module_init(i40evf_init_module);
  3463. /**
  3464. * i40e_exit_module - Driver Exit Cleanup Routine
  3465. *
  3466. * i40e_exit_module is called just before the driver is removed
  3467. * from memory.
  3468. **/
  3469. static void __exit i40evf_exit_module(void)
  3470. {
  3471. pci_unregister_driver(&i40evf_driver);
  3472. destroy_workqueue(i40evf_wq);
  3473. }
  3474. module_exit(i40evf_exit_module);
  3475. /* i40evf_main.c */