i40evf_main.c 64 KB

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