i40evf_main.c 82 KB

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