i40evf_main.c 109 KB

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