i40evf_main.c 61 KB

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