i40evf_main.c 85 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include "i40evf.h"
  27. #include "i40e_prototype.h"
  28. #include "i40evf_client.h"
  29. /* All i40evf tracepoints are defined by the include below, which must
  30. * be included exactly once across the whole kernel with
  31. * CREATE_TRACE_POINTS defined
  32. */
  33. #define CREATE_TRACE_POINTS
  34. #include "i40e_trace.h"
  35. static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
  36. static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
  37. static int i40evf_close(struct net_device *netdev);
  38. char i40evf_driver_name[] = "i40evf";
  39. static const char i40evf_driver_string[] =
  40. "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
  41. #define DRV_KERN "-k"
  42. #define DRV_VERSION_MAJOR 3
  43. #define DRV_VERSION_MINOR 0
  44. #define DRV_VERSION_BUILD 0
  45. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  46. __stringify(DRV_VERSION_MINOR) "." \
  47. __stringify(DRV_VERSION_BUILD) \
  48. DRV_KERN
  49. const char i40evf_driver_version[] = DRV_VERSION;
  50. static const char i40evf_copyright[] =
  51. "Copyright (c) 2013 - 2015 Intel Corporation.";
  52. /* i40evf_pci_tbl - PCI Device ID Table
  53. *
  54. * Wildcard entries (PCI_ANY_ID) should come last
  55. * Last entry must be all 0s
  56. *
  57. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  58. * Class, Class Mask, private data (not used) }
  59. */
  60. static const struct pci_device_id i40evf_pci_tbl[] = {
  61. {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
  62. {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
  65. /* required last entry */
  66. {0, }
  67. };
  68. MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
  69. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  70. MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
  71. MODULE_LICENSE("GPL");
  72. MODULE_VERSION(DRV_VERSION);
  73. static struct workqueue_struct *i40evf_wq;
  74. /**
  75. * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
  76. * @hw: pointer to the HW structure
  77. * @mem: ptr to mem struct to fill out
  78. * @size: size of memory requested
  79. * @alignment: what to align the allocation to
  80. **/
  81. i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
  82. struct i40e_dma_mem *mem,
  83. u64 size, u32 alignment)
  84. {
  85. struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  86. if (!mem)
  87. return I40E_ERR_PARAM;
  88. mem->size = ALIGN(size, alignment);
  89. mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
  90. (dma_addr_t *)&mem->pa, GFP_KERNEL);
  91. if (mem->va)
  92. return 0;
  93. else
  94. return I40E_ERR_NO_MEMORY;
  95. }
  96. /**
  97. * i40evf_free_dma_mem_d - OS specific memory free for shared code
  98. * @hw: pointer to the HW structure
  99. * @mem: ptr to mem struct to free
  100. **/
  101. i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  102. {
  103. struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  104. if (!mem || !mem->va)
  105. return I40E_ERR_PARAM;
  106. dma_free_coherent(&adapter->pdev->dev, mem->size,
  107. mem->va, (dma_addr_t)mem->pa);
  108. return 0;
  109. }
  110. /**
  111. * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
  112. * @hw: pointer to the HW structure
  113. * @mem: ptr to mem struct to fill out
  114. * @size: size of memory requested
  115. **/
  116. i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
  117. struct i40e_virt_mem *mem, u32 size)
  118. {
  119. if (!mem)
  120. return I40E_ERR_PARAM;
  121. mem->size = size;
  122. mem->va = kzalloc(size, GFP_KERNEL);
  123. if (mem->va)
  124. return 0;
  125. else
  126. return I40E_ERR_NO_MEMORY;
  127. }
  128. /**
  129. * i40evf_free_virt_mem_d - OS specific memory free for shared code
  130. * @hw: pointer to the HW structure
  131. * @mem: ptr to mem struct to free
  132. **/
  133. i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
  134. struct i40e_virt_mem *mem)
  135. {
  136. if (!mem)
  137. return I40E_ERR_PARAM;
  138. /* it's ok to kfree a NULL pointer */
  139. kfree(mem->va);
  140. return 0;
  141. }
  142. /**
  143. * i40evf_debug_d - OS dependent version of debug printing
  144. * @hw: pointer to the HW structure
  145. * @mask: debug level mask
  146. * @fmt_str: printf-type format description
  147. **/
  148. void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
  149. {
  150. char buf[512];
  151. va_list argptr;
  152. if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
  153. return;
  154. va_start(argptr, fmt_str);
  155. vsnprintf(buf, sizeof(buf), fmt_str, argptr);
  156. va_end(argptr);
  157. /* the debug string is already formatted with a newline */
  158. pr_info("%s", buf);
  159. }
  160. /**
  161. * i40evf_schedule_reset - Set the flags and schedule a reset event
  162. * @adapter: board private structure
  163. **/
  164. void i40evf_schedule_reset(struct i40evf_adapter *adapter)
  165. {
  166. if (!(adapter->flags &
  167. (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
  168. adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
  169. schedule_work(&adapter->reset_task);
  170. }
  171. }
  172. /**
  173. * i40evf_tx_timeout - Respond to a Tx Hang
  174. * @netdev: network interface device structure
  175. **/
  176. static void i40evf_tx_timeout(struct net_device *netdev)
  177. {
  178. struct i40evf_adapter *adapter = netdev_priv(netdev);
  179. adapter->tx_timeout_count++;
  180. i40evf_schedule_reset(adapter);
  181. }
  182. /**
  183. * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
  184. * @adapter: board private structure
  185. **/
  186. static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
  187. {
  188. struct i40e_hw *hw = &adapter->hw;
  189. if (!adapter->msix_entries)
  190. return;
  191. wr32(hw, I40E_VFINT_DYN_CTL01, 0);
  192. /* read flush */
  193. rd32(hw, I40E_VFGEN_RSTAT);
  194. synchronize_irq(adapter->msix_entries[0].vector);
  195. }
  196. /**
  197. * i40evf_misc_irq_enable - Enable default interrupt generation settings
  198. * @adapter: board private structure
  199. **/
  200. static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
  201. {
  202. struct i40e_hw *hw = &adapter->hw;
  203. wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
  204. I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
  205. wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
  206. /* read flush */
  207. rd32(hw, I40E_VFGEN_RSTAT);
  208. }
  209. /**
  210. * i40evf_irq_disable - Mask off interrupt generation on the NIC
  211. * @adapter: board private structure
  212. **/
  213. static void i40evf_irq_disable(struct i40evf_adapter *adapter)
  214. {
  215. int i;
  216. struct i40e_hw *hw = &adapter->hw;
  217. if (!adapter->msix_entries)
  218. return;
  219. for (i = 1; i < adapter->num_msix_vectors; i++) {
  220. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
  221. synchronize_irq(adapter->msix_entries[i].vector);
  222. }
  223. /* read flush */
  224. rd32(hw, I40E_VFGEN_RSTAT);
  225. }
  226. /**
  227. * i40evf_irq_enable_queues - Enable interrupt for specified queues
  228. * @adapter: board private structure
  229. * @mask: bitmap of queues to enable
  230. **/
  231. void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
  232. {
  233. struct i40e_hw *hw = &adapter->hw;
  234. int i;
  235. for (i = 1; i < adapter->num_msix_vectors; i++) {
  236. if (mask & BIT(i - 1)) {
  237. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
  238. I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  239. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
  240. I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK);
  241. }
  242. }
  243. }
  244. /**
  245. * i40evf_fire_sw_int - Generate SW interrupt for specified vectors
  246. * @adapter: board private structure
  247. * @mask: bitmap of vectors to trigger
  248. **/
  249. static void i40evf_fire_sw_int(struct i40evf_adapter *adapter, u32 mask)
  250. {
  251. struct i40e_hw *hw = &adapter->hw;
  252. int i;
  253. u32 dyn_ctl;
  254. if (mask & 1) {
  255. dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTL01);
  256. dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  257. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
  258. I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
  259. wr32(hw, I40E_VFINT_DYN_CTL01, dyn_ctl);
  260. }
  261. for (i = 1; i < adapter->num_msix_vectors; i++) {
  262. if (mask & BIT(i)) {
  263. dyn_ctl = rd32(hw, I40E_VFINT_DYN_CTLN1(i - 1));
  264. dyn_ctl |= I40E_VFINT_DYN_CTLN1_SWINT_TRIG_MASK |
  265. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK |
  266. I40E_VFINT_DYN_CTLN1_CLEARPBA_MASK;
  267. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), dyn_ctl);
  268. }
  269. }
  270. }
  271. /**
  272. * i40evf_irq_enable - Enable default interrupt generation settings
  273. * @adapter: board private structure
  274. * @flush: boolean value whether to run rd32()
  275. **/
  276. void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
  277. {
  278. struct i40e_hw *hw = &adapter->hw;
  279. i40evf_misc_irq_enable(adapter);
  280. i40evf_irq_enable_queues(adapter, ~0);
  281. if (flush)
  282. rd32(hw, I40E_VFGEN_RSTAT);
  283. }
  284. /**
  285. * i40evf_msix_aq - Interrupt handler for vector 0
  286. * @irq: interrupt number
  287. * @data: pointer to netdev
  288. **/
  289. static irqreturn_t i40evf_msix_aq(int irq, void *data)
  290. {
  291. struct net_device *netdev = data;
  292. struct i40evf_adapter *adapter = netdev_priv(netdev);
  293. struct i40e_hw *hw = &adapter->hw;
  294. u32 val;
  295. /* handle non-queue interrupts, these reads clear the registers */
  296. val = rd32(hw, I40E_VFINT_ICR01);
  297. val = rd32(hw, I40E_VFINT_ICR0_ENA1);
  298. val = rd32(hw, I40E_VFINT_DYN_CTL01) |
  299. I40E_VFINT_DYN_CTL01_CLEARPBA_MASK;
  300. wr32(hw, I40E_VFINT_DYN_CTL01, val);
  301. /* schedule work on the private workqueue */
  302. schedule_work(&adapter->adminq_task);
  303. return IRQ_HANDLED;
  304. }
  305. /**
  306. * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
  307. * @irq: interrupt number
  308. * @data: pointer to a q_vector
  309. **/
  310. static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
  311. {
  312. struct i40e_q_vector *q_vector = data;
  313. if (!q_vector->tx.ring && !q_vector->rx.ring)
  314. return IRQ_HANDLED;
  315. napi_schedule_irqoff(&q_vector->napi);
  316. return IRQ_HANDLED;
  317. }
  318. /**
  319. * i40evf_map_vector_to_rxq - associate irqs with rx queues
  320. * @adapter: board private structure
  321. * @v_idx: interrupt number
  322. * @r_idx: queue number
  323. **/
  324. static void
  325. i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
  326. {
  327. struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
  328. struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
  329. struct i40e_hw *hw = &adapter->hw;
  330. rx_ring->q_vector = q_vector;
  331. rx_ring->next = q_vector->rx.ring;
  332. rx_ring->vsi = &adapter->vsi;
  333. q_vector->rx.ring = rx_ring;
  334. q_vector->rx.count++;
  335. q_vector->rx.latency_range = I40E_LOW_LATENCY;
  336. q_vector->rx.itr = ITR_TO_REG(rx_ring->rx_itr_setting);
  337. q_vector->ring_mask |= BIT(r_idx);
  338. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  339. wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, v_idx - 1), q_vector->rx.itr);
  340. }
  341. /**
  342. * i40evf_map_vector_to_txq - associate irqs with tx queues
  343. * @adapter: board private structure
  344. * @v_idx: interrupt number
  345. * @t_idx: queue number
  346. **/
  347. static void
  348. i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
  349. {
  350. struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
  351. struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
  352. struct i40e_hw *hw = &adapter->hw;
  353. tx_ring->q_vector = q_vector;
  354. tx_ring->next = q_vector->tx.ring;
  355. tx_ring->vsi = &adapter->vsi;
  356. q_vector->tx.ring = tx_ring;
  357. q_vector->tx.count++;
  358. q_vector->tx.latency_range = I40E_LOW_LATENCY;
  359. q_vector->tx.itr = ITR_TO_REG(tx_ring->tx_itr_setting);
  360. q_vector->itr_countdown = ITR_COUNTDOWN_START;
  361. q_vector->num_ringpairs++;
  362. wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, v_idx - 1), q_vector->tx.itr);
  363. }
  364. /**
  365. * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
  366. * @adapter: board private structure to initialize
  367. *
  368. * This function maps descriptor rings to the queue-specific vectors
  369. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  370. * one vector per ring/queue, but on a constrained vector budget, we
  371. * group the rings as "efficiently" as possible. You would add new
  372. * mapping configurations in here.
  373. **/
  374. static int i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
  375. {
  376. int q_vectors;
  377. int v_start = 0;
  378. int rxr_idx = 0, txr_idx = 0;
  379. int rxr_remaining = adapter->num_active_queues;
  380. int txr_remaining = adapter->num_active_queues;
  381. int i, j;
  382. int rqpv, tqpv;
  383. int err = 0;
  384. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  385. /* The ideal configuration...
  386. * We have enough vectors to map one per queue.
  387. */
  388. if (q_vectors >= (rxr_remaining * 2)) {
  389. for (; rxr_idx < rxr_remaining; v_start++, rxr_idx++)
  390. i40evf_map_vector_to_rxq(adapter, v_start, rxr_idx);
  391. for (; txr_idx < txr_remaining; v_start++, txr_idx++)
  392. i40evf_map_vector_to_txq(adapter, v_start, txr_idx);
  393. goto out;
  394. }
  395. /* If we don't have enough vectors for a 1-to-1
  396. * mapping, we'll have to group them so there are
  397. * multiple queues per vector.
  398. * Re-adjusting *qpv takes care of the remainder.
  399. */
  400. for (i = v_start; i < q_vectors; i++) {
  401. rqpv = DIV_ROUND_UP(rxr_remaining, q_vectors - i);
  402. for (j = 0; j < rqpv; j++) {
  403. i40evf_map_vector_to_rxq(adapter, i, rxr_idx);
  404. rxr_idx++;
  405. rxr_remaining--;
  406. }
  407. }
  408. for (i = v_start; i < q_vectors; i++) {
  409. tqpv = DIV_ROUND_UP(txr_remaining, q_vectors - i);
  410. for (j = 0; j < tqpv; j++) {
  411. i40evf_map_vector_to_txq(adapter, i, txr_idx);
  412. txr_idx++;
  413. txr_remaining--;
  414. }
  415. }
  416. out:
  417. adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
  418. return err;
  419. }
  420. #ifdef CONFIG_NET_POLL_CONTROLLER
  421. /**
  422. * i40evf_netpoll - A Polling 'interrupt' handler
  423. * @netdev: network interface device structure
  424. *
  425. * This is used by netconsole to send skbs without having to re-enable
  426. * interrupts. It's not called while the normal interrupt routine is executing.
  427. **/
  428. static void i40evf_netpoll(struct net_device *netdev)
  429. {
  430. struct i40evf_adapter *adapter = netdev_priv(netdev);
  431. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  432. int i;
  433. /* if interface is down do nothing */
  434. if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
  435. return;
  436. for (i = 0; i < q_vectors; i++)
  437. i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
  438. }
  439. #endif
  440. /**
  441. * i40evf_irq_affinity_notify - Callback for affinity changes
  442. * @notify: context as to what irq was changed
  443. * @mask: the new affinity mask
  444. *
  445. * This is a callback function used by the irq_set_affinity_notifier function
  446. * so that we may register to receive changes to the irq affinity masks.
  447. **/
  448. static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
  449. const cpumask_t *mask)
  450. {
  451. struct i40e_q_vector *q_vector =
  452. container_of(notify, struct i40e_q_vector, affinity_notify);
  453. cpumask_copy(&q_vector->affinity_mask, mask);
  454. }
  455. /**
  456. * i40evf_irq_affinity_release - Callback for affinity notifier release
  457. * @ref: internal core kernel usage
  458. *
  459. * This is a callback function used by the irq_set_affinity_notifier function
  460. * to inform the current notification subscriber that they will no longer
  461. * receive notifications.
  462. **/
  463. static void i40evf_irq_affinity_release(struct kref *ref) {}
  464. /**
  465. * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
  466. * @adapter: board private structure
  467. *
  468. * Allocates MSI-X vectors for tx and rx handling, and requests
  469. * interrupts from the kernel.
  470. **/
  471. static int
  472. i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
  473. {
  474. unsigned int vector, q_vectors;
  475. unsigned int rx_int_idx = 0, tx_int_idx = 0;
  476. int irq_num, err;
  477. i40evf_irq_disable(adapter);
  478. /* Decrement for Other and TCP Timer vectors */
  479. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  480. for (vector = 0; vector < q_vectors; vector++) {
  481. struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
  482. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  483. if (q_vector->tx.ring && q_vector->rx.ring) {
  484. snprintf(q_vector->name, sizeof(q_vector->name),
  485. "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
  486. tx_int_idx++;
  487. } else if (q_vector->rx.ring) {
  488. snprintf(q_vector->name, sizeof(q_vector->name),
  489. "i40evf-%s-rx-%d", basename, rx_int_idx++);
  490. } else if (q_vector->tx.ring) {
  491. snprintf(q_vector->name, sizeof(q_vector->name),
  492. "i40evf-%s-tx-%d", basename, tx_int_idx++);
  493. } else {
  494. /* skip this unused q_vector */
  495. continue;
  496. }
  497. err = request_irq(irq_num,
  498. i40evf_msix_clean_rings,
  499. 0,
  500. q_vector->name,
  501. q_vector);
  502. if (err) {
  503. dev_info(&adapter->pdev->dev,
  504. "Request_irq failed, error: %d\n", err);
  505. goto free_queue_irqs;
  506. }
  507. /* register for affinity change notifications */
  508. q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
  509. q_vector->affinity_notify.release =
  510. i40evf_irq_affinity_release;
  511. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  512. /* get_cpu_mask returns a static constant mask with
  513. * a permanent lifetime so it's ok to use here.
  514. */
  515. irq_set_affinity_hint(irq_num, get_cpu_mask(q_vector->v_idx));
  516. }
  517. return 0;
  518. free_queue_irqs:
  519. while (vector) {
  520. vector--;
  521. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  522. irq_set_affinity_notifier(irq_num, NULL);
  523. irq_set_affinity_hint(irq_num, NULL);
  524. free_irq(irq_num, &adapter->q_vectors[vector]);
  525. }
  526. return err;
  527. }
  528. /**
  529. * i40evf_request_misc_irq - Initialize MSI-X interrupts
  530. * @adapter: board private structure
  531. *
  532. * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
  533. * vector is only for the admin queue, and stays active even when the netdev
  534. * is closed.
  535. **/
  536. static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
  537. {
  538. struct net_device *netdev = adapter->netdev;
  539. int err;
  540. snprintf(adapter->misc_vector_name,
  541. sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
  542. dev_name(&adapter->pdev->dev));
  543. err = request_irq(adapter->msix_entries[0].vector,
  544. &i40evf_msix_aq, 0,
  545. adapter->misc_vector_name, netdev);
  546. if (err) {
  547. dev_err(&adapter->pdev->dev,
  548. "request_irq for %s failed: %d\n",
  549. adapter->misc_vector_name, err);
  550. free_irq(adapter->msix_entries[0].vector, netdev);
  551. }
  552. return err;
  553. }
  554. /**
  555. * i40evf_free_traffic_irqs - Free MSI-X interrupts
  556. * @adapter: board private structure
  557. *
  558. * Frees all MSI-X vectors other than 0.
  559. **/
  560. static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
  561. {
  562. int vector, irq_num, q_vectors;
  563. if (!adapter->msix_entries)
  564. return;
  565. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  566. for (vector = 0; vector < q_vectors; vector++) {
  567. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  568. irq_set_affinity_notifier(irq_num, NULL);
  569. irq_set_affinity_hint(irq_num, NULL);
  570. free_irq(irq_num, &adapter->q_vectors[vector]);
  571. }
  572. }
  573. /**
  574. * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
  575. * @adapter: board private structure
  576. *
  577. * Frees MSI-X vector 0.
  578. **/
  579. static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
  580. {
  581. struct net_device *netdev = adapter->netdev;
  582. if (!adapter->msix_entries)
  583. return;
  584. free_irq(adapter->msix_entries[0].vector, netdev);
  585. }
  586. /**
  587. * i40evf_configure_tx - Configure Transmit Unit after Reset
  588. * @adapter: board private structure
  589. *
  590. * Configure the Tx unit of the MAC after a reset.
  591. **/
  592. static void i40evf_configure_tx(struct i40evf_adapter *adapter)
  593. {
  594. struct i40e_hw *hw = &adapter->hw;
  595. int i;
  596. for (i = 0; i < adapter->num_active_queues; i++)
  597. adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
  598. }
  599. /**
  600. * i40evf_configure_rx - Configure Receive Unit after Reset
  601. * @adapter: board private structure
  602. *
  603. * Configure the Rx unit of the MAC after a reset.
  604. **/
  605. static void i40evf_configure_rx(struct i40evf_adapter *adapter)
  606. {
  607. unsigned int rx_buf_len = I40E_RXBUFFER_2048;
  608. struct i40e_hw *hw = &adapter->hw;
  609. int i;
  610. /* Legacy Rx will always default to a 2048 buffer size. */
  611. #if (PAGE_SIZE < 8192)
  612. if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
  613. struct net_device *netdev = adapter->netdev;
  614. /* For jumbo frames on systems with 4K pages we have to use
  615. * an order 1 page, so we might as well increase the size
  616. * of our Rx buffer to make better use of the available space
  617. */
  618. rx_buf_len = I40E_RXBUFFER_3072;
  619. /* We use a 1536 buffer size for configurations with
  620. * standard Ethernet mtu. On x86 this gives us enough room
  621. * for shared info and 192 bytes of padding.
  622. */
  623. if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  624. (netdev->mtu <= ETH_DATA_LEN))
  625. rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  626. }
  627. #endif
  628. for (i = 0; i < adapter->num_active_queues; i++) {
  629. adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
  630. adapter->rx_rings[i].rx_buf_len = rx_buf_len;
  631. if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
  632. clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
  633. else
  634. set_ring_build_skb_enabled(&adapter->rx_rings[i]);
  635. }
  636. }
  637. /**
  638. * i40evf_find_vlan - Search filter list for specific vlan filter
  639. * @adapter: board private structure
  640. * @vlan: vlan tag
  641. *
  642. * Returns ptr to the filter object or NULL
  643. **/
  644. static struct
  645. i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
  646. {
  647. struct i40evf_vlan_filter *f;
  648. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  649. if (vlan == f->vlan)
  650. return f;
  651. }
  652. return NULL;
  653. }
  654. /**
  655. * i40evf_add_vlan - Add a vlan filter to the list
  656. * @adapter: board private structure
  657. * @vlan: VLAN tag
  658. *
  659. * Returns ptr to the filter object or NULL when no memory available.
  660. **/
  661. static struct
  662. i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
  663. {
  664. struct i40evf_vlan_filter *f = NULL;
  665. int count = 50;
  666. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  667. &adapter->crit_section)) {
  668. udelay(1);
  669. if (--count == 0)
  670. goto out;
  671. }
  672. f = i40evf_find_vlan(adapter, vlan);
  673. if (!f) {
  674. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  675. if (!f)
  676. goto clearout;
  677. f->vlan = vlan;
  678. INIT_LIST_HEAD(&f->list);
  679. list_add(&f->list, &adapter->vlan_filter_list);
  680. f->add = true;
  681. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  682. }
  683. clearout:
  684. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  685. out:
  686. return f;
  687. }
  688. /**
  689. * i40evf_del_vlan - Remove a vlan filter from the list
  690. * @adapter: board private structure
  691. * @vlan: VLAN tag
  692. **/
  693. static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
  694. {
  695. struct i40evf_vlan_filter *f;
  696. int count = 50;
  697. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  698. &adapter->crit_section)) {
  699. udelay(1);
  700. if (--count == 0)
  701. return;
  702. }
  703. f = i40evf_find_vlan(adapter, vlan);
  704. if (f) {
  705. f->remove = true;
  706. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  707. }
  708. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  709. }
  710. /**
  711. * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
  712. * @netdev: network device struct
  713. * @vid: VLAN tag
  714. **/
  715. static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
  716. __always_unused __be16 proto, u16 vid)
  717. {
  718. struct i40evf_adapter *adapter = netdev_priv(netdev);
  719. if (!VLAN_ALLOWED(adapter))
  720. return -EIO;
  721. if (i40evf_add_vlan(adapter, vid) == NULL)
  722. return -ENOMEM;
  723. return 0;
  724. }
  725. /**
  726. * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
  727. * @netdev: network device struct
  728. * @vid: VLAN tag
  729. **/
  730. static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
  731. __always_unused __be16 proto, u16 vid)
  732. {
  733. struct i40evf_adapter *adapter = netdev_priv(netdev);
  734. if (VLAN_ALLOWED(adapter)) {
  735. i40evf_del_vlan(adapter, vid);
  736. return 0;
  737. }
  738. return -EIO;
  739. }
  740. /**
  741. * i40evf_find_filter - Search filter list for specific mac filter
  742. * @adapter: board private structure
  743. * @macaddr: the MAC address
  744. *
  745. * Returns ptr to the filter object or NULL
  746. **/
  747. static struct
  748. i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
  749. u8 *macaddr)
  750. {
  751. struct i40evf_mac_filter *f;
  752. if (!macaddr)
  753. return NULL;
  754. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  755. if (ether_addr_equal(macaddr, f->macaddr))
  756. return f;
  757. }
  758. return NULL;
  759. }
  760. /**
  761. * i40e_add_filter - Add a mac filter to the filter list
  762. * @adapter: board private structure
  763. * @macaddr: the MAC address
  764. *
  765. * Returns ptr to the filter object or NULL when no memory available.
  766. **/
  767. static struct
  768. i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
  769. u8 *macaddr)
  770. {
  771. struct i40evf_mac_filter *f;
  772. int count = 50;
  773. if (!macaddr)
  774. return NULL;
  775. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  776. &adapter->crit_section)) {
  777. udelay(1);
  778. if (--count == 0)
  779. return NULL;
  780. }
  781. f = i40evf_find_filter(adapter, macaddr);
  782. if (!f) {
  783. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  784. if (!f) {
  785. clear_bit(__I40EVF_IN_CRITICAL_TASK,
  786. &adapter->crit_section);
  787. return NULL;
  788. }
  789. ether_addr_copy(f->macaddr, macaddr);
  790. list_add_tail(&f->list, &adapter->mac_filter_list);
  791. f->add = true;
  792. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  793. }
  794. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  795. return f;
  796. }
  797. /**
  798. * i40evf_set_mac - NDO callback to set port mac address
  799. * @netdev: network interface device structure
  800. * @p: pointer to an address structure
  801. *
  802. * Returns 0 on success, negative on failure
  803. **/
  804. static int i40evf_set_mac(struct net_device *netdev, void *p)
  805. {
  806. struct i40evf_adapter *adapter = netdev_priv(netdev);
  807. struct i40e_hw *hw = &adapter->hw;
  808. struct i40evf_mac_filter *f;
  809. struct sockaddr *addr = p;
  810. if (!is_valid_ether_addr(addr->sa_data))
  811. return -EADDRNOTAVAIL;
  812. if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
  813. return 0;
  814. if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
  815. return -EPERM;
  816. f = i40evf_find_filter(adapter, hw->mac.addr);
  817. if (f) {
  818. f->remove = true;
  819. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  820. }
  821. f = i40evf_add_filter(adapter, addr->sa_data);
  822. if (f) {
  823. ether_addr_copy(hw->mac.addr, addr->sa_data);
  824. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  825. }
  826. return (f == NULL) ? -ENOMEM : 0;
  827. }
  828. /**
  829. * i40evf_set_rx_mode - NDO callback to set the netdev filters
  830. * @netdev: network interface device structure
  831. **/
  832. static void i40evf_set_rx_mode(struct net_device *netdev)
  833. {
  834. struct i40evf_adapter *adapter = netdev_priv(netdev);
  835. struct i40evf_mac_filter *f, *ftmp;
  836. struct netdev_hw_addr *uca;
  837. struct netdev_hw_addr *mca;
  838. struct netdev_hw_addr *ha;
  839. int count = 50;
  840. /* add addr if not already in the filter list */
  841. netdev_for_each_uc_addr(uca, netdev) {
  842. i40evf_add_filter(adapter, uca->addr);
  843. }
  844. netdev_for_each_mc_addr(mca, netdev) {
  845. i40evf_add_filter(adapter, mca->addr);
  846. }
  847. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  848. &adapter->crit_section)) {
  849. udelay(1);
  850. if (--count == 0) {
  851. dev_err(&adapter->pdev->dev,
  852. "Failed to get lock in %s\n", __func__);
  853. return;
  854. }
  855. }
  856. /* remove filter if not in netdev list */
  857. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  858. netdev_for_each_mc_addr(mca, netdev)
  859. if (ether_addr_equal(mca->addr, f->macaddr))
  860. goto bottom_of_search_loop;
  861. netdev_for_each_uc_addr(uca, netdev)
  862. if (ether_addr_equal(uca->addr, f->macaddr))
  863. goto bottom_of_search_loop;
  864. for_each_dev_addr(netdev, ha)
  865. if (ether_addr_equal(ha->addr, f->macaddr))
  866. goto bottom_of_search_loop;
  867. if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
  868. goto bottom_of_search_loop;
  869. /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
  870. f->remove = true;
  871. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  872. bottom_of_search_loop:
  873. continue;
  874. }
  875. if (netdev->flags & IFF_PROMISC &&
  876. !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
  877. adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
  878. else if (!(netdev->flags & IFF_PROMISC) &&
  879. adapter->flags & I40EVF_FLAG_PROMISC_ON)
  880. adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
  881. if (netdev->flags & IFF_ALLMULTI &&
  882. !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
  883. adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
  884. else if (!(netdev->flags & IFF_ALLMULTI) &&
  885. adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
  886. adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
  887. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  888. }
  889. /**
  890. * i40evf_napi_enable_all - enable NAPI on all queue vectors
  891. * @adapter: board private structure
  892. **/
  893. static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
  894. {
  895. int q_idx;
  896. struct i40e_q_vector *q_vector;
  897. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  898. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  899. struct napi_struct *napi;
  900. q_vector = &adapter->q_vectors[q_idx];
  901. napi = &q_vector->napi;
  902. napi_enable(napi);
  903. }
  904. }
  905. /**
  906. * i40evf_napi_disable_all - disable NAPI on all queue vectors
  907. * @adapter: board private structure
  908. **/
  909. static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
  910. {
  911. int q_idx;
  912. struct i40e_q_vector *q_vector;
  913. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  914. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  915. q_vector = &adapter->q_vectors[q_idx];
  916. napi_disable(&q_vector->napi);
  917. }
  918. }
  919. /**
  920. * i40evf_configure - set up transmit and receive data structures
  921. * @adapter: board private structure
  922. **/
  923. static void i40evf_configure(struct i40evf_adapter *adapter)
  924. {
  925. struct net_device *netdev = adapter->netdev;
  926. int i;
  927. i40evf_set_rx_mode(netdev);
  928. i40evf_configure_tx(adapter);
  929. i40evf_configure_rx(adapter);
  930. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
  931. for (i = 0; i < adapter->num_active_queues; i++) {
  932. struct i40e_ring *ring = &adapter->rx_rings[i];
  933. i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  934. }
  935. }
  936. /**
  937. * i40evf_up_complete - Finish the last steps of bringing up a connection
  938. * @adapter: board private structure
  939. **/
  940. static void i40evf_up_complete(struct i40evf_adapter *adapter)
  941. {
  942. adapter->state = __I40EVF_RUNNING;
  943. clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  944. i40evf_napi_enable_all(adapter);
  945. adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
  946. if (CLIENT_ENABLED(adapter))
  947. adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
  948. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  949. }
  950. /**
  951. * i40e_down - Shutdown the connection processing
  952. * @adapter: board private structure
  953. **/
  954. void i40evf_down(struct i40evf_adapter *adapter)
  955. {
  956. struct net_device *netdev = adapter->netdev;
  957. struct i40evf_mac_filter *f;
  958. if (adapter->state <= __I40EVF_DOWN_PENDING)
  959. return;
  960. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  961. &adapter->crit_section))
  962. usleep_range(500, 1000);
  963. netif_carrier_off(netdev);
  964. netif_tx_disable(netdev);
  965. adapter->link_up = false;
  966. i40evf_napi_disable_all(adapter);
  967. i40evf_irq_disable(adapter);
  968. /* remove all MAC filters */
  969. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  970. f->remove = true;
  971. }
  972. /* remove all VLAN filters */
  973. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  974. f->remove = true;
  975. }
  976. if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
  977. adapter->state != __I40EVF_RESETTING) {
  978. /* cancel any current operation */
  979. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  980. /* Schedule operations to close down the HW. Don't wait
  981. * here for this to complete. The watchdog is still running
  982. * and it will take care of this.
  983. */
  984. adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  985. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  986. adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
  987. }
  988. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  989. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  990. }
  991. /**
  992. * i40evf_acquire_msix_vectors - Setup the MSIX capability
  993. * @adapter: board private structure
  994. * @vectors: number of vectors to request
  995. *
  996. * Work with the OS to set up the MSIX vectors needed.
  997. *
  998. * Returns 0 on success, negative on failure
  999. **/
  1000. static int
  1001. i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
  1002. {
  1003. int err, vector_threshold;
  1004. /* We'll want at least 3 (vector_threshold):
  1005. * 0) Other (Admin Queue and link, mostly)
  1006. * 1) TxQ[0] Cleanup
  1007. * 2) RxQ[0] Cleanup
  1008. */
  1009. vector_threshold = MIN_MSIX_COUNT;
  1010. /* The more we get, the more we will assign to Tx/Rx Cleanup
  1011. * for the separate queues...where Rx Cleanup >= Tx Cleanup.
  1012. * Right now, we simply care about how many we'll get; we'll
  1013. * set them up later while requesting irq's.
  1014. */
  1015. err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
  1016. vector_threshold, vectors);
  1017. if (err < 0) {
  1018. dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
  1019. kfree(adapter->msix_entries);
  1020. adapter->msix_entries = NULL;
  1021. return err;
  1022. }
  1023. /* Adjust for only the vectors we'll use, which is minimum
  1024. * of max_msix_q_vectors + NONQ_VECS, or the number of
  1025. * vectors we were allocated.
  1026. */
  1027. adapter->num_msix_vectors = err;
  1028. return 0;
  1029. }
  1030. /**
  1031. * i40evf_free_queues - Free memory for all rings
  1032. * @adapter: board private structure to initialize
  1033. *
  1034. * Free all of the memory associated with queue pairs.
  1035. **/
  1036. static void i40evf_free_queues(struct i40evf_adapter *adapter)
  1037. {
  1038. if (!adapter->vsi_res)
  1039. return;
  1040. adapter->num_active_queues = 0;
  1041. kfree(adapter->tx_rings);
  1042. adapter->tx_rings = NULL;
  1043. kfree(adapter->rx_rings);
  1044. adapter->rx_rings = NULL;
  1045. }
  1046. /**
  1047. * i40evf_alloc_queues - Allocate memory for all rings
  1048. * @adapter: board private structure to initialize
  1049. *
  1050. * We allocate one ring per queue at run-time since we don't know the
  1051. * number of queues at compile-time. The polling_netdev array is
  1052. * intended for Multiqueue, but should work fine with a single queue.
  1053. **/
  1054. static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
  1055. {
  1056. int i, num_active_queues;
  1057. num_active_queues = min_t(int,
  1058. adapter->vsi_res->num_queue_pairs,
  1059. (int)(num_online_cpus()));
  1060. adapter->tx_rings = kcalloc(num_active_queues,
  1061. sizeof(struct i40e_ring), GFP_KERNEL);
  1062. if (!adapter->tx_rings)
  1063. goto err_out;
  1064. adapter->rx_rings = kcalloc(num_active_queues,
  1065. sizeof(struct i40e_ring), GFP_KERNEL);
  1066. if (!adapter->rx_rings)
  1067. goto err_out;
  1068. for (i = 0; i < num_active_queues; i++) {
  1069. struct i40e_ring *tx_ring;
  1070. struct i40e_ring *rx_ring;
  1071. tx_ring = &adapter->tx_rings[i];
  1072. tx_ring->queue_index = i;
  1073. tx_ring->netdev = adapter->netdev;
  1074. tx_ring->dev = &adapter->pdev->dev;
  1075. tx_ring->count = adapter->tx_desc_count;
  1076. tx_ring->tx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_TX_DEF);
  1077. if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
  1078. tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
  1079. rx_ring = &adapter->rx_rings[i];
  1080. rx_ring->queue_index = i;
  1081. rx_ring->netdev = adapter->netdev;
  1082. rx_ring->dev = &adapter->pdev->dev;
  1083. rx_ring->count = adapter->rx_desc_count;
  1084. rx_ring->rx_itr_setting = (I40E_ITR_DYNAMIC | I40E_ITR_RX_DEF);
  1085. }
  1086. adapter->num_active_queues = num_active_queues;
  1087. return 0;
  1088. err_out:
  1089. i40evf_free_queues(adapter);
  1090. return -ENOMEM;
  1091. }
  1092. /**
  1093. * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
  1094. * @adapter: board private structure to initialize
  1095. *
  1096. * Attempt to configure the interrupts using the best available
  1097. * capabilities of the hardware and the kernel.
  1098. **/
  1099. static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
  1100. {
  1101. int vector, v_budget;
  1102. int pairs = 0;
  1103. int err = 0;
  1104. if (!adapter->vsi_res) {
  1105. err = -EIO;
  1106. goto out;
  1107. }
  1108. pairs = adapter->num_active_queues;
  1109. /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
  1110. * us much good if we have more vectors than CPUs. However, we already
  1111. * limit the total number of queues by the number of CPUs so we do not
  1112. * need any further limiting here.
  1113. */
  1114. v_budget = min_t(int, pairs + NONQ_VECS,
  1115. (int)adapter->vf_res->max_vectors);
  1116. adapter->msix_entries = kcalloc(v_budget,
  1117. sizeof(struct msix_entry), GFP_KERNEL);
  1118. if (!adapter->msix_entries) {
  1119. err = -ENOMEM;
  1120. goto out;
  1121. }
  1122. for (vector = 0; vector < v_budget; vector++)
  1123. adapter->msix_entries[vector].entry = vector;
  1124. err = i40evf_acquire_msix_vectors(adapter, v_budget);
  1125. out:
  1126. netif_set_real_num_rx_queues(adapter->netdev, pairs);
  1127. netif_set_real_num_tx_queues(adapter->netdev, pairs);
  1128. return err;
  1129. }
  1130. /**
  1131. * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
  1132. * @adapter: board private structure
  1133. *
  1134. * Return 0 on success, negative on failure
  1135. **/
  1136. static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
  1137. {
  1138. struct i40e_aqc_get_set_rss_key_data *rss_key =
  1139. (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
  1140. struct i40e_hw *hw = &adapter->hw;
  1141. int ret = 0;
  1142. if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
  1143. /* bail because we already have a command pending */
  1144. dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
  1145. adapter->current_op);
  1146. return -EBUSY;
  1147. }
  1148. ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
  1149. if (ret) {
  1150. dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
  1151. i40evf_stat_str(hw, ret),
  1152. i40evf_aq_str(hw, hw->aq.asq_last_status));
  1153. return ret;
  1154. }
  1155. ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
  1156. adapter->rss_lut, adapter->rss_lut_size);
  1157. if (ret) {
  1158. dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
  1159. i40evf_stat_str(hw, ret),
  1160. i40evf_aq_str(hw, hw->aq.asq_last_status));
  1161. }
  1162. return ret;
  1163. }
  1164. /**
  1165. * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
  1166. * @adapter: board private structure
  1167. *
  1168. * Returns 0 on success, negative on failure
  1169. **/
  1170. static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
  1171. {
  1172. struct i40e_hw *hw = &adapter->hw;
  1173. u32 *dw;
  1174. u16 i;
  1175. dw = (u32 *)adapter->rss_key;
  1176. for (i = 0; i <= adapter->rss_key_size / 4; i++)
  1177. wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
  1178. dw = (u32 *)adapter->rss_lut;
  1179. for (i = 0; i <= adapter->rss_lut_size / 4; i++)
  1180. wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
  1181. i40e_flush(hw);
  1182. return 0;
  1183. }
  1184. /**
  1185. * i40evf_config_rss - Configure RSS keys and lut
  1186. * @adapter: board private structure
  1187. *
  1188. * Returns 0 on success, negative on failure
  1189. **/
  1190. int i40evf_config_rss(struct i40evf_adapter *adapter)
  1191. {
  1192. if (RSS_PF(adapter)) {
  1193. adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
  1194. I40EVF_FLAG_AQ_SET_RSS_KEY;
  1195. return 0;
  1196. } else if (RSS_AQ(adapter)) {
  1197. return i40evf_config_rss_aq(adapter);
  1198. } else {
  1199. return i40evf_config_rss_reg(adapter);
  1200. }
  1201. }
  1202. /**
  1203. * i40evf_fill_rss_lut - Fill the lut with default values
  1204. * @adapter: board private structure
  1205. **/
  1206. static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
  1207. {
  1208. u16 i;
  1209. for (i = 0; i < adapter->rss_lut_size; i++)
  1210. adapter->rss_lut[i] = i % adapter->num_active_queues;
  1211. }
  1212. /**
  1213. * i40evf_init_rss - Prepare for RSS
  1214. * @adapter: board private structure
  1215. *
  1216. * Return 0 on success, negative on failure
  1217. **/
  1218. static int i40evf_init_rss(struct i40evf_adapter *adapter)
  1219. {
  1220. struct i40e_hw *hw = &adapter->hw;
  1221. int ret;
  1222. if (!RSS_PF(adapter)) {
  1223. /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
  1224. if (adapter->vf_res->vf_cap_flags &
  1225. VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1226. adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
  1227. else
  1228. adapter->hena = I40E_DEFAULT_RSS_HENA;
  1229. wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
  1230. wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
  1231. }
  1232. i40evf_fill_rss_lut(adapter);
  1233. netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
  1234. ret = i40evf_config_rss(adapter);
  1235. return ret;
  1236. }
  1237. /**
  1238. * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
  1239. * @adapter: board private structure to initialize
  1240. *
  1241. * We allocate one q_vector per queue interrupt. If allocation fails we
  1242. * return -ENOMEM.
  1243. **/
  1244. static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
  1245. {
  1246. int q_idx = 0, num_q_vectors;
  1247. struct i40e_q_vector *q_vector;
  1248. num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  1249. adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
  1250. GFP_KERNEL);
  1251. if (!adapter->q_vectors)
  1252. return -ENOMEM;
  1253. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1254. q_vector = &adapter->q_vectors[q_idx];
  1255. q_vector->adapter = adapter;
  1256. q_vector->vsi = &adapter->vsi;
  1257. q_vector->v_idx = q_idx;
  1258. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  1259. netif_napi_add(adapter->netdev, &q_vector->napi,
  1260. i40evf_napi_poll, NAPI_POLL_WEIGHT);
  1261. }
  1262. return 0;
  1263. }
  1264. /**
  1265. * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
  1266. * @adapter: board private structure to initialize
  1267. *
  1268. * This function frees the memory allocated to the q_vectors. In addition if
  1269. * NAPI is enabled it will delete any references to the NAPI struct prior
  1270. * to freeing the q_vector.
  1271. **/
  1272. static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
  1273. {
  1274. int q_idx, num_q_vectors;
  1275. int napi_vectors;
  1276. if (!adapter->q_vectors)
  1277. return;
  1278. num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  1279. napi_vectors = adapter->num_active_queues;
  1280. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1281. struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
  1282. if (q_idx < napi_vectors)
  1283. netif_napi_del(&q_vector->napi);
  1284. }
  1285. kfree(adapter->q_vectors);
  1286. adapter->q_vectors = NULL;
  1287. }
  1288. /**
  1289. * i40evf_reset_interrupt_capability - Reset MSIX setup
  1290. * @adapter: board private structure
  1291. *
  1292. **/
  1293. void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
  1294. {
  1295. if (!adapter->msix_entries)
  1296. return;
  1297. pci_disable_msix(adapter->pdev);
  1298. kfree(adapter->msix_entries);
  1299. adapter->msix_entries = NULL;
  1300. }
  1301. /**
  1302. * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
  1303. * @adapter: board private structure to initialize
  1304. *
  1305. **/
  1306. int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
  1307. {
  1308. int err;
  1309. err = i40evf_alloc_queues(adapter);
  1310. if (err) {
  1311. dev_err(&adapter->pdev->dev,
  1312. "Unable to allocate memory for queues\n");
  1313. goto err_alloc_queues;
  1314. }
  1315. rtnl_lock();
  1316. err = i40evf_set_interrupt_capability(adapter);
  1317. rtnl_unlock();
  1318. if (err) {
  1319. dev_err(&adapter->pdev->dev,
  1320. "Unable to setup interrupt capabilities\n");
  1321. goto err_set_interrupt;
  1322. }
  1323. err = i40evf_alloc_q_vectors(adapter);
  1324. if (err) {
  1325. dev_err(&adapter->pdev->dev,
  1326. "Unable to allocate memory for queue vectors\n");
  1327. goto err_alloc_q_vectors;
  1328. }
  1329. dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
  1330. (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
  1331. adapter->num_active_queues);
  1332. return 0;
  1333. err_alloc_q_vectors:
  1334. i40evf_reset_interrupt_capability(adapter);
  1335. err_set_interrupt:
  1336. i40evf_free_queues(adapter);
  1337. err_alloc_queues:
  1338. return err;
  1339. }
  1340. /**
  1341. * i40evf_free_rss - Free memory used by RSS structs
  1342. * @adapter: board private structure
  1343. **/
  1344. static void i40evf_free_rss(struct i40evf_adapter *adapter)
  1345. {
  1346. kfree(adapter->rss_key);
  1347. adapter->rss_key = NULL;
  1348. kfree(adapter->rss_lut);
  1349. adapter->rss_lut = NULL;
  1350. }
  1351. /**
  1352. * i40evf_watchdog_timer - Periodic call-back timer
  1353. * @data: pointer to adapter disguised as unsigned long
  1354. **/
  1355. static void i40evf_watchdog_timer(unsigned long data)
  1356. {
  1357. struct i40evf_adapter *adapter = (struct i40evf_adapter *)data;
  1358. schedule_work(&adapter->watchdog_task);
  1359. /* timer will be rescheduled in watchdog task */
  1360. }
  1361. /**
  1362. * i40evf_watchdog_task - Periodic call-back task
  1363. * @work: pointer to work_struct
  1364. **/
  1365. static void i40evf_watchdog_task(struct work_struct *work)
  1366. {
  1367. struct i40evf_adapter *adapter = container_of(work,
  1368. struct i40evf_adapter,
  1369. watchdog_task);
  1370. struct i40e_hw *hw = &adapter->hw;
  1371. u32 reg_val;
  1372. if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
  1373. goto restart_watchdog;
  1374. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
  1375. reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
  1376. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1377. if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
  1378. (reg_val == VIRTCHNL_VFR_COMPLETED)) {
  1379. /* A chance for redemption! */
  1380. dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
  1381. adapter->state = __I40EVF_STARTUP;
  1382. adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
  1383. schedule_delayed_work(&adapter->init_task, 10);
  1384. clear_bit(__I40EVF_IN_CRITICAL_TASK,
  1385. &adapter->crit_section);
  1386. /* Don't reschedule the watchdog, since we've restarted
  1387. * the init task. When init_task contacts the PF and
  1388. * gets everything set up again, it'll restart the
  1389. * watchdog for us. Down, boy. Sit. Stay. Woof.
  1390. */
  1391. return;
  1392. }
  1393. adapter->aq_required = 0;
  1394. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1395. goto watchdog_done;
  1396. }
  1397. if ((adapter->state < __I40EVF_DOWN) ||
  1398. (adapter->flags & I40EVF_FLAG_RESET_PENDING))
  1399. goto watchdog_done;
  1400. /* check for reset */
  1401. reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
  1402. if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
  1403. adapter->state = __I40EVF_RESETTING;
  1404. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  1405. dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
  1406. schedule_work(&adapter->reset_task);
  1407. adapter->aq_required = 0;
  1408. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1409. goto watchdog_done;
  1410. }
  1411. /* Process admin queue tasks. After init, everything gets done
  1412. * here so we don't race on the admin queue.
  1413. */
  1414. if (adapter->current_op) {
  1415. if (!i40evf_asq_done(hw)) {
  1416. dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
  1417. i40evf_send_api_ver(adapter);
  1418. }
  1419. goto watchdog_done;
  1420. }
  1421. if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
  1422. i40evf_send_vf_config_msg(adapter);
  1423. goto watchdog_done;
  1424. }
  1425. if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
  1426. i40evf_disable_queues(adapter);
  1427. goto watchdog_done;
  1428. }
  1429. if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
  1430. i40evf_map_queues(adapter);
  1431. goto watchdog_done;
  1432. }
  1433. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
  1434. i40evf_add_ether_addrs(adapter);
  1435. goto watchdog_done;
  1436. }
  1437. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
  1438. i40evf_add_vlans(adapter);
  1439. goto watchdog_done;
  1440. }
  1441. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
  1442. i40evf_del_ether_addrs(adapter);
  1443. goto watchdog_done;
  1444. }
  1445. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
  1446. i40evf_del_vlans(adapter);
  1447. goto watchdog_done;
  1448. }
  1449. if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
  1450. i40evf_enable_vlan_stripping(adapter);
  1451. goto watchdog_done;
  1452. }
  1453. if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
  1454. i40evf_disable_vlan_stripping(adapter);
  1455. goto watchdog_done;
  1456. }
  1457. if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
  1458. i40evf_configure_queues(adapter);
  1459. goto watchdog_done;
  1460. }
  1461. if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
  1462. i40evf_enable_queues(adapter);
  1463. goto watchdog_done;
  1464. }
  1465. if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
  1466. /* This message goes straight to the firmware, not the
  1467. * PF, so we don't have to set current_op as we will
  1468. * not get a response through the ARQ.
  1469. */
  1470. i40evf_init_rss(adapter);
  1471. adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
  1472. goto watchdog_done;
  1473. }
  1474. if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
  1475. i40evf_get_hena(adapter);
  1476. goto watchdog_done;
  1477. }
  1478. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
  1479. i40evf_set_hena(adapter);
  1480. goto watchdog_done;
  1481. }
  1482. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
  1483. i40evf_set_rss_key(adapter);
  1484. goto watchdog_done;
  1485. }
  1486. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
  1487. i40evf_set_rss_lut(adapter);
  1488. goto watchdog_done;
  1489. }
  1490. if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
  1491. i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
  1492. FLAG_VF_MULTICAST_PROMISC);
  1493. goto watchdog_done;
  1494. }
  1495. if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
  1496. i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
  1497. goto watchdog_done;
  1498. }
  1499. if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
  1500. (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
  1501. i40evf_set_promiscuous(adapter, 0);
  1502. goto watchdog_done;
  1503. }
  1504. schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
  1505. if (adapter->state == __I40EVF_RUNNING)
  1506. i40evf_request_stats(adapter);
  1507. watchdog_done:
  1508. if (adapter->state == __I40EVF_RUNNING) {
  1509. i40evf_irq_enable_queues(adapter, ~0);
  1510. i40evf_fire_sw_int(adapter, 0xFF);
  1511. } else {
  1512. i40evf_fire_sw_int(adapter, 0x1);
  1513. }
  1514. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1515. restart_watchdog:
  1516. if (adapter->state == __I40EVF_REMOVE)
  1517. return;
  1518. if (adapter->aq_required)
  1519. mod_timer(&adapter->watchdog_timer,
  1520. jiffies + msecs_to_jiffies(20));
  1521. else
  1522. mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
  1523. schedule_work(&adapter->adminq_task);
  1524. }
  1525. static void i40evf_disable_vf(struct i40evf_adapter *adapter)
  1526. {
  1527. struct i40evf_mac_filter *f, *ftmp;
  1528. struct i40evf_vlan_filter *fv, *fvtmp;
  1529. adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
  1530. if (netif_running(adapter->netdev)) {
  1531. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  1532. netif_carrier_off(adapter->netdev);
  1533. netif_tx_disable(adapter->netdev);
  1534. adapter->link_up = false;
  1535. i40evf_napi_disable_all(adapter);
  1536. i40evf_irq_disable(adapter);
  1537. i40evf_free_traffic_irqs(adapter);
  1538. i40evf_free_all_tx_resources(adapter);
  1539. i40evf_free_all_rx_resources(adapter);
  1540. }
  1541. /* Delete all of the filters, both MAC and VLAN. */
  1542. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  1543. list_del(&f->list);
  1544. kfree(f);
  1545. }
  1546. list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
  1547. list_del(&fv->list);
  1548. kfree(fv);
  1549. }
  1550. i40evf_free_misc_irq(adapter);
  1551. i40evf_reset_interrupt_capability(adapter);
  1552. i40evf_free_queues(adapter);
  1553. i40evf_free_q_vectors(adapter);
  1554. kfree(adapter->vf_res);
  1555. i40evf_shutdown_adminq(&adapter->hw);
  1556. adapter->netdev->flags &= ~IFF_UP;
  1557. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1558. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1559. adapter->state = __I40EVF_DOWN;
  1560. wake_up(&adapter->down_waitqueue);
  1561. dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
  1562. }
  1563. #define I40EVF_RESET_WAIT_MS 10
  1564. #define I40EVF_RESET_WAIT_COUNT 500
  1565. /**
  1566. * i40evf_reset_task - Call-back task to handle hardware reset
  1567. * @work: pointer to work_struct
  1568. *
  1569. * During reset we need to shut down and reinitialize the admin queue
  1570. * before we can use it to communicate with the PF again. We also clear
  1571. * and reinit the rings because that context is lost as well.
  1572. **/
  1573. static void i40evf_reset_task(struct work_struct *work)
  1574. {
  1575. struct i40evf_adapter *adapter = container_of(work,
  1576. struct i40evf_adapter,
  1577. reset_task);
  1578. struct net_device *netdev = adapter->netdev;
  1579. struct i40e_hw *hw = &adapter->hw;
  1580. struct i40evf_vlan_filter *vlf;
  1581. struct i40evf_mac_filter *f;
  1582. u32 reg_val;
  1583. int i = 0, err;
  1584. while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
  1585. &adapter->crit_section))
  1586. usleep_range(500, 1000);
  1587. if (CLIENT_ENABLED(adapter)) {
  1588. adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
  1589. I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
  1590. I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
  1591. I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
  1592. cancel_delayed_work_sync(&adapter->client_task);
  1593. i40evf_notify_client_close(&adapter->vsi, true);
  1594. }
  1595. i40evf_misc_irq_disable(adapter);
  1596. if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
  1597. adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
  1598. /* Restart the AQ here. If we have been reset but didn't
  1599. * detect it, or if the PF had to reinit, our AQ will be hosed.
  1600. */
  1601. i40evf_shutdown_adminq(hw);
  1602. i40evf_init_adminq(hw);
  1603. i40evf_request_reset(adapter);
  1604. }
  1605. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  1606. /* poll until we see the reset actually happen */
  1607. for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
  1608. reg_val = rd32(hw, I40E_VF_ARQLEN1) &
  1609. I40E_VF_ARQLEN1_ARQENABLE_MASK;
  1610. if (!reg_val)
  1611. break;
  1612. usleep_range(5000, 10000);
  1613. }
  1614. if (i == I40EVF_RESET_WAIT_COUNT) {
  1615. dev_info(&adapter->pdev->dev, "Never saw reset\n");
  1616. goto continue_reset; /* act like the reset happened */
  1617. }
  1618. /* wait until the reset is complete and the PF is responding to us */
  1619. for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
  1620. /* sleep first to make sure a minimum wait time is met */
  1621. msleep(I40EVF_RESET_WAIT_MS);
  1622. reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
  1623. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1624. if (reg_val == VIRTCHNL_VFR_VFACTIVE)
  1625. break;
  1626. }
  1627. pci_set_master(adapter->pdev);
  1628. if (i == I40EVF_RESET_WAIT_COUNT) {
  1629. dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
  1630. reg_val);
  1631. i40evf_disable_vf(adapter);
  1632. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1633. return; /* Do not attempt to reinit. It's dead, Jim. */
  1634. }
  1635. continue_reset:
  1636. if (netif_running(netdev)) {
  1637. netif_carrier_off(netdev);
  1638. netif_tx_stop_all_queues(netdev);
  1639. adapter->link_up = false;
  1640. i40evf_napi_disable_all(adapter);
  1641. }
  1642. i40evf_irq_disable(adapter);
  1643. adapter->state = __I40EVF_RESETTING;
  1644. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1645. /* free the Tx/Rx rings and descriptors, might be better to just
  1646. * re-use them sometime in the future
  1647. */
  1648. i40evf_free_all_rx_resources(adapter);
  1649. i40evf_free_all_tx_resources(adapter);
  1650. /* kill and reinit the admin queue */
  1651. i40evf_shutdown_adminq(hw);
  1652. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1653. err = i40evf_init_adminq(hw);
  1654. if (err)
  1655. dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
  1656. err);
  1657. adapter->aq_required = I40EVF_FLAG_AQ_GET_CONFIG;
  1658. adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
  1659. /* re-add all MAC filters */
  1660. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  1661. f->add = true;
  1662. }
  1663. /* re-add all VLAN filters */
  1664. list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
  1665. vlf->add = true;
  1666. }
  1667. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  1668. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  1669. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1670. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1671. i40evf_misc_irq_enable(adapter);
  1672. mod_timer(&adapter->watchdog_timer, jiffies + 2);
  1673. if (netif_running(adapter->netdev)) {
  1674. /* allocate transmit descriptors */
  1675. err = i40evf_setup_all_tx_resources(adapter);
  1676. if (err)
  1677. goto reset_err;
  1678. /* allocate receive descriptors */
  1679. err = i40evf_setup_all_rx_resources(adapter);
  1680. if (err)
  1681. goto reset_err;
  1682. i40evf_configure(adapter);
  1683. i40evf_up_complete(adapter);
  1684. i40evf_irq_enable(adapter, true);
  1685. } else {
  1686. adapter->state = __I40EVF_DOWN;
  1687. wake_up(&adapter->down_waitqueue);
  1688. }
  1689. return;
  1690. reset_err:
  1691. dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
  1692. i40evf_close(netdev);
  1693. }
  1694. /**
  1695. * i40evf_adminq_task - worker thread to clean the admin queue
  1696. * @work: pointer to work_struct containing our data
  1697. **/
  1698. static void i40evf_adminq_task(struct work_struct *work)
  1699. {
  1700. struct i40evf_adapter *adapter =
  1701. container_of(work, struct i40evf_adapter, adminq_task);
  1702. struct i40e_hw *hw = &adapter->hw;
  1703. struct i40e_arq_event_info event;
  1704. enum virtchnl_ops v_op;
  1705. i40e_status ret, v_ret;
  1706. u32 val, oldval;
  1707. u16 pending;
  1708. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
  1709. goto out;
  1710. event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
  1711. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  1712. if (!event.msg_buf)
  1713. goto out;
  1714. do {
  1715. ret = i40evf_clean_arq_element(hw, &event, &pending);
  1716. v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
  1717. v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
  1718. if (ret || !v_op)
  1719. break; /* No event to process or error cleaning ARQ */
  1720. i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
  1721. event.msg_len);
  1722. if (pending != 0)
  1723. memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
  1724. } while (pending);
  1725. if ((adapter->flags &
  1726. (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
  1727. adapter->state == __I40EVF_RESETTING)
  1728. goto freedom;
  1729. /* check for error indications */
  1730. val = rd32(hw, hw->aq.arq.len);
  1731. if (val == 0xdeadbeef) /* indicates device in reset */
  1732. goto freedom;
  1733. oldval = val;
  1734. if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
  1735. dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
  1736. val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
  1737. }
  1738. if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
  1739. dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
  1740. val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
  1741. }
  1742. if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
  1743. dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
  1744. val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
  1745. }
  1746. if (oldval != val)
  1747. wr32(hw, hw->aq.arq.len, val);
  1748. val = rd32(hw, hw->aq.asq.len);
  1749. oldval = val;
  1750. if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
  1751. dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
  1752. val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
  1753. }
  1754. if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
  1755. dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
  1756. val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
  1757. }
  1758. if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
  1759. dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
  1760. val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
  1761. }
  1762. if (oldval != val)
  1763. wr32(hw, hw->aq.asq.len, val);
  1764. freedom:
  1765. kfree(event.msg_buf);
  1766. out:
  1767. /* re-enable Admin queue interrupt cause */
  1768. i40evf_misc_irq_enable(adapter);
  1769. }
  1770. /**
  1771. * i40evf_client_task - worker thread to perform client work
  1772. * @work: pointer to work_struct containing our data
  1773. *
  1774. * This task handles client interactions. Because client calls can be
  1775. * reentrant, we can't handle them in the watchdog.
  1776. **/
  1777. static void i40evf_client_task(struct work_struct *work)
  1778. {
  1779. struct i40evf_adapter *adapter =
  1780. container_of(work, struct i40evf_adapter, client_task.work);
  1781. /* If we can't get the client bit, just give up. We'll be rescheduled
  1782. * later.
  1783. */
  1784. if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
  1785. return;
  1786. if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
  1787. i40evf_client_subtask(adapter);
  1788. adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
  1789. goto out;
  1790. }
  1791. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
  1792. i40evf_notify_client_close(&adapter->vsi, false);
  1793. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
  1794. goto out;
  1795. }
  1796. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
  1797. i40evf_notify_client_open(&adapter->vsi);
  1798. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
  1799. goto out;
  1800. }
  1801. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
  1802. i40evf_notify_client_l2_params(&adapter->vsi);
  1803. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
  1804. }
  1805. out:
  1806. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1807. }
  1808. /**
  1809. * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
  1810. * @adapter: board private structure
  1811. *
  1812. * Free all transmit software resources
  1813. **/
  1814. void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
  1815. {
  1816. int i;
  1817. if (!adapter->tx_rings)
  1818. return;
  1819. for (i = 0; i < adapter->num_active_queues; i++)
  1820. if (adapter->tx_rings[i].desc)
  1821. i40evf_free_tx_resources(&adapter->tx_rings[i]);
  1822. }
  1823. /**
  1824. * i40evf_setup_all_tx_resources - allocate all queues Tx resources
  1825. * @adapter: board private structure
  1826. *
  1827. * If this function returns with an error, then it's possible one or
  1828. * more of the rings is populated (while the rest are not). It is the
  1829. * callers duty to clean those orphaned rings.
  1830. *
  1831. * Return 0 on success, negative on failure
  1832. **/
  1833. static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
  1834. {
  1835. int i, err = 0;
  1836. for (i = 0; i < adapter->num_active_queues; i++) {
  1837. adapter->tx_rings[i].count = adapter->tx_desc_count;
  1838. err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
  1839. if (!err)
  1840. continue;
  1841. dev_err(&adapter->pdev->dev,
  1842. "Allocation for Tx Queue %u failed\n", i);
  1843. break;
  1844. }
  1845. return err;
  1846. }
  1847. /**
  1848. * i40evf_setup_all_rx_resources - allocate all queues Rx resources
  1849. * @adapter: board private structure
  1850. *
  1851. * If this function returns with an error, then it's possible one or
  1852. * more of the rings is populated (while the rest are not). It is the
  1853. * callers duty to clean those orphaned rings.
  1854. *
  1855. * Return 0 on success, negative on failure
  1856. **/
  1857. static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
  1858. {
  1859. int i, err = 0;
  1860. for (i = 0; i < adapter->num_active_queues; i++) {
  1861. adapter->rx_rings[i].count = adapter->rx_desc_count;
  1862. err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
  1863. if (!err)
  1864. continue;
  1865. dev_err(&adapter->pdev->dev,
  1866. "Allocation for Rx Queue %u failed\n", i);
  1867. break;
  1868. }
  1869. return err;
  1870. }
  1871. /**
  1872. * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
  1873. * @adapter: board private structure
  1874. *
  1875. * Free all receive software resources
  1876. **/
  1877. void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
  1878. {
  1879. int i;
  1880. if (!adapter->rx_rings)
  1881. return;
  1882. for (i = 0; i < adapter->num_active_queues; i++)
  1883. if (adapter->rx_rings[i].desc)
  1884. i40evf_free_rx_resources(&adapter->rx_rings[i]);
  1885. }
  1886. /**
  1887. * i40evf_open - Called when a network interface is made active
  1888. * @netdev: network interface device structure
  1889. *
  1890. * Returns 0 on success, negative value on failure
  1891. *
  1892. * The open entry point is called when a network interface is made
  1893. * active by the system (IFF_UP). At this point all resources needed
  1894. * for transmit and receive operations are allocated, the interrupt
  1895. * handler is registered with the OS, the watchdog timer is started,
  1896. * and the stack is notified that the interface is ready.
  1897. **/
  1898. static int i40evf_open(struct net_device *netdev)
  1899. {
  1900. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1901. int err;
  1902. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
  1903. dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
  1904. return -EIO;
  1905. }
  1906. if (adapter->state != __I40EVF_DOWN)
  1907. return -EBUSY;
  1908. /* allocate transmit descriptors */
  1909. err = i40evf_setup_all_tx_resources(adapter);
  1910. if (err)
  1911. goto err_setup_tx;
  1912. /* allocate receive descriptors */
  1913. err = i40evf_setup_all_rx_resources(adapter);
  1914. if (err)
  1915. goto err_setup_rx;
  1916. /* clear any pending interrupts, may auto mask */
  1917. err = i40evf_request_traffic_irqs(adapter, netdev->name);
  1918. if (err)
  1919. goto err_req_irq;
  1920. i40evf_add_filter(adapter, adapter->hw.mac.addr);
  1921. i40evf_configure(adapter);
  1922. i40evf_up_complete(adapter);
  1923. i40evf_irq_enable(adapter, true);
  1924. return 0;
  1925. err_req_irq:
  1926. i40evf_down(adapter);
  1927. i40evf_free_traffic_irqs(adapter);
  1928. err_setup_rx:
  1929. i40evf_free_all_rx_resources(adapter);
  1930. err_setup_tx:
  1931. i40evf_free_all_tx_resources(adapter);
  1932. return err;
  1933. }
  1934. /**
  1935. * i40evf_close - Disables a network interface
  1936. * @netdev: network interface device structure
  1937. *
  1938. * Returns 0, this is not allowed to fail
  1939. *
  1940. * The close entry point is called when an interface is de-activated
  1941. * by the OS. The hardware is still under the drivers control, but
  1942. * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
  1943. * are freed, along with all transmit and receive resources.
  1944. **/
  1945. static int i40evf_close(struct net_device *netdev)
  1946. {
  1947. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1948. int status;
  1949. if (adapter->state <= __I40EVF_DOWN_PENDING)
  1950. return 0;
  1951. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  1952. if (CLIENT_ENABLED(adapter))
  1953. adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
  1954. i40evf_down(adapter);
  1955. adapter->state = __I40EVF_DOWN_PENDING;
  1956. i40evf_free_traffic_irqs(adapter);
  1957. /* We explicitly don't free resources here because the hardware is
  1958. * still active and can DMA into memory. Resources are cleared in
  1959. * i40evf_virtchnl_completion() after we get confirmation from the PF
  1960. * driver that the rings have been stopped.
  1961. *
  1962. * Also, we wait for state to transition to __I40EVF_DOWN before
  1963. * returning. State change occurs in i40evf_virtchnl_completion() after
  1964. * VF resources are released (which occurs after PF driver processes and
  1965. * responds to admin queue commands).
  1966. */
  1967. status = wait_event_timeout(adapter->down_waitqueue,
  1968. adapter->state == __I40EVF_DOWN,
  1969. msecs_to_jiffies(200));
  1970. if (!status)
  1971. netdev_warn(netdev, "Device resources not yet released\n");
  1972. return 0;
  1973. }
  1974. /**
  1975. * i40evf_change_mtu - Change the Maximum Transfer Unit
  1976. * @netdev: network interface device structure
  1977. * @new_mtu: new value for maximum frame size
  1978. *
  1979. * Returns 0 on success, negative on failure
  1980. **/
  1981. static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
  1982. {
  1983. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1984. netdev->mtu = new_mtu;
  1985. if (CLIENT_ENABLED(adapter)) {
  1986. i40evf_notify_client_l2_params(&adapter->vsi);
  1987. adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
  1988. }
  1989. adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
  1990. schedule_work(&adapter->reset_task);
  1991. return 0;
  1992. }
  1993. /**
  1994. * i40e_set_features - set the netdev feature flags
  1995. * @netdev: ptr to the netdev being adjusted
  1996. * @features: the feature set that the stack is suggesting
  1997. * Note: expects to be called while under rtnl_lock()
  1998. **/
  1999. static int i40evf_set_features(struct net_device *netdev,
  2000. netdev_features_t features)
  2001. {
  2002. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2003. if (!VLAN_ALLOWED(adapter))
  2004. return -EINVAL;
  2005. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2006. adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
  2007. else
  2008. adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
  2009. return 0;
  2010. }
  2011. /**
  2012. * i40evf_features_check - Validate encapsulated packet conforms to limits
  2013. * @skb: skb buff
  2014. * @netdev: This physical port's netdev
  2015. * @features: Offload features that the stack believes apply
  2016. **/
  2017. static netdev_features_t i40evf_features_check(struct sk_buff *skb,
  2018. struct net_device *dev,
  2019. netdev_features_t features)
  2020. {
  2021. size_t len;
  2022. /* No point in doing any of this if neither checksum nor GSO are
  2023. * being requested for this frame. We can rule out both by just
  2024. * checking for CHECKSUM_PARTIAL
  2025. */
  2026. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2027. return features;
  2028. /* We cannot support GSO if the MSS is going to be less than
  2029. * 64 bytes. If it is then we need to drop support for GSO.
  2030. */
  2031. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  2032. features &= ~NETIF_F_GSO_MASK;
  2033. /* MACLEN can support at most 63 words */
  2034. len = skb_network_header(skb) - skb->data;
  2035. if (len & ~(63 * 2))
  2036. goto out_err;
  2037. /* IPLEN and EIPLEN can support at most 127 dwords */
  2038. len = skb_transport_header(skb) - skb_network_header(skb);
  2039. if (len & ~(127 * 4))
  2040. goto out_err;
  2041. if (skb->encapsulation) {
  2042. /* L4TUNLEN can support 127 words */
  2043. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  2044. if (len & ~(127 * 2))
  2045. goto out_err;
  2046. /* IPLEN can support at most 127 dwords */
  2047. len = skb_inner_transport_header(skb) -
  2048. skb_inner_network_header(skb);
  2049. if (len & ~(127 * 4))
  2050. goto out_err;
  2051. }
  2052. /* No need to validate L4LEN as TCP is the only protocol with a
  2053. * a flexible value and we support all possible values supported
  2054. * by TCP, which is at most 15 dwords
  2055. */
  2056. return features;
  2057. out_err:
  2058. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2059. }
  2060. #define I40EVF_VLAN_FEATURES (NETIF_F_HW_VLAN_CTAG_TX |\
  2061. NETIF_F_HW_VLAN_CTAG_RX |\
  2062. NETIF_F_HW_VLAN_CTAG_FILTER)
  2063. /**
  2064. * i40evf_fix_features - fix up the netdev feature bits
  2065. * @netdev: our net device
  2066. * @features: desired feature bits
  2067. *
  2068. * Returns fixed-up features bits
  2069. **/
  2070. static netdev_features_t i40evf_fix_features(struct net_device *netdev,
  2071. netdev_features_t features)
  2072. {
  2073. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2074. features &= ~I40EVF_VLAN_FEATURES;
  2075. if (adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
  2076. features |= I40EVF_VLAN_FEATURES;
  2077. return features;
  2078. }
  2079. static const struct net_device_ops i40evf_netdev_ops = {
  2080. .ndo_open = i40evf_open,
  2081. .ndo_stop = i40evf_close,
  2082. .ndo_start_xmit = i40evf_xmit_frame,
  2083. .ndo_set_rx_mode = i40evf_set_rx_mode,
  2084. .ndo_validate_addr = eth_validate_addr,
  2085. .ndo_set_mac_address = i40evf_set_mac,
  2086. .ndo_change_mtu = i40evf_change_mtu,
  2087. .ndo_tx_timeout = i40evf_tx_timeout,
  2088. .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
  2089. .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
  2090. .ndo_features_check = i40evf_features_check,
  2091. .ndo_fix_features = i40evf_fix_features,
  2092. .ndo_set_features = i40evf_set_features,
  2093. #ifdef CONFIG_NET_POLL_CONTROLLER
  2094. .ndo_poll_controller = i40evf_netpoll,
  2095. #endif
  2096. };
  2097. /**
  2098. * i40evf_check_reset_complete - check that VF reset is complete
  2099. * @hw: pointer to hw struct
  2100. *
  2101. * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
  2102. **/
  2103. static int i40evf_check_reset_complete(struct i40e_hw *hw)
  2104. {
  2105. u32 rstat;
  2106. int i;
  2107. for (i = 0; i < 100; i++) {
  2108. rstat = rd32(hw, I40E_VFGEN_RSTAT) &
  2109. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  2110. if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
  2111. (rstat == VIRTCHNL_VFR_COMPLETED))
  2112. return 0;
  2113. usleep_range(10, 20);
  2114. }
  2115. return -EBUSY;
  2116. }
  2117. /**
  2118. * i40evf_process_config - Process the config information we got from the PF
  2119. * @adapter: board private structure
  2120. *
  2121. * Verify that we have a valid config struct, and set up our netdev features
  2122. * and our VSI struct.
  2123. **/
  2124. int i40evf_process_config(struct i40evf_adapter *adapter)
  2125. {
  2126. struct virtchnl_vf_resource *vfres = adapter->vf_res;
  2127. struct net_device *netdev = adapter->netdev;
  2128. struct i40e_vsi *vsi = &adapter->vsi;
  2129. int i;
  2130. netdev_features_t hw_enc_features;
  2131. netdev_features_t hw_features;
  2132. /* got VF config message back from PF, now we can parse it */
  2133. for (i = 0; i < vfres->num_vsis; i++) {
  2134. if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
  2135. adapter->vsi_res = &vfres->vsi_res[i];
  2136. }
  2137. if (!adapter->vsi_res) {
  2138. dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
  2139. return -ENODEV;
  2140. }
  2141. hw_enc_features = NETIF_F_SG |
  2142. NETIF_F_IP_CSUM |
  2143. NETIF_F_IPV6_CSUM |
  2144. NETIF_F_HIGHDMA |
  2145. NETIF_F_SOFT_FEATURES |
  2146. NETIF_F_TSO |
  2147. NETIF_F_TSO_ECN |
  2148. NETIF_F_TSO6 |
  2149. NETIF_F_SCTP_CRC |
  2150. NETIF_F_RXHASH |
  2151. NETIF_F_RXCSUM |
  2152. 0;
  2153. /* advertise to stack only if offloads for encapsulated packets is
  2154. * supported
  2155. */
  2156. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
  2157. hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
  2158. NETIF_F_GSO_GRE |
  2159. NETIF_F_GSO_GRE_CSUM |
  2160. NETIF_F_GSO_IPXIP4 |
  2161. NETIF_F_GSO_IPXIP6 |
  2162. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  2163. NETIF_F_GSO_PARTIAL |
  2164. 0;
  2165. if (!(vfres->vf_cap_flags &
  2166. VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
  2167. netdev->gso_partial_features |=
  2168. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  2169. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  2170. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  2171. netdev->hw_enc_features |= hw_enc_features;
  2172. }
  2173. /* record features VLANs can make use of */
  2174. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  2175. /* Write features and hw_features separately to avoid polluting
  2176. * with, or dropping, features that are set when we registered.
  2177. */
  2178. hw_features = hw_enc_features;
  2179. netdev->hw_features |= hw_features;
  2180. netdev->features |= hw_features | I40EVF_VLAN_FEATURES;
  2181. adapter->vsi.id = adapter->vsi_res->vsi_id;
  2182. adapter->vsi.back = adapter;
  2183. adapter->vsi.base_vector = 1;
  2184. adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
  2185. vsi->netdev = adapter->netdev;
  2186. vsi->qs_handle = adapter->vsi_res->qset_handle;
  2187. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  2188. adapter->rss_key_size = vfres->rss_key_size;
  2189. adapter->rss_lut_size = vfres->rss_lut_size;
  2190. } else {
  2191. adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
  2192. adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
  2193. }
  2194. return 0;
  2195. }
  2196. /**
  2197. * i40evf_init_task - worker thread to perform delayed initialization
  2198. * @work: pointer to work_struct containing our data
  2199. *
  2200. * This task completes the work that was begun in probe. Due to the nature
  2201. * of VF-PF communications, we may need to wait tens of milliseconds to get
  2202. * responses back from the PF. Rather than busy-wait in probe and bog down the
  2203. * whole system, we'll do it in a task so we can sleep.
  2204. * This task only runs during driver init. Once we've established
  2205. * communications with the PF driver and set up our netdev, the watchdog
  2206. * takes over.
  2207. **/
  2208. static void i40evf_init_task(struct work_struct *work)
  2209. {
  2210. struct i40evf_adapter *adapter = container_of(work,
  2211. struct i40evf_adapter,
  2212. init_task.work);
  2213. struct net_device *netdev = adapter->netdev;
  2214. struct i40e_hw *hw = &adapter->hw;
  2215. struct pci_dev *pdev = adapter->pdev;
  2216. int err, bufsz;
  2217. switch (adapter->state) {
  2218. case __I40EVF_STARTUP:
  2219. /* driver loaded, probe complete */
  2220. adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
  2221. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  2222. err = i40e_set_mac_type(hw);
  2223. if (err) {
  2224. dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
  2225. err);
  2226. goto err;
  2227. }
  2228. err = i40evf_check_reset_complete(hw);
  2229. if (err) {
  2230. dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
  2231. err);
  2232. goto err;
  2233. }
  2234. hw->aq.num_arq_entries = I40EVF_AQ_LEN;
  2235. hw->aq.num_asq_entries = I40EVF_AQ_LEN;
  2236. hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
  2237. hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
  2238. err = i40evf_init_adminq(hw);
  2239. if (err) {
  2240. dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
  2241. err);
  2242. goto err;
  2243. }
  2244. err = i40evf_send_api_ver(adapter);
  2245. if (err) {
  2246. dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
  2247. i40evf_shutdown_adminq(hw);
  2248. goto err;
  2249. }
  2250. adapter->state = __I40EVF_INIT_VERSION_CHECK;
  2251. goto restart;
  2252. case __I40EVF_INIT_VERSION_CHECK:
  2253. if (!i40evf_asq_done(hw)) {
  2254. dev_err(&pdev->dev, "Admin queue command never completed\n");
  2255. i40evf_shutdown_adminq(hw);
  2256. adapter->state = __I40EVF_STARTUP;
  2257. goto err;
  2258. }
  2259. /* aq msg sent, awaiting reply */
  2260. err = i40evf_verify_api_ver(adapter);
  2261. if (err) {
  2262. if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  2263. err = i40evf_send_api_ver(adapter);
  2264. else
  2265. dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
  2266. adapter->pf_version.major,
  2267. adapter->pf_version.minor,
  2268. VIRTCHNL_VERSION_MAJOR,
  2269. VIRTCHNL_VERSION_MINOR);
  2270. goto err;
  2271. }
  2272. err = i40evf_send_vf_config_msg(adapter);
  2273. if (err) {
  2274. dev_err(&pdev->dev, "Unable to send config request (%d)\n",
  2275. err);
  2276. goto err;
  2277. }
  2278. adapter->state = __I40EVF_INIT_GET_RESOURCES;
  2279. goto restart;
  2280. case __I40EVF_INIT_GET_RESOURCES:
  2281. /* aq msg sent, awaiting reply */
  2282. if (!adapter->vf_res) {
  2283. bufsz = sizeof(struct virtchnl_vf_resource) +
  2284. (I40E_MAX_VF_VSI *
  2285. sizeof(struct virtchnl_vsi_resource));
  2286. adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
  2287. if (!adapter->vf_res)
  2288. goto err;
  2289. }
  2290. err = i40evf_get_vf_config(adapter);
  2291. if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
  2292. err = i40evf_send_vf_config_msg(adapter);
  2293. goto err;
  2294. } else if (err == I40E_ERR_PARAM) {
  2295. /* We only get ERR_PARAM if the device is in a very bad
  2296. * state or if we've been disabled for previous bad
  2297. * behavior. Either way, we're done now.
  2298. */
  2299. i40evf_shutdown_adminq(hw);
  2300. dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
  2301. return;
  2302. }
  2303. if (err) {
  2304. dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
  2305. err);
  2306. goto err_alloc;
  2307. }
  2308. adapter->state = __I40EVF_INIT_SW;
  2309. break;
  2310. default:
  2311. goto err_alloc;
  2312. }
  2313. if (i40evf_process_config(adapter))
  2314. goto err_alloc;
  2315. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  2316. adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
  2317. netdev->netdev_ops = &i40evf_netdev_ops;
  2318. i40evf_set_ethtool_ops(netdev);
  2319. netdev->watchdog_timeo = 5 * HZ;
  2320. /* MTU range: 68 - 9710 */
  2321. netdev->min_mtu = ETH_MIN_MTU;
  2322. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  2323. if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
  2324. dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
  2325. adapter->hw.mac.addr);
  2326. eth_hw_addr_random(netdev);
  2327. ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
  2328. } else {
  2329. adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
  2330. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  2331. ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
  2332. }
  2333. init_timer(&adapter->watchdog_timer);
  2334. adapter->watchdog_timer.function = &i40evf_watchdog_timer;
  2335. adapter->watchdog_timer.data = (unsigned long)adapter;
  2336. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  2337. adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
  2338. adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
  2339. err = i40evf_init_interrupt_scheme(adapter);
  2340. if (err)
  2341. goto err_sw_init;
  2342. i40evf_map_rings_to_vectors(adapter);
  2343. if (adapter->vf_res->vf_cap_flags &
  2344. VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  2345. adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
  2346. err = i40evf_request_misc_irq(adapter);
  2347. if (err)
  2348. goto err_sw_init;
  2349. netif_carrier_off(netdev);
  2350. adapter->link_up = false;
  2351. if (!adapter->netdev_registered) {
  2352. err = register_netdev(netdev);
  2353. if (err)
  2354. goto err_register;
  2355. }
  2356. adapter->netdev_registered = true;
  2357. netif_tx_stop_all_queues(netdev);
  2358. if (CLIENT_ALLOWED(adapter)) {
  2359. err = i40evf_lan_add_device(adapter);
  2360. if (err)
  2361. dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
  2362. err);
  2363. }
  2364. dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
  2365. if (netdev->features & NETIF_F_GRO)
  2366. dev_info(&pdev->dev, "GRO is enabled\n");
  2367. adapter->state = __I40EVF_DOWN;
  2368. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  2369. i40evf_misc_irq_enable(adapter);
  2370. wake_up(&adapter->down_waitqueue);
  2371. adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
  2372. adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
  2373. if (!adapter->rss_key || !adapter->rss_lut)
  2374. goto err_mem;
  2375. if (RSS_AQ(adapter)) {
  2376. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
  2377. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  2378. } else {
  2379. i40evf_init_rss(adapter);
  2380. }
  2381. return;
  2382. restart:
  2383. schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
  2384. return;
  2385. err_mem:
  2386. i40evf_free_rss(adapter);
  2387. err_register:
  2388. i40evf_free_misc_irq(adapter);
  2389. err_sw_init:
  2390. i40evf_reset_interrupt_capability(adapter);
  2391. err_alloc:
  2392. kfree(adapter->vf_res);
  2393. adapter->vf_res = NULL;
  2394. err:
  2395. /* Things went into the weeds, so try again later */
  2396. if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
  2397. dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
  2398. adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
  2399. i40evf_shutdown_adminq(hw);
  2400. adapter->state = __I40EVF_STARTUP;
  2401. schedule_delayed_work(&adapter->init_task, HZ * 5);
  2402. return;
  2403. }
  2404. schedule_delayed_work(&adapter->init_task, HZ);
  2405. }
  2406. /**
  2407. * i40evf_shutdown - Shutdown the device in preparation for a reboot
  2408. * @pdev: pci device structure
  2409. **/
  2410. static void i40evf_shutdown(struct pci_dev *pdev)
  2411. {
  2412. struct net_device *netdev = pci_get_drvdata(pdev);
  2413. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2414. netif_device_detach(netdev);
  2415. if (netif_running(netdev))
  2416. i40evf_close(netdev);
  2417. /* Prevent the watchdog from running. */
  2418. adapter->state = __I40EVF_REMOVE;
  2419. adapter->aq_required = 0;
  2420. #ifdef CONFIG_PM
  2421. pci_save_state(pdev);
  2422. #endif
  2423. pci_disable_device(pdev);
  2424. }
  2425. /**
  2426. * i40evf_probe - Device Initialization Routine
  2427. * @pdev: PCI device information struct
  2428. * @ent: entry in i40evf_pci_tbl
  2429. *
  2430. * Returns 0 on success, negative on failure
  2431. *
  2432. * i40evf_probe initializes an adapter identified by a pci_dev structure.
  2433. * The OS initialization, configuring of the adapter private structure,
  2434. * and a hardware reset occur.
  2435. **/
  2436. static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2437. {
  2438. struct net_device *netdev;
  2439. struct i40evf_adapter *adapter = NULL;
  2440. struct i40e_hw *hw = NULL;
  2441. int err;
  2442. err = pci_enable_device(pdev);
  2443. if (err)
  2444. return err;
  2445. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2446. if (err) {
  2447. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2448. if (err) {
  2449. dev_err(&pdev->dev,
  2450. "DMA configuration failed: 0x%x\n", err);
  2451. goto err_dma;
  2452. }
  2453. }
  2454. err = pci_request_regions(pdev, i40evf_driver_name);
  2455. if (err) {
  2456. dev_err(&pdev->dev,
  2457. "pci_request_regions failed 0x%x\n", err);
  2458. goto err_pci_reg;
  2459. }
  2460. pci_enable_pcie_error_reporting(pdev);
  2461. pci_set_master(pdev);
  2462. netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
  2463. if (!netdev) {
  2464. err = -ENOMEM;
  2465. goto err_alloc_etherdev;
  2466. }
  2467. SET_NETDEV_DEV(netdev, &pdev->dev);
  2468. pci_set_drvdata(pdev, netdev);
  2469. adapter = netdev_priv(netdev);
  2470. adapter->netdev = netdev;
  2471. adapter->pdev = pdev;
  2472. hw = &adapter->hw;
  2473. hw->back = adapter;
  2474. adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
  2475. adapter->state = __I40EVF_STARTUP;
  2476. /* Call save state here because it relies on the adapter struct. */
  2477. pci_save_state(pdev);
  2478. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  2479. pci_resource_len(pdev, 0));
  2480. if (!hw->hw_addr) {
  2481. err = -EIO;
  2482. goto err_ioremap;
  2483. }
  2484. hw->vendor_id = pdev->vendor;
  2485. hw->device_id = pdev->device;
  2486. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  2487. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2488. hw->subsystem_device_id = pdev->subsystem_device;
  2489. hw->bus.device = PCI_SLOT(pdev->devfn);
  2490. hw->bus.func = PCI_FUNC(pdev->devfn);
  2491. hw->bus.bus_id = pdev->bus->number;
  2492. /* set up the locks for the AQ, do this only once in probe
  2493. * and destroy them only once in remove
  2494. */
  2495. mutex_init(&hw->aq.asq_mutex);
  2496. mutex_init(&hw->aq.arq_mutex);
  2497. INIT_LIST_HEAD(&adapter->mac_filter_list);
  2498. INIT_LIST_HEAD(&adapter->vlan_filter_list);
  2499. INIT_WORK(&adapter->reset_task, i40evf_reset_task);
  2500. INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
  2501. INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
  2502. INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
  2503. INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
  2504. schedule_delayed_work(&adapter->init_task,
  2505. msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
  2506. /* Setup the wait queue for indicating transition to down status */
  2507. init_waitqueue_head(&adapter->down_waitqueue);
  2508. return 0;
  2509. err_ioremap:
  2510. free_netdev(netdev);
  2511. err_alloc_etherdev:
  2512. pci_release_regions(pdev);
  2513. err_pci_reg:
  2514. err_dma:
  2515. pci_disable_device(pdev);
  2516. return err;
  2517. }
  2518. #ifdef CONFIG_PM
  2519. /**
  2520. * i40evf_suspend - Power management suspend routine
  2521. * @pdev: PCI device information struct
  2522. * @state: unused
  2523. *
  2524. * Called when the system (VM) is entering sleep/suspend.
  2525. **/
  2526. static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
  2527. {
  2528. struct net_device *netdev = pci_get_drvdata(pdev);
  2529. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2530. int retval = 0;
  2531. netif_device_detach(netdev);
  2532. if (netif_running(netdev)) {
  2533. rtnl_lock();
  2534. i40evf_down(adapter);
  2535. rtnl_unlock();
  2536. }
  2537. i40evf_free_misc_irq(adapter);
  2538. i40evf_reset_interrupt_capability(adapter);
  2539. retval = pci_save_state(pdev);
  2540. if (retval)
  2541. return retval;
  2542. pci_disable_device(pdev);
  2543. return 0;
  2544. }
  2545. /**
  2546. * i40evf_resume - Power management resume routine
  2547. * @pdev: PCI device information struct
  2548. *
  2549. * Called when the system (VM) is resumed from sleep/suspend.
  2550. **/
  2551. static int i40evf_resume(struct pci_dev *pdev)
  2552. {
  2553. struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
  2554. struct net_device *netdev = adapter->netdev;
  2555. u32 err;
  2556. pci_set_power_state(pdev, PCI_D0);
  2557. pci_restore_state(pdev);
  2558. /* pci_restore_state clears dev->state_saved so call
  2559. * pci_save_state to restore it.
  2560. */
  2561. pci_save_state(pdev);
  2562. err = pci_enable_device_mem(pdev);
  2563. if (err) {
  2564. dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
  2565. return err;
  2566. }
  2567. pci_set_master(pdev);
  2568. rtnl_lock();
  2569. err = i40evf_set_interrupt_capability(adapter);
  2570. if (err) {
  2571. rtnl_unlock();
  2572. dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
  2573. return err;
  2574. }
  2575. err = i40evf_request_misc_irq(adapter);
  2576. rtnl_unlock();
  2577. if (err) {
  2578. dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
  2579. return err;
  2580. }
  2581. schedule_work(&adapter->reset_task);
  2582. netif_device_attach(netdev);
  2583. return err;
  2584. }
  2585. #endif /* CONFIG_PM */
  2586. /**
  2587. * i40evf_remove - Device Removal Routine
  2588. * @pdev: PCI device information struct
  2589. *
  2590. * i40evf_remove is called by the PCI subsystem to alert the driver
  2591. * that it should release a PCI device. The could be caused by a
  2592. * Hot-Plug event, or because the driver is going to be removed from
  2593. * memory.
  2594. **/
  2595. static void i40evf_remove(struct pci_dev *pdev)
  2596. {
  2597. struct net_device *netdev = pci_get_drvdata(pdev);
  2598. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2599. struct i40evf_mac_filter *f, *ftmp;
  2600. struct i40e_hw *hw = &adapter->hw;
  2601. int err;
  2602. cancel_delayed_work_sync(&adapter->init_task);
  2603. cancel_work_sync(&adapter->reset_task);
  2604. cancel_delayed_work_sync(&adapter->client_task);
  2605. if (adapter->netdev_registered) {
  2606. unregister_netdev(netdev);
  2607. adapter->netdev_registered = false;
  2608. }
  2609. if (CLIENT_ALLOWED(adapter)) {
  2610. err = i40evf_lan_del_device(adapter);
  2611. if (err)
  2612. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  2613. err);
  2614. }
  2615. /* Shut down all the garbage mashers on the detention level */
  2616. adapter->state = __I40EVF_REMOVE;
  2617. adapter->aq_required = 0;
  2618. i40evf_request_reset(adapter);
  2619. msleep(50);
  2620. /* If the FW isn't responding, kick it once, but only once. */
  2621. if (!i40evf_asq_done(hw)) {
  2622. i40evf_request_reset(adapter);
  2623. msleep(50);
  2624. }
  2625. i40evf_free_all_tx_resources(adapter);
  2626. i40evf_free_all_rx_resources(adapter);
  2627. i40evf_misc_irq_disable(adapter);
  2628. i40evf_free_misc_irq(adapter);
  2629. i40evf_reset_interrupt_capability(adapter);
  2630. i40evf_free_q_vectors(adapter);
  2631. if (adapter->watchdog_timer.function)
  2632. del_timer_sync(&adapter->watchdog_timer);
  2633. flush_scheduled_work();
  2634. i40evf_free_rss(adapter);
  2635. if (hw->aq.asq.count)
  2636. i40evf_shutdown_adminq(hw);
  2637. /* destroy the locks only once, here */
  2638. mutex_destroy(&hw->aq.arq_mutex);
  2639. mutex_destroy(&hw->aq.asq_mutex);
  2640. iounmap(hw->hw_addr);
  2641. pci_release_regions(pdev);
  2642. i40evf_free_all_tx_resources(adapter);
  2643. i40evf_free_all_rx_resources(adapter);
  2644. i40evf_free_queues(adapter);
  2645. kfree(adapter->vf_res);
  2646. /* If we got removed before an up/down sequence, we've got a filter
  2647. * hanging out there that we need to get rid of.
  2648. */
  2649. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  2650. list_del(&f->list);
  2651. kfree(f);
  2652. }
  2653. list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
  2654. list_del(&f->list);
  2655. kfree(f);
  2656. }
  2657. free_netdev(netdev);
  2658. pci_disable_pcie_error_reporting(pdev);
  2659. pci_disable_device(pdev);
  2660. }
  2661. static struct pci_driver i40evf_driver = {
  2662. .name = i40evf_driver_name,
  2663. .id_table = i40evf_pci_tbl,
  2664. .probe = i40evf_probe,
  2665. .remove = i40evf_remove,
  2666. #ifdef CONFIG_PM
  2667. .suspend = i40evf_suspend,
  2668. .resume = i40evf_resume,
  2669. #endif
  2670. .shutdown = i40evf_shutdown,
  2671. };
  2672. /**
  2673. * i40e_init_module - Driver Registration Routine
  2674. *
  2675. * i40e_init_module is the first routine called when the driver is
  2676. * loaded. All it does is register with the PCI subsystem.
  2677. **/
  2678. static int __init i40evf_init_module(void)
  2679. {
  2680. int ret;
  2681. pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
  2682. i40evf_driver_version);
  2683. pr_info("%s\n", i40evf_copyright);
  2684. i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  2685. i40evf_driver_name);
  2686. if (!i40evf_wq) {
  2687. pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
  2688. return -ENOMEM;
  2689. }
  2690. ret = pci_register_driver(&i40evf_driver);
  2691. return ret;
  2692. }
  2693. module_init(i40evf_init_module);
  2694. /**
  2695. * i40e_exit_module - Driver Exit Cleanup Routine
  2696. *
  2697. * i40e_exit_module is called just before the driver is removed
  2698. * from memory.
  2699. **/
  2700. static void __exit i40evf_exit_module(void)
  2701. {
  2702. pci_unregister_driver(&i40evf_driver);
  2703. destroy_workqueue(i40evf_wq);
  2704. }
  2705. module_exit(i40evf_exit_module);
  2706. /* i40evf_main.c */