iavf_main.c 108 KB

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