i40evf_main.c 65 KB

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