i40evf_main.c 70 KB

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