i40evf_main.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199
  1. /*******************************************************************************
  2. *
  3. * Intel Ethernet Controller XL710 Family Linux Virtual Function Driver
  4. * Copyright(c) 2013 - 2016 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms and conditions of the GNU General Public License,
  8. * version 2, as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope it will be useful, but WITHOUT
  11. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  13. * more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * The full GNU General Public License is included in this distribution in
  19. * the file called "COPYING".
  20. *
  21. * Contact Information:
  22. * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  23. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  24. *
  25. ******************************************************************************/
  26. #include "i40evf.h"
  27. #include "i40e_prototype.h"
  28. #include "i40evf_client.h"
  29. /* All i40evf tracepoints are defined by the include below, which must
  30. * be included exactly once across the whole kernel with
  31. * CREATE_TRACE_POINTS defined
  32. */
  33. #define CREATE_TRACE_POINTS
  34. #include "i40e_trace.h"
  35. static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter);
  36. static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter);
  37. static int i40evf_close(struct net_device *netdev);
  38. char i40evf_driver_name[] = "i40evf";
  39. static const char i40evf_driver_string[] =
  40. "Intel(R) 40-10 Gigabit Virtual Function Network Driver";
  41. #define DRV_KERN "-k"
  42. #define DRV_VERSION_MAJOR 3
  43. #define DRV_VERSION_MINOR 2
  44. #define DRV_VERSION_BUILD 2
  45. #define DRV_VERSION __stringify(DRV_VERSION_MAJOR) "." \
  46. __stringify(DRV_VERSION_MINOR) "." \
  47. __stringify(DRV_VERSION_BUILD) \
  48. DRV_KERN
  49. const char i40evf_driver_version[] = DRV_VERSION;
  50. static const char i40evf_copyright[] =
  51. "Copyright (c) 2013 - 2015 Intel Corporation.";
  52. /* i40evf_pci_tbl - PCI Device ID Table
  53. *
  54. * Wildcard entries (PCI_ANY_ID) should come last
  55. * Last entry must be all 0s
  56. *
  57. * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
  58. * Class, Class Mask, private data (not used) }
  59. */
  60. static const struct pci_device_id i40evf_pci_tbl[] = {
  61. {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF), 0},
  62. {PCI_VDEVICE(INTEL, I40E_DEV_ID_VF_HV), 0},
  63. {PCI_VDEVICE(INTEL, I40E_DEV_ID_X722_VF), 0},
  64. {PCI_VDEVICE(INTEL, I40E_DEV_ID_ADAPTIVE_VF), 0},
  65. /* required last entry */
  66. {0, }
  67. };
  68. MODULE_DEVICE_TABLE(pci, i40evf_pci_tbl);
  69. MODULE_AUTHOR("Intel Corporation, <linux.nics@intel.com>");
  70. MODULE_DESCRIPTION("Intel(R) XL710 X710 Virtual Function Network Driver");
  71. MODULE_LICENSE("GPL");
  72. MODULE_VERSION(DRV_VERSION);
  73. static struct workqueue_struct *i40evf_wq;
  74. /**
  75. * i40evf_allocate_dma_mem_d - OS specific memory alloc for shared code
  76. * @hw: pointer to the HW structure
  77. * @mem: ptr to mem struct to fill out
  78. * @size: size of memory requested
  79. * @alignment: what to align the allocation to
  80. **/
  81. i40e_status i40evf_allocate_dma_mem_d(struct i40e_hw *hw,
  82. struct i40e_dma_mem *mem,
  83. u64 size, u32 alignment)
  84. {
  85. struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  86. if (!mem)
  87. return I40E_ERR_PARAM;
  88. mem->size = ALIGN(size, alignment);
  89. mem->va = dma_alloc_coherent(&adapter->pdev->dev, mem->size,
  90. (dma_addr_t *)&mem->pa, GFP_KERNEL);
  91. if (mem->va)
  92. return 0;
  93. else
  94. return I40E_ERR_NO_MEMORY;
  95. }
  96. /**
  97. * i40evf_free_dma_mem_d - OS specific memory free for shared code
  98. * @hw: pointer to the HW structure
  99. * @mem: ptr to mem struct to free
  100. **/
  101. i40e_status i40evf_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
  102. {
  103. struct i40evf_adapter *adapter = (struct i40evf_adapter *)hw->back;
  104. if (!mem || !mem->va)
  105. return I40E_ERR_PARAM;
  106. dma_free_coherent(&adapter->pdev->dev, mem->size,
  107. mem->va, (dma_addr_t)mem->pa);
  108. return 0;
  109. }
  110. /**
  111. * i40evf_allocate_virt_mem_d - OS specific memory alloc for shared code
  112. * @hw: pointer to the HW structure
  113. * @mem: ptr to mem struct to fill out
  114. * @size: size of memory requested
  115. **/
  116. i40e_status i40evf_allocate_virt_mem_d(struct i40e_hw *hw,
  117. struct i40e_virt_mem *mem, u32 size)
  118. {
  119. if (!mem)
  120. return I40E_ERR_PARAM;
  121. mem->size = size;
  122. mem->va = kzalloc(size, GFP_KERNEL);
  123. if (mem->va)
  124. return 0;
  125. else
  126. return I40E_ERR_NO_MEMORY;
  127. }
  128. /**
  129. * i40evf_free_virt_mem_d - OS specific memory free for shared code
  130. * @hw: pointer to the HW structure
  131. * @mem: ptr to mem struct to free
  132. **/
  133. i40e_status i40evf_free_virt_mem_d(struct i40e_hw *hw,
  134. struct i40e_virt_mem *mem)
  135. {
  136. if (!mem)
  137. return I40E_ERR_PARAM;
  138. /* it's ok to kfree a NULL pointer */
  139. kfree(mem->va);
  140. return 0;
  141. }
  142. /**
  143. * i40evf_debug_d - OS dependent version of debug printing
  144. * @hw: pointer to the HW structure
  145. * @mask: debug level mask
  146. * @fmt_str: printf-type format description
  147. **/
  148. void i40evf_debug_d(void *hw, u32 mask, char *fmt_str, ...)
  149. {
  150. char buf[512];
  151. va_list argptr;
  152. if (!(mask & ((struct i40e_hw *)hw)->debug_mask))
  153. return;
  154. va_start(argptr, fmt_str);
  155. vsnprintf(buf, sizeof(buf), fmt_str, argptr);
  156. va_end(argptr);
  157. /* the debug string is already formatted with a newline */
  158. pr_info("%s", buf);
  159. }
  160. /**
  161. * i40evf_schedule_reset - Set the flags and schedule a reset event
  162. * @adapter: board private structure
  163. **/
  164. void i40evf_schedule_reset(struct i40evf_adapter *adapter)
  165. {
  166. if (!(adapter->flags &
  167. (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED))) {
  168. adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
  169. schedule_work(&adapter->reset_task);
  170. }
  171. }
  172. /**
  173. * i40evf_tx_timeout - Respond to a Tx Hang
  174. * @netdev: network interface device structure
  175. **/
  176. static void i40evf_tx_timeout(struct net_device *netdev)
  177. {
  178. struct i40evf_adapter *adapter = netdev_priv(netdev);
  179. adapter->tx_timeout_count++;
  180. i40evf_schedule_reset(adapter);
  181. }
  182. /**
  183. * i40evf_misc_irq_disable - Mask off interrupt generation on the NIC
  184. * @adapter: board private structure
  185. **/
  186. static void i40evf_misc_irq_disable(struct i40evf_adapter *adapter)
  187. {
  188. struct i40e_hw *hw = &adapter->hw;
  189. if (!adapter->msix_entries)
  190. return;
  191. wr32(hw, I40E_VFINT_DYN_CTL01, 0);
  192. /* read flush */
  193. rd32(hw, I40E_VFGEN_RSTAT);
  194. synchronize_irq(adapter->msix_entries[0].vector);
  195. }
  196. /**
  197. * i40evf_misc_irq_enable - Enable default interrupt generation settings
  198. * @adapter: board private structure
  199. **/
  200. static void i40evf_misc_irq_enable(struct i40evf_adapter *adapter)
  201. {
  202. struct i40e_hw *hw = &adapter->hw;
  203. wr32(hw, I40E_VFINT_DYN_CTL01, I40E_VFINT_DYN_CTL01_INTENA_MASK |
  204. I40E_VFINT_DYN_CTL01_ITR_INDX_MASK);
  205. wr32(hw, I40E_VFINT_ICR0_ENA1, I40E_VFINT_ICR0_ENA1_ADMINQ_MASK);
  206. /* read flush */
  207. rd32(hw, I40E_VFGEN_RSTAT);
  208. }
  209. /**
  210. * i40evf_irq_disable - Mask off interrupt generation on the NIC
  211. * @adapter: board private structure
  212. **/
  213. static void i40evf_irq_disable(struct i40evf_adapter *adapter)
  214. {
  215. int i;
  216. struct i40e_hw *hw = &adapter->hw;
  217. if (!adapter->msix_entries)
  218. return;
  219. for (i = 1; i < adapter->num_msix_vectors; i++) {
  220. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1), 0);
  221. synchronize_irq(adapter->msix_entries[i].vector);
  222. }
  223. /* read flush */
  224. rd32(hw, I40E_VFGEN_RSTAT);
  225. }
  226. /**
  227. * i40evf_irq_enable_queues - Enable interrupt for specified queues
  228. * @adapter: board private structure
  229. * @mask: bitmap of queues to enable
  230. **/
  231. void i40evf_irq_enable_queues(struct i40evf_adapter *adapter, u32 mask)
  232. {
  233. struct i40e_hw *hw = &adapter->hw;
  234. int i;
  235. for (i = 1; i < adapter->num_msix_vectors; i++) {
  236. if (mask & BIT(i - 1)) {
  237. wr32(hw, I40E_VFINT_DYN_CTLN1(i - 1),
  238. I40E_VFINT_DYN_CTLN1_INTENA_MASK |
  239. I40E_VFINT_DYN_CTLN1_ITR_INDX_MASK);
  240. }
  241. }
  242. }
  243. /**
  244. * i40evf_irq_enable - Enable default interrupt generation settings
  245. * @adapter: board private structure
  246. * @flush: boolean value whether to run rd32()
  247. **/
  248. void i40evf_irq_enable(struct i40evf_adapter *adapter, bool flush)
  249. {
  250. struct i40e_hw *hw = &adapter->hw;
  251. i40evf_misc_irq_enable(adapter);
  252. i40evf_irq_enable_queues(adapter, ~0);
  253. if (flush)
  254. rd32(hw, I40E_VFGEN_RSTAT);
  255. }
  256. /**
  257. * i40evf_msix_aq - Interrupt handler for vector 0
  258. * @irq: interrupt number
  259. * @data: pointer to netdev
  260. **/
  261. static irqreturn_t i40evf_msix_aq(int irq, void *data)
  262. {
  263. struct net_device *netdev = data;
  264. struct i40evf_adapter *adapter = netdev_priv(netdev);
  265. struct i40e_hw *hw = &adapter->hw;
  266. /* handle non-queue interrupts, these reads clear the registers */
  267. rd32(hw, I40E_VFINT_ICR01);
  268. rd32(hw, I40E_VFINT_ICR0_ENA1);
  269. /* schedule work on the private workqueue */
  270. schedule_work(&adapter->adminq_task);
  271. return IRQ_HANDLED;
  272. }
  273. /**
  274. * i40evf_msix_clean_rings - MSIX mode Interrupt Handler
  275. * @irq: interrupt number
  276. * @data: pointer to a q_vector
  277. **/
  278. static irqreturn_t i40evf_msix_clean_rings(int irq, void *data)
  279. {
  280. struct i40e_q_vector *q_vector = data;
  281. if (!q_vector->tx.ring && !q_vector->rx.ring)
  282. return IRQ_HANDLED;
  283. napi_schedule_irqoff(&q_vector->napi);
  284. return IRQ_HANDLED;
  285. }
  286. /**
  287. * i40evf_map_vector_to_rxq - associate irqs with rx queues
  288. * @adapter: board private structure
  289. * @v_idx: interrupt number
  290. * @r_idx: queue number
  291. **/
  292. static void
  293. i40evf_map_vector_to_rxq(struct i40evf_adapter *adapter, int v_idx, int r_idx)
  294. {
  295. struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
  296. struct i40e_ring *rx_ring = &adapter->rx_rings[r_idx];
  297. struct i40e_hw *hw = &adapter->hw;
  298. rx_ring->q_vector = q_vector;
  299. rx_ring->next = q_vector->rx.ring;
  300. rx_ring->vsi = &adapter->vsi;
  301. q_vector->rx.ring = rx_ring;
  302. q_vector->rx.count++;
  303. q_vector->rx.next_update = jiffies + 1;
  304. q_vector->rx.target_itr = ITR_TO_REG(rx_ring->itr_setting);
  305. q_vector->ring_mask |= BIT(r_idx);
  306. wr32(hw, I40E_VFINT_ITRN1(I40E_RX_ITR, q_vector->reg_idx),
  307. q_vector->rx.current_itr);
  308. q_vector->rx.current_itr = q_vector->rx.target_itr;
  309. }
  310. /**
  311. * i40evf_map_vector_to_txq - associate irqs with tx queues
  312. * @adapter: board private structure
  313. * @v_idx: interrupt number
  314. * @t_idx: queue number
  315. **/
  316. static void
  317. i40evf_map_vector_to_txq(struct i40evf_adapter *adapter, int v_idx, int t_idx)
  318. {
  319. struct i40e_q_vector *q_vector = &adapter->q_vectors[v_idx];
  320. struct i40e_ring *tx_ring = &adapter->tx_rings[t_idx];
  321. struct i40e_hw *hw = &adapter->hw;
  322. tx_ring->q_vector = q_vector;
  323. tx_ring->next = q_vector->tx.ring;
  324. tx_ring->vsi = &adapter->vsi;
  325. q_vector->tx.ring = tx_ring;
  326. q_vector->tx.count++;
  327. q_vector->tx.next_update = jiffies + 1;
  328. q_vector->tx.target_itr = ITR_TO_REG(tx_ring->itr_setting);
  329. q_vector->num_ringpairs++;
  330. wr32(hw, I40E_VFINT_ITRN1(I40E_TX_ITR, q_vector->reg_idx),
  331. q_vector->tx.target_itr);
  332. q_vector->tx.current_itr = q_vector->tx.target_itr;
  333. }
  334. /**
  335. * i40evf_map_rings_to_vectors - Maps descriptor rings to vectors
  336. * @adapter: board private structure to initialize
  337. *
  338. * This function maps descriptor rings to the queue-specific vectors
  339. * we were allotted through the MSI-X enabling code. Ideally, we'd have
  340. * one vector per ring/queue, but on a constrained vector budget, we
  341. * group the rings as "efficiently" as possible. You would add new
  342. * mapping configurations in here.
  343. **/
  344. static void i40evf_map_rings_to_vectors(struct i40evf_adapter *adapter)
  345. {
  346. int rings_remaining = adapter->num_active_queues;
  347. int ridx = 0, vidx = 0;
  348. int q_vectors;
  349. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  350. for (; ridx < rings_remaining; ridx++) {
  351. i40evf_map_vector_to_rxq(adapter, vidx, ridx);
  352. i40evf_map_vector_to_txq(adapter, vidx, ridx);
  353. /* In the case where we have more queues than vectors, continue
  354. * round-robin on vectors until all queues are mapped.
  355. */
  356. if (++vidx >= q_vectors)
  357. vidx = 0;
  358. }
  359. adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
  360. }
  361. #ifdef CONFIG_NET_POLL_CONTROLLER
  362. /**
  363. * i40evf_netpoll - A Polling 'interrupt' handler
  364. * @netdev: network interface device structure
  365. *
  366. * This is used by netconsole to send skbs without having to re-enable
  367. * interrupts. It's not called while the normal interrupt routine is executing.
  368. **/
  369. static void i40evf_netpoll(struct net_device *netdev)
  370. {
  371. struct i40evf_adapter *adapter = netdev_priv(netdev);
  372. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  373. int i;
  374. /* if interface is down do nothing */
  375. if (test_bit(__I40E_VSI_DOWN, adapter->vsi.state))
  376. return;
  377. for (i = 0; i < q_vectors; i++)
  378. i40evf_msix_clean_rings(0, &adapter->q_vectors[i]);
  379. }
  380. #endif
  381. /**
  382. * i40evf_irq_affinity_notify - Callback for affinity changes
  383. * @notify: context as to what irq was changed
  384. * @mask: the new affinity mask
  385. *
  386. * This is a callback function used by the irq_set_affinity_notifier function
  387. * so that we may register to receive changes to the irq affinity masks.
  388. **/
  389. static void i40evf_irq_affinity_notify(struct irq_affinity_notify *notify,
  390. const cpumask_t *mask)
  391. {
  392. struct i40e_q_vector *q_vector =
  393. container_of(notify, struct i40e_q_vector, affinity_notify);
  394. cpumask_copy(&q_vector->affinity_mask, mask);
  395. }
  396. /**
  397. * i40evf_irq_affinity_release - Callback for affinity notifier release
  398. * @ref: internal core kernel usage
  399. *
  400. * This is a callback function used by the irq_set_affinity_notifier function
  401. * to inform the current notification subscriber that they will no longer
  402. * receive notifications.
  403. **/
  404. static void i40evf_irq_affinity_release(struct kref *ref) {}
  405. /**
  406. * i40evf_request_traffic_irqs - Initialize MSI-X interrupts
  407. * @adapter: board private structure
  408. *
  409. * Allocates MSI-X vectors for tx and rx handling, and requests
  410. * interrupts from the kernel.
  411. **/
  412. static int
  413. i40evf_request_traffic_irqs(struct i40evf_adapter *adapter, char *basename)
  414. {
  415. unsigned int vector, q_vectors;
  416. unsigned int rx_int_idx = 0, tx_int_idx = 0;
  417. int irq_num, err;
  418. int cpu;
  419. i40evf_irq_disable(adapter);
  420. /* Decrement for Other and TCP Timer vectors */
  421. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  422. for (vector = 0; vector < q_vectors; vector++) {
  423. struct i40e_q_vector *q_vector = &adapter->q_vectors[vector];
  424. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  425. if (q_vector->tx.ring && q_vector->rx.ring) {
  426. snprintf(q_vector->name, sizeof(q_vector->name),
  427. "i40evf-%s-TxRx-%d", basename, rx_int_idx++);
  428. tx_int_idx++;
  429. } else if (q_vector->rx.ring) {
  430. snprintf(q_vector->name, sizeof(q_vector->name),
  431. "i40evf-%s-rx-%d", basename, rx_int_idx++);
  432. } else if (q_vector->tx.ring) {
  433. snprintf(q_vector->name, sizeof(q_vector->name),
  434. "i40evf-%s-tx-%d", basename, tx_int_idx++);
  435. } else {
  436. /* skip this unused q_vector */
  437. continue;
  438. }
  439. err = request_irq(irq_num,
  440. i40evf_msix_clean_rings,
  441. 0,
  442. q_vector->name,
  443. q_vector);
  444. if (err) {
  445. dev_info(&adapter->pdev->dev,
  446. "Request_irq failed, error: %d\n", err);
  447. goto free_queue_irqs;
  448. }
  449. /* register for affinity change notifications */
  450. q_vector->affinity_notify.notify = i40evf_irq_affinity_notify;
  451. q_vector->affinity_notify.release =
  452. i40evf_irq_affinity_release;
  453. irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
  454. /* Spread the IRQ affinity hints across online CPUs. Note that
  455. * get_cpu_mask returns a mask with a permanent lifetime so
  456. * it's safe to use as a hint for irq_set_affinity_hint.
  457. */
  458. cpu = cpumask_local_spread(q_vector->v_idx, -1);
  459. irq_set_affinity_hint(irq_num, get_cpu_mask(cpu));
  460. }
  461. return 0;
  462. free_queue_irqs:
  463. while (vector) {
  464. vector--;
  465. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  466. irq_set_affinity_notifier(irq_num, NULL);
  467. irq_set_affinity_hint(irq_num, NULL);
  468. free_irq(irq_num, &adapter->q_vectors[vector]);
  469. }
  470. return err;
  471. }
  472. /**
  473. * i40evf_request_misc_irq - Initialize MSI-X interrupts
  474. * @adapter: board private structure
  475. *
  476. * Allocates MSI-X vector 0 and requests interrupts from the kernel. This
  477. * vector is only for the admin queue, and stays active even when the netdev
  478. * is closed.
  479. **/
  480. static int i40evf_request_misc_irq(struct i40evf_adapter *adapter)
  481. {
  482. struct net_device *netdev = adapter->netdev;
  483. int err;
  484. snprintf(adapter->misc_vector_name,
  485. sizeof(adapter->misc_vector_name) - 1, "i40evf-%s:mbx",
  486. dev_name(&adapter->pdev->dev));
  487. err = request_irq(adapter->msix_entries[0].vector,
  488. &i40evf_msix_aq, 0,
  489. adapter->misc_vector_name, netdev);
  490. if (err) {
  491. dev_err(&adapter->pdev->dev,
  492. "request_irq for %s failed: %d\n",
  493. adapter->misc_vector_name, err);
  494. free_irq(adapter->msix_entries[0].vector, netdev);
  495. }
  496. return err;
  497. }
  498. /**
  499. * i40evf_free_traffic_irqs - Free MSI-X interrupts
  500. * @adapter: board private structure
  501. *
  502. * Frees all MSI-X vectors other than 0.
  503. **/
  504. static void i40evf_free_traffic_irqs(struct i40evf_adapter *adapter)
  505. {
  506. int vector, irq_num, q_vectors;
  507. if (!adapter->msix_entries)
  508. return;
  509. q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  510. for (vector = 0; vector < q_vectors; vector++) {
  511. irq_num = adapter->msix_entries[vector + NONQ_VECS].vector;
  512. irq_set_affinity_notifier(irq_num, NULL);
  513. irq_set_affinity_hint(irq_num, NULL);
  514. free_irq(irq_num, &adapter->q_vectors[vector]);
  515. }
  516. }
  517. /**
  518. * i40evf_free_misc_irq - Free MSI-X miscellaneous vector
  519. * @adapter: board private structure
  520. *
  521. * Frees MSI-X vector 0.
  522. **/
  523. static void i40evf_free_misc_irq(struct i40evf_adapter *adapter)
  524. {
  525. struct net_device *netdev = adapter->netdev;
  526. if (!adapter->msix_entries)
  527. return;
  528. free_irq(adapter->msix_entries[0].vector, netdev);
  529. }
  530. /**
  531. * i40evf_configure_tx - Configure Transmit Unit after Reset
  532. * @adapter: board private structure
  533. *
  534. * Configure the Tx unit of the MAC after a reset.
  535. **/
  536. static void i40evf_configure_tx(struct i40evf_adapter *adapter)
  537. {
  538. struct i40e_hw *hw = &adapter->hw;
  539. int i;
  540. for (i = 0; i < adapter->num_active_queues; i++)
  541. adapter->tx_rings[i].tail = hw->hw_addr + I40E_QTX_TAIL1(i);
  542. }
  543. /**
  544. * i40evf_configure_rx - Configure Receive Unit after Reset
  545. * @adapter: board private structure
  546. *
  547. * Configure the Rx unit of the MAC after a reset.
  548. **/
  549. static void i40evf_configure_rx(struct i40evf_adapter *adapter)
  550. {
  551. unsigned int rx_buf_len = I40E_RXBUFFER_2048;
  552. struct i40e_hw *hw = &adapter->hw;
  553. int i;
  554. /* Legacy Rx will always default to a 2048 buffer size. */
  555. #if (PAGE_SIZE < 8192)
  556. if (!(adapter->flags & I40EVF_FLAG_LEGACY_RX)) {
  557. struct net_device *netdev = adapter->netdev;
  558. /* For jumbo frames on systems with 4K pages we have to use
  559. * an order 1 page, so we might as well increase the size
  560. * of our Rx buffer to make better use of the available space
  561. */
  562. rx_buf_len = I40E_RXBUFFER_3072;
  563. /* We use a 1536 buffer size for configurations with
  564. * standard Ethernet mtu. On x86 this gives us enough room
  565. * for shared info and 192 bytes of padding.
  566. */
  567. if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
  568. (netdev->mtu <= ETH_DATA_LEN))
  569. rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
  570. }
  571. #endif
  572. for (i = 0; i < adapter->num_active_queues; i++) {
  573. adapter->rx_rings[i].tail = hw->hw_addr + I40E_QRX_TAIL1(i);
  574. adapter->rx_rings[i].rx_buf_len = rx_buf_len;
  575. if (adapter->flags & I40EVF_FLAG_LEGACY_RX)
  576. clear_ring_build_skb_enabled(&adapter->rx_rings[i]);
  577. else
  578. set_ring_build_skb_enabled(&adapter->rx_rings[i]);
  579. }
  580. }
  581. /**
  582. * i40evf_find_vlan - Search filter list for specific vlan filter
  583. * @adapter: board private structure
  584. * @vlan: vlan tag
  585. *
  586. * Returns ptr to the filter object or NULL. Must be called while holding the
  587. * mac_vlan_list_lock.
  588. **/
  589. static struct
  590. i40evf_vlan_filter *i40evf_find_vlan(struct i40evf_adapter *adapter, u16 vlan)
  591. {
  592. struct i40evf_vlan_filter *f;
  593. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  594. if (vlan == f->vlan)
  595. return f;
  596. }
  597. return NULL;
  598. }
  599. /**
  600. * i40evf_add_vlan - Add a vlan filter to the list
  601. * @adapter: board private structure
  602. * @vlan: VLAN tag
  603. *
  604. * Returns ptr to the filter object or NULL when no memory available.
  605. **/
  606. static struct
  607. i40evf_vlan_filter *i40evf_add_vlan(struct i40evf_adapter *adapter, u16 vlan)
  608. {
  609. struct i40evf_vlan_filter *f = NULL;
  610. spin_lock_bh(&adapter->mac_vlan_list_lock);
  611. f = i40evf_find_vlan(adapter, vlan);
  612. if (!f) {
  613. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  614. if (!f)
  615. goto clearout;
  616. f->vlan = vlan;
  617. INIT_LIST_HEAD(&f->list);
  618. list_add(&f->list, &adapter->vlan_filter_list);
  619. f->add = true;
  620. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  621. }
  622. clearout:
  623. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  624. return f;
  625. }
  626. /**
  627. * i40evf_del_vlan - Remove a vlan filter from the list
  628. * @adapter: board private structure
  629. * @vlan: VLAN tag
  630. **/
  631. static void i40evf_del_vlan(struct i40evf_adapter *adapter, u16 vlan)
  632. {
  633. struct i40evf_vlan_filter *f;
  634. spin_lock_bh(&adapter->mac_vlan_list_lock);
  635. f = i40evf_find_vlan(adapter, vlan);
  636. if (f) {
  637. f->remove = true;
  638. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  639. }
  640. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  641. }
  642. /**
  643. * i40evf_vlan_rx_add_vid - Add a VLAN filter to a device
  644. * @netdev: network device struct
  645. * @vid: VLAN tag
  646. **/
  647. static int i40evf_vlan_rx_add_vid(struct net_device *netdev,
  648. __always_unused __be16 proto, u16 vid)
  649. {
  650. struct i40evf_adapter *adapter = netdev_priv(netdev);
  651. if (!VLAN_ALLOWED(adapter))
  652. return -EIO;
  653. if (i40evf_add_vlan(adapter, vid) == NULL)
  654. return -ENOMEM;
  655. return 0;
  656. }
  657. /**
  658. * i40evf_vlan_rx_kill_vid - Remove a VLAN filter from a device
  659. * @netdev: network device struct
  660. * @vid: VLAN tag
  661. **/
  662. static int i40evf_vlan_rx_kill_vid(struct net_device *netdev,
  663. __always_unused __be16 proto, u16 vid)
  664. {
  665. struct i40evf_adapter *adapter = netdev_priv(netdev);
  666. if (VLAN_ALLOWED(adapter)) {
  667. i40evf_del_vlan(adapter, vid);
  668. return 0;
  669. }
  670. return -EIO;
  671. }
  672. /**
  673. * i40evf_find_filter - Search filter list for specific mac filter
  674. * @adapter: board private structure
  675. * @macaddr: the MAC address
  676. *
  677. * Returns ptr to the filter object or NULL. Must be called while holding the
  678. * mac_vlan_list_lock.
  679. **/
  680. static struct
  681. i40evf_mac_filter *i40evf_find_filter(struct i40evf_adapter *adapter,
  682. u8 *macaddr)
  683. {
  684. struct i40evf_mac_filter *f;
  685. if (!macaddr)
  686. return NULL;
  687. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  688. if (ether_addr_equal(macaddr, f->macaddr))
  689. return f;
  690. }
  691. return NULL;
  692. }
  693. /**
  694. * i40e_add_filter - Add a mac filter to the filter list
  695. * @adapter: board private structure
  696. * @macaddr: the MAC address
  697. *
  698. * Returns ptr to the filter object or NULL when no memory available.
  699. **/
  700. static struct
  701. i40evf_mac_filter *i40evf_add_filter(struct i40evf_adapter *adapter,
  702. u8 *macaddr)
  703. {
  704. struct i40evf_mac_filter *f;
  705. if (!macaddr)
  706. return NULL;
  707. spin_lock_bh(&adapter->mac_vlan_list_lock);
  708. f = i40evf_find_filter(adapter, macaddr);
  709. if (!f) {
  710. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  711. if (!f)
  712. goto clearout;
  713. ether_addr_copy(f->macaddr, macaddr);
  714. list_add_tail(&f->list, &adapter->mac_filter_list);
  715. f->add = true;
  716. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  717. } else {
  718. f->remove = false;
  719. }
  720. clearout:
  721. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  722. return f;
  723. }
  724. /**
  725. * i40evf_set_mac - NDO callback to set port mac address
  726. * @netdev: network interface device structure
  727. * @p: pointer to an address structure
  728. *
  729. * Returns 0 on success, negative on failure
  730. **/
  731. static int i40evf_set_mac(struct net_device *netdev, void *p)
  732. {
  733. struct i40evf_adapter *adapter = netdev_priv(netdev);
  734. struct i40e_hw *hw = &adapter->hw;
  735. struct i40evf_mac_filter *f;
  736. struct sockaddr *addr = p;
  737. if (!is_valid_ether_addr(addr->sa_data))
  738. return -EADDRNOTAVAIL;
  739. if (ether_addr_equal(netdev->dev_addr, addr->sa_data))
  740. return 0;
  741. if (adapter->flags & I40EVF_FLAG_ADDR_SET_BY_PF)
  742. return -EPERM;
  743. spin_lock_bh(&adapter->mac_vlan_list_lock);
  744. f = i40evf_find_filter(adapter, hw->mac.addr);
  745. if (f) {
  746. f->remove = true;
  747. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  748. }
  749. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  750. f = i40evf_add_filter(adapter, addr->sa_data);
  751. if (f) {
  752. ether_addr_copy(hw->mac.addr, addr->sa_data);
  753. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  754. }
  755. return (f == NULL) ? -ENOMEM : 0;
  756. }
  757. /**
  758. * i40evf_set_rx_mode - NDO callback to set the netdev filters
  759. * @netdev: network interface device structure
  760. **/
  761. static void i40evf_set_rx_mode(struct net_device *netdev)
  762. {
  763. struct i40evf_adapter *adapter = netdev_priv(netdev);
  764. struct i40evf_mac_filter *f, *ftmp;
  765. struct netdev_hw_addr *uca;
  766. struct netdev_hw_addr *mca;
  767. struct netdev_hw_addr *ha;
  768. /* add addr if not already in the filter list */
  769. netdev_for_each_uc_addr(uca, netdev) {
  770. i40evf_add_filter(adapter, uca->addr);
  771. }
  772. netdev_for_each_mc_addr(mca, netdev) {
  773. i40evf_add_filter(adapter, mca->addr);
  774. }
  775. spin_lock_bh(&adapter->mac_vlan_list_lock);
  776. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  777. netdev_for_each_mc_addr(mca, netdev)
  778. if (ether_addr_equal(mca->addr, f->macaddr))
  779. goto bottom_of_search_loop;
  780. netdev_for_each_uc_addr(uca, netdev)
  781. if (ether_addr_equal(uca->addr, f->macaddr))
  782. goto bottom_of_search_loop;
  783. for_each_dev_addr(netdev, ha)
  784. if (ether_addr_equal(ha->addr, f->macaddr))
  785. goto bottom_of_search_loop;
  786. if (ether_addr_equal(f->macaddr, adapter->hw.mac.addr))
  787. goto bottom_of_search_loop;
  788. /* f->macaddr wasn't found in uc, mc, or ha list so delete it */
  789. f->remove = true;
  790. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  791. bottom_of_search_loop:
  792. continue;
  793. }
  794. if (netdev->flags & IFF_PROMISC &&
  795. !(adapter->flags & I40EVF_FLAG_PROMISC_ON))
  796. adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_PROMISC;
  797. else if (!(netdev->flags & IFF_PROMISC) &&
  798. adapter->flags & I40EVF_FLAG_PROMISC_ON)
  799. adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_PROMISC;
  800. if (netdev->flags & IFF_ALLMULTI &&
  801. !(adapter->flags & I40EVF_FLAG_ALLMULTI_ON))
  802. adapter->aq_required |= I40EVF_FLAG_AQ_REQUEST_ALLMULTI;
  803. else if (!(netdev->flags & IFF_ALLMULTI) &&
  804. adapter->flags & I40EVF_FLAG_ALLMULTI_ON)
  805. adapter->aq_required |= I40EVF_FLAG_AQ_RELEASE_ALLMULTI;
  806. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  807. }
  808. /**
  809. * i40evf_napi_enable_all - enable NAPI on all queue vectors
  810. * @adapter: board private structure
  811. **/
  812. static void i40evf_napi_enable_all(struct i40evf_adapter *adapter)
  813. {
  814. int q_idx;
  815. struct i40e_q_vector *q_vector;
  816. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  817. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  818. struct napi_struct *napi;
  819. q_vector = &adapter->q_vectors[q_idx];
  820. napi = &q_vector->napi;
  821. napi_enable(napi);
  822. }
  823. }
  824. /**
  825. * i40evf_napi_disable_all - disable NAPI on all queue vectors
  826. * @adapter: board private structure
  827. **/
  828. static void i40evf_napi_disable_all(struct i40evf_adapter *adapter)
  829. {
  830. int q_idx;
  831. struct i40e_q_vector *q_vector;
  832. int q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  833. for (q_idx = 0; q_idx < q_vectors; q_idx++) {
  834. q_vector = &adapter->q_vectors[q_idx];
  835. napi_disable(&q_vector->napi);
  836. }
  837. }
  838. /**
  839. * i40evf_configure - set up transmit and receive data structures
  840. * @adapter: board private structure
  841. **/
  842. static void i40evf_configure(struct i40evf_adapter *adapter)
  843. {
  844. struct net_device *netdev = adapter->netdev;
  845. int i;
  846. i40evf_set_rx_mode(netdev);
  847. i40evf_configure_tx(adapter);
  848. i40evf_configure_rx(adapter);
  849. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_QUEUES;
  850. for (i = 0; i < adapter->num_active_queues; i++) {
  851. struct i40e_ring *ring = &adapter->rx_rings[i];
  852. i40evf_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
  853. }
  854. }
  855. /**
  856. * i40evf_up_complete - Finish the last steps of bringing up a connection
  857. * @adapter: board private structure
  858. *
  859. * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
  860. **/
  861. static void i40evf_up_complete(struct i40evf_adapter *adapter)
  862. {
  863. adapter->state = __I40EVF_RUNNING;
  864. clear_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  865. i40evf_napi_enable_all(adapter);
  866. adapter->aq_required |= I40EVF_FLAG_AQ_ENABLE_QUEUES;
  867. if (CLIENT_ENABLED(adapter))
  868. adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_OPEN;
  869. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  870. }
  871. /**
  872. * i40e_down - Shutdown the connection processing
  873. * @adapter: board private structure
  874. *
  875. * Expects to be called while holding the __I40EVF_IN_CRITICAL_TASK bit lock.
  876. **/
  877. void i40evf_down(struct i40evf_adapter *adapter)
  878. {
  879. struct net_device *netdev = adapter->netdev;
  880. struct i40evf_mac_filter *f;
  881. if (adapter->state <= __I40EVF_DOWN_PENDING)
  882. return;
  883. netif_carrier_off(netdev);
  884. netif_tx_disable(netdev);
  885. adapter->link_up = false;
  886. i40evf_napi_disable_all(adapter);
  887. i40evf_irq_disable(adapter);
  888. spin_lock_bh(&adapter->mac_vlan_list_lock);
  889. /* remove all MAC filters */
  890. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  891. f->remove = true;
  892. }
  893. /* remove all VLAN filters */
  894. list_for_each_entry(f, &adapter->vlan_filter_list, list) {
  895. f->remove = true;
  896. }
  897. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  898. if (!(adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) &&
  899. adapter->state != __I40EVF_RESETTING) {
  900. /* cancel any current operation */
  901. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  902. /* Schedule operations to close down the HW. Don't wait
  903. * here for this to complete. The watchdog is still running
  904. * and it will take care of this.
  905. */
  906. adapter->aq_required = I40EVF_FLAG_AQ_DEL_MAC_FILTER;
  907. adapter->aq_required |= I40EVF_FLAG_AQ_DEL_VLAN_FILTER;
  908. adapter->aq_required |= I40EVF_FLAG_AQ_DISABLE_QUEUES;
  909. }
  910. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  911. }
  912. /**
  913. * i40evf_acquire_msix_vectors - Setup the MSIX capability
  914. * @adapter: board private structure
  915. * @vectors: number of vectors to request
  916. *
  917. * Work with the OS to set up the MSIX vectors needed.
  918. *
  919. * Returns 0 on success, negative on failure
  920. **/
  921. static int
  922. i40evf_acquire_msix_vectors(struct i40evf_adapter *adapter, int vectors)
  923. {
  924. int err, vector_threshold;
  925. /* We'll want at least 3 (vector_threshold):
  926. * 0) Other (Admin Queue and link, mostly)
  927. * 1) TxQ[0] Cleanup
  928. * 2) RxQ[0] Cleanup
  929. */
  930. vector_threshold = MIN_MSIX_COUNT;
  931. /* The more we get, the more we will assign to Tx/Rx Cleanup
  932. * for the separate queues...where Rx Cleanup >= Tx Cleanup.
  933. * Right now, we simply care about how many we'll get; we'll
  934. * set them up later while requesting irq's.
  935. */
  936. err = pci_enable_msix_range(adapter->pdev, adapter->msix_entries,
  937. vector_threshold, vectors);
  938. if (err < 0) {
  939. dev_err(&adapter->pdev->dev, "Unable to allocate MSI-X interrupts\n");
  940. kfree(adapter->msix_entries);
  941. adapter->msix_entries = NULL;
  942. return err;
  943. }
  944. /* Adjust for only the vectors we'll use, which is minimum
  945. * of max_msix_q_vectors + NONQ_VECS, or the number of
  946. * vectors we were allocated.
  947. */
  948. adapter->num_msix_vectors = err;
  949. return 0;
  950. }
  951. /**
  952. * i40evf_free_queues - Free memory for all rings
  953. * @adapter: board private structure to initialize
  954. *
  955. * Free all of the memory associated with queue pairs.
  956. **/
  957. static void i40evf_free_queues(struct i40evf_adapter *adapter)
  958. {
  959. if (!adapter->vsi_res)
  960. return;
  961. adapter->num_active_queues = 0;
  962. kfree(adapter->tx_rings);
  963. adapter->tx_rings = NULL;
  964. kfree(adapter->rx_rings);
  965. adapter->rx_rings = NULL;
  966. }
  967. /**
  968. * i40evf_alloc_queues - Allocate memory for all rings
  969. * @adapter: board private structure to initialize
  970. *
  971. * We allocate one ring per queue at run-time since we don't know the
  972. * number of queues at compile-time. The polling_netdev array is
  973. * intended for Multiqueue, but should work fine with a single queue.
  974. **/
  975. static int i40evf_alloc_queues(struct i40evf_adapter *adapter)
  976. {
  977. int i, num_active_queues;
  978. /* If we're in reset reallocating queues we don't actually know yet for
  979. * certain the PF gave us the number of queues we asked for but we'll
  980. * assume it did. Once basic reset is finished we'll confirm once we
  981. * start negotiating config with PF.
  982. */
  983. if (adapter->num_req_queues)
  984. num_active_queues = adapter->num_req_queues;
  985. else
  986. num_active_queues = min_t(int,
  987. adapter->vsi_res->num_queue_pairs,
  988. (int)(num_online_cpus()));
  989. adapter->tx_rings = kcalloc(num_active_queues,
  990. sizeof(struct i40e_ring), GFP_KERNEL);
  991. if (!adapter->tx_rings)
  992. goto err_out;
  993. adapter->rx_rings = kcalloc(num_active_queues,
  994. sizeof(struct i40e_ring), GFP_KERNEL);
  995. if (!adapter->rx_rings)
  996. goto err_out;
  997. for (i = 0; i < num_active_queues; i++) {
  998. struct i40e_ring *tx_ring;
  999. struct i40e_ring *rx_ring;
  1000. tx_ring = &adapter->tx_rings[i];
  1001. tx_ring->queue_index = i;
  1002. tx_ring->netdev = adapter->netdev;
  1003. tx_ring->dev = &adapter->pdev->dev;
  1004. tx_ring->count = adapter->tx_desc_count;
  1005. tx_ring->itr_setting = I40E_ITR_TX_DEF;
  1006. if (adapter->flags & I40EVF_FLAG_WB_ON_ITR_CAPABLE)
  1007. tx_ring->flags |= I40E_TXR_FLAGS_WB_ON_ITR;
  1008. rx_ring = &adapter->rx_rings[i];
  1009. rx_ring->queue_index = i;
  1010. rx_ring->netdev = adapter->netdev;
  1011. rx_ring->dev = &adapter->pdev->dev;
  1012. rx_ring->count = adapter->rx_desc_count;
  1013. rx_ring->itr_setting = I40E_ITR_RX_DEF;
  1014. }
  1015. adapter->num_active_queues = num_active_queues;
  1016. return 0;
  1017. err_out:
  1018. i40evf_free_queues(adapter);
  1019. return -ENOMEM;
  1020. }
  1021. /**
  1022. * i40evf_set_interrupt_capability - set MSI-X or FAIL if not supported
  1023. * @adapter: board private structure to initialize
  1024. *
  1025. * Attempt to configure the interrupts using the best available
  1026. * capabilities of the hardware and the kernel.
  1027. **/
  1028. static int i40evf_set_interrupt_capability(struct i40evf_adapter *adapter)
  1029. {
  1030. int vector, v_budget;
  1031. int pairs = 0;
  1032. int err = 0;
  1033. if (!adapter->vsi_res) {
  1034. err = -EIO;
  1035. goto out;
  1036. }
  1037. pairs = adapter->num_active_queues;
  1038. /* It's easy to be greedy for MSI-X vectors, but it really doesn't do
  1039. * us much good if we have more vectors than CPUs. However, we already
  1040. * limit the total number of queues by the number of CPUs so we do not
  1041. * need any further limiting here.
  1042. */
  1043. v_budget = min_t(int, pairs + NONQ_VECS,
  1044. (int)adapter->vf_res->max_vectors);
  1045. adapter->msix_entries = kcalloc(v_budget,
  1046. sizeof(struct msix_entry), GFP_KERNEL);
  1047. if (!adapter->msix_entries) {
  1048. err = -ENOMEM;
  1049. goto out;
  1050. }
  1051. for (vector = 0; vector < v_budget; vector++)
  1052. adapter->msix_entries[vector].entry = vector;
  1053. err = i40evf_acquire_msix_vectors(adapter, v_budget);
  1054. out:
  1055. netif_set_real_num_rx_queues(adapter->netdev, pairs);
  1056. netif_set_real_num_tx_queues(adapter->netdev, pairs);
  1057. return err;
  1058. }
  1059. /**
  1060. * i40e_config_rss_aq - Configure RSS keys and lut by using AQ commands
  1061. * @adapter: board private structure
  1062. *
  1063. * Return 0 on success, negative on failure
  1064. **/
  1065. static int i40evf_config_rss_aq(struct i40evf_adapter *adapter)
  1066. {
  1067. struct i40e_aqc_get_set_rss_key_data *rss_key =
  1068. (struct i40e_aqc_get_set_rss_key_data *)adapter->rss_key;
  1069. struct i40e_hw *hw = &adapter->hw;
  1070. int ret = 0;
  1071. if (adapter->current_op != VIRTCHNL_OP_UNKNOWN) {
  1072. /* bail because we already have a command pending */
  1073. dev_err(&adapter->pdev->dev, "Cannot configure RSS, command %d pending\n",
  1074. adapter->current_op);
  1075. return -EBUSY;
  1076. }
  1077. ret = i40evf_aq_set_rss_key(hw, adapter->vsi.id, rss_key);
  1078. if (ret) {
  1079. dev_err(&adapter->pdev->dev, "Cannot set RSS key, err %s aq_err %s\n",
  1080. i40evf_stat_str(hw, ret),
  1081. i40evf_aq_str(hw, hw->aq.asq_last_status));
  1082. return ret;
  1083. }
  1084. ret = i40evf_aq_set_rss_lut(hw, adapter->vsi.id, false,
  1085. adapter->rss_lut, adapter->rss_lut_size);
  1086. if (ret) {
  1087. dev_err(&adapter->pdev->dev, "Cannot set RSS lut, err %s aq_err %s\n",
  1088. i40evf_stat_str(hw, ret),
  1089. i40evf_aq_str(hw, hw->aq.asq_last_status));
  1090. }
  1091. return ret;
  1092. }
  1093. /**
  1094. * i40evf_config_rss_reg - Configure RSS keys and lut by writing registers
  1095. * @adapter: board private structure
  1096. *
  1097. * Returns 0 on success, negative on failure
  1098. **/
  1099. static int i40evf_config_rss_reg(struct i40evf_adapter *adapter)
  1100. {
  1101. struct i40e_hw *hw = &adapter->hw;
  1102. u32 *dw;
  1103. u16 i;
  1104. dw = (u32 *)adapter->rss_key;
  1105. for (i = 0; i <= adapter->rss_key_size / 4; i++)
  1106. wr32(hw, I40E_VFQF_HKEY(i), dw[i]);
  1107. dw = (u32 *)adapter->rss_lut;
  1108. for (i = 0; i <= adapter->rss_lut_size / 4; i++)
  1109. wr32(hw, I40E_VFQF_HLUT(i), dw[i]);
  1110. i40e_flush(hw);
  1111. return 0;
  1112. }
  1113. /**
  1114. * i40evf_config_rss - Configure RSS keys and lut
  1115. * @adapter: board private structure
  1116. *
  1117. * Returns 0 on success, negative on failure
  1118. **/
  1119. int i40evf_config_rss(struct i40evf_adapter *adapter)
  1120. {
  1121. if (RSS_PF(adapter)) {
  1122. adapter->aq_required |= I40EVF_FLAG_AQ_SET_RSS_LUT |
  1123. I40EVF_FLAG_AQ_SET_RSS_KEY;
  1124. return 0;
  1125. } else if (RSS_AQ(adapter)) {
  1126. return i40evf_config_rss_aq(adapter);
  1127. } else {
  1128. return i40evf_config_rss_reg(adapter);
  1129. }
  1130. }
  1131. /**
  1132. * i40evf_fill_rss_lut - Fill the lut with default values
  1133. * @adapter: board private structure
  1134. **/
  1135. static void i40evf_fill_rss_lut(struct i40evf_adapter *adapter)
  1136. {
  1137. u16 i;
  1138. for (i = 0; i < adapter->rss_lut_size; i++)
  1139. adapter->rss_lut[i] = i % adapter->num_active_queues;
  1140. }
  1141. /**
  1142. * i40evf_init_rss - Prepare for RSS
  1143. * @adapter: board private structure
  1144. *
  1145. * Return 0 on success, negative on failure
  1146. **/
  1147. static int i40evf_init_rss(struct i40evf_adapter *adapter)
  1148. {
  1149. struct i40e_hw *hw = &adapter->hw;
  1150. int ret;
  1151. if (!RSS_PF(adapter)) {
  1152. /* Enable PCTYPES for RSS, TCP/UDP with IPv4/IPv6 */
  1153. if (adapter->vf_res->vf_cap_flags &
  1154. VIRTCHNL_VF_OFFLOAD_RSS_PCTYPE_V2)
  1155. adapter->hena = I40E_DEFAULT_RSS_HENA_EXPANDED;
  1156. else
  1157. adapter->hena = I40E_DEFAULT_RSS_HENA;
  1158. wr32(hw, I40E_VFQF_HENA(0), (u32)adapter->hena);
  1159. wr32(hw, I40E_VFQF_HENA(1), (u32)(adapter->hena >> 32));
  1160. }
  1161. i40evf_fill_rss_lut(adapter);
  1162. netdev_rss_key_fill((void *)adapter->rss_key, adapter->rss_key_size);
  1163. ret = i40evf_config_rss(adapter);
  1164. return ret;
  1165. }
  1166. /**
  1167. * i40evf_alloc_q_vectors - Allocate memory for interrupt vectors
  1168. * @adapter: board private structure to initialize
  1169. *
  1170. * We allocate one q_vector per queue interrupt. If allocation fails we
  1171. * return -ENOMEM.
  1172. **/
  1173. static int i40evf_alloc_q_vectors(struct i40evf_adapter *adapter)
  1174. {
  1175. int q_idx = 0, num_q_vectors;
  1176. struct i40e_q_vector *q_vector;
  1177. num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  1178. adapter->q_vectors = kcalloc(num_q_vectors, sizeof(*q_vector),
  1179. GFP_KERNEL);
  1180. if (!adapter->q_vectors)
  1181. return -ENOMEM;
  1182. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1183. q_vector = &adapter->q_vectors[q_idx];
  1184. q_vector->adapter = adapter;
  1185. q_vector->vsi = &adapter->vsi;
  1186. q_vector->v_idx = q_idx;
  1187. q_vector->reg_idx = q_idx;
  1188. cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
  1189. netif_napi_add(adapter->netdev, &q_vector->napi,
  1190. i40evf_napi_poll, NAPI_POLL_WEIGHT);
  1191. }
  1192. return 0;
  1193. }
  1194. /**
  1195. * i40evf_free_q_vectors - Free memory allocated for interrupt vectors
  1196. * @adapter: board private structure to initialize
  1197. *
  1198. * This function frees the memory allocated to the q_vectors. In addition if
  1199. * NAPI is enabled it will delete any references to the NAPI struct prior
  1200. * to freeing the q_vector.
  1201. **/
  1202. static void i40evf_free_q_vectors(struct i40evf_adapter *adapter)
  1203. {
  1204. int q_idx, num_q_vectors;
  1205. int napi_vectors;
  1206. if (!adapter->q_vectors)
  1207. return;
  1208. num_q_vectors = adapter->num_msix_vectors - NONQ_VECS;
  1209. napi_vectors = adapter->num_active_queues;
  1210. for (q_idx = 0; q_idx < num_q_vectors; q_idx++) {
  1211. struct i40e_q_vector *q_vector = &adapter->q_vectors[q_idx];
  1212. if (q_idx < napi_vectors)
  1213. netif_napi_del(&q_vector->napi);
  1214. }
  1215. kfree(adapter->q_vectors);
  1216. adapter->q_vectors = NULL;
  1217. }
  1218. /**
  1219. * i40evf_reset_interrupt_capability - Reset MSIX setup
  1220. * @adapter: board private structure
  1221. *
  1222. **/
  1223. void i40evf_reset_interrupt_capability(struct i40evf_adapter *adapter)
  1224. {
  1225. if (!adapter->msix_entries)
  1226. return;
  1227. pci_disable_msix(adapter->pdev);
  1228. kfree(adapter->msix_entries);
  1229. adapter->msix_entries = NULL;
  1230. }
  1231. /**
  1232. * i40evf_init_interrupt_scheme - Determine if MSIX is supported and init
  1233. * @adapter: board private structure to initialize
  1234. *
  1235. **/
  1236. int i40evf_init_interrupt_scheme(struct i40evf_adapter *adapter)
  1237. {
  1238. int err;
  1239. err = i40evf_alloc_queues(adapter);
  1240. if (err) {
  1241. dev_err(&adapter->pdev->dev,
  1242. "Unable to allocate memory for queues\n");
  1243. goto err_alloc_queues;
  1244. }
  1245. rtnl_lock();
  1246. err = i40evf_set_interrupt_capability(adapter);
  1247. rtnl_unlock();
  1248. if (err) {
  1249. dev_err(&adapter->pdev->dev,
  1250. "Unable to setup interrupt capabilities\n");
  1251. goto err_set_interrupt;
  1252. }
  1253. err = i40evf_alloc_q_vectors(adapter);
  1254. if (err) {
  1255. dev_err(&adapter->pdev->dev,
  1256. "Unable to allocate memory for queue vectors\n");
  1257. goto err_alloc_q_vectors;
  1258. }
  1259. dev_info(&adapter->pdev->dev, "Multiqueue %s: Queue pair count = %u",
  1260. (adapter->num_active_queues > 1) ? "Enabled" : "Disabled",
  1261. adapter->num_active_queues);
  1262. return 0;
  1263. err_alloc_q_vectors:
  1264. i40evf_reset_interrupt_capability(adapter);
  1265. err_set_interrupt:
  1266. i40evf_free_queues(adapter);
  1267. err_alloc_queues:
  1268. return err;
  1269. }
  1270. /**
  1271. * i40evf_free_rss - Free memory used by RSS structs
  1272. * @adapter: board private structure
  1273. **/
  1274. static void i40evf_free_rss(struct i40evf_adapter *adapter)
  1275. {
  1276. kfree(adapter->rss_key);
  1277. adapter->rss_key = NULL;
  1278. kfree(adapter->rss_lut);
  1279. adapter->rss_lut = NULL;
  1280. }
  1281. /**
  1282. * i40evf_reinit_interrupt_scheme - Reallocate queues and vectors
  1283. * @adapter: board private structure
  1284. *
  1285. * Returns 0 on success, negative on failure
  1286. **/
  1287. static int i40evf_reinit_interrupt_scheme(struct i40evf_adapter *adapter)
  1288. {
  1289. struct net_device *netdev = adapter->netdev;
  1290. int err;
  1291. if (netif_running(netdev))
  1292. i40evf_free_traffic_irqs(adapter);
  1293. i40evf_free_misc_irq(adapter);
  1294. i40evf_reset_interrupt_capability(adapter);
  1295. i40evf_free_q_vectors(adapter);
  1296. i40evf_free_queues(adapter);
  1297. err = i40evf_init_interrupt_scheme(adapter);
  1298. if (err)
  1299. goto err;
  1300. netif_tx_stop_all_queues(netdev);
  1301. err = i40evf_request_misc_irq(adapter);
  1302. if (err)
  1303. goto err;
  1304. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  1305. i40evf_map_rings_to_vectors(adapter);
  1306. if (RSS_AQ(adapter))
  1307. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
  1308. else
  1309. err = i40evf_init_rss(adapter);
  1310. err:
  1311. return err;
  1312. }
  1313. /**
  1314. * i40evf_watchdog_timer - Periodic call-back timer
  1315. * @data: pointer to adapter disguised as unsigned long
  1316. **/
  1317. static void i40evf_watchdog_timer(struct timer_list *t)
  1318. {
  1319. struct i40evf_adapter *adapter = from_timer(adapter, t,
  1320. watchdog_timer);
  1321. schedule_work(&adapter->watchdog_task);
  1322. /* timer will be rescheduled in watchdog task */
  1323. }
  1324. /**
  1325. * i40evf_watchdog_task - Periodic call-back task
  1326. * @work: pointer to work_struct
  1327. **/
  1328. static void i40evf_watchdog_task(struct work_struct *work)
  1329. {
  1330. struct i40evf_adapter *adapter = container_of(work,
  1331. struct i40evf_adapter,
  1332. watchdog_task);
  1333. struct i40e_hw *hw = &adapter->hw;
  1334. u32 reg_val;
  1335. if (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section))
  1336. goto restart_watchdog;
  1337. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
  1338. reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
  1339. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1340. if ((reg_val == VIRTCHNL_VFR_VFACTIVE) ||
  1341. (reg_val == VIRTCHNL_VFR_COMPLETED)) {
  1342. /* A chance for redemption! */
  1343. dev_err(&adapter->pdev->dev, "Hardware came out of reset. Attempting reinit.\n");
  1344. adapter->state = __I40EVF_STARTUP;
  1345. adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
  1346. schedule_delayed_work(&adapter->init_task, 10);
  1347. clear_bit(__I40EVF_IN_CRITICAL_TASK,
  1348. &adapter->crit_section);
  1349. /* Don't reschedule the watchdog, since we've restarted
  1350. * the init task. When init_task contacts the PF and
  1351. * gets everything set up again, it'll restart the
  1352. * watchdog for us. Down, boy. Sit. Stay. Woof.
  1353. */
  1354. return;
  1355. }
  1356. adapter->aq_required = 0;
  1357. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1358. goto watchdog_done;
  1359. }
  1360. if ((adapter->state < __I40EVF_DOWN) ||
  1361. (adapter->flags & I40EVF_FLAG_RESET_PENDING))
  1362. goto watchdog_done;
  1363. /* check for reset */
  1364. reg_val = rd32(hw, I40E_VF_ARQLEN1) & I40E_VF_ARQLEN1_ARQENABLE_MASK;
  1365. if (!(adapter->flags & I40EVF_FLAG_RESET_PENDING) && !reg_val) {
  1366. adapter->state = __I40EVF_RESETTING;
  1367. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  1368. dev_err(&adapter->pdev->dev, "Hardware reset detected\n");
  1369. schedule_work(&adapter->reset_task);
  1370. adapter->aq_required = 0;
  1371. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1372. goto watchdog_done;
  1373. }
  1374. /* Process admin queue tasks. After init, everything gets done
  1375. * here so we don't race on the admin queue.
  1376. */
  1377. if (adapter->current_op) {
  1378. if (!i40evf_asq_done(hw)) {
  1379. dev_dbg(&adapter->pdev->dev, "Admin queue timeout\n");
  1380. i40evf_send_api_ver(adapter);
  1381. }
  1382. goto watchdog_done;
  1383. }
  1384. if (adapter->aq_required & I40EVF_FLAG_AQ_GET_CONFIG) {
  1385. i40evf_send_vf_config_msg(adapter);
  1386. goto watchdog_done;
  1387. }
  1388. if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_QUEUES) {
  1389. i40evf_disable_queues(adapter);
  1390. goto watchdog_done;
  1391. }
  1392. if (adapter->aq_required & I40EVF_FLAG_AQ_MAP_VECTORS) {
  1393. i40evf_map_queues(adapter);
  1394. goto watchdog_done;
  1395. }
  1396. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_MAC_FILTER) {
  1397. i40evf_add_ether_addrs(adapter);
  1398. goto watchdog_done;
  1399. }
  1400. if (adapter->aq_required & I40EVF_FLAG_AQ_ADD_VLAN_FILTER) {
  1401. i40evf_add_vlans(adapter);
  1402. goto watchdog_done;
  1403. }
  1404. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_MAC_FILTER) {
  1405. i40evf_del_ether_addrs(adapter);
  1406. goto watchdog_done;
  1407. }
  1408. if (adapter->aq_required & I40EVF_FLAG_AQ_DEL_VLAN_FILTER) {
  1409. i40evf_del_vlans(adapter);
  1410. goto watchdog_done;
  1411. }
  1412. if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING) {
  1413. i40evf_enable_vlan_stripping(adapter);
  1414. goto watchdog_done;
  1415. }
  1416. if (adapter->aq_required & I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING) {
  1417. i40evf_disable_vlan_stripping(adapter);
  1418. goto watchdog_done;
  1419. }
  1420. if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_QUEUES) {
  1421. i40evf_configure_queues(adapter);
  1422. goto watchdog_done;
  1423. }
  1424. if (adapter->aq_required & I40EVF_FLAG_AQ_ENABLE_QUEUES) {
  1425. i40evf_enable_queues(adapter);
  1426. goto watchdog_done;
  1427. }
  1428. if (adapter->aq_required & I40EVF_FLAG_AQ_CONFIGURE_RSS) {
  1429. /* This message goes straight to the firmware, not the
  1430. * PF, so we don't have to set current_op as we will
  1431. * not get a response through the ARQ.
  1432. */
  1433. i40evf_init_rss(adapter);
  1434. adapter->aq_required &= ~I40EVF_FLAG_AQ_CONFIGURE_RSS;
  1435. goto watchdog_done;
  1436. }
  1437. if (adapter->aq_required & I40EVF_FLAG_AQ_GET_HENA) {
  1438. i40evf_get_hena(adapter);
  1439. goto watchdog_done;
  1440. }
  1441. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_HENA) {
  1442. i40evf_set_hena(adapter);
  1443. goto watchdog_done;
  1444. }
  1445. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_KEY) {
  1446. i40evf_set_rss_key(adapter);
  1447. goto watchdog_done;
  1448. }
  1449. if (adapter->aq_required & I40EVF_FLAG_AQ_SET_RSS_LUT) {
  1450. i40evf_set_rss_lut(adapter);
  1451. goto watchdog_done;
  1452. }
  1453. if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_PROMISC) {
  1454. i40evf_set_promiscuous(adapter, FLAG_VF_UNICAST_PROMISC |
  1455. FLAG_VF_MULTICAST_PROMISC);
  1456. goto watchdog_done;
  1457. }
  1458. if (adapter->aq_required & I40EVF_FLAG_AQ_REQUEST_ALLMULTI) {
  1459. i40evf_set_promiscuous(adapter, FLAG_VF_MULTICAST_PROMISC);
  1460. goto watchdog_done;
  1461. }
  1462. if ((adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_PROMISC) &&
  1463. (adapter->aq_required & I40EVF_FLAG_AQ_RELEASE_ALLMULTI)) {
  1464. i40evf_set_promiscuous(adapter, 0);
  1465. goto watchdog_done;
  1466. }
  1467. schedule_delayed_work(&adapter->client_task, msecs_to_jiffies(5));
  1468. if (adapter->state == __I40EVF_RUNNING)
  1469. i40evf_request_stats(adapter);
  1470. watchdog_done:
  1471. if (adapter->state == __I40EVF_RUNNING)
  1472. i40evf_detect_recover_hung(&adapter->vsi);
  1473. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1474. restart_watchdog:
  1475. if (adapter->state == __I40EVF_REMOVE)
  1476. return;
  1477. if (adapter->aq_required)
  1478. mod_timer(&adapter->watchdog_timer,
  1479. jiffies + msecs_to_jiffies(20));
  1480. else
  1481. mod_timer(&adapter->watchdog_timer, jiffies + (HZ * 2));
  1482. schedule_work(&adapter->adminq_task);
  1483. }
  1484. static void i40evf_disable_vf(struct i40evf_adapter *adapter)
  1485. {
  1486. struct i40evf_mac_filter *f, *ftmp;
  1487. struct i40evf_vlan_filter *fv, *fvtmp;
  1488. adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
  1489. /* We don't use netif_running() because it may be true prior to
  1490. * ndo_open() returning, so we can't assume it means all our open
  1491. * tasks have finished, since we're not holding the rtnl_lock here.
  1492. */
  1493. if (adapter->state == __I40EVF_RUNNING) {
  1494. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  1495. netif_carrier_off(adapter->netdev);
  1496. netif_tx_disable(adapter->netdev);
  1497. adapter->link_up = false;
  1498. i40evf_napi_disable_all(adapter);
  1499. i40evf_irq_disable(adapter);
  1500. i40evf_free_traffic_irqs(adapter);
  1501. i40evf_free_all_tx_resources(adapter);
  1502. i40evf_free_all_rx_resources(adapter);
  1503. }
  1504. spin_lock_bh(&adapter->mac_vlan_list_lock);
  1505. /* Delete all of the filters, both MAC and VLAN. */
  1506. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  1507. list_del(&f->list);
  1508. kfree(f);
  1509. }
  1510. list_for_each_entry_safe(fv, fvtmp, &adapter->vlan_filter_list, list) {
  1511. list_del(&fv->list);
  1512. kfree(fv);
  1513. }
  1514. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  1515. i40evf_free_misc_irq(adapter);
  1516. i40evf_reset_interrupt_capability(adapter);
  1517. i40evf_free_queues(adapter);
  1518. i40evf_free_q_vectors(adapter);
  1519. kfree(adapter->vf_res);
  1520. i40evf_shutdown_adminq(&adapter->hw);
  1521. adapter->netdev->flags &= ~IFF_UP;
  1522. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1523. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1524. adapter->state = __I40EVF_DOWN;
  1525. wake_up(&adapter->down_waitqueue);
  1526. dev_info(&adapter->pdev->dev, "Reset task did not complete, VF disabled\n");
  1527. }
  1528. #define I40EVF_RESET_WAIT_MS 10
  1529. #define I40EVF_RESET_WAIT_COUNT 500
  1530. /**
  1531. * i40evf_reset_task - Call-back task to handle hardware reset
  1532. * @work: pointer to work_struct
  1533. *
  1534. * During reset we need to shut down and reinitialize the admin queue
  1535. * before we can use it to communicate with the PF again. We also clear
  1536. * and reinit the rings because that context is lost as well.
  1537. **/
  1538. static void i40evf_reset_task(struct work_struct *work)
  1539. {
  1540. struct i40evf_adapter *adapter = container_of(work,
  1541. struct i40evf_adapter,
  1542. reset_task);
  1543. struct net_device *netdev = adapter->netdev;
  1544. struct i40e_hw *hw = &adapter->hw;
  1545. struct i40evf_vlan_filter *vlf;
  1546. struct i40evf_mac_filter *f;
  1547. u32 reg_val;
  1548. int i = 0, err;
  1549. bool running;
  1550. /* When device is being removed it doesn't make sense to run the reset
  1551. * task, just return in such a case.
  1552. */
  1553. if (test_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section))
  1554. return;
  1555. while (test_and_set_bit(__I40EVF_IN_CLIENT_TASK,
  1556. &adapter->crit_section))
  1557. usleep_range(500, 1000);
  1558. if (CLIENT_ENABLED(adapter)) {
  1559. adapter->flags &= ~(I40EVF_FLAG_CLIENT_NEEDS_OPEN |
  1560. I40EVF_FLAG_CLIENT_NEEDS_CLOSE |
  1561. I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS |
  1562. I40EVF_FLAG_SERVICE_CLIENT_REQUESTED);
  1563. cancel_delayed_work_sync(&adapter->client_task);
  1564. i40evf_notify_client_close(&adapter->vsi, true);
  1565. }
  1566. i40evf_misc_irq_disable(adapter);
  1567. if (adapter->flags & I40EVF_FLAG_RESET_NEEDED) {
  1568. adapter->flags &= ~I40EVF_FLAG_RESET_NEEDED;
  1569. /* Restart the AQ here. If we have been reset but didn't
  1570. * detect it, or if the PF had to reinit, our AQ will be hosed.
  1571. */
  1572. i40evf_shutdown_adminq(hw);
  1573. i40evf_init_adminq(hw);
  1574. i40evf_request_reset(adapter);
  1575. }
  1576. adapter->flags |= I40EVF_FLAG_RESET_PENDING;
  1577. /* poll until we see the reset actually happen */
  1578. for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
  1579. reg_val = rd32(hw, I40E_VF_ARQLEN1) &
  1580. I40E_VF_ARQLEN1_ARQENABLE_MASK;
  1581. if (!reg_val)
  1582. break;
  1583. usleep_range(5000, 10000);
  1584. }
  1585. if (i == I40EVF_RESET_WAIT_COUNT) {
  1586. dev_info(&adapter->pdev->dev, "Never saw reset\n");
  1587. goto continue_reset; /* act like the reset happened */
  1588. }
  1589. /* wait until the reset is complete and the PF is responding to us */
  1590. for (i = 0; i < I40EVF_RESET_WAIT_COUNT; i++) {
  1591. /* sleep first to make sure a minimum wait time is met */
  1592. msleep(I40EVF_RESET_WAIT_MS);
  1593. reg_val = rd32(hw, I40E_VFGEN_RSTAT) &
  1594. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  1595. if (reg_val == VIRTCHNL_VFR_VFACTIVE)
  1596. break;
  1597. }
  1598. pci_set_master(adapter->pdev);
  1599. if (i == I40EVF_RESET_WAIT_COUNT) {
  1600. dev_err(&adapter->pdev->dev, "Reset never finished (%x)\n",
  1601. reg_val);
  1602. i40evf_disable_vf(adapter);
  1603. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1604. return; /* Do not attempt to reinit. It's dead, Jim. */
  1605. }
  1606. continue_reset:
  1607. /* We don't use netif_running() because it may be true prior to
  1608. * ndo_open() returning, so we can't assume it means all our open
  1609. * tasks have finished, since we're not holding the rtnl_lock here.
  1610. */
  1611. running = (adapter->state == __I40EVF_RUNNING);
  1612. if (running) {
  1613. netif_carrier_off(netdev);
  1614. netif_tx_stop_all_queues(netdev);
  1615. adapter->link_up = false;
  1616. i40evf_napi_disable_all(adapter);
  1617. }
  1618. i40evf_irq_disable(adapter);
  1619. adapter->state = __I40EVF_RESETTING;
  1620. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  1621. /* free the Tx/Rx rings and descriptors, might be better to just
  1622. * re-use them sometime in the future
  1623. */
  1624. i40evf_free_all_rx_resources(adapter);
  1625. i40evf_free_all_tx_resources(adapter);
  1626. /* kill and reinit the admin queue */
  1627. i40evf_shutdown_adminq(hw);
  1628. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  1629. err = i40evf_init_adminq(hw);
  1630. if (err)
  1631. dev_info(&adapter->pdev->dev, "Failed to init adminq: %d\n",
  1632. err);
  1633. adapter->aq_required = 0;
  1634. if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
  1635. err = i40evf_reinit_interrupt_scheme(adapter);
  1636. if (err)
  1637. goto reset_err;
  1638. }
  1639. adapter->aq_required |= I40EVF_FLAG_AQ_GET_CONFIG;
  1640. adapter->aq_required |= I40EVF_FLAG_AQ_MAP_VECTORS;
  1641. spin_lock_bh(&adapter->mac_vlan_list_lock);
  1642. /* re-add all MAC filters */
  1643. list_for_each_entry(f, &adapter->mac_filter_list, list) {
  1644. f->add = true;
  1645. }
  1646. /* re-add all VLAN filters */
  1647. list_for_each_entry(vlf, &adapter->vlan_filter_list, list) {
  1648. vlf->add = true;
  1649. }
  1650. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  1651. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_MAC_FILTER;
  1652. adapter->aq_required |= I40EVF_FLAG_AQ_ADD_VLAN_FILTER;
  1653. i40evf_misc_irq_enable(adapter);
  1654. mod_timer(&adapter->watchdog_timer, jiffies + 2);
  1655. /* We were running when the reset started, so we need to restore some
  1656. * state here.
  1657. */
  1658. if (running) {
  1659. /* allocate transmit descriptors */
  1660. err = i40evf_setup_all_tx_resources(adapter);
  1661. if (err)
  1662. goto reset_err;
  1663. /* allocate receive descriptors */
  1664. err = i40evf_setup_all_rx_resources(adapter);
  1665. if (err)
  1666. goto reset_err;
  1667. if (adapter->flags & I40EVF_FLAG_REINIT_ITR_NEEDED) {
  1668. err = i40evf_request_traffic_irqs(adapter,
  1669. netdev->name);
  1670. if (err)
  1671. goto reset_err;
  1672. adapter->flags &= ~I40EVF_FLAG_REINIT_ITR_NEEDED;
  1673. }
  1674. i40evf_configure(adapter);
  1675. i40evf_up_complete(adapter);
  1676. i40evf_irq_enable(adapter, true);
  1677. } else {
  1678. adapter->state = __I40EVF_DOWN;
  1679. wake_up(&adapter->down_waitqueue);
  1680. }
  1681. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1682. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1683. return;
  1684. reset_err:
  1685. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1686. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1687. dev_err(&adapter->pdev->dev, "failed to allocate resources during reinit\n");
  1688. i40evf_close(netdev);
  1689. }
  1690. /**
  1691. * i40evf_adminq_task - worker thread to clean the admin queue
  1692. * @work: pointer to work_struct containing our data
  1693. **/
  1694. static void i40evf_adminq_task(struct work_struct *work)
  1695. {
  1696. struct i40evf_adapter *adapter =
  1697. container_of(work, struct i40evf_adapter, adminq_task);
  1698. struct i40e_hw *hw = &adapter->hw;
  1699. struct i40e_arq_event_info event;
  1700. enum virtchnl_ops v_op;
  1701. i40e_status ret, v_ret;
  1702. u32 val, oldval;
  1703. u16 pending;
  1704. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED)
  1705. goto out;
  1706. event.buf_len = I40EVF_MAX_AQ_BUF_SIZE;
  1707. event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
  1708. if (!event.msg_buf)
  1709. goto out;
  1710. do {
  1711. ret = i40evf_clean_arq_element(hw, &event, &pending);
  1712. v_op = (enum virtchnl_ops)le32_to_cpu(event.desc.cookie_high);
  1713. v_ret = (i40e_status)le32_to_cpu(event.desc.cookie_low);
  1714. if (ret || !v_op)
  1715. break; /* No event to process or error cleaning ARQ */
  1716. i40evf_virtchnl_completion(adapter, v_op, v_ret, event.msg_buf,
  1717. event.msg_len);
  1718. if (pending != 0)
  1719. memset(event.msg_buf, 0, I40EVF_MAX_AQ_BUF_SIZE);
  1720. } while (pending);
  1721. if ((adapter->flags &
  1722. (I40EVF_FLAG_RESET_PENDING | I40EVF_FLAG_RESET_NEEDED)) ||
  1723. adapter->state == __I40EVF_RESETTING)
  1724. goto freedom;
  1725. /* check for error indications */
  1726. val = rd32(hw, hw->aq.arq.len);
  1727. if (val == 0xdeadbeef) /* indicates device in reset */
  1728. goto freedom;
  1729. oldval = val;
  1730. if (val & I40E_VF_ARQLEN1_ARQVFE_MASK) {
  1731. dev_info(&adapter->pdev->dev, "ARQ VF Error detected\n");
  1732. val &= ~I40E_VF_ARQLEN1_ARQVFE_MASK;
  1733. }
  1734. if (val & I40E_VF_ARQLEN1_ARQOVFL_MASK) {
  1735. dev_info(&adapter->pdev->dev, "ARQ Overflow Error detected\n");
  1736. val &= ~I40E_VF_ARQLEN1_ARQOVFL_MASK;
  1737. }
  1738. if (val & I40E_VF_ARQLEN1_ARQCRIT_MASK) {
  1739. dev_info(&adapter->pdev->dev, "ARQ Critical Error detected\n");
  1740. val &= ~I40E_VF_ARQLEN1_ARQCRIT_MASK;
  1741. }
  1742. if (oldval != val)
  1743. wr32(hw, hw->aq.arq.len, val);
  1744. val = rd32(hw, hw->aq.asq.len);
  1745. oldval = val;
  1746. if (val & I40E_VF_ATQLEN1_ATQVFE_MASK) {
  1747. dev_info(&adapter->pdev->dev, "ASQ VF Error detected\n");
  1748. val &= ~I40E_VF_ATQLEN1_ATQVFE_MASK;
  1749. }
  1750. if (val & I40E_VF_ATQLEN1_ATQOVFL_MASK) {
  1751. dev_info(&adapter->pdev->dev, "ASQ Overflow Error detected\n");
  1752. val &= ~I40E_VF_ATQLEN1_ATQOVFL_MASK;
  1753. }
  1754. if (val & I40E_VF_ATQLEN1_ATQCRIT_MASK) {
  1755. dev_info(&adapter->pdev->dev, "ASQ Critical Error detected\n");
  1756. val &= ~I40E_VF_ATQLEN1_ATQCRIT_MASK;
  1757. }
  1758. if (oldval != val)
  1759. wr32(hw, hw->aq.asq.len, val);
  1760. freedom:
  1761. kfree(event.msg_buf);
  1762. out:
  1763. /* re-enable Admin queue interrupt cause */
  1764. i40evf_misc_irq_enable(adapter);
  1765. }
  1766. /**
  1767. * i40evf_client_task - worker thread to perform client work
  1768. * @work: pointer to work_struct containing our data
  1769. *
  1770. * This task handles client interactions. Because client calls can be
  1771. * reentrant, we can't handle them in the watchdog.
  1772. **/
  1773. static void i40evf_client_task(struct work_struct *work)
  1774. {
  1775. struct i40evf_adapter *adapter =
  1776. container_of(work, struct i40evf_adapter, client_task.work);
  1777. /* If we can't get the client bit, just give up. We'll be rescheduled
  1778. * later.
  1779. */
  1780. if (test_and_set_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section))
  1781. return;
  1782. if (adapter->flags & I40EVF_FLAG_SERVICE_CLIENT_REQUESTED) {
  1783. i40evf_client_subtask(adapter);
  1784. adapter->flags &= ~I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
  1785. goto out;
  1786. }
  1787. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS) {
  1788. i40evf_notify_client_l2_params(&adapter->vsi);
  1789. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_L2_PARAMS;
  1790. goto out;
  1791. }
  1792. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_CLOSE) {
  1793. i40evf_notify_client_close(&adapter->vsi, false);
  1794. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
  1795. goto out;
  1796. }
  1797. if (adapter->flags & I40EVF_FLAG_CLIENT_NEEDS_OPEN) {
  1798. i40evf_notify_client_open(&adapter->vsi);
  1799. adapter->flags &= ~I40EVF_FLAG_CLIENT_NEEDS_OPEN;
  1800. }
  1801. out:
  1802. clear_bit(__I40EVF_IN_CLIENT_TASK, &adapter->crit_section);
  1803. }
  1804. /**
  1805. * i40evf_free_all_tx_resources - Free Tx Resources for All Queues
  1806. * @adapter: board private structure
  1807. *
  1808. * Free all transmit software resources
  1809. **/
  1810. void i40evf_free_all_tx_resources(struct i40evf_adapter *adapter)
  1811. {
  1812. int i;
  1813. if (!adapter->tx_rings)
  1814. return;
  1815. for (i = 0; i < adapter->num_active_queues; i++)
  1816. if (adapter->tx_rings[i].desc)
  1817. i40evf_free_tx_resources(&adapter->tx_rings[i]);
  1818. }
  1819. /**
  1820. * i40evf_setup_all_tx_resources - allocate all queues Tx resources
  1821. * @adapter: board private structure
  1822. *
  1823. * If this function returns with an error, then it's possible one or
  1824. * more of the rings is populated (while the rest are not). It is the
  1825. * callers duty to clean those orphaned rings.
  1826. *
  1827. * Return 0 on success, negative on failure
  1828. **/
  1829. static int i40evf_setup_all_tx_resources(struct i40evf_adapter *adapter)
  1830. {
  1831. int i, err = 0;
  1832. for (i = 0; i < adapter->num_active_queues; i++) {
  1833. adapter->tx_rings[i].count = adapter->tx_desc_count;
  1834. err = i40evf_setup_tx_descriptors(&adapter->tx_rings[i]);
  1835. if (!err)
  1836. continue;
  1837. dev_err(&adapter->pdev->dev,
  1838. "Allocation for Tx Queue %u failed\n", i);
  1839. break;
  1840. }
  1841. return err;
  1842. }
  1843. /**
  1844. * i40evf_setup_all_rx_resources - allocate all queues Rx resources
  1845. * @adapter: board private structure
  1846. *
  1847. * If this function returns with an error, then it's possible one or
  1848. * more of the rings is populated (while the rest are not). It is the
  1849. * callers duty to clean those orphaned rings.
  1850. *
  1851. * Return 0 on success, negative on failure
  1852. **/
  1853. static int i40evf_setup_all_rx_resources(struct i40evf_adapter *adapter)
  1854. {
  1855. int i, err = 0;
  1856. for (i = 0; i < adapter->num_active_queues; i++) {
  1857. adapter->rx_rings[i].count = adapter->rx_desc_count;
  1858. err = i40evf_setup_rx_descriptors(&adapter->rx_rings[i]);
  1859. if (!err)
  1860. continue;
  1861. dev_err(&adapter->pdev->dev,
  1862. "Allocation for Rx Queue %u failed\n", i);
  1863. break;
  1864. }
  1865. return err;
  1866. }
  1867. /**
  1868. * i40evf_free_all_rx_resources - Free Rx Resources for All Queues
  1869. * @adapter: board private structure
  1870. *
  1871. * Free all receive software resources
  1872. **/
  1873. void i40evf_free_all_rx_resources(struct i40evf_adapter *adapter)
  1874. {
  1875. int i;
  1876. if (!adapter->rx_rings)
  1877. return;
  1878. for (i = 0; i < adapter->num_active_queues; i++)
  1879. if (adapter->rx_rings[i].desc)
  1880. i40evf_free_rx_resources(&adapter->rx_rings[i]);
  1881. }
  1882. /**
  1883. * i40evf_open - Called when a network interface is made active
  1884. * @netdev: network interface device structure
  1885. *
  1886. * Returns 0 on success, negative value on failure
  1887. *
  1888. * The open entry point is called when a network interface is made
  1889. * active by the system (IFF_UP). At this point all resources needed
  1890. * for transmit and receive operations are allocated, the interrupt
  1891. * handler is registered with the OS, the watchdog timer is started,
  1892. * and the stack is notified that the interface is ready.
  1893. **/
  1894. static int i40evf_open(struct net_device *netdev)
  1895. {
  1896. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1897. int err;
  1898. if (adapter->flags & I40EVF_FLAG_PF_COMMS_FAILED) {
  1899. dev_err(&adapter->pdev->dev, "Unable to open device due to PF driver failure.\n");
  1900. return -EIO;
  1901. }
  1902. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  1903. &adapter->crit_section))
  1904. usleep_range(500, 1000);
  1905. if (adapter->state != __I40EVF_DOWN) {
  1906. err = -EBUSY;
  1907. goto err_unlock;
  1908. }
  1909. /* allocate transmit descriptors */
  1910. err = i40evf_setup_all_tx_resources(adapter);
  1911. if (err)
  1912. goto err_setup_tx;
  1913. /* allocate receive descriptors */
  1914. err = i40evf_setup_all_rx_resources(adapter);
  1915. if (err)
  1916. goto err_setup_rx;
  1917. /* clear any pending interrupts, may auto mask */
  1918. err = i40evf_request_traffic_irqs(adapter, netdev->name);
  1919. if (err)
  1920. goto err_req_irq;
  1921. i40evf_add_filter(adapter, adapter->hw.mac.addr);
  1922. i40evf_configure(adapter);
  1923. i40evf_up_complete(adapter);
  1924. i40evf_irq_enable(adapter, true);
  1925. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1926. return 0;
  1927. err_req_irq:
  1928. i40evf_down(adapter);
  1929. i40evf_free_traffic_irqs(adapter);
  1930. err_setup_rx:
  1931. i40evf_free_all_rx_resources(adapter);
  1932. err_setup_tx:
  1933. i40evf_free_all_tx_resources(adapter);
  1934. err_unlock:
  1935. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1936. return err;
  1937. }
  1938. /**
  1939. * i40evf_close - Disables a network interface
  1940. * @netdev: network interface device structure
  1941. *
  1942. * Returns 0, this is not allowed to fail
  1943. *
  1944. * The close entry point is called when an interface is de-activated
  1945. * by the OS. The hardware is still under the drivers control, but
  1946. * needs to be disabled. All IRQs except vector 0 (reserved for admin queue)
  1947. * are freed, along with all transmit and receive resources.
  1948. **/
  1949. static int i40evf_close(struct net_device *netdev)
  1950. {
  1951. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1952. int status;
  1953. if (adapter->state <= __I40EVF_DOWN_PENDING)
  1954. return 0;
  1955. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  1956. &adapter->crit_section))
  1957. usleep_range(500, 1000);
  1958. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  1959. if (CLIENT_ENABLED(adapter))
  1960. adapter->flags |= I40EVF_FLAG_CLIENT_NEEDS_CLOSE;
  1961. i40evf_down(adapter);
  1962. adapter->state = __I40EVF_DOWN_PENDING;
  1963. i40evf_free_traffic_irqs(adapter);
  1964. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  1965. /* We explicitly don't free resources here because the hardware is
  1966. * still active and can DMA into memory. Resources are cleared in
  1967. * i40evf_virtchnl_completion() after we get confirmation from the PF
  1968. * driver that the rings have been stopped.
  1969. *
  1970. * Also, we wait for state to transition to __I40EVF_DOWN before
  1971. * returning. State change occurs in i40evf_virtchnl_completion() after
  1972. * VF resources are released (which occurs after PF driver processes and
  1973. * responds to admin queue commands).
  1974. */
  1975. status = wait_event_timeout(adapter->down_waitqueue,
  1976. adapter->state == __I40EVF_DOWN,
  1977. msecs_to_jiffies(200));
  1978. if (!status)
  1979. netdev_warn(netdev, "Device resources not yet released\n");
  1980. return 0;
  1981. }
  1982. /**
  1983. * i40evf_change_mtu - Change the Maximum Transfer Unit
  1984. * @netdev: network interface device structure
  1985. * @new_mtu: new value for maximum frame size
  1986. *
  1987. * Returns 0 on success, negative on failure
  1988. **/
  1989. static int i40evf_change_mtu(struct net_device *netdev, int new_mtu)
  1990. {
  1991. struct i40evf_adapter *adapter = netdev_priv(netdev);
  1992. netdev->mtu = new_mtu;
  1993. if (CLIENT_ENABLED(adapter)) {
  1994. i40evf_notify_client_l2_params(&adapter->vsi);
  1995. adapter->flags |= I40EVF_FLAG_SERVICE_CLIENT_REQUESTED;
  1996. }
  1997. adapter->flags |= I40EVF_FLAG_RESET_NEEDED;
  1998. schedule_work(&adapter->reset_task);
  1999. return 0;
  2000. }
  2001. /**
  2002. * i40e_set_features - set the netdev feature flags
  2003. * @netdev: ptr to the netdev being adjusted
  2004. * @features: the feature set that the stack is suggesting
  2005. * Note: expects to be called while under rtnl_lock()
  2006. **/
  2007. static int i40evf_set_features(struct net_device *netdev,
  2008. netdev_features_t features)
  2009. {
  2010. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2011. /* Don't allow changing VLAN_RX flag when VLAN is set for VF
  2012. * and return an error in this case
  2013. */
  2014. if (VLAN_ALLOWED(adapter)) {
  2015. if (features & NETIF_F_HW_VLAN_CTAG_RX)
  2016. adapter->aq_required |=
  2017. I40EVF_FLAG_AQ_ENABLE_VLAN_STRIPPING;
  2018. else
  2019. adapter->aq_required |=
  2020. I40EVF_FLAG_AQ_DISABLE_VLAN_STRIPPING;
  2021. } else if ((netdev->features ^ features) & NETIF_F_HW_VLAN_CTAG_RX) {
  2022. return -EINVAL;
  2023. }
  2024. return 0;
  2025. }
  2026. /**
  2027. * i40evf_features_check - Validate encapsulated packet conforms to limits
  2028. * @skb: skb buff
  2029. * @netdev: This physical port's netdev
  2030. * @features: Offload features that the stack believes apply
  2031. **/
  2032. static netdev_features_t i40evf_features_check(struct sk_buff *skb,
  2033. struct net_device *dev,
  2034. netdev_features_t features)
  2035. {
  2036. size_t len;
  2037. /* No point in doing any of this if neither checksum nor GSO are
  2038. * being requested for this frame. We can rule out both by just
  2039. * checking for CHECKSUM_PARTIAL
  2040. */
  2041. if (skb->ip_summed != CHECKSUM_PARTIAL)
  2042. return features;
  2043. /* We cannot support GSO if the MSS is going to be less than
  2044. * 64 bytes. If it is then we need to drop support for GSO.
  2045. */
  2046. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
  2047. features &= ~NETIF_F_GSO_MASK;
  2048. /* MACLEN can support at most 63 words */
  2049. len = skb_network_header(skb) - skb->data;
  2050. if (len & ~(63 * 2))
  2051. goto out_err;
  2052. /* IPLEN and EIPLEN can support at most 127 dwords */
  2053. len = skb_transport_header(skb) - skb_network_header(skb);
  2054. if (len & ~(127 * 4))
  2055. goto out_err;
  2056. if (skb->encapsulation) {
  2057. /* L4TUNLEN can support 127 words */
  2058. len = skb_inner_network_header(skb) - skb_transport_header(skb);
  2059. if (len & ~(127 * 2))
  2060. goto out_err;
  2061. /* IPLEN can support at most 127 dwords */
  2062. len = skb_inner_transport_header(skb) -
  2063. skb_inner_network_header(skb);
  2064. if (len & ~(127 * 4))
  2065. goto out_err;
  2066. }
  2067. /* No need to validate L4LEN as TCP is the only protocol with a
  2068. * a flexible value and we support all possible values supported
  2069. * by TCP, which is at most 15 dwords
  2070. */
  2071. return features;
  2072. out_err:
  2073. return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
  2074. }
  2075. /**
  2076. * i40evf_fix_features - fix up the netdev feature bits
  2077. * @netdev: our net device
  2078. * @features: desired feature bits
  2079. *
  2080. * Returns fixed-up features bits
  2081. **/
  2082. static netdev_features_t i40evf_fix_features(struct net_device *netdev,
  2083. netdev_features_t features)
  2084. {
  2085. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2086. if (!(adapter->vf_res->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN))
  2087. features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
  2088. NETIF_F_HW_VLAN_CTAG_RX |
  2089. NETIF_F_HW_VLAN_CTAG_FILTER);
  2090. return features;
  2091. }
  2092. static const struct net_device_ops i40evf_netdev_ops = {
  2093. .ndo_open = i40evf_open,
  2094. .ndo_stop = i40evf_close,
  2095. .ndo_start_xmit = i40evf_xmit_frame,
  2096. .ndo_set_rx_mode = i40evf_set_rx_mode,
  2097. .ndo_validate_addr = eth_validate_addr,
  2098. .ndo_set_mac_address = i40evf_set_mac,
  2099. .ndo_change_mtu = i40evf_change_mtu,
  2100. .ndo_tx_timeout = i40evf_tx_timeout,
  2101. .ndo_vlan_rx_add_vid = i40evf_vlan_rx_add_vid,
  2102. .ndo_vlan_rx_kill_vid = i40evf_vlan_rx_kill_vid,
  2103. .ndo_features_check = i40evf_features_check,
  2104. .ndo_fix_features = i40evf_fix_features,
  2105. .ndo_set_features = i40evf_set_features,
  2106. #ifdef CONFIG_NET_POLL_CONTROLLER
  2107. .ndo_poll_controller = i40evf_netpoll,
  2108. #endif
  2109. };
  2110. /**
  2111. * i40evf_check_reset_complete - check that VF reset is complete
  2112. * @hw: pointer to hw struct
  2113. *
  2114. * Returns 0 if device is ready to use, or -EBUSY if it's in reset.
  2115. **/
  2116. static int i40evf_check_reset_complete(struct i40e_hw *hw)
  2117. {
  2118. u32 rstat;
  2119. int i;
  2120. for (i = 0; i < 100; i++) {
  2121. rstat = rd32(hw, I40E_VFGEN_RSTAT) &
  2122. I40E_VFGEN_RSTAT_VFR_STATE_MASK;
  2123. if ((rstat == VIRTCHNL_VFR_VFACTIVE) ||
  2124. (rstat == VIRTCHNL_VFR_COMPLETED))
  2125. return 0;
  2126. usleep_range(10, 20);
  2127. }
  2128. return -EBUSY;
  2129. }
  2130. /**
  2131. * i40evf_process_config - Process the config information we got from the PF
  2132. * @adapter: board private structure
  2133. *
  2134. * Verify that we have a valid config struct, and set up our netdev features
  2135. * and our VSI struct.
  2136. **/
  2137. int i40evf_process_config(struct i40evf_adapter *adapter)
  2138. {
  2139. struct virtchnl_vf_resource *vfres = adapter->vf_res;
  2140. int i, num_req_queues = adapter->num_req_queues;
  2141. struct net_device *netdev = adapter->netdev;
  2142. struct i40e_vsi *vsi = &adapter->vsi;
  2143. netdev_features_t hw_enc_features;
  2144. netdev_features_t hw_features;
  2145. /* got VF config message back from PF, now we can parse it */
  2146. for (i = 0; i < vfres->num_vsis; i++) {
  2147. if (vfres->vsi_res[i].vsi_type == VIRTCHNL_VSI_SRIOV)
  2148. adapter->vsi_res = &vfres->vsi_res[i];
  2149. }
  2150. if (!adapter->vsi_res) {
  2151. dev_err(&adapter->pdev->dev, "No LAN VSI found\n");
  2152. return -ENODEV;
  2153. }
  2154. if (num_req_queues &&
  2155. num_req_queues != adapter->vsi_res->num_queue_pairs) {
  2156. /* Problem. The PF gave us fewer queues than what we had
  2157. * negotiated in our request. Need a reset to see if we can't
  2158. * get back to a working state.
  2159. */
  2160. dev_err(&adapter->pdev->dev,
  2161. "Requested %d queues, but PF only gave us %d.\n",
  2162. num_req_queues,
  2163. adapter->vsi_res->num_queue_pairs);
  2164. adapter->flags |= I40EVF_FLAG_REINIT_ITR_NEEDED;
  2165. adapter->num_req_queues = adapter->vsi_res->num_queue_pairs;
  2166. i40evf_schedule_reset(adapter);
  2167. return -ENODEV;
  2168. }
  2169. adapter->num_req_queues = 0;
  2170. hw_enc_features = NETIF_F_SG |
  2171. NETIF_F_IP_CSUM |
  2172. NETIF_F_IPV6_CSUM |
  2173. NETIF_F_HIGHDMA |
  2174. NETIF_F_SOFT_FEATURES |
  2175. NETIF_F_TSO |
  2176. NETIF_F_TSO_ECN |
  2177. NETIF_F_TSO6 |
  2178. NETIF_F_SCTP_CRC |
  2179. NETIF_F_RXHASH |
  2180. NETIF_F_RXCSUM |
  2181. 0;
  2182. /* advertise to stack only if offloads for encapsulated packets is
  2183. * supported
  2184. */
  2185. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_ENCAP) {
  2186. hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL |
  2187. NETIF_F_GSO_GRE |
  2188. NETIF_F_GSO_GRE_CSUM |
  2189. NETIF_F_GSO_IPXIP4 |
  2190. NETIF_F_GSO_IPXIP6 |
  2191. NETIF_F_GSO_UDP_TUNNEL_CSUM |
  2192. NETIF_F_GSO_PARTIAL |
  2193. 0;
  2194. if (!(vfres->vf_cap_flags &
  2195. VIRTCHNL_VF_OFFLOAD_ENCAP_CSUM))
  2196. netdev->gso_partial_features |=
  2197. NETIF_F_GSO_UDP_TUNNEL_CSUM;
  2198. netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
  2199. netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
  2200. netdev->hw_enc_features |= hw_enc_features;
  2201. }
  2202. /* record features VLANs can make use of */
  2203. netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
  2204. /* Write features and hw_features separately to avoid polluting
  2205. * with, or dropping, features that are set when we registered.
  2206. */
  2207. hw_features = hw_enc_features;
  2208. /* Enable VLAN features if supported */
  2209. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
  2210. hw_features |= (NETIF_F_HW_VLAN_CTAG_TX |
  2211. NETIF_F_HW_VLAN_CTAG_RX);
  2212. netdev->hw_features |= hw_features;
  2213. netdev->features |= hw_features;
  2214. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_VLAN)
  2215. netdev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
  2216. adapter->vsi.id = adapter->vsi_res->vsi_id;
  2217. adapter->vsi.back = adapter;
  2218. adapter->vsi.base_vector = 1;
  2219. adapter->vsi.work_limit = I40E_DEFAULT_IRQ_WORK;
  2220. vsi->netdev = adapter->netdev;
  2221. vsi->qs_handle = adapter->vsi_res->qset_handle;
  2222. if (vfres->vf_cap_flags & VIRTCHNL_VF_OFFLOAD_RSS_PF) {
  2223. adapter->rss_key_size = vfres->rss_key_size;
  2224. adapter->rss_lut_size = vfres->rss_lut_size;
  2225. } else {
  2226. adapter->rss_key_size = I40EVF_HKEY_ARRAY_SIZE;
  2227. adapter->rss_lut_size = I40EVF_HLUT_ARRAY_SIZE;
  2228. }
  2229. return 0;
  2230. }
  2231. /**
  2232. * i40evf_init_task - worker thread to perform delayed initialization
  2233. * @work: pointer to work_struct containing our data
  2234. *
  2235. * This task completes the work that was begun in probe. Due to the nature
  2236. * of VF-PF communications, we may need to wait tens of milliseconds to get
  2237. * responses back from the PF. Rather than busy-wait in probe and bog down the
  2238. * whole system, we'll do it in a task so we can sleep.
  2239. * This task only runs during driver init. Once we've established
  2240. * communications with the PF driver and set up our netdev, the watchdog
  2241. * takes over.
  2242. **/
  2243. static void i40evf_init_task(struct work_struct *work)
  2244. {
  2245. struct i40evf_adapter *adapter = container_of(work,
  2246. struct i40evf_adapter,
  2247. init_task.work);
  2248. struct net_device *netdev = adapter->netdev;
  2249. struct i40e_hw *hw = &adapter->hw;
  2250. struct pci_dev *pdev = adapter->pdev;
  2251. int err, bufsz;
  2252. switch (adapter->state) {
  2253. case __I40EVF_STARTUP:
  2254. /* driver loaded, probe complete */
  2255. adapter->flags &= ~I40EVF_FLAG_PF_COMMS_FAILED;
  2256. adapter->flags &= ~I40EVF_FLAG_RESET_PENDING;
  2257. err = i40e_set_mac_type(hw);
  2258. if (err) {
  2259. dev_err(&pdev->dev, "Failed to set MAC type (%d)\n",
  2260. err);
  2261. goto err;
  2262. }
  2263. err = i40evf_check_reset_complete(hw);
  2264. if (err) {
  2265. dev_info(&pdev->dev, "Device is still in reset (%d), retrying\n",
  2266. err);
  2267. goto err;
  2268. }
  2269. hw->aq.num_arq_entries = I40EVF_AQ_LEN;
  2270. hw->aq.num_asq_entries = I40EVF_AQ_LEN;
  2271. hw->aq.arq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
  2272. hw->aq.asq_buf_size = I40EVF_MAX_AQ_BUF_SIZE;
  2273. err = i40evf_init_adminq(hw);
  2274. if (err) {
  2275. dev_err(&pdev->dev, "Failed to init Admin Queue (%d)\n",
  2276. err);
  2277. goto err;
  2278. }
  2279. err = i40evf_send_api_ver(adapter);
  2280. if (err) {
  2281. dev_err(&pdev->dev, "Unable to send to PF (%d)\n", err);
  2282. i40evf_shutdown_adminq(hw);
  2283. goto err;
  2284. }
  2285. adapter->state = __I40EVF_INIT_VERSION_CHECK;
  2286. goto restart;
  2287. case __I40EVF_INIT_VERSION_CHECK:
  2288. if (!i40evf_asq_done(hw)) {
  2289. dev_err(&pdev->dev, "Admin queue command never completed\n");
  2290. i40evf_shutdown_adminq(hw);
  2291. adapter->state = __I40EVF_STARTUP;
  2292. goto err;
  2293. }
  2294. /* aq msg sent, awaiting reply */
  2295. err = i40evf_verify_api_ver(adapter);
  2296. if (err) {
  2297. if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK)
  2298. err = i40evf_send_api_ver(adapter);
  2299. else
  2300. dev_err(&pdev->dev, "Unsupported PF API version %d.%d, expected %d.%d\n",
  2301. adapter->pf_version.major,
  2302. adapter->pf_version.minor,
  2303. VIRTCHNL_VERSION_MAJOR,
  2304. VIRTCHNL_VERSION_MINOR);
  2305. goto err;
  2306. }
  2307. err = i40evf_send_vf_config_msg(adapter);
  2308. if (err) {
  2309. dev_err(&pdev->dev, "Unable to send config request (%d)\n",
  2310. err);
  2311. goto err;
  2312. }
  2313. adapter->state = __I40EVF_INIT_GET_RESOURCES;
  2314. goto restart;
  2315. case __I40EVF_INIT_GET_RESOURCES:
  2316. /* aq msg sent, awaiting reply */
  2317. if (!adapter->vf_res) {
  2318. bufsz = sizeof(struct virtchnl_vf_resource) +
  2319. (I40E_MAX_VF_VSI *
  2320. sizeof(struct virtchnl_vsi_resource));
  2321. adapter->vf_res = kzalloc(bufsz, GFP_KERNEL);
  2322. if (!adapter->vf_res)
  2323. goto err;
  2324. }
  2325. err = i40evf_get_vf_config(adapter);
  2326. if (err == I40E_ERR_ADMIN_QUEUE_NO_WORK) {
  2327. err = i40evf_send_vf_config_msg(adapter);
  2328. goto err;
  2329. } else if (err == I40E_ERR_PARAM) {
  2330. /* We only get ERR_PARAM if the device is in a very bad
  2331. * state or if we've been disabled for previous bad
  2332. * behavior. Either way, we're done now.
  2333. */
  2334. i40evf_shutdown_adminq(hw);
  2335. dev_err(&pdev->dev, "Unable to get VF config due to PF error condition, not retrying\n");
  2336. return;
  2337. }
  2338. if (err) {
  2339. dev_err(&pdev->dev, "Unable to get VF config (%d)\n",
  2340. err);
  2341. goto err_alloc;
  2342. }
  2343. adapter->state = __I40EVF_INIT_SW;
  2344. break;
  2345. default:
  2346. goto err_alloc;
  2347. }
  2348. if (i40evf_process_config(adapter))
  2349. goto err_alloc;
  2350. adapter->current_op = VIRTCHNL_OP_UNKNOWN;
  2351. adapter->flags |= I40EVF_FLAG_RX_CSUM_ENABLED;
  2352. netdev->netdev_ops = &i40evf_netdev_ops;
  2353. i40evf_set_ethtool_ops(netdev);
  2354. netdev->watchdog_timeo = 5 * HZ;
  2355. /* MTU range: 68 - 9710 */
  2356. netdev->min_mtu = ETH_MIN_MTU;
  2357. netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
  2358. if (!is_valid_ether_addr(adapter->hw.mac.addr)) {
  2359. dev_info(&pdev->dev, "Invalid MAC address %pM, using random\n",
  2360. adapter->hw.mac.addr);
  2361. eth_hw_addr_random(netdev);
  2362. ether_addr_copy(adapter->hw.mac.addr, netdev->dev_addr);
  2363. } else {
  2364. adapter->flags |= I40EVF_FLAG_ADDR_SET_BY_PF;
  2365. ether_addr_copy(netdev->dev_addr, adapter->hw.mac.addr);
  2366. ether_addr_copy(netdev->perm_addr, adapter->hw.mac.addr);
  2367. }
  2368. timer_setup(&adapter->watchdog_timer, i40evf_watchdog_timer, 0);
  2369. mod_timer(&adapter->watchdog_timer, jiffies + 1);
  2370. adapter->tx_desc_count = I40EVF_DEFAULT_TXD;
  2371. adapter->rx_desc_count = I40EVF_DEFAULT_RXD;
  2372. err = i40evf_init_interrupt_scheme(adapter);
  2373. if (err)
  2374. goto err_sw_init;
  2375. i40evf_map_rings_to_vectors(adapter);
  2376. if (adapter->vf_res->vf_cap_flags &
  2377. VIRTCHNL_VF_OFFLOAD_WB_ON_ITR)
  2378. adapter->flags |= I40EVF_FLAG_WB_ON_ITR_CAPABLE;
  2379. err = i40evf_request_misc_irq(adapter);
  2380. if (err)
  2381. goto err_sw_init;
  2382. netif_carrier_off(netdev);
  2383. adapter->link_up = false;
  2384. if (!adapter->netdev_registered) {
  2385. err = register_netdev(netdev);
  2386. if (err)
  2387. goto err_register;
  2388. }
  2389. adapter->netdev_registered = true;
  2390. netif_tx_stop_all_queues(netdev);
  2391. if (CLIENT_ALLOWED(adapter)) {
  2392. err = i40evf_lan_add_device(adapter);
  2393. if (err)
  2394. dev_info(&pdev->dev, "Failed to add VF to client API service list: %d\n",
  2395. err);
  2396. }
  2397. dev_info(&pdev->dev, "MAC address: %pM\n", adapter->hw.mac.addr);
  2398. if (netdev->features & NETIF_F_GRO)
  2399. dev_info(&pdev->dev, "GRO is enabled\n");
  2400. adapter->state = __I40EVF_DOWN;
  2401. set_bit(__I40E_VSI_DOWN, adapter->vsi.state);
  2402. i40evf_misc_irq_enable(adapter);
  2403. wake_up(&adapter->down_waitqueue);
  2404. adapter->rss_key = kzalloc(adapter->rss_key_size, GFP_KERNEL);
  2405. adapter->rss_lut = kzalloc(adapter->rss_lut_size, GFP_KERNEL);
  2406. if (!adapter->rss_key || !adapter->rss_lut)
  2407. goto err_mem;
  2408. if (RSS_AQ(adapter)) {
  2409. adapter->aq_required |= I40EVF_FLAG_AQ_CONFIGURE_RSS;
  2410. mod_timer_pending(&adapter->watchdog_timer, jiffies + 1);
  2411. } else {
  2412. i40evf_init_rss(adapter);
  2413. }
  2414. return;
  2415. restart:
  2416. schedule_delayed_work(&adapter->init_task, msecs_to_jiffies(30));
  2417. return;
  2418. err_mem:
  2419. i40evf_free_rss(adapter);
  2420. err_register:
  2421. i40evf_free_misc_irq(adapter);
  2422. err_sw_init:
  2423. i40evf_reset_interrupt_capability(adapter);
  2424. err_alloc:
  2425. kfree(adapter->vf_res);
  2426. adapter->vf_res = NULL;
  2427. err:
  2428. /* Things went into the weeds, so try again later */
  2429. if (++adapter->aq_wait_count > I40EVF_AQ_MAX_ERR) {
  2430. dev_err(&pdev->dev, "Failed to communicate with PF; waiting before retry\n");
  2431. adapter->flags |= I40EVF_FLAG_PF_COMMS_FAILED;
  2432. i40evf_shutdown_adminq(hw);
  2433. adapter->state = __I40EVF_STARTUP;
  2434. schedule_delayed_work(&adapter->init_task, HZ * 5);
  2435. return;
  2436. }
  2437. schedule_delayed_work(&adapter->init_task, HZ);
  2438. }
  2439. /**
  2440. * i40evf_shutdown - Shutdown the device in preparation for a reboot
  2441. * @pdev: pci device structure
  2442. **/
  2443. static void i40evf_shutdown(struct pci_dev *pdev)
  2444. {
  2445. struct net_device *netdev = pci_get_drvdata(pdev);
  2446. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2447. netif_device_detach(netdev);
  2448. if (netif_running(netdev))
  2449. i40evf_close(netdev);
  2450. /* Prevent the watchdog from running. */
  2451. adapter->state = __I40EVF_REMOVE;
  2452. adapter->aq_required = 0;
  2453. #ifdef CONFIG_PM
  2454. pci_save_state(pdev);
  2455. #endif
  2456. pci_disable_device(pdev);
  2457. }
  2458. /**
  2459. * i40evf_probe - Device Initialization Routine
  2460. * @pdev: PCI device information struct
  2461. * @ent: entry in i40evf_pci_tbl
  2462. *
  2463. * Returns 0 on success, negative on failure
  2464. *
  2465. * i40evf_probe initializes an adapter identified by a pci_dev structure.
  2466. * The OS initialization, configuring of the adapter private structure,
  2467. * and a hardware reset occur.
  2468. **/
  2469. static int i40evf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  2470. {
  2471. struct net_device *netdev;
  2472. struct i40evf_adapter *adapter = NULL;
  2473. struct i40e_hw *hw = NULL;
  2474. int err;
  2475. err = pci_enable_device(pdev);
  2476. if (err)
  2477. return err;
  2478. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
  2479. if (err) {
  2480. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  2481. if (err) {
  2482. dev_err(&pdev->dev,
  2483. "DMA configuration failed: 0x%x\n", err);
  2484. goto err_dma;
  2485. }
  2486. }
  2487. err = pci_request_regions(pdev, i40evf_driver_name);
  2488. if (err) {
  2489. dev_err(&pdev->dev,
  2490. "pci_request_regions failed 0x%x\n", err);
  2491. goto err_pci_reg;
  2492. }
  2493. pci_enable_pcie_error_reporting(pdev);
  2494. pci_set_master(pdev);
  2495. netdev = alloc_etherdev_mq(sizeof(struct i40evf_adapter), MAX_QUEUES);
  2496. if (!netdev) {
  2497. err = -ENOMEM;
  2498. goto err_alloc_etherdev;
  2499. }
  2500. SET_NETDEV_DEV(netdev, &pdev->dev);
  2501. pci_set_drvdata(pdev, netdev);
  2502. adapter = netdev_priv(netdev);
  2503. adapter->netdev = netdev;
  2504. adapter->pdev = pdev;
  2505. hw = &adapter->hw;
  2506. hw->back = adapter;
  2507. adapter->msg_enable = BIT(DEFAULT_DEBUG_LEVEL_SHIFT) - 1;
  2508. adapter->state = __I40EVF_STARTUP;
  2509. /* Call save state here because it relies on the adapter struct. */
  2510. pci_save_state(pdev);
  2511. hw->hw_addr = ioremap(pci_resource_start(pdev, 0),
  2512. pci_resource_len(pdev, 0));
  2513. if (!hw->hw_addr) {
  2514. err = -EIO;
  2515. goto err_ioremap;
  2516. }
  2517. hw->vendor_id = pdev->vendor;
  2518. hw->device_id = pdev->device;
  2519. pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
  2520. hw->subsystem_vendor_id = pdev->subsystem_vendor;
  2521. hw->subsystem_device_id = pdev->subsystem_device;
  2522. hw->bus.device = PCI_SLOT(pdev->devfn);
  2523. hw->bus.func = PCI_FUNC(pdev->devfn);
  2524. hw->bus.bus_id = pdev->bus->number;
  2525. /* set up the locks for the AQ, do this only once in probe
  2526. * and destroy them only once in remove
  2527. */
  2528. mutex_init(&hw->aq.asq_mutex);
  2529. mutex_init(&hw->aq.arq_mutex);
  2530. spin_lock_init(&adapter->mac_vlan_list_lock);
  2531. INIT_LIST_HEAD(&adapter->mac_filter_list);
  2532. INIT_LIST_HEAD(&adapter->vlan_filter_list);
  2533. INIT_WORK(&adapter->reset_task, i40evf_reset_task);
  2534. INIT_WORK(&adapter->adminq_task, i40evf_adminq_task);
  2535. INIT_WORK(&adapter->watchdog_task, i40evf_watchdog_task);
  2536. INIT_DELAYED_WORK(&adapter->client_task, i40evf_client_task);
  2537. INIT_DELAYED_WORK(&adapter->init_task, i40evf_init_task);
  2538. schedule_delayed_work(&adapter->init_task,
  2539. msecs_to_jiffies(5 * (pdev->devfn & 0x07)));
  2540. /* Setup the wait queue for indicating transition to down status */
  2541. init_waitqueue_head(&adapter->down_waitqueue);
  2542. return 0;
  2543. err_ioremap:
  2544. free_netdev(netdev);
  2545. err_alloc_etherdev:
  2546. pci_release_regions(pdev);
  2547. err_pci_reg:
  2548. err_dma:
  2549. pci_disable_device(pdev);
  2550. return err;
  2551. }
  2552. #ifdef CONFIG_PM
  2553. /**
  2554. * i40evf_suspend - Power management suspend routine
  2555. * @pdev: PCI device information struct
  2556. * @state: unused
  2557. *
  2558. * Called when the system (VM) is entering sleep/suspend.
  2559. **/
  2560. static int i40evf_suspend(struct pci_dev *pdev, pm_message_t state)
  2561. {
  2562. struct net_device *netdev = pci_get_drvdata(pdev);
  2563. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2564. int retval = 0;
  2565. netif_device_detach(netdev);
  2566. while (test_and_set_bit(__I40EVF_IN_CRITICAL_TASK,
  2567. &adapter->crit_section))
  2568. usleep_range(500, 1000);
  2569. if (netif_running(netdev)) {
  2570. rtnl_lock();
  2571. i40evf_down(adapter);
  2572. rtnl_unlock();
  2573. }
  2574. i40evf_free_misc_irq(adapter);
  2575. i40evf_reset_interrupt_capability(adapter);
  2576. clear_bit(__I40EVF_IN_CRITICAL_TASK, &adapter->crit_section);
  2577. retval = pci_save_state(pdev);
  2578. if (retval)
  2579. return retval;
  2580. pci_disable_device(pdev);
  2581. return 0;
  2582. }
  2583. /**
  2584. * i40evf_resume - Power management resume routine
  2585. * @pdev: PCI device information struct
  2586. *
  2587. * Called when the system (VM) is resumed from sleep/suspend.
  2588. **/
  2589. static int i40evf_resume(struct pci_dev *pdev)
  2590. {
  2591. struct i40evf_adapter *adapter = pci_get_drvdata(pdev);
  2592. struct net_device *netdev = adapter->netdev;
  2593. u32 err;
  2594. pci_set_power_state(pdev, PCI_D0);
  2595. pci_restore_state(pdev);
  2596. /* pci_restore_state clears dev->state_saved so call
  2597. * pci_save_state to restore it.
  2598. */
  2599. pci_save_state(pdev);
  2600. err = pci_enable_device_mem(pdev);
  2601. if (err) {
  2602. dev_err(&pdev->dev, "Cannot enable PCI device from suspend.\n");
  2603. return err;
  2604. }
  2605. pci_set_master(pdev);
  2606. rtnl_lock();
  2607. err = i40evf_set_interrupt_capability(adapter);
  2608. if (err) {
  2609. rtnl_unlock();
  2610. dev_err(&pdev->dev, "Cannot enable MSI-X interrupts.\n");
  2611. return err;
  2612. }
  2613. err = i40evf_request_misc_irq(adapter);
  2614. rtnl_unlock();
  2615. if (err) {
  2616. dev_err(&pdev->dev, "Cannot get interrupt vector.\n");
  2617. return err;
  2618. }
  2619. schedule_work(&adapter->reset_task);
  2620. netif_device_attach(netdev);
  2621. return err;
  2622. }
  2623. #endif /* CONFIG_PM */
  2624. /**
  2625. * i40evf_remove - Device Removal Routine
  2626. * @pdev: PCI device information struct
  2627. *
  2628. * i40evf_remove is called by the PCI subsystem to alert the driver
  2629. * that it should release a PCI device. The could be caused by a
  2630. * Hot-Plug event, or because the driver is going to be removed from
  2631. * memory.
  2632. **/
  2633. static void i40evf_remove(struct pci_dev *pdev)
  2634. {
  2635. struct net_device *netdev = pci_get_drvdata(pdev);
  2636. struct i40evf_adapter *adapter = netdev_priv(netdev);
  2637. struct i40evf_mac_filter *f, *ftmp;
  2638. struct i40e_hw *hw = &adapter->hw;
  2639. int err;
  2640. /* Indicate we are in remove and not to run reset_task */
  2641. set_bit(__I40EVF_IN_REMOVE_TASK, &adapter->crit_section);
  2642. cancel_delayed_work_sync(&adapter->init_task);
  2643. cancel_work_sync(&adapter->reset_task);
  2644. cancel_delayed_work_sync(&adapter->client_task);
  2645. if (adapter->netdev_registered) {
  2646. unregister_netdev(netdev);
  2647. adapter->netdev_registered = false;
  2648. }
  2649. if (CLIENT_ALLOWED(adapter)) {
  2650. err = i40evf_lan_del_device(adapter);
  2651. if (err)
  2652. dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
  2653. err);
  2654. }
  2655. /* Shut down all the garbage mashers on the detention level */
  2656. adapter->state = __I40EVF_REMOVE;
  2657. adapter->aq_required = 0;
  2658. i40evf_request_reset(adapter);
  2659. msleep(50);
  2660. /* If the FW isn't responding, kick it once, but only once. */
  2661. if (!i40evf_asq_done(hw)) {
  2662. i40evf_request_reset(adapter);
  2663. msleep(50);
  2664. }
  2665. i40evf_free_all_tx_resources(adapter);
  2666. i40evf_free_all_rx_resources(adapter);
  2667. i40evf_misc_irq_disable(adapter);
  2668. i40evf_free_misc_irq(adapter);
  2669. i40evf_reset_interrupt_capability(adapter);
  2670. i40evf_free_q_vectors(adapter);
  2671. if (adapter->watchdog_timer.function)
  2672. del_timer_sync(&adapter->watchdog_timer);
  2673. i40evf_free_rss(adapter);
  2674. if (hw->aq.asq.count)
  2675. i40evf_shutdown_adminq(hw);
  2676. /* destroy the locks only once, here */
  2677. mutex_destroy(&hw->aq.arq_mutex);
  2678. mutex_destroy(&hw->aq.asq_mutex);
  2679. iounmap(hw->hw_addr);
  2680. pci_release_regions(pdev);
  2681. i40evf_free_all_tx_resources(adapter);
  2682. i40evf_free_all_rx_resources(adapter);
  2683. i40evf_free_queues(adapter);
  2684. kfree(adapter->vf_res);
  2685. spin_lock_bh(&adapter->mac_vlan_list_lock);
  2686. /* If we got removed before an up/down sequence, we've got a filter
  2687. * hanging out there that we need to get rid of.
  2688. */
  2689. list_for_each_entry_safe(f, ftmp, &adapter->mac_filter_list, list) {
  2690. list_del(&f->list);
  2691. kfree(f);
  2692. }
  2693. list_for_each_entry_safe(f, ftmp, &adapter->vlan_filter_list, list) {
  2694. list_del(&f->list);
  2695. kfree(f);
  2696. }
  2697. spin_unlock_bh(&adapter->mac_vlan_list_lock);
  2698. free_netdev(netdev);
  2699. pci_disable_pcie_error_reporting(pdev);
  2700. pci_disable_device(pdev);
  2701. }
  2702. static struct pci_driver i40evf_driver = {
  2703. .name = i40evf_driver_name,
  2704. .id_table = i40evf_pci_tbl,
  2705. .probe = i40evf_probe,
  2706. .remove = i40evf_remove,
  2707. #ifdef CONFIG_PM
  2708. .suspend = i40evf_suspend,
  2709. .resume = i40evf_resume,
  2710. #endif
  2711. .shutdown = i40evf_shutdown,
  2712. };
  2713. /**
  2714. * i40e_init_module - Driver Registration Routine
  2715. *
  2716. * i40e_init_module is the first routine called when the driver is
  2717. * loaded. All it does is register with the PCI subsystem.
  2718. **/
  2719. static int __init i40evf_init_module(void)
  2720. {
  2721. int ret;
  2722. pr_info("i40evf: %s - version %s\n", i40evf_driver_string,
  2723. i40evf_driver_version);
  2724. pr_info("%s\n", i40evf_copyright);
  2725. i40evf_wq = alloc_workqueue("%s", WQ_UNBOUND | WQ_MEM_RECLAIM, 1,
  2726. i40evf_driver_name);
  2727. if (!i40evf_wq) {
  2728. pr_err("%s: Failed to create workqueue\n", i40evf_driver_name);
  2729. return -ENOMEM;
  2730. }
  2731. ret = pci_register_driver(&i40evf_driver);
  2732. return ret;
  2733. }
  2734. module_init(i40evf_init_module);
  2735. /**
  2736. * i40e_exit_module - Driver Exit Cleanup Routine
  2737. *
  2738. * i40e_exit_module is called just before the driver is removed
  2739. * from memory.
  2740. **/
  2741. static void __exit i40evf_exit_module(void)
  2742. {
  2743. pci_unregister_driver(&i40evf_driver);
  2744. destroy_workqueue(i40evf_wq);
  2745. }
  2746. module_exit(i40evf_exit_module);
  2747. /* i40evf_main.c */