i40evf_main.c 109 KB

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