i40evf_main.c 67 KB

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