iavf_main.c 108 KB

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