i40evf_main.c 109 KB

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