i40evf_main.c 108 KB

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