i40evf_main.c 110 KB

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